|
1 | 1 | package frc.utility; |
2 | 2 |
|
| 3 | +import static edu.wpi.first.units.Units.Degrees; |
| 4 | + |
3 | 5 | import com.revrobotics.servohub.ServoChannel; |
4 | 6 | import com.revrobotics.servohub.ServoHub; |
5 | 7 | import com.revrobotics.servohub.ServoChannel.ChannelId; |
6 | 8 |
|
| 9 | +import edu.wpi.first.units.measure.Angle; |
| 10 | + |
7 | 11 | public class ServoEx { |
| 12 | + private final boolean isEnabled; |
8 | 13 | private final ServoChannel channel; |
9 | | - private double min,max; |
| 14 | + private double minPulse,maxPulse,centerPulse; |
| 15 | + private double maxDegree,degRange; |
| 16 | + private boolean centerZeroPoint = false; |
| 17 | + |
10 | 18 |
|
11 | | - private ServoEx(ServoChannel channel) { |
| 19 | + private ServoEx(ServoChannel channel, boolean isEnabled) { |
12 | 20 | this.channel=channel; |
| 21 | + this.isEnabled=isEnabled; |
| 22 | + |
| 23 | + this.channel.setPowered(this.isEnabled); |
| 24 | + this.channel.setEnabled(this.isEnabled); |
13 | 25 | } |
14 | 26 |
|
15 | | - // public void setDegrees(double deg) { |
16 | | - // deg = Math.max(0, Math.min(degRange, deg)); |
17 | | - // double norm = deg / degRange; |
18 | | - // double pulseUs = min + norm * (max - min); |
19 | | - // channel.setPulseWidth((int)pulseUs); |
20 | | - // } |
21 | | - |
22 | | - public ServoEx create(ServoHub hub,int channelId) { |
23 | | - new ServoEx(hub.getServoChannel(ChannelId.fromInt(channelId))); |
| 27 | + /** |
| 28 | + * @param hub the ServoHub object to initialize the servo channel with |
| 29 | + * @param channelId the ID of the channel that the servo is connected to on the servo hub |
| 30 | + * @return a ServoEx object |
| 31 | + */ |
| 32 | + public ServoEx create(ServoHub hub,int channelId, boolean isEnabled) { |
| 33 | + new ServoEx(hub.getServoChannel(ChannelId.fromInt(channelId)),isEnabled); |
24 | 34 | return this; |
25 | 35 | } |
26 | 36 |
|
27 | 37 | public ServoEx withMinPulseWidth(double value) { |
28 | | - min=value; |
| 38 | + minPulse=value; |
29 | 39 | return this; |
30 | 40 | } |
31 | 41 | public ServoEx withMaxPulseWidth(double value) { |
32 | | - max=value; |
| 42 | + maxPulse=value; |
33 | 43 | return this; |
34 | 44 | } |
| 45 | + |
| 46 | + /** |
| 47 | + * Used to set the maximum degree of rotation supported by the servo for use in pulse width calculations. |
| 48 | + * Do not call this method when using a servo with a center zero point, as it will throw an UnsupportedOperationException. |
| 49 | + * @param value maximum degree of rotation |
| 50 | + */ |
| 51 | + public ServoEx withDegRange(double value) { |
| 52 | + if(centerZeroPoint){ |
| 53 | + throw new UnsupportedOperationException("Cannot use this method with a center zero point!"); |
| 54 | + } else { |
| 55 | + degRange=value; |
| 56 | + } |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Used to set the pulse width that represents the zero point on a servo with a center zero point. |
| 62 | + * @param value pulse width, in microseconds |
| 63 | + */ |
| 64 | + public ServoEx withCenterPulseWidth(double value) { |
| 65 | + centerPulse=value; |
| 66 | + centerZeroPoint=true; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Used when the servo has a center zero point. |
| 72 | + * An example is the REV SRS with -135 deg as min, 0 deg as center, and 135 deg as max. |
| 73 | + * The value would be set to 135 in this case. |
| 74 | + * @param value the max degrees of rotation in one direction from center |
| 75 | + */ |
| 76 | + public ServoEx withMaxDegree(double value) { |
| 77 | + maxDegree=value; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public void setDegrees(double deg) { |
| 82 | + if(centerZeroPoint){ |
| 83 | + // Clamp to range -maxDegree .. +maxDegree |
| 84 | + deg = Math.max(-maxDegree, Math.min(maxDegree, deg)); |
| 85 | + |
| 86 | + double pulse; |
| 87 | + if (deg >= 0) { |
| 88 | + pulse = centerPulse + (deg / maxDegree) * (maxPulse - centerPulse); |
| 89 | + } else { |
| 90 | + pulse = centerPulse + (deg / maxDegree) * (centerPulse - minPulse); |
| 91 | + } |
| 92 | + |
| 93 | + channel.setPulseWidth((int)pulse); |
| 94 | + } else { |
| 95 | + deg = Math.max(0, Math.min(degRange, deg)); |
| 96 | + double norm = deg / degRange; |
| 97 | + double pulse = minPulse + norm * (maxPulse - minPulse); |
| 98 | + |
| 99 | + channel.setPulseWidth((int)pulse); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void setRadians(double rad) { |
| 104 | + setDegrees(Math.toDegrees(rad)); |
| 105 | + } |
| 106 | + |
| 107 | + public void setAngle(Angle angle) { |
| 108 | + setDegrees(angle.in(Degrees)); |
| 109 | + } |
35 | 110 | } |
0 commit comments