candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
RobotLoader.h
Go to the documentation of this file.
1
2#pragma once
3
5
6#include <pinocchio/multibody/fwd.hpp>
7#include <pinocchio/parsers/urdf.hpp>
8#include <pinocchio/parsers/srdf.hpp>
9
10#include <vector>
11#include <string>
12#include <filesystem>
13#include <spdlog/fmt/std.h>
14
15namespace candlewick::multibody {
16namespace pin = pinocchio;
17
18struct RobotSpec {
20 std::filesystem::path urdf_path;
22 std::filesystem::path srdf_path;
24 std::filesystem::path base_package_path;
26 std::filesystem::path relative_package_path;
29 bool has_free_flyer = false;
30
31 // Convert URDF and SRDF paths to absolute.
33 if (base_package_path.is_relative()) {
34 terminate_with_message("Field base_package_path must be absolute.");
35 }
36 if (urdf_path.is_relative()) {
38 }
39 if (srdf_path.is_relative()) {
41 }
42 return *this;
43 }
44};
45} // namespace candlewick::multibody
46
47FMT_BEGIN_NAMESPACE
48template <> struct formatter<candlewick::multibody::RobotSpec> {
49 constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }
50
51 template <typename FormatContext>
53 FormatContext &ctx) const {
54 return fmt::format_to(ctx.out(),
55 "RobotSpec{{\n"
56 " urdf_path: \"{}\"\n"
57 " srdf_path: \"{}\"\n"
58 " base_package_path: \"{}\"\n"
59 " relative_package_path: \"{}\"\n"
60 " has_free_flyer: {}\n"
61 "}}",
62 spec.urdf_path, spec.srdf_path,
64 spec.has_free_flyer);
65 }
66};
67FMT_END_NAMESPACE
68
69namespace candlewick::multibody {
70inline std::vector<std::string> getPackageDirs(const RobotSpec &spec) {
71 if (spec.relative_package_path.is_absolute()) {
73 "robot spec relative package path ({:s}) isn't relative.",
74 spec.relative_package_path.native());
75 }
76 if (spec.urdf_path.empty()) {
77 terminate_with_message("robot spec's urdf_path field cannot be empty.");
78 }
79 auto absolute_package_path =
81 return {
82 spec.base_package_path, spec.base_package_path.parent_path(),
83 absolute_package_path, absolute_package_path.parent_path(),
84 spec.urdf_path.parent_path(),
85 };
86}
87
88inline pin::Model &loadModel(const RobotSpec &spec, pin::Model &model,
89 bool verbose = false) {
90 if (spec.has_free_flyer) {
91 pin::urdf::buildModel(spec.urdf_path, pin::JointModelFreeFlyer(), model,
92 verbose);
93 } else {
94 pin::urdf::buildModel(spec.urdf_path, model, verbose);
95 }
96 if (std::filesystem::exists(spec.srdf_path)) {
97 pin::srdf::loadReferenceConfigurations(model, spec.srdf_path, verbose);
98 try {
99 pin::srdf::loadRotorParameters(model, spec.srdf_path, verbose);
100 } catch (const std::invalid_argument &) {
101 // do nothing
102 }
103 }
104 return model;
105}
106
107inline void loadModels(const RobotSpec &spec, pin::Model &model,
108 pin::GeometryModel *visual_model,
109 pin::GeometryModel *collision_model,
110 bool verbose = false) {
111 loadModel(spec, model, verbose);
112 auto package_dirs = getPackageDirs(spec);
113
114 if (visual_model)
115 pin::urdf::buildGeom(model, spec.urdf_path, pin::VISUAL, *visual_model,
116 package_dirs);
117
118 if (collision_model) {
119 pin::urdf::buildGeom(model, spec.urdf_path, pin::COLLISION,
120 *collision_model, package_dirs);
121 if (std::filesystem::exists(spec.srdf_path)) {
122 collision_model->addAllCollisionPairs();
123 pin::srdf::removeCollisionPairs(model, *collision_model, spec.srdf_path,
124 false);
125 }
126 }
127}
128
129} // 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:88
std::vector< std::string > getPackageDirs(const RobotSpec &spec)
Definition RobotLoader.h:70
void loadModels(const RobotSpec &spec, pin::Model &model, pin::GeometryModel *visual_model, pin::GeometryModel *collision_model, bool verbose=false)
Definition RobotLoader.h:107
Definition Camera.h:8
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
Definition RobotLoader.h:18
std::filesystem::path base_package_path
Path to the base package path.
Definition RobotLoader.h:24
std::filesystem::path urdf_path
Path to the URDF file.
Definition RobotLoader.h:20
RobotSpec & ensure_absolute_filepaths()
Definition RobotLoader.h:32
std::filesystem::path relative_package_path
Path to the actual model package, relative to the base path.
Definition RobotLoader.h:26
bool has_free_flyer
Definition RobotLoader.h:29
std::filesystem::path srdf_path
Path to the SRDF file.
Definition RobotLoader.h:22
constexpr auto parse(format_parse_context &ctx)
Definition RobotLoader.h:49
auto format(const candlewick::multibody::RobotSpec &spec, FormatContext &ctx) const
Definition RobotLoader.h:52