Class JavaMotorRegulator

  • All Implemented Interfaces:
    MotorRegulator

    public class JavaMotorRegulator
    extends java.lang.Object
    implements MotorRegulator
    Java based regulator regulate velocity; also stop motor at desired rotation angle. This class uses a very simple movement model based on simple linear acceleration. This model is used to generate ideal target positions which are then used to generate error terms between the actual and target position this error term is then used to drive a PID style motor controller to regulate the power supplied to the motor. If new command are issued while a move is in progress, the new command is blended with the current one to provide smooth movement. If the requested speed is not possible then the controller will simply drop move cycles until the motor catches up with the ideal position. If too many consecutive dropped moves are required then the motor is viewed to have stalled and the move is terminated. Once the motor stops, the final position is held using the same PID control mechanism (with slightly different parameters), as that used for movement. TODO: Should we add synchronization to this?
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  JavaMotorRegulator.Controller
      This class provides a single thread that drives all of the motor regulation process.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addListener​(RegulatedMotor motor, RegulatedMotorListener listener)
      Add a motor listener.
      void adjustAcceleration​(int newAcc)
      The target acceleration has been changed.
      void adjustSpeed​(float newSpeed)
      The target speed has been changed.
      void endSynchronization​(boolean b)
      Complete a set of synchronized motor operations.
      float getCurrentVelocity()
      Return the current velocity (in degrees/second) that the motor is currently running at.
      int getLimitAngle()
      Return the angle that this Motor is rotating to.
      float getPosition()
      return the regulations models current position.
      int getTachoCount()
      Get the current hardware tachometer reading for the motor,
      boolean isMoving()
      Return true if the motor is currently active
      boolean isStalled()
      Return true if the motor is currently stalled.
      void newMove​(float speed, int acceleration, int limit, boolean hold, boolean waitComplete)
      Initiate a new move and optionally wait for it to complete.
      RegulatedMotorListener removeListener()  
      protected void reset()
      Reset the tachometer readings
      void resetTachoCount()
      Reset the tachometer base value, after this call the tachometer will return zero for the current position.
      void setControlParamaters​(int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset)
      Set the motion control parameters used by the regulator.
      void setStallThreshold​(int error, int time)
      Set the stall detection parameters.
      void startSynchronization()
      Begin a set of synchronized motor operations
      void synchronizeWith​(MotorRegulator[] rl)
      Specify a set of motors that should be kept in synchronization with this one.
      protected void updateState​(int velocity, boolean hold, boolean stalled)
      Update the internal state of the motor.
      void waitComplete()
      Wait until the current movement operation is complete (this can include the motor stalling).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • getTachoCount

        public int getTachoCount()
        Description copied from interface: MotorRegulator
        Get the current hardware tachometer reading for the motor,
        Specified by:
        getTachoCount in interface MotorRegulator
        Returns:
        hardware reading
      • resetTachoCount

        public void resetTachoCount()
        Description copied from interface: MotorRegulator
        Reset the tachometer base value, after this call the tachometer will return zero for the current position. Note that any in progress movements will be aborted.
        Specified by:
        resetTachoCount in interface MotorRegulator
      • isMoving

        public boolean isMoving()
        Description copied from interface: MotorRegulator
        Return true if the motor is currently active
        Specified by:
        isMoving in interface MotorRegulator
        Returns:
        True if the motor is moving.
      • getCurrentVelocity

        public float getCurrentVelocity()
        Description copied from interface: MotorRegulator
        Return the current velocity (in degrees/second) that the motor is currently running at. Note that this value may be supplied from the internal control model not from actually measuring the rotation speed. If the regulator is functioning correctly this will closely match the actual velocity
        Specified by:
        getCurrentVelocity in interface MotorRegulator
        Returns:
        velocity
      • setStallThreshold

        public void setStallThreshold​(int error,
                                      int time)
        Description copied from interface: MotorRegulator
        Set the stall detection parameters. The motor will be declared as stalled if the error in the motor position exceeds the specified value for longer than the given time.
        Specified by:
        setStallThreshold in interface MotorRegulator
      • updateState

        protected void updateState​(int velocity,
                                   boolean hold,
                                   boolean stalled)
        Update the internal state of the motor.
        Parameters:
        velocity -
        hold -
        stalled -
      • setControlParamaters

        public void setControlParamaters​(int typ,
                                         float moveP,
                                         float moveI,
                                         float moveD,
                                         float holdP,
                                         float holdI,
                                         float holdD,
                                         int offset)
        Description copied from interface: MotorRegulator
        Set the motion control parameters used by the regulator.
        Specified by:
        setControlParamaters in interface MotorRegulator
        Parameters:
        typ - The type of motor
        moveP - The Proportional control value used while moving
        moveI - The integral control parameter used while moving
        moveD - The differential control parameter used while moving
        holdP - The Proportional control value used while holding position
        holdI - The integral control value used while holding position
        holdD - The differential control value used while holding position
        offset - Motor PWM offset value range 0-10000.
      • waitComplete

        public void waitComplete()
        Description copied from interface: MotorRegulator
        Wait until the current movement operation is complete (this can include the motor stalling).
        Specified by:
        waitComplete in interface MotorRegulator
      • getLimitAngle

        public int getLimitAngle()
        Description copied from interface: MotorRegulator
        Return the angle that this Motor is rotating to.
        Specified by:
        getLimitAngle in interface MotorRegulator
        Returns:
        angle in degrees
      • isStalled

        public boolean isStalled()
        Description copied from interface: MotorRegulator
        Return true if the motor is currently stalled.
        Specified by:
        isStalled in interface MotorRegulator
        Returns:
        true if the motor is stalled, else false
      • reset

        protected void reset()
        Reset the tachometer readings
      • getPosition

        public float getPosition()
        return the regulations models current position. Ensure that the motor is active if needed.
        Specified by:
        getPosition in interface MotorRegulator
        Returns:
        the models current position
      • newMove

        public void newMove​(float speed,
                            int acceleration,
                            int limit,
                            boolean hold,
                            boolean waitComplete)
        Initiate a new move and optionally wait for it to complete. If some other move is currently executing then ensure that this move is terminated correctly and then start the new move operation.
        Specified by:
        newMove in interface MotorRegulator
        Parameters:
        speed -
        acceleration -
        limit -
        hold -
        waitComplete -
      • adjustSpeed

        public void adjustSpeed​(float newSpeed)
        The target speed has been changed. Reflect this change in the regulator.
        Specified by:
        adjustSpeed in interface MotorRegulator
        Parameters:
        newSpeed - new target speed.
      • adjustAcceleration

        public void adjustAcceleration​(int newAcc)
        The target acceleration has been changed. Updated the regulator.
        Specified by:
        adjustAcceleration in interface MotorRegulator
        Parameters:
        newAcc -
      • endSynchronization

        public void endSynchronization​(boolean b)
        Description copied from interface: MotorRegulator
        Complete a set of synchronized motor operations.
        Specified by:
        endSynchronization in interface MotorRegulator
      • synchronizeWith

        public void synchronizeWith​(MotorRegulator[] rl)
        Description copied from interface: MotorRegulator
        Specify a set of motors that should be kept in synchronization with this one. The synchronization mechanism simply ensures that operations between a startSynchronization call and an endSynchronization call will all be executed at the same time (when the endSynchronization method is called). This is all that is needed to ensure that motors will operate in a synchronized fashion. The start/end methods can also be used to ensure that reads of the motor state will also be consistent.
        Specified by:
        synchronizeWith in interface MotorRegulator
        Parameters:
        rl - an array of motors to synchronize with.