Skip to content

Commit 894c689

Browse files
author
年迈的老秋风Windy
committed
ServoUsingMotor Impl
1 parent 92cbbae commit 894c689

File tree

5 files changed

+122
-13
lines changed

5 files changed

+122
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public interface Encoder {
77
EncoderTypeInstance getEncoderType();
8-
int getCurrentTick();
8+
long getCurrentTick();
99
Angle getCurrentAngularSpeed();
1010
double getCurrentTicksPerSecond();
1111
boolean isDirectionReversed();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public enum RunMode{
88
PIDF_WITH_SPEED,
99
NO_PIDF_WITH_SPEED
1010
}
11-
int getTargetPositionTick();
12-
void setTargetPositionTick(int targetPositionTick);
11+
long getTargetPositionTick();
12+
void setTargetPositionTick(long targetPositionTick);
1313
RunMode getCurrentRunMode();
1414
void setCurrentRunMode(RunMode mode);
1515
PIDFCoefficients getSpeedPIDFCoefficients();

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class PIDFMotorImpl implements PIDFMotor, AsyncDevice {
1111
public SimpleMotor motor;
1212
public Encoder encoder;
13-
private int targetPosition = 0;
13+
private long targetPosition = 0;
1414
private RunMode currentRunMode = RunMode.NO_PIDF_WITH_SPEED;
1515
private double currentPower = 0.0;
1616
private PIDFController SpeedPIDFController = new PIDFController(new PIDFCoefficients());
@@ -31,12 +31,12 @@ public PIDFMotorImpl(PIDFMotorImpl motorImpl){
3131
this.errorToleranceTicks = motorImpl.errorToleranceTicks;
3232
}
3333
@Override
34-
public int getTargetPositionTick() {
34+
public long getTargetPositionTick() {
3535
return this.targetPosition;
3636
}
3737

3838
@Override
39-
public void setTargetPositionTick(int targetPositionTick) {
39+
public void setTargetPositionTick(long targetPositionTick) {
4040
this.targetPosition = targetPositionTick;
4141
}
4242

@@ -103,7 +103,7 @@ public EncoderTypeInstance getEncoderType() {
103103
}
104104

105105
@Override
106-
public int getCurrentTick() {
106+
public long getCurrentTick() {
107107
if(this.encoder == null){
108108
return 0;
109109
}else{
@@ -198,6 +198,12 @@ public boolean isBusy() {
198198

199199
@Override
200200
public void update() {
201+
if(this.motor instanceof AsyncDevice){
202+
((AsyncDevice) this.motor).update();
203+
}
204+
if(this.encoder instanceof AsyncDevice){
205+
((AsyncDevice) this.encoder).update();
206+
}
201207
if(!this.isBusy()){
202208
this.stop();
203209
}
@@ -239,7 +245,7 @@ protected void applyEncoderPower(double currentTPS, double maxTPS, double power)
239245
this.motor.setPower(pidfPower);
240246
}
241247

242-
protected void applyPIDFToPosition(int currentPosition, int targetPosition, double currentTPS, double maxTPS, double power){
248+
protected void applyPIDFToPosition(long currentPosition, long targetPosition, double currentTPS, double maxTPS, double power){
243249
if(this.motor == null){
244250
return;
245251
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.darbots.corebotlib.hardware.typedef.instances.ServoTypeInstance;
44

55
public interface Servo {
6+
public static interface ServoPositionReachedCallback{
7+
void positionReached(Servo servo, double targetPosition, double actualPosition);
8+
}
69
/**
710
* Get last set position
811
* @return Last set position in range of [-1,1]
Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,131 @@
11
package org.darbots.corebotlib.hardware;
22

3+
import org.darbots.corebotlib.calculations.Range;
4+
import org.darbots.corebotlib.hardware.typedef.instances.CRServoTypeInstance;
35
import org.darbots.corebotlib.hardware.typedef.instances.ServoTypeInstance;
46

57
public class ServoUsingMotor implements Servo, AsyncDevice {
6-
8+
private double targetPosition;
9+
private long encoderTickAtZeroPos;
10+
private PIDFMotor motor;
11+
private double targetPower;
12+
private double maximumPosition;
13+
private double minimumPosition;
14+
15+
public ServoUsingMotor(PIDFMotor motor, double targetPower, double minPosition, double maxPosition, double initialPosition){
16+
this.motor = motor;
17+
this.motor.setCurrentRunMode(PIDFMotor.RunMode.PIDF_TO_POSITION);
18+
this.setTargetPower(targetPower);
19+
this.minimumPosition = minPosition;
20+
this.maximumPosition = maxPosition;
21+
this.calibrateCurrentPosition(initialPosition);
22+
this.targetPosition = initialPosition;
23+
}
24+
25+
public ServoUsingMotor(ServoUsingMotor servoUsingMotor){
26+
this.motor = servoUsingMotor.motor;
27+
this.targetPosition = servoUsingMotor.targetPosition;
28+
this.encoderTickAtZeroPos = servoUsingMotor.encoderTickAtZeroPos;
29+
this.targetPower = servoUsingMotor.targetPower;
30+
this.maximumPosition = servoUsingMotor.maximumPosition;
31+
this.minimumPosition = servoUsingMotor.minimumPosition;
32+
}
33+
34+
public double getPositionTolerance(){
35+
return ((double) this.motor.getPositionErrorToleranceTicks()) / this.motor.getEncoderType().getTicksPerRev();
36+
}
37+
38+
public void setPositionTolerance(double tolerance){
39+
this.motor.setPositionErrorToleranceTicks((int) Math.round(tolerance * this.motor.getEncoderType().getTicksPerRev()));
40+
}
41+
42+
public PIDFMotor getMotor(){
43+
return this.motor;
44+
}
45+
46+
public double getTargetPower(){
47+
return this.targetPower;
48+
}
49+
50+
public void setTargetPower(double power){
51+
this.targetPower = Range.clip(Math.abs(power),0.0,1.0);
52+
this.motor.setPower(this.targetPower);
53+
}
54+
755
@Override
856
public double getTargetPosition() {
9-
return 0;
57+
return this.targetPosition;
58+
}
59+
60+
public void calibrateCurrentPosition(double currentPosition){
61+
long currentEncoderCount = this.motor.getCurrentTick();
62+
long deltaEncoderCount = Math.round(currentPosition * this.motor.getEncoderType().getTicksPerRev());
63+
this.encoderTickAtZeroPos = currentEncoderCount - deltaEncoderCount;
1064
}
1165

66+
public long targetPositionToTicks(double targetPosition){
67+
return Math.round(targetPosition * motor.getEncoderType().getTicksPerRev()) + encoderTickAtZeroPos;
68+
}
69+
70+
public double encoderTicksToTargetPosition(long ticks){
71+
return (ticks - encoderTickAtZeroPos) / motor.getEncoderType().getTicksPerRev();
72+
}
73+
74+
1275
@Override
1376
public void setTargetPosition(double targetPosition) {
77+
this.motor.setTargetPositionTick(this.targetPositionToTicks(targetPosition));
78+
this.targetPosition = targetPosition;
79+
}
1480

81+
public void setTargetPosition(double targetPosition, double targetPower){
82+
this.setTargetPower(targetPower);
83+
this.setTargetPosition(targetPosition);
1584
}
1685

1786
@Override
1887
public ServoTypeInstance getServoType() {
19-
return null;
88+
return new ServoTypeInstance(
89+
"ServoUsingMotor",
90+
"IntegratedSensors",
91+
1500,
92+
2500,
93+
this.getMaxAngleRange(),
94+
CRServoTypeInstance.getSecondsToTurn60Deg(this.motor.getMotorType().getMaximumRPM()),
95+
this.motor.getMotorType().getMinimumVoltage(),
96+
this.motor.getMotorType().getMaximumVoltage()
97+
);
2098
}
2199

22100
@Override
23101
public boolean isBusy() {
24-
return false;
102+
return motor.isBusy();
103+
}
104+
105+
public double getMaxAngleRange(){
106+
return (this.maximumPosition - this.minimumPosition) * 360.0;
107+
}
108+
109+
public double getMaximumPosition(){
110+
return this.maximumPosition;
111+
}
112+
113+
public void setMaximumPosition(double maximumPosition){
114+
this.maximumPosition = maximumPosition;
115+
}
116+
117+
public double getMinimumPosition(){
118+
return this.minimumPosition;
119+
}
120+
121+
public void setMinimumPosition(double minimumPosition){
122+
this.minimumPosition = minimumPosition;
25123
}
26124

27125
@Override
28126
public void update() {
29-
127+
if(motor instanceof AsyncDevice){
128+
((AsyncDevice) motor).update();
129+
}
30130
}
31131
}

0 commit comments

Comments
 (0)