iCub-main
CanBusAnalogSensor.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 __CANBUSANALOGSENSOR_H__
8 #define __CANBUSANALOGSENSOR_H__
9 
10 #include <mutex>
11 #include <yarp/os/PeriodicThread.h>
12 #include <yarp/dev/IAnalogSensor.h>
13 #include <yarp/dev/PolyDriver.h>
14 #include <yarp/dev/CanBusInterface.h>
15 #include <yarp/sig/Vector.h>
16 
17 using namespace yarp::os;
18 using namespace yarp::dev;
19 
42 class CanBusAnalogSensor : public PeriodicThread, public yarp::dev::IAnalogSensor, public DeviceDriver
43 {
44  enum AnalogDataFormat
45  {
46  ANALOG_FORMAT_ERR = 0,
47  ANALOG_FORMAT_8_BIT = 8,
48  ANALOG_FORMAT_16_BIT = 16,
49  };
50 
51  enum SensorStatus
52  {
53  ANALOG_IDLE=0,
54  ANALOG_OK=1,
55  ANALOG_NOT_RESPONDING=-1,
56  ANALOG_SATURATION=-2,
57  ANALOG_ERROR=-3,
58  };
59 
60 protected:
61  PolyDriver driver;
62  ICanBus *pCanBus;
63  ICanBufferFactory *pCanBufferFactory;
64  CanBuffer inBuffer;
65  CanBuffer outBuffer;
67 
68  std::mutex mtx;
69 
70  unsigned int channelsNum;
71  unsigned short boardId;
72  short status;
73  double timeStamp;
74  AnalogDataFormat dataFormat;
75  yarp::sig::Vector data;
76  yarp::sig::Vector scaleFactor;
77  unsigned short useCalibration;
78  bool diagnostic;
79 
80 public:
81  CanBusAnalogSensor(int period=20) : PeriodicThread((double)period/1000.0)
82  {}
83 
84 
86  {
87  }
88 
89  virtual bool open(yarp::os::Searchable& config);
90  virtual bool close();
91 
92 
93  //IAnalogSensor interface
94  virtual int read(yarp::sig::Vector &out);
95  virtual int getState(int ch);
96  virtual int getChannels();
97  int calibrateSensor();
98  virtual int calibrateChannel(int ch, double v);
99 
100  virtual int calibrateSensor(const yarp::sig::Vector& v);
101  virtual int calibrateChannel(int ch);
102 
103  virtual bool threadInit();
104  virtual void threadRelease();
105  virtual void run();
106 
107  //internal methods
108  private:
109  bool decode8 (const unsigned char *msg, int msg_id, double *data);
110  bool decode16(const unsigned char *msg, int msg_id, double *data);
111  bool sensor_start (yarp::os::Searchable& config);
112  bool sensor_stop ();
113  bool readFullScaleAnalog(int ch);
114 };
115 
116 
117 #endif
@ data
canbusanalogsensor : driver for CAN communication with IIT's analog sensor boards,...
ICanBufferFactory * pCanBufferFactory
yarp::sig::Vector scaleFactor
yarp::sig::Vector data
AnalogDataFormat dataFormat
CanBusAnalogSensor(int period=20)
unsigned short useCalibration
unsigned short boardId
unsigned int channelsNum
static int v
Definition: iCub_Sim.cpp:42
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
Definition: ethParser.cpp:92
out
Definition: sine.m:8