ros2_control - jazzy
Loading...
Searching...
No Matches
four_bar_linkage_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
16
17#ifndef TRANSMISSION_INTERFACE__FOUR_BAR_LINKAGE_TRANSMISSION_HPP_
18#define TRANSMISSION_INTERFACE__FOUR_BAR_LINKAGE_TRANSMISSION_HPP_
19
20#include <fmt/compile.h>
21
22#include <cassert>
23#include <string>
24#include <vector>
25
26#include "hardware_interface/types/hardware_interface_type_values.hpp"
27#include "transmission_interface/accessor.hpp"
28#include "transmission_interface/exception.hpp"
29#include "transmission_interface/transmission.hpp"
30
32{
34
110{
111public:
119 const std::vector<double> & actuator_reduction, const std::vector<double> & joint_reduction,
120 const std::vector<double> & joint_offset = std::vector<double>(2, 0.0));
121
127 void configure(
128 const std::vector<JointHandle> & joint_handles,
129 const std::vector<ActuatorHandle> & actuator_handles) override;
130
132
137 void actuator_to_joint() override;
138
140
145 void joint_to_actuator() override;
146
147 std::size_t num_actuators() const override { return 2; }
148 std::size_t num_joints() const override { return 2; }
149
150 const std::vector<double> & get_actuator_reduction() const { return actuator_reduction_; }
151 const std::vector<double> & get_joint_reduction() const { return joint_reduction_; }
152 const std::vector<double> & get_joint_offset() const { return joint_offset_; }
153
155 std::string get_handles_info() const;
156
157protected:
158 std::vector<double> actuator_reduction_;
159 std::vector<double> joint_reduction_;
160 std::vector<double> joint_offset_;
161
162 std::vector<JointHandle> joint_position_;
163 std::vector<JointHandle> joint_velocity_;
164 std::vector<JointHandle> joint_effort_;
165
166 std::vector<ActuatorHandle> actuator_position_;
167 std::vector<ActuatorHandle> actuator_velocity_;
168 std::vector<ActuatorHandle> actuator_effort_;
169};
170
172 const std::vector<double> & actuator_reduction, const std::vector<double> & joint_reduction,
173 const std::vector<double> & joint_offset)
174: actuator_reduction_(actuator_reduction),
175 joint_reduction_(joint_reduction),
176 joint_offset_(joint_offset)
177{
178 if (
179 num_actuators() != actuator_reduction_.size() || num_joints() != joint_reduction_.size() ||
180 num_joints() != joint_offset_.size())
181 {
182 throw Exception("Reduction and offset vectors must have size 2.");
183 }
184 if (
185 0.0 == actuator_reduction_[0] || 0.0 == actuator_reduction_[1] || 0.0 == joint_reduction_[0] ||
186 0.0 == joint_reduction_[1])
187 {
188 throw Exception("Transmission reduction ratios cannot be zero.");
189 }
190}
191
193 const std::vector<JointHandle> & joint_handles,
194 const std::vector<ActuatorHandle> & actuator_handles)
195{
196 if (joint_handles.empty())
197 {
198 throw Exception("No joint handles were passed in");
199 }
200
201 if (actuator_handles.empty())
202 {
203 throw Exception("No actuator handles were passed in");
204 }
205
206 const auto joint_names = get_names(joint_handles);
207 if (joint_names.size() != 2)
208 {
209 throw Exception(
210 fmt::format(
211 FMT_COMPILE("There should be exactly two unique joint names but was given '{}'."),
212 to_string(joint_names)));
213 }
214 const auto actuator_names = get_names(actuator_handles);
215 if (actuator_names.size() != 2)
216 {
217 throw Exception(
218 fmt::format(
219 FMT_COMPILE("There should be exactly two unique actuator names but was given '{}'."),
220 to_string(actuator_names)));
221 }
222
223 joint_position_ =
224 get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_POSITION);
225 joint_velocity_ =
226 get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_VELOCITY);
227 joint_effort_ = get_ordered_handles(joint_handles, joint_names, hardware_interface::HW_IF_EFFORT);
228
229 if (joint_position_.size() != 2 && joint_velocity_.size() != 2 && joint_effort_.size() != 2)
230 {
231 throw Exception("Not enough valid or required joint handles were presented.");
232 }
233
234 actuator_position_ =
235 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_POSITION);
236 actuator_velocity_ =
237 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_VELOCITY);
238 actuator_effort_ =
239 get_ordered_handles(actuator_handles, actuator_names, hardware_interface::HW_IF_EFFORT);
240
241 if (
242 actuator_position_.size() != 2 && actuator_velocity_.size() != 2 &&
243 actuator_effort_.size() != 2)
244 {
245 throw Exception(
246 fmt::format(
247 FMT_COMPILE("Not enough valid or required actuator handles were presented. \n{}"),
249 }
250
251 if (
252 joint_position_.size() != actuator_position_.size() &&
253 joint_velocity_.size() != actuator_velocity_.size() &&
254 joint_effort_.size() != actuator_effort_.size())
255 {
256 throw Exception(
257 fmt::format(FMT_COMPILE("Pair-wise mismatch on interfaces. \n{}"), get_handles_info()));
258 }
259}
260
262{
263 const auto & ar = actuator_reduction_;
264 const auto & jr = joint_reduction_;
265
266 // position
267 auto & act_pos = actuator_position_;
268 auto & joint_pos = joint_position_;
269 if (act_pos.size() == num_actuators() && joint_pos.size() == num_joints())
270 {
271 assert(act_pos[0] && act_pos[1] && joint_pos[0] && joint_pos[1]);
272
273 joint_pos[0].set_value(act_pos[0].get_value() / (jr[0] * ar[0]) + joint_offset_[0]);
274 joint_pos[1].set_value(
275 (act_pos[1].get_value() / ar[1] - act_pos[0].get_value() / (jr[0] * ar[0])) / jr[1] +
276 joint_offset_[1]);
277 }
278
279 // velocity
280 auto & act_vel = actuator_velocity_;
281 auto & joint_vel = joint_velocity_;
282 if (act_vel.size() == num_actuators() && joint_vel.size() == num_joints())
283 {
284 assert(act_vel[0] && act_vel[1] && joint_vel[0] && joint_vel[1]);
285
286 joint_vel[0].set_value(act_vel[0].get_value() / (jr[0] * ar[0]));
287 joint_vel[1].set_value(
288 (act_vel[1].get_value() / ar[1] - act_vel[0].get_value() / (jr[0] * ar[0])) / jr[1]);
289 }
290
291 // effort
292 auto & act_eff = actuator_effort_;
293 auto & joint_eff = joint_effort_;
294 if (act_eff.size() == num_actuators() && joint_eff.size() == num_joints())
295 {
296 assert(act_eff[0] && act_eff[1] && joint_eff[0] && joint_eff[1]);
297
298 joint_eff[0].set_value(jr[0] * act_eff[0].get_value() * ar[0] + act_eff[1].get_value() * ar[1]);
299 joint_eff[1].set_value(jr[1] * act_eff[1].get_value() * ar[1]);
300 }
301}
302
304{
305 const auto & ar = actuator_reduction_;
306 const auto & jr = joint_reduction_;
307
308 // position
309 auto & act_pos = actuator_position_;
310 auto & joint_pos = joint_position_;
311 if (act_pos.size() == num_actuators() && joint_pos.size() == num_joints())
312 {
313 assert(act_pos[0] && act_pos[1] && joint_pos[0] && joint_pos[1]);
314
315 double joints_offset_applied[2] = {
316 joint_pos[0].get_value() - joint_offset_[0], joint_pos[1].get_value() - joint_offset_[1]};
317 act_pos[0].set_value(joints_offset_applied[0] * jr[0] * ar[0]);
318 act_pos[1].set_value((joints_offset_applied[0] + joints_offset_applied[1] * jr[1]) * ar[1]);
319 }
320
321 // velocity
322 auto & act_vel = actuator_velocity_;
323 auto & joint_vel = joint_velocity_;
324 if (act_vel.size() == num_actuators() && joint_vel.size() == num_joints())
325 {
326 assert(act_vel[0] && act_vel[1] && joint_vel[0] && joint_vel[1]);
327
328 act_vel[0].set_value(joint_vel[0].get_value() * jr[0] * ar[0]);
329 act_vel[1].set_value((joint_vel[0].get_value() + joint_vel[1].get_value() * jr[1]) * ar[1]);
330 }
331
332 // effort
333 auto & act_eff = actuator_effort_;
334 auto & joint_eff = joint_effort_;
335 if (act_eff.size() == num_actuators() && joint_eff.size() == num_joints())
336 {
337 assert(act_eff[0] && act_eff[1] && joint_eff[0] && joint_eff[1]);
338
339 act_eff[0].set_value(
340 (joint_eff[0].get_value() - joint_eff[1].get_value() / jr[1]) / (jr[0] * ar[0]));
341 act_eff[1].set_value(joint_eff[1].get_value() / (ar[1] * jr[1]));
342 }
343}
344
346{
347 return fmt::format(
348 FMT_COMPILE(
349 "Got the following handles:\n"
350 "Joint position: {}, Actuator position: {}\n"
351 "Joint velocity: {}, Actuator velocity: {}\n"
352 "Joint effort: {}, Actuator effort: {}"),
353 to_string(get_names(joint_position_)), to_string(get_names(actuator_position_)),
354 to_string(get_names(joint_velocity_)), to_string(get_names(actuator_velocity_)),
355 to_string(get_names(joint_effort_)), to_string(get_names(actuator_effort_)));
356}
357
358} // namespace transmission_interface
359
360#endif // TRANSMISSION_INTERFACE__FOUR_BAR_LINKAGE_TRANSMISSION_HPP_
Definition exception.hpp:23
Implementation of a four-bar-linkage transmission.
Definition four_bar_linkage_transmission.hpp:110
void joint_to_actuator() override
Transform variables from joint to actuator space.
Definition four_bar_linkage_transmission.hpp:303
std::size_t num_actuators() const override
Definition four_bar_linkage_transmission.hpp:147
FourBarLinkageTransmission(const std::vector< double > &actuator_reduction, const std::vector< double > &joint_reduction, const std::vector< double > &joint_offset=std::vector< double >(2, 0.0))
Definition four_bar_linkage_transmission.hpp:171
std::size_t num_joints() const override
Definition four_bar_linkage_transmission.hpp:148
void actuator_to_joint() override
Transform variables from actuator to joint space.
Definition four_bar_linkage_transmission.hpp:261
std::string get_handles_info() const
Get human-friendly report of handles.
Definition four_bar_linkage_transmission.hpp:345
void configure(const std::vector< JointHandle > &joint_handles, const std::vector< ActuatorHandle > &actuator_handles) override
Definition four_bar_linkage_transmission.hpp:192
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_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