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