1.使用命令修改参数服务器
单独使用命令启动小乌龟节点,不是用lanuch文件,不启动键盘控制节点
rosrun turtlesim turtlesim_node
使用命令打印参数服务器参数列表
rosparam list
修改小乌龟节点的背景色
rosparam set /turtlesim/background_b 自定义数值
rosparam set /turtlesim/background_g 自定义数值
rosparam set /turtlesim/background_r 自定义数值
修改相关参数后,重启 turtlesim_node 节点,背景色就会发生改变了
2.启动节点时,直接设置参数
rosrun turtlesim turtlesim_node _background_r:=100 _background_g=0 _background_b=0
3.通过launch文件传参
<launch>
<node pkg="turtlesim" type="turtlesim_node" name="set_bg" output="screen">
<!-- launch 传参策略1 -->
<!-- <param name="background_b" value="0" type="int" />
<param name="background_g" value="0" type="int" />
<param name="background_r" value="0" type="int" /> -->
<!-- launch 传参策略2 -->
<rosparam command="load" file="$(find demo03_test_parameter)/cfg/color.yaml" />
</node>
</
4.C++实现修改参数服务器参数
创建编程文件,编写代码,编辑配置文件
/*
注意命名空间的使用。
*/
#include "ros/ros.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"haha");
ros::NodeHandle nh("turtlesim");
//ros::NodeHandle nh;
// ros::param::set("/turtlesim/background_r",0);
// ros::param::set("/turtlesim/background_g",0);
// ros::param::set("/turtlesim/background_b",0);
nh.setParam("background_r",0);
nh.setParam("background_g",0);
nh.setParam("background_b",0);
return 0;
}
add_executable(change_rgb_c src/change_rgb_c.cpp)
target_link_libraries(change_rgb_c
${catkin_LIBRARIES}
)
运行程序查看效果
rosrun control_turtle0 change_rgb_c
rosrun turtlesim turtlesim_node
5.Python实现修改参数服务器参数
新建编程文件,编辑配置文件,添加可执行权限
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
if __name__ == "__main__":
rospy.init_node("hehe")
rospy.set_param("/turtlesim/background_r",255)
rospy.set_param("/turtlesim/background_g",0)
rospy.set_param("/turtlesim/background_b",0)
# rospy.set_param("background_r",0)
# rospy.set_param("background_g",0)
# rospy.set_param("background_b",255) # 调用时,需要传入 __ns:=xxx
catkin_install_python(PROGRAMS
scripts/change_rgb_p.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
运行查看结果
rosrun control_turtle0 change_rgb_p.py
rosrun turtlesim turtlesim_nod
https://mp.csdn.net/mp_download/manage/download/UpDetailed