Package lejos.hardware.motor
Class JavaMotorRegulator
- java.lang.Object
-
- lejos.hardware.motor.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 classJavaMotorRegulator.ControllerThis class provides a single thread that drives all of the motor regulation process.
-
Field Summary
Fields Modifier and Type Field Description protected static JavaMotorRegulator.Controllercontintpowerprotected intstallLimitprotected intstallTimeprotected TachoMotorPorttachoPort-
Fields inherited from interface lejos.hardware.motor.MotorRegulator
NO_LIMIT
-
-
Constructor Summary
Constructors Constructor Description JavaMotorRegulator(TachoMotorPort p)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(RegulatedMotor motor, RegulatedMotorListener listener)Add a motor listener.voidadjustAcceleration(int newAcc)The target acceleration has been changed.voidadjustSpeed(float newSpeed)The target speed has been changed.voidendSynchronization(boolean b)Complete a set of synchronized motor operations.floatgetCurrentVelocity()Return the current velocity (in degrees/second) that the motor is currently running at.intgetLimitAngle()Return the angle that this Motor is rotating to.floatgetPosition()return the regulations models current position.intgetTachoCount()Get the current hardware tachometer reading for the motor,booleanisMoving()Return true if the motor is currently activebooleanisStalled()Return true if the motor is currently stalled.voidnewMove(float speed, int acceleration, int limit, boolean hold, boolean waitComplete)Initiate a new move and optionally wait for it to complete.RegulatedMotorListenerremoveListener()protected voidreset()Reset the tachometer readingsvoidresetTachoCount()Reset the tachometer base value, after this call the tachometer will return zero for the current position.voidsetControlParamaters(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.voidsetStallThreshold(int error, int time)Set the stall detection parameters.voidstartSynchronization()Begin a set of synchronized motor operationsvoidsynchronizeWith(MotorRegulator[] rl)Specify a set of motors that should be kept in synchronization with this one.protected voidupdateState(int velocity, boolean hold, boolean stalled)Update the internal state of the motor.voidwaitComplete()Wait until the current movement operation is complete (this can include the motor stalling).
-
-
-
Field Detail
-
power
public int power
-
stallLimit
protected int stallLimit
-
stallTime
protected int stallTime
-
tachoPort
protected TachoMotorPort tachoPort
-
cont
protected static final JavaMotorRegulator.Controller cont
-
-
Constructor Detail
-
JavaMotorRegulator
public JavaMotorRegulator(TachoMotorPort p)
-
-
Method Detail
-
getTachoCount
public int getTachoCount()
Description copied from interface:MotorRegulatorGet the current hardware tachometer reading for the motor,- Specified by:
getTachoCountin interfaceMotorRegulator- Returns:
- hardware reading
-
resetTachoCount
public void resetTachoCount()
Description copied from interface:MotorRegulatorReset 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:
resetTachoCountin interfaceMotorRegulator
-
isMoving
public boolean isMoving()
Description copied from interface:MotorRegulatorReturn true if the motor is currently active- Specified by:
isMovingin interfaceMotorRegulator- Returns:
- True if the motor is moving.
-
getCurrentVelocity
public float getCurrentVelocity()
Description copied from interface:MotorRegulatorReturn 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:
getCurrentVelocityin interfaceMotorRegulator- Returns:
- velocity
-
setStallThreshold
public void setStallThreshold(int error, int time)Description copied from interface:MotorRegulatorSet 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:
setStallThresholdin interfaceMotorRegulator
-
updateState
protected void updateState(int velocity, boolean hold, boolean stalled)Update the internal state of the motor.- Parameters:
velocity-hold-stalled-
-
addListener
public void addListener(RegulatedMotor motor, RegulatedMotorListener listener)
Description copied from interface:MotorRegulatorAdd a motor listener. Move operations will be reported to this object.- Specified by:
addListenerin interfaceMotorRegulator
-
removeListener
public RegulatedMotorListener removeListener()
- Specified by:
removeListenerin interfaceMotorRegulator
-
setControlParamaters
public void setControlParamaters(int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset)Description copied from interface:MotorRegulatorSet the motion control parameters used by the regulator.- Specified by:
setControlParamatersin interfaceMotorRegulator- Parameters:
typ- The type of motormoveP- The Proportional control value used while movingmoveI- The integral control parameter used while movingmoveD- The differential control parameter used while movingholdP- The Proportional control value used while holding positionholdI- The integral control value used while holding positionholdD- The differential control value used while holding positionoffset- Motor PWM offset value range 0-10000.
-
waitComplete
public void waitComplete()
Description copied from interface:MotorRegulatorWait until the current movement operation is complete (this can include the motor stalling).- Specified by:
waitCompletein interfaceMotorRegulator
-
getLimitAngle
public int getLimitAngle()
Description copied from interface:MotorRegulatorReturn the angle that this Motor is rotating to.- Specified by:
getLimitAnglein interfaceMotorRegulator- Returns:
- angle in degrees
-
isStalled
public boolean isStalled()
Description copied from interface:MotorRegulatorReturn true if the motor is currently stalled.- Specified by:
isStalledin interfaceMotorRegulator- 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:
getPositionin interfaceMotorRegulator- 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:
newMovein interfaceMotorRegulator- 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:
adjustSpeedin interfaceMotorRegulator- Parameters:
newSpeed- new target speed.
-
adjustAcceleration
public void adjustAcceleration(int newAcc)
The target acceleration has been changed. Updated the regulator.- Specified by:
adjustAccelerationin interfaceMotorRegulator- Parameters:
newAcc-
-
startSynchronization
public void startSynchronization()
Description copied from interface:MotorRegulatorBegin a set of synchronized motor operations- Specified by:
startSynchronizationin interfaceMotorRegulator
-
endSynchronization
public void endSynchronization(boolean b)
Description copied from interface:MotorRegulatorComplete a set of synchronized motor operations.- Specified by:
endSynchronizationin interfaceMotorRegulator
-
synchronizeWith
public void synchronizeWith(MotorRegulator[] rl)
Description copied from interface:MotorRegulatorSpecify 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:
synchronizeWithin interfaceMotorRegulator- Parameters:
rl- an array of motors to synchronize with.
-
-