iCub-main
EsdMessageSniffer.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2006 Giorgio Metta
5  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
6  *
7  */
8 
9 //
10 // $Id: EsdMessageSniffer.h,v 1.1 2007/07/26 08:24:49 babybot Exp $
11 //
12 //
13 
14 #ifndef __YARPEsdMessageSnifferh__
15 #define __YARPEsdMessageSnifferh__
16 
17 #include <mutex>
18 #include <condition_variable>
19 
20 #include <yarp/dev/DeviceDriver.h>
21 #include <yarp/dev/ControlBoardInterfaces.h>
22 #include <yarp/dev/ControlBoardInterfacesImpl.h>
23 #include <yarp/os/Thread.h>
24 
25 namespace yarp{
26  namespace dev{
27  class EsdMessageSniffer;
28  struct EsdMessageSnifferParameters;
29  }
30 }
31 
41 {
47 
52 
53  long int _txQueueSize;
54  long int _rxQueueSize;
55  long int _txTimeout;
56  long int _rxTimeout;
57 
58  int _networkN;
59  int _njoints;
60  unsigned char _my_address;
62  int _timeout;
63  int (*_p) (const char *fmt, ...);
65  unsigned char *_destinations;
66  int *_axisMap;
67  double *_angleToEncoder;
68  double *_zeros;
69 };
70 
78  public DeviceDriver,
79  public os::Thread,
80  public IPidControlRaw,
81  public IAmplifierControlRaw,
82  public IEncodersRaw,
83  public ImplementPidControl<EsdMessageSniffer, IPidControl>,
84  public ImplementAmplifierControl<EsdMessageSniffer, IAmplifierControl>,
85  public ImplementEncoders<EsdMessageSniffer, IEncoders>
86 {
87 private:
89  void operator=(const EsdMessageSniffer&);
90 
91 protected:
92  inline bool NOT_YET_IMPLEMENTED(const char *txt) {
93  fprintf(stderr, "%s not yet implemented for EsdMotionControl\n", txt);
94  return false;
95  }
96 
97 public:
102 
106  virtual ~EsdMessageSniffer();
107 
114  virtual bool open(EsdMessageSnifferParameters& params);
115 
121  virtual bool open(yarp::os::Searchable& config);
122 
127  virtual bool close(void);
128 
135  virtual bool getAxes(int *axis);
136 
137  virtual bool resetEncoderRaw(int j) { return NOT_YET_IMPLEMENTED("resetEncoderRaw"); }
138  virtual bool resetEncodersRaw() { return NOT_YET_IMPLEMENTED("resetEncodersRaw"); }
139  virtual bool setEncoderRaw(int j, double val) { return NOT_YET_IMPLEMENTED("setEncoderRaw"); }
140  virtual bool setEncodersRaw(const double *vals) { return NOT_YET_IMPLEMENTED("setEncodersRaw"); }
141  virtual bool getEncoderSpeedRaw(int j, double *sp) { return NOT_YET_IMPLEMENTED("getEncoderSpeedRaw"); }
142  virtual bool getEncoderSpeedsRaw(double *spds) { return NOT_YET_IMPLEMENTED("getEncoderSpeedsRaw"); }
143  virtual bool getEncoderAccelerationRaw(int j, double *spds) { return NOT_YET_IMPLEMENTED("getEncoderAccelerationRaw"); }
144  virtual bool getEncoderAccelerationsRaw(double *accs) { return NOT_YET_IMPLEMENTED("getEncoderAccelerationsRaw"); }
145 
152  virtual bool getEncoderRaw(int j, double *v) { return getBCastPosition(j, v); }
153 
159  virtual bool getEncodersRaw(double *encs) { return getBCastPositions(encs); }
160 
166  virtual bool getErrorRaw(int j, double *err) { return getBCastPositionError(j, err); }
167 
171  virtual bool getErrorsRaw(double *errs) { return getBCastPositionErrors(errs); }
172 
178  virtual bool getOutputRaw(int j, double *out) { return getBCastPIDOutput(j, out); }
179 
183  virtual bool getOutputsRaw(double *outs) { return getBCastPIDOutputs(outs); }
184 
185  virtual bool setPidRaw(int j, const Pid &pid) { return NOT_YET_IMPLEMENTED("setPidRaw"); }
186  virtual bool setPidsRaw(const Pid *pids) { return NOT_YET_IMPLEMENTED("setPidsRaw"); }
187  virtual bool setReferenceRaw(int j, double ref) { return NOT_YET_IMPLEMENTED("setReferenceRaw"); }
188  virtual bool setReferencesRaw(const double *refs) { return NOT_YET_IMPLEMENTED("setReferencesRaw"); }
189  virtual bool setErrorLimitRaw(int j, double limit) { return NOT_YET_IMPLEMENTED("setErrorLimitRaw"); }
190  virtual bool setErrorLimitsRaw(const double *limits) { return NOT_YET_IMPLEMENTED("setErrorLimitsRaw"); }
191  virtual bool getPidRaw(int j, Pid *pid) { return NOT_YET_IMPLEMENTED("getPidRaw"); }
192  virtual bool getPidsRaw(Pid *pids) { return NOT_YET_IMPLEMENTED("getPidsRaw"); }
193  virtual bool getReferenceRaw(int j, double *ref) { return NOT_YET_IMPLEMENTED("getReferenceRaw"); }
194  virtual bool getReferencesRaw(double *refs) { return NOT_YET_IMPLEMENTED("getReferencesRaw"); }
195  virtual bool getErrorLimitRaw(int j, double *limit) { return NOT_YET_IMPLEMENTED("getErrorLimitRaw"); }
196  virtual bool getErrorLimitsRaw(double *limits) { return NOT_YET_IMPLEMENTED("getErrorLimitsRaw"); }
197  virtual bool resetPidRaw(int j) { return NOT_YET_IMPLEMENTED("resetPidRaw"); }
198  virtual bool disablePidRaw(int j) { return NOT_YET_IMPLEMENTED("disablePidRaw"); }
199  virtual bool enablePidRaw(int j) { return NOT_YET_IMPLEMENTED("enablePidRaw"); }
200  virtual bool setOffsetRaw(int j, double v) { return NOT_YET_IMPLEMENTED("setOffsetRaw"); }
201 
207  virtual bool getCurrentsRaw(double *vals) { return getBCastCurrents(vals); }
208 
215  virtual bool getCurrentRaw(int j, double *val) { return getBCastCurrent(j, val); }
216 
217  /* Get the status of the amplifiers, coded in a 32 bits integer for
218  * each amplifier (at the moment contains only the fault, it will be
219  * expanded in the future).
220  * @param st pointer to storage
221  * @return true in good luck, false otherwise.
222  */
223  virtual bool getAmpStatusRaw(int *st) { return getBCastFaults(st); }
224 
225  virtual bool enableAmpRaw(int j) { return NOT_YET_IMPLEMENTED("enableAmpRaw"); }
226  virtual bool disableAmpRaw(int j) { return NOT_YET_IMPLEMENTED("disableAmpRaw"); }
227  virtual bool setMaxCurrentRaw(int j, double v) { return NOT_YET_IMPLEMENTED("setMaxCurrentRaw"); }
228 
229 protected:
230 
231  bool setBCastMessages (int i, int value);
232  bool getBCastPositions (double *p);
233  bool getBCastPosition (int i, double *value);
234  bool getBCastPIDOutput (int i, double *value);
235  bool getBCastPIDOutputs (double *p);
236  bool getBCastCurrent (int i, double *value);
237  bool getBCastCurrents (double *p);
238  bool getBCastFaults (int *p);
239  bool getBCastPositionError (int i, double *value);
240  bool getBCastPositionErrors (double *cmd);
241 
242  bool setDebugPrintFunction (void *cmd);
243 
244 protected:
246  std::mutex _mutex;
247  std::mutex mtx_done;
248  std::condition_variable cv_done;
249 
251  bool _noreply;
252 
259  inline bool ENABLED (int axis);
260 
264  int (*_p) (const char *fmt, ...);
265 
266  virtual void run(void);
267 
269  bool _writeWord16 (int msg, int axis, short s);
270  bool _writeWord16Ex (int msg, int axis, short s1, short s2);
271  bool _readWord16 (int msg, int axis, short& value);
272  bool _readWord16Array (int msg, double *out);
273  bool _readDWord (int msg, int axis, int& value);
274  bool _readDWordArray (int msg, double *out);
275  bool _writeDWord (int msg, int axis, int value);
276  bool _writeNone (int msg, int axis);
277 
278  enum { MAX_SHORT = 32767, MIN_SHORT = -32768, MAX_INT = 0x7fffffff, MIN_INT = 0x80000000 };
279  enum { ESD_CAN_SKIP_ADDR = 0x80 };
280 
281  inline short S_16(double x) const
282  {
283  if (x <= double(-(MAX_SHORT))-1)
284  return MIN_SHORT;
285  else
286  if (x >= double(MAX_SHORT))
287  return MAX_SHORT;
288  else
289  return short(x + .5);
290  }
291 
292  inline int S_32(double x) const
293  {
294  if (x <= double(-(MAX_INT))-1.0)
295  return MIN_INT;
296  else
297  if (x >= double(MAX_INT))
298  return MAX_INT;
299  else
300  return int(x + .5);
301  }
302 };
303 
304 
305 #endif
The EsdMessageSniffer device driver.
virtual bool setEncodersRaw(const double *vals)
virtual bool close(void)
Close the device driver.
short S_16(double x) const
virtual bool setErrorLimitsRaw(const double *limits)
virtual bool getAmpStatusRaw(int *st)
bool getBCastPositionErrors(double *cmd)
virtual bool getEncoderSpeedRaw(int j, double *sp)
virtual bool getEncodersRaw(double *encs)
Read the position of all axes.
int(* _p)(const char *fmt,...)
pointer to the function printing the device debug information.
virtual bool resetEncoderRaw(int j)
virtual bool enableAmpRaw(int j)
virtual bool setEncoderRaw(int j, double val)
virtual bool enablePidRaw(int j)
virtual bool disableAmpRaw(int j)
virtual bool setOffsetRaw(int j, double v)
bool _readWord16(int msg, int axis, short &value)
virtual bool open(EsdMessageSnifferParameters &params)
Open the device driver.
virtual bool getEncoderSpeedsRaw(double *spds)
virtual bool setReferencesRaw(const double *refs)
std::condition_variable cv_done
virtual ~EsdMessageSniffer()
Destructor.
virtual bool setReferenceRaw(int j, double ref)
virtual bool setPidsRaw(const Pid *pids)
bool _readWord16Array(int msg, double *out)
reads an array.
virtual bool disablePidRaw(int j)
virtual bool getCurrentRaw(int j, double *val)
Read the electric current going to a given motor.
virtual bool getOutputsRaw(double *outs)
Get the output of the controllers (e.g.
virtual bool setErrorLimitRaw(int j, double limit)
virtual bool getCurrentsRaw(double *vals)
Read the electric current going to all motors.
bool _writeDWord(int msg, int axis, int value)
write a DWord
virtual bool getEncoderRaw(int j, double *v)
Read the value of an encoder.
virtual bool setPidRaw(int j, const Pid &pid)
virtual bool getReferencesRaw(double *refs)
bool _writeWord16Ex(int msg, int axis, short s1, short s2)
two shorts in a single Can message (both must belong to the same control card).
virtual bool getAxes(int *axis)
Get the number of controlled axes.
virtual bool getPidRaw(int j, Pid *pid)
virtual bool setMaxCurrentRaw(int j, double v)
virtual bool getErrorLimitsRaw(double *limits)
virtual bool getPidsRaw(Pid *pids)
bool getBCastPIDOutput(int i, double *value)
virtual bool getEncoderAccelerationsRaw(double *accs)
bool getBCastCurrent(int i, double *value)
bool _readDWordArray(int msg, double *out)
reads an array of double words.
virtual bool resetPidRaw(int j)
virtual bool getEncoderAccelerationRaw(int j, double *spds)
bool getBCastPosition(int i, double *value)
reads an array of double from the broadcast message position buffer.
virtual bool getOutputRaw(int j, double *out)
Get the output of the controller (e.g.
virtual bool getErrorsRaw(double *errs)
Get the error of all joints.
bool _writeNone(int msg, int axis)
helper functions.
bool NOT_YET_IMPLEMENTED(const char *txt)
bool ENABLED(int axis)
helper function to check whether the enabled flag is on or off.
virtual bool getReferenceRaw(int j, double *ref)
virtual bool getErrorRaw(int j, double *err)
Get the current error for a joint.
virtual bool getErrorLimitRaw(int j, double *limit)
bool _writeWord16(int msg, int axis, short s)
helper functions
bool _readDWord(int msg, int axis, int &value)
sends a message and gets a dword back.
bool setBCastMessages(int i, int value)
sets the broadcast policy for a given board (don't need to be called twice).
bool getBCastPositionError(int i, double *value)
cmd
Definition: dataTypes.h:30
Copyright (C) 2008 RobotCub Consortium.
fprintf(fid,'\n')
out
Definition: sine.m:8
A container class to pass parameters to the open() method of the device driver.
unsigned char _my_address
number of joints (cards * 2)
int _timeout
thread polling interval [ms]
int(* _p)(const char *fmt,...)
number of cycles before timing out
unsigned char * _destinations
printf-like function for spying messages
int * _axisMap
destination addresses
double * _zeros
angle to encoder conversion factors
double * _angleToEncoder
axis remapping lookup-table