ros2_control - rolling
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 <iterator>
21#include <string>
22#include <vector>
23
24#include "hardware_interface/types/hardware_interface_type_values.hpp"
25#include "transmission_interface/exception.hpp"
26#include "transmission_interface/transmission.hpp"
27
29{
31
86{
87public:
93 explicit SimpleTransmission(
94 const double joint_to_actuator_reduction, const double joint_offset = 0.0);
95
97
103 void configure(
104 const std::vector<JointHandle> & joint_handles,
105 const std::vector<ActuatorHandle> & actuator_handles) override;
106
108
113 void actuator_to_joint() override;
114
116
121 void joint_to_actuator() override;
122
123 std::size_t num_actuators() const override { return 1; }
124 std::size_t num_joints() const override { return 1; }
125
126 double get_actuator_reduction() const { return reduction_; }
127 double get_joint_offset() const { return jnt_offset_; }
128
135
142
143protected:
144 double reduction_;
145 double jnt_offset_;
146
147 JointHandle joint_position_ = {"", "", nullptr};
148 JointHandle joint_velocity_ = {"", "", nullptr};
149 JointHandle joint_effort_ = {"", "", nullptr};
150 JointHandle joint_torque_ = {"", "", nullptr};
151 JointHandle joint_force_ = {"", "", nullptr};
152 JointHandle joint_absolute_position_ = {"", "", nullptr};
153
154 ActuatorHandle actuator_position_ = {"", "", nullptr};
155 ActuatorHandle actuator_velocity_ = {"", "", nullptr};
156 ActuatorHandle actuator_effort_ = {"", "", nullptr};
157 ActuatorHandle actuator_torque_ = {"", "", nullptr};
158 ActuatorHandle actuator_force_ = {"", "", nullptr};
159 ActuatorHandle actuator_absolute_position_ = {"", "", nullptr};
160};
161
163 const double joint_to_actuator_reduction, const double joint_offset)
164: reduction_(joint_to_actuator_reduction), jnt_offset_(joint_offset)
165{
166 if (reduction_ == 0.0)
167 {
168 throw Exception("Transmission reduction ratio cannot be zero.");
169 }
170}
171
172template <class HandleType>
173HandleType get_by_interface(
174 const std::vector<HandleType> & handles, const std::string & interface_name)
175{
176 const auto result = std::find_if(
177 handles.cbegin(), handles.cend(),
178 [&interface_name](const auto handle) { return handle.get_interface_name() == interface_name; });
179 if (result == handles.cend())
180 {
181 return HandleType(handles.cbegin()->get_prefix_name(), interface_name, nullptr);
182 }
183 return *result;
184}
185
186template <class T>
187bool are_names_identical(const std::vector<T> & handles)
188{
189 std::vector<std::string> names;
190 std::transform(
191 handles.cbegin(), handles.cend(), std::back_inserter(names),
192 [](const auto & handle) { return handle.get_prefix_name(); });
193 return std::equal(names.cbegin() + 1, names.cend(), names.cbegin());
194}
195
197 const std::vector<JointHandle> & joint_handles,
198 const std::vector<ActuatorHandle> & actuator_handles)
199{
200 if (joint_handles.empty())
201 {
202 throw Exception("No joint handles were passed in");
203 }
204
205 if (actuator_handles.empty())
206 {
207 throw Exception("No actuator handles were passed in");
208 }
209
210 if (!are_names_identical(joint_handles))
211 {
212 throw Exception("Joint names given to transmissions should be identical");
213 }
214
215 if (!are_names_identical(actuator_handles))
216 {
217 throw Exception("Actuator names given to transmissions should be identical");
218 }
219
220 joint_position_ = get_by_interface(joint_handles, hardware_interface::HW_IF_POSITION);
221 joint_velocity_ = get_by_interface(joint_handles, hardware_interface::HW_IF_VELOCITY);
222 joint_effort_ = get_by_interface(joint_handles, hardware_interface::HW_IF_EFFORT);
223 joint_torque_ = get_by_interface(joint_handles, hardware_interface::HW_IF_TORQUE);
224 joint_force_ = get_by_interface(joint_handles, hardware_interface::HW_IF_FORCE);
225 joint_absolute_position_ = get_by_interface(joint_handles, HW_IF_ABSOLUTE_POSITION);
226
227 if (
228 !joint_position_ && !joint_velocity_ && !joint_effort_ && !joint_torque_ && !joint_force_ &&
229 !joint_absolute_position_)
230 {
231 throw Exception("None of the provided joint handles are valid or from the required interfaces");
232 }
233
234 actuator_position_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_POSITION);
235 actuator_velocity_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_VELOCITY);
236 actuator_effort_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_EFFORT);
237 actuator_torque_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_TORQUE);
238 actuator_force_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_FORCE);
239 actuator_absolute_position_ = get_by_interface(actuator_handles, HW_IF_ABSOLUTE_POSITION);
240
241 if (
242 !actuator_position_ && !actuator_velocity_ && !actuator_effort_ && !actuator_torque_ &&
243 !actuator_force_ && !actuator_absolute_position_)
244 {
245 throw Exception(
246 "None of the provided actuator handles are valid or from the required interfaces");
247 }
248}
249
251{
252 if (joint_effort_ && actuator_effort_)
253 {
254 joint_effort_.set_value(actuator_effort_.get_value() * reduction_);
255 }
256
257 if (joint_velocity_ && actuator_velocity_)
258 {
259 joint_velocity_.set_value(actuator_velocity_.get_value() / reduction_);
260 }
261
262 if (joint_position_ && actuator_position_)
263 {
264 joint_position_.set_value(actuator_position_.get_value() / reduction_ + jnt_offset_);
265 }
266
267 if (joint_torque_ && actuator_torque_)
268 {
269 joint_torque_.set_value(actuator_torque_.get_value() * reduction_);
270 }
271
272 if (joint_force_ && actuator_force_)
273 {
274 joint_force_.set_value(actuator_force_.get_value() * reduction_);
275 }
276
277 if (joint_absolute_position_ && actuator_absolute_position_)
278 {
279 joint_absolute_position_.set_value(
280 actuator_absolute_position_.get_value() / reduction_ + jnt_offset_);
281 }
282}
283
285{
286 if (joint_effort_ && actuator_effort_)
287 {
288 actuator_effort_.set_value(joint_effort_.get_value() / reduction_);
289 }
290
291 if (joint_velocity_ && actuator_velocity_)
292 {
293 actuator_velocity_.set_value(joint_velocity_.get_value() * reduction_);
294 }
295
296 if (joint_position_ && actuator_position_)
297 {
298 actuator_position_.set_value((joint_position_.get_value() - jnt_offset_) * reduction_);
299 }
300
301 if (joint_torque_ && actuator_torque_)
302 {
303 actuator_torque_.set_value(joint_torque_.get_value() / reduction_);
304 }
305
306 if (joint_force_ && actuator_force_)
307 {
308 actuator_force_.set_value(joint_force_.get_value() / reduction_);
309 }
310}
311
312} // namespace transmission_interface
313
314#endif // TRANSMISSION_INTERFACE__SIMPLE_TRANSMISSION_HPP_
Definition exception.hpp:23
Definition handle.hpp:79
Implementation of a simple reducer transmission.
Definition simple_transmission.hpp:86
SimpleTransmission(const double joint_to_actuator_reduction, const double joint_offset=0.0)
Definition simple_transmission.hpp:162
std::size_t num_actuators() const override
Definition simple_transmission.hpp:123
void actuator_to_joint() override
Transform variables from actuator to joint space.
Definition simple_transmission.hpp:250
void joint_to_actuator() override
Transform variables from joint to actuator space.
Definition simple_transmission.hpp:284
std::vector< std::string > get_supported_joint_interfaces() const override
Definition simple_transmission.hpp:136
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:196
std::vector< std::string > get_supported_actuator_interfaces() const override
Definition simple_transmission.hpp:129
std::size_t num_joints() const override
Definition simple_transmission.hpp:124
Abstract base class for representing mechanical transmissions.
Definition transmission.hpp:50
constexpr char HW_IF_EFFORT[]
Constant defining effort interface name.
Definition hardware_interface_type_values.hpp:27
constexpr char HW_IF_FORCE[]
Constant defining force interface name.
Definition mujoco_system_interface.hpp:61
constexpr char HW_IF_TORQUE[]
Constant defining torque interface name.
Definition mujoco_system_interface.hpp:59
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:26