类属性未在ROS中的类外部调用

问题描述 投票:0回答:1

我当时在一个项目中,我必须从两个订阅者那里获取价值,然后满足一些条件,然后发布数据。我正在使用类为两个订户的值。但是,无论何时在属性move_bot中,我都调用self.ranges和self.linear_pose,它们应按代码订阅时设置的那样工作。但是,我必须在laser_callback中调用move_bot属性以使其工作,但现在它只能识别self.ranges而不是self.linear_pose。如果我确实将move_bot放置在odom_callback中,则发生相同的事情,但带有self.linear_pose。为什么回调不起作用?

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()

我收到以下错误消息

  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
python class ros
1个回答
0
投票

指出@a_guest在评论中说的是:

class Move(object):
  def __init__(self):
      ...
      self.ranges = None
      self.linear_pose = None
  ...
  def move_bot(self):
      if self.ranges is not None and self.linear_pose is not None:
          if self.linear_pose.twist.twist.linear.x < 0.001:
              ...
          elif self.ranges[360] > 1.2:
              ...
          else:
              ...
          self.pub.publish(self.twist_angle) # Don't publish until received initial values
© www.soinside.com 2019 - 2024. All rights reserved.