Skip to content

Commit 6c5e05d

Browse files
committed
Get contact forces from fulldyn mpc
1 parent 8611a62 commit 6c5e05d

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

bindings/expose-mpc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ namespace simple_mpc
6464
void exposeMPC()
6565
{
6666
using StageVec = std::vector<std::shared_ptr<StageModel>>;
67+
using ForceVec = std::vector<Eigen::Vector3d>;
6768
using MapBool = std::map<std::string, bool>;
6869
StdVectorPythonVisitor<StageVec, true>::expose(
6970
"StdVec_StageModel", eigenpy::details::overload_base_get_item_for_std_vector<StageVec>());
71+
StdVectorPythonVisitor<ForceVec, true>::expose(
72+
"StdVec_Force", eigenpy::details::overload_base_get_item_for_std_vector<ForceVec>());
7073

7174
StdVectorPythonVisitor<std::vector<MapBool>, true>::expose("StdVec_MapBool");
7275

@@ -88,6 +91,7 @@ namespace simple_mpc
8891
.def("getFootTakeoffCycle", &MPC::getFootTakeoffCycle, ("self"_a, "ee_name"))
8992
.def("getFootLandCycle", &MPC::getFootLandCycle, ("self"_a, "ee_name"))
9093
.def("getStateDerivative", &MPC::getStateDerivative, ("self"_a, "t"))
94+
.def("getContactForces", &MPC::getContactForces, ("self"_a, "t"))
9195
.def("getCyclingContactState", &MPC::getCyclingContactState, ("self"_a, "t", "ee_name"))
9296
.def(
9397
"getModelHandler", &MPC::getModelHandler, "self"_a, bp::return_internal_reference<>(),

examples/go2_fulldynamics.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
w_basepos = [0, 0, 0, 0, 0, 0]
4545
w_legpos = [10, 10, 10]
4646

47-
w_basevel = [10, 10, 10, 10, 10, 10]
47+
w_basevel = [0, 0, 0, 0, 0, 0]
4848
w_legvel = [0.1, 0.1, 0.1]
4949
w_x = np.array(w_basepos + w_legpos * 4 + w_basevel + w_legvel * 4)
5050
w_cent_lin = np.array([0.0, 0.0, 0])
@@ -201,9 +201,9 @@
201201
L_measured = []
202202

203203
v = np.zeros(6)
204-
v[0] = 0.
204+
v[5] = 0.2
205205
mpc.velocity_base = v
206-
for t in range(500):
206+
for t in range(1000):
207207
print("Time " + str(t))
208208
land_LF = mpc.getFootLandCycle("FL_foot")
209209
land_RF = mpc.getFootLandCycle("RL_foot")
@@ -214,7 +214,10 @@
214214
str(land_RF) + ", takeoff_LF = " + str(takeoff_LF) + ", landing_LF = ",
215215
str(land_LF),
216216
) """
217-
217+
if t == 400:
218+
v = np.zeros(6)
219+
v[0] = 0.2
220+
mpc.velocity_base = v
218221
""" if t == 200:
219222
for s in range(T):
220223
device.resetState(mpc.xs[s][:nq])
@@ -238,13 +241,14 @@
238241
a0 = mpc.getStateDerivative(0)[nv:]
239242
a1 = mpc.getStateDerivative(1)[nv:]
240243

241-
FL_f, FR_f, RL_f, RR_f = extract_forces(mpc.getTrajOptProblem(), mpc.solver.workspace, 0)
244+
forces_vec = mpc.getContactForces(0)
242245
contact_states = mpc.ocp_handler.getContactState(0)
243-
total_forces = np.concatenate((FL_f, FR_f, RL_f, RR_f))
244-
force_FL.append(FL_f)
245-
force_FR.append(FR_f)
246-
force_RL.append(RL_f)
247-
force_RR.append(RR_f)
246+
total_forces = np.concatenate((forces_vec[0], forces_vec[1], forces_vec[2], forces_vec[3]))
247+
248+
force_FL.append(forces_vec[0])
249+
force_FR.append(forces_vec[1])
250+
force_RL.append(forces_vec[2])
251+
force_RR.append(forces_vec[3])
248252

249253
forces = [total_forces, total_forces]
250254
ddqs = [a0, a1]

include/simple-mpc/mpc.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <aligator/fwd.hpp>
1212
#include <aligator/modelling/dynamics/fwd.hpp>
1313
#include <aligator/modelling/dynamics/integrator-explicit.hpp>
14+
#include <aligator/modelling/dynamics/multibody-constraint-fwd.hpp>
1415
#include <aligator/solvers/proxddp/solver-proxddp.hpp>
1516

1617
#include "simple-mpc/deprecated.hpp"
@@ -22,6 +23,8 @@
2223
namespace simple_mpc
2324
{
2425
using ExplicitIntegratorData = dynamics::ExplicitIntegratorDataTpl<double>;
26+
using MultibodyConstraintFwdData = dynamics::MultibodyConstraintFwdDataTpl<double>;
27+
using MultibodyConstraintFwdDynamics = dynamics::MultibodyConstraintFwdDynamicsTpl<double>;
2528

2629
struct MPCSettings
2730
{
@@ -183,6 +186,33 @@ namespace simple_mpc
183186
return int_data->continuous_data->xdot_;
184187
}
185188

189+
const std::vector<Eigen::Vector3d> getContactForces(const std::size_t t)
190+
{
191+
std::vector<Eigen::Vector3d> contact_forces;
192+
ExplicitIntegratorData * int_data =
193+
dynamic_cast<ExplicitIntegratorData *>(&*solver_->workspace_.problem_data.stage_data[t]->dynamics_data);
194+
assert(int_data != nullptr);
195+
MultibodyConstraintFwdData * mc_data = dynamic_cast<MultibodyConstraintFwdData *>(&*int_data->continuous_data);
196+
assert(mc_data != nullptr);
197+
198+
std::vector<bool> contact_state = ocp_handler_->getContactState(t);
199+
200+
size_t force_id = 0;
201+
for (size_t i = 0; i < contact_state.size(); i++)
202+
{
203+
if (contact_state[i])
204+
{
205+
contact_forces.push_back(mc_data->constraint_datas_[force_id].contact_force.linear());
206+
force_id += 1;
207+
}
208+
else
209+
{
210+
contact_forces.push_back(Eigen::Vector3d::Zero());
211+
}
212+
}
213+
return contact_forces;
214+
}
215+
186216
void switchToWalk(const Vector6d & velocity_base);
187217

188218
void switchToStand();

tests/mpc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ BOOST_AUTO_TEST_CASE(mpc_fulldynamics)
9090
BOOST_CHECK_EQUAL(mpc.foot_land_times_.at("right_sole_link")[0], 150);
9191

9292
Eigen::VectorXd xdot = mpc.getStateDerivative(0);
93+
94+
std::vector<Eigen::Vector3d> forces = mpc.getContactForces(0);
9395
}
9496

9597
BOOST_AUTO_TEST_CASE(mpc_kinodynamics)

0 commit comments

Comments
 (0)