-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMPController.cpp
More file actions
304 lines (261 loc) · 14.9 KB
/
Copy pathMPController.cpp
File metadata and controls
304 lines (261 loc) · 14.9 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// This file is part of mpc.
// mpc is free software: you can redistribute it and/or
// modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mpc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mpc. If not, see
// <http://www.gnu.org/licenses/>.
#include "MPC.h"
#include "AutoSpan.h"
#include "PreviewSystem.h"
#include "constraints.h"
#include "costFunctions.h"
#include "solverUtils.h"
#include <boost/python.hpp>
#include <memory>
#include <string>
//TODO: Add python docstring
namespace boost {
// Make boost::python understand std::shared_ptr
template <class T>
T* get_pointer(std::shared_ptr<T> p)
{
return p.get();
}
} // namespace boost
namespace mpc {
template <typename T, typename... Args>
std::shared_ptr<T> createSharedPointer(Args... args)
{
return std::make_shared<T>(args...);
}
auto NewPreviewSystem1 = &createSharedPointer<PreviewSystem>;
auto NewPreviewSystem2 = &createSharedPointer<PreviewSystem, const Eigen::MatrixXd&, const Eigen::MatrixXd&,
const Eigen::VectorXd&, const Eigen::VectorXd&, int>;
auto NewTrajectoryConstraint1 = &createSharedPointer<TrajectoryConstraint, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewTrajectoryConstraint2 = &createSharedPointer<TrajectoryConstraint, const Eigen::MatrixXd&, const Eigen::VectorXd&, bool>;
auto NewControlConstraint1 = &createSharedPointer<ControlConstraint, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewControlConstraint2 = &createSharedPointer<ControlConstraint, const Eigen::MatrixXd&, const Eigen::VectorXd&, bool>;
auto NewMixedConstraint1 = &createSharedPointer<MixedConstraint, const Eigen::MatrixXd&, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewMixedConstraint2 = &createSharedPointer<MixedConstraint, const Eigen::MatrixXd&, const Eigen::MatrixXd&, const Eigen::VectorXd&, bool>;
auto NewTrajectoryBoundConstraint = &createSharedPointer<TrajectoryBoundConstraint, const Eigen::VectorXd&, const Eigen::VectorXd&>;
auto NewControlBoundConstraint = &createSharedPointer<ControlBoundConstraint, const Eigen::VectorXd&, const Eigen::VectorXd&>;
auto NewTrajectoryCost = &createSharedPointer<TrajectoryCost, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewTargetCost = &createSharedPointer<TargetCost, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewControlCost = &createSharedPointer<ControlCost, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
auto NewMixedCost = &createSharedPointer<MixedCost, const Eigen::MatrixXd&, const Eigen::MatrixXd&, const Eigen::VectorXd&>;
} // namespace mpc
BOOST_PYTHON_MODULE(_mpc)
{
using namespace mpc;
using namespace boost::python;
def("NewPreviewSystem", NewPreviewSystem1, "Create a new instance of a PrevieSystem shared_ptr with default constructor");
def("NewPreviewSystem", NewPreviewSystem2, "Create a new instance of a PrevieSystem shared_ptr");
def("NewTrajectoryConstraint", NewTrajectoryConstraint1, "Create a new instance of a TrajectoryConstraint shared_ptr");
def("NewTrajectoryConstraint", NewTrajectoryConstraint2, "Create a new instance of a TrajectoryConstraint shared_ptr");
def("NewControlConstraint", NewControlConstraint1, "Create a new instance of a ControlConstraint shared_ptr");
def("NewControlConstraint", NewControlConstraint2, "Create a new instance of a ControlConstraint shared_ptr");
def("NewMixedConstraint", NewMixedConstraint1, "Create a new instance of a MixedConstraint shared_ptr");
def("NewMixedConstraint", NewMixedConstraint2, "Create a new instance of a MixedConstraint shared_ptr");
def("NewTrajectoryBoundConstraint", NewTrajectoryBoundConstraint, "Create a new instance of a TrajectoryBoundConstraint shared_ptr");
def("NewControlBoundConstraint", NewControlBoundConstraint, "Create a new instance of a ControlBoundConstraint shared_ptr");
def("NewTrajectoryCost", NewTrajectoryCost, "Create a new instance of a TrajectoryCost shared_ptr");
def("NewTargetCost", NewTargetCost, "Create a new instance of a TargetCost shared_ptr");
def("NewControlCost", NewControlCost, "Create a new instance of a ControlCost shared_ptr");
def("NewMixedCost", NewMixedCost, "Create a new instance of a NewMixedCost shared_ptr");
enum_<SolverFlag>("SolverFlag", "Flags to qp solver")
.value("DEFAULT", SolverFlag::DEFAULT)
#ifdef EIGEN_LSSOL_FOUND
.value("LSSOL", SolverFlag::LSSOL)
#endif
#ifdef EIGEN_GUROBI_FOUND
.value("GUROBIDense", SolverFlag::GUROBIDense)
#endif
#ifdef EIGEN_QLD_FOUND
.value("QLD", SolverFlag::QLD)
#endif
.value("QuadProgDense", SolverFlag::QuadProgDense);
// Access Solvers from python
def("pythonSolverFactory", &pythonSolverFactory, return_value_policy<manage_new_object>(), "Return a solver corresponding to the giden flag");
// Preview System
class_<PreviewSystem>("PreviewSystem", "The PreviewSystem is a read-write structure that holds all the information of the system.", no_init)
.def("system", &PreviewSystem::system)
.def("updateSystem", &PreviewSystem::updateSystem)
.def_readwrite("isUpdated", &PreviewSystem::isUpdated)
.def_readwrite("nrUStep", &PreviewSystem::nrUStep)
.def_readwrite("nrXStep", &PreviewSystem::nrXStep)
.def_readwrite("xDim", &PreviewSystem::xDim)
.def_readwrite("uDim", &PreviewSystem::uDim)
.def_readwrite("fullXDim", &PreviewSystem::fullXDim)
.def_readwrite("fullUDim", &PreviewSystem::fullUDim)
.def_readwrite("x0", &PreviewSystem::x0)
.def_readwrite("A", &PreviewSystem::A)
.def_readwrite("B", &PreviewSystem::B)
.def_readwrite("d", &PreviewSystem::d)
.def_readwrite("Phi", &PreviewSystem::Phi)
.def_readwrite("Psi", &PreviewSystem::Psi)
.def_readwrite("xi", &PreviewSystem::xi);
//AutoSpan
class_<AutoSpan, boost::noncopyable>("AutoSpan", "Helper functions to automatically extend a matrix to a desired dimension.", no_init)
.def("spanMatrix", &AutoSpan::spanMatrix).staticmethod("spanMatrix")
.def("spanVector", &AutoSpan::spanVector).staticmethod("spanVector");
//Constraint
enum_<ConstraintFlag>("ConstraintFlag", "Flag to constraint type")
.value("Constraint", ConstraintFlag::Constraint)
.value("EqualityConstraint", ConstraintFlag::EqualityConstraint)
.value("InequalityConstraint", ConstraintFlag::InequalityConstraint)
.value("BoundConstraint", ConstraintFlag::BoundConstraint);
struct ConstraintWrap : Constraint, wrapper<Constraint> {
using Constraint::Constraint;
void autoSpan()
{
this->get_override("autoSpan")();
}
void initializeConstraint(const PreviewSystem& ps)
{
this->get_override("initializeConstraint")(ps);
}
void update(const PreviewSystem& ps)
{
this->get_override("update")(ps);
}
ConstraintFlag constraintType() const noexcept
{
return this->get_override("constraintType")();
}
};
class_<ConstraintWrap, boost::noncopyable>("Constraint", no_init) // Disable the constructor because of move semantics. No need anyway.
.def("initializeConstraint", pure_virtual(&Constraint::initializeConstraint))
.def("update", pure_virtual(&Constraint::update))
.def("constraintType", pure_virtual(&Constraint::constraintType))
.def("name", &Constraint::name, return_internal_reference<>())
.def("nrConstr", &Constraint::nrConstr);
struct EqIneqConstraintWrap : EqIneqConstraint, wrapper<EqIneqConstraint> {
using EqIneqConstraint::EqIneqConstraint;
void autoSpan()
{
this->get_override("autoSpan")();
}
void initializeConstraint(const PreviewSystem& ps)
{
this->get_override("initializeConstraint")(ps);
}
void update(const PreviewSystem& ps)
{
this->get_override("update")(ps);
}
ConstraintFlag constraintType() const noexcept
{
return this->get_override("constraintType")();
}
};
class_<EqIneqConstraintWrap, boost::noncopyable, bases<Constraint> >("EqIneqConstraint", init<const std::string&, bool>())
.def("A", &EqIneqConstraint::A, return_internal_reference<>())
.def("b", &EqIneqConstraint::b, return_internal_reference<>());
// Delete constructor to enforce call of New<Name_of_constraint> function that return a shared_ptr.
class_<TrajectoryConstraint, boost::noncopyable, bases<EqIneqConstraint> >("TrajectoryConstraint", "Trajectory constraint. The object is instansiable through a NewTrajectoryConstraint function", no_init);
class_<ControlConstraint, boost::noncopyable, bases<EqIneqConstraint> >("ControlConstraint", "Control constraint. The object is instansiable through a NewControlConstraint function", no_init);
class_<MixedConstraint, boost::noncopyable, bases<EqIneqConstraint> >("MixedConstraint", "Mixed constraint. The object is instansiable through a NewMixedConstraint function", no_init);
class_<TrajectoryBoundConstraint, boost::noncopyable, bases<EqIneqConstraint> >("TrajectoryBoundConstraint", "Trajectory Bound constraint. The object is instansiable through a NewTrajectoryBoundConstraint function", no_init);
class_<ControlBoundConstraint, boost::noncopyable, bases<Constraint> >("ControlBoundConstraint", "Control Bound constraint. The object is instansiable through a NewControlBoundConstraint function", no_init)
.def("lower", &ControlBoundConstraint::lower, return_internal_reference<>())
.def("upper", &ControlBoundConstraint::upper, return_internal_reference<>());
//Cost Functions
struct CostFunctionWrap : CostFunction, wrapper<CostFunction> {
using CostFunction::CostFunction;
void autoSpan()
{
if (override autoSpan = this->get_override("autoSpan"))
autoSpan();
else
CostFunction::autoSpan();
}
void default_autoSpan()
{
this->get_override("autoSpan");
}
void initializeCost(const PreviewSystem& ps)
{
if (override initializeConstraint = this->get_override("initializeCost"))
initializeCost(ps);
else
CostFunction::initializeCost(ps);
}
void default_initializeCost(const PreviewSystem& ps)
{
this->get_override("initializeCost")(ps);
}
ConstraintFlag constraintType() const noexcept
{
return this->get_override("constraintType")();
}
void update(const PreviewSystem& ps)
{
this->get_override("update")(ps);
}
void eigenWeights(const Eigen::VectorXd& w)
{
weights(w);
}
void doubleWeight(double w)
{
weights(w);
}
};
class_<CostFunctionWrap, boost::noncopyable>("CostFunction", no_init) // Disable the constructor because of move semantics. No need anyway.
.def("initializeConstraint", &CostFunction::initializeCost, &CostFunctionWrap::default_initializeCost)
.def("update", pure_virtual(&CostFunction::update))
.def("name", &CostFunction::name, return_internal_reference<>())
.def("Q", &CostFunction::Q, return_internal_reference<>())
.def("c", &CostFunction::c, return_internal_reference<>())
.def("weights", &CostFunctionWrap::eigenWeights)
.def("weights", &CostFunctionWrap::doubleWeight);
// Delete constructor to enforce call of New<Name_of_cost> function that return a shared_ptr.
class_<TrajectoryCost, boost::noncopyable, bases<CostFunction> >("TrajectoryCost", "Trajectory cost. The object is instansiable through a NewTrajectoryCost function", no_init);
class_<TargetCost, boost::noncopyable, bases<CostFunction> >("TargetCost", "Target cost. The object is instansiable through a NewTargetCost function", no_init);
class_<ControlCost, boost::noncopyable, bases<CostFunction> >("ControlCost", "Control cost. The object is instansiable through a NewControlCost function", no_init);
class_<MixedCost, boost::noncopyable, bases<CostFunction> >("MixedCost", "Mixed cost. The object is instansiable through a NewMixedCost function", no_init);
//MPC
class_<MPC, boost::noncopyable>("MPC",
"MPC. This class runs the mpc with the desired QP and fills the PreviewSystem it is attach to", init<optional<SolverFlag> >())
.def(init<const std::shared_ptr<PreviewSystem>&, optional<SolverFlag> >())
.def("selectQPSolver", &MPC::selectQPSolver)
.def("initializeController", &MPC::initializeController)
.def("solve", &MPC::solve)
.def("solveTime", &MPC::solveTime)
.def("solveAndBuildTime", &MPC::solveAndBuildTime)
.def("control", &MPC::control, return_internal_reference<>())
.def("trajectory", &MPC::trajectory)
.def("addCost", &MPC::addConstraint)
.def("addConstraint", &MPC::addConstraint)
.def("resetConstraints", &MPC::resetConstraints);
// Register pointer
register_ptr_to_python<std::shared_ptr<ControlBoundConstraint> >();
register_ptr_to_python<std::shared_ptr<ControlConstraint> >();
register_ptr_to_python<std::shared_ptr<ControlCost> >();
register_ptr_to_python<std::shared_ptr<MixedConstraint> >();
register_ptr_to_python<std::shared_ptr<MixedCost> >();
register_ptr_to_python<std::shared_ptr<PreviewSystem> >();
register_ptr_to_python<std::shared_ptr<TargetCost> >();
register_ptr_to_python<std::shared_ptr<TrajectoryBoundConstraint> >();
register_ptr_to_python<std::shared_ptr<TrajectoryConstraint> >();
register_ptr_to_python<std::shared_ptr<TrajectoryCost> >();
// Implicit conversion of pointers
implicitly_convertible<std::shared_ptr<ControlBoundConstraint>, std::shared_ptr<Constraint> >();
implicitly_convertible<std::shared_ptr<ControlConstraint>, std::shared_ptr<Constraint> >();
implicitly_convertible<std::shared_ptr<ControlCost>, std::shared_ptr<CostFunction> >();
implicitly_convertible<std::shared_ptr<MixedConstraint>, std::shared_ptr<Constraint> >();
implicitly_convertible<std::shared_ptr<MixedCost>, std::shared_ptr<CostFunction> >();
implicitly_convertible<std::shared_ptr<TargetCost>, std::shared_ptr<CostFunction> >();
implicitly_convertible<std::shared_ptr<TrajectoryBoundConstraint>, std::shared_ptr<Constraint> >();
implicitly_convertible<std::shared_ptr<TrajectoryConstraint>, std::shared_ptr<Constraint> >();
implicitly_convertible<std::shared_ptr<TrajectoryCost>, std::shared_ptr<CostFunction> >();
}