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