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");
33 correct &= config.check(
"sensorName");
37 yError() <<
"Insufficient parameters to CanBusFtSensor\n";
41 if (!config.check(
"frameName"))
43 yWarning() <<
"frameName not found.. using default value unknown_frame_name";
46 int period=config.find(
"period").asInt32();
47 setPeriod((
double)period/1000.0);
51 prop.put(
"device", config.find(
"canbusDevice").asString().c_str());
52 prop.put(
"physDevice", config.find(
"physDevice").asString().c_str());
53 prop.put(
"canTxTimeout", 500);
54 prop.put(
"canRxTimeout", 500);
57 prop.put(
"canMyAddress", 0);
67 yError() <<
"Error opening PolyDriver check parameters";
73 yError() <<
"Error opening can device not available";
86 this->
boardId = config.find(
"canAddress").asInt32();
89 this->
sensorName = config.find(
"sensorName").asString();
90 this->
frameName = config.check(
"frameName", yarp::os::Value(
"unknown_frame_name")).asString();
91 unsigned int tmpFormat = config.find(
"format").asInt32();
94 else if (tmpFormat == 16)
100 if( config.check(
"diagnostic") && config.find(
"diagnostic").asInt32() == 1)
111 for (
int id=0;
id<16; ++id)
119 this->
channelsNum = config.find(
"channels").asInt32();
126 sensor_start(config);
128 PeriodicThread::start();
132bool CanBusFtSensor::readFullScaleAnalog(
int ch)
136 unsigned int canMessages=0;
140 msg.getData()[0]=0x18;
147 bool full_scale_read=
false;
151 unsigned int read_messages = 0;
154 for (
unsigned int i=0; i<read_messages; i++)
157 unsigned int currId=m.getId();
158 if (currId==(0x0200 |
boardId << 4))
160 m.getData()[0]==0x18 &&
163 scaleFactor[ch]=m.getData()[2]<<8 | m.getData()[3];
164 full_scale_read=
true;
168 yarp::os::Time::delay(0.002);
171 while(timeout<32 && full_scale_read==
false);
173 if (full_scale_read==
false)
175 yError(
"Trying to get fullscale data from sensor %d net [%d]: no answer received or message lost (ch:%d)\n",
boardId,
canDeviceNum, ch);
182bool CanBusFtSensor::sensor_start(yarp::os::Searchable& analogConfig)
184 yTrace(
"Initializing analog device %s\n", analogConfig.find(
"deviceId").toString().c_str());
186 unsigned int canMessages=0;
189 if (analogConfig.check(
"period"))
191 int period=analogConfig.find(
"period").asInt32();
194 msg.getData()[0]=0x08;
195 msg.getData()[1]=period;
199 yDebug(
"using broadcast period %d on device %s\n", period, analogConfig.find(
"deviceId").toString().c_str());
207 msg.getData()[0]=0x07;
208 msg.getData()[1]=0x00;
220 yDebug(
"Using internal calibration on device %s\n", analogConfig.find(
"deviceId").toString().c_str());
222 for (
int ch=0; ch<6; ch++)
228 b = readFullScaleAnalog(ch);
231 if (attempts>0) yWarning(
"Trying to get fullscale data from sensor: channel recovered (ch:%d)\n", ch);
235 yarp::os::Time::delay(0.020);
239 yError(
"Trying to get fullscale data from sensor: all attempts failed (ch:%d)\n", ch);
258 msg.getData()[0]=0x07;
269 yInfo(
"Using uncalibrated raw data for device %s\n", analogConfig.find(
"deviceId").toString().c_str());
272 msg.getData()[0]=0x07;
273 msg.getData()[1]=0x03;
282bool CanBusFtSensor::sensor_stop()
284 unsigned int canMessages=0;
288 msg.getData()[0]=0x07;
289 msg.getData()[1]=0x01;
299 PeriodicThread::stop();
320bool CanBusFtSensor::decode16(
const unsigned char *msg,
int msg_id,
double *
data)
322 const char groupId=(msg_id & 0x00f);
331 data[k]=(((
unsigned short)(msg[2*k+1]))<<8)+msg[2*k]-0x8000;
343 data[k+3]=(((
unsigned short)(msg[2*k+1]))<<8)+msg[2*k]-0x8000;
358 yWarning(
"Got unexpected class 0x3 msg(s): groupId 0x%x\n", groupId);
369bool CanBusFtSensor::decode8(
const unsigned char *msg,
int msg_id,
double *
data)
371 const char groupId=(msg_id & 0x00f);
378 for(
int k=0;k<=6;k++)
384 for(
int k=0;k<=7;k++)
395 yWarning(
"CanBusFtSensor got unexpected class 0x3 msg(s): groupId 0x%x\n", groupId);
407 lock_guard<mutex> lck(
mtx);
409 unsigned int canMessages=0;
415 yError()<<
"CanBusFtSensor canRead failed\n";
418 double timeNow=Time::now();
419 for (
unsigned int i=0; i<canMessages; i++)
423 unsigned int msgid = msg.getId();
424 unsigned char *buff = msg.getData();
425 unsigned int len = msg.getLen();
426 unsigned int id = (msgid & 0x00f0)>>4;
427 const char type = ((msgid&0x700)>>8);
430 status=MAS_status::MAS_OK;
439 case ANALOG_FORMAT_8_BIT:
440 ret=decode8(buff, msgid,
data.data());
441 status=MAS_status::MAS_OK;
443 case ANALOG_FORMAT_16_BIT:
446 ret=decode16(buff, msgid,
data.data());
447 status=MAS_status::MAS_OK;
451 if ((len==7) && (buff[6] != 0))
453 status=MAS_status::MAS_OVF;
457 status=MAS_status::MAS_ERROR;
459 ret=decode16(buff, msgid,
data.data());
463 status=MAS_status::MAS_ERROR;
473 status=MAS_status::MAS_TIMEOUT;
479 yTrace(
"CanBusVirtualAnalogSensor Thread released\n");
490 return yarp::dev::MAS_OK;
507 std::lock_guard<std::mutex> lck(
mtx);
const int CAN_DRIVER_BUFFER_SIZE
const int CAN_DRIVER_BUFFER_SIZE
virtual bool open(yarp::os::Searchable &config)
virtual yarp::dev::MAS_status getSixAxisForceTorqueSensorStatus(size_t sens_index) const override
virtual bool getSixAxisForceTorqueSensorFrameName(size_t sens_index, std::string &frameName) const override
unsigned short useCalibration
virtual size_t getNrOfSixAxisForceTorqueSensors() const override
ICanBufferFactory * pCanBufferFactory
AnalogDataFormat dataFormat
virtual bool getSixAxisForceTorqueSensorName(size_t sens_index, std::string &name) const override
virtual bool threadInit()
yarp::sig::Vector scaleFactor
virtual bool getSixAxisForceTorqueSensorMeasure(size_t sens_index, yarp::sig::Vector &out, double ×tamp) const override
virtual void threadRelease()