Ask Your Question

rospy message_converter outputs zero while in the terminal I got different output for the same topic

asked 2019-11-12 00:45:29 -0500

wallybeam gravatar image

ROS Kinetic Ubuntu 16.04 Python 3.5

I am using diff_drive_controller to publish rwheel_angular_vel_motor and lwheel_angular_vel_motor topics. My goal is to take messages published by those topics and transmit them via tcp/ip.

Currently, I am trying to convert ros msgs into a json file by using following script.

from rospy_message_converter import json_message_converter
from std_msgs.msg import Float32
  while True:
   message = Float32()
   json_str = json_message_converter.convert_ros_message_to_json(message)

When I run it I always see {"data": 0.0} in the output. But when I use:

rostopic echo rwheel_angular_vel_motor

I am able to see the data. I also used rostopic info rwheel_angular_vel_motor command and got the following output:

Type: std_msgs/Float32

Publishers: * /gopigo_controller (http://R2:34603/)

Subscribers: * /gopigo_state_updater (http://R2:45111/)

So anyone knows why I am getting 0 as output?

edit retag flag offensive close merge delete


it seems that you define Float32 msg and convert it without assigning any value. where do you subscribe to the topic rwheel_angular_vel_motor in your code?

mali gravatar image mali  ( 2019-11-12 02:41:31 -0500 )edit

Actually, I am not subscribing to that topic. I am only reading std_msg/FLoat32. I thought since rwheel_angular_vel_motor publishes that message, I don't need to subscribe to that topic or the from std_msgs.msg import Float32 part handles it itself.

wallybeam gravatar image wallybeam  ( 2019-11-12 02:49:32 -0500 )edit

from std_msgs.msg import Float32 imports the definition of the message so that you can use it in your code. To access the data published on any topic, you should subscribe to that topic first. so, you need to write a subscriber to rwheel_angular_vel_motor and then assign the coming data to a variable and then convert the data.

mali gravatar image mali  ( 2019-11-12 03:08:55 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-11-13 00:19:07 -0500

wallybeam gravatar image

Thanks to mali's comment I changed my code. Following script works for me. My topic name is rwheel_angular_vel_motor and it publishes std_msgs/Float32 message. It turns out for converting messages to json package, json_message_converter is not necessary.

import rospy
from std_msgs.msg import Float32
import json

def callback(data):
   rospy.loginfo("I heard %s",
   dict_data = {'rwheel':}
   json_data = json.dumps(str(dict_data), ensure_ascii=False)

def listener():
   rospy.Subscriber("rwheel_angular_vel_motor", Float32, callback)
   # spin() simply keeps python from exiting until this node is stopped

if __name__ == '__main__':
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



Asked: 2019-11-12 00:45:29 -0500

Seen: 106 times

Last updated: Nov 13 '19