NameError: global name 'clbk_laser' is not defined
I am new to ROS. I tried to create a package which reads laser scan values and avoids robot from obstacles. I created 2 python files but when i tried to run them it gives an error like below:
", line 17, in main
sub = rospy.Subscriber('/m2wr/laser/scan', LaserScan, clbk_laser)
NameError: global name 'clbk_laser' is not defined
Here is my code:
#! /usr/bin/env python2.7
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
pub = None
def main():
global pub
rospy.init_node('reading_laser')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
sub = rospy.Subscriber('/m2wr/laser/scan', LaserScan, clbk_laser)
rospy.spin()
if __name__ == '__main__':
main()
def clbk_laser(msg):
regions = {
'right': min(min(msg.ranges[0:143]), 10),
'fright': min(min(msg.ranges[144:287]), 10),
'front': min(min(msg.ranges[288:431]), 10),
'fleft': min(min(msg.ranges[432:575]), 10),
'left': min(min(msg.ranges[576:713]), 10),
}
take_action(regions)
def take_action(regions):
msg = Twist()
linear_x = 0
angular_z = 0
state_description = ''
if regions['front'] > 1 and regions['fleft'] > 1 and regions['fright'] > 1:
state_description = 'case 1 - nothing'
linear_x = 0.6
angular_z = 0
elif regions['front'] < 1 and regions['fleft'] > 1 and regions['fright'] > 1:
state_description = 'case 2 - front'
linear_x = 0
angular_z = -0.3
elif regions['front'] > 1 and regions['fleft'] > 1 and regions['fright'] < 1:
state_description = 'case 3 - fright'
linear_x = 0
angular_z = -0.3
elif regions['front'] > 1 and regions['fleft'] < 1 and regions['fright'] > 1:
state_description = 'case 4 - fleft'
linear_x = 0
angular_z = 0.3
elif regions['front'] < 1 and regions['fleft'] > 1 and regions['fright'] < 1:
state_description = 'case 5 - front and fright'
linear_x = 0
angular_z = -0.3
elif regions['front'] < 1 and regions['fleft'] < 1 and regions['fright'] > 1:
state_description = 'case 6 - front and fleft'
linear_x = 0
angular_z = 0.3
elif regions['front'] < 1 and regions['fleft'] < 1 and regions['fright'] < 1:
state_description = 'case 7 - front and fleft and fright'
linear_x = 0
angular_z = -0.3
elif regions['front'] > 1 and regions['fleft'] < 1 and regions['fright'] < 1:
state_description = 'case 8 - fleft and fright'
linear_x = 0
angular_z = -0.3
else:
state_description = 'unknown case'
rospy.loginfo(regions)
rospy.loginfo(state_description)
msg.linear.x = linear_x
msg.angular.z = angular_z
pub.publish(msg)