Skip to content

Commit 1997888

Browse files
authored
Merge pull request #31 from Simple-Robotics/topic/get_contact_state
Add getters for contact vector and state derivative
2 parents 2a3746e + af13abe commit 1997888

18 files changed

+98
-54
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<>(),

bindings/expose-problem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace simple_mpc
7171
.def("getPoseBase", bp::pure_virtual(&OCPHandler::getPoseBase), bp::args("self", "t"))
7272
.def("getProblemState", bp::pure_virtual(&OCPHandler::getProblemState), bp::args("self", "data_handler"))
7373
.def("getContactSupport", bp::pure_virtual(&OCPHandler::getContactSupport), bp::args("self", "t"))
74+
.def("getContactState", bp::pure_virtual(&OCPHandler::getContactState), bp::args("self", "t"))
7475
.def(
7576
"createProblem", &OCPHandler::createProblem,
7677
("self"_a, "x0", "horizon", "force_size", "gravity", "terminal_constraint"))

examples/go2_fulldynamics.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,11 @@
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

235-
FL_f, FR_f, RL_f, RR_f, contact_states = extract_forces(mpc.getTrajOptProblem(), mpc.solver.workspace, 0)
227+
FL_f, FR_f, RL_f, RR_f = extract_forces(mpc.getTrajOptProblem(), mpc.solver.workspace, 0)
228+
contact_states = mpc.ocp_handler.getContactState(0)
236229
total_forces = np.concatenate((FL_f, FR_f, RL_f, RR_f))
237230
force_FL.append(FL_f)
238231
force_FR.append(FR_f)

examples/go2_kinodynamics.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,14 @@
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]
241234
forces1 = mpc.us[1][: nk * force_size]
242-
contact_states = (
243-
mpc.getTrajOptProblem().stages[0].dynamics.differential_dynamics.contact_states
244-
)
235+
contact_states = mpc.ocp_handler.getContactState(0)
245236

246237
device.moveQuadrupedFeet(
247238
mpc.getReferencePose(0, "FL_foot").translation,

examples/talos_centroidal.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,10 @@
201201
mpc.getReferencePose(0, "right_sole_link").translation,
202202
)
203203

204-
contact_states = (
205-
mpc.getTrajOptProblem()
206-
.stages[0]
207-
.dynamics.differential_dynamics.contact_map.contact_states.tolist()
208-
)
204+
contact_states = mpc.ocp_handler.getContactState(0)
209205
foot_ref = [mpc.getReferencePose(0, name) for name in model_handler.getFeetNames()]
210206
foot_ref_next = [mpc.getReferencePose(1, name) for name in model_handler.getFeetNames()]
211-
dH = (
212-
mpc.solver
213-
.workspace.problem_data.stage_data[0]
214-
.dynamics_data.continuous_data.xdot[3:9]
215-
)
207+
dH = mpc.getStateDerivative(0)[3:9]
216208
qp.computeDifferences(
217209
mpc.getDataHandler().getData(), x_measured, foot_ref, foot_ref_next
218210
)

examples/talos_kinodynamics.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,14 @@
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]
226219
forces1 = mpc.us[1][: nk * force_size]
227-
contact_states = (
228-
mpc.getTrajOptProblem().stages[0].dynamics.differential_dynamics.contact_states
229-
)
220+
contact_states = mpc.ocp_handler.getContactState(0)
230221

231222
device.moveMarkers(
232223
mpc.getReferencePose(0, "left_sole_link").translation,

examples/utils.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,15 @@ def extract_forces(problem, workspace, id):
7171
force_FR = np.zeros(3)
7272
force_RL = np.zeros(3)
7373
force_RR = np.zeros(3)
74-
contact_states = [False, False, False, False]
7574
in_contact = problem.stages[id].dynamics.differential_dynamics.constraint_models.__len__()
76-
7775
for i in range(in_contact):
7876
if problem.stages[id].dynamics.differential_dynamics.constraint_models[i].name == 'FL_foot':
79-
contact_states[0] = True
8077
force_FL = workspace.problem_data.stage_data[id].dynamics_data.continuous_data.constraint_datas[i].contact_force.linear
8178
elif problem.stages[id].dynamics.differential_dynamics.constraint_models[i].name == 'FR_foot':
82-
contact_states[1] = True
8379
force_FR = workspace.problem_data.stage_data[id].dynamics_data.continuous_data.constraint_datas[i].contact_force.linear
8480
elif problem.stages[id].dynamics.differential_dynamics.constraint_models[i].name == 'RL_foot':
85-
contact_states[2] = True
8681
force_RL = workspace.problem_data.stage_data[id].dynamics_data.continuous_data.constraint_datas[i].contact_force.linear
8782
elif problem.stages[id].dynamics.differential_dynamics.constraint_models[i].name == 'RR_foot':
88-
contact_states[3] = True
8983
force_RR = workspace.problem_data.stage_data[id].dynamics_data.continuous_data.constraint_datas[i].contact_force.linear
9084

91-
return force_FL, force_FR, force_RL, force_RR, contact_states
85+
return force_FL, force_FR, force_RL, force_RR

include/simple-mpc/centroidal-dynamics.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ namespace simple_mpc
9696
void setPoseBase(const std::size_t t, const ConstVectorRef & pose_base) override;
9797
const Eigen::VectorXd getProblemState(const RobotDataHandler & data_handler) override;
9898
size_t getContactSupport(const std::size_t t) override;
99+
std::vector<bool> getContactState(const std::size_t t) override;
99100

100101
CentroidalSettings getSettings()
101102
{

include/simple-mpc/fulldynamics.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ namespace simple_mpc
101101
void setPoseBase(const std::size_t t, const ConstVectorRef & pose_base) override;
102102
const Eigen::VectorXd getProblemState(const RobotDataHandler & data_handler) override;
103103
size_t getContactSupport(const std::size_t t) override;
104+
std::vector<bool> getContactState(const std::size_t t) override;
104105
FullDynamicsSettings getSettings()
105106
{
106107
return settings_;

include/simple-mpc/kinodynamics.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ namespace simple_mpc
8989
void setVelocityBase(const std::size_t t, const ConstVectorRef & velocity_base) override;
9090
const Eigen::VectorXd getProblemState(const RobotDataHandler & data_handler) override;
9191
size_t getContactSupport(const std::size_t t) override;
92+
std::vector<bool> getContactState(const std::size_t t) override;
9293

9394
void computeControlFromForces(const std::map<std::string, Eigen::VectorXd> & force_refs);
9495

0 commit comments

Comments
 (0)