Created
January 22, 2019 13:38
-
-
Save acetousk/b8e072ca6c5e606473d51be9159b78fc to your computer and use it in GitHub Desktop.
PID Help
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task leftPIDController() | |
{ | |
taskRunning=true; | |
float pidSensorCurrentValue; | |
float pidError; | |
float pidLastError; | |
float pidIntegral; | |
float pidDerivative; | |
float pidDrive; | |
// If we are using an encoder then clear it | |
if( SensorType[ LEFT_SENSOR_INDEX ] == sensorQuadEncoder ) | |
SensorValue[ LEFT_SENSOR_INDEX ] = 0; | |
// Init the variables - thanks Glenn :) | |
pidLastError = 0; | |
pidIntegral = 0; | |
while( true ){ | |
if( pidRunning ){ | |
if(pidSensorCurrentValue==pidRequestedValue){ | |
taskRunning = false; | |
stopTask(leftPIDController); | |
// Read the sensor value and scale | |
pidSensorCurrentValue = SensorValue[ LEFT_SENSOR_INDEX ] * PID_SENSOR_SCALE; | |
// calculate error | |
pidError = pidSensorCurrentValue - pidRequestedValue; | |
// integral - if Ki is not 0 | |
if( pid_Ki != 0 ) | |
{ | |
// If we are inside controlable window then integrate the error | |
if( abs(pidError) < PID_INTEGRAL_LIMIT ) | |
pidIntegral = pidIntegral + pidError; | |
else | |
pidIntegral = 0; | |
} | |
else | |
pidIntegral = 0; | |
// calculate the derivative | |
pidDerivative = pidError - pidLastError; | |
pidLastError = pidError; | |
// calculate drive | |
pidDrive = (pid_Kp * pidError) + (pid_Ki * pidIntegral) + (pid_Kd * pidDerivative); | |
// limit drive | |
if( pidDrive > PID_DRIVE_MAX ) | |
pidDrive = PID_DRIVE_MAX; | |
if( pidDrive < PID_DRIVE_MIN ) | |
pidDrive = PID_DRIVE_MIN; | |
leftFunc(pidDrive); //my left side is master | |
rightFunc(pidDrive); //my right side is slave | |
} | |
}else{ | |
// clear all | |
pidError = 0; | |
pidLastError = 0; | |
pidIntegral = 0; | |
pidDerivative = 0; | |
leftFunc(0); | |
rightFunc(0); | |
} | |
// Run at 50Hz | |
wait1Msec( 25 ); | |
} | |
} | |
void drivePID(int clicks){ | |
// send the motor off somewhere | |
pidRequestedValue= clicks*-1; | |
// start the PID task | |
startTask( leftPIDController ); | |
taskRunning=true; | |
// startTask( rightPIDController ); | |
// use joystick to modify the requested position | |
while(taskRunning){ | |
if(pidRequestedValue==clicks*-1) | |
taskRunning=false; | |
} | |
// maximum change for pidRequestedValue will be 127/4*20, around 640 counts per second | |
// free spinning motor is 100rmp so 1.67 rotations per second | |
// 1.67 * 360 counts is 600 | |
wait1Msec(20); | |
resetEncoders(); | |
stopTask(leftPIDController); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PID | |
//Drive Top Level | |
// PID using optical shaft encoder | |
// | |
// Shaft encoder has 360 pulses per revolution | |
#define PID_INTEGRAL_LIMIT 50 | |
#define PD_INTEGRAL_LIMIT 50 | |
#define LEFT_SENSOR_INDEX leftEncoder | |
#define RIGHT_SENSOR_INDEX rightEncoder | |
#define PID_SENSOR_SCALE -1 | |
#define LEFT_MOTOR_INDEX left1 | |
#define RIGHT_MOTOR_INDEX right1 | |
#define PID_MOTOR_SCALE -1 | |
#define PID_DRIVE_MAX 80 | |
#define PID_DRIVE_MIN (-80) | |
// These could be constants but leaving | |
// as variables allows them to be modified in the debugger "live" | |
//auton vars | |
//Left | |
float pid_Kp = 0.7; | |
float pid_Ki = 0.09; | |
float pid_Kd = 0.5; | |
static int pidRunning = 1; | |
static float pidRequestedValue; | |
bool taskRunning=false; | |
bool driveReverse=false; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment