YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeMotionControlMicro.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
7
9
10#include <yarp/os/Bottle.h>
11#include <yarp/os/Time.h>
13#include <yarp/os/LogStream.h>
14#include <yarp/os/NetType.h>
15#include <yarp/dev/Drivers.h>
16
17#include <sstream>
18#include <cstring>
19
20using namespace yarp::dev;
21using namespace yarp::os;
22using namespace yarp::os::impl;
23
24// macros
25#define NEW_JSTATUS_STRUCT 1
26#define VELOCITY_WATCHDOG 0.1
27#define OPENLOOP_WATCHDOG 0.1
28#define PWM_CONSTANT 0.1
29
30namespace {
31YARP_LOG_COMPONENT(FAKEMOTIONCONTROLMICRO, "yarp.device.fakeMotionControlMicro")
32}
33
35{
36 std::lock_guard lock(_mutex);
37
38 for (int i=0;i <_njoints ;i++)
39 {
40 }
41 prev_time = yarp::os::Time::now();
42}
43
44static inline bool NOT_YET_IMPLEMENTED(const char *txt)
45{
46 yCError(FAKEMOTIONCONTROLMICRO) << txt << "is not yet implemented";
47 return true;
48}
49
50static inline bool DEPRECATED(const char *txt)
51{
52 yCError(FAKEMOTIONCONTROLMICRO) << txt << "has been deprecated";
53 return true;
54}
55
56
57// replace with to_string as soon as C++11 is required by YARP
62template<typename T>
63std::string toString(const T& value)
64{
65 std::ostringstream oss;
66 oss << value;
67 return oss.str();
68}
69
70//generic function that check is key1 is present in input bottle and that the result has size elements
71// return true/false
72bool FakeMotionControlMicro::extractGroup(Bottle &input, Bottle &out, const std::string &key1, const std::string &txt, int size)
73{
74 size++;
75 Bottle &tmp=input.findGroup(key1, txt);
76
77 if (tmp.isNull())
78 {
79 yCError(FAKEMOTIONCONTROLMICRO) << key1.c_str() << "parameter not found";
80 return false;
81 }
82
83 if(tmp.size()!=(size_t) size)
84 {
85 yCError(FAKEMOTIONCONTROLMICRO) << key1.c_str() << "incorrect number of entries";
86 return false;
87 }
88
89 out=tmp;
90 return true;
91}
92
94{
95 pos.resize(_njoints);
96 dpos.resize(_njoints);
97 vel.resize(_njoints);
98 speed.resize(_njoints);
99 acc.resize(_njoints);
100 loc.resize(_njoints);
101 amp.resize(_njoints);
102
103 pos.zero();
104 dpos.zero();
105 vel.zero();
106 speed.zero();
107 acc.zero();
108 loc.zero();
109 amp.zero();
110}
111
113{
114 _axisMap = allocAndCheck<int>(nj);
115 _controlModes = allocAndCheck<int>(nj);
116 _angleToEncoder = allocAndCheck<double>(nj);
117 _encodersStamp = allocAndCheck<double>(nj);
118 _hwfault_code = allocAndCheck<int>(nj);
119 _hwfault_message = allocAndCheck<std::string>(nj);
120
121 _axisName = new std::string[nj];
122 _jointType = new JointTypeEnum[nj];
123
125
126 return true;
127}
128
129bool FakeMotionControlMicro::dealloc()
130{
131 checkAndDestroy(_axisMap);
132 checkAndDestroy(_controlModes);
133 checkAndDestroy(_angleToEncoder);
134 checkAndDestroy(_encodersStamp);
135
136 checkAndDestroy(_axisName);
137 checkAndDestroy(_jointType);
138
139 checkAndDestroy(_hwfault_code);
140 checkAndDestroy(_hwfault_message);
141
142 return true;
143}
144
146 PeriodicThread(0.01),
151 _mutex(),
152 _njoints (0),
153 _axisMap (nullptr),
154 _angleToEncoder(nullptr),
155 _encodersStamp (nullptr),
156 _axisName (nullptr),
157 _jointType (nullptr),
158 useRawEncoderData (false),
159 prev_time (0.0),
160 opened (false),
161 verbose (VERY_VERBOSE)
162{
164 std::string tmp = yarp::conf::environment::get_string("VERBOSE_STICA");
165 verbosewhenok = (tmp != "") ? (bool)yarp::conf::numeric::from_string<int>(tmp) :
166 false;
167}
168
174
176{
177 return opened;
178}
179
181{
183 for(int i=0; i<_njoints; i++)
184 {
185 _controlModes[i] = VOCAB_CM_POSITION;
186 }
187 prev_time = yarp::os::Time::now();
188 return true;
189}
190
194
196{
197 if (!this->parseParams(config)) {return false;}
198
199 std::string str;
200
201 //
202 // Read Configuration params from file
203 //
204 _njoints = m_GENERAL_Joints;
205
206 if(!alloc(_njoints))
207 {
208 yCError(FAKEMOTIONCONTROLMICRO) << "Malloc failed";
209 return false;
210 }
211
212 if(!fromConfig(config))
213 {
214 yCError(FAKEMOTIONCONTROLMICRO) << "Missing parameters in config file";
215 return false;
216 }
217
218 // INIT ALL INTERFACES
219 yarp::sig::Vector tmpZeros; tmpZeros.resize (_njoints, 0.0);
220 yarp::sig::Vector tmpOnes; tmpOnes.resize (_njoints, 1.0);
221 ControlBoardHelper cb(_njoints, _axisMap, _angleToEncoder, nullptr, nullptr, nullptr, nullptr);
223 ImplementEncodersTimed::initialize(_njoints, _axisMap, _angleToEncoder, nullptr);
224 ImplementMotorEncoders::initialize(_njoints, _axisMap, _angleToEncoder, nullptr);
225 ImplementAxisInfo::initialize(_njoints, _axisMap);
226 ImplementJointFault::initialize(_njoints, _axisMap);
227
228 //start the rateThread
229 bool init = this->start();
230 if(!init)
231 {
232 yCError(FAKEMOTIONCONTROLMICRO) << "open() has an error in call of FakeMotionControlMicro::init() for board" ;
233 return false;
234 }
235 else
236 {
237 if(verbosewhenok)
238 {
239 yCDebug(FAKEMOTIONCONTROLMICRO) << "init() has successfully initialized board ";
240 }
241 }
242 opened = true;
243
244 return true;
245}
246
248{
249 size_t i;
250
251 // AxisMap
252 if (!m_GENERAL_AxisMap.empty())
253 {
254 for (i = 0; i < m_GENERAL_AxisMap.size(); i++) {
255 _axisMap[i] = m_GENERAL_AxisMap[i];
256 }
257 }
258 else
259 {
260 for (i = 0; i < _njoints; i++) {
261 _axisMap[i] = i;
262 }
263 }
264
265 // AxisName
266 if (!m_GENERAL_AxisName.empty())
267 {
268 for (i = 0; i < m_GENERAL_AxisName.size(); i++) {
269 _axisName[_axisMap[i]] = m_GENERAL_AxisName[i];
270 }
271 }
272 else
273 {
274 for (i = 0; i < _njoints; i++) {
275 _axisName[_axisMap[i]] = "joint" + toString(i);
276 }
277 }
278
279 // Axis Type
280 if (!m_GENERAL_AxisType.empty())
281 {
282 //beware: axis type has to be remapped here because they are not set using the toHw() helper function
283 for (i = 0; i < m_GENERAL_AxisType.size(); i++)
284 {
285 std::string typeString = m_GENERAL_AxisType[i];
286 if (typeString == "revolute") {
287 _jointType[_axisMap[i]] = VOCAB_JOINTTYPE_REVOLUTE;
288 }
289 else if (typeString == "prismatic") {
290 _jointType[_axisMap[i]] = VOCAB_JOINTTYPE_PRISMATIC;
291 }
292 else {
293 yCError(FAKEMOTIONCONTROLMICRO, "Unknown AxisType value %s!", typeString.c_str());
294 _jointType[_axisMap[i]] = VOCAB_JOINTTYPE_UNKNOWN;
295 return false;
296 }
297 }
298 }
299 else
300 {
301 yCInfo(FAKEMOTIONCONTROLMICRO) << "Using default AxisType (revolute)";
302 for (i = 0; i < _njoints; i++) {
303 _jointType[_axisMap[i]] = VOCAB_JOINTTYPE_REVOLUTE;
304 }
305 }
306
307
308
309
310
311
312// double tmp_A2E;
313 // Encoder scales
314 if(!m_GENERAL_Encoder.empty())
315 {
316 for (i = 0; i < m_GENERAL_Encoder.size(); i++) {
317 _angleToEncoder[i] = m_GENERAL_Encoder[i]; }
318 }
319 else
320 {
321 yCInfo(FAKEMOTIONCONTROLMICRO) << "Using default Encoder=1";
322 for (i = 0; i < _njoints; i++) {
323 _angleToEncoder[i] = 1; }
324 }
325
326 return true;
327}
328
329
331{
332 std::lock_guard lock(_mutex);
333
335
336 yCTrace(FAKEMOTIONCONTROLMICRO) << " close()";
337
341
342// cleanup();
343
344 return true;
345}
346
347void FakeMotionControlMicro::cleanup()
348{
349
350}
351
353
355{
356 return NOT_YET_IMPLEMENTED("setEncoder");
357}
358
360{
361 return NOT_YET_IMPLEMENTED("setEncoders");
362}
363
365{
366 return NOT_YET_IMPLEMENTED("resetEncoder");
367}
368
370{
371 return NOT_YET_IMPLEMENTED("resetEncoders");
372}
373
375{
376 bool ret = true;
377
378 // To simulate a real controlboard, we assume that the joint
379 // encoders is exactly the last set by setPosition(s) or positionMove
380 *value = pos[j];
381
382 return ret;
383}
384
386{
387 bool ret = true;
388 for(int j=0; j< _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< _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< _njoints; j++)
426 {
428 }
429 return ret;
430}
431
433
435{
436 bool ret = getEncodersRaw(encs);
437 _mutex.lock();
438 for (int i = 0; i < _njoints; i++) {
439 stamps[i] = _encodersStamp[i];
440 }
441 _mutex.unlock();
442 return ret;
443}
444
445bool FakeMotionControlMicro::getEncoderTimedRaw(int j, double *encs, double *stamp)
446{
447 bool ret = getEncoderRaw(j, encs);
448 _mutex.lock();
449 *stamp = _encodersStamp[j];
450 _mutex.unlock();
451
452 return ret;
453}
454
456
458{
459 *num=_njoints;
460 return true;
461}
462
464{
465 return NOT_YET_IMPLEMENTED("setMotorEncoder");
466}
467
469{
470 return NOT_YET_IMPLEMENTED("setMotorEncoders");
471}
472
474{
475 return NOT_YET_IMPLEMENTED("setMotorEncoderCountsPerRevolutionRaw");
476}
477
479{
480 return NOT_YET_IMPLEMENTED("getMotorEncoderCountsPerRevolutionRaw");
481}
482
484{
485 return NOT_YET_IMPLEMENTED("resetMotorEncoder");
486}
487
489{
490 return NOT_YET_IMPLEMENTED("reseMotortEncoders");
491}
492
494{
495 *value = pos[m]*10;
496 return true;
497}
498
500{
501 bool ret = true;
502 for(int j=0; j< _njoints; j++)
503 {
504 ret &= getMotorEncoderRaw(j, &encs[j]);
505
506 }
507 return ret;
508}
509
511{
512 *sp = 0.0;
513 return true;
514}
515
517{
518 bool ret = true;
519 for(int j=0; j< _njoints; j++)
520 {
522 }
523 return ret;
524}
525
527{
528 *acc = 0.0;
529 return true;
530}
531
533{
534 bool ret = true;
535 for(int j=0; j< _njoints; j++)
536 {
538 }
539 return ret;
540}
541
543{
544 bool ret = getMotorEncodersRaw(encs);
545 _mutex.lock();
546 for (int i = 0; i < _njoints; i++) {
547 stamps[i] = _encodersStamp[i];
548 }
549 _mutex.unlock();
550
551 return ret;
552}
553
554bool FakeMotionControlMicro::getMotorEncoderTimedRaw(int m, double *encs, double *stamp)
555{
556 bool ret = getMotorEncoderRaw(m, encs);
557 _mutex.lock();
558 *stamp = _encodersStamp[m];
559 _mutex.unlock();
560
561 return ret;
562}
564
565
567{
568 if (axis >= 0 && axis < _njoints)
569 {
570 name = _axisName[axis];
571 return true;
572 }
573 else
574 {
575 name = "ERROR";
576 return false;
577 }
578}
579
581{
582 if (axis >= 0 && axis < _njoints)
583 {
584 type = _jointType[axis];
585 return true;
586 }
587 else
588 {
589 return false;
590 }
591}
592
593bool FakeMotionControlMicro::getLastJointFaultRaw(int j, int& fault, std::string& message)
594{
595 _mutex.lock();
596 fault = _hwfault_code[j];
597 message = _hwfault_message[j];
598 _mutex.unlock();
599 return true;
600}
601
603{
604 *ax = _njoints;
605 return true;
606}
607// eof
void checkAndDestroy(T *&p)
bool NOT_YET_IMPLEMENTED(const char *txt)
std::string toString(const T &value)
convert an arbitrary type to string.
static bool NOT_YET_IMPLEMENTED(const char *txt)
static bool DEPRECATED(const char *txt)
std::string toString(const T &value)
convert an arbitrary type to string.
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
bool ret
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
void threadRelease() override
Release method.
bool getJointTypeRaw(int axis, yarp::dev::JointTypeEnum &type) override
bool getMotorEncoderSpeedRaw(int j, double *sp) override
Read the istantaneous speed of a motor encoder.
bool alloc(int njoints)
Allocated buffers.
bool setEncodersRaw(const double *vals) override
Set the value of all encoders.
bool getEncoderRaw(int j, double *v) override
Read the value of an encoder.
bool getNumberOfMotorEncodersRaw(int *num) override
Get the number of available motor encoders.
bool getMotorEncoderAccelerationsRaw(double *accs) override
Read the instantaneous acceleration of all motor encoders.
bool getAxes(int *ax) override
Get the number of controlled axes.
bool fromConfig(yarp::os::Searchable &config)
bool resetEncodersRaw() override
Reset encoders.
bool getMotorEncoderTimedRaw(int m, double *encs, double *stamp) override
Read the instantaneous position of a motor encoder.
bool resetEncoderRaw(int j) override
Reset encoder, single joint.
bool getEncoderSpeedsRaw(double *spds) override
Read the instantaneous acceleration of an axis.
bool close() override
Close the DeviceDriver.
bool getEncodersRaw(double *encs) override
Read the position of all axes.
bool getMotorEncodersRaw(double *encs) override
Read the position of all motor encoders.
bool getEncodersTimedRaw(double *encs, double *stamps) override
Read the instantaneous acceleration of all axes.
bool setMotorEncoderRaw(int j, double val) override
Set the value of the motor encoder for a given motor.
bool getMotorEncodersTimedRaw(double *encs, double *stamps) override
Read the instantaneous position of all motor encoders.
void run() override
Loop function.
void resizeBuffers()
Resize previously allocated buffers.
bool resetMotorEncodersRaw() override
Reset motor encoders.
bool open(yarp::os::Searchable &par) override
Open the DeviceDriver.
bool getEncoderTimedRaw(int j, double *encs, double *stamp) override
Read the instantaneous acceleration of all axes.
bool getMotorEncoderCountsPerRevolutionRaw(int m, double *v) override
Gets number of counts per revolution for motor encoder m.
bool getMotorEncoderRaw(int j, double *v) override
Read the value of a motor encoder.
bool setEncoderRaw(int j, double val) override
Set the value of the encoder for a given joint.
bool threadInit() override
Initialization method.
bool getEncoderAccelerationRaw(int j, double *spds) override
Read the instantaneous acceleration of an axis.
bool getEncoderSpeedRaw(int j, double *sp) override
Read the instantaneous speed of an axis.
bool getMotorEncoderAccelerationRaw(int j, double *spds) override
Read the instantaneous acceleration of a motor encoder.
bool setMotorEncodersRaw(const double *vals) override
Set the value of all motor encoders.
bool getAxisNameRaw(int axis, std::string &name) override
bool getMotorEncoderSpeedsRaw(double *spds) override
Read the instantaneous speed of all motor encoders.
bool resetMotorEncoderRaw(int j) override
Reset motor encoder, single motor.
bool setMotorEncoderCountsPerRevolutionRaw(int m, const double cpr) override
Sets number of counts per revolution for motor encoder m.
bool getEncoderAccelerationsRaw(double *accs) override
Read the instantaneous acceleration of all axes.
bool getLastJointFaultRaw(int j, int &fault, std::string &message) override
bool uninitialize()
Clean up internal data and memory.
bool initialize(int size, const int *amap)
Initialize the internal data and alloc memory.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
bool uninitialize()
Clean up internal data and memory.
bool initialize(int size, const int *amap)
Initialize the internal data and alloc memory.
bool uninitialize()
Clean up internal data and memory.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Bottle.cpp:293
A mini-server for performing network communication in the background.
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A base class for nested structures that can be searched.
Definition Searchable.h:31
void resize(size_t size) override
Resize the vector.
Definition Vector.h:221
void zero()
Zero the elements of the vector.
Definition Vector.h:363
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition environment.h:66
For streams capable of holding different kinds of content, check what they actually have.
@ VOCAB_JOINTTYPE_REVOLUTE
Definition IAxisInfo.h:24
@ VOCAB_JOINTTYPE_PRISMATIC
Definition IAxisInfo.h:25
@ VOCAB_JOINTTYPE_UNKNOWN
Definition IAxisInfo.h:26
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.