Skip to content

Commit

Permalink
Merge pull request #7363 from jwnimmer-tri/util-robotInterfaces
Browse files Browse the repository at this point in the history
Move non-general utilities into robotInterfaces QP controllers
  • Loading branch information
jwnimmer-tri authored Nov 1, 2017
2 parents 8c5d873 + 0db1225 commit 544e30b
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 244 deletions.
39 changes: 37 additions & 2 deletions drake/systems/controllers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ drake_cc_library(
],
)

drake_cc_library(
name = "yaml_util",
srcs = ["yaml_util.cc"],
hdrs = ["yaml_util.h"],
visibility = [],
deps = [
":qp_common",
"//drake/common:essential",
"@yaml_cpp",
],
)

drake_cc_library(
name = "polynomial_encode_decode",
srcs = ["polynomial_encode_decode.cc"],
hdrs = ["polynomial_encode_decode.h"],
deps = [
"//drake/common/trajectories:piecewise_polynomial",
"//drake/lcmtypes:piecewise_polynomial",
"//drake/lcmtypes:polynomial",
"//drake/lcmtypes:polynomial_matrix",
"//drake/util",
],
)

# This library is empty unless Gurobi is available.
drake_cc_library(
name = "instantaneous_qp_controller",
Expand All @@ -159,7 +184,9 @@ drake_cc_library(
}),
deps = [
":control_util",
":polynomial_encode_decode",
":qp_common",
":yaml_util",
"//drake/common:essential",
"//drake/common:find_resource",
"//drake/common:is_approx_equal_abstol",
Expand All @@ -171,10 +198,11 @@ drake_cc_library(
"//drake/solvers:gurobi_qp",
"//drake/util",
"//drake/util:lcm_util",
"//drake/util:yaml_util",
],
)

# === test/ ===

drake_cc_library(
name = "zmp_test_util",
testonly = 1,
Expand All @@ -185,7 +213,14 @@ drake_cc_library(
],
)

# === test/ ===
drake_cc_googletest(
name = "polynomial_encode_decode_test",
deps = [
":polynomial_encode_decode",
"//drake/common/test_utilities",
"//drake/common/trajectories:random_piecewise_polynomial",
],
)

drake_cc_googletest(
name = "inverse_dynamics_test",
Expand Down
3 changes: 2 additions & 1 deletion drake/systems/controllers/InstantaneousQPController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
#include "drake/multibody/parsers/urdf_parser.h"
#include "drake/solvers/fast_qp.h"
#include "drake/systems/controllers/controlUtil.h"
#include "drake/systems/controllers/polynomial_encode_decode.h"
#include "drake/systems/controllers/yaml_util.h"
#include "drake/util/drakeGeometryUtil.h"
#include "drake/util/lcmUtil.h"
#include "drake/util/yaml/yamlUtil.h"

const double REG = 1e-8;

Expand Down
48 changes: 48 additions & 0 deletions drake/systems/controllers/polynomial_encode_decode.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "drake/systems/controllers/polynomial_encode_decode.h"

#include <vector>

#include "drake/util/drakeUtil.h"

using Eigen::Dynamic;
using Eigen::Map;
using Eigen::VectorXd;

void encodePolynomial(const Polynomial<double>& polynomial,
// NOLINTNEXTLINE(runtime/references)
drake::lcmt_polynomial& msg) {
eigenVectorToStdVector(polynomial.GetCoefficients(), msg.coefficients);
msg.num_coefficients = polynomial.GetNumberOfCoefficients();
}

Polynomial<double> decodePolynomial(const drake::lcmt_polynomial& msg) {
Map<const VectorXd> coefficients(msg.coefficients.data(),
msg.coefficients.size());
return Polynomial<double>(coefficients);
}

void encodePiecewisePolynomial(
const PiecewisePolynomial<double>& piecewise_polynomial,
// NOLINTNEXTLINE(runtime/references)
drake::lcmt_piecewise_polynomial& msg) {
msg.num_segments = piecewise_polynomial.getNumberOfSegments();
msg.num_breaks = piecewise_polynomial.getNumberOfSegments() + 1;
msg.breaks = piecewise_polynomial.getSegmentTimes();
msg.polynomial_matrices.resize(piecewise_polynomial.getNumberOfSegments());
for (int i = 0; i < piecewise_polynomial.getNumberOfSegments(); ++i) {
encodePolynomialMatrix<Eigen::Dynamic, Eigen::Dynamic>(
piecewise_polynomial.getPolynomialMatrix(i),
msg.polynomial_matrices[i]);
}
}

PiecewisePolynomial<double> decodePiecewisePolynomial(
const drake::lcmt_piecewise_polynomial& msg) {
typedef PiecewisePolynomial<double>::PolynomialMatrix PolynomialMatrix;
std::vector<PolynomialMatrix> polynomial_matrices;
for (size_t i = 0; i < msg.polynomial_matrices.size(); ++i) {
polynomial_matrices.push_back(
decodePolynomialMatrix<Dynamic, Dynamic>(msg.polynomial_matrices[i]));
}
return PiecewisePolynomial<double>(polynomial_matrices, msg.breaks);
}
55 changes: 55 additions & 0 deletions drake/systems/controllers/polynomial_encode_decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <Eigen/Core>

#include "drake/common/trajectories/piecewise_polynomial.h"
#include "drake/lcmt_piecewise_polynomial.hpp"
#include "drake/lcmt_polynomial.hpp"
#include "drake/lcmt_polynomial_matrix.hpp"

void encodePolynomial(const Polynomial<double>& polynomial,
// NOLINTNEXTLINE(runtime/references)
drake::lcmt_polynomial& msg);

Polynomial<double> decodePolynomial(
const drake::lcmt_polynomial& msg);

template <int RowsAtCompileTime, int ColsAtCompileTime>
void encodePolynomialMatrix(
const Eigen::Matrix<Polynomial<double>, RowsAtCompileTime,
ColsAtCompileTime>& polynomial_matrix,
// NOLINTNEXTLINE(runtime/references)
drake::lcmt_polynomial_matrix& msg) {
msg.polynomials.clear();
msg.polynomials.resize(polynomial_matrix.rows());
for (int row = 0; row < polynomial_matrix.rows(); ++row) {
auto& polynomial_msg_row = msg.polynomials[row];
polynomial_msg_row.resize(polynomial_matrix.cols());
for (int col = 0; col < polynomial_matrix.cols(); ++col) {
encodePolynomial(polynomial_matrix(row, col), polynomial_msg_row[col]);
}
}
msg.rows = polynomial_matrix.rows();
msg.cols = polynomial_matrix.cols();
}

template <int RowsAtCompileTime, int ColsAtCompileTime>
Eigen::Matrix<Polynomial<double>, RowsAtCompileTime, ColsAtCompileTime>
decodePolynomialMatrix(const drake::lcmt_polynomial_matrix& msg) {
Eigen::Matrix<Polynomial<double>, RowsAtCompileTime, ColsAtCompileTime> ret(
msg.rows, msg.cols);
for (int row = 0; row < msg.rows; ++row) {
for (int col = 0; col < msg.cols; ++col) {
ret(row, col) = decodePolynomial(msg.polynomials[row][col]);
}
}
return ret;
}

void encodePiecewisePolynomial(
const PiecewisePolynomial<double>& piecewise_polynomial,
// NOLINTNEXTLINE(runtime/references)
drake::lcmt_piecewise_polynomial& msg);

PiecewisePolynomial<double> decodePiecewisePolynomial(
const drake::lcmt_piecewise_polynomial& msg);
66 changes: 66 additions & 0 deletions drake/systems/controllers/test/polynomial_encode_decode_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "drake/systems/controllers/polynomial_encode_decode.h"

#include <gtest/gtest.h>

#include "drake/common/test_utilities/random_polynomial_matrix.h"
#include "drake/common/trajectories/test/random_piecewise_polynomial.h"
#include "drake/math/random_rotation.h"

using Eigen::Dynamic;
using Eigen::VectorXd;

namespace drake {
namespace {

// TODO(jwnimmer-tri) Unit tests should not use unseeded randomness.

GTEST_TEST(TestLcmUtil, testPolynomial) {
std::default_random_engine generator;
int max_num_coefficients = 5;
std::uniform_int_distribution<> int_distribution(1, max_num_coefficients);
int num_coefficients = int_distribution(generator);
VectorXd coefficients = VectorXd::Random(num_coefficients);
Polynomial<double> poly(coefficients);
drake::lcmt_polynomial msg;
encodePolynomial(poly, msg);
auto poly_back = decodePolynomial(msg);
EXPECT_TRUE(poly.IsApprox(poly_back, 1e-8));
}

GTEST_TEST(TestLcmUtil, testPolynomialMatrix) {
auto poly_matrix = drake::test::RandomPolynomialMatrix<double>(6, 5, 8);
drake::lcmt_polynomial_matrix msg;
encodePolynomialMatrix<Eigen::Dynamic, Eigen::Dynamic>(poly_matrix, msg);
EXPECT_EQ(static_cast<int>(msg.rows), static_cast<int>(poly_matrix.rows()));
EXPECT_EQ(static_cast<int>(msg.cols), static_cast<int>(poly_matrix.cols()));
auto poly_matrix_back = decodePolynomialMatrix<Dynamic, Dynamic>(msg);
EXPECT_EQ(poly_matrix.rows(), poly_matrix_back.rows());
EXPECT_EQ(poly_matrix.cols(), poly_matrix_back.cols());
for (int row = 0; row < msg.rows; ++row) {
for (int col = 0; col < msg.cols; ++col) {
EXPECT_TRUE(
poly_matrix(row, col).IsApprox(poly_matrix_back(row, col), 1e-8));
}
}
}

GTEST_TEST(TestLcmUtil, testPiecewisePolynomial) {
std::default_random_engine generator;
int num_segments = 6;
int rows = 4;
int cols = 7;
int num_coefficients = 3;
std::vector<double> segment_times =
PiecewiseFunction::randomSegmentTimes(num_segments, generator);
PiecewisePolynomial<double> piecewise_polynomial =
test::MakeRandomPiecewisePolynomial<double>(
rows, cols, num_coefficients, segment_times);
drake::lcmt_piecewise_polynomial msg;
encodePiecewisePolynomial(piecewise_polynomial, msg);
auto piecewise_polynomial_back = decodePiecewisePolynomial(msg);
EXPECT_TRUE(piecewise_polynomial_back.isApprox(piecewise_polynomial, 1e-10));
}

} // namespace
} // namespace drake

Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "drake/util/yaml/yamlUtil.h"
#include "drake/systems/controllers/yaml_util.h"

#include <algorithm>
#include <set>
#include <utility>
#include <vector>

#include "drake/multibody/joints/floating_base_types.h"

Expand Down Expand Up @@ -69,6 +74,7 @@ YAML::Node get(const YAML::Node& parent, const std::string& key) {
}
}

// NOLINTNEXTLINE(runtime/references)
void loadBodyMotionParams(QPControllerParams& params, const YAML::Node& config,
const RigidBodyTree<double>& robot) {
for (auto body_it = robot.bodies.begin(); body_it != robot.bodies.end();
Expand All @@ -87,6 +93,7 @@ void loadBodyMotionParams(QPControllerParams& params, const YAML::Node& config,
}
}

// NOLINTNEXTLINE(runtime/references)
void loadSingleJointParams(QPControllerParams& params,
Eigen::DenseIndex position_index,
const YAML::Node& config,
Expand Down Expand Up @@ -142,6 +149,7 @@ void loadSingleJointParams(QPControllerParams& params,
get(soft_limits_config, "k_logistic").as<double>();
}

// NOLINTNEXTLINE(runtime/references)
void loadJointParams(QPControllerParams& params, const YAML::Node& config,
const RigidBodyTree<double>& robot) {
std::map<std::string, int> position_name_to_index =
Expand Down Expand Up @@ -176,6 +184,7 @@ void loadJointParams(QPControllerParams& params, const YAML::Node& config,
// }
}

// NOLINTNEXTLINE(runtime/references)
void loadSingleInputParams(QPControllerParams& params,
Eigen::DenseIndex position_index,
YAML::Node config) {
Expand Down Expand Up @@ -209,6 +218,7 @@ void loadSingleInputParams(QPControllerParams& params,
}
}

// NOLINTNEXTLINE(runtime/references)
void loadInputParams(QPControllerParams& params, const YAML::Node& config,
const RigidBodyTree<double>& robot) {
for (auto actuator_it = robot.actuators.begin();
Expand Down Expand Up @@ -244,6 +254,7 @@ void loadInputParams(QPControllerParams& params, const YAML::Node& config,
namespace YAML {
template <>
struct convert<VRefIntegratorParams> {
// NOLINTNEXTLINE(runtime/references)
static bool decode(const Node& node, VRefIntegratorParams& rhs) {
if (!node.IsMap()) {
return false;
Expand All @@ -257,6 +268,7 @@ struct convert<VRefIntegratorParams> {

template <>
struct convert<BodyMotionParams> {
// NOLINTNEXTLINE(runtime/references)
static bool decode(const Node& node, BodyMotionParams& params) {
if (!node.IsMap()) {
return false;
Expand Down Expand Up @@ -425,6 +437,7 @@ JointNames parseRobotJointNames(const YAML::Node& joint_names,
namespace YAML {
template <>
struct convert<FloatingBaseType> {
// NOLINTNEXTLINE(runtime/references)
static bool decode(const Node& node, FloatingBaseType& rhs) {
std::string joint_type = node.as<std::string>();
if (joint_type == "kFixed") {
Expand All @@ -442,6 +455,7 @@ struct convert<FloatingBaseType> {

template <>
struct convert<Attachment> {
// NOLINTNEXTLINE(runtime/references)
static bool decode(const Node& node, Attachment& rhs) {
if (!node.IsMap()) {
return false;
Expand All @@ -465,6 +479,7 @@ struct convert<Attachment> {

template <>
struct convert<KinematicModifications> {
// NOLINTNEXTLINE(runtime/references)
static bool decode(const Node& node, KinematicModifications& rhs) {
if (!node.IsMap()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <fstream>
#include <map>
#include <string>

#include "yaml-cpp/yaml.h"

Expand All @@ -25,6 +26,7 @@ drake::eigen_aligned_std_map<std::string, QPControllerParams> loadAllParamSets(
YAML::Node config, const RigidBodyTree<double>& robot);
drake::eigen_aligned_std_map<std::string, QPControllerParams> loadAllParamSets(
YAML::Node config, const RigidBodyTree<double>& robot,
// NOLINTNEXTLINE(runtime/references)
std::ofstream& debug_output_file);
RobotPropertyCache parseKinematicTreeMetadata(
const YAML::Node& metadata, const RigidBodyTree<double>& robot);
Expand Down
Loading

0 comments on commit 544e30b

Please sign in to comment.