Class ServoMotorComponent

java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.ServoMotorComponent

public class ServoMotorComponent extends Component
Implementation of the CrowPi Servo Motor using PWM with Pi4J
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
    Default PWM frequency of the servo, based on values for the CrowPi servo
    protected static final float
    Default maximum angle of the servo motor, based on values for the CrowPi servo
    protected static final float
    Maximum PWM duty cycle to put the PWM into the maximum angle position
    protected static final float
    Default minimum angle of the servo motor, based on values for the CrowPi servo
    protected static final float
    Default minimum PWM duty cycle to put the PWM into the minimum angle position

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    ServoMotorComponent(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 Type
    Method
    Description
    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.
    float
    Returns the maximum angle configured for this servo.
    float
    Returns the minimum angle configured for this servo.
    protected com.pi4j.io.pwm.Pwm
    Returns the created PWM instance for the servo
    void
    moveOnRange(float value)
    Maps the given value based on the range previously defined with setRange(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 between getMinAngle() and getMaxAngle().
    void
    setRange(float minValue, float maxValue)
    Adjusts the minimum and maximum for the user-defined range which can be used in combination with moveOnRange(float).

    Methods inherited from class com.pi4j.crowpi.components.Component

    sleep, triggerSimpleEvent

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_FREQUENCY

      protected static final int DEFAULT_FREQUENCY
      Default PWM frequency of the servo, based on values for the CrowPi servo
      See Also:
    • DEFAULT_MIN_ANGLE

      protected static final float DEFAULT_MIN_ANGLE
      Default minimum angle of the servo motor, based on values for the CrowPi servo
      See Also:
    • DEFAULT_MAX_ANGLE

      protected static final float DEFAULT_MAX_ANGLE
      Default 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_CYCLE
      Default 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_CYCLE
      Maximum 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 context
      minAngle - Minimum angle in degrees
      maxAngle - Maximum angle in degrees
      minDutyCycle - Minimum duty cycle as float, between 0 and 100
      maxDutyCycle - 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 context
      address - Custom BCM pin address
      frequency - Frequency used for PWM with servo
      minAngle - Minimum angle in degrees
      maxAngle - Maximum angle in degrees
      minDutyCycle - Minimum duty cycle as float, between 0 and 100
      maxDutyCycle - 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 between getMinAngle() and getMaxAngle() 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 between getMinAngle() and getMaxAngle(). 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 with setRange(float, float) to the full range of the servo. If setRange(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. Unlike moveOnRange(float), this method will NOT use or adjust the values set by setRange(float, float).
      Parameters:
      value - Value to map
      minValue - Minimum range value
      maxValue - 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 with moveOnRange(float). This method will only affect future calls to moveOnRange(float) and does not change the current position.
      Parameters:
      minValue - Minimum range value
      maxValue - 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 context
      address - BCM address
      frequency - PWM frequency
      Returns:
      PWM configuration