ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

IndexError : tuple index out of range

asked 2022-08-29 11:32:01 -0600

Leonard Cho gravatar image

updated 2022-08-29 21:02:24 -0600

ravijoshi gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-08-29 21:11:54 -0600

ravijoshi gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-08-29 11:32:01 -0600

Seen: 334 times

Last updated: Aug 29 '22