Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Parking Sensor
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ryan Tresman
Parking Sensor
Commits
e9624b5f
Commit
e9624b5f
authored
7 months ago
by
Ryan Tresman
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
0263cc0e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
parking_sensor.py
+127
-0
127 additions, 0 deletions
parking_sensor.py
with
127 additions
and
0 deletions
parking_sensor.py
0 → 100644
+
127
−
0
View file @
e9624b5f
import
time
import
datetime
import
sys
import
os
import
grovepi
import
math
import
json
import
traceback
import
subprocess
import
multiprocessing
as
mp
from
video_server
import
runVideo
ranger
=
4
buzzer
=
6
alertThreshold
=
100
midThreshold
=
50
warningThreshold
=
20
grovepi
.
pinMode
(
ranger
,
"
INPUT
"
)
grovepi
.
pinMode
(
buzzer
,
"
OUTPUT
"
)
startBuzz
=
None
buzzDelay
=
None
buzzOn
=
False
LCDUpdated
=
datetime
.
datetime
.
now
().
timestamp
()
buzzDuration
=
0.05
#Read data from ultrasonic ranger
def
readDistance
():
try
:
distance
=
grovepi
.
ultrasonicRead
(
ranger
)
return
distance
except
:
return
None
def
setBuzzer
(
buzzOn
):
buzzing
=
grovepi
.
digitalRead
(
buzzer
)
# Read buzzer state, if buzz is needed and not buzzing already, then start buzzing. Turn off if buzz state is not wanted.
if
buzzOn
:
if
not
buzzing
:
grovepi
.
digitalWrite
(
buzzer
,
1
)
buzzing
=
True
else
:
if
buzzing
:
grovepi
.
digitalWrite
(
buzzer
,
0
)
buzzing
=
False
#Wrapper for the buzzer
def
buzzerWrapper
(
distance
):
global
startBuzz
,
buzzDelay
,
buzzOn
,
buzzDuration
#Only start buzzer if in alert threshold
if
distance
and
distance
<=
alertThreshold
:
#Delay is equal to inverse of distance, with a minimum value
delay
=
(
distance
-
warningThreshold
)
/
100
delay
=
max
(
0.03
,
delay
)
#If buzzer has not started, start it and set time.
if
startBuzz
is
None
:
setBuzzer
(
True
)
startBuzz
=
datetime
.
datetime
.
now
().
timestamp
()
#If not in delay, but are buzzing, keep buzzing until buzz duration has elapsed.
elif
not
buzzDelay
and
startBuzz
and
datetime
.
datetime
.
now
().
timestamp
()
-
startBuzz
>
buzzDuration
:
if
distance
>
warningThreshold
:
buzzDelay
=
datetime
.
datetime
.
now
().
timestamp
()
setBuzzer
(
False
)
#If in delay, delay until the pause between delays has elapsed.
elif
buzzDelay
and
datetime
.
datetime
.
now
().
timestamp
()
-
buzzDelay
>
delay
:
buzzDelay
=
None
startBuzz
=
None
else
:
#If not in alert threshold, make sure buzzer is off.
setBuzzer
(
False
)
def
resetSystem
():
#Reset the system to off state
grovepi
.
digitalWrite
(
buzzer
,
0
)
def
videoProcess
():
queue
=
mp
.
Queue
()
return
mp
.
Process
(
target
=
runVideo
,
args
=
(
queue
,)),
queue
def
exceptHandler
(
process
,
queue
):
#Force system quit on exception
if
queue
:
queue
.
put
(
'
terminate
'
)
time
.
sleep
(
1
)
if
process
:
process
.
terminate
()
process
.
join
()
resetSystem
()
exit
()
if
__name__
==
'
__main__
'
:
process
=
None
try
:
#Start the video server
process
,
queue
=
videoProcess
()
process
.
start
()
#Initialise multiprocessing dictionary
manager
=
mp
.
Manager
()
d
=
manager
.
dict
()
d
[
'
warningThreshold
'
]
=
warningThreshold
d
[
'
midThreshold
'
]
=
midThreshold
d
[
'
alertThreshold
'
]
=
alertThreshold
#First read of distance is None
readDistance
()
while
True
:
#Event loop
distance
=
readDistance
()
d
[
'
distance
'
]
=
distance
queue
.
put
(
d
)
buzzerWrapper
(
distance
)
except
KeyboardInterrupt
:
exceptHandler
(
process
,
queue
)
except
Exception
as
e
:
print
(
e
)
exceptHandler
(
process
,
queue
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment