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
5c7fa63b
Commit
5c7fa63b
authored
4 years ago
by
jhierck laptop
Browse files
Options
Downloads
Patches
Plain Diff
Edited encoder.py to work without global variables
parent
bc509d5e
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/encoder.py
+40
-41
40 additions, 41 deletions
mappingbot/encoder.py
with
40 additions
and
41 deletions
mappingbot/encoder.py
+
40
−
41
View file @
5c7fa63b
...
...
@@ -8,70 +8,69 @@ print(GPIO.VERSION)
class
Encoder
:
def
__init__
(
self
,
name
,
encoder_pin
:
int
):
# Initialize some stuff
global
pulse_count
,
start_time
,
time_passed
,
old_time_passed
,
old_pulse_count
"""
Initialize some stuff
"""
self
.
name
=
name
self
.
encoder_pin
=
encoder_pin
pulse_count
=
0
start_time
=
time
.
time
()
time_passed
=
0
old_time_passed
=
0
old_pulse_count
=
0
self
.
pulse_count
=
0
self
.
start_time
=
time
.
time
()
self
.
time_passed
=
0
self
.
old_time_passed
=
0
self
.
old_pulse_count
=
0
GPIO
.
setup
(
self
.
encoder_pin
,
GPIO
.
IN
,
GPIO
.
PUD_DOWN
)
def
callback_encoder
(
self
,
channel
):
# Interrupt handler, increases pulse_count and updates time
global
pulse_count
,
start_time
,
time_passed
time_passed
=
time
.
time
()
-
start_time
pulse_count
+=
1
"""
Interrupt handler, increases pulse_count and updates time
"""
self
.
time_passed
=
time
.
time
()
-
self
.
start_time
self
.
pulse_count
+=
1
def
get_pulse_count
(
self
)
->
int
:
# return pulse_count
global
pulse_count
return
pulse_count
"""
return pulse_count
"""
return
self
.
pulse_count
def
reset_pulse_count
(
self
):
# reset pulse_count
global
pulse_count
pulse_count
=
0
"""
reset pulse_count
"""
self
.
pulse_count
=
0
def
reset_time
(
self
):
# reset time_passed and start_time
global
time_passed
,
start_time
time_passed
=
0
start_time
=
time
.
time
()
"""
reset time_passed and start_time
"""
self
.
time_passed
=
0
self
.
start_time
=
time
.
time
()
def
get_current_pulse_speed
(
self
)
->
float
:
# Get average speed since last call in pulses/s
global
pulse_count
,
time_passed
,
old_time_passed
,
old_pulse_count
if
(
time_passed
-
old_time_passed
):
speed
=
(
pulse_count
-
old_pulse_count
)
/
(
time_passed
-
old_time_passed
)
"""
Get average speed since last call in pulses/s
"""
if
self
.
time_passed
-
self
.
old_time_passed
!=
0
:
# If the time has changed
speed
=
(
self
.
pulse_count
-
self
.
old_pulse_count
)
/
(
self
.
time_passed
-
self
.
old_time_passed
)
else
:
speed
=
0
old_pulse_count
=
pulse_count
old_time_passed
=
time_passed
self
.
old_pulse_count
=
self
.
pulse_count
self
.
old_time_passed
=
self
.
time_passed
return
speed
def
get_speed
(
self
)
->
float
:
#
Convert pulse speed to cm/s
"""
Convert pulse speed to cm/s
"""
pulse_per_second
=
self
.
get_current_pulse_speed
()
return
pulse_per_second
*
(
6
*
2
*
math
.
pi
)
/
40
def
init_callback
(
self
):
#
Initialize the interrupt
"""
Initialize the interrupt
"""
GPIO
.
add_event_detect
(
self
.
encoder_pin
,
GPIO
.
BOTH
,
callback
=
self
.
callback_encoder
,
bouncetime
=
10
)
# try:
# encoder1 = Encoder(name="encoder1", encoder_pin=4)
# encoder1.init_callback()
# while True:
# print(encoder1.get_pulse_count())
# print(encoder1.get_speed())
# time.sleep(1)
# except KeyboardInterrupt: # Exit by pressing CTRL + C
# GPIO.cleanup()
# print("Measurement stopped by User")
# finally:
# GPIO.cleanup()
if
__name__
==
'
__main__
'
:
try
:
# encoder1 = Encoder(name="encoder1", encoder_pin=4)
encoder1
=
Encoder
(
name
=
'
encoder1
'
,
encoder_pin
=
23
)
encoder2
=
Encoder
(
name
=
'
encoder2
'
,
encoder_pin
=
24
)
encoder1
.
init_callback
()
encoder2
.
init_callback
()
while
True
:
print
(
"
%s pulses: %d
"
%
(
encoder1
.
name
,
encoder1
.
get_pulse_count
()))
print
(
"
%s speed : %0.2f
"
%
(
encoder1
.
name
,
encoder1
.
get_speed
()))
print
(
"
%s pulses: %d
"
%
(
encoder2
.
name
,
encoder2
.
get_pulse_count
()))
print
(
"
%s speed : %0.2f
"
%
(
encoder2
.
name
,
encoder2
.
get_speed
()))
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
# Exit by pressing CTRL + C
print
(
"
Measurement stopped by User
"
)
finally
:
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