How to program for using Laser Data

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

in move() function, laser_current is not recognised. so how to program???
kindly direct me to a solution. thanks & regards.

Need to define laser_current with a default value in the python class constructor

1 Like

It’s a quiz so we can’t provide a solution. The only thing we can do is give you some hints and suggestions. As jm.carb pointed out laser_current is not defined properly. If you have notes look at how to write and use a callback for the laser subscriber if don’t have notes go back to the chapter on Subscribers and look at how their written you can also just do a search on writing python subscribers. Your close but your callback is not written correctly. If you don’t have notes starting with the next course their are several tools available for all operating systems that help you organize notes. Even a simple text editor is better than nothing.