Skip to content

Commit eab2f35

Browse files
committed
Add getStateDerivative function
1 parent 2794128 commit eab2f35

File tree

7 files changed

+28
-35
lines changed

7 files changed

+28
-35
lines changed

bindings/expose-mpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace simple_mpc
9090
.def("switchToStand", &MPC::switchToStand, "self"_a)
9191
.def("getFootTakeoffCycle", &MPC::getFootTakeoffCycle, ("self"_a, "ee_name"))
9292
.def("getFootLandCycle", &MPC::getFootLandCycle, ("self"_a, "ee_name"))
93+
.def("getStateDerivative", &MPC::getStateDerivative, ("self"_a, "t"))
9394
.def("getCyclingContactState", &MPC::getCyclingContactState, ("self"_a, "t", "ee_name"))
9495
.def(
9596
"getModelHandler", &MPC::getModelHandler, "self"_a, bp::return_internal_reference<>(),

examples/go2_fulldynamics.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,8 @@
221221
end = time.time()
222222
solve_time.append(end - start)
223223

224-
a0 = (
225-
mpc.solver
226-
.workspace.problem_data.stage_data[0]
227-
.dynamics_data.continuous_data.xdot[nv:]
228-
)
229-
a1 = (
230-
mpc.solver
231-
.workspace.problem_data.stage_data[1]
232-
.dynamics_data.continuous_data.xdot[nv:]
233-
)
224+
a0 = mpc.getStateDerivative(0)[nv:]
225+
a1 = mpc.getStateDerivative(1)[nv:]
234226

235227
FL_f, FR_f, RL_f, RR_f = extract_forces(mpc.getTrajOptProblem(), mpc.solver.workspace, 0)
236228
contact_states = mpc.ocp_handler.getContactState(0)

examples/go2_kinodynamics.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,9 @@
225225
com_measured.append(mpc.getDataHandler().getData().com[0].copy())
226226
L_measured.append(mpc.getDataHandler().getData().hg.angular.copy())
227227

228-
a0 = (
229-
mpc.solver
230-
.workspace.problem_data.stage_data[0]
231-
.dynamics_data.continuous_data.xdot[nv:]
232-
)
233-
a1 = (
234-
mpc.solver
235-
.workspace.problem_data.stage_data[1]
236-
.dynamics_data.continuous_data.xdot[nv:]
237-
)
228+
a0 = mpc.getStateDerivative(0)[nv:]
229+
a1 = mpc.getStateDerivative(1)[nv:]
230+
238231
a0[6:] = mpc.us[0][nk * force_size :]
239232
a1[6:] = mpc.us[1][nk * force_size :]
240233
forces0 = mpc.us[0][: nk * force_size]

examples/talos_centroidal.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@
204204
contact_states = mpc.ocp_handler.getContactState(0)
205205
foot_ref = [mpc.getReferencePose(0, name) for name in model_handler.getFeetNames()]
206206
foot_ref_next = [mpc.getReferencePose(1, name) for name in model_handler.getFeetNames()]
207-
dH = (
208-
mpc.solver
209-
.workspace.problem_data.stage_data[0]
210-
.dynamics_data.continuous_data.xdot[3:9]
211-
)
207+
dH = mpc.getStateDerivative(0)[3:9]
212208
qp.computeDifferences(
213209
mpc.getDataHandler().getData(), x_measured, foot_ref, foot_ref_next
214210
)

examples/talos_kinodynamics.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,9 @@
210210
mpc.iterate(x_measured)
211211
end = time.time()
212212
print("MPC iterate = " + str(end - start))
213-
a0 = (
214-
mpc.solver
215-
.workspace.problem_data.stage_data[0]
216-
.dynamics_data.continuous_data.xdot[nv:]
217-
)
218-
a1 = (
219-
mpc.solver
220-
.workspace.problem_data.stage_data[1]
221-
.dynamics_data.continuous_data.xdot[nv:]
222-
)
213+
214+
a0 = mpc.getStateDerivative(0)[nv:]
215+
a1 = mpc.getStateDerivative(1)[nv:]
223216
a0[6:] = mpc.us[0][nk * force_size :]
224217
a1[6:] = mpc.us[1][nk * force_size :]
225218
forces0 = mpc.us[0][: nk * force_size]

include/simple-mpc/mpc.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
///////////////////////////////////////////////////////////////////////////////
88
#pragma once
99

10+
#include <aligator/core/stage-data.hpp>
11+
#include <aligator/fwd.hpp>
12+
#include <aligator/modelling/dynamics/fwd.hpp>
13+
#include <aligator/modelling/dynamics/integrator-explicit.hpp>
1014
#include <aligator/solvers/proxddp/solver-proxddp.hpp>
1115

1216
#include "simple-mpc/deprecated.hpp"
@@ -17,6 +21,7 @@
1721

1822
namespace simple_mpc
1923
{
24+
using ExplicitIntegratorData = dynamics::ExplicitIntegratorDataTpl<double>;
2025

2126
struct MPCSettings
2227
{
@@ -171,6 +176,13 @@ namespace simple_mpc
171176
}
172177
}
173178

179+
const Eigen::VectorXd getStateDerivative(const std::size_t t)
180+
{
181+
ExplicitIntegratorData * int_data =
182+
dynamic_cast<ExplicitIntegratorData *>(&*solver_->workspace_.problem_data.stage_data[t]->dynamics_data);
183+
return int_data->continuous_data->xdot_;
184+
}
185+
174186
void switchToWalk(const Vector6d & velocity_base);
175187

176188
void switchToStand();

tests/mpc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ BOOST_AUTO_TEST_CASE(mpc_fulldynamics)
8888
BOOST_CHECK_EQUAL(mpc.foot_takeoff_times_.at("right_sole_link")[0], 100);
8989
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("left_sole_link")[0], 209);
9090
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("right_sole_link")[0], 150);
91+
92+
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
9193
}
9294

9395
BOOST_AUTO_TEST_CASE(mpc_kinodynamics)
@@ -161,6 +163,8 @@ BOOST_AUTO_TEST_CASE(mpc_kinodynamics)
161163
{
162164
mpc.iterate(model_handler.getReferenceState());
163165
}
166+
167+
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
164168
}
165169

166170
BOOST_AUTO_TEST_CASE(mpc_centroidal)
@@ -236,6 +240,8 @@ BOOST_AUTO_TEST_CASE(mpc_centroidal)
236240
{
237241
mpc.iterate(x_multibody);
238242
}
243+
244+
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
239245
}
240246

241247
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)