YARP
Yet Another Robot Platform
ControlBoardWrapperMotorEncoders.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 
11 {
12  int off = device.lut[m].offset;
13  size_t subIndex = device.lut[m].deviceEntry;
14 
15  SubDevice* p = device.getSubdevice(subIndex);
16  if (!p) {
17  return false;
18  }
19 
20  if (p->iMotEnc) {
21  return p->iMotEnc->resetMotorEncoder(static_cast<int>(off + p->base));
22  }
23  return false;
24 }
25 
26 
28 {
29  bool ret = true;
30 
31  for (size_t l = 0; l < controlledJoints; l++) {
32  int off = device.lut[l].offset;
33  size_t subIndex = device.lut[l].deviceEntry;
34 
35  SubDevice* p = device.getSubdevice(subIndex);
36  if (!p) {
37  return false;
38  }
39 
40  if (p->iMotEnc) {
41  ret = ret && p->iMotEnc->resetMotorEncoder(static_cast<int>(off + p->base));
42  } else {
43  ret = false;
44  }
45  }
46  return ret;
47 }
48 
49 
51 {
52  int off = device.lut[m].offset;
53  size_t subIndex = device.lut[m].deviceEntry;
54 
55  SubDevice* p = device.getSubdevice(subIndex);
56  if (!p) {
57  return false;
58  }
59 
60  if (p->iMotEnc) {
61  return p->iMotEnc->setMotorEncoder(static_cast<int>(off + p->base), val);
62  }
63  return false;
64 }
65 
66 
68 {
69  bool ret = true;
70 
71  for (size_t l = 0; l < controlledJoints; l++) {
72  int off = device.lut[l].offset;
73  size_t subIndex = device.lut[l].deviceEntry;
74 
75  SubDevice* p = device.getSubdevice(subIndex);
76  if (!p) {
77  return false;
78  }
79 
80  if (p->iMotEnc) {
81  ret = ret && p->iMotEnc->setMotorEncoder(static_cast<int>(off + p->base), vals[l]);
82  } else {
83  ret = false;
84  }
85  }
86  return ret;
87 }
88 
89 
91 {
92  int off = device.lut[m].offset;
93  size_t subIndex = device.lut[m].deviceEntry;
94 
95  SubDevice* p = device.getSubdevice(subIndex);
96  if (!p) {
97  return false;
98  }
99 
100  if (p->iMotEnc) {
101  return p->iMotEnc->setMotorEncoderCountsPerRevolution(static_cast<int>(off + p->base), cpr);
102  }
103  return false;
104 }
105 
106 
108 {
109  int off = device.lut[m].offset;
110  size_t subIndex = device.lut[m].deviceEntry;
111 
112  SubDevice* p = device.getSubdevice(subIndex);
113  if (!p) {
114  return false;
115  }
116 
117  if (p->iMotEnc) {
118  return p->iMotEnc->getMotorEncoderCountsPerRevolution(static_cast<int>(off + p->base), cpr);
119  }
120  *cpr = 0.0;
121  return false;
122 }
123 
124 
126 {
127  int off = device.lut[m].offset;
128  size_t subIndex = device.lut[m].deviceEntry;
129 
130  SubDevice* p = device.getSubdevice(subIndex);
131  if (!p) {
132  return false;
133  }
134 
135  if (p->iMotEnc) {
136  return p->iMotEnc->getMotorEncoder(static_cast<int>(off + p->base), v);
137  }
138  *v = 0.0;
139  return false;
140 }
141 
142 
144 {
145 
146  auto* encValues = new double[device.maxNumOfJointsInDevices];
147  bool ret = true;
148  for (size_t d = 0; d < device.subdevices.size(); d++) {
149  SubDevice* p = device.getSubdevice(d);
150  if (!p) {
151  ret = false;
152  break;
153  }
154 
155  if ((p->iMotEnc) && (ret = p->iMotEnc->getMotorEncoders(encValues))) {
156  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
157  encs[juser] = encValues[jdevice];
158  }
159  } else {
160  printError("getMotorEncoders", p->id, ret);
161  ret = false;
162  break;
163  }
164  }
165 
166  delete[] encValues;
167  return ret;
168 }
169 
170 
172 {
173  auto* encValues = new double[device.maxNumOfJointsInDevices];
174  auto* tValues = new double[device.maxNumOfJointsInDevices];
175  bool ret = true;
176  for (size_t d = 0; d < device.subdevices.size(); d++) {
177  SubDevice* p = device.getSubdevice(d);
178  if (!p) {
179  ret = false;
180  break;
181  }
182 
183  if ((p->iMotEnc) && (ret = p->iMotEnc->getMotorEncodersTimed(encValues, tValues))) {
184  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
185  encs[juser] = encValues[jdevice];
186  t[juser] = tValues[jdevice];
187  }
188  } else {
189  printError("getMotorEncodersTimed", p->id, ret);
190  ret = false;
191  break;
192  }
193  }
194 
195  delete[] encValues;
196  delete[] tValues;
197  return ret;
198 }
199 
200 
202 {
203  int off = device.lut[m].offset;
204  size_t subIndex = device.lut[m].deviceEntry;
205 
206  SubDevice* p = device.getSubdevice(subIndex);
207  if (!p) {
208  return false;
209  }
210 
211  if (p->iMotEnc) {
212  return p->iMotEnc->getMotorEncoderTimed(static_cast<int>(off + p->base), v, t);
213  }
214  *v = 0.0;
215  return false;
216 }
217 
218 
220 {
221  int off = device.lut[m].offset;
222  size_t subIndex = device.lut[m].deviceEntry;
223 
224  SubDevice* p = device.getSubdevice(subIndex);
225  if (!p) {
226  return false;
227  }
228 
229  if (p->iMotEnc) {
230  return p->iMotEnc->getMotorEncoderSpeed(static_cast<int>(off + p->base), sp);
231  }
232  *sp = 0.0;
233  return false;
234 }
235 
236 
238 {
239  auto* sValues = new double[device.maxNumOfJointsInDevices];
240  bool ret = true;
241  for (size_t d = 0; d < device.subdevices.size(); d++) {
242  SubDevice* p = device.getSubdevice(d);
243  if (!p) {
244  ret = false;
245  break;
246  }
247 
248  if ((p->iMotEnc) && (ret = p->iMotEnc->getMotorEncoderSpeeds(sValues))) {
249  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
250  spds[juser] = sValues[jdevice];
251  }
252  } else {
253  printError("getMotorEncoderSpeeds", p->id, ret);
254  ret = false;
255  break;
256  }
257  }
258 
259  delete[] sValues;
260  return ret;
261 }
262 
263 
265 {
266  int off = device.lut[m].offset;
267  size_t subIndex = device.lut[m].deviceEntry;
268 
269  SubDevice* p = device.getSubdevice(subIndex);
270  if (!p) {
271  return false;
272  }
273 
274  if (p->iMotEnc) {
275  return p->iMotEnc->getMotorEncoderAcceleration(static_cast<int>(off + p->base), acc);
276  }
277  *acc = 0.0;
278  return false;
279 }
280 
281 
283 {
284  auto* aValues = new double[device.maxNumOfJointsInDevices];
285  bool ret = true;
286  for (size_t d = 0; d < device.subdevices.size(); d++) {
287  SubDevice* p = device.getSubdevice(d);
288  if (!p) {
289  ret = false;
290  break;
291  }
292 
293  if ((p->iMotEnc) && (ret = p->iMotEnc->getMotorEncoderAccelerations(aValues))) {
294  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
295  accs[juser] = aValues[jdevice];
296  }
297  } else {
298  printError("getMotorEncoderAccelerations", p->id, ret);
299  ret = false;
300  break;
301  }
302  }
303 
304  delete[] aValues;
305  return ret;
306 }
307 
308 
310 {
311  *num = controlledJoints;
312  return true;
313 }
float t
bool ret
void printError(const std::string &func_name, const std::string &info, bool result)
bool getMotorEncoderTimed(int m, double *v, double *t) override
Read the instantaneous position of a motor encoder.
bool getMotorEncoderCountsPerRevolution(int m, double *cpr) override
Gets number of counts per revolution for motor encoder m.
bool getMotorEncoder(int m, double *v) override
Read the value of a motor encoder.
bool getMotorEncodersTimed(double *encs, double *t) override
Read the instantaneous position of all motor encoders.
bool resetMotorEncoders() override
Reset motor encoders.
bool getMotorEncoderSpeeds(double *spds) override
Read the instantaneous speed of all motor encoders.
bool setMotorEncoderCountsPerRevolution(int m, const double cpr) override
Sets number of counts per revolution for motor encoder m.
bool getMotorEncoderAcceleration(int m, double *acc) override
Read the instantaneous acceleration of a motor encoder.
bool getNumberOfMotorEncoders(int *num) override
Get the number of available motor encoders.
bool setMotorEncoders(const double *vals) override
Set the value of all motor encoders.
bool getMotorEncoders(double *encs) override
Read the position of all motor encoders.
bool getMotorEncoderSpeed(int m, double *sp) override
Read the istantaneous speed of a motor encoder.
bool getMotorEncoderAccelerations(double *accs) override
Read the instantaneous acceleration of all motor encoders.
bool resetMotorEncoder(int m) override
Reset motor encoder, single motor.
bool setMotorEncoder(int m, const double val) override
Set the value of the motor encoder for a given motor.
yarp::dev::IMotorEncoders * iMotEnc
Definition: SubDevice.h:71
size_t base
Definition: SubDevice.h:55
std::string id
Definition: SubDevice.h:54
size_t wbase
Definition: SubDevice.h:57
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:122
SubDevice * getSubdevice(size_t i)
Definition: SubDevice.h:125
size_t maxNumOfJointsInDevices
Definition: SubDevice.h:123
SubDeviceVector subdevices
Definition: SubDevice.h:121
virtual bool getMotorEncodersTimed(double *encs, double *time)=0
Read the instantaneous position of all motor encoders.
virtual bool getMotorEncoderCountsPerRevolution(int m, double *cpr)=0
Gets number of counts per revolution for motor encoder m.
virtual bool setMotorEncoderCountsPerRevolution(int m, const double cpr)=0
Sets number of counts per revolution for motor encoder m.
virtual bool resetMotorEncoder(int m)=0
Reset motor encoder, single motor.
virtual bool getMotorEncoderSpeed(int m, double *sp)=0
Read the istantaneous speed of a motor encoder.
virtual bool getMotorEncoderSpeeds(double *spds)=0
Read the instantaneous speed of all motor encoders.
virtual bool getMotorEncoderTimed(int m, double *encs, double *time)=0
Read the instantaneous position of a motor encoder.
virtual bool getMotorEncoderAcceleration(int m, double *acc)=0
Read the instantaneous acceleration of a motor encoder.
virtual bool getMotorEncoderAccelerations(double *accs)=0
Read the instantaneous acceleration of all motor encoders.
virtual bool getMotorEncoders(double *encs)=0
Read the position of all motor encoders.
virtual bool setMotorEncoder(int m, const double val)=0
Set the value of the motor encoder for a given motor.
virtual bool getMotorEncoder(int m, double *v)=0
Read the value of a motor encoder.