New to micropython trying do animate Ironman mask for son #19807
Samiamjohnson1
started this conversation in
General
Replies: 2 comments
|
The following function (as in your second code sample) moves both servos to the same angle: def set_servo_angle(angle):
"""
Set servo angle (0-180 degrees).
Converts angle to duty cycle for PWM.
"""
if not 0 <= angle <= 180:
raise ValueError("Angle must be between 0 and 180")
# Convert angle to duty cycle (ESP32/ESP8266: duty_u16 range 0-65535)
min_us = 500 # pulse width for 0 degrees
max_us = 2500 # pulse width for 180 degrees
us = min_us + (max_us - min_us) * angle / 180
duty = int(us / 20000 * 65535) # 20ms period for 50Hz
servo1.duty_u16(duty)
servo2.duty_u16(duty)To move them in opposition you need to calculate two duty cycles: def set_servo_angle(angle):
"""
Set servo angle (0-180 degrees).
Converts angle to duty cycle for PWM.
"""
if not 0 <= angle <= 180:
raise ValueError("Angle must be between 0 and 180")
# Convert angle to duty cycle (ESP32/ESP8266: duty_u16 range 0-65535)
min_us = 500 # pulse width for 0 degrees
max_us = 2500 # pulse width for 180 degrees
us1 = min_us + (max_us - min_us) * angle / 180
duty1 = int(us1 / 20000 * 65535) # 20ms period for 50Hz
us2 = min_us + (max_us - min_us) * (180 - angle) / 180
duty2 = int(us2 / 20000 * 65535) # 20ms period for 50Hz
servo1.duty_u16(duty1)
servo2.duty_u16(duty2) |
0 replies
|
Based on @peterhinch's example: Code is not tested. import time
from machine import Pin, Signal, PWM
SERVO1_PIN = 11
SERVO2_PIN = 14
BUTTON_PIN = 2
FREQ = 50
class Servo:
def __init__(signal_pin, frequency = FREQ):
self.pwm = PWM(Pin(signal_pin), freq=frequency, duty_u16=0)
def __call__(angle):
"""
Set servo angle (0-180 degrees).
Converts angle to duty cycle for PWM.
"""
if not 0 <= angle <= 180:
raise ValueError("Angle must be between 0 and 180")
min_us = 500 # pulse width for 0 degrees
max_us = 2500 # pulse width for 180 degrees
us = min_us + (max_us - min_us) * angle / 180
duty = int(us / 20000 * 65535) # 20ms period for 50Hz
self.pwm.duty_u16(duty)
def stop(self):
self.pwm.duty_u16(0)
def main(servo1, servo2, button):
# start position
servo1(0)
servo2(0)
# loop to poll button and set angle of servo1 and servo2
while True:
time.sleep_ms(10)
# sleep longer, if the button bounces
if button():
servo1(180)
servo2(180)
else:
servo1(0)
servo2(0)
if __name__ == "__main__":
servo1 = Servo(SERVO1_PIN)
servo2 = Servo(SERVO2_PIN)
# using Signal directly instead of Pin to invert the signal
# https://docs.micropython.org/en/latest/library/machine.Signal.html#constructors
button = Signal(BUTTON_PIN, Pin.IN, pull=Pin.PULL_UP, invert=True)
# let you control servo1, servo2 and use button after
# CTRL+C
# The main function takes the 2 Servo instances and the button
try:
# no loop here,
# it's inside main()
main(servo1, servo2, button)
except KeyboadInterrupt:
servo1.stop()
servo2.stop() |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Good afternoon, 1 week ago I had never heard of Micro python. I am building an Ironman mask and my 14-year-old who decided he wanted the mask to open and close. I do have a background in SolidWorks, so i was able to make the required mechanism to open and close it. I had a raspberry Pico laying around from a 3d printer build so off I went.
After a lot of trial and error and web surfing i came up with 2 scripts.
the first one works perfect except the servo orientation make the 2 servos (MG90S) turn the same direction. but it does work with a button to open on first click and return to close on the second,
the second script does fix the rotating the same direction. but now with them rotating together opposite ways it travels from 0 to 180 and back to 0 with each click. I cannot get it to pause and wait for a second button click
his file runs both servos oppiste dirrection not smooth and does not stop.txt
this file runs both servos oppiste dirrection not smooth and does not stop.py
this one works best but both servos turn the same dirrection.py
this one works best but both servos turn the same dirrection.txt
I'm running out of time, any ideas would be AMAZING
All reactions