List index out of range
Hi, I am new to python and ros and i am trying to get to different goals defined by me but I get the error: list imdex out of range and i dont know why because as far as i know, the list index is not out of range. my code is below and also the error:
#!/usr/bin/env python
import rospy
import math
from std_msgs.msg import String
from std_msgs.msg import Int32
from geometry_msgs.msg import Twist # Import the Twist message from the geometry_msgs
from sensor_msgs.msg import LaserScan # Import the LaserScan message from the sensor
from gazebo_msgs.msg import ModelStates #import ModelState message from gazebo_msgs
from geometry_msgs.msg import Pose,Point # Import the Pose message from the geometry_msgs
from tf.transformations import euler_from_quaternion
from goal_publisher.msg import PointArray
from math import pow, atan2, sqrt
PI= 3.1415926535897
repeat = 0
theta = 0.0
x = 0.0
y = 0.0
msg = 0.0
index_same=0.0
target = 90
kp=0.5
class MovetoGoals:
def __init__(self):
# Creates a node with name 'final_projekt_node' and make sure it is a
# unique node (using anonymous=True).
rospy.init_node('final_projekt_node', anonymous=True)
# Publisher which will publish to the topic 'Twist'.
self.pub = rospy.Publisher('cmd_vel',Twist, queue_size=10)
# A subscriber to the topic 'Modelstate'. self.update_pose is called
# when a message of type Pose is received.
self.pose_sub = rospy.Subscriber('/gazebo/model_states',ModelStates, self.update_pose)
self.goals_sub = rospy.Subscriber('/goals',PointArray,self.get_goals)
self.dist_sub = rospy.Subscriber('/distance',Int32,self.distance_tracker)
self.laser_sub = rospy.Subscriber('scan', LaserScan,self.detect_obstacle)# subscriber to subscribe to laser messages
self.pose = ModelStates()
self.current_distance = Int32()
self.the_goals = PointArray()
self.rate = rospy.Rate(10)
def update_pose(self, data):
"""Callback function which is called when a new message of type Pose is
received by the subscriber."""
#print('in update_pose function')
self.pose = data
self.pose.pose[1].position.x = round(self.pose.pose[1].position.x, 4)
self.pose.pose[1].position.y = round(self.pose.pose[1].position.y, 4)
#print ('robots x position' self.pose.pose[1].position.x)
def euclidean_distance(self,goal_pose):
return sqrt(pow((goal_pose.x - self.pose.pose[1].position.x), 2) +
pow((goal_pose.y - self.pose.pose[1].position.y), 2))
def linear_vel(self,goal_pose, constant=1):
#return constant * (self.euclidean_distance(goal_pose))
return abs(1.0)
def steering_angle(self, goal_pose):
return atan2(goal_pose.y - self.pose.pose[1].position.y,goal_pose.x - self.pose.pose[1].position.x)
def angular_vel(self,goal_pose, constant=2):
global theta
self.pose.pose[1].orientation = self.pose.pose[1].orientation
(roll, pitch, theta) = euler_from_quaternion([self.pose.pose[1].orientation.x, self.pose.pose[1].orientation.y, self.pose.pose[1].orientation.z, self.pose.pose[1].orientation.w])
#print ('theta at update pose is', + theta)
#print ('theta is', + theta)
return constant * (self.steering_angle(goal_pose) - theta)
#return 0.3
def get_goals(self,the_goals):
global x
global y
x = the_goals.goals[0].x
y = the_goals.goals[0].y
#print x
#print y
def distance_tracker(self,distance):
self.current_distance ...
Please make sure to use the preformatted text (
101010
) button when posting code or terminal output. It makes your question much easier to read when you use itWhat topic are you showing exactly? Can you please update your question with the output of
rostopic echo /gazebo/model_states
with just one message of the topic printed? Something doesn't look right because when I do it, i get
notice the
-
in there which is YAML syntax for a list.Thank you for edting my question and the tips on posting a code here. I have solved the problem with the answer given below.
If that answered your question, then please click the to the left of the answer to mark it as correct.