Correct usage of socketcan_interface - hex2dec
I've recently started using the socketCAN_interface
component of ros_canopen
to get data from a Kuebler encoder.
My code is relatively straight forward, I receive messages on a particular CAN-ID using the following:
can::CommInterface::FrameListener::Ptr one_frame = driver.createMsgListener(can::MsgHeader(0x101), handleFrame101);
Which calls the following method, writing data to a float which is a global variable:
void handleFrame101(const can::Frame &f){
// handle specific frame
std::string frame = can::tostring(f, true); //Break frame into string to extract data
std::string data = frame.substr(frame.find("#")+1);
//Check if data matches expected data length from frame, length/2 as dlc is hex (2 bytes per char)
if( (data.length()/2) == int(f.dlc) ){
uint64_t output;
std::stringstream ss;
ss << std::hex << data;
ss >> output;
extension = output;
}else{
extension = 0;
}
}
I'm wondering if there's a better way to do this, it seems ridiculous to decode the hex myself when there's an inbuilt function in socketcan_interface
to do so. However, I was never able to get it to take f.data
and compile successfully. Is it an internal only function? I'm not very familiar with C++ so the documentation wasn't clear.
I'm running ubuntu 14.04 and Indigo.