Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
PID.cpp
- Committer:
- x58alex41
- Date:
- 2017-12-01
- Revision:
- 8:45797dcd8b66
- Parent:
- 7:edd065946e9b
File content as of revision 8:45797dcd8b66:
#include "PID.h"
//led controlled
const double Kp =.00001;
const double Ki = 0;
const double Kd = 0.1;
const double decay=1;
double total_error=0;
double prev_error=0;
//error based on encoders
double PID()
{
double error = 0;
error= signed( counterM1- counterM2);
double P = Kp * error;
total_error = (total_error+error);
double I = Ki * total_error;
total_error /= decay;
double D = Kd * (error - prev_error);
prev_error = error;
return P + I+ D;
}
//error for led
const double Kp_e =.00001;
const double Ki_e = 0;
const double Kd_e = 0;
const double decay_e=1;
double total_error_e=0;
double prev_error_e=0;
double PID_e()
{
double error = 0;
error= signed ( LL_t.read_u16()-RR_t.read_u16());
double P = Kp_e * error;
total_error = (total_error+error);
double I = Ki_e * total_error;
total_error /= decay_e;
double D = Kd_e * (error - prev_error);
prev_error = error;
return (P + I+ D);
}
//opening on right side
bool opening_r(double threshold_p, double threshold_d)
{
return threshold_p < LL_t.read_u16() && prev_error > threshold_d;
}
bool opening_l(double threshold_p, double threshold_d)
{
return threshold_p < LL_t.read_u16() && prev_error > threshold_d;
}
//wall in fron the car
bool wall_front(double threshold_r, double threshold_l)
{
return threshold_r < R_t.read_u16() && threshold_l < L_t.read_u16();
}