forked from shihchengyen/auto_nav
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.py
executable file
·41 lines (29 loc) · 846 Bytes
/
scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
import rospy
import numpy as np
from sensor_msgs.msg import LaserScan
def callback(msg):
# create numpy array
laser_range = np.array(msg.ranges)
# replace 0's with nan
laser_range[laser_range==0] = np.nan
# find index with minimum value
lr2i = np.nanargmin(laser_range)
# log the info
rospy.loginfo('Shortest distance at %i degrees', lr2i)
def scanner():
# initialize node
rospy.init_node('scanner', anonymous=True)
# set the update rate to 1 Hz
rate = rospy.Rate(1) # 1 Hz
# subscribe to LaserScan data
rospy.Subscriber('scan', LaserScan, callback)
# wait until it is time to run again
rate.sleep()
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
try:
scanner()
except rospy.ROSInterruptException:
pass