Iain-S / sentry-bot

Autonomous Nerf turret
MIT License
3 stars 3 forks source link

Use rpi.gpio for servo control #34

Closed Iain-S closed 1 year ago

Iain-S commented 2 years ago

~Using gpiozero seemed to crash the Pi so~ we could switch to RPi.GPIO. The following is known to work but it doesn't seem to support hardware PWM (so is pretty hard to control).

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(37, GPIO.OUT)

p = GPIO.PWM(37, 50)
p.start(5)
p.start(7)
p.start(10)

for i in range(11):
  p.ChangeDutyCycle(5+i*0.5)
  sleep(0.5)

p.stop()

Note that

  1. We are using GPIO.BOARD numbering so physical pin 37 is GPIO pin 26 in the image below
  2. The servo works on 50Hz freq (20ms period). 1ms on (5% of 20ms) is 0 degrees an 2ms on (10%) is 180 degrees. See here for more.

Image

note the crash could have been from trying to turn some kind of important system pin into a Servo.

Iain-S commented 2 years ago

Alternatively, we could use pigpio, which does support hardware PWM.

sudo pigpiod
python
import gpiozero

s = gpiozero.Servo(13, pin_factory=gpiozero.pins.pigpio.PiGPIOFactory())
s.min()
s.mid()
s.max()
s.value = 0.9 (close to max)
s.detach()
Iain-S commented 2 years ago

Closing as we do want to use gpiozero after all. See comment above for code to move a Servo.

Notes: