ROS Fundamentals (Fri Sep 6, lecture 3) previous next
Introducing ROS Fundamentals

Homework due for today

Legend: : Participation (pass/fail) | : PDF | : Team | : Attachment

  1. Reading: PRR - Chapters 3, 4 (Follow along with (3: Topics, 4: Services). Once you’ve read and done the examples on your own computer, please answer these warmup questions. Deliverable: Your response to these questions
    • List the differences between an topic and a service. Which one is in your opinion the most important difference?
    • Come up with your own original example of when you would use a service, and why you would not use a topic for that example.
    • Topics, Topic Names, Messages: What are they, what are the differences between them?
    • What one or two things are you still totally confused about in this reading? If its all clear, then what are your biggest takeaways from this reading?
    • Deliverable: Submit warmup questions in Latte as a pdf.
  2. Do this homework assignment: Basic ROS Play Homework. Deliverable: See the instructions in the homework writeup.

TurtleBot3 From Robotis

  • Small 3 wheeled Robot
  • Many manufactururs
  • Ours come from Robotis
  • See Robotis Turtlebot3
  • We assemble them
  • Cost around $500
  • Two powered wheels, and one caster wheel
  • Powered wheels
    • Dynamixel motors
    • “Smart” motors
  • OpenCR board
    • Arduino compatible
    • Will accept Arduino add-on boards
    • Motor controller
    • Pre-programmed
    • Includes
      • Gyroscope 3Axis
      • Accelerometer 3Axis
      • Magnetometer 3Axis (MPU9250)
  • [Raspberry Pi SBC)
    • Single Board Computer
    • Full Linux!
    • Connects to the OpenCR

Chapter 3

PRR Chapter 3: Topics

  • Nodes talk to each other with Topics
  • A node can “publish” a topic
  • Another node can “subscribe” to a topic
    • The two nodes don’t know about each other
    • A node can subscribe to a topic that no one is publishing
    • Two nodes can publish the same topic with no one subscribing
    • etc. etc. etc.
  • What is being published and subscribed?
    • messages
    • A message is a data structure with named and typed fields
    • A collection of messages already exists, e.g. Int32, which is message with one element, a 32 bit integer.

Some details

  • Queues
    • When you define your node as a Publisher you need to supply a queue_size
    • Messages that are published are kept in the queue until all subscribers have received them
    • Therefore if you publish at a rate faster than all the subscribers are processing, you will loose topics
    • A good starting queue_size is 10
  • Latched Topics
    • New subscribers to a latched topic automatically get the last published message for that topic
    • Useful for topics which change rarely
  • Shebang line.
    • Ignore why its called shebang.
    • pronounced shuh-bang
    • It’s an old joke.
    • Here’s what it looks like:
#!/usr/bin/env python
  • To make a text file executable on linux you do these things:
    • Put shebang line as line 1. It tells what the language of that text file
    • Change the file mode to have “executable priviliges”
    • chmod +x filename
    • Execute it from shell $ ./filename

Useful references to look at

Publishing a topic

  • You will find all the book’s examples in the prrexamples github package
  • Use the ones in our github because they have been tested with TB3
  • If it’s not there yet, clone it in your ~/catkin_ws/src directory
  • See ~/catkin_ws/src/prrexamples/scripts/topic_publisher.py
  • Remember:
    • to do the necessary code gen and compilation you must run catkin_make (with the alias cm)
    • to run roscore in another terminal
  • Run it and examine that it is publishing as expected, as follows:
$ roscd prrexamples
$ chmod +x scripts/topic_publisher.py
$ rosrun prrexamples topic_publisher.py
$ rostopic list
$ rostopic echo counter -n 5

Subscribing to a topic

  • See $ ~/catkin_ws/src/prrexamples/scripts/topic_subscriber.py
  • It defines and runs a node whch subscribes to the counter, and prints it
# shell commands
$ chmod +x topic_subscriber.py
$ roscd prrexamples
$ rosrun prrexamples topic_subscriber

Examine what’s going on

  • Make sure you can see that topics are being publsihed (see rostopic)
  • Make sure you can see that topics are being subscribed (see console output)
  • Try running rqt_graph to see the node charts

Custom Message Types

  • First look if any of the standard message types can do the job for you
  • Here’s a List of standard message type
  • The procedure for defining a new message is easy but convoluted
  • This is because you define your message in a .msg file with a particular format
  • And then catkin_make uses that to generate appropriate C++ headers and Python libraries
Steps to create a new message type (see Creating a msg)
  1. Define the message file, foo.msg, and place it in the msg/ subdirectory of the package
  2. Update (if needed) the package.xml file to include two new commands to indicate dependencies ()
  3. Update your python program to import the messages
  4. Update CMakeLists.txt

Chapter 4

PRR Chapter 4: Services

What is a service?

  • Very related to a topic
  • Instead of “subscribing” and “publishing”
  • Services are a classic “client/server”
  • Node “N” asks a question of Service “S”
  • Service “S” responds immediately with a result
  • Services are good for questions that have immediate answers
  • They are synchronous whereas topics are asynchronous
  • Another way to think of a service is as a “Remote Procedure Call”

Defining a Service

Defining the inputs and outputs
  • Analogous to defining the “method signature” of the service
  • Very reminiscent of how Topic messages were defined
  • You will find this example in the prrexamples github package
Example: WordCount.srv
  • WordCount.srv shows that it will take one input (string) and give one output (uint32)
  • Notice some hairy tweaks of both CMakeLists.txt and package.xml
  • Once done run catkin make ($ cm) to process and do the code generation
  • You will immediately be able to see the service declared with $ rossrv show WordCount
Defining the logic
  • It looks a lot like a node subscribing to a topic
  • Big difference is that it has a ‘return’ statement
  • In this example, it returns an integer equal to the number of words
  • The money quote: return len(request.words.split())
  • Notice the two ways of returning the result
  • Everything else is plubming
Checking if it worked
  • Run the service with a standard rosrun command
  • Remember our package is called prrexamples
  • Remember that you must have $ roscore running!
  • rosrun prrexamples service_server.py
  • You can tell that it worked:
    1. It shows up in $ rosservice list
    2. It shows up in $ rosservice info word_count
A client Node
  • It’s a normal node (nothing magic about it)
  • It includes code to send a request to the service (ask it a question)
  • The code is more, but the effect is of a function call
  • Impressive because it is inter-process and even inter-host - i.e. can be over the internet

Next Class