Ask Your Question
0

Publish CAN frame on topic

asked 2015-09-24 09:18:35 -0500

Droter gravatar image

updated 2015-09-25 18:36:55 -0500

Hi,

I am trying to publish CAN data to a ROS topic. I followed the talker tutorial and am using the suggestion on this question: http://answers.ros.org/question/21364...

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include "socketcan_interface/socketcan.h"
#include "socketcan_interface/threading.h"
#include "socketcan_interface/string.h"

void handleFrames(const can::Frame &f){
    // handle all frames
    LOG(can::tostring(f, true)); // lowercase: true
}
void handleFrame123(const can::Frame &f){
    // handle specific frame
    LOG("123? " << can::tostring(f, true)); // lowercase: true
}

int main(int argc, char **argv)
{
    can::ThreadedSocketCANInterface driver;
    if(!driver.init("can0", false)) return 1; // read own messages: false

    can::CommInterface::FrameListener::Ptr all_frames = driver.createMsgListener(handleFrames);
    can::CommInterface::FrameListener::Ptr one_frame = driver.createMsgListener(can::MsgHeader(0xFF32), handleFrame123); // handle only frames with CAN-ID 0x123

    ros::init(argc, argv, "data_raw");
    ros::NodeHandle n;
    ros::Publisher canbus_pub = n.advertise<std_msgs::String>("canbus", 1000);

    while (ros::ok())
    {
      std_msgs::String msg;
      std::stringstream ss;

      ss << all_frames;
      msg.data = ss.str();
      canbus_pub.publish(msg);

      //ROS_INFO("%s", msg.data.c_str());

      //ROS_INFO("%s", std::cout<<"all_frames is of type: "<<typeid(all_frames).name()<<std::endl);

      ros::spinOnce();
    }

  driver.shutdown();
  return 0;
}

This line LOG(can::tostring(f, true) will print the CAN frame to the console like:

00063200#8000010100000000
00061200#0000010100000000
00063200#8000010100000000
00EF0900#0000000000000000
00061200#0000010100000000
00063200#8000010100000000
00061200#0000010100000000
00063200#8000010100000000

This line canbus_pub.publish(msg) prints to a rostopic canbus but no frame data:

---
data: 0x1e9db80
---
data: 0x1e9db80

How do I get the individual CAN frames to print to the topic?

Thanks,

Matt

edit retag flag offensive close merge delete

Comments

This code cannot compile! Please move std_msgs::String msg; into handleFrames. The CAN-IDs in the LOG outputs look strange, too. I have to check can::tostring next week.

Mathias Lüdtke gravatar imageMathias Lüdtke ( 2015-09-25 04:52:34 -0500 )edit

I found this package. Maybe it is a better place to start. https://github.com/juancamilog/canbus...

Haven't got it working yet.

Droter gravatar imageDroter ( 2015-09-25 18:37:42 -0500 )edit

Any good news about this? Please advice me how to us it...

khairulizwan gravatar imagekhairulizwan ( 2015-11-13 01:53:20 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2015-10-04 11:52:37 -0500

Droter gravatar image

This interface works well. I haven't tried sending but it will publish CAN data to a topic.

https://github.com/juancamilog/canbus...

edit flag offensive delete link more

Comments

Any good news about this? Please advice me how to us it...

khairulizwan gravatar imagekhairulizwan ( 2015-11-13 01:53:29 -0500 )edit

This package has been updated to include a new CanFrame2 msg that includes a bool for extended. https://github.com/jeremy-a/canbus_in...

Droter gravatar imageDroter ( 2016-04-26 12:41:06 -0500 )edit

I am a newbie. I installed the package,but I am sure it been installed correctly. I used rosrun to find the command, but i can not find any command about the packet. Any command to prove the packet is installed correctly?

yaovincent gravatar imageyaovincent ( 2016-06-28 04:27:27 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-09-24 09:18:35 -0500

Seen: 1,506 times

Last updated: Oct 04 '15