Table of contents
C++ realizes the modification of the background color
Python realizes the modification of the background color
Preparation
Start the ROS Master and the GUI interface of the turtle separately, because the parameters of the parameter server need to be changed in subsequent operations. If roscore is closed and then restarted, the parameter server will return to the initial state.
roscore
rosrun turtlesim turtlesim_node
List all parameters of the parameter server
rosparam list
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam list
/rosdistro
/roslaunch/uris/host_rosmelodic_virtual_machine__33639
/rosversion
/run_id
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
The following is the key name of the parameter of the background color.
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
The following is the key value corresponding to the background color parameter key name.
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_g
86
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_b
255
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_r
69
According to the previous parameter server learning, changing the color belongs to "changing" the parameters of the parameter server, and the method of overwriting is used.
10. ROS programming learning: c++ implementation of parameter management mechanism Learning: Python Implementation of Parameter Management Mechanism
C++ realizes the modification of the background color
wugui_param_beijing.cpp
#include "ros/ros.h"
/*
1.被修改键值的键名:/turtlesim/background_b /turtlesim/background_g /turtlesim/background_r
2.两种方法一种是通过节点句柄,另一种是通过ros::param
*/
int main(int argc, char *argv[])
{
// 防止控制台中文乱码
setlocale(LC_ALL,"");
// 初始化ROS节点
ros::init(argc,argv,"wugui_beijing_set");
// 初始化节点句柄
ros::NodeHandle n;
// 通过节点句柄修改参数服务器参数(第一次覆盖),背景白色
n.setParam("/turtlesim/background_b",255);
n.setParam("/turtlesim/background_g",255);
n.setParam("/turtlesim/background_r",255);
// 通过ros::param修改参数服务器参数(第二次覆盖),背景黑色
ros::param::set("/turtlesim/background_b",0);
ros::param::set("/turtlesim/background_g",0);
ros::param::set("/turtlesim/background_r",0);
return 0;
}
This program first changes the background to white and then changes the background to black.
CMakeList.txt configuration
add_executable(wugui_param_beijing src/wugui_param_beijing.cpp)
add_dependencies(wugui_param_beijing ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(wugui_param_beijing
${catkin_LIBRARIES}
)
compile
catkin_make
Start ROS Master
roscore
Start the executable file node with changed parameters
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ source ./devel/setup.bash
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun wugui_ttest wugui_param_beijing
View revisions
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_b
0
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_g
0
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosparam get /turtlesim/background_r
0
Start the turtle GUI
rosrun turtlesim turtlesim_node
result
question
It is cumbersome to start various nodes manually one by one, and there will be problems if adding the nodes whose background color is modified directly to the launch file, because each node in the launch file has no startup order, so the parameter setting function <param> of launch must be used.
<!-- 启动乌龟GUI和键盘控制节点 -->
<launch>
<!-- 乌龟GUI -->
<node pkg="turtlesim" type="turtlesim_node" name="turtle1" output="screen">
<!-- 给乌龟GUI传参 -->
<param name="background_b" value="255" type="int"/>
<param name="background_g" value="255" type="int"/>
<param name="background_r" value="255" type="int"/>
</node>
<!-- 键盘控制 -->
<node pkg="turtlesim" type="turtle_teleop_key" name="key" output="screen"/>
<!-- 乌龟位姿订阅 -->
<node pkg="wugui_ttest" type="test01_sub_pose_p.py" name="wugui_pose" output="screen"/>
<!-- 加一个乌龟 -->
<node pkg="wugui_ttest" type="wugui_service_client" name="wugui_spawn" output="screen"/>
<!-- 加另一个乌龟 -->
<node pkg="wugui_ttest" type="wugui_service_client_p.py" name="wugui_spawn_p" output="screen"/>
</launch>
main part
<!-- 乌龟GUI -->
<node pkg="turtlesim" type="turtlesim_node" name="turtle1" output="screen">
<!-- 给乌龟GUI传参 -->
<param name="background_b" value="255" type="int"/>
<param name="background_g" value="255" type="int"/>
<param name="background_r" value="255" type="int"/>
</node>
Note that this time the node node </node> has been added to the tail, which is to point <param> to the parameters in turtlesim.
Also pay attention to the error:
RLException: Invalid roslaunch XML syntax: mismatched tag: line ???, column ???
The traceback for the exception was written to the log file
start launch file
roslaunch wugui_ttest wugui_start.launch
Python realizes the modification of the background color
wugui_param_beijing_p.py
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
if __name__ == "__main__":
# 初始化ROS节点
rospy.init_node("wugui_beijing_p")
# 通过rospy.set_param调节参数
rospy.set_param("/turtlesim/background_r",255)
rospy.set_param("/turtlesim/background_g",255)
rospy.set_param("/turtlesim/background_b",255)
Add executable permission
chmod +x *.py
CMakeList.txt configuration
catkin_install_python(PROGRAMS
scripts/wugui_param_beijing_p.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
start sequentially
roscore
rosrun wugui_ttest wugui_param_beijing_p.py
rosrun turtlesim turtlesim_node