Skip to content

Commit ba8b8e5

Browse files
2 parents c79a821 + 12b3b5c commit ba8b8e5

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/main/java/org/darbots/corebotlib/hardware/ServoUsingMotor.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ public class ServoUsingMotor implements Servo, AsyncDevice {
1111
private double targetPower;
1212
private double maximumPosition;
1313
private double minimumPosition;
14+
public Servo.ServoPositionReachedCallback positionReachedCallback = null;
15+
private boolean lastIsBusy = false;
1416

1517
public ServoUsingMotor(PIDFMotor motor, double targetPower, double minPosition, double maxPosition, double initialPosition){
16-
this.motor = motor;
18+
this.setMotor(motor);
1719
this.motor.setCurrentRunMode(PIDFMotor.RunMode.PIDF_TO_POSITION);
1820
this.setTargetPower(targetPower);
1921
this.minimumPosition = minPosition;
@@ -23,7 +25,7 @@ public ServoUsingMotor(PIDFMotor motor, double targetPower, double minPosition,
2325
}
2426

2527
public ServoUsingMotor(ServoUsingMotor servoUsingMotor){
26-
this.motor = servoUsingMotor.motor;
28+
this.setMotor(servoUsingMotor.motor);
2729
this.targetPosition = servoUsingMotor.targetPosition;
2830
this.encoderTickAtZeroPos = servoUsingMotor.encoderTickAtZeroPos;
2931
this.targetPower = servoUsingMotor.targetPower;
@@ -43,6 +45,11 @@ public PIDFMotor getMotor(){
4345
return this.motor;
4446
}
4547

48+
public void setMotor(PIDFMotor motor){
49+
this.motor = motor;
50+
this.lastIsBusy = motor.isBusy();
51+
}
52+
4653
public double getTargetPower(){
4754
return this.targetPower;
4855
}
@@ -52,6 +59,13 @@ public void setTargetPower(double power){
5259
this.motor.setPower(this.targetPower);
5360
}
5461

62+
public double getCurrentPosition(){
63+
long currentEncoderCount = this.motor.getCurrentTick();
64+
long deltaEncoderCount = currentEncoderCount - this.encoderTickAtZeroPos;
65+
double currentPosition = deltaEncoderCount / this.motor.getEncoderType().getTicksPerRev();
66+
return currentPosition;
67+
}
68+
5569
@Override
5670
public double getTargetPosition() {
5771
return this.targetPosition;
@@ -71,11 +85,12 @@ public double encoderTicksToTargetPosition(long ticks){
7185
return (ticks - encoderTickAtZeroPos) / motor.getEncoderType().getTicksPerRev();
7286
}
7387

74-
7588
@Override
7689
public void setTargetPosition(double targetPosition) {
90+
targetPosition = Range.clip(targetPosition,this.minimumPosition,this.maximumPosition);
7791
this.motor.setTargetPositionTick(this.targetPositionToTicks(targetPosition));
7892
this.targetPosition = targetPosition;
93+
this.lastIsBusy = true;
7994
}
8095

8196
public void setTargetPosition(double targetPosition, double targetPower){
@@ -127,5 +142,15 @@ public void update() {
127142
if(motor instanceof AsyncDevice){
128143
((AsyncDevice) motor).update();
129144
}
145+
{
146+
boolean currentIsBusy = this.motor.isBusy();
147+
if (lastIsBusy && (!currentIsBusy)) {
148+
if (this.positionReachedCallback != null) {
149+
this.positionReachedCallback.positionReached(this, this.targetPosition, this.getCurrentPosition());
150+
this.positionReachedCallback = null;
151+
}
152+
}
153+
lastIsBusy = currentIsBusy;
154+
}
130155
}
131156
}

0 commit comments

Comments
 (0)