candlewick 0.7.0-59-g23c6
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
RobotLoader.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <pinocchio/multibody/fwd.hpp>
6#include <pinocchio/parsers/urdf.hpp>
7#include <pinocchio/parsers/srdf.hpp>
8
9#include <vector>
10#include <string>
11#include <filesystem>
12
13namespace candlewick::multibody {
14namespace pin = pinocchio;
15
16struct RobotSpec {
18 std::filesystem::path urdf_path;
20 std::filesystem::path srdf_path;
22 std::filesystem::path base_package_path;
24 std::filesystem::path relative_package_path;
27 bool has_free_flyer = false;
28
29 // Convert URDF and SRDF paths to absolute.
31 if (base_package_path.is_relative()) {
32 terminate_with_message("Field base_package_path must be absolute.");
33 }
34 if (urdf_path.is_relative()) {
36 }
37 if (srdf_path.is_relative()) {
39 }
40 return *this;
41 }
42};
43
44inline std::vector<std::string> getPackageDirs(const RobotSpec &spec) {
45 if (spec.relative_package_path.is_absolute()) {
47 "robot spec relative package path ({:s}) isn't relative.",
48 spec.relative_package_path.native());
49 }
50 if (spec.urdf_path.empty()) {
51 terminate_with_message("robot spec's urdf_path field cannot be empty.");
52 }
53 auto absolute_package_path =
55 return {
56 spec.base_package_path, spec.base_package_path.parent_path(),
57 absolute_package_path, absolute_package_path.parent_path(),
58 spec.urdf_path.parent_path(),
59 };
60}
61
62inline pin::Model &loadModel(const RobotSpec &spec, pin::Model &model,
63 bool verbose = false) {
64 if (spec.has_free_flyer) {
65 pin::urdf::buildModel(spec.urdf_path, pin::JointModelFreeFlyer(), model,
66 verbose);
67 } else {
68 pin::urdf::buildModel(spec.urdf_path, model, verbose);
69 }
70 if (std::filesystem::exists(spec.srdf_path)) {
71 pin::srdf::loadReferenceConfigurations(model, spec.srdf_path, verbose);
72 try {
73 pin::srdf::loadRotorParameters(model, spec.srdf_path, verbose);
74 } catch (const std::invalid_argument &) {
75 // do nothing
76 }
77 }
78 return model;
79}
80
81inline void loadModels(const RobotSpec &spec, pin::Model &model,
82 pin::GeometryModel *visual_model,
83 pin::GeometryModel *collision_model,
84 bool verbose = false) {
85 loadModel(spec, model, verbose);
86 auto package_dirs = getPackageDirs(spec);
87
88 if (visual_model)
89 pin::urdf::buildGeom(model, spec.urdf_path, pin::VISUAL, *visual_model,
90 package_dirs);
91
92 if (collision_model) {
93 pin::urdf::buildGeom(model, spec.urdf_path, pin::COLLISION,
94 *collision_model, package_dirs);
95 if (std::filesystem::exists(spec.srdf_path)) {
96 collision_model->addAllCollisionPairs();
97 pin::srdf::removeCollisionPairs(model, *collision_model, spec.srdf_path,
98 false);
99 }
100 }
101}
102
103} // namespace candlewick::multibody
Support for the Pinocchio rigid-body algorithms library and the Coal collision detection library.
Definition LoadPinocchioGeometry.h:8
pin::Model & loadModel(const RobotSpec &spec, pin::Model &model, bool verbose=false)
Definition RobotLoader.h:62
std::vector< std::string > getPackageDirs(const RobotSpec &spec)
Definition RobotLoader.h:44
void loadModels(const RobotSpec &spec, pin::Model &model, pin::GeometryModel *visual_model, pin::GeometryModel *collision_model, bool verbose=false)
Definition RobotLoader.h:81
void terminate_with_message(std::source_location location, std::string_view fmt, Ts &&...args)
Definition errors.h:51
Definition RobotLoader.h:16
std::filesystem::path base_package_path
Path to the base package path.
Definition RobotLoader.h:22
std::filesystem::path urdf_path
Path to the URDF file.
Definition RobotLoader.h:18
RobotSpec & ensure_absolute_filepaths()
Definition RobotLoader.h:30
std::filesystem::path relative_package_path
Path to the actual model package, relative to the base path.
Definition RobotLoader.h:24
bool has_free_flyer
Definition RobotLoader.h:27
std::filesystem::path srdf_path
Path to the SRDF file.
Definition RobotLoader.h:20