7. ROS Parameters

In the previous part of the Onboarding, you made the car drive on the road using a publisher and steer().

However, you still have many hard-coded constants within your code. E.g. if you want to change the update rate with which steer() gets called or change the publisher’s topic, you will need to modify your source code. This makes your code less flexible and harder to understand. As you’ve seen in onboarding_ros_package, ROS can load parameters from a parameter file; making them accessible in your node. When subclassing from simulation.utils.ros_base.NodeBase, you can access the parameters inside the node with

>>> self.param.{NAME_OF_PARAMETER}  

Our nodes usually have two parameter files:

  • default.yaml: Define general parameters

  • topics.yaml: Define topics used by subscribers and publishers

Parameter files are yaml files. See https://learn.getgrav.org/16/advanced/yaml for a quick introduction into the yaml-syntax.

The parameter files are located in path/to/ros_package/param/<NAME_OF_NODE>.

Let’s take a look at the ROS Node’s parameter files and how they are included when launching the node.

The default.yaml currently only defines two parameters; param_name_1 and param_name_2:

param_name_1: param_value_1
param_name_2: param_value_2

The topics.yaml also defines one topic:

topic_name: /simulation/onboarding/topic

After defining the parameters, they must also be included in the launch file. In the node’s launch file, the parameters are loaded using <rosparam>:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Launch script for onboarding node -->
  <!-- Start a node with the <node> tag. The `pkg` and `type` attribute specify which script should be called to start the node. Here, the script is located at simulation_onboarding/scripts/simulation_onboarding -->
  <node name="onboarding_node" ns="simulation_onboarding" pkg="simulation_onboarding" type="onboarding_node" output="screen">
    <!-- Load parameters from default.yaml with <rosparam>.-->
    <rosparam file="$(find simulation_onboarding)/param/onboarding/default.yaml" command="load"/>
    <!-- Load parameters from topics.yaml with <rosparam>. In this case, the parameters are loaded into the namespace `topics`. Namespaces allow to load a number of parameter files without conflicts. The parameters can then be accessed within the node. -->
    <rosparam file="$(find simulation_onboarding)/param/onboarding/topics.yaml" command="load" ns="topics" />

Inside the node, you can now access the parameters with:

>>> print(self.param.param_name_1)
>>> print(self.param.param_name_2)

When taking a closer look, you can see that topics are loaded with ns=”topics”. Whenever

<rosparam ... ns="<NAMESPACE>" />

is provided, parameters from that file are accessible through:

>>> self.param.NAMESPACE...  

Therefore, the topic can be accessed with:

>>> print(self.param.topics.topic_name)  

7.1. Replace Constants with Parameters

Your Task

  • Create and use a rate-parameter for the update frequency of the steer function.

  • Add a topic parameter set_pose for the publisher’s topic.

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