Hello there, I’ve tested my code multiple times and it works. However, when I submitted it to the auto grader it tells me my code doesn’t subscribe to /kobuki/laser/scan correctly. But if it didn’t subscribed it correctly how come it works as it is intended. You can find my code as below:
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
def callback(msg):
dist = 0.0
for value in msg.ranges[340:380]:
if value > dist and value != float('inf'):
dist = value
print('Maximum distance is: ', dist)
if dist > 1.0:
act.linear.x = 0.10
act.angular.z = 0.0
print('Moving forward', dist)
if dist < 1.0:
act.linear.x = 0.0
act.angular.z = 0.30
print('Turning', dist)
pub.publish(act)
rospy.init_node('topics_quiz_node')
pub = rospy.Publisher('/cmd_vel',Twist,queue_size=1)
sub =rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
act = Twist()
rospy.spin()