1 of 15

Publishers and Subscribers

October 16, 2023

2 of 15

Icebreaker

  • Your name
  • W
  • TODO - we couldn’t think of a good icebreaker so we are gonna make Kevin come up with one on the spot

3 of 15

Recap

4 of 15

Brief recap of last meeting

5 of 15

Making a Publisher

  • We've learned how to interact with topics on the command line
  • Let's learn how to do that with code!
  • We'll be roughly following along with this tutorial:

6 of 15

Making a Publisher: Setup

  • Run these commands within 2023RobotCode outside of Docker:

git pull -r

git checkout tutorials_2023

  • Go into Docker and run these three commands:

cd ~/2023RobotCode/zebROS_ws/src/beginner_tutorials

natbuild beginner_tutorials ← new! (and important)

code .

  • Now, you should have VS Code open in a directory called beginner_tutorials

7 of 15

Making a Publisher

  • We've added the sample ZebraData.msg file to the beginner_tutorials package
    • Type natbuild beginner_tutorials inside Docker to build the .msg file
  • Now, let's create a Python program to read all of the message fields using input() and publish a message
  • To be able to run our program:
    • Add #!/usr/bin/env python3 to the top of the program
    • Run the command chmod +x [filepath to program]
    • These changes make the program executable

8 of 15

Running our Code

  • To run our node, let's type rosrun beginner_tutorials publisher.py
  • Question: what rostopic command would we use to view the messages being published?

9 of 15

Viewing Output

  • We use the rostopic echo command to view messages published on topics

10 of 15

Making a Subscriber

  • Now, let's create a Python program that prints out all the messages published by the publisher, along with how many messages it’s received
  • To be able to run our program:
    • Anyone remember?

    • Add #!/usr/bin/env python3 to the top of the program
    • Run the command chmod +x [path to program]
    • These changes make the program executable

11 of 15

Running the Subscriber

  • In a terminal inside Docker:
    • rosrun beginner_tutorials publisher.py
  • In a different terminal inside Docker:
    • rosstd
    • rosrun beginner_tutorials subscriber.py
    • Type messages away!

12 of 15

Adding new messages

13 of 15

Group activity

  • Find 3-4 people near you
  • Create a .msg file, publisher, and subscriber together

  • Connect to the robot radio
  • Inside Docker, type rosjet
  • Now you can all talk to each other!
    • Have different people run the publisher and the subscriber
    • Everyone can rostopic echo [your_topic_name]

14 of 15

Interacting on the Network

  • New command: rosnode
  • While connected to the radio, rosnode list
  • Try rosnode info [node name] and see what info you can find
  • Can you figure out how to kill (stop) your friend’s nodes remotely?

15 of 15

Additional references