ROS PARAM KEYERROR
我开始学习ROS。我尝试使用param读取启动文件的数据。但这给了我一个我不明白的错误。请帮我。这是构造函数:
def__init__(self,nodeName,goalx,goaly):
rospy.init_node("node", anonymous="True")
self.goalX = rospy.get_param("x1")
self.goalY = rospy.get_param("y1")
self.pub = rospy.Publisher(nodeName + '/cmd_vel',Twist,queue_size=10)
self.sub = rospy.Subscriber(nodeName+"/odom",Odometry,callback=self.update_pose)
self.odometry = Odometry()
self.rate = rospy.Rate(10)
if __name__ == "__main__":
nodeId = sys.argv[1]
try:
nodeName = "robot_"+nodeId
x = Turtlebot(nodeName,0,0)
x.move2goal()
rospy.spin()
...
启动文件:
<param name="x1" type="int" value="4" />
<param name="y1" type="int" value="0" />
<param name="x2" type="int" value="5" />
<param name="y2" type="int" value="0" />
<node pkg="stage_ros" name="stageros" type="stageros" args="$(find beginner_tutorials)/world/worldfile.world" output="screen"/>
<!-- <node pkg="beginner_tutorials" name="rotate1" args="0" type="projecttask2.py"/> -->
<node pkg="beginner_tutorials" name="rotate2" args="1" type="projecttask2.py"
Error: KeyError('x1')I started learn ROS. I try to read data from launch file with param. but it gave me an error which I did not understand. Please help me. Here is the constructor:
def__init__(self,nodeName,goalx,goaly):
rospy.init_node("node", anonymous="True")
self.goalX = rospy.get_param("x1")
self.goalY = rospy.get_param("y1")
self.pub = rospy.Publisher(nodeName + '/cmd_vel',Twist,queue_size=10)
self.sub = rospy.Subscriber(nodeName+"/odom",Odometry,callback=self.update_pose)
self.odometry = Odometry()
self.rate = rospy.Rate(10)
if __name__ == "__main__":
nodeId = sys.argv[1]
try:
nodeName = "robot_"+nodeId
x = Turtlebot(nodeName,0,0)
x.move2goal()
rospy.spin()
...
Launch File:
<param name="x1" type="int" value="4" />
<param name="y1" type="int" value="0" />
<param name="x2" type="int" value="5" />
<param name="y2" type="int" value="0" />
<node pkg="stage_ros" name="stageros" type="stageros" args="$(find beginner_tutorials)/world/worldfile.world" output="screen"/>
<!-- <node pkg="beginner_tutorials" name="rotate1" args="0" type="projecttask2.py"/> -->
<node pkg="beginner_tutorials" name="rotate2" args="1" type="projecttask2.py"
Error: KeyError('x1')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于参数命名间,您正在遇到此问题。使用
rospy.get_param(“ x1”)
您正在检查有关相对名称空间的拉参数。这意味着您实际上是在&lt; node_namespace&gt;/x1
上检查一个参数。但是,在您的节点中,您不会声明您的节点下的参数;这意味着它是在全局名称空间下进行的。您有两个解决此问题的选择。首先,您可以简单地声明节点的名称空间中的参数,例如:其次,您可以在节点内部修复命名区,以便从这样的全局名称空间中汲取参数:
有关命名空间的更多信息,您可以检查[ROS Wiki Page] ]。 1
You're running into this issue because of parameter namespacing. When using
rospy.get_param("x1")
you're checking pulling the param with respect to the relative namespace. This means you're actually checking for a param at<node_namespace>/x1
. However in your node you're not declaring the param under your node; this means it's setup under the global namespace. You have two options to fix this. First, you can simply declare the params within your node's namespace like so:Secondly, you could fix the namespacing inside your node so that it pulls params from the global namespace like this:
For more information regarding namespaces you can check the [ros wiki page].1