Tekkotsu Homepage | Demos | Overview | Downloads | Dev. Resources | Reference | Credits |
MotorControllers.ccGo to the documentation of this file.00001 #ifdef HAVE_BULLET 00002 00003 #include "MotorControllers.h" 00004 00005 void ProportionalMotorController::updateControllerResponse(float dt) { 00006 float x = constraint.getValue(dt); 00007 float response = 0; 00008 if(dt>0) { 00009 float err = tgt-x; // find error for PID 00010 response = p * err; 00011 } 00012 constraint.applyResponse(dt, response); 00013 } 00014 00015 void PIDMotorController::updateControllerResponse(float dt) { 00016 float x = constraint.getValue(dt); 00017 if(velocity && dt>0) 00018 x = (x-lastX)/dt; 00019 float scTgt = tgt*scale; // scale target domain (e.g. convert wheel rim dist/s to axle rad/s) 00020 00021 float response = 0; 00022 00023 if(verbose.size()>0) 00024 std::cout << verbose << " sctgt=" << scTgt << " cur=" << x; 00025 00026 // PID calculation 00027 if(dt>0) { 00028 float err = scTgt-x; // find error for PID 00029 00030 if(i==0) { // if i is 0, don't track sumErr 00031 sumErr = 0; 00032 } else { 00033 sumErr += (err+lastErr)/2 * dt; 00034 response += i * sumErr; 00035 } 00036 00037 if(d==0) { // if d is 0, don't track derr and avgDerr 00038 avgDerr = 0; 00039 } else { 00040 float derr = (err - lastErr)/dt; 00041 avgDerr = avgDerr*derrGamma + derr*(1-derrGamma); 00042 response += d * avgDerr; 00043 } 00044 00045 response = p * ( err + response ); // proportional control, proportional also applies to accumulated 'id' error correction (PID "standard form") 00046 lastErr = err; 00047 if(verbose.size()>0) 00048 std::cout << " err=" << err << " sumErr=" << sumErr << " avgDerr=" << avgDerr; 00049 } 00050 00051 response += scTgt * linearff; // apply feed forward term 00052 00053 // response has to clear minResponse, if so apply punch 00054 if(std::abs(response) < minResponse) { 00055 response = 0; 00056 } else { 00057 if(response>0) 00058 response = response - minResponse + punch; 00059 else 00060 response = response + minResponse - punch; 00061 } 00062 00063 // response is clipped to maxResponse 00064 if(maxResponse<0) { 00065 // no-op 00066 } else if(std::abs(response) > maxResponse) { 00067 if(response>0) 00068 response = maxResponse; 00069 else 00070 response = -maxResponse; 00071 } 00072 00073 if(friction!=0 && dt>0) { 00074 float spd = velocity ? x : (x - lastX) / dt; 00075 response -= spd*friction; 00076 if(verbose.size()>0) 00077 std::cout << " spd="<<spd; 00078 } 00079 00080 if(verbose.size()>0) 00081 std::cout << " response="<<response <<std::endl; 00082 lastX = x; 00083 constraint.applyResponse(dt, response); 00084 } 00085 00086 #endif 00087 00088 00089 /*! @file 00090 * @brief 00091 * @author Ethan Tira-Thompson (ejt) (Creator) 00092 */ |
Tekkotsu v5.1CVS |
Generated Mon May 9 04:58:45 2016 by Doxygen 1.6.3 |