@@ -23,6 +23,9 @@ public class GSpeedController implements MotorController {
2323 private Clock mClock ;
2424 private double mLastTime ;
2525 private double mScale = 1.0 ;
26+ private boolean mUseVoltageControl = false ;
27+ private double mMaxVoltage = 12.0 ;
28+ private double mResetDuration = Double .POSITIVE_INFINITY ;
2629
2730 /**
2831 * A speed controller with added functionality
@@ -134,23 +137,51 @@ public GSpeedController(MotorController speedController, DistanceSensor distance
134137 mDistancePID = distancePID ;
135138 mVelocityPID = velocityPID ;
136139 mClock = clock ;
137- mLastTime = mClock . getTime () ;
140+ mLastTime = 0.0 ;
138141 }
139142
140143 @ Override
141144 public void set (double speed ) {
142145 if (mSecondsToFullSpeed <= 0 ){
143- mSpeedController . set (speed * mScale );
146+ setHelper (speed );
144147 } else {
148+ if (mLastTime == 0.0 ){
149+ mLastTime = mClock .getTime ();
150+ }
145151 var time = mClock .getTime ();
146152 var deltaTime = time - mLastTime ;
153+ if (deltaTime > mResetDuration || deltaTime == 0.0 ){
154+ deltaTime = 0.02 ;
155+ }
147156 mLastTime = time ;
148157 double maximumRate = getMaxRate (deltaTime );
149158 var newSpeed = GMath .rateLimit (maximumRate , speed , get ());
150- mSpeedController . set (newSpeed * mScale );
159+ setHelper (newSpeed );
151160 }
152161 }
153162
163+ private void setHelper (double speed ){
164+ var newSpeed = speed * mScale ;
165+ if (mUseVoltageControl ){
166+ mSpeedController .setVoltage (newSpeed * mMaxVoltage );
167+ } else {
168+ mSpeedController .set (newSpeed );
169+ }
170+ }
171+
172+ public void useVoltageControl (double maxVoltage ){
173+ mUseVoltageControl = true ;
174+ mMaxVoltage = maxVoltage ;
175+ }
176+
177+ public void disableVoltageControl (){
178+ mUseVoltageControl = false ;
179+ }
180+
181+ public void setResetDuration (double seconds ){
182+ mResetDuration = seconds ;
183+ }
184+
154185 /**
155186 * Controls the ramping of the motor. Set to 0 to disable.
156187 * @param secondsToFullSpeed the ramping time in seconds from 0 to full speed
@@ -173,6 +204,22 @@ public double getVelocity(){
173204 return mVelocitySensor .getVelocity ();
174205 }
175206
207+ public boolean atDistanceSetpoint (){
208+ return mDistancePID .atSetpoint ();
209+ }
210+
211+ public boolean atVelocitySetpoint (){
212+ return mVelocityPID .atSetpoint ();
213+ }
214+
215+ public void resetDistancePID (){
216+ mDistancePID .reset ();
217+ }
218+
219+ public void resetVelocityPID (){
220+ mVelocityPID .reset ();
221+ }
222+
176223 /**
177224 * @param distance the distance of the motor in encoder units
178225 */
0 commit comments