-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmpc.cpp
More file actions
249 lines (203 loc) · 8.19 KB
/
mpc.cpp
File metadata and controls
249 lines (203 loc) · 8.19 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
#include <boost/test/unit_test.hpp>
#include "simple-mpc/centroidal-dynamics.hpp"
#include "simple-mpc/fulldynamics.hpp"
#include "simple-mpc/fwd.hpp"
#include "simple-mpc/kinodynamics.hpp"
#include "simple-mpc/mpc.hpp"
#include "simple-mpc/robot-handler.hpp"
#include "test_utils.cpp"
BOOST_AUTO_TEST_SUITE(mpc)
using namespace simple_mpc;
BOOST_AUTO_TEST_CASE(mpc_fulldynamics)
{
RobotModelHandler model_handler = getTalosModelHandler();
RobotDataHandler data_handler(model_handler);
FullDynamicsSettings settings = getFullDynamicsSettings(model_handler);
auto problem = std::make_shared<FullDynamicsOCP>(settings, model_handler);
FullDynamicsOCP & fdproblem = *problem;
const size_t T = 100;
fdproblem.createProblem(model_handler.getReferenceState(), T, 6, -settings.gravity[2], true);
MPCSettings mpc_settings;
mpc_settings.max_iters = 1;
mpc_settings.support_force = -model_handler.getMass() * settings.gravity[2];
mpc_settings.TOL = 1e-6;
mpc_settings.mu_init = 1e-8;
mpc_settings.num_threads = 1;
mpc_settings.swing_apex = 0.1;
mpc_settings.T_fly = 80;
mpc_settings.T_contact = 20;
mpc_settings.timestep = 0.01;
MPC mpc = MPC(mpc_settings, problem);
BOOST_CHECK_EQUAL(mpc.xs_.size(), T + 1);
BOOST_CHECK_EQUAL(mpc.us_.size(), T);
std::vector<std::map<std::string, bool>> contact_states;
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), false});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), false});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
mpc.generateCycleHorizon(contact_states);
BOOST_CHECK_EQUAL(mpc.foot_takeoff_times_.at("left_sole_link")[0], 170);
BOOST_CHECK_EQUAL(mpc.foot_takeoff_times_.at("right_sole_link")[0], 110);
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("left_sole_link")[0], 219);
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("right_sole_link")[0], 160);
for (std::size_t i = 0; i < 10; i++)
{
mpc.iterate(model_handler.getReferenceState());
}
BOOST_CHECK_EQUAL(mpc.foot_takeoff_times_.at("left_sole_link")[0], 160);
BOOST_CHECK_EQUAL(mpc.foot_takeoff_times_.at("right_sole_link")[0], 100);
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("left_sole_link")[0], 209);
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("right_sole_link")[0], 150);
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
Eigen::Matrix<double, Eigen::Dynamic, 3> forces = mpc.getContactForces(0);
}
BOOST_AUTO_TEST_CASE(mpc_kinodynamics)
{
RobotModelHandler model_handler = getTalosModelHandler();
RobotDataHandler data_handler(model_handler);
KinodynamicsSettings settings = getKinodynamicsSettings(model_handler);
auto problem = std::make_shared<KinodynamicsOCP>(settings, model_handler);
KinodynamicsOCP & kinoproblem = *problem;
const std::size_t T = 100;
const double support_force = -model_handler.getMass() * settings.gravity[2];
Eigen::VectorXd f1(6);
f1 << 0, 0, support_force, 0, 0, 0;
kinoproblem.createProblem(model_handler.getReferenceState(), T, 6, -settings.gravity[2], true);
MPCSettings mpc_settings;
mpc_settings.max_iters = 1;
mpc_settings.support_force = support_force;
mpc_settings.TOL = 1e-6;
mpc_settings.mu_init = 1e-8;
mpc_settings.num_threads = 8;
mpc_settings.swing_apex = 0.1;
mpc_settings.T_fly = 80;
mpc_settings.T_contact = 20;
mpc_settings.timestep = 0.01;
MPC mpc = MPC(mpc_settings, problem);
BOOST_CHECK_EQUAL(mpc.xs_.size(), T + 1);
BOOST_CHECK_EQUAL(mpc.us_.size(), T);
std::vector<std::map<std::string, bool>> contact_states;
// std::vector<std::vector<bool>> contact_states;
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), false});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), false});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
mpc.generateCycleHorizon(contact_states);
for (std::size_t i = 0; i < 10; i++)
{
mpc.iterate(model_handler.getReferenceState());
}
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
}
BOOST_AUTO_TEST_CASE(mpc_centroidal)
{
RobotModelHandler model_handler = getTalosModelHandler();
RobotDataHandler data_handler(model_handler);
CentroidalSettings settings = getCentroidalSettings();
auto problem = std::make_shared<CentroidalOCP>(settings, model_handler);
CentroidalOCP & centproblem = *problem;
std::vector<std::string> contact_names = {"left_sole_link", "right_sole_link"};
const double support_force = -model_handler.getMass() * settings.gravity[2];
const std::size_t T = 100;
Eigen::VectorXd f1(6);
f1 << 0, 0, support_force / 2., 0, 0, 0;
Eigen::VectorXd x_multibody = model_handler.getReferenceState();
centproblem.createProblem(data_handler.getCentroidalState(), T, 6, -settings.gravity[2], false);
MPCSettings mpc_settings;
mpc_settings.max_iters = 1;
mpc_settings.support_force = support_force;
mpc_settings.TOL = 1e-6;
mpc_settings.mu_init = 1e-8;
mpc_settings.num_threads = 8;
mpc_settings.swing_apex = 0.1;
mpc_settings.T_fly = 80;
mpc_settings.T_contact = 20;
mpc_settings.timestep = 0.01;
MPC mpc = MPC(mpc_settings, problem);
BOOST_CHECK_EQUAL(mpc.xs_.size(), T + 1);
BOOST_CHECK_EQUAL(mpc.us_.size(), T);
std::vector<std::map<std::string, bool>> contact_states;
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), false});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 10; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), true});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
for (std::size_t i = 0; i < 50; i++)
{
std::map<std::string, bool> contact_state;
contact_state.insert({model_handler.getFootFrameName(0), false});
contact_state.insert({model_handler.getFootFrameName(1), true});
contact_states.push_back(contact_state);
}
mpc.generateCycleHorizon(contact_states);
for (std::size_t i = 0; i < 10; i++)
{
mpc.iterate(x_multibody);
}
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
}
BOOST_AUTO_TEST_SUITE_END()