How to write the publisher in Python correctly?
Hi! I'm very confused... I failed to follow the instructions from http://library.isr.ist.utl.pt/docs/roswiki/wiki(3a)ROSNodeTutorialPython.html
1 #!/usr/bin/env python
2 # license removed for brevity
3 import rospy
4 from std_msgs.msg import String
5
6 def publisher():
7 pub = rospy.Publisher('chatter', String, queue_size=10)
.........
I have a file name publisher.py and it has
float32 a
float32 b
float32 c
Should I do the following? But it doesn't work. What should I do so it pulls the data from publisher.py?
1 #!/usr/bin/env python
2 # license removed for brevity
3 import rospy
4 from std_msgs.msg import Float32
5
6 def publisher():
7 pub = rospy.Publisher('topicName', Float32, queue_size=10)
Asked by notSoTechnical on 2019-09-26 00:12:34 UTC
Answers
I usually program in C++ when working with ROS. By the way, what you first define as publisher.py
is not a python file, it is a message file and it must be a *.msg file. Once you've written the code of your *.msg file, you must compile it in order to let ROS create the header file for that message.
To use that message, lets imagine you've called your message my_msg, so you must write in python something like this:
import rospy
from your_ros_package_name import my_msg
def publisher():
pub = rospy.Publisher('topicName',my_msg, queue_size=10)
Try with something like this. It could be some python spelling mistake because as I've already told you I am used to work with C++ nodes.
Asked by drodgu on 2019-09-26 02:03:50 UTC
Comments
I thought : "The std_msgs.msg import is so that we can reuse the std_msgs/String message type (a simple string container) for publishing."
Asked by notSoTechnical on 2019-09-26 11:55:47 UTC
If you want to use a String type msgs you can import String from std_msgs.msg. But I understood that you want to use a msg to broadcast 3 float32 numbers. This second way is better because you do not have to filter the String msg once is received.
Asked by drodgu on 2019-09-30 02:04:39 UTC
Comments
Have you gone through the official tutorials of the ROS wiki? The tutorial you link to is pretty out-of-date, it is from 2011. The official tutorials should actually cover/answer all your questions. Once you've checked those and still have issues, please post them here by editing your question...
Asked by mgruhler on 2019-09-26 01:16:04 UTC
@mgruhler Thank you!
Asked by notSoTechnical on 2019-09-26 11:56:15 UTC