forked from shihchengyen/auto_nav
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.py
executable file
·42 lines (30 loc) · 845 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
42
#!/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
lr2 = laser_range
lr2[lr2==0] = np.nan
# find index with minimum value
lr2i = np.nanargmin(lr2)
# log the info
rospy.loginfo('Shortest distance is %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