Package lejos.hardware.motor
Class BaseRegulatedMotor
- java.lang.Object
-
- lejos.hardware.Device
-
- lejos.hardware.motor.BaseRegulatedMotor
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,BaseMotor,Encoder,RegulatedMotor,Tachometer
- Direct Known Subclasses:
EV3LargeRegulatedMotor,EV3MediumRegulatedMotor,MindsensorsGlideWheelMRegulatedMotor,MindsensorsGlideWheelXLRegulatedMotor,NXTRegulatedMotor
public abstract class BaseRegulatedMotor extends Device implements RegulatedMotor
Abstraction for a Regulated motor motor. The basic control methods are:forward, backward, reverseDirection, stopandflt. To set each motor's velocity, use. The maximum velocity of the motor is limited by the battery voltage and load. With no load, the maximum degrees per second is about 100 times the voltage (for the large EV3 motor).setSpeed
The velocity is regulated by comparing the tacho count with velocity times elapsed time, and adjusting motor power to keep these closely matched. Changes in velocity will be made at the rate specified via thesetAcceleration(int acceleration)method. The methodsrotate(int angle)androtateTo(int ange)use the tachometer to control the position at which the motor stops, usually within 1 degree or 2.
Listeners. An object implementing theinterface may register with this class. It will be informed each time the motor starts or stops.RegulatedMotorListener
Stall detection If a stall is detected or if for some other reason the speed regulation fails, the motor will stop, andisStalled()returns true.
Motors will hold their position when stopped. If this is not what you require use the flt() method instead of stop().
Example:
TODO: Fix the nameMotor.A.setSpeed(720);// 2 RPM Motor.C.setSpeed(720); Motor.A.forward(); Motor.C.forward(); Delay.msDelay(1000); Motor.A.stop(); Motor.C.stop(); Motor.A.rotateTo( 360); Motor.A.rotate(-720,true); while(Motor.A.isMoving()Thread.yield(); int angle = Motor.A.getTachoCount(); // should be -360 LCD.drawInt(angle,0,0);
- Author:
- Roger Glassey/Andy Shaw
-
-
Field Summary
Fields Modifier and Type Field Description protected intaccelerationprotected intMAX_SPEED_AT_9Vprotected static intNO_LIMITprotected MotorRegulatorregprotected floatspeedprotected TachoMotorPorttachoPort
-
Constructor Summary
Constructors Constructor Description BaseRegulatedMotor(Port port, MotorRegulator regulator, int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset, int maxSpeed)Use this constructor to assign a variable of type motor connected to a particular port.BaseRegulatedMotor(TachoMotorPort port, MotorRegulator regulator, int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset, int maxSpeed)Use this constructor to assign a variable of type motor connected to a particular port.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddListener(RegulatedMotorListener listener)Add a motor listener.voidbackward()Causes motor to rotate backwards untilstop()orflt()is called.voidclose()Close the motor regulator.voidendSynchronization()Complete a set of synchronized motor operations.voidflt()Set the motor into float mode.voidflt(boolean immediateReturn)Set the motor into float mode.voidforward()Causes motor to rotate forward untilstop()orflt()is called.intgetAcceleration()returns acceleration in degrees/second/secondintgetLimitAngle()Return the angle that this Motor is rotating to.floatgetMaxSpeed()Returns the maximum speed that can be maintained by the regulation system based upon the current state of the battery.floatgetPosition()Returns the current position that the motor regulator is trying to maintain.intgetRotationSpeed()Return the current velocity.intgetSpeed()Return the current target speed.intgetTachoCount()Returns the tachometer count.booleanisMoving()This method returns true if the motor is attempting to rotate.booleanisStalled()Return true if the motor is currently stalled.voidlock(int power)Deprecated.The regulator will always try to hold position unless the motor is set into float mode using flt().RegulatedMotorListenerremoveListener()Removes the RegulatedMotorListener from this class.voidresetTachoCount()Reset the tachometer associated with this motor.voidrotate(int angle)Rotate by the requested number of degrees.voidrotate(int angle, boolean immediateReturn)Rotate by the request number of degrees.voidrotateTo(int limitAngle)Rotate to the target angle.voidrotateTo(int limitAngle, boolean immediateReturn)causes motor to rotate to limitAngle;
if immediateReturn is true, method returns immediately and the motor stops by itself
and getTachoCount should be within +- 2 degrees if the limit angle If any motor method is called before the limit is reached, the rotation is canceled.voidsetAcceleration(int acceleration)sets the acceleration rate of this motor in degrees/sec/sec
The default value is 6000; Smaller values will make speeding up.voidsetSpeed(float speed)Sets desired motor speed , in degrees per second; The maximum reliably sustainable velocity is 100 x battery voltage under moderate load, such as a direct drive robot on the level.voidsetSpeed(int speed)Sets desired motor speed , in degrees per second; The maximum reliably sustainable velocity is 100 x battery voltage under moderate load, such as a direct drive robot on the level.voidsetStallThreshold(int error, int time)Set the parameters for detecting a stalled motor.voidstartSynchronization()Begin a set of synchronized motor operationsvoidstop()Causes motor to stop, pretty much instantaneously.voidstop(boolean immediateReturn)Causes motor to stop, pretty much instantaneously.booleansuspendRegulation()Removes this motor from the motor regulation system.voidsynchronizeWith(RegulatedMotor[] syncList)Specify a set of motors that should be kept in synchronization with this one.voidwaitComplete()Wait until the current movement operation is complete (this can include the motor stalling).-
Methods inherited from class lejos.hardware.Device
releaseOnClose
-
-
-
-
Field Detail
-
MAX_SPEED_AT_9V
protected final int MAX_SPEED_AT_9V
-
NO_LIMIT
protected static final int NO_LIMIT
- See Also:
- Constant Field Values
-
reg
protected final MotorRegulator reg
-
tachoPort
protected TachoMotorPort tachoPort
-
speed
protected float speed
-
acceleration
protected int acceleration
-
-
Constructor Detail
-
BaseRegulatedMotor
public BaseRegulatedMotor(TachoMotorPort port, MotorRegulator regulator, int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset, int maxSpeed)
Use this constructor to assign a variable of type motor connected to a particular port.- Parameters:
port- to which this motor is connected
-
BaseRegulatedMotor
public BaseRegulatedMotor(Port port, MotorRegulator regulator, int typ, float moveP, float moveI, float moveD, float holdP, float holdI, float holdD, int offset, int maxSpeed)
Use this constructor to assign a variable of type motor connected to a particular port.- Parameters:
port- to which this motor is connected
-
-
Method Detail
-
close
public void close()
Close the motor regulator. Release the motor from regulation and free any associated resources.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceRegulatedMotor- Overrides:
closein classDevice
-
suspendRegulation
public boolean suspendRegulation()
Removes this motor from the motor regulation system. After this call the motor will be in float mode and will have stopped. Note calling any of the high level move operations (forward, rotate etc.), will automatically enable regulation.- Returns:
- true iff regulation has been suspended.
-
getTachoCount
public int getTachoCount()
Description copied from interface:EncoderReturns the tachometer count.- Specified by:
getTachoCountin interfaceEncoder- Returns:
- the current tachometer count.
- See Also:
Encoder.getTachoCount()
-
getPosition
public float getPosition()
Returns the current position that the motor regulator is trying to maintain. Normally this will be the actual position of the motor and will be the same as the value returned by getTachoCount(). However in some circumstances (activeMotors that are in the process of stalling, or activeMotors that have been forced out of position), the two values may differ. Note that if regulation has been suspended calling this method will restart it.- Returns:
- the current position calculated by the regulator.
-
forward
public void forward()
Description copied from interface:BaseMotorCauses motor to rotate forward untilstop()orflt()is called.- Specified by:
forwardin interfaceBaseMotor- See Also:
BasicMotor.forward()
-
backward
public void backward()
Description copied from interface:BaseMotorCauses motor to rotate backwards untilstop()orflt()is called.- Specified by:
backwardin interfaceBaseMotor- See Also:
BasicMotor.backward()
-
flt
public void flt()
Set the motor into float mode. This will stop the motor without braking and the position of the motor will not be maintained.
-
flt
public void flt(boolean immediateReturn)
Set the motor into float mode. This will stop the motor without braking and the position of the motor will not be maintained.- Specified by:
fltin interfaceRegulatedMotor- Parameters:
immediateReturn- If true do not wait for the motor to actually stop
-
stop
public void stop()
Causes motor to stop, pretty much instantaneously. In other words, the motor doesn't just stop; it will resist any further motion. Cancels any rotate() orders in progress
-
stop
public void stop(boolean immediateReturn)
Causes motor to stop, pretty much instantaneously. In other words, the motor doesn't just stop; it will resist any further motion. Cancels any rotate() orders in progress- Specified by:
stopin interfaceRegulatedMotor- Parameters:
immediateReturn- if true do not wait for the motor to actually stop
-
isMoving
public boolean isMoving()
This method returns true if the motor is attempting to rotate. The return value may not correspond to the actual motor movement.
For example, If the motor is stalled, isMoving() will return true.
After flt() is called, this method will return false even though the motor axle may continue to rotate by inertia. If the motor is stalled, isMoving() will return true. . A stall can be detected by callingisStalled();
-
waitComplete
public void waitComplete()
Wait until the current movement operation is complete (this can include the motor stalling).- Specified by:
waitCompletein interfaceRegulatedMotor
-
rotateTo
public void rotateTo(int limitAngle, boolean immediateReturn)Description copied from interface:RegulatedMotorcauses motor to rotate to limitAngle;
if immediateReturn is true, method returns immediately and the motor stops by itself
and getTachoCount should be within +- 2 degrees if the limit angle If any motor method is called before the limit is reached, the rotation is canceled. When the angle is reached, the method isMoving() returns false;- Specified by:
rotateToin interfaceRegulatedMotor- Parameters:
limitAngle- to which the motor will rotate, and then stop (in degrees). Includes any positive or negative int, even values > 360.immediateReturn- iff true, method returns immediately, thus allowing monitoring of sensors in the calling thread.
-
setSpeed
public void setSpeed(int speed)
Sets desired motor speed , in degrees per second; The maximum reliably sustainable velocity is 100 x battery voltage under moderate load, such as a direct drive robot on the level.- Specified by:
setSpeedin interfaceRegulatedMotor- Parameters:
speed- value in degrees/sec
-
setSpeed
public void setSpeed(float speed)
Sets desired motor speed , in degrees per second; The maximum reliably sustainable velocity is 100 x battery voltage under moderate load, such as a direct drive robot on the level.- Parameters:
speed- value in degrees/sec
-
setAcceleration
public void setAcceleration(int acceleration)
sets the acceleration rate of this motor in degrees/sec/sec
The default value is 6000; Smaller values will make speeding up. or stopping at the end of a rotate() task, smoother;- Specified by:
setAccelerationin interfaceRegulatedMotor- Parameters:
acceleration-
-
getAcceleration
public int getAcceleration()
returns acceleration in degrees/second/second- Returns:
- the value of acceleration
-
getLimitAngle
public int getLimitAngle()
Return the angle that this Motor is rotating to.- Specified by:
getLimitAnglein interfaceRegulatedMotor- Returns:
- angle in degrees
-
resetTachoCount
public void resetTachoCount()
Reset the tachometer associated with this motor. Note calling this method will cause any current move operation to be halted.- Specified by:
resetTachoCountin interfaceEncoder
-
addListener
public void addListener(RegulatedMotorListener listener)
Add a motor listener. Move operations will be reported to this object.- Specified by:
addListenerin interfaceRegulatedMotor- Parameters:
listener-
-
removeListener
public RegulatedMotorListener removeListener()
Description copied from interface:RegulatedMotorRemoves the RegulatedMotorListener from this class.- Specified by:
removeListenerin interfaceRegulatedMotor- Returns:
- The RegulatedMotorListener that was removed, if any. Null if none existed.
-
rotate
public void rotate(int angle, boolean immediateReturn)Rotate by the request number of degrees.- Specified by:
rotatein interfaceRegulatedMotor- Parameters:
angle- number of degrees to rotate relative to the current positionimmediateReturn- if true do not wait for the move to complete- See Also:
RegulatedMotor.rotate(int, boolean)
-
rotate
public void rotate(int angle)
Rotate by the requested number of degrees. Wait for the move to complete.- Specified by:
rotatein interfaceRegulatedMotor- Parameters:
angle-
-
rotateTo
public void rotateTo(int limitAngle)
Rotate to the target angle. Do not return until the move is complete.- Specified by:
rotateToin interfaceRegulatedMotor- Parameters:
limitAngle- Angle to rotate to.
-
getSpeed
public int getSpeed()
Return the current target speed.- Specified by:
getSpeedin interfaceRegulatedMotor- Returns:
- the current target speed.
-
lock
@Deprecated public void lock(int power)
Deprecated.The regulator will always try to hold position unless the motor is set into float mode using flt().- Parameters:
power- - a value between 1 and 100;
-
isStalled
public boolean isStalled()
Return true if the motor is currently stalled.- Specified by:
isStalledin interfaceRegulatedMotor- Returns:
- true if the motor is stalled, else false
-
setStallThreshold
public void setStallThreshold(int error, int time)Set the parameters for detecting a stalled motor. A motor will be recognised as stalled if the movement error (the amount the motor lags the regulated position) is greater than error for a period longer than time.- Specified by:
setStallThresholdin interfaceRegulatedMotor- Parameters:
error- The error thresholdtime- The time that the error threshold needs to be exceeded for.
-
getRotationSpeed
public int getRotationSpeed()
Return the current velocity.- Specified by:
getRotationSpeedin interfaceTachometer- Returns:
- current velocity in degrees/s
-
getMaxSpeed
public float getMaxSpeed()
Description copied from interface:RegulatedMotorReturns the maximum speed that can be maintained by the regulation system based upon the current state of the battery.- Specified by:
getMaxSpeedin interfaceRegulatedMotor- Returns:
- the maximum speed of the Motor in degrees per second.
-
synchronizeWith
public void synchronizeWith(RegulatedMotor[] syncList)
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:
synchronizeWithin interfaceRegulatedMotor- Parameters:
syncList- an array of motors to synchronize with.
-
startSynchronization
public void startSynchronization()
Begin a set of synchronized motor operations- Specified by:
startSynchronizationin interfaceRegulatedMotor
-
endSynchronization
public void endSynchronization()
Complete a set of synchronized motor operations.- Specified by:
endSynchronizationin interfaceRegulatedMotor
-
-