YARP
Yet Another Robot Platform
ImplementPositionControl.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 #include <cstdio>
7 
10 #include <yarp/os/Log.h>
12 
13 using namespace yarp::dev;
14 using namespace yarp::os;
15 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
16 
18  iPosition(y),
19  helper(nullptr),
20  intBuffManager(nullptr),
21  doubleBuffManager(nullptr),
22  boolBuffManager(nullptr)
23 {;}
24 
25 
27 {
28  uninitialize();
29 }
30 
39 bool ImplementPositionControl::initialize(int size, const int *amap, const double *enc, const double *zos)
40 {
41  if (helper != nullptr) {
42  return false;
43  }
44 
45  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos));
46  yAssert(helper != nullptr);
47 
49  yAssert (intBuffManager != nullptr);
50 
52  yAssert (doubleBuffManager != nullptr);
53 
55  yAssert (boolBuffManager != nullptr);
56  return true;
57 }
58 
64 {
65  if(helper != nullptr)
66  {
67  delete castToMapper(helper);
68  helper = nullptr;
69  }
70 
71  if(intBuffManager)
72  {
73  delete intBuffManager;
74  intBuffManager=nullptr;
75  }
76 
78  {
79  delete doubleBuffManager;
80  doubleBuffManager=nullptr;
81  }
82 
83  if(boolBuffManager)
84  {
85  delete boolBuffManager;
86  boolBuffManager=nullptr;
87  }
88 
89  return true;
90 }
91 
93 {
95  int k;
96  double enc;
97  castToMapper(helper)->posA2E(ang, j, enc, k);
98  return iPosition->positionMoveRaw(k, enc);
99 }
100 
101 bool ImplementPositionControl::positionMove(const int n_joint, const int *joints, const double *refs)
102 {
103  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
104  return false;
105  }
106 
109 
110  for(int idx=0; idx<n_joint; idx++)
111  {
112  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
113  buffValues[idx] = castToMapper(helper)->posA2E(refs[idx], joints[idx]);
114  }
115  bool ret = iPosition->positionMoveRaw(n_joint, buffJoints.getData(), buffValues.getData());
116 
117  intBuffManager->releaseBuffer(buffJoints);
118  doubleBuffManager->releaseBuffer(buffValues);
119  return ret;
120 }
121 
123 {
125  castToMapper(helper)->posA2E(refs, buffValues.getData());
126 
127  bool ret = iPosition->positionMoveRaw(buffValues.getData());
128  doubleBuffManager->releaseBuffer(buffValues);
129  return ret;
130 }
131 
132 bool ImplementPositionControl::relativeMove(int j, double delta)
133 {
135  int k;
136  double enc;
137  castToMapper(helper)->velA2E(delta, j, enc, k);
138 
139  return iPosition->relativeMoveRaw(k,enc);
140 }
141 
142 bool ImplementPositionControl::relativeMove(const int n_joint, const int *joints, const double *deltas)
143 {
144  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
145  return false;
146  }
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  }
183 
185  for(int idx=0; idx<n_joint; idx++)
186  {
187  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
188  }
189  bool ret = iPosition->checkMotionDoneRaw(n_joint, buffJoints.getData(), flags);
190  intBuffManager->releaseBuffer(buffJoints);
191  return ret;
192 }
193 
195 {
196  return iPosition->checkMotionDoneRaw(flag);
197 }
198 
200 {
202  int k;
203  double enc;
204  castToMapper(helper)->velA2E_abs(sp, j, enc, k);
205  return iPosition->setRefSpeedRaw(k, enc);
206 }
207 
208 bool ImplementPositionControl::setRefSpeeds(const int n_joint, const int *joints, const double *spds)
209 {
210  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
211  return false;
212  }
215  for(int idx=0; idx<n_joint; idx++)
216  {
217  castToMapper(helper)->velA2E_abs(spds[idx], joints[idx], buffValues[idx], buffJoints[idx]);
218  }
219  bool ret = iPosition->setRefSpeedsRaw(n_joint, buffJoints.getData(), buffValues.getData());
220  doubleBuffManager->releaseBuffer(buffValues);
221  intBuffManager->releaseBuffer(buffJoints);
222  return ret;
223 }
224 
226 {
228  castToMapper(helper)->velA2E_abs(spds, buffValues.getData());
229  bool ret = iPosition->setRefSpeedsRaw(buffValues.getData());
230  doubleBuffManager->releaseBuffer(buffValues);
231  return ret;
232 }
233 
235 {
237  int k;
238  double enc;
239 
240  castToMapper(helper)->accA2E_abs(acc, j, enc, k);
241  return iPosition->setRefAccelerationRaw(k, enc);
242 }
243 
244 bool ImplementPositionControl::setRefAccelerations(const int n_joint, const int *joints, const double *accs)
245 {
246  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
247  return false;
248  }
249 
252  for(int idx=0; idx<n_joint; idx++)
253  {
254  castToMapper(helper)->accA2E_abs(accs[idx], joints[idx], buffValues[idx], buffJoints[idx]);
255  }
256 
257  bool ret = iPosition->setRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
258  doubleBuffManager->releaseBuffer(buffValues);
259  intBuffManager->releaseBuffer(buffJoints);
260  return ret;
261 }
262 
264 {
266  castToMapper(helper)->accA2E_abs(accs, buffValues.getData());
267 
268  bool ret = iPosition->setRefAccelerationsRaw(buffValues.getData());
269  doubleBuffManager->releaseBuffer(buffValues);
270  return ret;
271 }
272 
274 {
276  int k;
277  double enc;
278  k=castToMapper(helper)->toHw(j);
279 
280  bool ret = iPosition->getRefSpeedRaw(k, &enc);
281 
282  *ref=(castToMapper(helper)->velE2A_abs(enc, k));
283 
284  return ret;
285 }
286 
287 bool ImplementPositionControl::getRefSpeeds(const int n_joint, const int *joints, double *spds)
288 {
289  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
290  return false;
291  }
292 
294  for(int idx=0; idx<n_joint; idx++)
295  {
296  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
297  }
298 
300  bool ret = iPosition->getRefSpeedsRaw(n_joint, buffJoints.getData(), buffValues.getData());
301 
302  for(int idx=0; idx<n_joint; idx++)
303  {
304  spds[idx]=castToMapper(helper)->velE2A_abs(buffValues[idx], buffJoints[idx]);
305  }
306  doubleBuffManager->releaseBuffer(buffValues);
307  intBuffManager->releaseBuffer(buffJoints);
308  return ret;
309 }
310 
312 {
314  bool ret = iPosition->getRefSpeedsRaw(buffValues.getData());
315  castToMapper(helper)->velE2A_abs(buffValues.getData(), spds);
316  doubleBuffManager->releaseBuffer(buffValues);
317  return ret;
318 }
319 
321 {
323  bool ret=iPosition->getRefAccelerationsRaw(buffValues.getData());
324  castToMapper(helper)->accE2A_abs(buffValues.getData(), accs);
325  doubleBuffManager->releaseBuffer(buffValues);
326  return ret;
327 }
328 
329 bool ImplementPositionControl::getRefAccelerations(const int n_joint, const int *joints, double *accs)
330 {
331  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
332  return false;
333  }
334 
336  for(int idx=0; idx<n_joint; idx++)
337  {
338  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
339  }
340 
342  bool ret = iPosition->getRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
343 
344  for(int idx=0; idx<n_joint; idx++)
345  {
346  accs[idx]=castToMapper(helper)->accE2A_abs(buffValues[idx], buffJoints[idx]);
347  }
348  doubleBuffManager->releaseBuffer(buffValues);
349  intBuffManager->releaseBuffer(buffJoints);
350  return ret;
351 }
352 
354 {
356  int k;
357  double enc;
358  k=castToMapper(helper)->toHw(j);
359  bool ret = iPosition->getRefAccelerationRaw(k, &enc);
360 
361  *acc=castToMapper(helper)->accE2A_abs(enc, k);
362 
363  return ret;
364 }
365 
367 {
369  int k;
370  k=castToMapper(helper)->toHw(j);
371 
372  return iPosition->stopRaw(k);
373 }
374 
375 bool ImplementPositionControl::stop(const int n_joint, const int *joints)
376 {
378  for(int idx=0; idx<n_joint; idx++)
379  {
380  buffValues[idx] = castToMapper(helper)->toHw(joints[idx]);
381  }
382 
383  bool ret = iPosition->stopRaw(n_joint, buffValues.getData());
384  intBuffManager->releaseBuffer(buffValues);
385  return ret;
386 }
387 
389 {
390  return iPosition->stopRaw();
391 }
392 
394 {
395  (*axis)=castToMapper(helper)->axes();
396 
397  return true;
398 }
399 
400 
401 bool ImplementPositionControl::getTargetPosition(const int joint, double* ref)
402 {
403  if (!castToMapper(helper)->checkAxisId(joint)) {
404  return false;
405  }
406  int k;
407  double enc;
408  k=castToMapper(helper)->toHw(joint);
409  bool ret = iPosition->getTargetPositionRaw(k, &enc);
410 
411  *ref=castToMapper(helper)->posE2A(enc, k);
412 
413  return ret;
414 }
415 
417 {
419  bool ret=iPosition->getTargetPositionsRaw(buffValues.getData());
420  castToMapper(helper)->posE2A(buffValues.getData(), refs);
421  doubleBuffManager->releaseBuffer(buffValues);
422  return ret;
423 }
424 
425 bool ImplementPositionControl::getTargetPositions(const int n_joint, const int* joints, double* refs)
426 {
427  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
428  return false;
429  }
430 
432  for(int idx=0; idx<n_joint; idx++)
433  {
434  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
435  }
437  bool ret = iPosition->getTargetPositionsRaw(n_joint, buffJoints.getData(), buffValues.getData());
438 
439  for(int idx=0; idx<n_joint; idx++)
440  {
441  refs[idx]=castToMapper(helper)->posE2A(buffValues[idx], buffJoints[idx]);
442  }
443  doubleBuffManager->releaseBuffer(buffValues);
444  intBuffManager->releaseBuffer(buffJoints);
445  return ret;
446 }
448 
449 
450 
451 // Stub interface
452 
453 bool StubImplPositionControlRaw::NOT_YET_IMPLEMENTED(const char *func)
454 {
455  if (func) {
456  yError("%s: not yet implemented\n", func);
457  } else {
458  yError("Function not yet implemented\n");
459  }
460 
461  return false;
462 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yError(...)
Definition: Log.h:279
#define yAssert(x)
Definition: Log.h:294
void velE2A_abs(double enc, int j, double &ang, int &k)
void velA2E(double ang, int j, double &enc, int &k)
void posE2A(double enc, int j, double &ang, int &k)
void accA2E_abs(double ang, int j, double &enc, int &k)
void accE2A_abs(double enc, int j, double &ang, int &k)
void velA2E_abs(double ang, int j, double &enc, int &k)
void posA2E(double ang, int j, double &enc, int &k)
Interface for a generic control board device implementing position control in encoder coordinates.
virtual bool getRefAccelerationRaw(int j, double *acc)=0
Get reference acceleration for a joint.
virtual bool stopRaw(int j)=0
Stop motion, single joint.
virtual bool getRefSpeedsRaw(double *spds)=0
Get reference speed of all joints.
virtual bool relativeMoveRaw(int j, double delta)=0
Set relative position.
virtual bool getTargetPositionsRaw(double *refs)
Get the last position reference for all axes.
virtual bool setRefAccelerationRaw(int j, double acc)=0
Set reference acceleration for a joint.
virtual bool checkMotionDoneRaw(int j, bool *flag)=0
Check if the current trajectory is terminated.
virtual bool getRefSpeedRaw(int j, double *ref)=0
Get reference speed for a joint.
virtual bool setRefAccelerationsRaw(const double *accs)=0
Set reference acceleration on all joints.
virtual bool positionMoveRaw(int j, double ref)=0
Set new reference point for a single axis.
virtual bool getTargetPositionRaw(const int joint, double *ref)
Get the last position reference for the specified axis.
virtual bool setRefSpeedsRaw(const double *spds)=0
Set reference speed on all joints.
virtual bool getRefAccelerationsRaw(double *accs)=0
Get reference acceleration of all joints.
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.
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override
Get reference speed of all joints.
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
bool getTargetPositions(double *refs) override
Get the last position reference for all axes.
bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override
Set reference acceleration on all joints.
bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override
Get reference acceleration for a joint.
bool relativeMove(int j, double delta) override
Set relative position.
bool getTargetPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
bool checkMotionDone(bool *flag) override
Check if the current trajectory is terminated.
bool setRefSpeed(int j, double sp) override
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
yarp::dev::impl::FixedSizeBuffersManager< bool > * boolBuffManager
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
ImplementPositionControl(yarp::dev::IPositionControlRaw *y)
Constructor.
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
bool getAxes(int *axis) override
Get the number of controlled axes.
bool stop() override
Stop motion, multiple joints.
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override
Set reference speed on all joints.
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.
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
T * getData()
Return the data pointer.
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
An interface for the device drivers.
An interface to the operating system, including Port based communication.