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 kitcar_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:

The topics.yaml also defines one 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>:

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

>>> print(self.param.param_name_1)
'param_value_1'
>>> print(self.param.param_name_2)
'param_value_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)
'topic'

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!