差别
这里会显示出您选择的修订版和当前版本之间的差别。
mp_servo [2021/09/28 02:17] gongyusu 创建 |
mp_servo [2021/09/30 23:06] (当前版本) gongyusu |
||
---|---|---|---|
行 45: | 行 45: | ||
time.sleep(1) | time.sleep(1) | ||
+ | </code> | ||
+ | |||
+ | ### 来自Maker Pi | ||
+ | <code python> | ||
+ | # This code controls one servos to move from 0 degree to 180 degree, | ||
+ | # then back to 0 degree, and repeats forever. | ||
+ | # --- | ||
+ | # Connection: 1x Servo ports at GP12. Take note on the polarity. | ||
+ | # --- | ||
+ | # Hardware: | ||
+ | # 1. Cytron Maker Pi RP2040 (www.cytron.io/p-MAKER-PI-RP2040) | ||
+ | # - Any RP2040 boards should work too. | ||
+ | # 2. TS90A Micro Servo 3-6V (www.cytron.io/p-analog-micro-servo-9g-3v-6v) | ||
+ | # - Any servo motors within the rated voltage of 3.6-6V. | ||
+ | # --- | ||
+ | from machine import Pin, PWM | ||
+ | import time | ||
+ | |||
+ | # fine tune the duty cycle values to suit your servo motor | ||
+ | MIN_DUTY = 1600 | ||
+ | MAX_DUTY = 8400 | ||
+ | |||
+ | pwm = PWM(Pin(12)) | ||
+ | pwm.freq(50) | ||
+ | |||
+ | while True: | ||
+ | pwm.duty_u16(MIN_DUTY) | ||
+ | time.sleep_ms(1000) | ||
+ | pwm.duty_u16(MAX_DUTY) | ||
+ | time.sleep_ms(1000) | ||
+ | </code> | ||
+ | |||
+ | Example2 | ||
+ | <code python> | ||
+ | # This code controls all 4 servos to move from 0 degree to 180 degree, | ||
+ | # then back to 0 degree, and repeats forever. | ||
+ | # --- | ||
+ | # Connection: 4x Servo ports - from GP12 to GP15 | ||
+ | # --- | ||
+ | # Hardware: | ||
+ | # 1. Cytron Maker Pi RP2040 (www.cytron.io/p-MAKER-PI-RP2040) | ||
+ | # - Any RP2040 boards should work too. | ||
+ | # 2. TS90A Micro Servo 3-6V (www.cytron.io/p-analog-micro-servo-9g-3v-6v) | ||
+ | # - Any servo motors within the rated voltage of 3.6-6V. | ||
+ | # --- | ||
+ | from machine import Pin, PWM | ||
+ | import time | ||
+ | |||
+ | # fine tune the duty cycle values to suit your servo motors | ||
+ | MIN_DUTY = 1600 | ||
+ | MAX_DUTY = 8400 | ||
+ | |||
+ | servo1 = PWM(Pin(12)) | ||
+ | servo1.freq(50) | ||
+ | servo2 = PWM(Pin(13)) | ||
+ | servo2.freq(50) | ||
+ | servo3 = PWM(Pin(14)) | ||
+ | servo3.freq(50) | ||
+ | servo4 = PWM(Pin(15)) | ||
+ | servo4.freq(50) | ||
+ | |||
+ | while True: | ||
+ | servo1.duty_u16(MIN_DUTY) | ||
+ | servo2.duty_u16(MIN_DUTY) | ||
+ | servo3.duty_u16(MIN_DUTY) | ||
+ | servo4.duty_u16(MIN_DUTY) | ||
+ | time.sleep_ms(1000) | ||
+ | servo1.duty_u16(MAX_DUTY) | ||
+ | servo2.duty_u16(MAX_DUTY) | ||
+ | servo3.duty_u16(MAX_DUTY) | ||
+ | servo4.duty_u16(MAX_DUTY) | ||
+ | time.sleep_ms(1000) | ||
</code> | </code> |