iCub-main
CanBusVirtualAnalogSensor.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 // Copyright: (C) 2013 iCub Facility
4 // Authors: Marco Randazzo <marco.randazzo@iit.it>
5 // CopyPolicy: Released under the terms of the GNU GPL v2.0.
6 
7 #ifndef __CANBUS_VIRTUAL_ANALOG_SENSOR_H__
8 #define __CANBUS_VIRTUAL_ANALOG_SENSOR_H__
9 
10 #include <string>
11 #include <mutex>
12 
13 #include <yarp/os/PeriodicThread.h>
14 #include <yarp/dev/ControlBoardInterfaces.h>
15 #include <yarp/dev/IVirtualAnalogSensor.h>
16 #include <yarp/dev/PolyDriver.h>
17 #include <yarp/dev/CanBusInterface.h>
18 #include <yarp/sig/Vector.h>
19 
20 using namespace yarp::os;
21 using namespace yarp::dev;
22 
23 class CanBusVirtualAnalogSensor : public PeriodicThread, public yarp::dev::IVirtualAnalogSensor, public DeviceDriver
24 {
25  enum AnalogDataFormat
26  {
27  ANALOG_FORMAT_ERR = 0,
28  ANALOG_FORMAT_8_BIT = 8,
29  ANALOG_FORMAT_16_BIT = 16,
30  };
31 
32  enum SensorStatus
33  {
34  ANALOG_IDLE=0,
35  ANALOG_OK=1,
36  ANALOG_NOT_RESPONDING=-1,
37  ANALOG_SATURATION=-2,
38  ANALOG_ERROR=-3,
39  };
40 
41 protected:
42  PolyDriver driver;
43  ICanBus *pCanBus;
44  ICanBufferFactory *pCanBufferFactory;
45  CanBuffer inBuffer;
46  CanBuffer outBuffer;
47 
48  std::mutex mtx;
49 
50  unsigned int channelsNum;
51  unsigned short boardId;
52  unsigned short canId;
53  //std::string deviceName;
54  yarp::dev::VAS_status status;
55  double timeStamp;
56  AnalogDataFormat dataFormat;
57  yarp::sig::Vector data;
58  yarp::sig::Vector scaleFactor;
60 
61 public:
62  CanBusVirtualAnalogSensor(int period=20) : PeriodicThread((double)period/1000.0)
63  {}
64 
65 
67  {
68  }
69 
70  virtual bool open(yarp::os::Searchable& config);
71  virtual bool close();
72 
73 
74  //IVirtualAnalogSensor interface
75  virtual yarp::dev::VAS_status getVirtualAnalogSensorStatus (int ch);
76  virtual int getVirtualAnalogSensorChannels();
77 
78  virtual bool updateVirtualAnalogSensorMeasure(int ch, double &measure);
79  virtual bool updateVirtualAnalogSensorMeasure(yarp::sig::Vector &data);
80 
81  virtual bool threadInit();
82  virtual void threadRelease();
83  virtual void run();
84 
85  //internal methods
86  private:
87  bool decode8 (const unsigned char *msg, int msg_id, double *data);
88  bool decode16(const unsigned char *msg, int msg_id, double *data);
89  bool sensor_start (yarp::os::Searchable& config);
90  bool sensor_stop ();
91  bool readFullScaleAnalog(int ch);
92 };
93 
94 
96 
97 
98 
100 /*
101 
102 class TBR_AnalogSensor: public yarp::dev::IAnalogSensor,
103  public yarp::dev::DeviceDriver
104 {
105 public:
106  enum AnalogDataFormat
107  {
108  ANALOG_FORMAT_8,
109  ANALOG_FORMAT_16,
110  };
111 
112  enum SensorStatus
113  {
114  ANALOG_IDLE=0,
115  ANALOG_OK=1,
116  ANALOG_NOT_RESPONDING=-1,
117  ANALOG_SATURATION=-2,
118  ANALOG_ERROR=-3,
119  };
120 
121 private:
122  // debug messages
123  unsigned int counterSat;
124  unsigned int counterError;
125  unsigned int counterTimeout;
126  int rate;
127 
129  //AnalogData *data;
130  short status;
131  double timeStamp;
132  double* scaleFactor;
133  std::mutex mtx;
134  AnalogDataFormat dataFormat;
135  yarp::os::Bottle initMsg;
136  yarp::os::Bottle speedMsg;
137  yarp::os::Bottle closeMsg;
138  std::string deviceIdentifier;
139  short boardId;
140  short useCalibration;
141  bool isVirtualSensor; //RANDAZ
142 
143  bool decode8(const unsigned char *msg, int id, double *data);
144  bool decode16(const unsigned char *msg, int id, double *data);
145 
146 public:
147  TBR_CanBackDoor* backDoor; //RANDAZ
148 
149  TBR_AnalogSensor();
150  ~TBR_AnalogSensor();
151  bool handleAnalog(void *);
152 
153  void resetCounters()
154  {
155  counterSat=0;
156  counterError=0;
157  counterTimeout=0;
158  }
159 
160  void getCounters(unsigned int &sat, unsigned int &err, unsigned int &to)
161  {
162  sat=counterSat;
163  err=counterError;
164  to=counterTimeout;
165  }
166 
167  void setDeviceId(std::string id)
168  {
169  deviceIdentifier=id;
170  }
171 
172  std::string getDeviceId()
173  {
174  return deviceIdentifier;
175  }
176 
177  short getId()
178  { return boardId;}
179 
180  short getStatus()
181  { return status;}
182 
183  bool isOpen()
184  {
185  if (data)
186  return true;
187  else
188  return false;
189  }
190 
191  short getUseCalibration()
192  {return useCalibration;}
193  double* getScaleFactor()
194  {return scaleFactor;}
195  double getScaleFactor(int chan)
196  {
197  if (chan>=0 && chan<data->size())
198  return scaleFactor[chan];
199  else
200  return 0;
201  }
202 
203  bool open(int channels, AnalogDataFormat f, short bId, short useCalib, bool isVirtualSensor);
204 
206 };
207 */
208 
209 #endif
@ data