5 #include "pinocchio/parsers/lua/lua_tables.hpp" 6 #include "pinocchio/parsers/lua.hpp" 13 #include "pinocchio/spatial/se3.hpp" 14 #include "pinocchio/spatial/motion.hpp" 15 #include "pinocchio/spatial/inertia.hpp" 16 #include "pinocchio/multibody/model.hpp" 18 typedef pinocchio::SE3::Vector3 Vector3;
19 typedef pinocchio::SE3::Matrix3 Matrix3;
21 template<> Vector3 LuaTableNode::getDefault<Vector3> (
const Vector3 & default_value)
23 Vector3 result (default_value);
25 if (stackQueryValue()) {
26 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
28 if (vector_table.length() != 3) {
29 std::cerr <<
"LuaModel Error: invalid 3d vector!" << std::endl;
33 result[0] = vector_table[1];
34 result[1] = vector_table[2];
35 result[2] = vector_table[3];
43 template<> Matrix3 LuaTableNode::getDefault<Matrix3> (
const Matrix3 & default_value)
45 Matrix3 result (default_value);
47 if (stackQueryValue()) {
48 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
50 if (vector_table.length() != 3) {
51 std::cerr <<
"LuaModel Error: invalid 3d matrix!" << std::endl;
55 if (vector_table[1].length() != 3
56 || vector_table[2].length() != 3
57 || vector_table[3].length() != 3) {
58 std::cerr <<
"LuaModel Error: invalid 3d matrix!" << std::endl;
62 result(0,0) = vector_table[1][1];
63 result(0,1) = vector_table[1][2];
64 result(0,2) = vector_table[1][3];
66 result(1,0) = vector_table[2][1];
67 result(1,1) = vector_table[2][2];
68 result(1,2) = vector_table[2][3];
70 result(2,0) = vector_table[3][1];
71 result(2,1) = vector_table[3][2];
72 result(2,2) = vector_table[3][3];
84 if (stackQueryValue()) {
85 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
87 result.translation() = vector_table[
"r"].getDefault<Vector3> (Vector3::Zero (3));
88 result.rotation().transpose() = vector_table[
"E"].getDefault<Matrix3> (Matrix3::Identity (3,3));
100 if (stackQueryValue()) {
101 LuaTable vector_table = LuaTable::fromLuaState (luaTable->L);
105 Matrix3 inertia_matrix;
107 mass = vector_table[
"mass"];
108 com = vector_table[
"com"].getDefault<Vector3> (Vector3::Zero ());
109 inertia_matrix = vector_table[
"inertia"].getDefault<Matrix3> (Matrix3::Identity ());
124 template<
typename Jo
intModel>
125 Model::JointIndex addJointAndBody(Model & model,
const JointModelBase<JointModel> & jmodel,
const Model::JointIndex parent_id,
126 const SE3 & joint_placement,
const std::string & joint_name,
const Inertia & Y,
const std::string & body_name)
128 Model::JointIndex idx;
130 idx = model.addJoint(parent_id,jmodel,
131 joint_placement,joint_name);
132 model.addJointFrame(idx);
133 model.appendBodyToJoint(idx,Y);
134 model.addBodyFrame(body_name, idx);
139 bool LuaModelReadFromTable (
LuaTable & model_table, Model & model,
bool freeFlyer,
bool verbose)
141 typedef std::map<std::string, Model::Index> mapStringIndex_t;
142 mapStringIndex_t body_table_id_map;
143 mapStringIndex_t fixed_body_table_id_map;
145 typedef std::map<std::string, SE3> mapStringSE3_t;
146 mapStringSE3_t fixed_placement_map;
148 if (model_table[
"gravity"].exists())
150 model.gravity.linear() = model_table[
"gravity"].get<Vector3> ();
153 std::cout <<
"gravity = " << model.gravity.linear().transpose() << std::endl;
156 if (! model_table[
"frames"].exists())
158 std::cerr <<
"Frames table missing from model table - Abort" << std::endl;
162 size_t frame_count = model_table[
"frames"].length();
164 body_table_id_map[
"ROOT"] = 0;
166 for (
int i = 1; i <= (int) frame_count; i++) {
168 std::stringstream body_name_default;
169 body_name_default <<
"body " << i;
170 std::string body_name = model_table[
"frames"][i][
"name"].getDefault<std::string> (body_name_default.str());
172 std::string parent_name;
173 if (model_table[
"frames"][i][
"parent"].exists ())
175 parent_name = model_table[
"frames"][i][
"parent"].get<std::string> ();
179 parent_name =
"ROOT";
183 std::cerr <<
"Parent not defined for frame " << i <<
"." << std::endl;
187 Model::JointIndex parent_id;
188 SE3 fixed_placement_offset (SE3::Identity());
190 if (body_table_id_map.find (parent_name) != body_table_id_map.end ())
192 parent_id = body_table_id_map[parent_name];
194 else if (fixed_body_table_id_map.find(parent_name) != fixed_body_table_id_map.end ())
196 parent_id = fixed_body_table_id_map[parent_name];
197 fixed_placement_offset = fixed_placement_map[parent_name];
201 std::cerr << parent_name <<
" is not in the tree." << std::endl;
205 std::stringstream joint_name_default;
206 joint_name_default <<
"joint " << i;
207 std::string joint_name = model_table[
"frames"][i][
"joint_name"].getDefault<std::string> (joint_name_default.str());
209 SE3 joint_placement = model_table[
"frames"][i][
"joint_frame"].getDefault<SE3> (SE3::Identity ());
210 SE3 global_placement (fixed_placement_offset * joint_placement);
212 if (! model_table[
"frames"][i][
"body"].exists()) {
213 std::cerr <<
"body field not defined for frame " << i <<
"." << std::endl;
217 Inertia Y = model_table[
"frames"][i][
"body"].getDefault<Inertia> (Inertia::Identity ());
219 std::string joint_type;
220 if (model_table[
"frames"][i][
"joint"].exists())
222 if (model_table[
"frames"][i][
"joint"].length() == 0)
223 joint_type =
"JointTypeFixed";
224 else if (model_table[
"frames"][i][
"joint"].length() == 1)
225 joint_type = model_table[
"frames"][i][
"joint"][1].getDefault<std::string> (
"");
228 joint_type = model_table[
"frames"][i][
"joint"][1].getDefault<std::string> (
"");
229 std::cerr <<
"Joint compouned not yet implemented. Take only the first joint." << std::endl;
235 joint_type =
"JointTypeFloatbase";
238 std::cerr <<
"The first segment is defined without any definition of joint type relatily to the world." << std::endl;
244 std::cerr <<
"joint field not defined for frame " << i <<
"." << std::endl;
248 Model::JointIndex joint_id;
249 if (joint_type ==
"JointTypeRevoluteX")
251 joint_id = addJointAndBody(model,JointModelRX(),parent_id,global_placement,joint_name,Y,body_name);
253 else if (joint_type ==
"JointTypeRevoluteY")
255 joint_id = addJointAndBody(model,JointModelRY(),parent_id,global_placement,joint_name,Y,body_name);
257 else if (joint_type ==
"JointTypeRevoluteZ")
259 joint_id = addJointAndBody(model,JointModelRZ(),parent_id,global_placement,joint_name,Y,body_name);
261 else if (joint_type ==
"JointTypePrismaticX")
263 joint_id = addJointAndBody(model,JointModelPX(),parent_id,global_placement,joint_name,Y,body_name);
265 else if (joint_type ==
"JointTypePrismaticY")
267 joint_id = addJointAndBody(model,JointModelPY(),parent_id,global_placement,joint_name,Y,body_name);
269 else if (joint_type ==
"JointTypePrismaticZ")
271 joint_id = addJointAndBody(model,JointModelPZ(),parent_id,global_placement,joint_name,Y,body_name);
273 else if (joint_type ==
"JointTypeFloatingBase" || joint_type ==
"JointTypeFloatbase")
275 joint_id = addJointAndBody(model,JointModelFreeFlyer(),parent_id,global_placement,joint_name,Y,body_name);
277 else if (joint_type ==
"JointTypeSpherical")
279 joint_id = addJointAndBody(model,JointModelSpherical(),parent_id,global_placement,joint_name,Y,body_name);
281 else if (joint_type ==
"JointTypeEulerZYX")
283 joint_id = addJointAndBody(model,JointModelSphericalZYX(),parent_id,global_placement,joint_name,Y,body_name);
285 else if (joint_type ==
"JointTypeFixed")
287 model.appendBodyToJoint(parent_id, Y, global_placement);
293 joint_id = (Model::JointIndex)model.njoints;
295 fixed_body_table_id_map[body_name] = parent_id;
296 fixed_placement_map[body_name] = global_placement;
300 std::cerr << joint_type <<
" is not supported.." << std::endl;
304 body_table_id_map[body_name] = joint_id;
307 std::cout <<
"==== Added Body ====" << std::endl;
308 std::cout <<
" joint name : " << joint_type << std::endl;
309 std::cout <<
" joint id : " << joint_id << std::endl;
310 std::cout <<
" joint parent id : " << parent_id << std::endl;
311 std::cout <<
" joint placement (wrt its parent):\n" << joint_placement << std::endl;
312 std::cout <<
" joint type : " << joint_type << std::endl;
313 std::cout <<
" body name : " << body_name << std::endl;
314 std::cout <<
" body inertia:\n" << Y << std::endl;
321 Model buildModel (
const std::string & filename,
bool freeFlyer,
bool verbose)
325 LuaTable model_table = LuaTable::fromFile (filename.c_str ());
326 LuaModelReadFromTable (model_table, model, freeFlyer, verbose);
Main pinocchio namespace.
std::string randomStringGenerator(const int len)
Generate a random string composed of alphanumeric symbols of a given length.