YARP
Yet Another Robot Platform
FakeBot.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 #include <yarp/dev/DeviceDriver.h>
11 
14 #include <yarp/sig/Vector.h>
15 #include <yarp/sig/Image.h>
16 #include <yarp/os/Time.h>
17 #include <yarp/os/LogComponent.h>
18 
20 
21 
26 class FakeBot :
27  public yarp::dev::DeviceDriver,
28  public yarp::dev::IPositionControl,
29  public yarp::dev::IVelocityControl,
30  public yarp::dev::IAmplifierControl,
31  public yarp::dev::IEncodersTimed,
32  public yarp::dev::IFrameGrabberImage,
33  public yarp::dev::IControlCalibration,
34  public yarp::dev::IControlLimits,
35  public yarp::dev::DeviceResponder,
36  public yarp::os::Thread
37 {
38 private:
39  int njoints;
40  double m_x, m_y;
41  double m_dx, m_dy;
42  double m_tx, m_ty;
43  double m_tdx, m_tdy;
44  int m_w, m_h;
45  double xScale, yScale;
46  double noiseLevel;
47  yarp::sig::Vector pos, dpos, vel, speed, acc, loc, amp;
49  double lifetime;
50 
51  void init();
52 public:
53  FakeBot() :
54  njoints(2),
55  m_w(128),
56  m_h(128),
57  xScale(1),
58  yScale(1),
59  noiseLevel(0),
60  lifetime(-1)
61  {
62  pos.resize(njoints);
63  dpos.resize(njoints);
64  vel.resize(njoints);
65  speed.resize(njoints);
66  acc.resize(njoints);
67  loc.resize(njoints);
68  amp.resize(njoints);
69  for (int i=0; i<njoints; i++) {
70  pos[i] = 0;
71  dpos[i] = 0;
72  vel[i] = 0;
73  speed[i] = 0;
74  acc[i] = 0;
75  loc[i] = 0;
76  amp[i] = 1; // initially on - ok for simulator
77  }
78  init();
79  }
80 
81  bool open(yarp::os::Searchable& config) override;
82 
83  // IFrameGrabberImage
84  bool getImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image) override;
85 
86  int height() const override{
87  return m_h;
88  }
89 
90  int width() const override{
91  return m_w;
92  }
93 
94 
95 
96  // IPositionControl etc.
97 
98  bool getAxes(int *ax) override {
99  *ax = njoints;
100  return true;
101  }
102 
103  bool positionMove(int j, double ref) override {
104  if (j<njoints) {
105  pos[j] = ref;
106  }
107  return true;
108  }
109 
110 
111  bool positionMove(const double *refs) override {
112  for (int i=0; i<njoints; i++) {
113  pos[i] = refs[i];
114  }
115  return true;
116  }
117 
118 
119  bool relativeMove(int j, double delta) override {
120  if (j<njoints) {
121  dpos[j] = delta;
122  }
123  return true;
124  }
125 
126 
127  bool relativeMove(const double *deltas) override {
128  for (int i=0; i<njoints; i++) {
129  dpos[i] = deltas[i];
130  }
131  return true;
132  }
133 
134 
135  bool checkMotionDone(int j, bool *flag) override {
136  return true;
137  }
138 
139 
140  bool checkMotionDone(bool *flag) override {
141  return true;
142  }
143 
144 
145  bool setRefSpeed(int j, double sp) override {
146  if (j<njoints) {
147  speed[j] = sp;
148  }
149  return true;
150  }
151 
152 
153  bool setRefSpeeds(const double *spds) override {
154  for (int i=0; i<njoints; i++) {
155  speed[i] = spds[i];
156  }
157  return true;
158  }
159 
160 
161  bool setRefAcceleration(int j, double acc) override {
162  if (j<njoints) {
163  this->acc[j] = acc;
164  }
165  return true;
166  }
167 
168 
169  bool setRefAccelerations(const double *accs) override {
170  for (int i=0; i<njoints; i++) {
171  acc[i] = accs[i];
172  }
173  return true;
174  }
175 
176 
177  bool getRefSpeed(int j, double *ref) override {
178  if (j<njoints) {
179  (*ref) = speed[j];
180  }
181  return true;
182  }
183 
184 
185  bool getRefSpeeds(double *spds) override {
186  for (int i=0; i<njoints; i++) {
187  spds[i] = speed[i];
188  }
189  return true;
190  }
191 
192 
193  bool getRefAcceleration(int j, double *acc) override {
194  if (j<njoints) {
195  (*acc) = this->acc[j];
196  }
197  return true;
198  }
199 
200 
201  bool getRefAccelerations(double *accs) override {
202  for (int i=0; i<njoints; i++) {
203  accs[i] = acc[i];
204  }
205  return true;
206  }
207 
208 
209  bool stop(int j) override {
210  return true;
211  }
212 
213 
214  bool stop() override {
215  return true;
216  }
217 
218 
219  bool close() override {
220  return true;
221  }
222 
223  bool resetEncoder(int j) override {
224  if (j<njoints) {
225  pos[j] = 0;
226  dpos[j] = 0;
227  }
228  return true;
229  }
230 
231  bool resetEncoders() override {
232  for (int i=0; i<njoints; i++) {
233  pos[i] = 0;
234  }
235  return true;
236  }
237 
238  bool setEncoder(int j, double val) override {
239  if (j<njoints) {
240  pos[j] = val;
241  }
242  return true;
243  }
244 
245  bool setEncoders(const double *vals) override {
246  for (int i=0; i<njoints; i++) {
247  pos[i] = vals[i];
248  }
249  return true;
250  }
251 
252  bool getEncoder(int j, double *v) override {
253  if (j<njoints) {
254  (*v) = loc[j];
255  }
256 
257  return true;
258  }
259 
260  bool getEncoders(double *encs) override {
261  for (int i=0; i<njoints; i++) {
262  encs[i] = loc[i];
263  }
264  return true;
265  }
266 
267  bool getEncoderSpeed(int j, double *sp) override {
268  if (j<njoints) {
269  (*sp) = 0;
270  }
271  return true;
272  }
273 
274  bool getEncoderSpeeds(double *spds) override {
275  for (int i=0; i<njoints; i++) {
276  spds[i] = 0;
277  }
278  return true;
279  }
280 
281  bool getEncoderAcceleration(int j, double *spds) override {
282  if (j<njoints) {
283  (*spds) = 0;
284  }
285  return true;
286  }
287 
288  bool getEncoderAccelerations(double *accs) override {
289  for (int i=0; i<njoints; i++) {
290  accs[i] = 0;
291  }
292  return true;
293  }
294 
295  bool positionMove(const int n_joint, const int *joints, const double *refs) override { return false; }
296 
297  bool relativeMove(const int n_joint, const int *joints, const double *deltas) override { return false; }
298 
299  bool checkMotionDone(const int n_joint, const int *joints, bool *flags) override { return false; }
300 
301  bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override { return false; }
302 
303  bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override { return false; }
304 
305  bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override { return false; }
306 
307  bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override { return false; }
308 
309  bool stop(const int n_joint, const int *joints) override { return false; }
310 
311 
312  // IEncodersTimed
313  bool getEncodersTimed(double *encs, double *time) override
314  {
315  bool ret = getEncoders(encs);
316  double myTime = yarp::os::Time::now();
317 
318  for (int i=0; i<njoints; i++)
319  {
320  time[i] = myTime;
321  }
322  return ret;
323  }
324 
325  bool getEncoderTimed(int j, double *enc, double *time) override
326  {
327  bool ret = getEncoder(j, enc);
328  *time = yarp::os::Time::now();
329  return ret;
330  }
331 
332  bool velocityMove(int j, double sp) override {
333  if (j<njoints) {
334  vel[j] = sp;
335  }
336  return true;
337  }
338 
339  bool velocityMove(const double *sp) override {
340  for (int i=0; i<njoints; i++) {
341  vel[i] = sp[i];
342  }
343  return true;
344  }
345 
346  bool velocityMove(const int n_joint, const int *joints, const double *spds) override { return false; }
347 
348 
349 
350  bool enableAmp(int j) override {
351  if (j<njoints) {
352  amp[j] = 1;
353  }
354  return true;
355  }
356 
357  bool disableAmp(int j) override {
358  if (j<njoints) {
359  amp[j] = 0;
360  }
361  return true;
362  }
363 
364  bool getCurrent(int j, double *val) override {
365  if (j<njoints) {
366  val[j] = amp[j];
367  }
368  return true;
369  }
370 
371  bool getCurrents(double *vals) override {
372  for (int i=0; i<njoints; i++) {
373  vals[i] = amp[i];
374  }
375  return true;
376  }
377 
378  bool getMaxCurrent(int j, double* v) override {
379  *v = 0;
380  return true;
381  }
382 
383  bool setMaxCurrent(int j, double v) override {
384  return true;
385  }
386 
387  bool getAmpStatus(int *st) override {
388  *st = 0;
389  return true;
390  }
391 
392  bool getAmpStatus(int k, int *v) override
393  {
394  *v=0;
395  return true;
396  }
397 
398  bool calibrateAxisWithParams(int j, unsigned int iv, double v1, double v2, double v3) override
399  {
400  yCWarning(FAKEBOT, "Calibrating joint %d with parameters %u %lf %lf %lf", j, iv, v1, v2, v3);
401  return true;
402  }
403 
404  bool calibrationDone(int j) override
405  {
406  yCWarning(FAKEBOT, "Calibration done on joint %d.", j);
407  return true;
408  }
409 
410  bool getLimits(int axis, double *min, double *max) override
411  {
412  yCWarning(FAKEBOT, "Get limits");
413  *min=0;
414  *max=0;
415  return true;
416  }
417 
418  bool setLimits(int axis, double min, double max) override
419  {
420  yCWarning(FAKEBOT, "Set limits");
421  return true;
422  }
423 
424  bool setVelLimits(int axis, double min, double max) override { return false; }
425 
426  bool getVelLimits(int axis, double *min, double *max) override { return false; }
427 
428  void run() override;
429 };
YARP_DECLARE_LOG_COMPONENT
#define YARP_DECLARE_LOG_COMPONENT(name)
Definition: LogComponent.h:77
FakeBot::setVelLimits
bool setVelLimits(int axis, double min, double max) override
Set the software speed limits for a particular axis, the behavior of the control card when these limi...
Definition: FakeBot.h:424
FakeBot::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: FakeBot.h:193
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
FakeBot::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: FakeBot.h:161
FakeBot::velocityMove
bool velocityMove(int j, double sp) override
Start motion at a given speed, single joint.
Definition: FakeBot.h:332
FakeBot::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: FakeBot.h:346
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
FakeBot::width
int width() const override
Return the width of each frame.
Definition: FakeBot.h:90
FakeBot::setEncoder
bool setEncoder(int j, double val) override
Set the value of the encoder for a given joint.
Definition: FakeBot.h:238
FakeBot::close
bool close() override
Close the DeviceDriver.
Definition: FakeBot.h:219
Vector.h
contains the definition of a Vector type
FakeBot::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: FakeBot.h:145
FakeBot::getEncoderSpeeds
bool getEncoderSpeeds(double *spds) override
Read the instantaneous speed of all axes.
Definition: FakeBot.h:274
FakeBot::relativeMove
bool relativeMove(const int n_joint, const int *joints, const double *deltas) override
Set relative position for a subset of joints.
Definition: FakeBot.h:297
FakeBot::checkMotionDone
bool checkMotionDone(bool *flag) override
Check if the current trajectory is terminated.
Definition: FakeBot.h:140
FakeBot::stop
bool stop(const int n_joint, const int *joints) override
Stop motion for subset of joints.
Definition: FakeBot.h:309
FakeBot::relativeMove
bool relativeMove(int j, double delta) override
Set relative position.
Definition: FakeBot.h:119
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
FAKEBOT
const yarp::os::LogComponent & FAKEBOT()
Definition: FakeBot.cpp:22
FakeBot::positionMove
bool positionMove(const int n_joint, const int *joints, const double *refs) override
Set new reference point for a subset of joints.
Definition: FakeBot.h:295
FakeBot::getRefAccelerations
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
Definition: FakeBot.h:201
FakeBot::enableAmp
bool enableAmp(int j) override
Enable the amplifier on a specific joint.
Definition: FakeBot.h:350
FakeBot::getRefSpeed
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
Definition: FakeBot.h:177
FakeBot::getCurrents
bool getCurrents(double *vals) override
Definition: FakeBot.h:371
FakeBot::getLimits
bool getLimits(int axis, double *min, double *max) override
Get the software limits for a particular axis.
Definition: FakeBot.h:410
FakeBot::setMaxCurrent
bool setMaxCurrent(int j, double v) override
Definition: FakeBot.h:383
FakeBot::setLimits
bool setLimits(int axis, double min, double max) override
Set the software limits for a particular axis, the behavior of the control card when these limits are...
Definition: FakeBot.h:418
FakeBot::height
int height() const override
Return the height of each frame.
Definition: FakeBot.h:86
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
FakeBot::getEncoderAcceleration
bool getEncoderAcceleration(int j, double *spds) override
Read the instantaneous acceleration of an axis.
Definition: FakeBot.h:281
ControlBoardInterfaces.h
define control board standard interfaces
FakeBot::calibrationDone
bool calibrationDone(int j) override
Check if the calibration is terminated, on a particular joint.
Definition: FakeBot.h:404
FakeBot::getEncoderTimed
bool getEncoderTimed(int j, double *enc, double *time) override
Read the instantaneous acceleration of all axes.
Definition: FakeBot.h:325
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
FakeBot::setRefAccelerations
bool setRefAccelerations(const int n_joint, const int *joints, const double *accs) override
Set reference acceleration on all joints.
Definition: FakeBot.h:303
FakeBot::getEncodersTimed
bool getEncodersTimed(double *encs, double *time) override
Read the instantaneous acceleration of all axes.
Definition: FakeBot.h:313
yarp::sig::ImageOf< yarp::sig::PixelRgb >
FakeBot::getEncoderAccelerations
bool getEncoderAccelerations(double *accs) override
Read the instantaneous acceleration of all axes.
Definition: FakeBot.h:288
yarp::sig::VectorOf< double >
FakeBot::FakeBot
FakeBot()
Definition: FakeBot.h:53
FakeBot::setRefSpeeds
bool setRefSpeeds(const int n_joint, const int *joints, const double *spds) override
Set reference speed on all joints.
Definition: FakeBot.h:301
FakeBot::getAmpStatus
bool getAmpStatus(int k, int *v) override
Definition: FakeBot.h:392
FakeBot
fakebot: Documentation to be added
Definition: FakeBot.h:37
FakeBot::stop
bool stop(int j) override
Stop motion, single joint.
Definition: FakeBot.h:209
FakeBot::getMaxCurrent
bool getMaxCurrent(int j, double *v) override
Returns the maximum electric current allowed for a given motor.
Definition: FakeBot.h:378
FakeBot::resetEncoder
bool resetEncoder(int j) override
Reset encoder, single joint.
Definition: FakeBot.h:223
FakeBot::disableAmp
bool disableAmp(int j) override
Disable the amplifier on a specific joint.
Definition: FakeBot.h:357
FakeBot::getEncoderSpeed
bool getEncoderSpeed(int j, double *sp) override
Read the istantaneous speed of an axis.
Definition: FakeBot.h:267
FakeBot::setRefAccelerations
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
Definition: FakeBot.h:169
FrameGrabberInterfaces.h
define common interfaces to discover remote camera capabilities
LogComponent.h
Image.h
FakeBot::getAxes
bool getAxes(int *ax) override
Get the number of controlled axes.
Definition: FakeBot.h:98
FakeBot::getRefSpeeds
bool getRefSpeeds(const int n_joint, const int *joints, double *spds) override
Get reference speed of all joints.
Definition: FakeBot.h:305
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
FakeBot::resetEncoders
bool resetEncoders() override
Reset encoders.
Definition: FakeBot.h:231
FakeBot::getRefAccelerations
bool getRefAccelerations(const int n_joint, const int *joints, double *accs) override
Get reference acceleration for a joint.
Definition: FakeBot.h:307
FakeBot::positionMove
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
Definition: FakeBot.h:103
FakeBot::getEncoders
bool getEncoders(double *encs) override
Read the position of all axes.
Definition: FakeBot.h:260
FakeBot::setRefSpeeds
bool setRefSpeeds(const double *spds) override
Set reference speed on all joints.
Definition: FakeBot.h:153
FakeBot::relativeMove
bool relativeMove(const double *deltas) override
Set relative position, all joints.
Definition: FakeBot.h:127
FakeBot::velocityMove
bool velocityMove(const double *sp) override
Start motion at a given speed, multiple joints.
Definition: FakeBot.h:339
FakeBot::stop
bool stop() override
Stop motion, multiple joints.
Definition: FakeBot.h:214
Time.h
FakeBot::checkMotionDone
bool checkMotionDone(const int n_joint, const int *joints, bool *flags) override
Check if the current trajectory is terminated.
Definition: FakeBot.h:299
FakeBot::getAmpStatus
bool getAmpStatus(int *st) override
Definition: FakeBot.h:387
FakeBot::getCurrent
bool getCurrent(int j, double *val) override
Definition: FakeBot.h:364
FakeBot::setEncoders
bool setEncoders(const double *vals) override
Set the value of all encoders.
Definition: FakeBot.h:245
FakeBot::getRefSpeeds
bool getRefSpeeds(double *spds) override
Get reference speed of all joints.
Definition: FakeBot.h:185
FakeBot::getVelLimits
bool getVelLimits(int axis, double *min, double *max) override
Get the software speed limits for a particular axis.
Definition: FakeBot.h:426
FakeBot::getEncoder
bool getEncoder(int j, double *v) override
Read the value of an encoder.
Definition: FakeBot.h:252
FakeBot::checkMotionDone
bool checkMotionDone(int j, bool *flag) override
Check if the current trajectory is terminated.
Definition: FakeBot.h:135
FakeBot::positionMove
bool positionMove(const double *refs) override
Set new reference point for all axes.
Definition: FakeBot.h:111
FakeBot::calibrateAxisWithParams
bool calibrateAxisWithParams(int j, unsigned int iv, double v1, double v2, double v3) override
Start calibration, this method is very often platform specific.
Definition: FakeBot.h:398
DeviceDriver.h