python – Control speed of a PWM servo Raspberry Pi

I would like to know how to control the speed of a servo motor with Raspberry Pi and PWM pulses.

3 is the maximum speed value with a counterclockwise direction.
12 is the maximum speed value with a clockwise direction.
Right or am I wrong?

My question is:
Is there a function where I just put the direction and if I want a speed between 0% to 100%? That is, instead of 3 I want to indicate 100.

# Set up libraries and overall settings
import RPi.GPIO as GPIO  # Imports the standard Raspberry Pi GPIO library
from time import sleep   # Imports sleep (aka wait or pause) into the program
GPIO.setmode(GPIO.BOARD) # Sets the pin numbering system to use the physical layout

# Set up pin 11 for PWM
GPIO.setup(11,GPIO.OUT)  # Sets up pin 11 to an output (instead of an input)
p = GPIO.PWM(11, 50)     # Sets up pin 11 as a PWM pin
p.start(0)               # Starts running PWM on the pin and sets it to 0

# Move the servo back and forth
p.ChangeDutyCycle(3)     # Changes the pulse width to 3 (so moves the servo)
sleep(1)                 # Wait 1 second
p.ChangeDutyCycle(12)    # Changes the pulse width to 12 (so moves the servo)
sleep(1)

# Clean up everything
p.stop()                 # At the end of the program, stop the PWM
GPIO.cleanup()           # Resets the GPIO pins back to defaults

Example:

runServo(60,1) #The 60 refers to the speed of 60% and the 1 refers to the direction, ie left.

Can you help me with that function?

Read more here: Source link