ros2_control - rolling
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 <optional>
24#include <string>
25
26#define DEFINE_LIMIT_STRUCT(LimitType) \
27 struct LimitType \
28 { \
29 LimitType(double minimum_value, double maximum_value) \
30 : lower_limit(minimum_value), upper_limit(maximum_value) \
31 { \
32 } \
33 double lower_limit = -std::numeric_limits<double>::infinity(); \
34 double upper_limit = std::numeric_limits<double>::infinity(); \
35 };
36
37namespace joint_limits
38{
39
40DEFINE_LIMIT_STRUCT(PositionLimits)
41DEFINE_LIMIT_STRUCT(VelocityLimits)
42DEFINE_LIMIT_STRUCT(EffortLimits)
43DEFINE_LIMIT_STRUCT(AccelerationLimits)
44
46{
47 std::string joint_name;
48 std::optional<double> position = std::nullopt;
49 std::optional<double> velocity = std::nullopt;
50 std::optional<double> effort = std::nullopt;
51 std::optional<double> acceleration = std::nullopt;
52 std::optional<double> jerk = std::nullopt;
53
54 bool has_data() const
55 {
56 return has_position() || has_velocity() || has_effort() || has_acceleration() || has_jerk();
57 }
58
59 bool has_position() const { return position.has_value(); }
60
61 bool has_velocity() const { return velocity.has_value(); }
62
63 bool has_effort() const { return effort.has_value(); }
64
65 bool has_acceleration() const { return acceleration.has_value(); }
66
67 bool has_jerk() const { return jerk.has_value(); }
68
69 std::string to_string() const
70 {
71 std::string str;
72 str += "Joint: '" + joint_name + "', ";
73 if (has_position())
74 {
75 str += "position: " + std::to_string(position.value()) + ", ";
76 }
77 if (has_velocity())
78 {
79 str += "velocity: " + std::to_string(velocity.value()) + ", ";
80 }
81 if (has_effort())
82 {
83 str += "effort: " + std::to_string(effort.value()) + ", ";
84 }
85 if (has_acceleration())
86 {
87 str += "acceleration: " + std::to_string(acceleration.value()) + ", ";
88 }
89 if (has_jerk())
90 {
91 str += "jerk: " + std::to_string(jerk.value());
92 }
93 // trim the last comma and space
94 if (!str.empty() && str.back() == ' ')
95 {
96 str.pop_back();
97 }
98 if (!str.empty() && str.back() == ',')
99 {
100 str.pop_back();
101 }
102 return str;
103 }
104};
105
107{
108 std::string joint_name;
111 JointControlInterfacesData prev_command;
113
114 bool has_actual_data() const { return actual.has_data(); }
115
116 bool has_command_data() const { return command.has_data(); }
117
118 bool has_limited_data() const { return limited.has_data(); }
119
120 void set_joint_name(const std::string & name)
121 {
122 joint_name = name;
123 actual.joint_name = name;
124 command.joint_name = name;
125 limited.joint_name = name;
126 prev_command.joint_name = name;
127 }
128
129 std::string to_string() const
130 {
131 return fmt::format(
132 FMT_COMPILE("Joint: '{}', (actual: [{}], command: [{}], limited: [{}])"), joint_name,
133 actual.to_string(), command.to_string(), limited.to_string());
134 }
135};
136
137} // namespace joint_limits
138#endif // JOINT_LIMITS__DATA_STRUCTURES_HPP_
Definition data_structures.hpp:38
Definition data_structures.hpp:46
Definition data_structures.hpp:107