-
Notifications
You must be signed in to change notification settings - Fork 16
/
trajectory_optimizer_py.cc
128 lines (109 loc) · 4.81 KB
/
trajectory_optimizer_py.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include "optimizer/trajectory_optimizer.h"
#include "optimizer/warm_start.h"
#include <drake/multibody/parsing/parser.h>
#include <drake/multibody/plant/multibody_plant.h>
#include <drake/multibody/plant/multibody_plant_config_functions.h>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
using drake::geometry::SceneGraph;
using drake::multibody::AddMultibodyPlant;
using drake::multibody::MultibodyPlant;
using drake::multibody::MultibodyPlantConfig;
using drake::multibody::Parser;
using drake::systems::Diagram;
using drake::systems::DiagramBuilder;
using Eigen::VectorXd;
using idto::optimizer::ProblemDefinition;
using idto::optimizer::SolverParameters;
using idto::optimizer::TrajectoryOptimizer;
using idto::optimizer::TrajectoryOptimizerSolution;
using idto::optimizer::TrajectoryOptimizerStats;
using idto::optimizer::WarmStart;
/**
* A python wrapper class for TrajectoryOptimizer. Has access to a limited
* set of methods, and is initialized from a URDF or SDF file instead of a
* Drake diagram and MultibodyPlant.
*/
class TrajectoryOptimizerPy {
public:
TrajectoryOptimizerPy(const std::string& model_file,
const ProblemDefinition& problem,
const SolverParameters& params,
const double time_step) {
DiagramBuilder<double> builder;
MultibodyPlantConfig config;
config.time_step = time_step;
std::tie(plant_, scene_graph_) = AddMultibodyPlant(config, &builder);
Parser(plant_).AddModels(model_file);
plant_->Finalize();
diagram_ = builder.Build();
// For python we create a new set of parameters, where
// q_nom_relative_to_q_init is false for all DoFs.
SolverParameters py_params = params;
py_params.q_nom_relative_to_q_init =
drake::VectorX<bool>::Zero(plant_->num_positions());
optimizer_ = std::make_unique<TrajectoryOptimizer<double>>(
diagram_.get(), plant_, problem, py_params);
}
void Solve(const std::vector<VectorXd>& q_guess,
TrajectoryOptimizerSolution<double>* solution,
TrajectoryOptimizerStats<double>* stats) {
optimizer_->Solve(q_guess, solution, stats);
}
void SolveFromWarmStart(WarmStart* warm_start,
TrajectoryOptimizerSolution<double>* solution,
TrajectoryOptimizerStats<double>* stats) {
optimizer_->SolveFromWarmStart(warm_start, solution, stats);
}
std::unique_ptr<WarmStart> MakeWarmStart(
const std::vector<VectorXd>& q_guess) const {
return std::make_unique<WarmStart>(
optimizer_->num_steps(), optimizer_->diagram(), optimizer_->plant(),
optimizer_->num_equality_constraints(), q_guess,
optimizer_->params().Delta0);
}
void ResetInitialConditions(const VectorXd& q0, const VectorXd& v0) {
optimizer_->ResetInitialConditions(q0, v0);
}
void UpdateNominalTrajectory(const std::vector<VectorXd>& q_nom,
const std::vector<VectorXd>& v_nom) {
optimizer_->UpdateNominalTrajectory(q_nom, v_nom);
}
const SolverParameters& params() const { return optimizer_->params(); }
const ProblemDefinition& prob() const { return optimizer_->prob(); }
double time_step() const { return optimizer_->time_step(); }
int num_steps() const { return optimizer_->num_steps(); }
private:
// Plant and scene graph are owned by the diagram
MultibodyPlant<double>* plant_{nullptr};
SceneGraph<double>* scene_graph_{nullptr};
std::unique_ptr<Diagram<double>> diagram_;
std::unique_ptr<TrajectoryOptimizer<double>> optimizer_;
};
PYBIND11_MODULE(trajectory_optimizer, m) {
py::class_<TrajectoryOptimizerPy>(m, "TrajectoryOptimizer")
.def(py::init<const std::string&, const ProblemDefinition&,
const SolverParameters&, const double>())
.def("time_step", &TrajectoryOptimizerPy::time_step)
.def("num_steps", &TrajectoryOptimizerPy::num_steps)
.def("Solve", &TrajectoryOptimizerPy::Solve)
.def("SolveFromWarmStart", &TrajectoryOptimizerPy::SolveFromWarmStart)
.def("MakeWarmStart", &TrajectoryOptimizerPy::MakeWarmStart)
.def("ResetInitialConditions",
&TrajectoryOptimizerPy::ResetInitialConditions)
.def("UpdateNominalTrajectory",
&TrajectoryOptimizerPy::UpdateNominalTrajectory)
.def("params", &TrajectoryOptimizerPy::params)
.def("prob", &TrajectoryOptimizerPy::prob);
py::class_<WarmStart>(m, "WarmStart")
// Warm start is not default constructible: it should be created
// in python using the TrajectoryOptimizer.MakeWarmStart method.
.def("set_q", &WarmStart::set_q)
.def("get_q", &WarmStart::get_q)
.def_readonly("Delta", &WarmStart::Delta)
.def_readonly("dq", &WarmStart::dq)
.def_readonly("dqH", &WarmStart::dqH);
}