Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
solenoid_driver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Nikhil Lal
solenoid_driver
Commits
c8b0b2d0
Commit
c8b0b2d0
authored
10 months ago
by
nikhil b lal
Browse files
Options
Downloads
Patches
Plain Diff
simplified code
parent
49efeeae
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
code/main.py
+22
-29
22 additions, 29 deletions
code/main.py
with
22 additions
and
29 deletions
code/main.py
+
22
−
29
View file @
c8b0b2d0
from
machine
import
Pin
,
PWM
,
Timer
import
time
from
time
import
sleep
,
sleep_us
# constants
spike_duration
=
3.5
# spike duration, ms
pulse_duration
=
3
00
# dwell (including spike time), ms
total_cycle
=
2
000
# period of the cycle, ms
pulse_duration
=
5
00
# dwell (including spike time), ms
total_cycle
=
3
000
# period of the cycle, ms
# establish pins
led_pin
=
Pin
(
27
,
Pin
.
OUT
)
...
...
@@ -14,10 +14,8 @@ sol_pin = Pin(29, Pin.OUT)
timer
=
Timer
()
# timer object
# initiate variables
i
=
1
# counter increment, ms
counter
=
-
i
# counter, ms
duty
=
0
# duty cycle, %
duty_percent
=
7
duty_percent
=
15
hold_duty
=
round
(
duty_percent
/
100
*
65535
)
# reduced duty cycle, %
# pwm object, off
...
...
@@ -25,36 +23,31 @@ pwm_led = PWM(led_pin, freq=10000, duty_u16=duty)
pwm_sol
=
PWM
(
sol_pin
,
freq
=
100000
,
duty_u16
=
duty
)
def
pwm_control
(
timer
):
global
counter
,
duty
counter
+=
i
if
counter
<
spike_duration
:
# spike pulse, 100% duty cycle
duty
=
65535
elif
counter
<
pulse_duration
:
# hold, X% duty cycle
duty
=
hold_duty
elif
counter
<
total_cycle
:
# 0% duty cycle
duty
=
0
else
:
# counter rest
counter
=
-
i
;
print
(
"
reset counter
"
)
pwm_led
.
duty_u16
(
duty
)
# set the duty cycle of the led
pwm_sol
.
duty_u16
(
duty
)
# set the duty cycle of the solenoid
# on cycle
pwm_led
.
duty_u16
(
65535
)
# set the duty cycle of the led
pwm_sol
.
duty_u16
(
65535
)
# set the duty cycle of the solenoid
sleep_us
(
int
(
spike_duration
*
1000
))
# pwm cycle
pwm_led
.
duty_u16
(
hold_duty
)
# set the duty cycle of the led
pwm_sol
.
duty_u16
(
hold_duty
)
# set the duty cycle of the solenoid
sleep_us
(
int
((
pulse_duration
-
spike_duration
)
*
1000
))
# off cycle
pwm_led
.
duty_u16
(
0
)
# set the duty cycle of the led
pwm_sol
.
duty_u16
(
0
)
# set the duty cycle of the solenoid
print
(
"
initiate runtime
"
)
try
:
# timer initialize
timer
.
init
(
freq
=
(
1
/
i
)
*
1e3
,
mode
=
Timer
.
PERIODIC
,
callback
=
pwm_control
)
timer
.
init
(
period
=
total_cycle
,
mode
=
Timer
.
PERIODIC
,
callback
=
pwm_control
)
# allocate for extra application functionality
while
True
:
pass
sleep
(
10
)
except
KeyboardInterrupt
:
# catch a soft reboot or keyboard interrupt
...
...
@@ -91,4 +84,4 @@ finally:
print
(
"
purge complete
"
)
\ No newline at end of file
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