Class attributes not getting called outside the class

asked 2020-05-26 01:45:33 -0500

AzraelA9 gravatar image

I was doing in a project where i have to take values from two subscribers then fulfill some conditions and then publish data. Im using classes to values for the two subscribers. However, whenever In the attribute move_bot i call self.ranges and self.linear_pose which should work as they have been set when the code subscribed to them. However, i have to call the move_bot attribute within the laser_callback to make it work but now it only recognizes the self.ranges and not the self.linear_pose. If i do place the move_bot in the odom_callback the same thing happens but with self.linear_pose. Why are the callbacks not working?

import rospy
import math
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry

class Move(object):
    def __init__(self):
   = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
            self.twist_angle = Twist()

    def laser_callback(self,msg):
            self.ranges = msg.ranges

    def odom_callback(self,msg):
            self.linear_pose = msg 

    def move_bot(self):
        pi = math.pi
        kp = -0.027

        val =[]

        if self.linear_pose.twist.twist.linear.x < 0.001:
           self.twist_angle.linear.x = -0.2
           for i in self.ranges:
               if i <= 0.6:
                    if self.ranges.index(i) > 360:
                        self.twist_angle.angular.z = 1
                    self.twist_angle.angular.z = -1
        elif self.ranges[360] > 1.2:
           self.twist_angle.angular.z = 0
           self.twist_angle.linear.x = 0.5
            for i in self.ranges:
                if i > 1.2:
            angle = self.ranges.index(max(val))

            if self.ranges[angle] > self.ranges[360]:
                self.twist_angle.angular.z = kp * (360 - angle)

if  __name__== '__main__':

        move = Move()
        rate = rospy.Rate(10)
    except rospy.ROSInterruptException:  pass

    while not rospy.is_shutdown():

I get the following error message

Traceback (most recent call last):
  File "/home/user/catkin_ws/src/robot_maze/src/", line 100, in <module>
  File "/home/user/catkin_ws/src/robot_maze/src/", line 45, in move_bot
    if self.ranges[360] > 1.2:
AttributeError: 'Move' object has no attribute 'ranges'
[robot_maze-1] process has died [pid 16344, exit code 1, cmd /home/user/catkin_ws/src/robot_maze/src/ 
__name:=robot_maze __log:=/home/user/.ros/log/e4216cee-9f0e-11ea-9fd8-06b6df32753c/robot_maze-1.log].
log file: /home/user/.ros/log/e4216cee-9f0e-11ea-9fd8-06b6df32753c/robot_maze-1*.log
edit retag flag offensive close merge delete