ros2_control - rolling
Loading...
Searching...
No Matches
differential_transmission.hpp
1// Copyright 2020 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
15#ifndef TRANSMISSION_INTERFACE__DIFFERENTIAL_TRANSMISSION_HPP_
16#define TRANSMISSION_INTERFACE__DIFFERENTIAL_TRANSMISSION_HPP_
17
18#include <fmt/compile.h>
19
20#include <cassert>
21#include <string>
22#include <vector>
23
24#include "hardware_interface/types/hardware_interface_type_values.hpp"
25#include "transmission_interface/accessor.hpp"
26#include "transmission_interface/exception.hpp"
27#include "transmission_interface/transmission.hpp"
28
30{
32
112constexpr auto HW_IF_ABSOLUTE_POSITION = "absolute_position";
113
115{
116public:
124 const std::vector<double> & actuator_reduction, const std::vector<double> & joint_reduction,
125 const std::vector<double> & joint_offset = {0.0, 0.0});
126
132 void configure(
133 const std::vector<JointHandle> & joint_handles,
134 const std::vector<ActuatorHandle> & actuator_handles) override;
135
137
142 void actuator_to_joint() override;
143
145
150 void joint_to_actuator() override;
151
152 std::size_t num_actuators() const override { return 2; }
153 std::size_t num_joints() const override { return 2; }
154
155 const std::vector<double> & get_actuator_reduction() const { return actuator_reduction_; }
156 const std::vector<double> & get_joint_reduction() const { return joint_reduction_; }
157 const std::vector<double> & get_joint_offset() const { return joint_offset_; }
158
160 std::string get_handles_info() const;
161
162protected:
163 std::vector<double> actuator_reduction_;
164 std::vector<double> joint_reduction_;
165 std::vector<double> joint_offset_;
166
167 std::vector<JointHandle> joint_position_;
168 std::vector<JointHandle> joint_velocity_;
169 std::vector<JointHandle> joint_effort_;
170 std::vector<JointHandle> joint_torque_;
171 std::vector<JointHandle> joint_force_;
172 std::vector<JointHandle> joint_absolute_position_;
173
174 std::vector<ActuatorHandle> actuator_position_;
175 std::vector<ActuatorHandle> actuator_velocity_;
176 std::vector<ActuatorHandle> actuator_effort_;
177 std::vector<ActuatorHandle> actuator_torque_;
178 std::vector<ActuatorHandle> actuator_force_;
179 std::vector<ActuatorHandle> actuator_absolute_position_;
180};
181
183 const std::vector<double> & actuator_reduction, const std::vector<double> & joint_reduction,
184 const std::vector<double> & joint_offset)
185: actuator_reduction_(actuator_reduction),
186 joint_reduction_(joint_reduction),
187 joint_offset_(joint_offset)
188{
189 if (
190 num_actuators() != actuator_reduction_.size() || num_joints() != joint_reduction_.size() ||
191 num_joints() != joint_offset_.size())
192 {
193 throw Exception("Reduction and offset vectors must have size 2.");
194 }
195
196 if (
197 0.0 == actuator_reduction_[0] || 0.0 == actuator_reduction_[1] || 0.0 == joint_reduction_[0] ||
198 0.0 == joint_reduction_[1])
199 {
200 throw Exception("Transmission reduction ratios cannot be zero.");
201 }
202}
203
205 const std::vector<JointHandle> & joint_handles,
206 const std::vector<ActuatorHandle> & actuator_handles)
207{
208 if (joint_handles.empty())
209 {
210 throw Exception("No joint handles were passed in");
211 }
212
213 if (actuator_handles.empty())
214 {
215 throw Exception("No actuator handles were passed in");
216 }
217
218 const auto joint_names = get_names(joint_handles);
219 if (joint_names.size() != 2)
220 {
221 throw Exception(
222 fmt::format(
223 FMT_COMPILE("There should be exactly two unique joint names but was given '{}'."),
224 to_string(joint_names)));
225 }
226 const auto actuator_names = get_names(actuator_handles);
227 if (actuator_names.size() != 2)
228 {
229 throw Exception(
230 fmt::format(
231 FMT_COMPILE("There should be exactly two unique actuator names but was given '{}'."),
232 to_string(actuator_names)));
233 }
234
235 joint_position_ =
236 get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_POSITION);
237 joint_velocity_ =
238 get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_VELOCITY);
239 joint_effort_ = get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_EFFORT);
240 joint_torque_ = get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_TORQUE);
241 joint_force_ = get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_FORCE);
242 joint_absolute_position_ =
243 get_ordered_handles(joint_handles, joint_names, HW_IF_ABSOLUTE_POSITION);
244
245 if (!joint_position_.empty() && joint_position_.size() != 2)
246 {
247 throw Exception(
248 fmt::format(
249 FMT_COMPILE("Not enough valid or required joint position handles were present. \n{}"),
251 }
252 if (!joint_velocity_.empty() && joint_velocity_.size() != 2)
253 {
254 throw Exception(
255 fmt::format(
256 FMT_COMPILE("Not enough valid or required joint velocity handles were present. \n{}"),
258 }
259 if (!joint_effort_.empty() && joint_effort_.size() != 2)
260 {
261 throw Exception(
262 fmt::format(
263 FMT_COMPILE("Not enough valid or required joint effort handles were present. \n{}"),
265 }
266 if (!joint_torque_.empty() && joint_torque_.size() != 2)
267 {
268 throw Exception(
269 fmt::format(
270 FMT_COMPILE("Not enough valid or required joint torque handles were present. \n{}"),
272 }
273 if (!joint_force_.empty() && joint_force_.size() != 2)
274 {
275 throw Exception(
276 fmt::format(
277 FMT_COMPILE("Not enough valid or required joint force handles were present. \n{}"),
279 }
280 if (!joint_absolute_position_.empty() && joint_absolute_position_.size() != 2)
281 {
282 throw Exception(
283 fmt::format(
284 FMT_COMPILE(
285 "Not enough valid or required joint absolute position handles were present. \n{}"),
287 }
288
289 actuator_position_ =
290 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_POSITION);
291 actuator_velocity_ =
292 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_VELOCITY);
293 actuator_effort_ =
294 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_EFFORT);
295 actuator_torque_ =
296 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_TORQUE);
297 actuator_force_ =
298 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_FORCE);
299 actuator_absolute_position_ =
300 get_ordered_handles(actuator_handles, actuator_names, HW_IF_ABSOLUTE_POSITION);
301
302 if (!actuator_position_.empty() && actuator_position_.size() != 2)
303 {
304 throw Exception(
305 fmt::format(
306 FMT_COMPILE("Not enough valid or required actuator position handles were present. \n{}"),
308 }
309 if (!actuator_velocity_.empty() && actuator_velocity_.size() != 2)
310 {
311 throw Exception(
312 fmt::format(
313 FMT_COMPILE("Not enough valid or required actuator velocity handles were present. \n{}"),
315 }
316 if (!actuator_effort_.empty() && actuator_effort_.size() != 2)
317 {
318 throw Exception(
319 fmt::format(
320 FMT_COMPILE("Not enough valid or required actuator effort handles were present. \n{}"),
322 }
323 if (!actuator_torque_.empty() && actuator_torque_.size() != 2)
324 {
325 throw Exception(
326 fmt::format(
327 FMT_COMPILE("Not enough valid or required actuator torque handles were present. \n{}"),
329 }
330 if (!actuator_force_.empty() && actuator_force_.size() != 2)
331 {
332 throw Exception(
333 fmt::format(
334 FMT_COMPILE("Not enough valid or required actuator force handles were present. \n{}"),
336 }
337 if (!actuator_absolute_position_.empty() && actuator_absolute_position_.size() != 2)
338 {
339 throw Exception(
340 fmt::format(
341 FMT_COMPILE(
342 "Not enough valid or required actuator absolute position handles were "
343 "present. \n{}"),
345 }
346
347 if (
348 !joint_position_.empty() && !actuator_position_.empty() &&
349 joint_position_.size() != actuator_position_.size())
350 {
351 throw Exception(
352 fmt::format(
353 FMT_COMPILE("Pair-wise mismatch on position interfaces. \n{}"), get_handles_info()));
354 }
355 if (
356 !joint_velocity_.empty() && !actuator_velocity_.empty() &&
357 joint_velocity_.size() != actuator_velocity_.size())
358 {
359 throw Exception(
360 fmt::format(
361 FMT_COMPILE("Pair-wise mismatch on velocity interfaces. \n{}"), get_handles_info()));
362 }
363 if (
364 !joint_effort_.empty() && !actuator_effort_.empty() &&
365 joint_effort_.size() != actuator_effort_.size())
366 {
367 throw Exception(
368 fmt::format(
369 FMT_COMPILE("Pair-wise mismatch on effort interfaces. \n{}"), get_handles_info()));
370 }
371 if (
372 !joint_torque_.empty() && !actuator_torque_.empty() &&
373 joint_torque_.size() != actuator_torque_.size())
374 {
375 throw Exception(
376 fmt::format(
377 FMT_COMPILE("Pair-wise mismatch on torque interfaces. \n{}"), get_handles_info()));
378 }
379 if (
380 !joint_force_.empty() && !actuator_force_.empty() &&
381 joint_force_.size() != actuator_force_.size())
382 {
383 throw Exception(
384 fmt::format(FMT_COMPILE("Pair-wise mismatch on force interfaces. \n{}"), get_handles_info()));
385 }
386 if (
387 !joint_absolute_position_.empty() && !actuator_absolute_position_.empty() &&
388 joint_absolute_position_.size() != actuator_absolute_position_.size())
389 {
390 throw Exception(
391 fmt::format(
392 FMT_COMPILE("Pair-wise mismatch on absolute position interfaces. \n{}"),
394 }
395
396 // Check at least one pair-wise interface is available
397 if (!((!joint_position_.empty() && !actuator_position_.empty() &&
398 joint_position_.size() == actuator_position_.size()) ||
399 (!joint_velocity_.empty() && !actuator_velocity_.empty() &&
400 joint_velocity_.size() == actuator_velocity_.size()) ||
401 (!joint_effort_.empty() && !actuator_effort_.empty() &&
402 joint_effort_.size() == actuator_effort_.size()) ||
403 (!joint_torque_.empty() && !actuator_torque_.empty() &&
404 joint_torque_.size() == actuator_torque_.size()) ||
405 (!joint_force_.empty() && !actuator_force_.empty() &&
406 joint_force_.size() == actuator_force_.size()) ||
407 (!joint_absolute_position_.empty() && !actuator_absolute_position_.empty() &&
408 joint_absolute_position_.size() == actuator_absolute_position_.size())))
409 {
410 throw Exception(
411 fmt::format(
412 FMT_COMPILE("No pair-wise interface available between joints and actuators. \n{}"),
414 }
415}
416
418{
419 const auto & ar = actuator_reduction_;
420 const auto & jr = joint_reduction_;
421
422 auto & act_pos = actuator_position_;
423 auto & joint_pos = joint_position_;
424 if (act_pos.size() == num_actuators() && joint_pos.size() == num_joints())
425 {
426 assert(act_pos[0] && act_pos[1] && joint_pos[0] && joint_pos[1]);
427
428 joint_pos[0].set_value(
429 (act_pos[0].get_value() / ar[0] + act_pos[1].get_value() / ar[1]) / (2.0 * jr[0]) +
430 joint_offset_[0]);
431 joint_pos[1].set_value(
432 (act_pos[0].get_value() / ar[0] - act_pos[1].get_value() / ar[1]) / (2.0 * jr[1]) +
433 joint_offset_[1]);
434 }
435
436 auto & act_vel = actuator_velocity_;
437 auto & joint_vel = joint_velocity_;
438 if (act_vel.size() == num_actuators() && joint_vel.size() == num_joints())
439 {
440 assert(act_vel[0] && act_vel[1] && joint_vel[0] && joint_vel[1]);
441
442 joint_vel[0].set_value(
443 (act_vel[0].get_value() / ar[0] + act_vel[1].get_value() / ar[1]) / (2.0 * jr[0]));
444 joint_vel[1].set_value(
445 (act_vel[0].get_value() / ar[0] - act_vel[1].get_value() / ar[1]) / (2.0 * jr[1]));
446 }
447
448 auto & act_eff = actuator_effort_;
449 auto & joint_eff = joint_effort_;
450 if (act_eff.size() == num_actuators() && joint_eff.size() == num_joints())
451 {
452 assert(act_eff[0] && act_eff[1] && joint_eff[0] && joint_eff[1]);
453
454 joint_eff[0].set_value(
455 jr[0] * (act_eff[0].get_value() * ar[0] + act_eff[1].get_value() * ar[1]));
456 joint_eff[1].set_value(
457 jr[1] * (act_eff[0].get_value() * ar[0] - act_eff[1].get_value() * ar[1]));
458 }
459
460 auto & act_tor = actuator_torque_;
461 auto & joint_tor = joint_torque_;
462 if (act_tor.size() == num_actuators() && joint_tor.size() == num_joints())
463 {
464 assert(act_tor[0] && act_tor[1] && joint_tor[0] && joint_tor[1]);
465
466 joint_tor[0].set_value(
467 jr[0] * (act_tor[0].get_value() * ar[0] + act_tor[1].get_value() * ar[1]));
468 joint_tor[1].set_value(
469 jr[1] * (act_tor[0].get_value() * ar[0] - act_tor[1].get_value() * ar[1]));
470 }
471
472 auto & act_for = actuator_force_;
473 auto & joint_for = joint_force_;
474 if (act_for.size() == num_actuators() && joint_for.size() == num_joints())
475 {
476 assert(act_for[0] && act_for[1] && joint_for[0] && joint_for[1]);
477
478 joint_for[0].set_value(
479 jr[0] * (act_for[0].get_value() * ar[0] + act_for[1].get_value() * ar[1]));
480 joint_for[1].set_value(
481 jr[1] * (act_for[0].get_value() * ar[0] - act_for[1].get_value() * ar[1]));
482 }
483
484 auto & act_abs_pos = actuator_absolute_position_;
485 auto & joint_abs_pos = joint_absolute_position_;
486 if (act_abs_pos.size() == num_actuators() && joint_abs_pos.size() == num_joints())
487 {
488 assert(act_abs_pos[0] && act_abs_pos[1] && joint_abs_pos[0] && joint_abs_pos[1]);
489
490 joint_abs_pos[0].set_value(
491 (act_abs_pos[0].get_value() / ar[0] + act_abs_pos[1].get_value() / ar[1]) / (2.0 * jr[0]) +
492 joint_offset_[0]);
493 joint_abs_pos[1].set_value(
494 (act_abs_pos[0].get_value() / ar[0] - act_abs_pos[1].get_value() / ar[1]) / (2.0 * jr[1]) +
495 joint_offset_[1]);
496 }
497}
498
500{
501 const auto & ar = actuator_reduction_;
502 const auto & jr = joint_reduction_;
503
504 auto & act_pos = actuator_position_;
505 auto & joint_pos = joint_position_;
506 if (act_pos.size() == num_actuators() && joint_pos.size() == num_joints())
507 {
508 assert(act_pos[0] && act_pos[1] && joint_pos[0] && joint_pos[1]);
509
510 double joints_offset_applied[2] = {
511 joint_pos[0].get_value() - joint_offset_[0], joint_pos[1].get_value() - joint_offset_[1]};
512 act_pos[0].set_value(
513 (joints_offset_applied[0] * jr[0] + joints_offset_applied[1] * jr[1]) * ar[0]);
514 act_pos[1].set_value(
515 (joints_offset_applied[0] * jr[0] - joints_offset_applied[1] * jr[1]) * ar[1]);
516 }
517
518 auto & act_vel = actuator_velocity_;
519 auto & joint_vel = joint_velocity_;
520 if (act_vel.size() == num_actuators() && joint_vel.size() == num_joints())
521 {
522 assert(act_vel[0] && act_vel[1] && joint_vel[0] && joint_vel[1]);
523
524 act_vel[0].set_value(
525 (joint_vel[0].get_value() * jr[0] + joint_vel[1].get_value() * jr[1]) * ar[0]);
526 act_vel[1].set_value(
527 (joint_vel[0].get_value() * jr[0] - joint_vel[1].get_value() * jr[1]) * ar[1]);
528 }
529
530 auto & act_eff = actuator_effort_;
531 auto & joint_eff = joint_effort_;
532 if (act_eff.size() == num_actuators() && joint_eff.size() == num_joints())
533 {
534 assert(act_eff[0] && act_eff[1] && joint_eff[0] && joint_eff[1]);
535
536 act_eff[0].set_value(
537 (joint_eff[0].get_value() / jr[0] + joint_eff[1].get_value() / jr[1]) / (2.0 * ar[0]));
538 act_eff[1].set_value(
539 (joint_eff[0].get_value() / jr[0] - joint_eff[1].get_value() / jr[1]) / (2.0 * ar[1]));
540 }
541
542 auto & act_tor = actuator_torque_;
543 auto & joint_tor = joint_torque_;
544 if (act_tor.size() == num_actuators() && joint_tor.size() == num_joints())
545 {
546 assert(act_tor[0] && act_tor[1] && joint_tor[0] && joint_tor[1]);
547
548 act_tor[0].set_value(
549 (joint_tor[0].get_value() / jr[0] + joint_tor[1].get_value() / jr[1]) / (2.0 * ar[0]));
550 act_tor[1].set_value(
551 (joint_tor[0].get_value() / jr[0] - joint_tor[1].get_value() / jr[1]) / (2.0 * ar[1]));
552 }
553
554 auto & act_for = actuator_force_;
555 auto & joint_for = joint_force_;
556 if (act_for.size() == num_actuators() && joint_for.size() == num_joints())
557 {
558 assert(act_for[0] && act_for[1] && joint_for[0] && joint_for[1]);
559
560 act_for[0].set_value(
561 (joint_for[0].get_value() / jr[0] + joint_for[1].get_value() / jr[1]) / (2.0 * ar[0]));
562 act_for[1].set_value(
563 (joint_for[0].get_value() / jr[0] - joint_for[1].get_value() / jr[1]) / (2.0 * ar[1]));
564 }
565}
566
568{
569 return fmt::format(
570 FMT_COMPILE(
571 "Got the following handles:\n"
572 "Joint position: {}, Actuator position: {}\n"
573 "Joint velocity: {}, Actuator velocity: {}\n"
574 "Joint effort: {}, Actuator effort: {}\n"
575 "Joint torque: {}, Actuator torque: {}\n"
576 "Joint force: {}, Actuator force: {}\n"
577 "Joint absolute position: {}, Actuator absolute position: {}"),
578 to_string(get_names(joint_position_)), to_string(get_names(actuator_position_)),
579 to_string(get_names(joint_velocity_)), to_string(get_names(actuator_velocity_)),
580 to_string(get_names(joint_effort_)), to_string(get_names(actuator_effort_)),
581 to_string(get_names(joint_torque_)), to_string(get_names(actuator_torque_)),
582 to_string(get_names(joint_force_)), to_string(get_names(actuator_force_)),
583 to_string(get_names(joint_absolute_position_)),
584 to_string(get_names(actuator_absolute_position_)));
585}
586
587} // namespace transmission_interface
588
589#endif // TRANSMISSION_INTERFACE__DIFFERENTIAL_TRANSMISSION_HPP_
Definition differential_transmission.hpp:115
DifferentialTransmission(const std::vector< double > &actuator_reduction, const std::vector< double > &joint_reduction, const std::vector< double > &joint_offset={0.0, 0.0})
Definition differential_transmission.hpp:182
std::size_t num_joints() const override
Definition differential_transmission.hpp:153
void joint_to_actuator() override
Transform variables from joint to actuator space.
Definition differential_transmission.hpp:499
void actuator_to_joint() override
Transform variables from actuator to joint space.
Definition differential_transmission.hpp:417
std::string get_handles_info() const
Get human-friendly report of handles.
Definition differential_transmission.hpp:567
void configure(const std::vector< JointHandle > &joint_handles, const std::vector< ActuatorHandle > &actuator_handles) override
Definition differential_transmission.hpp:204
std::size_t num_actuators() const override
Definition differential_transmission.hpp:152
Definition exception.hpp:23
Abstract base class for representing mechanical transmissions.
Definition transmission.hpp:46
constexpr char HW_IF_EFFORT[]
Constant defining effort interface name.
Definition hardware_interface_type_values.hpp:27
constexpr char HW_IF_TORQUE[]
Constant defining torque interface name.
Definition hardware_interface_type_values.hpp:29
constexpr char HW_IF_FORCE[]
Constant defining force interface name.
Definition hardware_interface_type_values.hpp:31
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 accessor.hpp:25
constexpr auto HW_IF_ABSOLUTE_POSITION
Implementation of a differential transmission.
Definition differential_transmission.hpp:112