public class Motor
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
ENC_LAP_VALUE
Contains the encoder value for turning one lap
|
protected java.util.List<RangedValueListener> |
listenersWithRange
Listeners with range argument needed.
|
static int |
MAX_POWER
Contains the max power that can be set
|
static int |
MIN_POWER
Contains the min power that can be set
|
Constructor and Description |
---|
Motor() |
Modifier and Type | Method and Description |
---|---|
protected void |
addListenerWithRange(RangedValueListener listener) |
protected boolean |
encodersInitialized()
Returns true if the encoder value has been initialized.
|
double |
getDiameter()
Get the diameter (cm) configured for a wheel attached on the motor.
|
int |
getEncoderValue()
Returns a calculated value for the encoder of the motor.
|
double |
getKd()
Returns the derivative gain used for this motor's PID
|
double |
getKi()
Returns the integral gain used for this motor's PID
|
double |
getKp()
Returns the proportional gain used for this motor's PID
|
int |
getPower()
Returns the value of the power applied to the motor.
|
void |
move(double distance,
int initPower)
Rotates the motor to travel a specific distance (cm) using a PID eval loop.
The diameter of the wheel attached to the motor should be properly set before calling this method. |
java.lang.Thread |
moveNonBlocking(double distance,
int initPower)
Rotates the motor to travel a specific distance (cm) using a PID eval loop.
The diameter of the wheel attached to the motor should be properly set before calling this method. |
void |
onChange(int rangeValue,
ValueListener listener)
Adds a listener that will be triggered as soon as the encoder value
has changed for more than range value, wrt.
|
void |
onRotate(ValueListener listener)
Adds a listener that will be triggered each time a full rotation has been done.
|
void |
removeListener(RangedValueListener listener)
Removes the specified RangedValueListener if present in the listeners
|
void |
removeListener(ValueListener listener)
Removes the specified ValueListener if present in the listeners
|
void |
rotate(double nbRotations,
int initPower)
Rotate the motor for a specified number of rotations using a PID eval loop.
|
java.lang.Thread |
rotateNonBlocking(double nbRotations,
int initPower)
Rotate the motor for a specified number of rotations using a PID eval loop.
|
void |
setDiameter(double diameter)
Configure the diameter (cm) for a wheel attached on the motor
|
void |
setEncoderValue(int realEncoderValue)
Sets the value of the encoder of the motor.
|
void |
setPidParams(double kp,
double ki,
double kd)
Sets the PID gains to be used with this motor.
|
void |
setPower(int power)
Sets the power of the motor, with a value in the [-255; 255] range.
|
void |
stop()
Stops the motor.
|
java.lang.String |
toString() |
static void |
waitAllFinished(java.lang.Thread[] threads)
This utility method is used as a barrier: each Thread given in parameter must be finished, before the method stops.
This allows to have a blocking method combining non-blocking actions. |
public static final int MIN_POWER
public static final int MAX_POWER
public static final int ENC_LAP_VALUE
protected java.util.List<RangedValueListener> listenersWithRange
public double getDiameter()
public void setDiameter(double diameter)
diameter
- the diameter size in centimeterspublic double getKp()
public double getKi()
public double getKd()
public void setPidParams(double kp, double ki, double kd)
kp
- the proportional gain to use with pidki
- the integral gain to use with pidkd
- the derivative gain to use with pidpublic int getPower()
public void setPower(int power)
power
- the new power of the motor, in the [-255;255] range.public void stop()
public void setEncoderValue(int realEncoderValue)
realEncoderValue
- the new value for the encoder.protected boolean encodersInitialized()
public int getEncoderValue()
public void onChange(int rangeValue, ValueListener listener)
rangeValue
- the value range before an event is triggered.listener
- the new listener added to the list of listeners of the motor.public void onRotate(ValueListener listener)
listener
- the listener that will be triggered on a completed rotationpublic void rotate(double nbRotations, int initPower)
This method is blocking.
nbRotations
- the number of rotations to perform.initPower
- the initial power applied which is also the upper power bound used by the PID.public java.lang.Thread rotateNonBlocking(double nbRotations, int initPower)
This method is non-blocking.
nbRotations
- the number of rotations to perform.initPower
- the initial power applied which is also the upper power bound used by the PID.rotate(double, int)
public void move(double distance, int initPower)
This method is blocking.
distance
- the distance to travel, in centimeters.initPower
- the initial power applied, which is also the upper power bound used by the PID.public java.lang.Thread moveNonBlocking(double distance, int initPower)
This method is non-blocking.
distance
- the distance to travel, in centimeters.initPower
- the initial power applied, which is also the upper power bound used by the PID.public static void waitAllFinished(java.lang.Thread[] threads)
As an example, it is possible to rotate 2 motors at the same time, and wait that the rotations are finished on both motors to consider the complete action as finished.
This method is blocking.
threads
- the Threads to consider for the barrier.public void removeListener(RangedValueListener listener)
listener
- the RangedValueListener to be removedpublic void removeListener(ValueListener listener)
listener
- the ValueListener to be removedprotected void addListenerWithRange(RangedValueListener listener)
public java.lang.String toString()
toString
in class java.lang.Object