YARP
Yet Another Robot Platform
ImplementPositionControl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <cstdio>
10 
13 #include <yarp/os/Log.h>
15 
16 using namespace yarp::dev;
17 using namespace yarp::os;
18 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
19 
21  iPosition(y),
22  helper(nullptr),
23  intBuffManager(nullptr),
24  doubleBuffManager(nullptr),
25  boolBuffManager(nullptr)
26 {;}
27 
28 
30 {
31  uninitialize();
32 }
33 
42 bool ImplementPositionControl::initialize(int size, const int *amap, const double *enc, const double *zos)
43 {
44  if(helper != nullptr)
45  return false;
46 
47  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos));
48  yAssert(helper != nullptr);
49 
51  yAssert (intBuffManager != nullptr);
52 
54  yAssert (doubleBuffManager != nullptr);
55 
57  yAssert (boolBuffManager != nullptr);
58  return true;
59 }
60 
66 {
67  if(helper != nullptr)
68  {
69  delete castToMapper(helper);
70  helper = nullptr;
71  }
72 
73  if(intBuffManager)
74  {
75  delete intBuffManager;
76  intBuffManager=nullptr;
77  }
78 
80  {
81  delete doubleBuffManager;
82  doubleBuffManager=nullptr;
83  }
84 
85  if(boolBuffManager)
86  {
87  delete boolBuffManager;
88  boolBuffManager=nullptr;
89  }
90 
91  return true;
92 }
93 
95 {
97  int k;
98  double enc;
99  castToMapper(helper)->posA2E(ang, j, enc, k);
100  return iPosition->positionMoveRaw(k, enc);
101 }
102 
103 bool ImplementPositionControl::positionMove(const int n_joint, const int *joints, const double *refs)
104 {
105  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
106  return false;
107 
110 
111  for(int idx=0; idx<n_joint; idx++)
112  {
113  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
114  buffValues[idx] = castToMapper(helper)->posA2E(refs[idx], joints[idx]);
115  }
116  bool ret = iPosition->positionMoveRaw(n_joint, buffJoints.getData(), buffValues.getData());
117 
118  intBuffManager->releaseBuffer(buffJoints);
119  doubleBuffManager->releaseBuffer(buffValues);
120  return ret;
121 }
122 
124 {
126  castToMapper(helper)->posA2E(refs, buffValues.getData());
127 
128  bool ret = iPosition->positionMoveRaw(buffValues.getData());
129  doubleBuffManager->releaseBuffer(buffValues);
130  return ret;
131 }
132 
133 bool ImplementPositionControl::relativeMove(int j, double delta)
134 {
136  int k;
137  double enc;
138  castToMapper(helper)->velA2E(delta, j, enc, k);
139 
140  return iPosition->relativeMoveRaw(k,enc);
141 }
142 
143 bool ImplementPositionControl::relativeMove(const int n_joint, const int *joints, const double *deltas)
144 {
145  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
146  return false;
147 
150  for(int idx=0; idx<n_joint; idx++)
151  {
152  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
153  buffValues[idx] = castToMapper(helper)->velA2E(deltas[idx], joints[idx]);
154  }
155  bool ret = iPosition->relativeMoveRaw(n_joint, buffJoints.getData(), buffValues.getData());
156  doubleBuffManager->releaseBuffer(buffValues);
157  intBuffManager->releaseBuffer(buffJoints);
158  return ret;
159 }
160 
161 bool ImplementPositionControl::relativeMove(const double *deltas)
162 {
164  castToMapper(helper)->velA2E(deltas, buffValues.getData());
165  bool ret = iPosition->relativeMoveRaw(buffValues.getData());
166  doubleBuffManager->releaseBuffer(buffValues);
167  return ret;
168 }
169 
171 {
173  int k=castToMapper(helper)->toHw(j);
174 
175  return iPosition->checkMotionDoneRaw(k,flag);
176 }
177 
178 bool ImplementPositionControl::checkMotionDone(const int n_joint, const int *joints, bool *flags)
179 {
180  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
181  return false;
182 
184  for(int idx=0; idx<n_joint; idx++)
185  {
186  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
187  }
188  bool ret = iPosition->checkMotionDoneRaw(n_joint, buffJoints.getData(), flags);
189  intBuffManager->releaseBuffer(buffJoints);
190  return ret;
191 }
192 
194 {
195  return iPosition->checkMotionDoneRaw(flag);
196 }
197 
199 {
201  int k;
202  double enc;
203  castToMapper(helper)->velA2E_abs(sp, j, enc, k);
204  return iPosition->setRefSpeedRaw(k, enc);
205 }
206 
207 bool ImplementPositionControl::setRefSpeeds(const int n_joint, const int *joints, const double *spds)
208 {
209  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
210  return false;
213  for(int idx=0; idx<n_joint; idx++)
214  {
215  castToMapper(helper)->velA2E_abs(spds[idx], joints[idx], buffValues[idx], buffJoints[idx]);
216  }
217  bool ret = iPosition->setRefSpeedsRaw(n_joint, buffJoints.getData(), buffValues.getData());
218  doubleBuffManager->releaseBuffer(buffValues);
219  intBuffManager->releaseBuffer(buffJoints);
220  return ret;
221 }
222 
224 {
226  castToMapper(helper)->velA2E_abs(spds, buffValues.getData());
227  bool ret = iPosition->setRefSpeedsRaw(buffValues.getData());
228  doubleBuffManager->releaseBuffer(buffValues);
229  return ret;
230 }
231 
233 {
235  int k;
236  double enc;
237 
238  castToMapper(helper)->accA2E_abs(acc, j, enc, k);
239  return iPosition->setRefAccelerationRaw(k, enc);
240 }
241 
242 bool ImplementPositionControl::setRefAccelerations(const int n_joint, const int *joints, const double *accs)
243 {
244  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
245  return false;
246 
249  for(int idx=0; idx<n_joint; idx++)
250  {
251  castToMapper(helper)->accA2E_abs(accs[idx], joints[idx], buffValues[idx], buffJoints[idx]);
252  }
253 
254  bool ret = iPosition->setRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
255  doubleBuffManager->releaseBuffer(buffValues);
256  intBuffManager->releaseBuffer(buffJoints);
257  return ret;
258 }
259 
261 {
263  castToMapper(helper)->accA2E_abs(accs, buffValues.getData());
264 
265  bool ret = iPosition->setRefAccelerationsRaw(buffValues.getData());
266  doubleBuffManager->releaseBuffer(buffValues);
267  return ret;
268 }
269 
271 {
273  int k;
274  double enc;
275  k=castToMapper(helper)->toHw(j);
276 
277  bool ret = iPosition->getRefSpeedRaw(k, &enc);
278 
279  *ref=(castToMapper(helper)->velE2A_abs(enc, k));
280 
281  return ret;
282 }
283 
284 bool ImplementPositionControl::getRefSpeeds(const int n_joint, const int *joints, double *spds)
285 {
286  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
287  return false;
288 
290  for(int idx=0; idx<n_joint; idx++)
291  {
292  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
293  }
294 
296  bool ret = iPosition->getRefSpeedsRaw(n_joint, buffJoints.getData(), buffValues.getData());
297 
298  for(int idx=0; idx<n_joint; idx++)
299  {
300  spds[idx]=castToMapper(helper)->velE2A_abs(buffValues[idx], buffJoints[idx]);
301  }
302  doubleBuffManager->releaseBuffer(buffValues);
303  intBuffManager->releaseBuffer(buffJoints);
304  return ret;
305 }
306 
308 {
310  bool ret = iPosition->getRefSpeedsRaw(buffValues.getData());
311  castToMapper(helper)->velE2A_abs(buffValues.getData(), spds);
312  doubleBuffManager->releaseBuffer(buffValues);
313  return ret;
314 }
315 
317 {
319  bool ret=iPosition->getRefAccelerationsRaw(buffValues.getData());
320  castToMapper(helper)->accE2A_abs(buffValues.getData(), accs);
321  doubleBuffManager->releaseBuffer(buffValues);
322  return ret;
323 }
324 
325 bool ImplementPositionControl::getRefAccelerations(const int n_joint, const int *joints, double *accs)
326 {
327  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
328  return false;
329 
331  for(int idx=0; idx<n_joint; idx++)
332  {
333  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
334  }
335 
337  bool ret = iPosition->getRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
338 
339  for(int idx=0; idx<n_joint; idx++)
340  {
341  accs[idx]=castToMapper(helper)->accE2A_abs(buffValues[idx], buffJoints[idx]);
342  }
343  doubleBuffManager->releaseBuffer(buffValues);
344  intBuffManager->releaseBuffer(buffJoints);
345  return ret;
346 }
347 
349 {
351  int k;
352  double enc;
353  k=castToMapper(helper)->toHw(j);
354  bool ret = iPosition->getRefAccelerationRaw(k, &enc);
355 
356  *acc=castToMapper(helper)->accE2A_abs(enc, k);
357 
358  return ret;
359 }
360 
362 {
364  int k;
365  k=castToMapper(helper)->toHw(j);
366 
367  return iPosition->stopRaw(k);
368 }
369 
370 bool ImplementPositionControl::stop(const int n_joint, const int *joints)
371 {
373  for(int idx=0; idx<n_joint; idx++)
374  {
375  buffValues[idx] = castToMapper(helper)->toHw(joints[idx]);
376  }
377 
378  bool ret = iPosition->stopRaw(n_joint, buffValues.getData());
379  intBuffManager->releaseBuffer(buffValues);
380  return ret;
381 }
382 
384 {
385  return iPosition->stopRaw();
386 }
387 
389 {
390  (*axis)=castToMapper(helper)->axes();
391 
392  return true;
393 }
394 
395 
396 bool ImplementPositionControl::getTargetPosition(const int joint, double* ref)
397 {
398  if(!castToMapper(helper)->checkAxisId(joint))
399  return false;
400  int k;
401  double enc;
402  k=castToMapper(helper)->toHw(joint);
403  bool ret = iPosition->getTargetPositionRaw(k, &enc);
404 
405  *ref=castToMapper(helper)->posE2A(enc, k);
406 
407  return ret;
408 }
409 
411 {
413  bool ret=iPosition->getTargetPositionsRaw(buffValues.getData());
414  castToMapper(helper)->posE2A(buffValues.getData(), refs);
415  doubleBuffManager->releaseBuffer(buffValues);
416  return ret;
417 }
418 
419 bool ImplementPositionControl::getTargetPositions(const int n_joint, const int* joints, double* refs)
420 {
421  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
422  return false;
423 
425  for(int idx=0; idx<n_joint; idx++)
426  {
427  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
428  }
430  bool ret = iPosition->getTargetPositionsRaw(n_joint, buffJoints.getData(), buffValues.getData());
431 
432  for(int idx=0; idx<n_joint; idx++)
433  {
434  refs[idx]=castToMapper(helper)->posE2A(buffValues[idx], buffJoints[idx]);
435  }
436  doubleBuffManager->releaseBuffer(buffValues);
437  intBuffManager->releaseBuffer(buffJoints);
438  return ret;
439 }
441 
442 
443 
444 // Stub interface
445 
446 bool StubImplPositionControlRaw::NOT_YET_IMPLEMENTED(const char *func)
447 {
448  if (func)
449  yError("%s: not yet implemented\n", func);
450  else
451  yError("Function not yet implemented\n");
452 
453  return false;
454 }
yarp::dev::ImplementPositionControl::getRefAccelerations
bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override
Get reference acceleration for a joint.
Definition: ImplementPositionControl.cpp:325
yarp::dev::IPositionControlRaw::positionMoveRaw
virtual bool positionMoveRaw(int j, double ref)=0
Set new reference point for a single axis.
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::IPositionControlRaw::getTargetPositionRaw
virtual bool getTargetPositionRaw(const int joint, double *ref)
Get the last position reference for the specified axis.
Definition: IPositionControl.h:226
yarp::dev::ControlBoardHelper::posE2A
void posE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:290
yarp::dev::IPositionControlRaw::setRefAccelerationsRaw
virtual bool setRefAccelerationsRaw(const double *accs)=0
Set reference acceleration on all joints.
yarp::dev::ImplementPositionControl::setRefSpeed
bool setRefSpeed(int j, double sp) override
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
Definition: ImplementPositionControl.cpp:198
yarp::dev::ControlBoardHelper::velA2E_abs
void velA2E_abs(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:408
yarp::dev::ImplementPositionControl::boolBuffManager
yarp::dev::impl::FixedSizeBuffersManager< bool > * boolBuffManager
Definition: ImplementPositionControl.h:45
yarp::dev::ImplementPositionControl::iPosition
IPositionControlRaw * iPosition
Definition: ImplementPositionControl.h:41
ImplementPositionControl.h
yarp::dev::impl::Buffer::getData
T * getData()
Return the data pointer.
Definition: FixedSizeBuffersManager-inl.h:29
yarp::dev::impl::FixedSizeBuffersManager< int >
yarp::dev::impl::FixedSizeBuffersManager::getBuffer
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
Definition: FixedSizeBuffersManager-inl.h:87
yarp::dev::IPositionControlRaw::getRefSpeedRaw
virtual bool getRefSpeedRaw(int j, double *ref)=0
Get reference speed for a joint.
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::IPositionControlRaw::setRefAccelerationRaw
virtual bool setRefAccelerationRaw(int j, double acc)=0
Set reference acceleration for a joint.
yarp::dev::IPositionControlRaw::checkMotionDoneRaw
virtual bool checkMotionDoneRaw(int j, bool *flag)=0
Check if the current trajectory is terminated.
yError
#define yError(...)
Definition: Log.h:282
yarp::dev::ImplementPositionControl::setRefAccelerations
bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override
Set reference acceleration on all joints.
Definition: ImplementPositionControl.cpp:242
yarp::dev::ImplementPositionControl::intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
Definition: ImplementPositionControl.h:43
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
Log.h
yarp::dev::ImplementPositionControl::relativeMove
bool relativeMove(int j, double delta) override
Set relative position.
Definition: ImplementPositionControl.cpp:133
yarp::dev::ControlBoardHelper::velE2A_abs
void velE2A_abs(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:420
yarp::dev::ImplementPositionControl::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: ImplementPositionControl.cpp:348
yarp::dev::ImplementPositionControl::~ImplementPositionControl
virtual ~ImplementPositionControl()
Destructor.
Definition: ImplementPositionControl.cpp:29
yarp::dev::ControlBoardHelper::posA2E
void posA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:279
yarp::dev::ImplementPositionControl::initialize
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
Definition: ImplementPositionControl.cpp:42
FixedSizeBuffersManager.h
yarp::dev::impl::Buffer
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
Definition: FixedSizeBuffersManager.h:27
ControlBoardHelper.h
yarp::dev::ImplementPositionControl::stop
bool stop() override
Stop motion, multiple joints.
Definition: ImplementPositionControl.cpp:383
yarp::dev::IPositionControlRaw::setRefSpeedRaw
virtual bool setRefSpeedRaw(int j, double sp)=0
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::ImplementPositionControl::checkMotionDone
bool checkMotionDone(bool *flag) override
Check if the current trajectory is terminated.
Definition: ImplementPositionControl.cpp:193
yarp::dev::ImplementPositionControl::helper
void * helper
Definition: ImplementPositionControl.h:42
yarp::dev::IPositionControlRaw::relativeMoveRaw
virtual bool relativeMoveRaw(int j, double delta)=0
Set relative position.
yarp::dev::ControlBoardHelper::velA2E
void velA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:397
yarp::dev::ImplementPositionControl::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: ImplementPositionControl.cpp:232
JOINTIDCHECK
#define JOINTIDCHECK
Definition: ImplementPositionControl.cpp:18
yarp::dev::ControlBoardHelper::accE2A_abs
void accE2A_abs(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:441
yarp::dev::ControlBoardHelper::accA2E_abs
void accA2E_abs(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:431
yarp::dev::ImplementPositionControl::getRefSpeed
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
Definition: ImplementPositionControl.cpp:270
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
castToMapper
yarp::dev::ControlBoardHelper * castToMapper(void *p)
Definition: ControlBoardHelper.h:180
yarp::dev::ImplementPositionControl::setRefSpeeds
bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override
Set reference speed on all joints.
Definition: ImplementPositionControl.cpp:207
yarp::dev::IPositionControlRaw
Interface for a generic control board device implementing position control in encoder coordinates.
Definition: IPositionControl.h:30
yarp::dev::ImplementPositionControl::getTargetPosition
bool getTargetPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
Definition: ImplementPositionControl.cpp:396
yarp::dev::IPositionControlRaw::stopRaw
virtual bool stopRaw(int j)=0
Stop motion, single joint.
yarp::dev::ControlBoardHelper
Definition: ControlBoardHelper.h:60
yarp::dev::ImplementPositionControl::getRefSpeeds
bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override
Get reference speed of all joints.
Definition: ImplementPositionControl.cpp:284
yarp::dev::ImplementPositionControl::doubleBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
Definition: ImplementPositionControl.h:44
yarp::dev::ImplementPositionControl::getAxes
bool getAxes(int *axis) override
Get the number of controlled axes.
Definition: ImplementPositionControl.cpp:388
yarp::dev::ImplementPositionControl::ImplementPositionControl
ImplementPositionControl(yarp::dev::IPositionControlRaw *y)
Constructor.
Definition: ImplementPositionControl.cpp:20
yarp::dev::ControlBoardHelper::axes
int axes()
Definition: ControlBoardHelper.cpp:762
yarp::dev::IPositionControlRaw::getRefSpeedsRaw
virtual bool getRefSpeedsRaw(double *spds)=0
Get reference speed of all joints.
yarp::dev::ImplementPositionControl::getTargetPositions
bool getTargetPositions(double *refs) override
Get the last position reference for all axes.
Definition: ImplementPositionControl.cpp:410
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::dev::IPositionControlRaw::setRefSpeedsRaw
virtual bool setRefSpeedsRaw(const double *spds)=0
Set reference speed on all joints.
yarp::dev::IPositionControlRaw::getTargetPositionsRaw
virtual bool getTargetPositionsRaw(double *refs)
Get the last position reference for all axes.
Definition: IPositionControl.h:237
yarp::dev::IPositionControlRaw::getRefAccelerationsRaw
virtual bool getRefAccelerationsRaw(double *accs)=0
Get reference acceleration of all joints.
yarp::dev::ImplementPositionControl::positionMove
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
Definition: ImplementPositionControl.cpp:94
yarp::dev::ImplementPositionControl::uninitialize
bool uninitialize()
Clean up internal data and memory.
Definition: ImplementPositionControl.cpp:65
yarp::dev::IPositionControlRaw::getRefAccelerationRaw
virtual bool getRefAccelerationRaw(int j, double *acc)=0
Get reference acceleration for a joint.