Ros param key error

Hi everyone! I started learn ROS. I try to read data from launch file with param. bu I give an error and I did not understand why? Please help me
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"

Output:

Can you copy and paste the error message you are seeing into the thread? Also, what course are you following?

Hi! I am following school ROS course. Error message is KeyError(“x1”). When I run the launch file directly, I did not see any error.

It looks almost like your class and your main function are from two different examples. You are passing goalx and goaly as parameters to your constructor. Then, you are trying to pull self.goalx and self.goaly from ROS. You really only need to do one or the other. The error here means that ROS does not have your parameters. So rospy.get_param(“x1”) fails. You can fix this sample by changing those lines to be the following

self.goalX = goalx
self.goalY = goaly

Hope this helps.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.