ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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",data.data)
   dict_data = {'rwheel': data.data}
   json_data = json.dumps(str(dict_data), ensure_ascii=False)
   print(json_data)


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

if __name__ == '__main__':
  listener();