0

Basically I want to control the angle of the rotation of the servo motor but I don't seem to know how... From the tech sheet I know this motor shouldn't be a continuous one, any hint would be appreciated

I have tried:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)

p = GPIO.PWM(25, 50)

print("center")
p.start(7.5)
time.sleep(2)
print("45 degree")
p.ChangeDutyCycle(5)
time.sleep(2)
print("The other 45 degree")
p.ChangeDutyCycle(10)
time.sleep(2)
print("Center again")
p.ChangeDutyCycle(7.5)
time.sleep(2)
print("stop")
p.stop()
GPIO.cleanup()

And

from gpiozero import Servo
from time import sleep

myGPIO=25

myCorrection=0.45
maxPW=(2.0+myCorrection)/1000
minPW=(1.0-myCorrection)/1000

servo = Servo(myGPIO,min_pulse_width=minPW,max_pulse_width=maxPW)

while True:
    servo.value=0
    print("0")
    sleep(10)
    servo.value=0.6
    print("0")
    sleep(10)
ka_lin
  • 103
  • 3

1 Answers1

1

Servos move to a commanded angle and then stop.

Servos are controlled by a series of short pulses, typically sent 50 times per second (50 Hz). The length of each pulse determines the commanded angle. Typically the pulse length is between 1000 and 2000 microseconds (µs).

There is no standard for which pulse length is used for which angle. However most servos treat a pulse length of about 1500 µs as the centre. Longer pulse lengths turn more clockwise. Shorter pulse lengths turn more counterclockwise. Most servos are happy for pulses in the range 1000 to 2000 µs. I would not use pulse lengths outside the range 1000 to 2000 µs without care as some servos can be damaged.

You will have to experiment to determine the range of pulse lengths acceptable to your servos. There will be variations between models and between servos of the same model.

I suggest you use (my) pigs servo command to check the pulse lengths.

E.g. if your servo is connected to (Broadcom) GPIO 4.

sudo pigpiod # start the daemon

pigs s 4 1000 # send 1000 µs pulses on GPIO 4
pigs s 4 1500 # send 1500 µs pulses
pigs s 4 2000 # send 2000 µs pulses

Once you determine the end angles you can assume that movement will be linear between the two, i.e. a change in pulse length of 100 µs will result in the same change in angle throughout the range.

joan
  • 71,852
  • 5
  • 76
  • 108