ros2_control - jazzy
Loading...
Searching...
No Matches
data_structures.hpp
1// Copyright 2024 PAL Robotics S.L.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#ifndef JOINT_LIMITS__DATA_STRUCTURES_HPP_
18#define JOINT_LIMITS__DATA_STRUCTURES_HPP_
19
20#include <fmt/compile.h>
21
22#include <limits>
23#include <memory>
24#include <optional>
25#include <string>
26
27#define DEFINE_LIMIT_STRUCT(LimitType) \
28 struct LimitType \
29 { \
30 LimitType(double minimum_value, double maximum_value) \
31 : lower_limit(minimum_value), upper_limit(maximum_value) \
32 { \
33 } \
34 double lower_limit = -std::numeric_limits<double>::infinity(); \
35 double upper_limit = std::numeric_limits<double>::infinity(); \
36 };
37
38namespace joint_limits
39{
40
41DEFINE_LIMIT_STRUCT(PositionLimits);
42DEFINE_LIMIT_STRUCT(VelocityLimits);
43DEFINE_LIMIT_STRUCT(EffortLimits);
44DEFINE_LIMIT_STRUCT(AccelerationLimits);
45
47{
48 std::string joint_name;
49 std::optional<double> position = std::nullopt;
50 std::optional<double> velocity = std::nullopt;
51 std::optional<double> effort = std::nullopt;
52 std::optional<double> acceleration = std::nullopt;
53 std::optional<double> jerk = std::nullopt;
54
55 bool has_data() const
56 {
57 return has_position() || has_velocity() || has_effort() || has_acceleration() || has_jerk();
58 }
59
60 bool has_position() const { return position.has_value(); }
61
62 bool has_velocity() const { return velocity.has_value(); }
63
64 bool has_effort() const { return effort.has_value(); }
65
66 bool has_acceleration() const { return acceleration.has_value(); }
67
68 bool has_jerk() const { return jerk.has_value(); }
69
70 std::string to_string() const
71 {
72 std::string str;
73 str += "Joint: '" + joint_name + "', ";
74 if (has_position())
75 {
76 str += "position: " + std::to_string(position.value()) + ", ";
77 }
78 if (has_velocity())
79 {
80 str += "velocity: " + std::to_string(velocity.value()) + ", ";
81 }
82 if (has_effort())
83 {
84 str += "effort: " + std::to_string(effort.value()) + ", ";
85 }
86 if (has_acceleration())
87 {
88 str += "acceleration: " + std::to_string(acceleration.value()) + ", ";
89 }
90 if (has_jerk())
91 {
92 str += "jerk: " + std::to_string(jerk.value());
93 }
94 // trim the last comma and space
95 if (!str.empty() && str.back() == ' ')
96 {
97 str.pop_back();
98 }
99 if (!str.empty() && str.back() == ',')
100 {
101 str.pop_back();
102 }
103 return str;
104 }
105};
106
108{
109 std::string joint_name;
112 JointControlInterfacesData prev_command;
114
115 bool has_actual_data() const { return actual.has_data(); }
116
117 bool has_command_data() const { return command.has_data(); }
118
119 bool has_limited_data() const { return limited.has_data(); }
120
121 void set_joint_name(const std::string & name)
122 {
123 joint_name = name;
124 actual.joint_name = name;
125 command.joint_name = name;
126 limited.joint_name = name;
127 prev_command.joint_name = name;
128 }
129
130 std::string to_string() const
131 {
132 return fmt::format(
133 FMT_COMPILE("Joint: '{}', (actual: [{}], command: [{}], limited: [{}])"), joint_name,
134 actual.to_string(), command.to_string(), limited.to_string());
135 }
136};
137
138} // namespace joint_limits
139#endif // JOINT_LIMITS__DATA_STRUCTURES_HPP_
Definition data_structures.hpp:39
Definition data_structures.hpp:47
Definition data_structures.hpp:108