ROS Basics: Understanding ROS topics: Publisher and subscriber: Project

Hello again guys. I am facing an issue in running a code to turn the robot before it collides with wall. My python script if pointing the following errors:

Traceback (most recent call last):
  File "velocity_publisher.py", line 36, in <module>
    walk = MoveRobot()
  File "velocity_publisher.py", line 12, in __init__
    self.laser_data = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
NameError: global name 'callback' is not defined

I know this is an programming mistake. However if you could help me. I am not the best programmer…I don know what is wrong in the code and what I can do in order to solve it. My code is below:

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class MoveRobot:

    def __init__(self):
        self.laser = LaserScan()
        self.move=  Twist()
        self.laser_data = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)

    def callback(msg):
        self.laser_data
        print msg

    def avoid_the_wall(self):
        laser_right = self.laser.ranges[0]
        laser_left = self.laser.ranges[719]
        laser_front = self.laser.ranges[360]
        while msg < inf and laser_front > 1:
            print("Don't Stop Moving")
            return self.move.linear.x == 0.5
        if self.laser_right < 1:
            print("Turn counterclockwise")
            return self.move.angular.z == -0.5
        elif self.laser_left <1:
            print("Turn clockwise")
            return self.move.angular.z == 0.5

rospy.init_node('topics_quiz_node')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rate = rospy.Rate(2)

walk = MoveRobot()
walk.avoid_the_wall()

rospy.spin()

while not rospy.is_shutdown():
    pub.publish(walk.avoid_the_wall)
    rate.sleep()

Thanks in advance

First of all the callback when inside of a class, needs to have the self varable as input. And second you need to call it with the self.whatever:

self.laser_data = rospy.Subscriber(‘/kobuki/laser/scan’, LaserScan, self.callback)

def callback(self, msg):
self.laser_data
print msg

Hope this helps

1 Like