Publishing messages at 10ms rate

asked 2019-12-09 17:32:43 -0600

prj1508 gravatar image

Hi,

I am using PCAN-USB Interface to communicate to CAN bus in the vehicle. I am using Ubuntu 16.04, ROS kinetic with Socketcan/socketcan_bridge. I can read CAN signals and also write to it in general. However, I am facing problems when I have to publish @10ms rate which is required for my application. When I publish, it fails to send the message.

My code is such that I am subscribing to a topic in a callback, then building a message of type can_msgs::Frame in the callback. Now, if I publish from within this callback on sent_messages topic, I see that the message is sent successfully to the bus.

However, I have to ensure the message should be sent at 10ms rate and hence I have created a timer in ROS (ros::Timer) and then publishing the message I want (which is a global variable in my class definition) from the timercallback. When I do this, the message is not sent and the console output is as above. Please suggest

edit retag flag offensive close merge delete

Comments

Hi,

Can you provide any code or output. In general, to achieve what you want it is better to publish out of callbacks since you do not have any control over the rate at witch callbacks are called, hence you will not have any control over the rate of your publisher.

Did you try to implement two different nodes: can_reader and can_writer and sync them with a heartbeat?

Weasfas gravatar imageWeasfas ( 2019-12-10 05:09:26 -0600 )edit

@Weasfas, thank you. i have to publish this message at 10ms for an actuation to happen in the vehicle. Publishing within subscribe callback will have no control on the publish rate and hence i am using timercallback only for publish. I guess you meant subscribe callback when you said there is no control over rate. With underlying resources permitting, i guess this timercallback should publish at the asked rate. pls let me know

prj1508 gravatar imageprj1508 ( 2019-12-10 09:35:21 -0600 )edit

@prj1508 As far as I understood your comment, a Timer can do the trick, publishing in the timer callback (AKA timer timeout), this code snipped maybe help you:

ros::Publisher my_pub = nh.advertise<Message>("my_topic", queue);
ros::Timer timer = nh.createTimer(ros::Duration(1/10), timerCallback);
Message modified_message;

...

// timer callback. Publishes modified message on a specified frequency
void timerCallback(const ros::TimerEvent &) {
  my_pub.publish(modified_message);
}

...
Weasfas gravatar imageWeasfas ( 2019-12-10 09:48:45 -0600 )edit