-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathROS_sonar_sensor.py
More file actions
66 lines (56 loc) · 1.7 KB
/
ROS_sonar_sensor.py
File metadata and controls
66 lines (56 loc) · 1.7 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import RPi.GPIO as gpio
import time
import sys
import signal
import rospy
from std_msgs.msg import Float32
def signal_handler(signal, frame): # ctrl + c -> exit program
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class sonar():
def __init__(self):
rospy.init_node('sonar', anonymous=True)
self.distance_publisher = rospy.Publisher('/sonar_dist',Float32, queue_size=1)
self.r = rospy.Rate(15)
def dist_sendor(self,dist):
data = Float32()
data.data=dist
self.distance_publisher.publish(data)
gpio.setmode(gpio.BCM)
trig = 27 # 7th
echo = 17 # 6th
gpio.setup(trig, gpio.OUT)
gpio.setup(echo, gpio.IN)
sensor=sonar()
time.sleep(0.5)
print ('-----------------------------------------------------------------sonar start')
try :
while True :
gpio.output(trig, False)
time.sleep(0.1)
gpio.output(trig, True)
time.sleep(0.00001)
gpio.output(trig, False)
while gpio.input(echo) == 0 :
pulse_start = time.time()
while gpio.input(echo) == 1 :
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17000
if pulse_duration >=0.01746:
#print('time out')
continue
elif distance > 300 or distance==0:
#print('out of range')
continue
distance = round(distance, 3)
#print ('Distance : %f cm'%distance)
sensor.dist_sendor(distance)
sensor.r.sleep()
except (KeyboardInterrupt, SystemExit):
gpio.cleanup()
sys.exit(0)
except:
gpio.cleanup()