YARP
Yet Another Robot Platform
ImplementVelocityControl.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  iVelocity(y),
22  helper(nullptr),
23  intBuffManager(nullptr),
24  doubleBuffManager(nullptr)
25 {;}
26 
28 {
29  uninitialize();
30 }
31 
32 bool ImplementVelocityControl::initialize(int size, const int *axis_map, const double *enc, const double *zeros)
33 {
34  if (helper != nullptr)
35  return false;
36 
37  helper=(void *)(new ControlBoardHelper(size, axis_map, enc, zeros));
38  yAssert (helper != nullptr);
39 
41  yAssert (intBuffManager != nullptr);
42 
44  yAssert (doubleBuffManager != nullptr);
45 
46  return true;
47 }
48 
50 {
51  if(helper != nullptr)
52  {
53  delete castToMapper(helper);
54  helper = nullptr;
55  }
56 
57  if(intBuffManager)
58  {
59  delete intBuffManager;
60  intBuffManager=nullptr;
61  }
62 
64  {
65  delete doubleBuffManager;
66  doubleBuffManager=nullptr;
67  }
68 
69  return true;
70 }
71 
73 {
74  (*ax)=castToMapper(helper)->axes();
75  return true;
76 }
77 
79 {
81  int k;
82  double enc;
83  castToMapper(helper)->velA2E(sp, j, enc, k);
84  return iVelocity->velocityMoveRaw(k, enc);
85 }
86 
87 bool ImplementVelocityControl::velocityMove(const int n_joint, const int *joints, const double *spds)
88 {
89  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
90  return false;
91 
94 
95  for(int idx=0; idx<n_joint; idx++)
96  {
97  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
98  buffValues[idx] = castToMapper(helper)->velA2E(spds[idx], joints[idx]);
99  }
100  bool ret = iVelocity->velocityMoveRaw(n_joint, buffJoints.getData(), buffValues.getData());
101 
102  doubleBuffManager->releaseBuffer(buffValues);
103  intBuffManager->releaseBuffer(buffJoints);
104  return ret;
105 }
106 
108 {
110  castToMapper(helper)->velA2E(sp, buffValues.getData());
111  bool ret = iVelocity->velocityMoveRaw(buffValues.getData());
112  doubleBuffManager->releaseBuffer(buffValues);
113  return ret;
114 }
115 
116 bool ImplementVelocityControl::getRefVelocity(const int j, double* vel)
117 {
119  int k;
120  double tmp;
121  k=castToMapper(helper)->toHw(j);
122  bool ret = iVelocity->getRefVelocityRaw(k, &tmp);
123  *vel=castToMapper(helper)->velE2A(tmp, k);
124  return ret;
125 }
126 
128 {
130  bool ret=iVelocity->getRefVelocitiesRaw(buffValues.getData());
131  castToMapper(helper)->velE2A(buffValues.getData(), vels);
132  doubleBuffManager->releaseBuffer(buffValues);
133  return ret;
134 }
135 
136 bool ImplementVelocityControl::getRefVelocities(const int n_joint, const int *joints, double *vels)
137 {
138  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
139  return false;
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 
177 
178  for(int idx=0; idx<n_joint; idx++)
179  {
180  castToMapper(helper)->accA2E_abs(accs[idx], joints[idx], buffValues[idx], buffJoints[idx]);
181  }
182  bool ret = iVelocity->setRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
183 
184  doubleBuffManager->releaseBuffer(buffValues);
185  intBuffManager->releaseBuffer(buffJoints);
186 
187  return ret;
188 }
189 
191 {
193  castToMapper(helper)->accA2E_abs(accs, buffValues.getData());
194  bool ret = iVelocity->setRefAccelerationsRaw(buffValues.getData());
195  doubleBuffManager->releaseBuffer(buffValues);
196  return ret;
197 }
198 
200 {
202  int k;
203  double enc;
204  k=castToMapper(helper)->toHw(j);
205  bool ret = iVelocity->getRefAccelerationRaw(k, &enc);
206  *acc=castToMapper(helper)->accE2A_abs(enc, k);
207  return ret;
208 }
209 
210 bool ImplementVelocityControl::getRefAccelerations(const int n_joint, const int *joints, double *accs)
211 {
212  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
213  return false;
214 
217 
218  for(int idx=0; idx<n_joint; idx++)
219  {
220  buffJoints[idx]=castToMapper(helper)->toHw(joints[idx]);
221  }
222 
223  bool ret = iVelocity->getRefAccelerationsRaw(n_joint, buffJoints.getData(), buffValues.getData());
224 
225  for(int idx=0; idx<n_joint; idx++)
226  {
227  accs[idx]=castToMapper(helper)->accE2A_abs(buffValues[idx], buffJoints[idx]);
228  }
229 
230  doubleBuffManager->releaseBuffer(buffValues);
231  intBuffManager->releaseBuffer(buffJoints);
232  return ret;
233 }
234 
235 
237 {
239  bool ret=iVelocity->getRefAccelerationsRaw(buffValues.getData());
240  castToMapper(helper)->accE2A_abs(buffValues.getData(), accs);
241  doubleBuffManager->releaseBuffer(buffValues);
242  return ret;
243 }
244 
245 
247 {
249  int k;
250  k=castToMapper(helper)->toHw(j);
251  return iVelocity->stopRaw(k);
252 }
253 
254 
255 bool ImplementVelocityControl::stop(const int n_joint, const int *joints)
256 {
257  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
258  return false;
259 
261  for(int idx=0; idx<n_joint; idx++)
262  {
263  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
264  }
265  bool ret = iVelocity->stopRaw(n_joint, buffJoints.getData());
266  intBuffManager->releaseBuffer(buffJoints);
267  return ret;
268 }
269 
270 
272 {
273  return iVelocity->stopRaw();
274 }
yarp::dev::ControlBoardHelper::velE2A
void velE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:414
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::ImplementVelocityControl::stop
bool stop() override
Stop motion, multiple joints.
Definition: ImplementVelocityControl.cpp:271
yarp::dev::IVelocityControlRaw::getRefVelocitiesRaw
virtual bool getRefVelocitiesRaw(double *vels)
Get the last reference speed set by velocityMove for all joints.
Definition: IVelocityControl.h:120
yarp::dev::ImplementVelocityControl::initialize
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
Definition: ImplementVelocityControl.cpp:32
yarp::dev::IVelocityControlRaw::setRefAccelerationRaw
virtual bool setRefAccelerationRaw(int j, double acc)=0
Set reference acceleration for a joint.
yarp::dev::ImplementVelocityControl::doubleBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
Definition: ImplementVelocityControl.h:40
yarp::math::zeros
yarp::sig::Vector zeros(int s)
Creates a vector of zeros (defined in Math.h).
Definition: math.cpp:552
yarp::dev::ImplementVelocityControl::velocityMove
bool velocityMove(int j, double sp) override
Start motion at a given speed, single joint.
Definition: ImplementVelocityControl.cpp:78
yarp::dev::impl::Buffer::getData
T * getData()
Return the data pointer.
Definition: FixedSizeBuffersManager-inl.h:29
yarp::dev::ImplementVelocityControl::ImplementVelocityControl
ImplementVelocityControl(yarp::dev::IVelocityControlRaw *y)
Constructor.
Definition: ImplementVelocityControl.cpp:20
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::ImplementVelocityControl::uninitialize
bool uninitialize()
Clean up internal data and memory.
Definition: ImplementVelocityControl.cpp:49
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::ImplementVelocityControl::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: ImplementVelocityControl.cpp:199
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
Log.h
yarp::dev::ImplementVelocityControl::helper
void * helper
Definition: ImplementVelocityControl.h:38
yarp::dev::ImplementVelocityControl::iVelocity
IVelocityControlRaw * iVelocity
Definition: ImplementVelocityControl.h:37
JOINTIDCHECK
#define JOINTIDCHECK
Definition: ImplementVelocityControl.cpp:18
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
yarp::dev::IVelocityControlRaw::getRefVelocityRaw
virtual bool getRefVelocityRaw(const int joint, double *vel)
Get the last reference speed set by velocityMove for single joint.
Definition: IVelocityControl.h:114
ControlBoardHelper.h
ImplementVelocityControl.h
yarp::dev::IVelocityControlRaw::stopRaw
virtual bool stopRaw(int j)=0
Stop motion, single joint.
yarp::dev::IVelocityControlRaw::getRefAccelerationsRaw
virtual bool getRefAccelerationsRaw(double *accs)=0
Get reference acceleration of all joints.
yarp::dev::ImplementVelocityControl::getRefVelocity
bool getRefVelocity(const int joint, double *vel) override
Get the last reference speed set by velocityMove for single joint.
Definition: ImplementVelocityControl.cpp:116
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::ImplementVelocityControl::getRefVelocities
bool getRefVelocities(double *vels) override
Get the last reference speed set by velocityMove for all joints.
Definition: ImplementVelocityControl.cpp:127
yarp::dev::ControlBoardHelper::velA2E
void velA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:397
yarp::dev::ImplementVelocityControl::intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
Definition: ImplementVelocityControl.h:39
yarp::dev::ControlBoardHelper::accE2A_abs
void accE2A_abs(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:441
yarp::dev::ImplementVelocityControl::getAxes
bool getAxes(int *axes) override
Get the number of controlled axes.
Definition: ImplementVelocityControl.cpp:72
yarp::dev::ControlBoardHelper::accA2E_abs
void accA2E_abs(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:431
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::dev::IVelocityControlRaw::getRefAccelerationRaw
virtual bool getRefAccelerationRaw(int j, double *acc)=0
Get reference acceleration for a joint.
castToMapper
yarp::dev::ControlBoardHelper * castToMapper(void *p)
Definition: ControlBoardHelper.h:180
yarp::dev::IVelocityControlRaw::velocityMoveRaw
virtual bool velocityMoveRaw(int j, double sp)=0
Start motion at a given speed, single joint.
yarp::dev::ImplementVelocityControl::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: ImplementVelocityControl.cpp:161
yarp::dev::ControlBoardHelper
Definition: ControlBoardHelper.h:60
yarp::dev::IVelocityControlRaw::setRefAccelerationsRaw
virtual bool setRefAccelerationsRaw(const double *accs)=0
Set reference acceleration on all joints.
yarp::dev::ControlBoardHelper::axes
int axes()
Definition: ControlBoardHelper.cpp:762
yarp::dev::IVelocityControlRaw
Interface for control boards implementig velocity control in encoder coordinates.
Definition: IVelocityControl.h:29
yarp::dev::ImplementVelocityControl::getRefAccelerations
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
Definition: ImplementVelocityControl.cpp:236
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::dev::ImplementVelocityControl::~ImplementVelocityControl
virtual ~ImplementVelocityControl()
Destructor.
Definition: ImplementVelocityControl.cpp:27
yarp::dev::ImplementVelocityControl::setRefAccelerations
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
Definition: ImplementVelocityControl.cpp:190