Hello everyone, I am a lovely fish. The fish-incense ROS website is almost finished, and it will be ready to meet you soon

In this lesson, learn how to use Python to change the timer cycle into a parameter that can be changed dynamically.

1. Code writing

Open vscode, open li4.py, and focus on the following line of code

class Li4Node(Node)
Copy the code

Li4Node is derived from rclpy.node. node. Therefore, Li Si acquired the ability of parameter correlation:

The name of the function describe
declare_parameter Declare and initialize a parameter
declare_parameters Declare and initialize a bunch of parameters
get_parameter Gets a parameter by parameter name
get_parameters Get multiple parameters by multiple parameter names
set_parameters Sets the values of a set of parameters
More function Docs.ros2.org/latest/api/…

To complete the modification of li Si writing speed parameters, we need to do two steps, declare parameters and obtain parameters

1.1 Declaration Parameters

Modify the __init__ function by adding the following line of code at the end

Declare_parameter ("write_timer_period",5)Copy the code

1.2 Obtaining and Setting Parameters

Here we do this in the timer callback, updating each time we finish writing the novel

Modify the timer_callback function by adding two lines of code at the bottom

Timer_period = self.get_parameter('write_timer_period').get_parameter_value().integer_value # Update callback period self.timer.timer_period_ns = timer_period * (1000*1000*1000)Copy the code

The complete code is as follows:

def timer_callback(self): MSG = String() MSG. Data = '%d' % (self.i,self.i) self.write.publish(MSG) # self.get_logger().info() "%s"' % msg.data) # For us to see self. I += 1 # section number +1 # update callback period after callback timer_period = Self.get_parameter ('write_timer_period').get_parameter_value().integer_value # Update callback period self.timer_period_ns = timer_period * (1000*1000*1000)Copy the code

Just to explain a little bit, there are two new lines of code one that takes the current value of a parameter by its name and converts it into an integer

Another line is used to update the period of the callback (by modifying the timer_period_ns member variable of the timer)

2. The test

2.1 Compiler

colcon build --packages-select village_li
Copy the code

2.2 Running the Li Node

ros2 run village_li  li4_node
Copy the code

2.3 Using the ROS2 param Command Line Test

2.3.1 Viewing the Parameter List

ros2 param list
Copy the code

2.3.2 Viewing description

ros2 param describe /wang2 novel_price
Copy the code

2.3.3 Obtaining Parameter Values

ros2 param get /wang2 novel_price
Copy the code

2.3.4 Set parameter values to increase the price

This is set to 1, so that Li Si can write a chapter in one second.

ros2 param set li4 write_timer_period 1
Copy the code

Ros2 Topic Hz

Tropical_drink: < tropical_drink >

ros2 topic hz /sexy_girl
Copy the code

This command allows us to see how often the topic is posted.

Compare the speed before setting and after setting, it is significantly faster.

We set the period as 5, why is the rate 0.200 here? Later we changed the period to 1, why does the rate keep increasing?

3. Summary

By manipulating parameters in Python in this section, you’ve learned how to use parameters in Python nodes. The next little fish section takes you through using parameters in C++ nodes.

References

[1] the Node – rclpy 0.6.1 documentation (ros2.org) : docs.ros2.org/latest/api/…