Need help in rospy programming
hi,
I wish to program my robot such that whenever the Lidar detect an obstacle, a 'stop' will be printed every 1 second. However, whenever I place remove the obstacle, the output will not stop printing 'stop'.
Can someone kindly help me in this programming?
Below is my code:
#!/usr/bin/env python
from __future__ import print_function
import sys
import rospy
from sensor_msgs.msg import ,LaserScan
import os
def stop():
print('stop')
time.sleep(1)
class obstacle_detector:
def __init__(self):
self.laser_scan_sub = rospy.Subscriber("/scan_raw",LaserScan,self.lasercallback)
def lasercallback(self,data):
angle = 285
self.laser_scan = 10000000
for x in range (90):
if data.ranges[angle] < self.laser_scan:
self.laser_scan = data.ranges[angle]
angle = angle + 1
if self.laser_scan < 1.5:
stop()
else:
print('go')
def main(args):
ic = obstacle_detector()
rospy.init_node('obstacle_detector', anonymous=True)
try:
rospy.spin()