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.
Dependents: Tourobo2022_TBCMotorDriver
Pid.cpp
- Committer:
- YutaTogashi
- Date:
- 2020-03-25
- Revision:
- 5:a3aa705d9023
- Parent:
- 4:3c2651359136
- Child:
- 6:4074aded9b9d
File content as of revision 5:a3aa705d9023:
#include "Pid.h"
void Pid::setup(float Kp,float Ki,float Kd,short PidMode,float period) {
KP = Kp;
KI = Ki;
KD = Kd;
MODE = PidMode;
PERIOD = period;
upperLimit = 1.0f;
fallLimit = -1.0f;
//pid.attach(this,&Pid::calculate,period);
}
void Pid::setupLimit(float UpperLimit,float FallLimit) {
upperLimit = UpperLimit;
fallLimit = FallLimit;
}
void Pid::calculate(float targetValue,float nowValue,bool enableErrorIntegration) {
switch(MODE) {
case 0:
before = now;
now = targetValue - nowValue;
p = now;
if(enableErrorIntegration) i += (now + before) * PERIOD / 2; //積分
else i = 0.0f;
d = (now - before) / PERIOD; //微分
//target_duty = p * KP + i * KI + d * KD;
duty = p * KP + i * KI + d * KD;
break;
case 1:
e2 = e1;
e1 = e;
e = targetValue - nowValue;
//p = (e - e1) / PERIOD;
p = e / PERIOD;
i = e;
d = (e - 2*e1 + e2)/PERIOD;
duty += p * KP + i * KI + d * KD; //dutyは微分されたものなので、積分してあげる(もとに戻す)
break;
default:
break;
}
if(duty > upperLimit) {
duty = upperLimit;
} else if (duty < fallLimit) {
duty = fallLimit;
}
};
void Pid::reset() {
duty = 0.0f;
}
float Pid::getDuty() {
return duty;
}