Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CPS_project_group13
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
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
Service Desk
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
Hierck, J.J. (Jelle, Student M-BME,M-EMSYS)
CPS_project_group13
Commits
3108263b
Commit
3108263b
authored
4 years ago
by
jhierck laptop
Browse files
Options
Downloads
Patches
Plain Diff
Added ultrasound sensor script
parent
596e5ea5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mappingbot/ultrasound.py
+64
-0
64 additions, 0 deletions
mappingbot/ultrasound.py
with
64 additions
and
0 deletions
mappingbot/ultrasound.py
0 → 100644
+
64
−
0
View file @
3108263b
# Libraries
import
RPi.GPIO
as
GPIO
import
time
# GPIO Mode (BOARD / BCM)
GPIO
.
setmode
(
GPIO
.
BCM
)
print
(
GPIO
.
VERSION
)
class
UltrasoundSensor
:
SONIC_SPEED
=
343
# Speed of sound in m/s
def
__init__
(
self
,
name
,
trigger_pin
:
int
,
echo_pin
:
int
):
self
.
name
=
name
self
.
trigger_pin
=
trigger_pin
self
.
echo_pin
=
echo_pin
# Setup GPIO pins
GPIO
.
setup
(
self
.
trigger_pin
,
GPIO
.
OUT
)
GPIO
.
setup
(
self
.
echo_pin
,
GPIO
.
IN
)
def
get_distance
(
self
)
->
float
:
GPIO
.
output
(
self
.
trigger_pin
,
True
)
# Fire ultrasound pulse
# Store initial time
pulse_sent
=
time
.
time
()
pulse_received
=
time
.
time
()
time
.
sleep
(
0.00001
)
# Wait for a very small time
GPIO
.
output
(
self
.
trigger_pin
,
False
)
# Stop firing the pulse
while
GPIO
.
input
(
self
.
echo_pin
)
==
0
:
# Record the exact time the pulse was sent
pulse_sent
=
time
.
time
()
while
GPIO
.
input
(
self
.
echo_pin
)
==
1
:
# Record the exact time the pulse was received
pulse_received
=
time
.
time
()
time_elapsed
=
pulse_received
-
pulse_sent
# time difference between sending and receiving pulse
return
(
time_elapsed
*
self
.
SONIC_SPEED
)
/
2
# We divide by two because of the reflection of the wave
def
print_distance
(
self
,
unit
=
'
cm
'
)
->
None
:
if
unit
==
'
m
'
:
dist
=
self
.
get_distance
()
print
(
"
%12s measured %0.3f m
"
%
(
self
.
name
,
dist
))
else
:
# Unit is cm or not recognised
dist
=
self
.
get_distance
()
*
100.0
print
(
"
%12s measured %0.1f cm
"
%
(
self
.
name
,
dist
))
if
__name__
==
'
__main__
'
:
try
:
sensor1
=
UltrasoundSensor
(
name
=
"
sensor1
"
,
trigger_pin
=
17
,
echo_pin
=
16
)
sensor2
=
UltrasoundSensor
(
name
=
"
sensor2
"
,
trigger_pin
=
27
,
echo_pin
=
20
)
sensor3
=
UltrasoundSensor
(
name
=
"
sensor3
"
,
trigger_pin
=
22
,
echo_pin
=
21
)
while
True
:
sensor1
.
print_distance
(
unit
=
'
cm
'
)
sensor2
.
print_distance
(
unit
=
'
cm
'
)
sensor3
.
print_distance
(
unit
=
'
cm
'
)
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
# Exit by pressing CTRL + C
print
(
"
Measurement stopped by User
"
)
finally
:
# Always clean up the pins
GPIO
.
cleanup
()
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