Add files via upload

main
ahmadlogs 2021-05-11 15:55:27 +05:00 zatwierdzone przez GitHub
rodzic e8d9612503
commit 877c4fa6b2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 71 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,26 @@
from servo import Servo
import utime
servo_pwm_freq = 50
min_u16_duty = 1000 - 400 # offset for correction
max_u16_duty = 6000 # offset for correction
min_angle = 0
max_angle = 180
current_angle = 0.001
pwmPin=15 #change for your pin
motor=Servo(pwmPin)
while True:
motor.move(0)
utime.sleep(1)
motor.move(45)
utime.sleep(1)
motor.move(90)
utime.sleep(1)
motor.move(135)
utime.sleep(1)
motor.move(180)
utime.sleep(1)

45
servo/servo.py 100644
Wyświetl plik

@ -0,0 +1,45 @@
from machine import Pin, PWM
class Servo:
# these defaults work for the standard TowerPro SG90
__servo_pwm_freq = 50
__min_u16_duty = 600 # offset for correction
__max_u16_duty = 6000 # 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)