Respected Prof,
Why does my code throw the following error and what’s the solution? I tried a lot and went throw some queries in the form to get some clue. I realized others had called the function in the same manner as I did, and they didn’t complain about error. I tried google too but couldn’t get my head around this.
Below is my code:
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
def callback(msg):
decide_direction(msg.ranges)
rospy.init_node('topics_quiz_node', anonymous = True)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
rate = rospy.Rate(2)
move = Twist()
def decide_direction(ranges):
straight = ranges[360]
left = ranges[719]
right = ranges[0]
if straight > 1:
straight_motion()
if (straight < 1) or (right < 1):
turn_left()
if left < 1:
turn_right()
def turn_left():
move.linear.x = 0
move.angular.z = 0.2
pub.publish(move)
def turn_right():
move.linear.x = 0
move.angular.z = -0.2
pub.publish(move)
def straight_motion():
move.linear.x = 0.6
move.angular.z = 0
pub.publish(move)
rospy.spin()