Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Simple Servo PWM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Qiyang Sun
Simple Servo PWM
Commits
3052982d
Verified
Commit
3052982d
authored
8 months ago
by
Qiyang Sun
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.py
+38
-0
38 additions, 0 deletions
main.py
servo.py
+45
-0
45 additions, 0 deletions
servo.py
with
83 additions
and
0 deletions
main.py
0 → 100644
+
38
−
0
View file @
3052982d
from
machine
import
UART
,
Pin
from
servo
import
Servo
import
time
uart
=
machine
.
UART
(
0
,
baudrate
=
9600
,
bits
=
8
,
parity
=
None
,
stop
=
1
,
tx
=
machine
.
Pin
(
0
),
rx
=
machine
.
Pin
(
1
))
sg90_servo
=
Servo
(
pin
=
12
)
is_new_gate
=
False
sg90_servo
.
move
(
90
)
def
next_char
():
while
True
:
#if uart.any():
data
=
uart
.
read
(
1
)
if
data
:
try
:
c
=
data
.
decode
(
'
utf-8
'
)
return
c
except
UnicodeError
as
e
:
print
(
e
)
while
True
:
char
=
next_char
()
print
(
"
char =
"
,
char
)
if
char
==
'
?
'
:
continue
deg
=
-
1.0
if
'
a
'
<=
char
<=
'
z
'
:
deg
=
(
ord
(
char
)
-
ord
(
'
a
'
))
/
25
*
180
if
'
A
'
<=
char
<=
'
Z
'
:
deg
=
(
ord
(
char
)
-
ord
(
'
A
'
))
/
25
*
180
#assert abs(deg + 1) > 0.0001
#sg90_servo.move(deg)
if
abs
(
deg
+
1
)
>
0.0001
:
sg90_servo
.
move
(
deg
)
This diff is collapsed.
Click to expand it.
servo.py
0 → 100644
+
45
−
0
View file @
3052982d
from
machine
import
Pin
,
PWM
class
Servo
:
__servo_pwm_freq
=
50
__min_u16_duty
=
1640
-
2
# offset for correction
__max_u16_duty
=
7864
-
0
# offset for correction
min_angle
=
0
max_angle
=
180
current_angle
=
0.001
def
__init__
(
self
,
pin
):
self
.
__initialise
(
pin
)
def
update_settings
(
self
,
servo_pwm_freq
,
min_u16_duty
,
max_u16_duty
,
min_angle
,
max_angle
,
pin
):
self
.
__servo_pwm_freq
=
servo_pwm_freq
self
.
__min_u16_duty
=
min_u16_duty
self
.
__max_u16_duty
=
max_u16_duty
self
.
min_angle
=
min_angle
self
.
max_angle
=
max_angle
self
.
__initialise
(
pin
)
def
move
(
self
,
angle
):
# round to 2 decimal places, so we have a chance of reducing unwanted servo adjustments
angle
=
round
(
angle
,
2
)
# do we need to move?
if
angle
==
self
.
current_angle
:
return
self
.
current_angle
=
angle
# calculate the new duty cycle and move the motor
duty_u16
=
self
.
__angle_to_u16_duty
(
angle
)
self
.
__motor
.
duty_u16
(
duty_u16
)
def
__angle_to_u16_duty
(
self
,
angle
):
return
int
((
angle
-
self
.
min_angle
)
*
self
.
__angle_conversion_factor
)
+
self
.
__min_u16_duty
def
__initialise
(
self
,
pin
):
self
.
current_angle
=
-
0.001
self
.
__angle_conversion_factor
=
(
self
.
__max_u16_duty
-
self
.
__min_u16_duty
)
/
(
self
.
max_angle
-
self
.
min_angle
)
self
.
__motor
=
PWM
(
Pin
(
pin
))
self
.
__motor
.
freq
(
self
.
__servo_pwm_freq
)
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