|
20 | 20 | #include <memory> |
21 | 21 | #include <string> |
22 | 22 | #include <string.h> |
| 23 | +#include <vector> |
23 | 24 |
|
24 | 25 | #include "sdf/sdf_config.h" |
25 | 26 | #include "sdf/Filesystem.hh" |
| 27 | +#include "sdf/Link.hh" |
| 28 | +#include "sdf/Model.hh" |
26 | 29 | #include "sdf/Root.hh" |
27 | 30 | #include "sdf/parser.hh" |
28 | 31 | #include "sdf/PrintConfig.hh" |
29 | 32 | #include "sdf/system_util.hh" |
30 | 33 |
|
| 34 | +#include "ignition/math/Inertial.hh" |
| 35 | + |
31 | 36 | #include "FrameSemantics.hh" |
32 | 37 | #include "ScopedGraph.hh" |
33 | 38 | #include "ign.hh" |
@@ -267,3 +272,111 @@ extern "C" SDFORMAT_VISIBLE int cmdGraph( |
267 | 272 |
|
268 | 273 | return 0; |
269 | 274 | } |
| 275 | + |
| 276 | +////////////////////////////////////////////////// |
| 277 | +extern "C" SDFORMAT_VISIBLE int cmdInertialStats( |
| 278 | + const char *_path) |
| 279 | +{ |
| 280 | + if (!sdf::filesystem::exists(_path)) |
| 281 | + { |
| 282 | + std::cerr << "Error: File [" << _path << "] does not exist.\n"; |
| 283 | + return -1; |
| 284 | + } |
| 285 | + |
| 286 | + sdf::Root root; |
| 287 | + sdf::Errors errors = root.Load(_path); |
| 288 | + if (!errors.empty()) |
| 289 | + { |
| 290 | + std::cerr << errors << std::endl; |
| 291 | + return -1; |
| 292 | + } |
| 293 | + |
| 294 | + if (root.WorldCount() > 0) |
| 295 | + { |
| 296 | + std::cerr << "Error: Expected a model file but received a world file." |
| 297 | + << std::endl; |
| 298 | + return -1; |
| 299 | + } |
| 300 | + |
| 301 | + const sdf::Model *model = root.Model(); |
| 302 | + if (!model) |
| 303 | + { |
| 304 | + std::cerr << "Error: Could not find the model." << std::endl; |
| 305 | + return -1; |
| 306 | + } |
| 307 | + |
| 308 | + if (model->ModelCount() > 0) |
| 309 | + { |
| 310 | + std::cout << "Warning: Inertial properties of links in nested" |
| 311 | + " models will not be included." << std::endl; |
| 312 | + } |
| 313 | + |
| 314 | + ignition::math::Inertiald totalInertial; |
| 315 | + |
| 316 | + for (uint64_t i = 0; i < model->LinkCount(); i++) |
| 317 | + { |
| 318 | + ignition::math::Pose3d linkPoseRelativeToModel; |
| 319 | + errors = model->LinkByIndex(i)->SemanticPose(). |
| 320 | + Resolve(linkPoseRelativeToModel, "__model__"); |
| 321 | + |
| 322 | + auto currentLinkInertial = model->LinkByIndex(i)->Inertial(); |
| 323 | + currentLinkInertial.SetPose(linkPoseRelativeToModel * |
| 324 | + currentLinkInertial.Pose()); |
| 325 | + totalInertial += currentLinkInertial; |
| 326 | + } |
| 327 | + |
| 328 | + auto totalMass = totalInertial.MassMatrix().Mass(); |
| 329 | + auto xCentreOfMass = totalInertial.Pose().Pos().X(); |
| 330 | + auto yCentreOfMass = totalInertial.Pose().Pos().Y(); |
| 331 | + auto zCentreOfMass = totalInertial.Pose().Pos().Z(); |
| 332 | + |
| 333 | + std::cout << "Inertial statistics for model: " << model->Name() << std::endl; |
| 334 | + std::cout << "---" << std::endl; |
| 335 | + std::cout << "Total mass of the model: " << totalMass << std::endl; |
| 336 | + std::cout << "---" << std::endl; |
| 337 | + |
| 338 | + std::cout << "Centre of mass in model frame: " << std::endl; |
| 339 | + std::cout << "X: " << xCentreOfMass << std::endl; |
| 340 | + std::cout << "Y: " << yCentreOfMass << std::endl; |
| 341 | + std::cout << "Z: " << zCentreOfMass << std::endl; |
| 342 | + std::cout << "---" << std::endl; |
| 343 | + |
| 344 | + std::cout << "Moment of inertia matrix: " << std::endl; |
| 345 | + |
| 346 | + // Pretty print the MOI matrix |
| 347 | + std::stringstream ss; |
| 348 | + ss << totalInertial.Moi(); |
| 349 | + |
| 350 | + std::string s; |
| 351 | + auto maxLength = 0u; |
| 352 | + std::vector<std::string> moiVector; |
| 353 | + while ( std::getline(ss, s, ' ' ) ) |
| 354 | + { |
| 355 | + moiVector.push_back(s); |
| 356 | + if (s.size() > maxLength) |
| 357 | + { |
| 358 | + maxLength = s.size(); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + for (int i = 0; i < 9; i++) |
| 363 | + { |
| 364 | + int spacePadding = maxLength - moiVector[i].size(); |
| 365 | + // Print the matrix element |
| 366 | + std::cout << moiVector[i]; |
| 367 | + for (int j = 0; j < spacePadding; j++) |
| 368 | + { |
| 369 | + std::cout << " "; |
| 370 | + } |
| 371 | + // Add space for the next element |
| 372 | + std::cout << " "; |
| 373 | + // Add '\n' if the next row is about to start |
| 374 | + if ((i+1)%3 == 0) |
| 375 | + { |
| 376 | + std::cout << "\n"; |
| 377 | + } |
| 378 | + } |
| 379 | + std::cout << "---" << std::endl; |
| 380 | + |
| 381 | + return 0; |
| 382 | +} |
0 commit comments