Unit 2 Topics Quiz - How to structure the packages?

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.


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!