9#include <yarp/os/Time.h>
10#include <yarp/os/LogStream.h>
11#include <yarp/os/Log.h>
24 fprintf(stderr,
"%s\n", config.toString().c_str());
26 correct &= config.check(
"canbusDevice");
27 correct &= config.check(
"canDeviceNum");
28 correct &= config.check(
"canAddress");
29 correct &= config.check(
"format");
30 correct &= config.check(
"period");
31 correct &= config.check(
"channels");
32 correct &= config.check(
"physDevice");
36 yError() <<
"Insufficient parameters to CanBusAnalogSensor\n";
40 int period=config.find(
"period").asInt32();
41 setPeriod((
double)period/1000.0);
45 prop.put(
"device", config.find(
"canbusDevice").asString().c_str());
46 prop.put(
"physDevice", config.find(
"physDevice").asString().c_str());
47 prop.put(
"canTxTimeout", 500);
48 prop.put(
"canRxTimeout", 500);
51 prop.put(
"canMyAddress", 0);
61 yError() <<
"Error opening PolyDriver check parameters";
67 yError() <<
"Error opening can device not available";
80 this->
boardId = config.find(
"canAddress").asInt32();
83 unsigned int tmpFormat = config.find(
"format").asInt32();
86 else if (tmpFormat == 16)
92 if( config.check(
"diagnostic") && config.find(
"diagnostic").asInt32() == 1)
103 for (
int id=0;
id<16; ++id)
111 this->
channelsNum = config.find(
"channels").asInt32();
118 sensor_start(config);
120 PeriodicThread::start();
124bool CanBusAnalogSensor::readFullScaleAnalog(
int ch)
128 unsigned int canMessages=0;
132 msg.getData()[0]=0x18;
139 bool full_scale_read=
false;
143 unsigned int read_messages = 0;
146 for (
unsigned int i=0; i<read_messages; i++)
149 unsigned int currId=m.getId();
150 if (currId==(0x0200 |
boardId << 4))
152 m.getData()[0]==0x18 &&
155 scaleFactor[ch]=m.getData()[2]<<8 | m.getData()[3];
156 full_scale_read=
true;
160 yarp::os::Time::delay(0.002);
163 while(timeout<32 && full_scale_read==
false);
165 if (full_scale_read==
false)
167 yError(
"Trying to get fullscale data from sensor %d net [%d]: no answer received or message lost (ch:%d)\n",
boardId,
canDeviceNum, ch);
174bool CanBusAnalogSensor::sensor_start(yarp::os::Searchable& analogConfig)
176 yTrace(
"Initializing analog device %s\n", analogConfig.find(
"deviceId").toString().c_str());
178 unsigned int canMessages=0;
181 if (analogConfig.check(
"period"))
183 int period=analogConfig.find(
"period").asInt32();
186 msg.getData()[0]=0x08;
187 msg.getData()[1]=period;
191 yDebug(
"using broadcast period %d on device %s\n", period, analogConfig.find(
"deviceId").toString().c_str());
199 msg.getData()[0]=0x07;
200 msg.getData()[1]=0x00;
212 yDebug(
"Using internal calibration on device %s\n", analogConfig.find(
"deviceId").toString().c_str());
214 for (
int ch=0; ch<6; ch++)
220 b = readFullScaleAnalog(ch);
223 if (attempts>0) yWarning(
"Trying to get fullscale data from sensor: channel recovered (ch:%d)\n", ch);
227 yarp::os::Time::delay(0.020);
231 yError(
"Trying to get fullscale data from sensor: all attempts failed (ch:%d)\n", ch);
250 msg.getData()[0]=0x07;
261 yInfo(
"Using uncalibrated raw data for device %s\n", analogConfig.find(
"deviceId").toString().c_str());
264 msg.getData()[0]=0x07;
265 msg.getData()[1]=0x03;
274bool CanBusAnalogSensor::sensor_stop()
276 unsigned int canMessages=0;
280 msg.getData()[0]=0x07;
281 msg.getData()[1]=0x01;
291 PeriodicThread::stop();
316 return IAnalogSensor::AS_OK;
324 return IAnalogSensor::AS_OK;
361bool CanBusAnalogSensor::decode16(
const unsigned char *msg,
int msg_id,
double *
data)
363 const char groupId=(msg_id & 0x00f);
372 data[k]=(((
unsigned short)(msg[2*k+1]))<<8)+msg[2*k]-0x8000;
384 data[k+3]=(((
unsigned short)(msg[2*k+1]))<<8)+msg[2*k]-0x8000;
399 yWarning(
"Got unexpected class 0x3 msg(s): groupId 0x%x\n", groupId);
410bool CanBusAnalogSensor::decode8(
const unsigned char *msg,
int msg_id,
double *
data)
412 const char groupId=(msg_id & 0x00f);
419 for(
int k=0;k<=6;k++)
425 for(
int k=0;k<=7;k++)
436 yWarning(
"CanBusAnalogSensor got unexpected class 0x3 msg(s): groupId 0x%x\n", groupId);
448 lock_guard<mutex> lck(
mtx);
450 unsigned int canMessages=0;
456 yError()<<
"CanBusAnalogSensor canRead failed\n";
459 double timeNow=Time::now();
460 for (
unsigned int i=0; i<canMessages; i++)
464 unsigned int msgid = msg.getId();
465 unsigned char *buff = msg.getData();
466 unsigned int len = msg.getLen();
467 unsigned int id = (msgid & 0x00f0)>>4;
468 const char type = ((msgid&0x700)>>8);
471 status=IAnalogSensor::AS_OK;
480 case ANALOG_FORMAT_8_BIT:
481 ret=decode8(buff, msgid,
data.data());
482 status=IAnalogSensor::AS_OK;
484 case ANALOG_FORMAT_16_BIT:
487 ret=decode16(buff, msgid,
data.data());
488 status=IAnalogSensor::AS_OK;
492 if ((len==7) && (buff[6] != 0))
494 status=IAnalogSensor::AS_OVF;
498 status=IAnalogSensor::AS_ERROR;
500 ret=decode16(buff, msgid,
data.data());
504 status=IAnalogSensor::AS_ERROR;
514 status=IAnalogSensor::AS_TIMEOUT;
520 yTrace(
"CanBusVirtualAnalogSensor Thread released\n");
const int CAN_DRIVER_BUFFER_SIZE
ICanBufferFactory * pCanBufferFactory
yarp::sig::Vector scaleFactor
AnalogDataFormat dataFormat
virtual int getChannels()
virtual void threadRelease()
unsigned short useCalibration
virtual bool open(yarp::os::Searchable &config)
virtual bool threadInit()
virtual int getState(int ch)
virtual int calibrateChannel(int ch, double v)
virtual int read(yarp::sig::Vector &out)