ROS PARAM KEYERROR

发布于 2025-01-31 13:45:53 字数 1206 浏览 2 评论 0原文

我开始学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-02-07 13:45:53

由于参数命名间,您正在遇到此问题。使用rospy.get_param(“ x1”)您正在检查有关相对名称空间的拉参数。这意味着您实际上是在&lt; node_namespace&gt;/x1上检查一个参数。但是,在您的节点中,您不会声明您的节点下的参数;这意味着它是在全局名称空间下进行的。您有两个解决此问题的选择。首先,您可以简单地声明节点的名称空间中的参数,例如:

<node pkg="stage_ros" name="stageros" type="stageros" args="$(find beginner_tutorials)/world/worldfile.world" output="screen">
    <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/>

其次,您可以在节点内部修复命名区,以便从这样的全局名称空间中汲取参数:

def__init__(self,nodeName,goalx,goaly):
  rospy.init_node("node", anonymous="True")
  self.goalX = rospy.get_param("/x1")
  self.goalY = rospy.get_param("/y1")

有关命名空间的更多信息,您可以检查[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:

<node pkg="stage_ros" name="stageros" type="stageros" args="$(find beginner_tutorials)/world/worldfile.world" output="screen">
    <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/>

Secondly, you could fix the namespacing inside your node so that it pulls params from the global namespace like this:

def__init__(self,nodeName,goalx,goaly):
  rospy.init_node("node", anonymous="True")
  self.goalX = rospy.get_param("/x1")
  self.goalY = rospy.get_param("/y1")

For more information regarding namespaces you can check the [ros wiki page].1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文