kani
Dependencies: 2017NHKpin_config FEP omni_wheel PID R1307 ikarashiMDC
\ ̄\ / ̄/ /l \ \ / / lヽ | ヽ ヽ | | / / | \ ` ‐ヽ ヽ ● ● / / ‐ / \ __ l | ||___|| / l __ / \ \ / \/ /\| 人__人 |/\ <ズワイガニ //\| |/\\ //\| ケガニ |/\\ / . \_____/ \ ┏┓ ┏━┓┏┓ ┏┓ ┏┓┏┓ ┏┓ ┏┓┗┛ ┏┓ ┗┓┃┗┛ ┏┛┗━┓ ┃┃┃┃ ┃┃┏━┛┗┓┏┓┏┛┗━┓┃┃┏┓┏┓┏━━━┓ ┗┓┏━┛ ┃┃┗┛ ┃┃┗━┓┏┛┗┛┗┓┏┓┃┗┛┗┛┃┃┗━━━┛ ┏┛┃┏━┓┃┗━━┓┃┃┏━┛┗┓ ┏┛┃┃┃ ┃┃ ┃┏┛┗━┛┗━━┓┃┃┃┃┏┓┏┛ ┗━┛┃┃ ┃┃┏┓ ┃┃┏━━┓┏━━┛┃┃┃┃┗┛┃ ┏┛┃ ┃┃┃┗━━┓ ┗┛┗━━┛┗━━━┛┗┛┗━━┛ ┗━┛ ┗┛┗━━━┛
bot/PIDcontroller/PID_controller.cpp@1:845af5425eec, 2017-09-05 (annotated)
- Committer:
- uchitake
- Date:
- Tue Sep 05 16:11:20 2017 +0900
- Revision:
- 1:845af5425eec
- Child:
- 3:369d9ee17e84
mbed-os5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uchitake |
1:845af5425eec | 1 | #include "PID_controller.h" |
uchitake |
1:845af5425eec | 2 | |
uchitake |
1:845af5425eec | 3 | void PIDC::updateOutput() |
uchitake |
1:845af5425eec | 4 | { |
uchitake |
1:845af5425eec | 5 | confirm(); |
uchitake |
1:845af5425eec | 6 | } |
uchitake |
1:845af5425eec | 7 | |
uchitake |
1:845af5425eec | 8 | PIDC::PIDC() : |
uchitake |
1:845af5425eec | 9 | PID(KC, TI, TD, INTERVAL), |
uchitake |
1:845af5425eec | 10 | HMC6352(HMCsda, HMCscl), |
uchitake |
1:845af5425eec | 11 | rawDegree(0), |
uchitake |
1:845af5425eec | 12 | offSetDegree(0), |
uchitake |
1:845af5425eec | 13 | turnOverNumber(0), |
uchitake |
1:845af5425eec | 14 | beforeDegree(0), |
uchitake |
1:845af5425eec | 15 | co(0), |
uchitake |
1:845af5425eec | 16 | processValue(0), |
uchitake |
1:845af5425eec | 17 | initDegree(0) |
uchitake |
1:845af5425eec | 18 | { |
uchitake |
1:845af5425eec | 19 | PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT); |
uchitake |
1:845af5425eec | 20 | PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT); |
uchitake |
1:845af5425eec | 21 | PID::setBias(BIAS); |
uchitake |
1:845af5425eec | 22 | PID::setMode(AUTO_MODE); |
uchitake |
1:845af5425eec | 23 | PID::setSetPoint(0.0); |
uchitake |
1:845af5425eec | 24 | |
uchitake |
1:845af5425eec | 25 | HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20); |
uchitake |
1:845af5425eec | 26 | rawDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 27 | beforeDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 28 | offSetDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 29 | initDegree = 0; |
uchitake |
1:845af5425eec | 30 | turnOverNumber = 0; |
uchitake |
1:845af5425eec | 31 | // this -> attach(this, &PIDC::updateOutput, INTERVAL); |
uchitake |
1:845af5425eec | 32 | } |
uchitake |
1:845af5425eec | 33 | |
uchitake |
1:845af5425eec | 34 | PIDC::PIDC(PinName sda, PinName scl, float kc, float ti, float td, float interval) : |
uchitake |
1:845af5425eec | 35 | PID(kc, ti, td, interval), |
uchitake |
1:845af5425eec | 36 | HMC6352(sda, scl), |
uchitake |
1:845af5425eec | 37 | rawDegree(0), |
uchitake |
1:845af5425eec | 38 | offSetDegree(0), |
uchitake |
1:845af5425eec | 39 | turnOverNumber(0), |
uchitake |
1:845af5425eec | 40 | beforeDegree(0), |
uchitake |
1:845af5425eec | 41 | co(0), |
uchitake |
1:845af5425eec | 42 | processValue(0), |
uchitake |
1:845af5425eec | 43 | initDegree(0) |
uchitake |
1:845af5425eec | 44 | { |
uchitake |
1:845af5425eec | 45 | PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT); |
uchitake |
1:845af5425eec | 46 | PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT); |
uchitake |
1:845af5425eec | 47 | PID::setBias(BIAS); |
uchitake |
1:845af5425eec | 48 | PID::setMode(AUTO_MODE); |
uchitake |
1:845af5425eec | 49 | PID::setSetPoint(0.0); |
uchitake |
1:845af5425eec | 50 | |
uchitake |
1:845af5425eec | 51 | HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20); |
uchitake |
1:845af5425eec | 52 | rawDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 53 | beforeDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 54 | offSetDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 55 | initDegree = 0; |
uchitake |
1:845af5425eec | 56 | turnOverNumber = 0; |
uchitake |
1:845af5425eec | 57 | // this -> attach(this, &PIDC::updateOutput, INTERVAL); |
uchitake |
1:845af5425eec | 58 | } |
uchitake |
1:845af5425eec | 59 | |
uchitake |
1:845af5425eec | 60 | |
uchitake |
1:845af5425eec | 61 | void PIDC::confirm() |
uchitake |
1:845af5425eec | 62 | { |
uchitake |
1:845af5425eec | 63 | rawDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 64 | if(rawDegree - beforeDegree < -1800) turnOverNumber++; |
uchitake |
1:845af5425eec | 65 | if(rawDegree - beforeDegree > 1800) turnOverNumber--; |
uchitake |
1:845af5425eec | 66 | initDegree = rawDegree - offSetDegree + turnOverNumber * 3600; |
uchitake |
1:845af5425eec | 67 | beforeDegree = HMC6352::sample(); |
uchitake |
1:845af5425eec | 68 | PID::setProcessValue(initDegree / 10.0); |
uchitake |
1:845af5425eec | 69 | co = PID::compute(); |
uchitake |
1:845af5425eec | 70 | } |
uchitake |
1:845af5425eec | 71 | |
uchitake |
1:845af5425eec | 72 | float PIDC::getCo() const |
uchitake |
1:845af5425eec | 73 | { |
uchitake |
1:845af5425eec | 74 | return co; |
uchitake |
1:845af5425eec | 75 | } |
uchitake |
1:845af5425eec | 76 | |
uchitake |
1:845af5425eec | 77 | |
uchitake |
1:845af5425eec | 78 | void PIDC::calibration(int mode) |
uchitake |
1:845af5425eec | 79 | { |
uchitake |
1:845af5425eec | 80 | setCalibrationMode(mode); |
uchitake |
1:845af5425eec | 81 | } |