YARP
Yet Another Robot Platform
ImplementVelocityControl.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  iVelocity(y),
19  helper(nullptr),
20  intBuffManager(nullptr),
21  doubleBuffManager(nullptr)
22 {;}
23 
25 {
26  uninitialize();
27 }
28 
29 bool ImplementVelocityControl::initialize(int size, const int *axis_map, const double *enc, const double *zeros)
30 {
31  if (helper != nullptr) {
32  return false;
33  }
34 
35  helper=(void *)(new ControlBoardHelper(size, axis_map, enc, zeros));
36  yAssert (helper != nullptr);
37 
39  yAssert (intBuffManager != nullptr);
40 
42  yAssert (doubleBuffManager != nullptr);
43 
44  return true;
45 }
46 
48 {
49  if(helper != nullptr)
50  {
51  delete castToMapper(helper);
52  helper = nullptr;
53  }
54 
55  if(intBuffManager)
56  {
57  delete intBuffManager;
58  intBuffManager=nullptr;
59  }
60 
62  {
63  delete doubleBuffManager;
64  doubleBuffManager=nullptr;
65  }
66 
67  return true;
68 }
69 
71 {
72  (*ax)=castToMapper(helper)->axes();
73  return true;
74 }
75 
77 {
79  int k;
80  double enc;
81  castToMapper(helper)->velA2E(sp, j, enc, k);
82  return iVelocity->velocityMoveRaw(k, enc);
83 }
84 
85 bool ImplementVelocityControl::velocityMove(const int n_joint, const int *joints, const double *spds)
86 {
87  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
88  return false;
89  }
90 
93 
94  for(int idx=0; idx<n_joint; idx++)
95  {
96  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
97  buffValues[idx] = castToMapper(helper)->velA2E(spds[idx], joints[idx]);
98  }
99  bool ret = iVelocity->velocityMoveRaw(n_joint, buffJoints.getData(), buffValues.getData());
100 
101  doubleBuffManager->releaseBuffer(buffValues);
102  intBuffManager->releaseBuffer(buffJoints);
103  return ret;
104 }
105 
107 {
109  castToMapper(helper)->velA2E(sp, buffValues.getData());
110  bool ret = iVelocity->velocityMoveRaw(buffValues.getData());
111  doubleBuffManager->releaseBuffer(buffValues);
112  return ret;
113 }
114 
115 bool ImplementVelocityControl::getRefVelocity(const int j, double* vel)
116 {
118  int k;
119  double tmp;
120  k=castToMapper(helper)->toHw(j);
121  bool ret = iVelocity->getRefVelocityRaw(k, &tmp);
122  *vel=castToMapper(helper)->velE2A(tmp, k);
123  return ret;
124 }
125 
127 {
129  bool ret=iVelocity->getRefVelocitiesRaw(buffValues.getData());
130  castToMapper(helper)->velE2A(buffValues.getData(), vels);
131  doubleBuffManager->releaseBuffer(buffValues);
132  return ret;
133 }
134 
135 bool ImplementVelocityControl::getRefVelocities(const int n_joint, const int *joints, double *vels)
136 {
137  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
138  return false;
139  }
140 
143 
144  for(int idx=0; idx<n_joint; idx++)
145  {
146  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
147  }
148 
149  bool ret = iVelocity->getRefVelocitiesRaw(n_joint, buffJoints.getData(), buffValues.getData());
150 
151  for(int idx=0; idx<n_joint; idx++)
152  {
153  vels[idx]=castToMapper(helper)->velE2A(buffValues[idx], buffJoints[idx]);
154  }
155 
156  intBuffManager->releaseBuffer(buffJoints);
157  doubleBuffManager->releaseBuffer(buffValues);
158  return ret;
159 }
160 
162 {
164  int k;
165  double enc;
166  castToMapper(helper)->accA2E_abs(acc, j, enc, k);
167  return iVelocity->setRefAccelerationRaw(k, enc);
168 }
169 
170 bool ImplementVelocityControl::setRefAccelerations(const int n_joint, const int *joints, const double *accs)
171 {
172  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
173  return false;
174  }
175 
178 
179  for(int idx=0; idx<n_joint; idx++)
180  {
181  castToMapper(helper)->accA2E_abs(accs[idx], joints[idx], buffValues[idx], buffJoints[idx]);
182  }
183  bool ret = iVelocity->setRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
184 
185  doubleBuffManager->releaseBuffer(buffValues);
186  intBuffManager->releaseBuffer(buffJoints);
187 
188  return ret;
189 }
190 
192 {
194  castToMapper(helper)->accA2E_abs(accs, buffValues.getData());
195  bool ret = iVelocity->setRefAccelerationsRaw(buffValues.getData());
196  doubleBuffManager->releaseBuffer(buffValues);
197  return ret;
198 }
199 
201 {
203  int k;
204  double enc;
205  k=castToMapper(helper)->toHw(j);
206  bool ret = iVelocity->getRefAccelerationRaw(k, &enc);
207  *acc=castToMapper(helper)->accE2A_abs(enc, k);
208  return ret;
209 }
210 
211 bool ImplementVelocityControl::getRefAccelerations(const int n_joint, const int *joints, double *accs)
212 {
213  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
214  return false;
215  }
216 
219 
220  for(int idx=0; idx<n_joint; idx++)
221  {
222  buffJoints[idx]=castToMapper(helper)->toHw(joints[idx]);
223  }
224 
225  bool ret = iVelocity->getRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
226 
227  for(int idx=0; idx<n_joint; idx++)
228  {
229  accs[idx]=castToMapper(helper)->accE2A_abs(buffValues[idx], buffJoints[idx]);
230  }
231 
232  doubleBuffManager->releaseBuffer(buffValues);
233  intBuffManager->releaseBuffer(buffJoints);
234  return ret;
235 }
236 
237 
239 {
241  bool ret=iVelocity->getRefAccelerationsRaw(buffValues.getData());
242  castToMapper(helper)->accE2A_abs(buffValues.getData(), accs);
243  doubleBuffManager->releaseBuffer(buffValues);
244  return ret;
245 }
246 
247 
249 {
251  int k;
252  k=castToMapper(helper)->toHw(j);
253  return iVelocity->stopRaw(k);
254 }
255 
256 
257 bool ImplementVelocityControl::stop(const int n_joint, const int *joints)
258 {
259  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
260  return false;
261  }
262 
264  for(int idx=0; idx<n_joint; idx++)
265  {
266  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
267  }
268  bool ret = iVelocity->stopRaw(n_joint, buffJoints.getData());
269  intBuffManager->releaseBuffer(buffJoints);
270  return ret;
271 }
272 
273 
275 {
276  return iVelocity->stopRaw();
277 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:294
void velE2A(double enc, int j, double &ang, int &k)
void velA2E(double ang, int j, double &enc, int &k)
void accA2E_abs(double ang, int j, double &enc, int &k)
void accE2A_abs(double enc, int j, double &ang, int &k)
Interface for control boards implementig velocity control in encoder coordinates.
virtual bool setRefAccelerationRaw(int j, double acc)=0
Set reference acceleration for a joint.
virtual bool stopRaw(int j)=0
Stop motion, single joint.
virtual bool velocityMoveRaw(int j, double sp)=0
Start motion at a given speed, single joint.
virtual bool setRefAccelerationsRaw(const double *accs)=0
Set reference acceleration on all joints.
virtual bool getRefVelocityRaw(const int joint, double *vel)
Get the last reference speed set by velocityMove for single joint.
virtual bool getRefVelocitiesRaw(double *vels)
Get the last reference speed set by velocityMove for all joints.
virtual bool getRefAccelerationRaw(int j, double *acc)=0
Get reference acceleration for a joint.
virtual bool getRefAccelerationsRaw(double *accs)=0
Get reference acceleration of all joints.
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
ImplementVelocityControl(yarp::dev::IVelocityControlRaw *y)
Constructor.
bool uninitialize()
Clean up internal data and memory.
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
bool stop() override
Stop motion, multiple joints.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
bool getRefVelocity(const int joint, double *vel) override
Get the last reference speed set by velocityMove for single joint.
bool getRefVelocities(double *vels) override
Get the last reference speed set by velocityMove for all joints.
bool getAxes(int *axes) override
Get the number of controlled axes.
bool velocityMove(int j, double sp) override
Start motion at a given speed, single joint.
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.
yarp::sig::Vector zeros(int s)
Creates a vector of zeros (defined in Math.h).
Definition: math.cpp:576
An interface to the operating system, including Port based communication.