繁体   English   中英

Class 属性未在 ROS 中的 class 之外调用

[英]Class attributes not getting called outside the class in ROS

我在一个项目中做,我必须从两个订阅者那里获取值,然后满足一些条件,然后发布数据。 我使用类来为两个订阅者设置值。 但是,每当在属性 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

指出@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM