-
Notifications
You must be signed in to change notification settings - Fork 0
/
senstest.py
133 lines (83 loc) · 2.93 KB
/
senstest.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import RPi.GPIO as GPIO
from threading import Thread
# Which GPIO's are used [0]=BCM Port Number [1]=BCM Name [2]=Use [3]=Pin
# ----------------------------------------------------------------------
arrgpio = [(17,"GPIO0","Echo",11),(18,"GPIO7","Trig",12)]
arrgpio2 = [(25,"GPIO0","Echo",22),(4,"GPIO7","Trig",7)]
#constants
Pins = {'P0':(11,17),'P1':(12,18),'P2':(13,21),'P3':(15,22),'P4':(16,23),'P5':(18,24),'P6':(22,25),'P7':(7,4),
'CE1':(26,0),'CE0':(24,0),'SCLK':(23,0),'MISO':(21,0),'MOSI':(19,0),'RDX':(10,0),'TDX':(8,0),'SCL':(5,0),'SDA':(3,0)}
# Set GPIO Channels
# -----------------
GPIO.setmode(GPIO.BCM)
GPIO.setup(arrgpio[0][0], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(arrgpio[1][0], GPIO.OUT)
GPIO.output(arrgpio[1][0], False)
GPIO.setup(arrgpio2[0][0], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(arrgpio2[1][0], GPIO.OUT)
GPIO.output(arrgpio2[1][0], False)
# A couple of variables
# ---------------------
# Number of loop iterations before timeout called
# Wait for 2 seconds to allow the ultrasonics to settle (probably not needed)
# ---------------------------------------------------------------------------
print "Waiting for 2 seconds....."
time.sleep(2)
# Go
# --
print "Running...."
# Never ending loop
# -----------------
class sens(object):
def __init__(self,gpio,arr):
self.GPIO = gpio
self.arr = arr
def run(self):
# Trigger high for 0.0001s then low
EXIT = 0 # Infinite loop
decpulsetrigger = 0.0001 # Trigger duration
inttimeout = 2100
self.GPIO.output(self.arr[1][0], True)
time.sleep(decpulsetrigger)
self.GPIO.output(self.arr[1][0], False)
# Wait for echo to go high (or timeout)
intcountdown = inttimeout
while (self.GPIO.input(self.arr[0][0]) == 0 and intcountdown > 0):
intcountdown = intcountdown - 1
# If echo is high
if intcountdown > 0:
# Start timer and init timeout countdown
echostart = time.time()
intcountdown = inttimeout
# Wait for echo to go low (or timeout)
while (self.GPIO.input(self.arr[0][0]) == 1 and intcountdown > 0):
intcountdown = intcountdown - 1
# Stop timer
echoend = time.time()
# Echo duration
echoduration = echoend - echostart
# Display distance
if intcountdown > 0:
intdistance = (echoduration*1000000)/58
print self.arr,
print "Distance = " + str(int(intdistance)) + "cm"
else:
print self.arr,
print "timeout"
# Wait at least .01s before re trig (or in this case .1s)
time.sleep(.1)
s1 = sens(GPIO,arrgpio)
s2 = sens(GPIO,arrgpio2)
while True:
time.sleep(0.5)
s1.run()
time.sleep(0.5)
s2.run()
class carMain(object):
def __init__(self):
self.forwardSens = UsonicSens([Pins['P4'][0]],[Pins['P5'][0]])
#self.leftSens = UsonicSens([Pins['P0'][0]],[Pins['P1'][0]])
#self.rightSens = UsonicSens([Pins['P2'][0]],[Pins['P3'][0]])