so i am brushing up on my ros basics and decided to write the code for topics quiz using oop, but the problem is i keep getting the following error.
.my code is as follows.
#! /usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
class MoveTurtle():
def __init__(self):
self.sub = rospy.Subscriber('/kobuki/laser/scan',LaserScan, self.callback)
self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self.vels = Twist()
self.rate = rospy.Rate(2)
self.lidar_data = LaserScan()
def callback(self, msg):
rospy.loginfo('callback is called')
self.lidar_data = msg
def turtle_mover(self):
while not rospy.is_shutdown():
rospy.wait_for_message('/kobuki/laser/scan', LaserScan, timeout=None)
print(self.lidar_data[359])
if self.lidar_data[359] > 1:
self.vels.linear.x = 0.2
self.vels.angular.z = 0.0
self.pub.publish(self.vels)
rospy.loginfo('exited the loop')
if self.lidar_data[359] < 1:
self.vels.linear.x = 0.2
self.vels.angular.z = 0.5 # left direction
self.pub.publish(self.vels)
if self.lidar_data[0] < 1:
self.vels.linear.x = 0.2
self.vels.angular.z = 0.5
self.pub.publish(self.vels)
if self.lidar_data[719] < 1:
self.vels.linear.x = 0.5
self.vels.angular.z = -0.5 # right direction
self.pub.publish(self.vels)
self.rate.sleep()
if name == ‘main’:
rospy.init_node('topics_quiz_node')
mt = MoveTurtle()
rospy.loginfo('publishing into /cmd_vel topic')
mt.turtle_mover()