目录
准备工作
c++实现对背景色的修改
python实现对背景色的修改
分开启动ROS Master和乌龟GUI界面,因为之后的操作要改动参数服务器的参数,如果roscore关闭再重启,参数服务器会回到初始状态。
roscore
rosrun turtlesim turtlesim_node
列出参数服务器所有参数
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
如下为背景色的参数的键名。
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
如下是背景色参数键名对应的键值。
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
根据之前的参数服务器学习,改颜色属于对参数服务器的参数“改”,利用的是覆盖的方法。
10.ROS编程学习:参数管理机制c++实现_机械专业的计算机小白的博客-CSDN博客https://blog.csdn.net/wzfafabga/article/details/127413670?spm=1001.2014.3001.550111.ROS编程学习:参数管理机制python实现_机械专业的计算机小白的博客-CSDN博客https://blog.csdn.net/wzfafabga/article/details/127432682?spm=1001.2014.3001.5501
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;
}
这个程序首先将背景改为白色,然后又将背景改为黑色。
CMakeList.txt配置
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}
)
编译
catkin_make
启动ROS Master
roscore
启动改参数的可执行文件节点
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ source ./devel/setup.bash
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun wugui_ttest wugui_param_beijing
查看修改情况
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
启动乌龟GUI
rosrun turtlesim turtlesim_node
结果
问题
依次手动启动各种节点很麻烦,而且将修改背景颜色的节点直接加入launch文件会出现问题,因为launch文件中的各个节点没有启动顺序,故要利用到launch的参数设置功能。
主要部分
注意的是这次的node节点加入了尾,这是为了把指向turtlesim中的参数。
还有注意报错:
RLException: Invalid roslaunch XML syntax: mismatched tag: line ???, column ???
The traceback for the exception was written to the log file
ROS改错:launch文件中标签的结束符号问题_机械专业的计算机小白的博客-CSDN博客https://blog.csdn.net/wzfafabga/article/details/127514851?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127514851%22%2C%22source%22%3A%22wzfafabga%22%7D
启动launch文件
roslaunch wugui_ttest wugui_start.launch
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)
添加可执行权限
chmod +x *.py
CMakeList.txt配置
catkin_install_python(PROGRAMS
scripts/wugui_param_beijing_p.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
依次启动
roscore
rosrun wugui_ttest wugui_param_beijing_p.py
rosrun turtlesim turtlesim_node