8. ROS Subscriber

You have already created a publisher that controls the car’s position. In this part, you will learn how to create a subscriber that can be used to interactively change the car’s speed.

8.1. Speed Message

Because we want to set the speed of the car, we need a message that is suitable for transmitting the speed. Messages can be defined in the msg/ folder of a ROS package. In this case, the message we need is already defined in msg/Speed.msg:

# Definition of the Speed message
# - Beginning sphinx -
float32 speed

Additionally, a message needs to be added to the CMakeLists.txt:

add_message_files(
  FILES
  Speed.msg  # Add this line for every message
)
generate_messages(
  DEPENDENCIES
  std_msgs
)

Now, we can import the message type

>>> from simulation_onboarding.msg import Speed

and initialize a subscriber within start() (and unregister in the stop()).

8.2. Create a Subscriber

The publisher was initialized by adding

self.pose_publisher = rospy.Publisher(
    name="/simulation/gazebo/model/dr_drift/set_pose",
    data_class=SetModelPose,
    queue_size=1,
)

to start().

Similarly, you can add a subscriber with

self.speed _subscriber = rospy.Subscriber(
    name="/simulation/onboarding/speed",
    data_class=Speed,
    callback = self.speed_callback  # Function that gets called when a message is sent on the topic
)

The subscriber requires a callback-parameter, that must be a function which takes a message as a parameter; i.e. with the above subscriber definition, you need to define the speed_callback() function:

def speed_callback(self, msg):
    """Receive new speed in message."""
    rospy.loginfo(f"Received speed message: {msg}")

Let’s test if the subscriber receives messages. You can launch your code with:

roslaunch simulation_onboarding onboarding_node.launch

While your node is running, you can send messages to the speed subscriber from a second terminal with

rostopic pub /simulation/onboarding/speed simulation_onboarding/Speed "speed: 5.0"

If everything works as expected, you will see a new line in the terminal the node is running:

[INFO] [1586247506.869336] [/simulation_onboarding/onboarding_node]: Received speed message: speed: 5.0

Tip

Many ROS commands are easy to use with auto-completion. E.g. instead of typing the complete command, you can just type rostopic pub / and hit <TAB> three times.

8.3. Change the Car’s Speed

With the subscriber receiving messages you can now go ahead and start your next task:

Your Task

  • Use a parameter for the subscriber’s topic

  • Update the car’s position as if it was driving with the speed that is received from the subscriber. E.g. if you publish the “speed: 5.0” message, the car should drive with 5 m/s.

After completing the task, don’t forget to commit and push your results!