ros2_control - kilted
Loading...
Searching...
No Matches
data.hpp
1
19
20#pragma once
21
22#include <Eigen/Core>
23#include <Eigen/Geometry>
24#include <hardware_interface/types/hardware_interface_type_values.hpp>
25#include "control_toolbox/pid_ros.hpp"
26
27#include <random>
28#include <string>
29#include <vector>
30
32{
33
46
47enum class ActuatorType
48{
49 UNKNOWN,
50 MOTOR,
51 POSITION,
52 VELOCITY,
53 PASSIVE,
54 CUSTOM,
55 PLUGIN
56};
57
61struct InterfaceData
62{
63 explicit InterfaceData(const std::string& command_interface) : command_interface_(command_interface)
64 {
65 }
66
67 std::string command_interface_;
68 double command_ = std::numeric_limits<double>::quiet_NaN();
69 double state_ = std::numeric_limits<double>::quiet_NaN();
70
71 // this is the "sink" that will be part of the transmission Joint/Actuator handles
72 double transmission_passthrough_ = std::numeric_limits<double>::quiet_NaN();
73};
74
97{
98 std::string joint_name = "";
102 std::shared_ptr<control_toolbox::PidROS> pos_pid{ nullptr };
103 std::shared_ptr<control_toolbox::PidROS> vel_pid{ nullptr };
104 ActuatorType actuator_type{ ActuatorType::UNKNOWN };
105 int mj_joint_type = -1;
106 int mj_pos_adr = -1;
107 int mj_vel_adr = -1;
108 int mj_actuator_id = -1;
109
110 // Booleans record whether or not we should be writing commands to these interfaces
111 // based on if they have been claimed.
112 bool is_position_control_enabled{ false };
113 bool is_position_pid_control_enabled{ false };
114 bool is_velocity_pid_control_enabled{ false };
115 bool is_velocity_control_enabled{ false };
116 bool is_effort_control_enabled{ false };
117 bool has_pos_pid{ false };
118 bool has_vel_pid{ false };
119
120 void copy_state_to_transmission()
121 {
122 position_interface.transmission_passthrough_ = position_interface.state_;
123 velocity_interface.transmission_passthrough_ = velocity_interface.state_;
124 effort_interface.transmission_passthrough_ = effort_interface.state_;
125 }
126
127 void copy_command_from_transmission()
128 {
129 position_interface.command_ = position_interface.transmission_passthrough_;
130 velocity_interface.command_ = velocity_interface.transmission_passthrough_;
131 effort_interface.command_ = effort_interface.transmission_passthrough_;
132 }
133
134 void copy_command_to_state()
135 {
136 position_interface.state_ = position_interface.command_;
137 velocity_interface.state_ = velocity_interface.command_;
138 effort_interface.state_ = effort_interface.command_;
139 }
140};
141
157{
158 std::string name = "";
162
163 std::vector<std::string> command_interfaces = {};
164
165 bool is_mimic{ false };
166 long int mimicked_joint_index;
167 double mimic_multiplier;
168
169 bool is_position_control_enabled{ false };
170 bool is_velocity_control_enabled{ false };
171 bool is_effort_control_enabled{ false };
172
173 void copy_state_from_transmission()
174 {
175 position_interface.state_ = position_interface.transmission_passthrough_;
176 velocity_interface.state_ = velocity_interface.transmission_passthrough_;
177 effort_interface.state_ = effort_interface.transmission_passthrough_;
178 }
179
180 void copy_command_to_transmission()
181 {
182 position_interface.transmission_passthrough_ = position_interface.command_;
183 velocity_interface.transmission_passthrough_ = velocity_interface.command_;
184 effort_interface.transmission_passthrough_ = effort_interface.command_;
185 }
186
187 void copy_state_to_command()
188 {
189 position_interface.command_ = position_interface.state_;
190 velocity_interface.command_ = velocity_interface.state_;
191 effort_interface.command_ = effort_interface.state_;
192 }
193};
194
195template <typename T>
197{
198 std::string name;
199 T data;
200 int mj_sensor_index;
201};
202
211{
212 double period = 0.0;
213 double next_update_time = -std::numeric_limits<double>::infinity();
214
219 bool is_update_due(double time)
220 {
221 if (period <= 0.0)
222 {
223 return true;
224 }
225 // Absorbs floating point drift from accumulated timesteps (e.g. 10 * 0.005 landing just below 0.05).
226 constexpr double kEpsilon = 1e-9;
227 if (time + kEpsilon < next_update_time)
228 {
229 return false;
230 }
231 // Advance by whole periods so rates that don't divide the loop rate keep their average frequency, but
232 // resync instead of bursting if samples were missed (e.g. on the first call or after a long stall).
233 next_update_time += period;
234 if (next_update_time <= time + kEpsilon)
235 {
236 next_update_time = time + period;
237 }
238 return true;
239 }
240};
241
248{
249 kGaussian,
250 kUniform,
251};
252
258{
259 NoiseDistribution distribution = NoiseDistribution::kGaussian;
260 std::mt19937 rng{ std::random_device{}() };
261};
262
274{
275 std::string name;
278
279 double force_noise_stddev = 0.0;
280 double torque_noise_stddev = 0.0;
281 NoiseState noise;
282 SensorUpdateRate update_rate;
283};
284
299{
300 std::string name;
302 SensorData<Eigen::Vector3d> angular_velocity;
303 SensorData<Eigen::Vector3d> linear_acceleration;
304
305 double orientation_noise_stddev = 0.0;
306 double angular_velocity_noise_stddev = 0.0;
307 double linear_acceleration_noise_stddev = 0.0;
308 NoiseState noise;
309 SensorUpdateRate update_rate;
310
311 // Diagonal-only (independent per-axis noise) covariance, derived from the *_noise_stddev fields above at
312 // registration time. Left at all-zero (as before noise support existed) when noise is not configured.
313 std::vector<double> orientation_covariance;
314 std::vector<double> angular_velocity_covariance;
315 std::vector<double> linear_acceleration_covariance;
316};
317
329{
330 std::string name;
333
334 double position_noise_stddev = 0.0;
335 double orientation_noise_stddev = 0.0;
336 NoiseState noise;
337 SensorUpdateRate update_rate;
338};
339
348{
349 std::string name;
350 SensorData<Eigen::Vector3d> magnetic_field;
351
352 double magnetic_field_noise_stddev = 0.0;
353 NoiseState noise;
354 SensorUpdateRate update_rate;
355};
356
357} // namespace mujoco_ros2_control
constexpr char HW_IF_EFFORT[]
Constant defining effort interface name.
Definition hardware_interface_type_values.hpp:27
constexpr char HW_IF_VELOCITY[]
Constant defining velocity interface name.
Definition hardware_interface_type_values.hpp:23
constexpr char HW_IF_POSITION[]
Constant defining position interface name.
Definition hardware_interface_type_values.hpp:21
Definition data.hpp:32
ActuatorType
Definition data.hpp:48
NoiseDistribution
Definition data.hpp:248
Definition data.hpp:274
Definition data.hpp:299
Definition data.hpp:258
Definition data.hpp:197
bool is_update_due(double time)
Returns whether the sensor should be refreshed at simulation time time, and if so schedules the next ...
Definition data.hpp:219
Definition data.hpp:329
Definition data.hpp:157