public abstract class ServoMotor extends PWMComponent
All you need to do to implement a new type of servo is to extend this class with the parameters corresponding to your servo.
You can use the ServoMotorCalibration class to easily determine the values of the different parameters.
To rotate the servo, we use the Pulse Width Modulation technique. Pulse Width Modulation (or PWM) is a technique for controlling power. We use it here to control the length of the pulse going to the servo and hence the position of the rotor.
There is only twos pins on the recent Pi that are capable of producing pulses in this way. But we also implemented the driver to easily use a PCA9685, capable of producing up to 16 independent PWM signals.
It is strongly recommended that the power to the servo is provided by an external battery as powering the servo from the Pi itself is likely to cause it to crash as the servo draws too much current as it starts to move. Servos require ~4.8-6V DC power to the motor, but the signal level (pulse output) can be 3.3V, which is how its OK to just connect the signal line directly to the GPIO output of the Pi.
Modifier and Type | Field and Description |
---|---|
static int |
PWM_RANGE_GENERATOR
Range register in the PWM generator.
|
static int |
TARGET_FREQUENCY
Each PWM period must be 20ms long.
|
DEFAULT_RPI_PWM_CLOCK_FREQUENCY, PCA9685_PULSE_TICKS, pinNumber
Constructor and Description |
---|
ServoMotor(PCA9685 pca9685,
PCA9685.PCA9685Channel channel,
double minAngle,
double minPulseLength,
double maxAngle,
double maxPulseLength,
double timeToRotate60Degrees,
double multiplicatorForTimeToReachPosition)
Creates a new instance of a servo motor, with the corresponding parameters,
using a PCA9685 module to generate PWM signals.
|
ServoMotor(PWMPin pin,
double minAngle,
double minPulseLength,
double maxAngle,
double maxPulseLength,
double timeToRotate60Degrees,
double multiplicatorForTimeToReachPosition)
Creates a new instance of a servo motor, with the corresponding parameters,
using a Raspberry Pi PWM pin to generate PWM signals.
|
Modifier and Type | Method and Description |
---|---|
double |
getPositionAngle()
Computes the current position of the rotor, in °.
|
void |
resetPosition()
Turns the rotor to the minAngle position.
|
void |
setAngle(double angle)
Sets the position of the rotor to the angle position, in degree.
|
void |
setPulseLength(int value)
Writes the value to the PWM register for the given pin.
|
void |
setPulseLength(int value,
int delayToReachPosition)
Sends a PWM signal during delayToReachPosition ms.
|
void |
stopMotor()
Stop sending orders to the servo motor.
|
getType, setPWM, setPWM, setPWMFreq
public static final int TARGET_FREQUENCY
public static final int PWM_RANGE_GENERATOR
The values sent to the generator must be in the [0; PWM_RANGE_GENERATOR] range.
public ServoMotor(PCA9685 pca9685, PCA9685.PCA9685Channel channel, double minAngle, double minPulseLength, double maxAngle, double maxPulseLength, double timeToRotate60Degrees, double multiplicatorForTimeToReachPosition)
Note: the ServoMotorCalibration class can be used to easily determine the usefull values.
Example:
pca9685
- the pca9685 module used to generate PWM signals.channel
- the channel, on the pca9685, used to generate PWM signals.minAngle
- the minimum angle available for the rotor of this servo motor.minPulseLength
- the pulse length in ms corresponding to the minAngle position of the rotor.maxAngle
- the maximum angle available for the rotor of this servo motor.maxPulseLength
- the pulse length in ms corresponding to the maxAngle position of the rotor.timeToRotate60Degrees
- the time needed in seconds to execute a rotation of 60° [cfr datasheets].multiplicatorForTimeToReachPosition
- the value used to multiply the computed time needed to reach a position.public ServoMotor(PWMPin pin, double minAngle, double minPulseLength, double maxAngle, double maxPulseLength, double timeToRotate60Degrees, double multiplicatorForTimeToReachPosition)
Note: the ServoMotorCalibration class can be used to easily determine the usefull values.
Example:
pin
- the PWMPin where the servo is attached.minAngle
- the minimum angle available for the rotor of this servo motor.minPulseLength
- the pulse length in ms corresponding to the minAngle position of the rotor.maxAngle
- the maximum angle available for the rotor of this servo motor.maxPulseLength
- the pulse length in ms corresponding to the maxAngle position of the rotor.timeToRotate60Degrees
- the time needed in seconds to execute a rotation of 60° [cfr datasheets].multiplicatorForTimeToReachPosition
- the value used to multiply the computed time needed to reach a position.public void stopMotor()
It tells the motor to turn itself off and wait for more instructions.
public void setAngle(double angle)
angle
- the target position of the rotor, in °.public void resetPosition()
public void setPulseLength(int value)
Rotates the servo to the new position, and then stops the signal.
value
- the value for the PWM signal corresponding to the new position to apply on the servo.public void setPulseLength(int value, int delayToReachPosition)
This allows you to send a PWM signal for a custom duration.
value
- the value for the PWM signal corresponding to the new position to apply on the servo.delayToReachPosition
- the estimated delay to reach the new position, with regard to it current position.public double getPositionAngle()