ros2_control - jazzy
Loading...
Searching...
No Matches
simple_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__SIMPLE_TRANSMISSION_HPP_
16#define TRANSMISSION_INTERFACE__SIMPLE_TRANSMISSION_HPP_
17
18#include <algorithm>
19#include <cassert>
20#include <string>
21#include <vector>
22
23#include "hardware_interface/types/hardware_interface_type_values.hpp"
24#include "transmission_interface/exception.hpp"
25#include "transmission_interface/transmission.hpp"
26
28{
30
84constexpr auto HW_IF_ABSOLUTE_POSITION = "absolute_position";
85
87{
88public:
94 explicit SimpleTransmission(
95 const double joint_to_actuator_reduction, const double joint_offset = 0.0);
96
98
104 void configure(
105 const std::vector<JointHandle> & joint_handles,
106 const std::vector<ActuatorHandle> & actuator_handles) override;
107
109
114 void actuator_to_joint() override;
115
117
122 void joint_to_actuator() override;
123
124 std::size_t num_actuators() const override { return 1; }
125 std::size_t num_joints() const override { return 1; }
126
127 double get_actuator_reduction() const { return reduction_; }
128 double get_joint_offset() const { return jnt_offset_; }
129
130protected:
131 double reduction_;
132 double jnt_offset_;
133
134 JointHandle joint_position_ = {"", "", nullptr};
135 JointHandle joint_velocity_ = {"", "", nullptr};
136 JointHandle joint_effort_ = {"", "", nullptr};
137 JointHandle joint_torque_ = {"", "", nullptr};
138 JointHandle joint_absolute_position_ = {"", "", nullptr};
139
140 ActuatorHandle actuator_position_ = {"", "", nullptr};
141 ActuatorHandle actuator_velocity_ = {"", "", nullptr};
142 ActuatorHandle actuator_effort_ = {"", "", nullptr};
143 ActuatorHandle actuator_torque_ = {"", "", nullptr};
144 ActuatorHandle actuator_absolute_position_ = {"", "", nullptr};
145};
146
148 const double joint_to_actuator_reduction, const double joint_offset)
149: reduction_(joint_to_actuator_reduction), jnt_offset_(joint_offset)
150{
151 if (reduction_ == 0.0)
152 {
153 throw Exception("Transmission reduction ratio cannot be zero.");
154 }
155}
156
157template <class HandleType>
158HandleType get_by_interface(
159 const std::vector<HandleType> & handles, const std::string & interface_name)
160{
161 const auto result = std::find_if(
162 handles.cbegin(), handles.cend(),
163 [&interface_name](const auto handle) { return handle.get_interface_name() == interface_name; });
164 if (result == handles.cend())
165 {
166 return HandleType(handles.cbegin()->get_prefix_name(), interface_name, nullptr);
167 }
168 return *result;
169}
170
171template <class T>
172bool are_names_identical(const std::vector<T> & handles)
173{
174 std::vector<std::string> names;
175 std::transform(
176 handles.cbegin(), handles.cend(), std::back_inserter(names),
177 [](const auto & handle) { return handle.get_prefix_name(); });
178 return std::equal(names.cbegin() + 1, names.cend(), names.cbegin());
179}
180
182 const std::vector<JointHandle> & joint_handles,
183 const std::vector<ActuatorHandle> & actuator_handles)
184{
185 if (joint_handles.empty())
186 {
187 throw Exception("No joint handles were passed in");
188 }
189
190 if (actuator_handles.empty())
191 {
192 throw Exception("No actuator handles were passed in");
193 }
194
195 if (!are_names_identical(joint_handles))
196 {
197 throw Exception("Joint names given to transmissions should be identical");
198 }
199
200 if (!are_names_identical(actuator_handles))
201 {
202 throw Exception("Actuator names given to transmissions should be identical");
203 }
204
205 joint_position_ = get_by_interface(joint_handles, hardware_interface::HW_IF_POSITION);
206 joint_velocity_ = get_by_interface(joint_handles, hardware_interface::HW_IF_VELOCITY);
207 joint_effort_ = get_by_interface(joint_handles, hardware_interface::HW_IF_EFFORT);
208 joint_torque_ = get_by_interface(joint_handles, hardware_interface::HW_IF_TORQUE);
209 joint_absolute_position_ = get_by_interface(joint_handles, HW_IF_ABSOLUTE_POSITION);
210
211 if (
212 !joint_position_ && !joint_velocity_ && !joint_effort_ && !joint_torque_ &&
213 !joint_absolute_position_)
214 {
215 throw Exception("None of the provided joint handles are valid or from the required interfaces");
216 }
217
218 actuator_position_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_POSITION);
219 actuator_velocity_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_VELOCITY);
220 actuator_effort_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_EFFORT);
221 actuator_torque_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_TORQUE);
222 actuator_absolute_position_ = get_by_interface(actuator_handles, HW_IF_ABSOLUTE_POSITION);
223
224 if (
225 !actuator_position_ && !actuator_velocity_ && !actuator_effort_ && !actuator_torque_ &&
226 !actuator_absolute_position_)
227 {
228 throw Exception("None of the provided joint handles are valid or from the required interfaces");
229 }
230}
231
233{
234 if (joint_effort_ && actuator_effort_)
235 {
236 joint_effort_.set_value(actuator_effort_.get_value() * reduction_);
237 }
238
239 if (joint_velocity_ && actuator_velocity_)
240 {
241 joint_velocity_.set_value(actuator_velocity_.get_value() / reduction_);
242 }
243
244 if (joint_position_ && actuator_position_)
245 {
246 joint_position_.set_value(actuator_position_.get_value() / reduction_ + jnt_offset_);
247 }
248
249 if (joint_torque_ && actuator_torque_)
250 {
251 joint_torque_.set_value(actuator_torque_.get_value() * reduction_);
252 }
253
254 if (joint_absolute_position_ && actuator_absolute_position_)
255 {
256 joint_absolute_position_.set_value(
257 actuator_absolute_position_.get_value() / reduction_ + jnt_offset_);
258 }
259}
260
262{
263 if (joint_effort_ && actuator_effort_)
264 {
265 actuator_effort_.set_value(joint_effort_.get_value() / reduction_);
266 }
267
268 if (joint_velocity_ && actuator_velocity_)
269 {
270 actuator_velocity_.set_value(joint_velocity_.get_value() * reduction_);
271 }
272
273 if (joint_position_ && actuator_position_)
274 {
275 actuator_position_.set_value((joint_position_.get_value() - jnt_offset_) * reduction_);
276 }
277
278 if (joint_torque_ && actuator_torque_)
279 {
280 actuator_torque_.set_value(joint_torque_.get_value() / reduction_);
281 }
282}
283
284} // namespace transmission_interface
285
286#endif // TRANSMISSION_INTERFACE__SIMPLE_TRANSMISSION_HPP_
Definition exception.hpp:23
Definition simple_transmission.hpp:87
SimpleTransmission(const double joint_to_actuator_reduction, const double joint_offset=0.0)
Definition simple_transmission.hpp:147
std::size_t num_actuators() const override
Definition simple_transmission.hpp:124
void actuator_to_joint() override
Transform variables from actuator to joint space.
Definition simple_transmission.hpp:232
void joint_to_actuator() override
Transform variables from joint to actuator space.
Definition simple_transmission.hpp:261
void configure(const std::vector< JointHandle > &joint_handles, const std::vector< ActuatorHandle > &actuator_handles) override
Set up the data the transmission operates on.
Definition simple_transmission.hpp:181
std::size_t num_joints() const override
Definition simple_transmission.hpp:125
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_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