|
1 | 1 | package org.darbots.corebotlib.hardware; |
2 | 2 |
|
| 3 | +import org.darbots.corebotlib.calculations.Range; |
| 4 | +import org.darbots.corebotlib.hardware.typedef.instances.CRServoTypeInstance; |
3 | 5 | import org.darbots.corebotlib.hardware.typedef.instances.ServoTypeInstance; |
4 | 6 |
|
5 | 7 | 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 | + |
7 | 55 | @Override |
8 | 56 | 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; |
10 | 64 | } |
11 | 65 |
|
| 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 | + |
12 | 75 | @Override |
13 | 76 | public void setTargetPosition(double targetPosition) { |
| 77 | + this.motor.setTargetPositionTick(this.targetPositionToTicks(targetPosition)); |
| 78 | + this.targetPosition = targetPosition; |
| 79 | + } |
14 | 80 |
|
| 81 | + public void setTargetPosition(double targetPosition, double targetPower){ |
| 82 | + this.setTargetPower(targetPower); |
| 83 | + this.setTargetPosition(targetPosition); |
15 | 84 | } |
16 | 85 |
|
17 | 86 | @Override |
18 | 87 | 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 | + ); |
20 | 98 | } |
21 | 99 |
|
22 | 100 | @Override |
23 | 101 | 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; |
25 | 123 | } |
26 | 124 |
|
27 | 125 | @Override |
28 | 126 | public void update() { |
29 | | - |
| 127 | + if(motor instanceof AsyncDevice){ |
| 128 | + ((AsyncDevice) motor).update(); |
| 129 | + } |
30 | 130 | } |
31 | 131 | } |
0 commit comments