Skip to content

Commit

Permalink
- added PIDController
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghozti committed Jul 4, 2023
1 parent 1513e16 commit 83c7360
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 5 deletions.
6 changes: 3 additions & 3 deletions core/src/pibotlib/graphics/DriverStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.badlogic.gdx.utils.ScreenUtils;
import pibotlib.graphics.utils.Font;
import pibotlib.lib.addons.TimedRobotBase;
import pibotlib.lib.addons.sensors.DisplayAbleDevice;
import pibotlib.lib.addons.sensors.DisplayAble;
import pibotlib.lib.constants.Constants;
import pibotlib.graphics.utils.DriverStationState;
import pibotlib.lib.gamecontrollers.LocalXboxController;
Expand All @@ -23,7 +23,7 @@ public class DriverStation implements Screen {
com.badlogic.gdx.math.Rectangle mouseHitbox;
Font font, versionFont, interfaceFont;
LocalXboxController controller;
static ArrayList<DisplayAbleDevice> sensors = new ArrayList<>();
static ArrayList<DisplayAble> sensors = new ArrayList<>();
TimedRobotBase robot;
boolean robotStarted;

Expand Down Expand Up @@ -194,7 +194,7 @@ private void updateTeleopButton(){

/**
*adds a sensor to the graphical driver station. NOTE: only 3 sensors are allowed at a time*/
public static void addSensor(DisplayAbleDevice sensor){
public static void addSensor(DisplayAble sensor){
if (sensors.size() == 3){
System.out.println("Driver station sensor capacity full");
}else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pibotlib.lib.addons.sensors;

public interface DisplayAbleDevice {
public interface DisplayAble {

String getValueToString();

Expand Down
2 changes: 1 addition & 1 deletion core/src/pibotlib/lib/addons/sensors/UltraSonicSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/**
*Used for the HC-SR04 sensor or any other ultra-sonic sensor that works like it*/
public class UltraSonicSensor implements Runnable, Sensor{
public class UltraSonicSensor implements Runnable, DisplayAble {

DigitalOutput sensorTriggerPin;
DigitalInput sensorEchoPin;
Expand Down
145 changes: 145 additions & 0 deletions core/src/pibotlib/lib/pid/PIDController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package pibotlib.lib.pid;

import pibotlib.lib.addons.sensors.DisplayAble;
import pibotlib.lib.time.ElapseTimer;

public class PIDController implements DisplayAble {

final double kP;
final double kI;
final double kD;

final double iLimit;
double maxTolerance;
double minTolerance;
double target;
double maxTarget;
double minTarget;

volatile double error = 0;
volatile double output = 0;
volatile double errorSum = 0;
volatile double errorRate = 0;
volatile double lastError = 0;
volatile double lastTimeStamp = 0;
volatile double currentTime = 0;
volatile double sensorValue;

volatile boolean greaterThanMax = false;
volatile boolean lowerThanMin = false;
volatile boolean isOnTarget = false;

boolean autoKill;

ElapseTimer timer;

public PIDController(double kP, double kI, double kD, double iLimit, double maxTolerance, double minTolerance, double target){
this.kP = kP;
this.kI = kI;
this.kD = kD;
this.iLimit = iLimit;
this.maxTolerance = maxTolerance;
this.minTolerance = minTolerance;
this.target = target;
minTarget = target - minTolerance;
maxTarget = target + maxTolerance;
timer = new ElapseTimer();
}

public PIDController(double kP, double kI, double kD, double iLimit, double maxTolerance, double minTolerance, double target, boolean autoKill){
this.kP = kP;
this.kI = kI;
this.kD = kD;
this.iLimit = iLimit;
this.maxTolerance = maxTolerance;
this.minTolerance = minTolerance;
this.target = target;
minTarget = target - minTolerance;
maxTarget = target + maxTolerance;
this.autoKill = autoKill;
timer = new ElapseTimer();
}

public double getOutput(double sensorValue){
timer.startTimer();
currentTime = timer.getElapsedMilliseconds() - lastTimeStamp;
if(sensorValue > target){
error = target - sensorValue;
greaterThanMax = true;
lowerThanMin = false;
}

if(sensorValue < target){
error = target - sensorValue;
greaterThanMax = false;
lowerThanMin = true;
}

if(Math.abs(error) < iLimit && Math.round(error) != 0){
errorSum += error * currentTime;
}

errorRate = (error - lastError)/currentTime;
lastTimeStamp = timer.getElapsedMilliseconds();
lastError = error;

isOnTarget = maxTarget >= sensorValue && minTarget <= sensorValue;

if(isOnTarget && autoKill){
error = 0;
errorSum = 0;
errorRate = 0;
timer.reset();
timer.stopTimer();
return 0;
}
return (kP * error) + (kI * errorSum) + (kD * errorRate);//only PI for now
}

public void pause(){
error = 0;
errorSum = 0;
errorRate = 0;
timer.reset();
timer.stopTimer();
}

public void setTarget(double minTolerance, double maxTolerance, double target){
this.target = target;
this.maxTolerance = maxTarget;
this.minTolerance = minTolerance;
this.maxTarget = target + maxTolerance;
this.minTarget = target - minTolerance;
}

public double getP(){return kP;}
public double getI(){return kI;}
public double getD(){return kD;}

public double getILimit(){return iLimit;}

public double getMaxTolerance(){return maxTolerance;}
public double getMinTolerance(){return minTolerance;}

public synchronized double getError(){return error;}
public synchronized double getErrorSum(){return errorSum;}

public synchronized double getLastTimeStamp(){return lastTimeStamp;}
public synchronized double getCurrentTime(){return currentTime;}

public synchronized double getSensorValue(){return sensorValue;}

public synchronized boolean isGreaterThanMax(){return greaterThanMax;}
public synchronized boolean isLowerThanMin(){return lowerThanMin;}
public synchronized boolean isOnTarget(){return isOnTarget;}

@Override
public String getValueToString() {
return null;
}

@Override
public String getName() {
return "PID Controller";
}
}

0 comments on commit 83c7360

Please sign in to comment.