When using Rpi3B+ python to control servo using PWM,
If d = f(t) where d = angular distance traversed, t = time
Is f a smooth step function?
Any suggestion to modify the function?
d = f(t), f is only a rough step function
Let us use a toy servo as an example. There is a 4 bit Amtel mcu inside doing simple proportional P (not PID) control. In other words, motor starts moving quickly and gradually slows down as the target position gets nearer and nearer. It is P, not PID, therefore the step with much overshooting/damping is not that smooth.
I know the speed of typical toy servo is about 60 degrees every 0.1 second. In other words, 0.1 x (360/60) = 0.6 sec per revolution, or 0.6 x 60 = 36 rpm. Usually we hobbyists use this kind of servos to control toy cars or drones. We don't know nothing of scary time functions! :)
However, we do know that the final steady, if not oscillating, trembling position, in degrees, (we don't know nothing of "radian") is a function (depends on) of the pulse width， or duty cycle, if frequency is fixed at the usual 50Hz, 20mS. In other words, the function mapping is something like below:
Domain/Range (independent variable/dependent variable)
pulse width (500uS ~ 2400uS) / +-90 degrees
motor power supply (4.8V ~ 6V) / speed, range with average speed 30rpm
loading / speed (non linear)
Suggestion to get a smooth step function
The very old design using a 4 bit mcu cannot do much more than simple proportional control. If space is not a problem, you can use modern 32bit controllers such as STM32, which is perhaps 1,000 times more powerful. But of course industrial/military/space digital servos are already using 32 bit controllers - toy motors are perhaps 10 years lagging behind! :)
Your Guide To RC Servos - RC Helicopter Fun
How Do Servo Motors Work? - Jameco
Servo motors are small in size, used in radio-controlled toy cars,
robots and airplanes.
How do the little guys work?
The servo circuitry is built right inside the motor unit and has a
positionable shaft, which usually is fitted with a gear. The motor is
controlled with an electric signal which determines the amount of
movement of the shaft.
What's inside the servo?
Example - Hitec HS-322HD Standard Heavy Duty Servo
To fully understand how the servo works, you need to take a look under
the hood. Inside there is a pretty simple set-up:
A small DC motor, potentiometer, and a control circuit.
The motor is attached by gears to the control wheel.
As the motor rotates, the potentiometer's resistance changes, so the
control circuit can precisely regulate how much movement there is and
in which direction.
When the shaft of the motor is at the desired position, power supplied
to the motor is stopped. If not, the motor is turned in the
appropriate direction. The desired position is sent via electrical
pulses through the signal wire.
The motor's speed is proportional to the difference between its actual
position and desired position. So if the motor is near the desired
position, it will turn slowly, otherwise it will turn fast. This is
called proportional control. This means the motor will only run as
hard as necessary to accomplish the task at hand, a very efficient
How is the servo controlled?
Servos are controlled by sending an electrical pulse of variable
width, or pulse width modulation (PWM), through the control wire.
There is a minimum pulse, a maximum pulse, and a repetition rate. A
servo motor can usually only turn 90° in either direction for a total
of 180° movement. The motor's neutral position is defined as the
position where the servo has the same amount of potential rotation in
the both the clockwise or counter-clockwise direction.
The PWM sent to the motor determines position of the shaft, and based
on the duration of the pulse sent via the control wire; the rotor will
turn to the desired position.
The servo motor expects to see a pulse every 20 milliseconds (ms) and
the length of the pulse will determine how far the motor turns. For
example, a 1.5ms pulse will make the motor turn to the 90° position.
Shorter than 1.5ms moves it in the counter clockwise direction toward
the 0° position, and any longer than 1.5ms will turn the servo in a
clockwise direction toward the 180° position.
Variable Pulse width control servo position
When these servos are commanded to move, they will move to the
position and hold that position. If an external force pushes against
the servo while the servo is holding a position, the servo will resist
from moving out of that position. The maximum amount of force the
servo can exert is called the torque rating of the servo. Servos will
not hold their position forever though; the position pulse must be
repeated to instruct the servo to stay in position.