Skip to content

Commit eb50e59

Browse files
committed
servo progress
1 parent 9055496 commit eb50e59

1 file changed

Lines changed: 88 additions & 13 deletions

File tree

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,110 @@
11
package frc.utility;
22

3+
import static edu.wpi.first.units.Units.Degrees;
4+
35
import com.revrobotics.servohub.ServoChannel;
46
import com.revrobotics.servohub.ServoHub;
57
import com.revrobotics.servohub.ServoChannel.ChannelId;
68

9+
import edu.wpi.first.units.measure.Angle;
10+
711
public class ServoEx {
12+
private final boolean isEnabled;
813
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+
1018

11-
private ServoEx(ServoChannel channel) {
19+
private ServoEx(ServoChannel channel, boolean isEnabled) {
1220
this.channel=channel;
21+
this.isEnabled=isEnabled;
22+
23+
this.channel.setPowered(this.isEnabled);
24+
this.channel.setEnabled(this.isEnabled);
1325
}
1426

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);
2434
return this;
2535
}
2636

2737
public ServoEx withMinPulseWidth(double value) {
28-
min=value;
38+
minPulse=value;
2939
return this;
3040
}
3141
public ServoEx withMaxPulseWidth(double value) {
32-
max=value;
42+
maxPulse=value;
3343
return this;
3444
}
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+
}
35110
}

0 commit comments

Comments
 (0)