ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Need help in rospy programming

asked 2021-02-17 05:10:40 -0500

loguna gravatar image

updated 2021-02-17 05:19:53 -0500

gvdhoorn gravatar image

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-02-17 06:24:11 -0500

mgruhler gravatar image

The problem lies here

    for x in range (90):    #  --> loop 90 times
     # --> check if range at specific angle is lowest visited so far
     if data.ranges[angle] <  self.laser_scan: 
         self.laser_scan = data.ranges[angle]
     angle = angle + 1  # --> increment angle

     # WITHOUT LEAVING FOR LOOP call stop() or print go
     if self.laser_scan < 1.5:
          stop()
     else:
          print('go')

This means you'd print either stop or go 90 times PER SCAN! The indentation of the last if clause is wrong.

edit flag offensive delete link more

Comments

Thank You. Never realize is my mistake on that part.

loguna gravatar image loguna  ( 2021-02-18 21:37:43 -0500 )edit

great that this helps. Please mark the answer as correct (if it is) by clicking the checkmark next to it. thanks.

mgruhler gravatar image mgruhler  ( 2021-02-19 07:42:08 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-02-17 05:10:40 -0500

Seen: 92 times

Last updated: Feb 17 '21