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 <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_force_ = {"", "", nullptr};
139 JointHandle joint_absolute_position_ = {"", "", nullptr};
140
141 ActuatorHandle actuator_position_ = {"", "", nullptr};
142 ActuatorHandle actuator_velocity_ = {"", "", nullptr};
143 ActuatorHandle actuator_effort_ = {"", "", nullptr};
144 ActuatorHandle actuator_torque_ = {"", "", nullptr};
145 ActuatorHandle actuator_force_ = {"", "", nullptr};
146 ActuatorHandle actuator_absolute_position_ = {"", "", nullptr};
147};
148
150 const double joint_to_actuator_reduction, const double joint_offset)
151: reduction_(joint_to_actuator_reduction), jnt_offset_(joint_offset)
152{
153 if (reduction_ == 0.0)
154 {
155 throw Exception("Transmission reduction ratio cannot be zero.");
156 }
157}
158
159template <class HandleType>
160HandleType get_by_interface(
161 const std::vector<HandleType> & handles, const std::string & interface_name)
162{
163 const auto result = std::find_if(
164 handles.cbegin(), handles.cend(),
165 [&interface_name](const auto handle) { return handle.get_interface_name() == interface_name; });
166 if (result == handles.cend())
167 {
168 return HandleType(handles.cbegin()->get_prefix_name(), interface_name, nullptr);
169 }
170 return *result;
171}
172
173template <class T>
174bool are_names_identical(const std::vector<T> & handles)
175{
176 std::vector<std::string> names;
177 std::transform(
178 handles.cbegin(), handles.cend(), std::back_inserter(names),
179 [](const auto & handle) { return handle.get_prefix_name(); });
180 return std::equal(names.cbegin() + 1, names.cend(), names.cbegin());
181}
182
184 const std::vector<JointHandle> & joint_handles,
185 const std::vector<ActuatorHandle> & actuator_handles)
186{
187 if (joint_handles.empty())
188 {
189 throw Exception("No joint handles were passed in");
190 }
191
192 if (actuator_handles.empty())
193 {
194 throw Exception("No actuator handles were passed in");
195 }
196
197 if (!are_names_identical(joint_handles))
198 {
199 throw Exception("Joint names given to transmissions should be identical");
200 }
201
202 if (!are_names_identical(actuator_handles))
203 {
204 throw Exception("Actuator names given to transmissions should be identical");
205 }
206
207 joint_position_ = get_by_interface(joint_handles, hardware_interface::HW_IF_POSITION);
208 joint_velocity_ = get_by_interface(joint_handles, hardware_interface::HW_IF_VELOCITY);
209 joint_effort_ = get_by_interface(joint_handles, hardware_interface::HW_IF_EFFORT);
210 joint_torque_ = get_by_interface(joint_handles, hardware_interface::HW_IF_TORQUE);
211 joint_force_ = get_by_interface(joint_handles, hardware_interface::HW_IF_FORCE);
212 joint_absolute_position_ = get_by_interface(joint_handles, HW_IF_ABSOLUTE_POSITION);
213
214 if (
215 !joint_position_ && !joint_velocity_ && !joint_effort_ && !joint_torque_ && !joint_force_ &&
216 !joint_absolute_position_)
217 {
218 throw Exception("None of the provided joint handles are valid or from the required interfaces");
219 }
220
221 actuator_position_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_POSITION);
222 actuator_velocity_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_VELOCITY);
223 actuator_effort_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_EFFORT);
224 actuator_torque_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_TORQUE);
225 actuator_force_ = get_by_interface(actuator_handles, hardware_interface::HW_IF_FORCE);
226 actuator_absolute_position_ = get_by_interface(actuator_handles, HW_IF_ABSOLUTE_POSITION);
227
228 if (
229 !actuator_position_ && !actuator_velocity_ && !actuator_effort_ && !actuator_torque_ &&
230 !actuator_force_ && !actuator_absolute_position_)
231 {
232 throw Exception(
233 "None of the provided actuator handles are valid or from the required interfaces");
234 }
235}
236
238{
239 if (joint_effort_ && actuator_effort_)
240 {
241 joint_effort_.set_value(actuator_effort_.get_value() * reduction_);
242 }
243
244 if (joint_velocity_ && actuator_velocity_)
245 {
246 joint_velocity_.set_value(actuator_velocity_.get_value() / reduction_);
247 }
248
249 if (joint_position_ && actuator_position_)
250 {
251 joint_position_.set_value(actuator_position_.get_value() / reduction_ + jnt_offset_);
252 }
253
254 if (joint_torque_ && actuator_torque_)
255 {
256 joint_torque_.set_value(actuator_torque_.get_value() * reduction_);
257 }
258
259 if (joint_force_ && actuator_force_)
260 {
261 joint_force_.set_value(actuator_force_.get_value() * reduction_);
262 }
263
264 if (joint_absolute_position_ && actuator_absolute_position_)
265 {
266 joint_absolute_position_.set_value(
267 actuator_absolute_position_.get_value() / reduction_ + jnt_offset_);
268 }
269}
270
272{
273 if (joint_effort_ && actuator_effort_)
274 {
275 actuator_effort_.set_value(joint_effort_.get_value() / reduction_);
276 }
277
278 if (joint_velocity_ && actuator_velocity_)
279 {
280 actuator_velocity_.set_value(joint_velocity_.get_value() * reduction_);
281 }
282
283 if (joint_position_ && actuator_position_)
284 {
285 actuator_position_.set_value((joint_position_.get_value() - jnt_offset_) * reduction_);
286 }
287
288 if (joint_torque_ && actuator_torque_)
289 {
290 actuator_torque_.set_value(joint_torque_.get_value() / reduction_);
291 }
292
293 if (joint_force_ && actuator_force_)
294 {
295 actuator_force_.set_value(joint_force_.get_value() / reduction_);
296 }
297}
298
299} // namespace transmission_interface
300
301#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:149
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:237
void joint_to_actuator() override
Transform variables from joint to actuator space.
Definition simple_transmission.hpp:271
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:183
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_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