java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.ServoMotorComponent
Implementation of the CrowPi Servo Motor using PWM with Pi4J
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final int
Default PWM frequency of the servo, based on values for the CrowPi servoprotected static final float
Default maximum angle of the servo motor, based on values for the CrowPi servoprotected static final float
Maximum PWM duty cycle to put the PWM into the maximum angle positionprotected static final float
Default minimum angle of the servo motor, based on values for the CrowPi servoprotected static final float
Default minimum PWM duty cycle to put the PWM into the minimum angle position -
Constructor Summary
ConstructorDescriptionServoMotorComponent
(com.pi4j.context.Context pi4j) Creates a new step motor component with the default pin, angle range as well as duty cycle range.ServoMotorComponent
(com.pi4j.context.Context pi4j, float minAngle, float maxAngle, float minDutyCycle, float maxDutyCycle) Creates a new step motor component with the default pin and frequency but customized angle and duty cycle values.ServoMotorComponent
(com.pi4j.context.Context pi4j, int address, int frequency, float minAngle, float maxAngle, float minDutyCycle, float maxDutyCycle) Creates a new step motor component with custom pin, frequency, angle range and duty cycle range values. -
Method Summary
Modifier and TypeMethodDescriptionprotected com.pi4j.io.pwm.PwmConfig
buildPwmConfig
(com.pi4j.context.Context pi4j, int address, int frequency) Builds a new PWM configuration for the step motor.float
Returns the maximum angle configured for this servo.float
Returns the minimum angle configured for this servo.protected com.pi4j.io.pwm.Pwm
getPwm()
Returns the created PWM instance for the servovoid
moveOnRange
(float value) Maps the given value based on the range previously defined withsetRange(float, float)
to the full range of the servo.void
moveOnRange
(float value, float minValue, float maxValue) Maps the given value based on the given input range to the full range of the servo.void
setAngle
(float angle) Rotates the servo motor to the specified angle in degrees.void
setPercent
(float percent) Rotates the servo by mapping a percentage value to the range betweengetMinAngle()
andgetMaxAngle()
.void
setRange
(float minValue, float maxValue) Adjusts the minimum and maximum for the user-defined range which can be used in combination withmoveOnRange(float)
.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
-
Field Details
-
DEFAULT_FREQUENCY
protected static final int DEFAULT_FREQUENCYDefault PWM frequency of the servo, based on values for the CrowPi servo- See Also:
-
DEFAULT_MIN_ANGLE
protected static final float DEFAULT_MIN_ANGLEDefault minimum angle of the servo motor, based on values for the CrowPi servo- See Also:
-
DEFAULT_MAX_ANGLE
protected static final float DEFAULT_MAX_ANGLEDefault maximum angle of the servo motor, based on values for the CrowPi servo- See Also:
-
DEFAULT_MIN_DUTY_CYCLE
protected static final float DEFAULT_MIN_DUTY_CYCLEDefault minimum PWM duty cycle to put the PWM into the minimum angle position- See Also:
-
DEFAULT_MAX_DUTY_CYCLE
protected static final float DEFAULT_MAX_DUTY_CYCLEMaximum PWM duty cycle to put the PWM into the maximum angle position- See Also:
-
-
Constructor Details
-
ServoMotorComponent
public ServoMotorComponent(com.pi4j.context.Context pi4j) Creates a new step motor component with the default pin, angle range as well as duty cycle range.- Parameters:
pi4j
- Pi4J context
-
ServoMotorComponent
public ServoMotorComponent(com.pi4j.context.Context pi4j, float minAngle, float maxAngle, float minDutyCycle, float maxDutyCycle) Creates a new step motor component with the default pin and frequency but customized angle and duty cycle values. This can be used if the servo bundled with the CrowPi should for some reason have values which are totally off.- Parameters:
pi4j
- Pi4J contextminAngle
- Minimum angle in degreesmaxAngle
- Maximum angle in degreesminDutyCycle
- Minimum duty cycle as float, between 0 and 100maxDutyCycle
- Maximum duty cycle as float, between 0 and 100
-
ServoMotorComponent
public ServoMotorComponent(com.pi4j.context.Context pi4j, int address, int frequency, float minAngle, float maxAngle, float minDutyCycle, float maxDutyCycle) Creates a new step motor component with custom pin, frequency, angle range and duty cycle range values.- Parameters:
pi4j
- Pi4J contextaddress
- Custom BCM pin addressfrequency
- Frequency used for PWM with servominAngle
- Minimum angle in degreesmaxAngle
- Maximum angle in degreesminDutyCycle
- Minimum duty cycle as float, between 0 and 100maxDutyCycle
- Maximum duty cycle as float, between 0 and 100
-
-
Method Details
-
setAngle
public void setAngle(float angle) Rotates the servo motor to the specified angle in degrees. The angle should be betweengetMinAngle()
andgetMaxAngle()
which was specified during initialization. Values outside of this inclusive range are automatically being clamped to their respective minimum / maximum.- Parameters:
angle
- New absolute angle
-
setPercent
public void setPercent(float percent) Rotates the servo by mapping a percentage value to the range betweengetMinAngle()
andgetMaxAngle()
. As an example, a value of 0% will equal to the minimum angle, 50% to the center and 100% to the maximum angle.- Parameters:
percent
- Percentage value, automatically clamped between 0 and 100
-
moveOnRange
public void moveOnRange(float value) Maps the given value based on the range previously defined withsetRange(float, float)
to the full range of the servo. IfsetRange(float, float)
was not called before, the default range of 0-1 (as float) is being used.- Parameters:
value
- Value to map
-
moveOnRange
public void moveOnRange(float value, float minValue, float maxValue) Maps the given value based on the given input range to the full range of the servo. UnlikemoveOnRange(float)
, this method will NOT use or adjust the values set bysetRange(float, float)
.- Parameters:
value
- Value to mapminValue
- Minimum range valuemaxValue
- Maximum range value
-
setRange
public void setRange(float minValue, float maxValue) Adjusts the minimum and maximum for the user-defined range which can be used in combination withmoveOnRange(float)
. This method will only affect future calls tomoveOnRange(float)
and does not change the current position.- Parameters:
minValue
- Minimum range valuemaxValue
- Maximum range value
-
getMinAngle
public float getMinAngle()Returns the minimum angle configured for this servo.- Returns:
- Minimum angle in degrees
-
getMaxAngle
public float getMaxAngle()Returns the maximum angle configured for this servo.- Returns:
- Maximum angle in degrees
-
getPwm
protected com.pi4j.io.pwm.Pwm getPwm()Returns the created PWM instance for the servo- Returns:
- PWM instance
-
buildPwmConfig
protected com.pi4j.io.pwm.PwmConfig buildPwmConfig(com.pi4j.context.Context pi4j, int address, int frequency) Builds a new PWM configuration for the step motor.- Parameters:
pi4j
- Pi4J contextaddress
- BCM addressfrequency
- PWM frequency- Returns:
- PWM configuration
-