I am unable to program properly to use the subscriber and process its data …
Below is the code being used — here unable to use data from Callback function …
#! /usr/bin/env python
import rospy; import random; import time
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
class MoveRobot():
def __init__(self):
self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self.sub = rospy.Subscriber('/scan', LaserScan, self.callback)
def callback(self,msg):
self.laser_current_data = msg
self.laser_current = laser_current_data.ranges
self.move_robot = Twist()
print("Frontal distance %f" % laser_current[360])
print("Right side distance %f" %laser_current[0])
print("Left side distance %f" %laser_current[719])
def move(self):
front = self.laser_current[360]; right = self.laser_current[0]; left = self.laser_current[719]
while right <= 0.3 and right >= 0.2:
move_robot.linear.x =0.2
if right <= 0.22:
move_robot.angular.z = 0.1
if right >= 0.28:
move_robot.angular.z = -0.1
self.pub.publish(move_robot)
if name == ‘main’:
rospy.init_node("robo_node")
robot = MoveRobot()
robot.move()
rospy.spin()