Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {
id "com.github.johnrengelman.shadow" version "4.0.3"
}

version = '2.6.0-SNAPSHOT'
version = '2.6.1-SNAPSHOT'

repositories {
jcenter()
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/ev3dev/actuators/lego/motors/BaseRegulatedMotor.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public abstract class BaseRegulatedMotor extends EV3DevMotorDevice implements Re
private int speed = 360;
protected int acceleration = 6000;

// State
private boolean regulationFlag = true;
private boolean inversePolarity = false;

private final List<RegulatedMotorListener> listenerList;

Expand Down Expand Up @@ -114,7 +116,9 @@ public boolean suspendRegulation() {
* @see lejos.robotics.RegulatedMotor#getTachoCount()
*/
public int getTachoCount() {
return getIntegerAttribute(POSITION);
int signCorrection = inversePolarity ? -1 : 1;

return getIntegerAttribute(POSITION) * signCorrection;
}

/**
Expand All @@ -132,7 +136,8 @@ public float getPosition() {

@Override
public void forward() {
this.setStringAttribute(POLARITY, POLARITY_NORMAL);
setPolarityInversion(false);

this.setSpeed(this.speed);
if (!this.regulationFlag) {
this.setStringAttribute(COMMAND, RUN_DIRECT);
Expand All @@ -147,7 +152,8 @@ public void forward() {

@Override
public void backward(){
this.setStringAttribute(POLARITY, POLARITY_INVERSED);
setPolarityInversion(true);

this.setSpeed(this.speed);
if (!this.regulationFlag) {
this.setStringAttribute(COMMAND, RUN_DIRECT);
Expand Down Expand Up @@ -403,4 +409,16 @@ public void endSynchronization() {
log.warn("Not implemented the method: endSynchronization");
}

/**
* Configure whether the motor polarity will be inverted or not.
*
* This sets the "inversePolarity" cache variable & it also
* sets the corresponding motor attribute.
*
* This will affect how the motor parameters are
*/
private void setPolarityInversion(boolean inverted) {
this.inversePolarity = inverted;
this.setStringAttribute(POLARITY, inverted ? POLARITY_INVERSED : POLARITY_NORMAL);
}
}