@@ -11,9 +11,11 @@ public class ServoUsingMotor implements Servo, AsyncDevice {
11
11
private double targetPower ;
12
12
private double maximumPosition ;
13
13
private double minimumPosition ;
14
+ public Servo .ServoPositionReachedCallback positionReachedCallback = null ;
15
+ private boolean lastIsBusy = false ;
14
16
15
17
public ServoUsingMotor (PIDFMotor motor , double targetPower , double minPosition , double maxPosition , double initialPosition ){
16
- this .motor = motor ;
18
+ this .setMotor ( motor ) ;
17
19
this .motor .setCurrentRunMode (PIDFMotor .RunMode .PIDF_TO_POSITION );
18
20
this .setTargetPower (targetPower );
19
21
this .minimumPosition = minPosition ;
@@ -23,7 +25,7 @@ public ServoUsingMotor(PIDFMotor motor, double targetPower, double minPosition,
23
25
}
24
26
25
27
public ServoUsingMotor (ServoUsingMotor servoUsingMotor ){
26
- this .motor = servoUsingMotor .motor ;
28
+ this .setMotor ( servoUsingMotor .motor ) ;
27
29
this .targetPosition = servoUsingMotor .targetPosition ;
28
30
this .encoderTickAtZeroPos = servoUsingMotor .encoderTickAtZeroPos ;
29
31
this .targetPower = servoUsingMotor .targetPower ;
@@ -43,6 +45,11 @@ public PIDFMotor getMotor(){
43
45
return this .motor ;
44
46
}
45
47
48
+ public void setMotor (PIDFMotor motor ){
49
+ this .motor = motor ;
50
+ this .lastIsBusy = motor .isBusy ();
51
+ }
52
+
46
53
public double getTargetPower (){
47
54
return this .targetPower ;
48
55
}
@@ -52,6 +59,13 @@ public void setTargetPower(double power){
52
59
this .motor .setPower (this .targetPower );
53
60
}
54
61
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
+
55
69
@ Override
56
70
public double getTargetPosition () {
57
71
return this .targetPosition ;
@@ -71,11 +85,12 @@ public double encoderTicksToTargetPosition(long ticks){
71
85
return (ticks - encoderTickAtZeroPos ) / motor .getEncoderType ().getTicksPerRev ();
72
86
}
73
87
74
-
75
88
@ Override
76
89
public void setTargetPosition (double targetPosition ) {
90
+ targetPosition = Range .clip (targetPosition ,this .minimumPosition ,this .maximumPosition );
77
91
this .motor .setTargetPositionTick (this .targetPositionToTicks (targetPosition ));
78
92
this .targetPosition = targetPosition ;
93
+ this .lastIsBusy = true ;
79
94
}
80
95
81
96
public void setTargetPosition (double targetPosition , double targetPower ){
@@ -127,5 +142,15 @@ public void update() {
127
142
if (motor instanceof AsyncDevice ){
128
143
((AsyncDevice ) motor ).update ();
129
144
}
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
+ }
130
155
}
131
156
}
0 commit comments