-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparking.py
More file actions
135 lines (109 loc) · 3.53 KB
/
parking.py
File metadata and controls
135 lines (109 loc) · 3.53 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
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
133
134
135
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import thread
import time
import requests
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.PWM as PWM
# Configuration czujnika odleglosci
GPIO.setup("P9_12", GPIO.OUT, initial=GPIO.LOW) # Trigger
GPIO.setup("P9_15", GPIO.IN, pull_up_down=GPIO.PUD_UP) # Echo
# SERVO config
servoPin = "P9_14"
PWM.start(servoPin, 5, 50) # NOTE lub bez tej piatki
# led config
greenLed = "P8_10"
redLed = "P8_9"
GPIO.setup("P8_10", GPIO.OUT) # green
GPIO.setup("P8_9", GPIO.OUT) # red
# note: 1 - wolny/odblokowany , 2 - zajety samochod, 3 - zarezerowwany
parking = [
{"id": 1,
"type": 0}
]
def checkPlace():
while(1):
r = requests.get("http://aieozn.pl/sw/isOccupied/1")
print(r.text)
time.sleep(2)
if(r.text == '2' and parking[0]['type'] != 2):
print('blokada')
closeServo()
ledOn(redLed)
ledOff(greenLed)
parking[0]['type'] = int(r.text)
else:
if(parking[0]['type'] == 2 and r.text != '2'):
openServo()
ledOn(greenLed)
ledOff(redLed)
print('unblock')
parking[0]['type'] = int(r.text)
def distanceMeasurement(TRIG, ECHO):
# Measure the distance between HC-SR04 and nearest wall or solid object.
GPIO.output(TRIG, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(TRIG, GPIO.LOW)
pulseStart = time.time()
while GPIO.input(ECHO) == 0:
pulseStart = time.time()
while GPIO.input(ECHO) == 1:
pulseEnd = time.time()
pulseDuration = pulseEnd - pulseStart
distance = pulseDuration * 17150
distance = round(distance, 2)
return distance
def openServo():
dutyCycle = 12
PWM.set_duty_cycle(servoPin, dutyCycle)
def closeServo():
dutyCycle = 3
PWM.set_duty_cycle(servoPin, dutyCycle)
def ledOn(ledPin):
GPIO.output(ledPin, GPIO.HIGH)
def ledOff(ledPin):
GPIO.output(ledPin, GPIO.LOW)
def measure_average():
d1 = distanceMeasurement("P9_12", "P9_15")
d2 = distanceMeasurement("P9_12", "P9_15")
d3 = distanceMeasurement("P9_12", "P9_15")
d4 = distanceMeasurement("P9_12", "P9_15")
distance = (d1 + d2 + d3 + d4) / 4
return distance
# main Loop
if __name__ == '__main__':
# _thread.start_new_thread(serverStart,())
ledOn(greenLed)
ledOff(redLed)
# thread.start_new_thread( checkPlace, ())
while(1):
r = requests.get("http://aieozn.pl/sw/isOccupied/1")
if (r.text != '2'):
if (parking[0]['type'] == 2):
openServo()
ledOn(greenLed)
ledOff(redLed)
print('unblock')
parking[0]['type'] = int(r.text)
else:
recoveredDistance = measure_average()
print('measure distance')
if (recoveredDistance < 10):
ledOff(greenLed)
ledOn(redLed)
parking[0]['type'] = 1
r = requests.get("http://aieozn.pl/sw/take/1")
time.sleep(10)
else:
ledOff(redLed)
ledOn(greenLed)
parking[0]['type'] = 0
r = requests.get("http://aieozn.pl/sw/leave/1")
else:
if (parking[0]['type'] != 2):
print('blokada')
closeServo()
ledOn(redLed)
ledOff(greenLed)
parking[0]['type'] = int(r.text)
time.sleep(5)