YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLaserWithMotor_motors.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Time.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
14#include <yarp/math/Vec2D.h>
15#include <iostream>
16#include <limits>
17#include <cstring>
18#include <cstdlib>
19#include <cmath>
20
21//#define LASER_DEBUG
22#ifndef DEG2RAD
23#define DEG2RAD M_PI/180.0
24#endif
25
26YARP_LOG_COMPONENT(FAKE_LASER_MOTORS, "yarp.devices.fakeLaserWithMotor.motors")
27
30
32 std::enable_if_t<std::is_trivial<T>::value, int> = 0>
33 T* allocAndCheck(int size)
34{
35 T* t = new T[size];
36 yAssert(t != 0);
37 memset(t, 0, sizeof(T) * size);
38 return t;
39}
40
41template <class T,
42 std::enable_if_t<!std::is_trivial<T>::value, int> = 0>
43 T* allocAndCheck(int size)
44{
45 T* t = new T[size];
46 yAssert(t != 0);
47 return t;
48}
49
50template <class T>
51void checkAndDestroy(T*& p) {
52 if (p != 0) {
53 delete[] p;
54 p = 0;
55 }
56}
57
58
59//Motor interfaces
61{
62 bool ret = getEncodersRaw(encs);
63 m_mutex.lock();
64 for (int i = 0; i < m_njoints; i++) {
66 }
67 m_mutex.unlock();
68 return ret;
69}
70
71bool FakeLaserWithMotor::getEncoderTimedRaw(int j, double* encs, double* stamp)
72{
73 bool ret = getEncoderRaw(j, encs);
74 m_mutex.lock();
75 *stamp = m_timestamp.getTime();
76 m_mutex.unlock();
77
78 return ret;
79}
80
82{
83 *ax = m_njoints;
84 return true;
85}
86
88{
89 int mode = 0;
90 getControlModeRaw(j, &mode);
91 if ((mode != VOCAB_CM_POSITION) &&
92 (mode != VOCAB_CM_MIXED) &&
93 (mode != VOCAB_CM_IMPEDANCE_POS) &&
94 (mode != VOCAB_CM_IDLE))
95 {
96 yCError(FAKE_LASER_MOTORS) << "positionMoveRaw: skipping command because joint " << j << " is not in VOCAB_CM_POSITION mode";
97 }
99 return true;
100}
101
103{
104 bool ret = true;
105 for (int j = 0, index = 0; j < m_njoints; j++, index++)
106 {
107 ret &= positionMoveRaw(j, refs[index]);
108 }
109 return ret;
110}
111
113{
114 int mode = 0;
115 getControlModeRaw(j, &mode);
116 if ((mode != VOCAB_CM_POSITION) &&
117 (mode != VOCAB_CM_MIXED) &&
118 (mode != VOCAB_CM_IMPEDANCE_POS) &&
119 (mode != VOCAB_CM_IDLE))
120 {
121 yCError(FAKE_LASER_MOTORS) << "relativeMoveRaw: skipping command because joint " << j << " is not in VOCAB_CM_POSITION mode";
122 }
124 return true;
125}
126
128{
129 bool ret = true;
130 for (int j = 0, index = 0; j < m_njoints; j++, index++)
131 {
132 ret &= relativeMoveRaw(j, deltas[index]);
133 }
134 return ret;
135}
136
137
139{
140 *flag = false;
141 return true;
142}
143
145{
146 bool ret = true;
147 bool val, tot_res = true;
148
149 for (int j = 0, index = 0; j < m_njoints; j++, index++)
150 {
151 ret &= checkMotionDoneRaw(j, &val);
152 tot_res &= val;
153 }
154 *flag = tot_res;
155 return ret;
156}
157
159{
160 // Velocity is expressed in iDegrees/s
161 // save internally the new value of speed; it'll be used in the positionMove
162 int index = j;
163 _ref_speeds[index] = sp;
164 return true;
165}
166
168{
169 // Velocity is expressed in iDegrees/s
170 // save internally the new value of speed; it'll be used in the positionMove
171 for (int j = 0, index = 0; j < m_njoints; j++, index++)
172 {
173 _ref_speeds[index] = spds[index];
174 }
175 return true;
176}
177
179{
180 // Acceleration is expressed in iDegrees/s^2
181 // save internally the new value of the acceleration; it'll be used in the velocityMove command
182
183 if (acc > 1e6)
184 {
185 _ref_accs[j] = 1e6;
186 }
187 else if (acc < -1e6)
188 {
189 _ref_accs[j] = -1e6;
190 }
191 else
192 {
193 _ref_accs[j] = acc;
194 }
195
196 return true;
197}
198
200{
201 // Acceleration is expressed in iDegrees/s^2
202 // save internally the new value of the acceleration; it'll be used in the velocityMove command
203 for (int j = 0, index = 0; j < m_njoints; j++, index++)
204 {
205 if (accs[j] > 1e6)
206 {
207 _ref_accs[index] = 1e6;
208 }
209 else if (accs[j] < -1e6)
210 {
211 _ref_accs[index] = -1e6;
212 }
213 else
214 {
215 _ref_accs[index] = accs[j];
216 }
217 }
218 return true;
219}
220
222{
223 *spd = _ref_speeds[j];
224 return true;
225}
226
228{
229 memcpy(spds, _ref_speeds, sizeof(double) * m_njoints);
230 return true;
231}
232
234{
235 *acc = _ref_accs[j];
236 return true;
237}
238
240{
241 memcpy(accs, _ref_accs, sizeof(double) * m_njoints);
242 return true;
243}
244
246{
247 return true;
248}
249
251{
252 bool ret = true;
253 for (int j = 0; j < m_njoints; j++)
254 {
255 ret &= stopRaw(j);
256 }
257 return ret;
258}
260
262{
263 *v = _controlModes[j];
264 return true;
265}
266
267// IControl Mode 2
269{
270 bool ret = true;
271 for (int j = 0; j < m_njoints; j++)
272 {
273 ret = ret && getControlModeRaw(j, &v[j]);
274 }
275 return ret;
276}
277
279{
280 bool ret = true;
281 for (int j = 0; j < n_joint; j++)
282 {
284 }
285 return ret;
286}
287
288
289
291{
293 {
295 }
296 else
297 {
299 }
300 return true;
301}
302
303
305{
306 bool ret = true;
307 for (int i = 0; i < n_joint; i++)
308 {
310 }
311 return ret;
312}
313
315{
316 bool ret = true;
317 for (int i = 0; i < m_njoints; i++)
318 {
320 }
321 return ret;
322}
323
325{
328 _axisName = new std::string[nj];
329 _jointType = new JointTypeEnum[nj];
330
331 // Reserve space for data stored locally. values are initialized to 0
336
337 //resizeBuffers();
338
339 return true;
340}
341
354
355
357{
358 return NOT_YET_IMPLEMENTED("setEncoder");
359}
360
362{
363 return NOT_YET_IMPLEMENTED("setEncoders");
364}
365
367{
368 return NOT_YET_IMPLEMENTED("resetEncoder");
369}
370
372{
373 return NOT_YET_IMPLEMENTED("resetEncoders");
374}
375
376bool FakeLaserWithMotor::getEncoderRaw(int j, double* value)
377{
378 bool ret = true;
379
380 *value = _encoders[j];
381
382 return ret;
383}
384
386{
387 bool ret = true;
388 for (int j = 0; j < m_njoints; j++)
389 {
390 bool ok = getEncoderRaw(j, &encs[j]);
391 ret = ret && ok;
392
393 }
394 return ret;
395}
396
398{
399 // To avoid returning uninitialized memory, we set the encoder speed to 0
400 *sp = 0.0;
401 return true;
402}
403
405{
406 bool ret = true;
407 for (int j = 0; j < m_njoints; j++)
408 {
410 }
411 return ret;
412}
413
415{
416 // To avoid returning uninitialized memory, we set the encoder acc to 0
417 *acc = 0.0;
418
419 return true;
420}
421
423{
424 bool ret = true;
425 for (int j = 0; j < m_njoints; j++)
426 {
428 }
429 return ret;
430}
431
432bool FakeLaserWithMotor::setRefAccelerationsRaw(const int n_joint, const int* joints, const double* accs)
433{
434 bool ret = true;
435 for (int j = 0; j < n_joint; j++)
436 {
438 }
439 return ret;
440}
441
442bool FakeLaserWithMotor::getRefSpeedsRaw(const int n_joint, const int* joints, double* spds)
443{
444 bool ret = true;
445 for (int j = 0; j < n_joint; j++)
446 {
447 ret = ret && getRefSpeedRaw(joints[j], &spds[j]);
448 }
449 return ret;
450}
451
453{
454 bool ret = true;
455 for (int j = 0; j < n_joint; j++)
456 {
458 }
459 return ret;
460}
461
462bool FakeLaserWithMotor::stopRaw(const int n_joint, const int* joints)
463{
464 bool ret = true;
465 for (int j = 0; j < n_joint; j++)
466 {
467 ret = ret && stopRaw(joints[j]);
468 }
469 return ret;
470}
471
472bool FakeLaserWithMotor::positionMoveRaw(const int n_joint, const int* joints, const double* refs)
473{
474 for (int j = 0; j < n_joint; j++)
475 {
476 yCDebug(FAKE_LASER_MOTORS, "j: %d; ref %f;\n", joints[j], refs[j]); fflush(stdout);
477 }
478
479 bool ret = true;
480 for (int j = 0; j < n_joint; j++)
481 {
483 }
484 return ret;
485}
486
487bool FakeLaserWithMotor::relativeMoveRaw(const int n_joint, const int* joints, const double* deltas)
488{
489 bool ret = true;
490 for (int j = 0; j < n_joint; j++)
491 {
493 }
494 return ret;
495}
496
498{
499 bool ret = true;
500 bool val = true;
501 bool tot_val = true;
502
503 for (int j = 0; j < n_joint; j++)
504 {
505 ret = ret && checkMotionDoneRaw(joints[j], &val);
506 tot_val &= val;
507 }
508 *flag = tot_val;
509 return ret;
510}
511
512bool FakeLaserWithMotor::setRefSpeedsRaw(const int n_joint, const int* joints, const double* spds)
513{
514 bool ret = true;
515 for (int j = 0; j < n_joint; j++)
516 {
518 }
519 return ret;
520}
521
523{
524 int mode = 0;
525 getControlModeRaw(axis, &mode);
526 if ((mode != VOCAB_CM_POSITION) &&
527 (mode != VOCAB_CM_MIXED) &&
528 (mode != VOCAB_CM_IMPEDANCE_POS))
529 {
530 yCWarning(FAKE_LASER_MOTORS) << "getTargetPosition: Joint " << axis << " is not in POSITION mode, therefore the value returned by " <<
531 "this call is for reference only and may not reflect the actual behaviour of the motor/firmware.";
532 }
534 return true;
535}
536
538{
539 bool ret = true;
540 for (int i = 0; i < m_njoints; i++) {
542 }
543 return ret;
544}
545
546bool FakeLaserWithMotor::getTargetPositionsRaw(int nj, const int* jnts, double* refs)
547{
548 bool ret = true;
549 for (int i = 0; i < nj; i++)
550 {
552 }
553 return ret;
554}
555
557{
558 int mode = 0;
559 getControlModeRaw(j, &mode);
560 if ((mode != VOCAB_CM_VELOCITY) &&
561 (mode != VOCAB_CM_MIXED) &&
562 (mode != VOCAB_CM_IMPEDANCE_VEL) &&
563 (mode != VOCAB_CM_IDLE))
564 {
565 yCError(FAKE_LASER_MOTORS) << "velocityMoveRaw: skipping command because board " << " joint " << j << " is not in VOCAB_CM_VELOCITY mode";
566 }
568 //last_velocity_command[j] = yarp::os::Time::now();
569 return true;
570}
571
573{
574 bool ret = true;
575 for (int i = 0; i < m_njoints; i++) {
576 ret &= velocityMoveRaw(i, sp[i]);
577 }
578 return ret;
579}
580bool FakeLaserWithMotor::velocityMoveRaw(const int n_joint, const int* joints, const double* spds)
581{
582 bool ret = true;
583 for (int i = 0; i < n_joint; i++)
584 {
586 }
587 return ret;
588}
589
591{
592 *ref = _command_speeds[axis];
593 return true;
594}
595
597{
598 bool ret = true;
599 for (int i = 0; i < m_njoints; i++)
600 {
602 }
603 return ret;
604}
605
606bool FakeLaserWithMotor::getRefVelocitiesRaw(int nj, const int* jnts, double* refs)
607{
608 bool ret = true;
609 for (int i = 0; i < nj; i++)
610 {
612 }
613 return ret;
614}
615
616
617bool FakeLaserWithMotor::getAxisNameRaw(int axis, std::string& name)
618{
619 if (axis >= 0 && axis < m_njoints)
620 {
621 name = _axisName[axis];
622 return true;
623 }
624 else
625 {
626 name = "ERROR";
627 return false;
628 }
629}
630
632{
633 if (axis >= 0 && axis < m_njoints)
634 {
635 type = _jointType[axis];
636 return true;
637 }
638 else
639 {
640 return false;
641 }
642}
void checkAndDestroy(T *&p)
void checkAndDestroy(T *&p)
const yarp::os::LogComponent & FAKE_LASER_MOTORS()
T * allocAndCheck(int size)
float t
constexpr yarp::conf::vocab32_t VOCAB_CM_MIXED
constexpr yarp::conf::vocab32_t VOCAB_CM_IMPEDANCE_VEL
constexpr yarp::conf::vocab32_t VOCAB_CM_IDLE
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
constexpr yarp::conf::vocab32_t VOCAB_CM_IMPEDANCE_POS
constexpr yarp::conf::vocab32_t VOCAB_CM_VELOCITY
constexpr yarp::conf::vocab32_t VOCAB_CM_FORCE_IDLE
bool ret
#define yAssert(x)
Definition Log.h:388
bool setRefSpeedsRaw(const double *spds) override
Set reference speed on all joints.
bool stopRaw() override
Stop motion, multiple joints.
bool getEncodersTimedRaw(double *encs, double *stamps) override
Read the instantaneous acceleration of all axes.
bool getEncodersRaw(double *encs) override
Read the position of all axes.
bool getTargetPositionRaw(const int joint, double *ref) override
Get the last position reference for the specified axis.
bool getEncoderSpeedsRaw(double *spds) override
Read the instantaneous acceleration of an axis.
bool setEncodersRaw(const double *vals) override
Set the value of all encoders.
bool getRefSpeedsRaw(double *spds) override
Get reference speed of all joints.
bool getJointTypeRaw(int axis, yarp::dev::JointTypeEnum &type) override
bool setControlModesRaw(const int n_joint, const int *joints, int *modes) override
bool resetEncoderRaw(int j) override
Reset encoder, single joint.
bool checkMotionDoneRaw(bool *flag) override
Check if the current trajectory is terminated.
bool getControlModesRaw(int *v) override
bool positionMoveRaw(int j, double ref) override
Set new reference point for a single axis.
bool getRefAccelerationsRaw(double *accs) override
Get reference acceleration of all joints.
bool getEncoderRaw(int j, double *v) override
Read the value of an encoder.
bool getEncoderTimedRaw(int j, double *encs, double *stamp) override
Read the instantaneous acceleration of all axes.
bool getTargetPositionsRaw(double *refs) override
Get the last position reference for all axes.
bool getControlModeRaw(int j, int *v) override
bool getEncoderSpeedRaw(int j, double *sp) override
Read the instantaneous speed of an axis.
bool getRefVelocityRaw(const int joint, double *ref) override
Get the last reference speed set by velocityMove for single joint.
bool getAxisNameRaw(int axis, std::string &name) override
bool resetEncodersRaw() override
Reset encoders.
bool setRefSpeedRaw(int j, double sp) override
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
bool setRefAccelerationRaw(int j, double acc) override
Set reference acceleration for a joint.
bool getRefAccelerationRaw(int j, double *acc) override
Get reference acceleration for a joint.
bool relativeMoveRaw(int j, double delta) override
Set relative position.
bool getRefVelocitiesRaw(double *refs) override
Get the last reference speed set by velocityMove for all joints.
yarp::dev::JointTypeEnum * _jointType
bool getEncoderAccelerationsRaw(double *accs) override
Read the instantaneous acceleration of all axes.
bool setEncoderRaw(int j, double val) override
Set the value of the encoder for a given joint.
bool getEncoderAccelerationRaw(int j, double *spds) override
Read the instantaneous acceleration of an axis.
bool velocityMoveRaw(int j, double sp) override
Start motion at a given speed, single joint.
bool setRefAccelerationsRaw(const double *accs) override
Set reference acceleration on all joints.
bool getAxes(int *ax) override
Get the number of controlled axes.
bool getRefSpeedRaw(int j, double *ref) override
Get reference speed for a joint.
bool setControlModeRaw(const int j, const int mode) override
bool NOT_YET_IMPLEMENTED(const char *func=0)
Helper for printing error message, see below.
A mini-server for performing network communication in the background.
double getTime() const
Get the time stamp.
Definition Stamp.cpp:34
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
The main, catch-all namespace for YARP.
Definition dirs.h:16