18 #include "pinocchio/parsers/lua/lua_tables.hpp" 19 #include "pinocchio/parsers/lua.hpp" 26 #include "pinocchio/spatial/se3.hpp" 27 #include "pinocchio/spatial/motion.hpp" 28 #include "pinocchio/spatial/inertia.hpp" 29 #include "pinocchio/multibody/model.hpp" 31 typedef se3::SE3::Vector3 Vector3;
32 typedef se3::SE3::Matrix3 Matrix3;
34 template<> Vector3 LuaTableNode::getDefault<Vector3> (
const Vector3 & default_value)
36 Vector3 result (default_value);
38 if (stackQueryValue()) {
39 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
41 if (vector_table.length() != 3) {
42 std::cerr <<
"LuaModel Error: invalid 3d vector!" << std::endl;
46 result[0] = vector_table[1];
47 result[1] = vector_table[2];
48 result[2] = vector_table[3];
56 template<> Matrix3 LuaTableNode::getDefault<Matrix3> (
const Matrix3 & default_value)
58 Matrix3 result (default_value);
60 if (stackQueryValue()) {
61 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
63 if (vector_table.length() != 3) {
64 std::cerr <<
"LuaModel Error: invalid 3d matrix!" << std::endl;
68 if (vector_table[1].length() != 3
69 || vector_table[2].length() != 3
70 || vector_table[3].length() != 3) {
71 std::cerr <<
"LuaModel Error: invalid 3d matrix!" << std::endl;
75 result(0,0) = vector_table[1][1];
76 result(0,1) = vector_table[1][2];
77 result(0,2) = vector_table[1][3];
79 result(1,0) = vector_table[2][1];
80 result(1,1) = vector_table[2][2];
81 result(1,2) = vector_table[2][3];
83 result(2,0) = vector_table[3][1];
84 result(2,1) = vector_table[3][2];
85 result(2,2) = vector_table[3][3];
93 template<>
se3::SE3 LuaTableNode::getDefault<se3::SE3> (
const se3::SE3 & default_value)
97 if (stackQueryValue()) {
98 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
100 result.translation() = vector_table[
"r"].getDefault<Vector3> (Vector3::Zero (3));
101 result.rotation().transpose() = vector_table[
"E"].getDefault<Matrix3> (Matrix3::Identity (3,3));
113 if (stackQueryValue()) {
114 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
118 Matrix3 inertia_matrix;
120 mass = vector_table[
"mass"];
121 com = vector_table[
"com"].getDefault<Vector3> (Vector3::Zero ());
122 inertia_matrix = vector_table[
"inertia"].getDefault<Matrix3> (Matrix3::Identity ());
137 template<
typename Jo
intModel>
138 Model::JointIndex addJointAndBody(Model & model,
const JointModelBase<JointModel> & jmodel,
const Model::JointIndex parent_id,
139 const SE3 & joint_placement,
const std::string & joint_name,
const Inertia & Y,
const std::string & body_name)
141 Model::JointIndex idx;
143 idx = model.addJoint(parent_id,jmodel,
144 joint_placement,joint_name);
145 model.addJointFrame(idx);
146 model.appendBodyToJoint(idx,Y);
147 model.addBodyFrame(body_name, idx);
152 bool LuaModelReadFromTable (
LuaTable & model_table, Model & model,
bool freeFlyer,
bool verbose)
154 typedef std::map<std::string, Model::Index> mapStringIndex_t;
155 mapStringIndex_t body_table_id_map;
156 mapStringIndex_t fixed_body_table_id_map;
158 typedef std::map<std::string, SE3> mapStringSE3_t;
159 mapStringSE3_t fixed_placement_map;
161 if (model_table[
"gravity"].exists())
163 model.gravity.linear() = model_table[
"gravity"].get<Vector3> ();
166 std::cout <<
"gravity = " << model.gravity.linear().transpose() << std::endl;
169 if (! model_table[
"frames"].exists())
171 std::cerr <<
"Frames table missing from model table - Abort" << std::endl;
175 size_t frame_count = model_table[
"frames"].length();
177 body_table_id_map[
"ROOT"] = 0;
179 for (
int i = 1; i <= (int) frame_count; i++) {
181 std::stringstream body_name_default;
182 body_name_default <<
"body " << i;
183 std::string body_name = model_table[
"frames"][i][
"name"].getDefault<std::string> (body_name_default.str());
185 std::string parent_name;
186 if (model_table[
"frames"][i][
"parent"].exists ())
188 parent_name = model_table[
"frames"][i][
"parent"].get<std::string> ();
192 parent_name =
"ROOT";
196 std::cerr <<
"Parent not defined for frame " << i <<
"." << std::endl;
200 Model::JointIndex parent_id;
201 SE3 fixed_placement_offset (SE3::Identity());;
203 if (body_table_id_map.find (parent_name) != body_table_id_map.end ())
205 parent_id = body_table_id_map[parent_name];
207 else if (fixed_body_table_id_map.find(parent_name) != fixed_body_table_id_map.end ())
209 parent_id = fixed_body_table_id_map[parent_name];
210 fixed_placement_offset = fixed_placement_map[parent_name];
214 std::cerr << parent_name <<
" is not in the tree." << std::endl;
218 std::stringstream joint_name_default;
219 joint_name_default <<
"joint " << i;
220 std::string joint_name = model_table[
"frames"][i][
"joint_name"].getDefault<std::string> (joint_name_default.str());
222 SE3 joint_placement = model_table[
"frames"][i][
"joint_frame"].getDefault<SE3> (SE3::Identity ());
223 SE3 global_placement (fixed_placement_offset * joint_placement);
225 if (! model_table[
"frames"][i][
"body"].exists()) {
226 std::cerr <<
"body field not defined for frame " << i <<
"." << std::endl;
230 Inertia Y = model_table[
"frames"][i][
"body"].getDefault<Inertia> (Inertia::Identity ());
232 std::string joint_type;
233 if (model_table[
"frames"][i][
"joint"].exists())
235 if (model_table[
"frames"][i][
"joint"].length() == 0)
236 joint_type =
"JointTypeFixed";
237 else if (model_table[
"frames"][i][
"joint"].length() == 1)
238 joint_type = model_table[
"frames"][i][
"joint"][1].getDefault<std::string> (
"");
241 joint_type = model_table[
"frames"][i][
"joint"][1].getDefault<std::string> (
"");
242 std::cerr <<
"Joint compouned not yet implemented. Take only the first joint." << std::endl;
248 joint_type =
"JointTypeFloatbase";
251 std::cerr <<
"The first segment is defined without any definition of joint type relatily to the world." << std::endl;
257 std::cerr <<
"joint field not defined for frame " << i <<
"." << std::endl;
261 Model::JointIndex joint_id;
262 if (joint_type ==
"JointTypeRevoluteX")
264 joint_id = addJointAndBody(model,JointModelRX(),parent_id,global_placement,joint_name,Y,body_name);
266 else if (joint_type ==
"JointTypeRevoluteY")
268 joint_id = addJointAndBody(model,JointModelRY(),parent_id,global_placement,joint_name,Y,body_name);
270 else if (joint_type ==
"JointTypeRevoluteZ")
272 joint_id = addJointAndBody(model,JointModelRZ(),parent_id,global_placement,joint_name,Y,body_name);
274 else if (joint_type ==
"JointTypePrismaticX")
276 joint_id = addJointAndBody(model,JointModelPX(),parent_id,global_placement,joint_name,Y,body_name);
278 else if (joint_type ==
"JointTypePrismaticY")
280 joint_id = addJointAndBody(model,JointModelPY(),parent_id,global_placement,joint_name,Y,body_name);
282 else if (joint_type ==
"JointTypePrismaticZ")
284 joint_id = addJointAndBody(model,JointModelPZ(),parent_id,global_placement,joint_name,Y,body_name);
286 else if (joint_type ==
"JointTypeFloatingBase" || joint_type ==
"JointTypeFloatbase")
288 joint_id = addJointAndBody(model,JointModelFreeFlyer(),parent_id,global_placement,joint_name,Y,body_name);
290 else if (joint_type ==
"JointTypeSpherical")
292 joint_id = addJointAndBody(model,JointModelSpherical(),parent_id,global_placement,joint_name,Y,body_name);
294 else if (joint_type ==
"JointTypeEulerZYX")
296 joint_id = addJointAndBody(model,JointModelSphericalZYX(),parent_id,global_placement,joint_name,Y,body_name);
298 else if (joint_type ==
"JointTypeFixed")
300 model.appendBodyToJoint(parent_id, Y, global_placement);
306 joint_id = (Model::JointIndex)model.njoints;
308 fixed_body_table_id_map[body_name] = parent_id;
309 fixed_placement_map[body_name] = global_placement;
313 std::cerr << joint_type <<
" is not supported.." << std::endl;
317 body_table_id_map[body_name] = joint_id;
320 std::cout <<
"==== Added Body ====" << std::endl;
321 std::cout <<
" joint name : " << joint_type << std::endl;
322 std::cout <<
" joint id : " << joint_id << std::endl;
323 std::cout <<
" joint parent id : " << parent_id << std::endl;
324 std::cout <<
" joint placement (wrt its parent):\n" << joint_placement << std::endl;
325 std::cout <<
" joint type : " << joint_type << std::endl;
326 std::cout <<
" body name : " << body_name << std::endl;
327 std::cout <<
" body inertia:\n" << Y << std::endl;
334 Model buildModel (
const std::string & filename,
bool freeFlyer,
bool verbose)
338 LuaTable model_table = LuaTable::fromFile (filename.c_str ());
339 LuaModelReadFromTable (model_table, model, freeFlyer, verbose);
std::string randomStringGenerator(const int len)
Generate a random string composed of alphanumeric symbols of a given length.