Skip to content

Commit 9ce070f

Browse files
committed
Reduced model is now built outside robot_handler
1 parent 1997888 commit 9ce070f

File tree

12 files changed

+130
-184
lines changed

12 files changed

+130
-184
lines changed

benchmark/talos.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "simple-mpc/mpc.hpp"
33
#include "simple-mpc/ocp-handler.hpp"
44
#include "simple-mpc/robot-handler.hpp"
5+
#include <pinocchio/algorithm/model.hpp>
56
#include <pinocchio/fwd.hpp>
67
#include <pinocchio/parsers/srdf.hpp>
78
#include <pinocchio/parsers/urdf.hpp>
@@ -19,13 +20,13 @@ using simple_mpc::OCPHandler;
1920
int main()
2021
{
2122
// Load pinocchio model from example robot data
22-
pinocchio::Model model;
23+
pinocchio::Model modelFull;
2324
std::string urdf_path = EXAMPLE_ROBOT_DATA_MODEL_DIR "/talos_data/robots/talos_reduced.urdf";
2425
std::string srdf_path = EXAMPLE_ROBOT_DATA_MODEL_DIR "/talos_data/srdf/talos.srdf";
2526

26-
pinocchio::urdf::buildModel(urdf_path, pinocchio::JointModelFreeFlyer(), model);
27-
pinocchio::srdf::loadReferenceConfigurations(model, srdf_path, false);
28-
pinocchio::srdf::loadRotorParameters(model, srdf_path, false);
27+
pinocchio::urdf::buildModel(urdf_path, pinocchio::JointModelFreeFlyer(), modelFull);
28+
pinocchio::srdf::loadReferenceConfigurations(modelFull, srdf_path, false);
29+
pinocchio::srdf::loadRotorParameters(modelFull, srdf_path, false);
2930

3031
// Lock joint list
3132
const std::vector<std::string> controlled_joints_names{
@@ -36,7 +37,8 @@ int main()
3637
"arm_right_1_joint", "arm_right_2_joint", "arm_right_3_joint", "arm_right_4_joint",
3738
};
3839

39-
std::vector<std::string> locked_joints_names{model.names};
40+
std::vector<std::string> locked_joints_names{modelFull.names};
41+
std::vector<pinocchio::JointIndex> controlled_joints_ids;
4042
locked_joints_names.erase(
4143
std::remove_if(
4244
locked_joints_names.begin(), locked_joints_names.end(),
@@ -46,9 +48,28 @@ int main()
4648
}),
4749
locked_joints_names.end());
4850

51+
// Construct controlled and locked joints ids list
52+
pinocchio::Model model;
53+
std::vector<unsigned long> locked_joint_ids;
54+
for (size_t i = 1; i < modelFull.names.size(); i++)
55+
{
56+
const std::string joint_name = modelFull.names.at(i);
57+
if (count(locked_joints_names.begin(), locked_joints_names.end(), joint_name) == 0)
58+
{
59+
controlled_joints_ids.push_back(modelFull.getJointId(joint_name));
60+
}
61+
else
62+
{
63+
locked_joint_ids.push_back(modelFull.getJointId(joint_name));
64+
}
65+
}
66+
67+
// Build reduced model with locked joints
68+
pinocchio::buildReducedModel(modelFull, locked_joint_ids, modelFull.referenceConfigurations["half_sitting"], model);
69+
4970
// Actually create handler
5071
std::string base_joint = "root_joint";
51-
RobotModelHandler model_handler(model, "half_sitting", base_joint, locked_joints_names);
72+
RobotModelHandler model_handler(model, "half_sitting", base_joint);
5273

5374
// Add feet
5475
model_handler.addFoot(

bindings/expose-robot-handler.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ namespace simple_mpc
2121
void exposeHandler()
2222
{
2323
bp::class_<RobotModelHandler>(
24-
"RobotModelHandler",
25-
bp::init<const pinocchio::Model &, const std::string &, const std::string &, const std::vector<std::string> &>(
26-
bp::args("self", "model", "reference_configuration_name", "base_frame_name", "locked_joint_names")))
24+
"RobotModelHandler", bp::init<const pinocchio::Model &, const std::string &, const std::string &>(
25+
bp::args("self", "model", "reference_configuration_name", "base_frame_name")))
2726
.def("addFoot", &RobotModelHandler::addFoot)
2827
.def("difference", &RobotModelHandler::difference)
29-
.def("shapeState", &RobotModelHandler::shapeState)
3028
.def("getBaseFrameId", &RobotModelHandler::getBaseFrameId)
3129
.def("getReferenceState", &RobotModelHandler::getReferenceState)
3230
.def("getFootNb", &RobotModelHandler::getFootNb)
@@ -37,8 +35,7 @@ namespace simple_mpc
3735
.def("getFootId", &RobotModelHandler::getFootId)
3836
.def("getRefFootId", &RobotModelHandler::getRefFootId)
3937
.def("getMass", &RobotModelHandler::getMass)
40-
.def("getModel", &RobotModelHandler::getModel, bp::return_internal_reference<>())
41-
.def("getCompleteModel", &RobotModelHandler::getCompleteModel, bp::return_internal_reference<>());
38+
.def("getModel", &RobotModelHandler::getModel, bp::return_internal_reference<>());
4239

4340
ENABLE_SPECIFIC_MATRIX_TYPE(RobotDataHandler::CentroidalStateVector);
4441

examples/go2_fulldynamics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
robot_wrapper = erd.load('go2')
1515

1616
# Create Model and Data handler
17-
model_handler = RobotModelHandler(robot_wrapper.model, "standing", base_joint_name, [])
17+
model_handler = RobotModelHandler(robot_wrapper.model, "standing", base_joint_name)
1818
model_handler.addFoot("FL_foot", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.17, 0.15, 0.0, 0,0,0,1])))
1919
model_handler.addFoot("FR_foot", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.17,-0.15, 0.0, 0,0,0,1])))
2020
model_handler.addFoot("RL_foot", base_joint_name, pin.XYZQUATToSE3(np.array([-0.24, 0.15, 0.0, 0,0,0,1])))

examples/go2_kinodynamics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
robot_wrapper = erd.load('go2')
1515

1616
# Create Model and Data handler
17-
model_handler = RobotModelHandler(robot_wrapper.model, "standing", base_joint_name, [])
17+
model_handler = RobotModelHandler(robot_wrapper.model, "standing", base_joint_name)
1818
model_handler.addFoot("FL_foot", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.17, 0.15, 0.0, 0,0,0,1])))
1919
model_handler.addFoot("FR_foot", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.17,-0.15, 0.0, 0,0,0,1])))
2020
model_handler.addFoot("RL_foot", base_joint_name, pin.XYZQUATToSE3(np.array([-0.24, 0.15, 0.0, 0,0,0,1])))
@@ -225,8 +225,8 @@
225225
com_measured.append(mpc.getDataHandler().getData().com[0].copy())
226226
L_measured.append(mpc.getDataHandler().getData().hg.angular.copy())
227227

228-
a0 = mpc.getStateDerivative(0)[nv:]
229-
a1 = mpc.getStateDerivative(1)[nv:]
228+
a0 = mpc.getStateDerivative(0)[nv:].copy()
229+
a1 = mpc.getStateDerivative(1)[nv:].copy()
230230

231231
a0[6:] = mpc.us[0][nk * force_size :]
232232
a1[6:] = mpc.us[1][nk * force_size :]

examples/talos_centroidal.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@
33
import example_robot_data as erd
44
from bullet_robot import BulletRobot
55
from simple_mpc import RobotModelHandler, RobotDataHandler, CentroidalOCP, MPC, IKIDSolver
6+
from utils import loadTalos
67
import time
78

89
# RobotWrapper
910
URDF_SUBPATH = "/talos_data/robots/talos_reduced.urdf"
1011
base_joint_name ="root_joint"
11-
robot_wrapper = erd.load('talos')
12-
1312
reference_configuration_name = "half_sitting"
14-
locked_joints = [
15-
'arm_left_5_joint',
16-
'arm_left_6_joint',
17-
'arm_left_7_joint',
18-
'gripper_left_joint',
19-
'arm_right_5_joint',
20-
'arm_right_6_joint',
21-
'arm_right_7_joint',
22-
'gripper_right_joint',
23-
'head_1_joint',
24-
'head_2_joint'
25-
]
13+
14+
rmodelComplete, rmodel, qComplete, q0 = loadTalos()
2615

2716
# Create Model and Data handler
28-
model_handler = RobotModelHandler(robot_wrapper.model, reference_configuration_name, base_joint_name, locked_joints)
17+
model_handler = RobotModelHandler(rmodel, reference_configuration_name, base_joint_name)
2918
model_handler.addFoot("left_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0, 0.1, 0.0, 0,0,0,1])))
3019
model_handler.addFoot("right_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0,-0.1, 0.0, 0,0,0,1])))
3120
data_handler = RobotDataHandler(model_handler)

examples/talos_fulldynamics.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,28 @@
77
import numpy as np
88
import time
99
from bullet_robot import BulletRobot
10-
import example_robot_data as erd
1110
import pinocchio as pin
1211
from simple_mpc import MPC, FullDynamicsOCP, RobotModelHandler, RobotDataHandler
12+
from utils import loadTalos
13+
import example_robot_data as erd
1314

1415
# ####### CONFIGURATION ############
1516
# RobotWrapper
1617
URDF_SUBPATH = "/talos_data/robots/talos_reduced.urdf"
1718
base_joint_name ="root_joint"
18-
robot_wrapper = erd.load('talos')
19-
2019
reference_configuration_name = "half_sitting"
21-
locked_joints = [
22-
'arm_left_5_joint',
23-
'arm_left_6_joint',
24-
'arm_left_7_joint',
25-
'gripper_left_joint',
26-
'arm_right_5_joint',
27-
'arm_right_6_joint',
28-
'arm_right_7_joint',
29-
'gripper_right_joint',
30-
'head_1_joint',
31-
'head_2_joint'
32-
]
20+
21+
rmodelComplete, rmodel, qComplete, q0 = loadTalos()
3322

3423
# Create Model and Data handler
35-
model_handler = RobotModelHandler(robot_wrapper.model, reference_configuration_name, base_joint_name, locked_joints)
24+
model_handler = RobotModelHandler(rmodel, reference_configuration_name, base_joint_name)
3625
model_handler.addFoot("left_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0, 0.1, 0.0, 0,0,0,1])))
3726
model_handler.addFoot("right_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0,-0.1, 0.0, 0,0,0,1])))
3827
data_handler = RobotDataHandler(model_handler)
3928

29+
controlled_joints = rmodel.names[1:].tolist()
30+
controlled_ids = [rmodelComplete.getJointId(name_joint) for name_joint in controlled_joints[1:]]
31+
4032
nq = model_handler.getModel().nq
4133
nv = model_handler.getModel().nv
4234
nu = nv - 6
@@ -150,7 +142,7 @@
150142
device.changeCamera(1.0, 90, -5, [1.5, 0, 1])
151143

152144
q_meas, v_meas = device.measureState()
153-
x_measured = np.concatenate([q_meas, v_meas])
145+
x_measured = np.concatenate((q_meas, v_meas))
154146

155147
land_LF = -1
156148
land_RF = -1
@@ -181,6 +173,7 @@
181173
str(land_LF),
182174
)
183175
start = time.time()
176+
184177
mpc.iterate(x_measured)
185178
end = time.time()
186179
print("MPC iterate = " + str(end - start))
@@ -191,7 +184,7 @@
191184

192185
for j in range(10):
193186
q_meas, v_meas = device.measureState()
194-
x_measured = np.concatenate([q_meas, v_meas])
187+
x_measured = np.concatenate((q_meas, v_meas))
195188

196189
current_torque = mpc.us[0] - mpc.Ks[0] @ model_handler.difference(x_measured, mpc.xs[0])
197190
device.execute(current_torque)

examples/talos_kinodynamics.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@
33
import pinocchio as pin
44
from bullet_robot import BulletRobot
55
import time
6+
from utils import loadTalos
67
from simple_mpc import RobotModelHandler, RobotDataHandler, KinodynamicsOCP, MPC, IDSolver
78

89
# RobotWrapper
910
URDF_SUBPATH = "/talos_data/robots/talos_reduced.urdf"
1011
base_joint_name ="root_joint"
11-
robot_wrapper = erd.load('talos')
12-
1312
reference_configuration_name = "half_sitting"
14-
locked_joints = [
15-
'arm_left_5_joint',
16-
'arm_left_6_joint',
17-
'arm_left_7_joint',
18-
'gripper_left_joint',
19-
'arm_right_5_joint',
20-
'arm_right_6_joint',
21-
'arm_right_7_joint',
22-
'gripper_right_joint',
23-
'head_1_joint',
24-
'head_2_joint'
25-
]
13+
14+
rmodelComplete, rmodel, qComplete, q0 = loadTalos()
2615

2716
# Create Model and Data handler
28-
model_handler = RobotModelHandler(robot_wrapper.model, reference_configuration_name, base_joint_name, locked_joints)
17+
model_handler = RobotModelHandler(rmodel, reference_configuration_name, base_joint_name)
2918
model_handler.addFoot("left_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0, 0.1, 0.0, 0,0,0,1])))
3019
model_handler.addFoot("right_sole_link", base_joint_name, pin.XYZQUATToSE3(np.array([ 0.0,-0.1, 0.0, 0,0,0,1])))
3120
data_handler = RobotDataHandler(model_handler)
@@ -211,8 +200,8 @@
211200
end = time.time()
212201
print("MPC iterate = " + str(end - start))
213202

214-
a0 = mpc.getStateDerivative(0)[nv:]
215-
a1 = mpc.getStateDerivative(1)[nv:]
203+
a0 = mpc.getStateDerivative(0)[nv:].copy()
204+
a1 = mpc.getStateDerivative(1)[nv:].copy()
216205
a0[6:] = mpc.us[0][nk * force_size :]
217206
a1[6:] = mpc.us[1][nk * force_size :]
218207
forces0 = mpc.us[0][: nk * force_size]

examples/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import numpy as np
2+
import pinocchio as pin
3+
import example_robot_data
24
import os
35

46
CURRENT_DIRECTORY = os.getcwd()
57
DEFAULT_SAVE_DIR = CURRENT_DIRECTORY + '/tmp'
68

9+
def loadTalos():
10+
robotComplete = example_robot_data.load("talos")
11+
qComplete = robotComplete.model.referenceConfigurations["half_sitting"]
12+
13+
locked_joints = [20,21,22,23,28,29,30,31]
14+
locked_joints += [32, 33]
15+
robot = robotComplete.buildReducedRobot(locked_joints, qComplete)
16+
rmodel: pin.Model = robot.model
17+
q0 = rmodel.referenceConfigurations["half_sitting"]
18+
19+
return robotComplete.model, rmodel, qComplete, q0
720

821
def save_trajectory(
922
xs,

include/simple-mpc/robot-handler.hpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ namespace simple_mpc
2929
{
3030
private:
3131
/**
32-
* @brief Robot model with all joints unlocked
33-
*/
34-
Model model_full_;
35-
36-
/**
37-
* @brief Reduced model to be used by ocp
32+
* @brief Model to be used by ocp
3833
*/
3934
Model model_;
4035

@@ -82,14 +77,9 @@ namespace simple_mpc
8277
* @param feet_names Name of the frames corresponding to the feet (e.g. can be
8378
* used for contact with the ground)
8479
* @param reference_configuration_name Reference configuration to use
85-
* @param locked_joint_names List of joints to lock (values will be fixed at
86-
* the reference configuration)
8780
*/
8881
RobotModelHandler(
89-
const Model & model,
90-
const std::string & reference_configuration_name,
91-
const std::string & base_frame_name,
92-
const std::vector<std::string> & locked_joint_names = {});
82+
const Model & model, const std::string & reference_configuration_name, const std::string & base_frame_name);
9383

9484
/**
9585
* @brief
@@ -113,16 +103,6 @@ namespace simple_mpc
113103
*/
114104
Eigen::VectorXd difference(const ConstVectorRef & x1, const ConstVectorRef & x2) const;
115105

116-
/**
117-
* @brief Compute reduced state from measures by concatenating q,v of the
118-
* reduced model.
119-
*
120-
* @param q Configuration vector of the full model
121-
* @param v Velocity vector of the full model
122-
* @return const Eigen::VectorXd State vector of the reduced model.
123-
*/
124-
Eigen::VectorXd shapeState(const ConstVectorRef & q, const ConstVectorRef & v) const;
125-
126106
// Const getters
127107
ConstVectorRef getReferenceState() const
128108
{
@@ -174,11 +154,6 @@ namespace simple_mpc
174154
{
175155
return model_;
176156
}
177-
178-
const Model & getCompleteModel() const
179-
{
180-
return model_full_;
181-
}
182157
};
183158

184159
class RobotDataHandler

0 commit comments

Comments
 (0)