YARP
Yet Another Robot Platform
FakeMotor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_FAKEMOTOR_FAKEMOTOR_H
11 #define YARP_FAKEMOTOR_FAKEMOTOR_H
12 
13 #include <cstdio>
14 
16 #include <yarp/dev/Drivers.h>
17 #include <yarp/dev/PolyDriver.h>
18 
19 #include <yarp/os/Time.h>
20 #include <yarp/os/Log.h>
21 #include <yarp/os/LogComponent.h>
22 #include <yarp/sig/Vector.h>
23 
25 
26 
33 class FakeMotor :
34  public yarp::dev::DeviceDriver,
35  public yarp::dev::IPositionControl,
36  public yarp::dev::IEncodersTimed,
37  public yarp::dev::IVelocityControl
38 {
39 private:
40  int njoints{1};
42  yarp::sig::Vector speed;
45  double delay{0};
46  double last{-1};
47  bool posMode{true};
48 
49  void update();
50 
51 public:
52  FakeMotor() = default;
53  FakeMotor(const FakeMotor&) = delete;
54  FakeMotor(FakeMotor&&) = delete;
55  FakeMotor& operator=(const FakeMotor&) = delete;
57  ~FakeMotor() override = default;
58 
59  bool getAxes(int *ax) override
60  {
61  *ax = njoints;
62  yCInfo(FAKEMOTOR, "FakeMotor reporting %d axes present", *ax);
63  return true;
64  }
65 
66  bool open(yarp::os::Searchable& config) override
67  {
68  njoints = config.check("axes",yarp::os::Value(4),"number of axes to pretend to have").asInt32();
69  pos.resize(njoints);
70  speed.resize(njoints);
71  acc.resize(njoints);
72  vel.resize(njoints);
73  for (int i=0; i<njoints; i++) {
74  pos[i] = 0;
75  speed[i] = 0;
76  acc[i] = 0;
77  vel[i] = 0;
78  }
79 
80  delay=config.check("delay",yarp::os::Value(0), "delay in each call for debugging purpose, in ms").asInt32();
81  return true;
82  }
83 
84 
85  bool positionMove(int j, double ref) override
86  {
87  posMode = true;
88  if (j<njoints) {
89  pos[j] = ref;
90  }
91  return true;
92  }
93 
94 
95  bool positionMove(const double *refs) override
96  {
97  posMode = true;
98  for (int i=0; i<njoints; i++) {
99  pos[i] = refs[i];
100  }
101  return true;
102  }
103 
104 
105  bool relativeMove(int j, double delta) override
106  {
107  posMode = true;
108  if (j<njoints) {
109  pos[j] += delta;
110  }
111  return true;
112  }
113 
114 
115  bool relativeMove(const double *deltas) override
116  {
117  posMode = true;
118  for (int i=0; i<njoints; i++) {
119  pos[i] += deltas[i];
120  }
121  return true;
122  }
123 
124 
125  bool checkMotionDone(int j, bool *flag) override
126  {
128  return true;
129  }
130 
131 
132  bool checkMotionDone(bool *flag) override
133  {
134  return true;
135  }
136 
137 
138  bool setRefSpeed(int j, double sp) override
139  {
140  if (j<njoints) {
141  speed[j] = sp;
142  }
143  return true;
144  }
145 
146 
147  bool setRefSpeeds(const double *spds) override
148  {
149  for (int i=0; i<njoints; i++) {
150  speed[i] = spds[i];
151  }
152  return true;
153  }
154 
155 
156  bool setRefAcceleration(int j, double acc) override
157  {
158  if (j<njoints) {
159  this->acc[j] = acc;
160  }
161  return true;
162  }
163 
164 
165  bool setRefAccelerations(const double *accs) override
166  {
167  for (int i=0; i<njoints; i++) {
168  acc[i] = accs[i];
169  }
170  return true;
171  }
172 
173 
174  bool getRefSpeed(int j, double *ref) override
175  {
176  if (j<njoints) {
177  (*ref) = speed[j];
178  }
179  return true;
180  }
181 
182 
183  bool getRefSpeeds(double *spds) override
184  {
185  for (int i=0; i<njoints; i++) {
186  spds[i] = speed[i];
187  }
188  return true;
189  }
190 
191 
192  bool getRefAcceleration(int j, double *acc) override
193  {
194  if (j<njoints) {
195  (*acc) = this->acc[j];
196  }
197  return true;
198  }
199 
200 
201  bool getRefAccelerations(double *accs) override
202  {
203  for (int i=0; i<njoints; i++) {
204  accs[i] = acc[i];
205  }
206  return true;
207  }
208 
209 
210  bool stop(int j) override
211  {
212  return true;
213  }
214 
215 
216  bool stop() override
217  {
218  return true;
219  }
220 
221  bool positionMove(const int n_joint, const int *joints, const double *refs) override
222  {
223  return false;
224  }
225 
226  bool relativeMove(const int n_joint, const int *joints, const double *deltas) override
227  {
228  return false;
229  }
230 
231  bool checkMotionDone(const int n_joint, const int *joints, bool *flags) override
232  {
233  return false;
234  }
235 
236  bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override
237  {
238  return false;
239  }
240 
241  bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override
242  {
243  return false;
244  }
245 
246  bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override
247  {
248  return false;
249  }
250 
251  bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override
252  {
253  return false;
254  }
255 
256  bool stop(const int n_joint, const int *joints) override
257  {
258  return false;
259  }
260 
261  bool getTargetPosition(const int joint, double *ref) override
262  {
263  return false;
264  }
265 
266  bool getTargetPositions(double *refs) override
267  {
268  return false;
269  }
270 
271  bool getTargetPositions(const int n_joint, const int *joints, double *refs) override
272  {
273  return false;
274  }
275 
276  bool velocityMove(const int n_joint, const int *joints, const double *spds) override
277  {
278  return false;
279  }
280 
281  bool close() override
282  {
283  return true;
284  }
285 
286  bool resetEncoder(int j) override
287  {
288  if (j<njoints) {
289  pos[j] = 0;
290  }
291  return true;
292  }
293 
294  bool resetEncoders() override
295  {
296  for (int i=0; i<njoints; i++) {
297  pos[i] = 0;
298  }
299  last = yarp::os::Time::now();
300  return true;
301  }
302 
303  bool setEncoder(int j, double val) override
304  {
305  if (j<njoints) {
306  pos[j] = val;
307  }
308  last = yarp::os::Time::now(); // not quite right, but ok for test
309  return true;
310  }
311 
312  bool setEncoders(const double *vals) override
313  {
314  for (int i=0; i<njoints; i++) {
315  pos[i] = vals[i];
316  }
317  last = yarp::os::Time::now();
318  return true;
319  }
320 
321  bool getEncoder(int j, double *v) override
322  {
323  update();
324  if (j<njoints) {
325  (*v) = pos[j];
326  }
327  return true;
328  }
329 
330  bool getEncoders(double *encs) override
331  {
332  update();
333  for (int i=0; i<njoints; i++) {
334  encs[i] = pos[i];
335  }
336  return true;
337  }
338 
339 
340  bool getEncoderTimed(int j, double *encs, double *time) override
341  {
342  bool ret = getEncoder(j, encs);
343  *time = yarp::os::Time::now();
344  return ret;
345  }
346 
347  bool getEncodersTimed(double *encs, double *time) override
348  {
349  for (int i=0; i<njoints; i++)
350  {
351  getEncoderTimed(i, &encs[i], &time[i]);
352  }
353  return true;
354  }
355 
356  bool getEncoderSpeed(int j, double *sp) override
357  {
358  if (j<njoints) {
359  (*sp) = 0;
360  }
361  return true;
362  }
363 
364  bool getEncoderSpeeds(double *spds) override
365  {
366  for (int i=0; i<njoints; i++) {
367  spds[i] = 0;
368  }
369  return true;
370  }
371 
372  bool getEncoderAcceleration(int j, double *spds) override
373  {
374  if (j<njoints) {
375  (*spds) = 0;
376  }
377  return true;
378  }
379 
380  bool getEncoderAccelerations(double *accs) override
381  {
382  for (int i=0; i<njoints; i++) {
383  accs[i] = 0;
384  }
385  return true;
386  }
387 
388  bool velocityMove(int j, double sp) override
389  {
390  posMode = false;
391  if (j<njoints) {
392  vel[j] = sp;
393  }
394  return true;
395  }
396 
397  bool velocityMove(const double *sp) override
398  {
399  posMode = false;
400  for (int i=0; i<njoints; i++) {
401  vel[i] = sp[i];
402  }
403  return true;
404  }
405 
406 };
407 
408 
409 class TestMotor : public FakeMotor
410 {
411 public:
412  bool open(yarp::os::Searchable& config) override;
413 };
414 
415 
416 #endif // YARP_FAKEMOTOR_FAKEMOTOR_H
FakeMotor::operator=
FakeMotor & operator=(const FakeMotor &)=delete
FakeMotor::positionMove
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
Definition: FakeMotor.h:85
YARP_DECLARE_LOG_COMPONENT
#define YARP_DECLARE_LOG_COMPONENT(name)
Definition: LogComponent.h:77
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
FakeMotor::velocityMove
bool velocityMove(const int n_joint, const int *joints, const double *spds) override
Start motion at a given speed for a subset of joints.
Definition: FakeMotor.h:276
Vector.h
contains the definition of a Vector type
FakeMotor::velocityMove
bool velocityMove(int j, double sp) override
Start motion at a given speed, single joint.
Definition: FakeMotor.h:388
Drivers.h
FakeMotor::getRefSpeeds
bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override
Get reference speed of all joints.
Definition: FakeMotor.h:246
FakeMotor::resetEncoder
bool resetEncoder(int j) override
Reset encoder, single joint.
Definition: FakeMotor.h:286
FakeMotor::FakeMotor
FakeMotor(const FakeMotor &)=delete
FakeMotor::relativeMove
bool relativeMove(int j, double delta) override
Set relative position.
Definition: FakeMotor.h:105
FakeMotor::operator=
FakeMotor & operator=(FakeMotor &&)=delete
FakeMotor::getEncoderAcceleration
bool getEncoderAcceleration(int j, double *spds) override
Read the instantaneous acceleration of an axis.
Definition: FakeMotor.h:372
FakeMotor::FakeMotor
FakeMotor(FakeMotor &&)=delete
FakeMotor::setEncoder
bool setEncoder(int j, double val) override
Set the value of the encoder for a given joint.
Definition: FakeMotor.h:303
FakeMotor::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: FakeMotor.h:156
FakeMotor::getEncoderAccelerations
bool getEncoderAccelerations(double *accs) override
Read the instantaneous acceleration of all axes.
Definition: FakeMotor.h:380
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
FakeMotor::getRefSpeed
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
Definition: FakeMotor.h:174
ControlBoardInterfaces.h
define control board standard interfaces
FakeMotor::relativeMove
bool relativeMove(const double *deltas) override
Set relative position, all joints.
Definition: FakeMotor.h:115
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
FakeMotor::getTargetPosition
bool getTargetPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
Definition: FakeMotor.h:261
FakeMotor::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: FakeMotor.h:66
yarp::sig::VectorOf< double >
FakeMotor::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: FakeMotor.h:138
FAKEMOTOR
const yarp::os::LogComponent & FAKEMOTOR()
Definition: FakeMotor.cpp:12
Log.h
FakeMotor::getRefAccelerations
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
Definition: FakeMotor.h:201
FakeMotor::relativeMove
bool relativeMove(const int n_joint, const int *joints, const double *deltas) override
Set relative position for a subset of joints.
Definition: FakeMotor.h:226
FakeMotor::getEncoderTimed
bool getEncoderTimed(int j, double *encs, double *time) override
Read the instantaneous acceleration of all axes.
Definition: FakeMotor.h:340
FakeMotor::positionMove
bool positionMove(const int n_joint, const int *joints, const double *refs) override
Set new reference point for a subset of joints.
Definition: FakeMotor.h:221
FakeMotor::FakeMotor
FakeMotor()=default
PolyDriver.h
FakeMotor::positionMove
bool positionMove(const double *refs) override
Set new reference point for all axes.
Definition: FakeMotor.h:95
FakeMotor::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: FakeMotor.h:192
FakeMotor::resetEncoders
bool resetEncoders() override
Reset encoders.
Definition: FakeMotor.h:294
FakeMotor::getEncoders
bool getEncoders(double *encs) override
Read the position of all axes.
Definition: FakeMotor.h:330
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
FakeMotor
fakeMotor: A fake motor control board for testing.
Definition: FakeMotor.h:38
FakeMotor::getRefAccelerations
bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override
Get reference acceleration for a joint.
Definition: FakeMotor.h:251
FakeMotor::checkMotionDone
bool checkMotionDone(int j, bool *flag) override
Check if the current trajectory is terminated.
Definition: FakeMotor.h:125
FakeMotor::setEncoders
bool setEncoders(const double *vals) override
Set the value of all encoders.
Definition: FakeMotor.h:312
FakeMotor::stop
bool stop(int j) override
Stop motion, single joint.
Definition: FakeMotor.h:210
FakeMotor::checkMotionDone
bool checkMotionDone(bool *flag) override
Check if the current trajectory is terminated.
Definition: FakeMotor.h:132
FakeMotor::close
bool close() override
Close the DeviceDriver.
Definition: FakeMotor.h:281
TestMotor::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: FakeMotor.cpp:32
FakeMotor::getAxes
bool getAxes(int *ax) override
Get the number of controlled axes.
Definition: FakeMotor.h:59
FakeMotor::setRefSpeeds
bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override
Set reference speed on all joints.
Definition: FakeMotor.h:236
LogComponent.h
FakeMotor::~FakeMotor
~FakeMotor() override=default
FakeMotor::setRefSpeeds
bool setRefSpeeds(const double *spds) override
Set reference speed on all joints.
Definition: FakeMotor.h:147
FakeMotor::setRefAccelerations
bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override
Set reference acceleration on all joints.
Definition: FakeMotor.h:241
FakeMotor::getEncodersTimed
bool getEncodersTimed(double *encs, double *time) override
Read the instantaneous acceleration of all axes.
Definition: FakeMotor.h:347
FakeMotor::getEncoderSpeed
bool getEncoderSpeed(int j, double *sp) override
Read the istantaneous speed of an axis.
Definition: FakeMotor.h:356
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
FakeMotor::getTargetPositions
bool getTargetPositions(const int n_joint, const int *joints, double *refs) override
Get the last position reference for the specified group of axes.
Definition: FakeMotor.h:271
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
FakeMotor::checkMotionDone
bool checkMotionDone(const int n_joint, const int *joints, bool *flags) override
Check if the current trajectory is terminated.
Definition: FakeMotor.h:231
FakeMotor::stop
bool stop() override
Stop motion, multiple joints.
Definition: FakeMotor.h:216
Time.h
FakeMotor::getEncoder
bool getEncoder(int j, double *v) override
Read the value of an encoder.
Definition: FakeMotor.h:321
TestMotor
Definition: FakeMotor.h:410
FakeMotor::velocityMove
bool velocityMove(const double *sp) override
Start motion at a given speed, multiple joints.
Definition: FakeMotor.h:397
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
FakeMotor::getTargetPositions
bool getTargetPositions(double *refs) override
Get the last position reference for all axes.
Definition: FakeMotor.h:266
FakeMotor::getEncoderSpeeds
bool getEncoderSpeeds(double *spds) override
Read the instantaneous speed of all axes.
Definition: FakeMotor.h:364
FakeMotor::setRefAccelerations
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
Definition: FakeMotor.h:165
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
FakeMotor::stop
bool stop(const int n_joint, const int *joints) override
Stop motion for subset of joints.
Definition: FakeMotor.h:256
FakeMotor::getRefSpeeds
bool getRefSpeeds(double *spds) override
Get reference speed of all joints.
Definition: FakeMotor.h:183