I am finishing up Unit 2 with the topics quiz and am confused about the structure. The instructions state a subscriber should be created to listen to LaserScan and a publisher to send instructions to cmd_vel to move the robot. Further down it says to put everything in one package with a specified name. I am confused, do publishers and subscribers not need separate packages?
My initial thought was to create the publisher and subscriber as separate packages. Then create a third package that called the subscriber to read, then based on its input call the publisher with a velocity value. However, I am still not sure how to call the publishers and subscribers from a seperate package. Is this something that was taught previously in the course and I have just forgotten?
Let me know if clarification is needed on my questions. Thank you in advance for any help you can provide!
You don’t need to create packages separately for both publisher and subscriber at all. As topic, publishers and subscribers are a node that should run in a package. So for your question regarding topics quiz, LaserScan
message is already published from sensor_msgs
package through /kobuki/laser/scan
topic. That is built-in package inside ROS. So, you need to subscribe to /kobuki/laser/scan
topic and use this scan information in a callback function to do what you want. After you use this scan information in your calculations, you will get a result so that you just need to publish this result (in your case, it is Twist
) through /cmd_vel
topic. That’s it.
All in all, you need to code all these thing inside only a one python file
under the src folder. I think 2nd chapter is self-explanatory and explain everything in an order. You may have missed some important points. Re-start from the very beginning of this chapter. Good luck.
2 Likes
Hi @braytonlarson1,
Just to emphasize what @enderayhan already said, and to add further hints: You need only one package and only one node (C++ source file).
- In your C++ program, define your publisher and subscriber in the
main
function and make sure to call ros::spin()
so that the subscriber will work and the program keeps running.
- It’s beneficial to have the publisher as a global variable so that you can access it from the subscriber callback. This usually means that you just declare the publisher variable outside the
main
function, and you define it in main
but creating and assigning a publisher to it.
- Your robot movement logic is better placed in the callback function, as a reaction to the feedback from the LaserScan. So you only need to publish to the robot from this function.
By the way, welcome to the Community!
1 Like
Thank you for your help. I’ve been working on it intermittently over the past couple days and I finally got it. My C++ is a little rusty and I really should visit the C++ for robotics course at some point. I definitely was confused how a publisher and subscriber could be one node, but it makes much more sense now.
1 Like
That was the key I was missing. The solution defines a couple global variables that are given values by the callback and used in main. I’m not sure which is better programming etiquette, a lot of C++ forums say to avoid defining global variables altogether. But it definitely works and seems to be the simplest and most logical solution. Thank you for your help!
1 Like