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.
pid.cpp
- Committer:
- jasonliujc
- Date:
- 2015-12-04
- Revision:
- 5:0975b797bf54
- Parent:
- 4:d59328f14363
- Child:
- 6:95722ada4706
File content as of revision 5:0975b797bf54:
//#include "pid.h" // //void PIDController::onLoop() //{ // float error = (m_error)(); // (m_out)(m_Kp * error + m_Ki * iController(error) + m_Kd * dController(error)); // wait(m_dt); //} // //float PIDController::iController(float error) //{ // m_integral += error; // return m_integral; //} // //float PIDController::dController(float error) //{ // float derivative = error - m_pError; // m_pError = error; // return derivative; //} // //void PIDController::reset() //{ // m_integral = 0; //} #include "pid.h" Timer timer; void ctrl::setKp(float num){ kp=num; } void ctrl::setKi(float num){ ki=num; } void ctrl::setKd(float num){ kd=num; } void ctrl::setErr(float num){ error=num; } void ctrl::setprevErr(float num){ prevErr=num; } void ctrl::setCorrection(float num){ correction=num; } float ctrl::pctrl(){ correction=kp*error; return correction; } float ctrl::dctrl(){ float dErr=error-prevErr; float dt=timer.read_us(); timer.reset(); setprevErr(error); correction=kd*dErr/dt; return correction; } float ctrl::ictrl(){ floategrater+=error; correction=ki*floategrater; floategrater/=decay; return correction; } float ctrl::total(){ return pctrl()+dctrl()+ictrl(); } void ctrl::adjust(float xSpeed,float ySpeed,float L, float R){ TErr=xSpeed-(L+R); RErr=rotationError(); } float ctrl::getT(){ return TErr; } float ctrl::getR(){ return RErr; }