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.init_node('robot_maze')
            rospy.Subscriber('/kobuki/laser/scan',LaserScan,self.laser_callback)
            rospy.Subscriber('/odom',Odometry,self.odom_callback)
            self.pub = 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
               else:
                    self.twist_angle.angular.z = -1
               break
        elif self.ranges[360] > 1.2:
           self.twist_angle.angular.z = 0
           self.twist_angle.linear.x = 0.5
        else:
            for i in self.ranges:
                if i > 1.2:
                   val.append(i)    
            angle = self.ranges.index(max(val))
            print(max(val))


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

        self.pub.publish(self.twist_angle)




if  __name__== '__main__':

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

    while not rospy.is_shutdown():
         rate.sleep()

I get the following error message

Traceback (most recent call last):
  File "/home/user/catkin_ws/src/robot_maze/src/robot_maze.py", line 100, in <module>
    move.move_bot()
  File "/home/user/catkin_ws/src/robot_maze/src/robot_maze.py", 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/robot_maze.py 
__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