IndexError : tuple index out of range
I made a code for autonomous driving of the designated stadium. But when I ran it on ubuntu 18.04, Jetson nano, there was a tuple error. Considering the Python version, i entered the driving code as "python *.py". Can you tell me what's wrong? marked with "" part where there was an error is
class Test():
count=0
corner_flag=0
check=0
check1=0
heading=0
ratio = 0
start = 0
start_angle = 0
def __init__(self):
self.scan_sub=rospy.Subscriber('/scan', LaserScan, self.scan_callback)
self.odom_sub=rospy.Subscriber('/odom', Odometry, self.odom_callback)
self.vel_pub=rospy.Publisher('/cmd_vel',Twist,queue_size=0)
self.twist=Twist()
rospy.init_node('imu_values', anonymous=False)
def scan_callback(self,data):
self.scan=data
def odom_callback(self,odom):
self.odom_pose=odom.pose.pose
def centerline(self):
if Test.count==0:
rospy.sleep(2)
Test.count=1
self.twist=Twist()
self.orientaion_list=[self.odom_pose.orientation.x,
self.odom_pose.orientation.y,
self.odom_pose.orientation.z,
self.odom_pose.orientation.w]
(self.roll,self.pitch,self.yaw)=euler_from_quaternion(self.orientaion_list)
if self.start == 0:
self.start_angle = int(math.degrees(self.yaw))
self.start = 1
self.l_01=round(self.scan.ranges[0],3)
self.l_02=round(self.scan.ranges[1],3)
self.l_03=round(self.scan.ranges[2],3)
self.l_04=round(self.scan.ranges[-1],3)
self.l_05=round(self.scan.ranges[-2],3)
self.l_11=round(self.scan.ranges[90],3)
self.l_12=round(self.scan.ranges[89],3)
self.l_13=round(self.scan.ranges[91],3)
self.l_21=round(self.scan.ranges[-90],3)
self.l_22=round(self.scan.ranges[-89],3)
self.l_23=round(self.scan.ranges[-91],3)
self.m_1=round(self.scan.ranges[179-self.heading],3)
**self.m_2=round(self.scan.ranges[359-self.heading],3)**
self.centroid_l=self.m_1+self.m_2
self.centroid_m=self.m_1+self.m_2+0.4
self.diff=max(self.l_01,self.l_02,self.l_03,self.l_04,self.l_05)
self.diff1=max(self.l_11,self.l_12,self.l_13)
self.diff2=max(self.l_21,self.l_22,self.l_23)
Test.ratio = self.diff1 - self.diff2
if self.diff<0.1:
self.diff=1
# start
self.twist.linear.x=0.1
self.twist.angular.z=0.0
if Test.ratio > -0:
self.twist.linear.x=0.1
self.twist.angular.z=0.02
self.vel_pub.publish(self.twist)
elif Test.ratio < 0:
self.twist.linear.x=0.1
self.twist.angular.z=-0.02
self.vel_pub.publish(self.twist)
else:
self.twist.linear.x=0.1
self.twist.angular.z=0.0
self.vel_pub.publish(self.twist)
print(self.twist.linear.x, self.twist.angular.z)
print(self.diff1,self.diff, self.diff2)
# turnning point
if self.diff <= 0.57:
print(self.twist.linear.x, self.twist.angular.z)
print(self.centroid_l, self.diff, Test.ratio, self.diff <= 0.5)
# right turn
if Test.check == 0:
while 1:
self.orientaion_list=[self.odom_pose.orientation.x,
self.odom_pose.orientation.y,
self.odom_pose.orientation.z,
self.odom_pose.orientation.w]
(self.roll,self.pitch,self.yaw)=euler_from_quaternion(self.orientaion_list)
self.twist=Twist()
self.heading= int(math.degrees(self.yaw)) - self.start_angle
if self.heading > 180:
self.heading = self.heading - 361
elif self.heading < -180:
self.heading = 361 + self.heading
self.heading = abs(self.heading)
if self.heading<170:
self.twist.linear.x=0.1
self.twist.angular.z=-0.4
self.vel_pub.publish(self.twist)
rospy.sleep(0.1)
print(self.twist.linear.x, self.twist.angular.z)
print(self.diff1,self.diff, self.diff2, self.heading,self.m_1)
elif self.heading>=170:
self.twist.linear.x=0.1
self.twist.angular.z=0.0
self.vel_pub.publish(self.twist)
rospy.sleep(0.1)
print(self.twist.linear.x, self.twist.angular.z)
print(self.diff1,self.diff, self.diff2, self.heading)
Test.check = 1
break;
elif Test.check == 1:
while 1:
self.orientaion_list=[self.odom_pose.orientation.x,self.odom_pose.orientation.y,self.odom_pose.orientation.z,self.odom_pose.orientation.w]
(self.roll,self.pitch,self.yaw)=euler_from_quaternion(self.orientaion_list)
self.twist=Twist()
self.heading= int(math.degrees(self.yaw)) - self.start_angle
if self.heading > 180:
self.heading = self.heading - 361
elif self.heading < -180:
self.heading = 361 + self.heading
self.heading = abs(self.heading)
if self.heading>10:
self.twist.linear.x=0.1
self.twist.angular.z=0.4
self.vel_pub.publish(self.twist)
rospy.sleep(0.1)
print(self.twist.linear.x, self.twist.angular.z)
print(self.diff1,self.diff, self.diff2, self.heading)
elif self.heading<=10:
self.twist.linear.x=0.1
self.twist.angular.z=0.0
self.vel_pub.publish(self.twist)
rospy.sleep(0.1)
print(self.twist.linear.x, self.twist.angular.z)
print(self.diff1,self.diff, self.diff2, self.heading)
Test.check = 0
break;
self.vel_pub.publish(self.twist)
rospy.sleep(0.1)
test=Test()
while True :
try :
**test.centerline()**
except KeyboardInterrupt:
exit()
break
Asked by Leonard Cho on 2022-08-29 11:32:01 UTC
Answers
You are subscribing to sensor_msgs/LaserScan data. Below is the message definition of sensor_msgs/LaserScan
:
# Single scan from a planar laser range-finder
#
# If you have another ranging device with different behavior (e.g. a sonar
# array), please find or create a different message, since applications
# will make fairly laser-specific assumptions about this data
Header header # timestamp in the header is the acquisition time of
# the first ray in the scan.
#
# in frame frame_id, angles are measured around
# the positive Z axis (counterclockwise, if Z is up)
# with zero angle being forward along the x axis
float32 angle_min # start angle of the scan [rad]
float32 angle_max # end angle of the scan [rad]
float32 angle_increment # angular distance between measurements [rad]
float32 time_increment # time between measurements [seconds] - if your scanner
# is moving, this will be used in interpolating position
# of 3d points
float32 scan_time # time between scans [seconds]
float32 range_min # minimum range value [m]
float32 range_max # maximum range value [m]
float32[] ranges # range data [m] (Note: values < range_min or > range_max should be discarded)
float32[] intensities # intensity data [device-specific units]. If your
# device does not provide intensities, please leave
# the array empty.
Please note that the ranges
is a list. Now, please see the following snippet from your code:
self.l_04=round(self.scan.ranges[-1],3)
self.l_05=round(self.scan.ranges[-2],3)
self.l_11=round(self.scan.ranges[90],3)
self.l_12=round(self.scan.ranges[89],3)
self.l_13=round(self.scan.ranges[91],3)
self.l_21=round(self.scan.ranges[-90],3)
self.l_22=round(self.scan.ranges[-89],3)
self.l_23=round(self.scan.ranges[-91],3)
self.m_1=round(self.scan.ranges[179-self.heading],3)
**self.m_2=round(self.scan.ranges[359-self.heading],3)**
Please pay attention to the index of the list. The index is mentioned negatively in some places. This is why you are getting "index out of range error".
On a side note, I recommend learning/understanding the basics of Python before getting started with ROS.
Asked by ravijoshi on 2022-08-29 21:11:54 UTC
Comments