Skip to content

Commit

Permalink
Fix GSpeedController ramping and add voltage control
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Apr 2, 2022
1 parent ace3e2a commit 99bf909
Showing 1 changed file with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class GSpeedController implements MotorController {
private Clock mClock;
private double mLastTime;
private double mScale = 1.0;
private boolean mUseVoltageControl = false;
private double mMaxVoltage = 12.0;
private double mResetDuration = Double.POSITIVE_INFINITY;

/**
* A speed controller with added functionality
Expand Down Expand Up @@ -134,23 +137,51 @@ public GSpeedController(MotorController speedController, DistanceSensor distance
mDistancePID = distancePID;
mVelocityPID = velocityPID;
mClock = clock;
mLastTime = mClock.getTime();
mLastTime = 0.0;
}

@Override
public void set(double speed) {
if (mSecondsToFullSpeed <= 0){
mSpeedController.set(speed * mScale);
setHelper(speed);
} else {
if (mLastTime == 0.0){
mLastTime = mClock.getTime();
}
var time = mClock.getTime();
var deltaTime = time - mLastTime;
if (deltaTime > mResetDuration || deltaTime == 0.0){
deltaTime = 0.02;
}
mLastTime = time;
double maximumRate = getMaxRate(deltaTime);
var newSpeed = GMath.rateLimit(maximumRate, speed, get());
mSpeedController.set(newSpeed * mScale);
setHelper(newSpeed);
}
}

private void setHelper(double speed){
var newSpeed = speed * mScale;
if (mUseVoltageControl){
mSpeedController.setVoltage(newSpeed * mMaxVoltage);
} else {
mSpeedController.set(newSpeed);
}
}

public void useVoltageControl(double maxVoltage){
mUseVoltageControl = true;
mMaxVoltage = maxVoltage;
}

public void disableVoltageControl(){
mUseVoltageControl = false;
}

public void setResetDuration(double seconds){
mResetDuration = seconds;
}

/**
* Controls the ramping of the motor. Set to 0 to disable.
* @param secondsToFullSpeed the ramping time in seconds from 0 to full speed
Expand All @@ -173,6 +204,22 @@ public double getVelocity(){
return mVelocitySensor.getVelocity();
}

public boolean atDistanceSetpoint(){
return mDistancePID.atSetpoint();
}

public boolean atVelocitySetpoint(){
return mVelocityPID.atSetpoint();
}

public void resetDistancePID(){
mDistancePID.reset();
}

public void resetVelocityPID(){
mVelocityPID.reset();
}

/**
* @param distance the distance of the motor in encoder units
*/
Expand Down

0 comments on commit 99bf909

Please sign in to comment.