在这里我将简单介绍参数的获取,参数的设置,参数的修改和参数的查询
1.参数的获取
使用rospy.get_param(‘param_name‘)
获取全局参数
rospy.get_param(‘/global_param_name')
获取目前命名空间的参数
rospy.get_param('param_name')
获取私有命名空间的参数
rospy.get_param('~private_param_name')
获取参数,如果没有,使用默认参数
rospy.get_param('foo','delete_value')
2.设置参数
rospy.set_param(para_name,param_value);
rospy.set_param('truth','true');
rospy.set_param('some_numbers','1.0,2.0,3.0');
rospy.set_param('~private_bar',1+2);
3.删除参数
rospy.delete_param(‘param_name’)
rospy.delete_param(‘to_delete’)
4.参数的查询
rospy.has_param(‘param_name’)
if rospy.has_param(‘to_delete’):
rospy.delete_param(‘to_delete’)
5.解释参数名
参数的名称可以映射成不同的名
节点可以放到不同的命名空间
下面是获取参数的实际名称和映射的名称
value=rospy.get_param(‘~foo‘)
rospy.loginfo("parameter %s has value %s ",rospy.resolve_name('~foo'),value)
6.搜索参数
如果你不知道某个参数,你可以从私有空间到向上到全局空间进行搜索,
rospy.search_param(‘param_name‘)
full_param_name=rospy.search_param('foo')
param_value=rospy.get_param(full_param_name)
获取参数名后就可以进行其他操作
7.通过launch设置参数
roscd begnner_tutorials /bringup
touch param_talker.launch
roscd beginner_tutorials param_talker.launch
<param name="utterance" value="hello word"/>
<param name="to_delete" value="delete me"/>
<param name="P" value="1.0"/>
<param name="I" value="2.0"/>
<param name="D" value="3.0"/>
<param name="topic_name" value="chatter"/>
制作节点
roscd beginner_tutorials/scripts
touch param_talker.py
chmod +x param_talker.py
roscd beginner_tutorials param_talker.py
python节点代码
#!/usr/nim/env python
import rospy
from std_msgs.msg import String
def param_talker():
rospy.init_node("param_talker")
global_example=rospy.get_param("/global_example")
rospy.loginfo(" %s is %s ",rospy.resolve_name('/global_example'),global_example)
utterances=rospy.get_param('utterances')
rospy.loginfo(" %s is %s ",rospy.resolve_name('utterances'),utterance)
topic_name=rospy.get_param('~topic_name')
rospy.loginfo(" %s is %s ",rospy.resolve_name('~topic_name'),topic_name)
default_param=rospy.get_param('default_param' ,'default_name')
rospy.loginfo(" %s is %s ",rospy.resolve_name('default_param'),default_param)
gains=rospy.get_param('gains')
p,i,d=gains['P'],gains['I'],gains['D']
rospy.loginfo(" gains are %s, %s,%s ",p,i,d)
pub=rospy.publish(topic_name,String,queue_size=10 )while not rospy.is_shutdown(0:
pub.publish(utterances)
rospy.loginfo(utterances)
rospy.sleep(1)
if __name__==__main__:
try:
param_talker()
except rospy.ROSinterruptException:pass