kani
Dependencies: 2017NHKpin_config FEP omni_wheel PID R1307 ikarashiMDC
\ ̄\ / ̄/ /l \ \ / / lヽ | ヽ ヽ | | / / | \ ` ‐ヽ ヽ ● ● / / ‐ / \ __ l | ||___|| / l __ / \ \ / \/ /\| 人__人 |/\ <ズワイガニ //\| |/\\ //\| ケガニ |/\\ / . \_____/ \ ┏┓ ┏━┓┏┓ ┏┓ ┏┓┏┓ ┏┓ ┏┓┗┛ ┏┓ ┗┓┃┗┛ ┏┛┗━┓ ┃┃┃┃ ┃┃┏━┛┗┓┏┓┏┛┗━┓┃┃┏┓┏┓┏━━━┓ ┗┓┏━┛ ┃┃┗┛ ┃┃┗━┓┏┛┗┛┗┓┏┓┃┗┛┗┛┃┃┗━━━┛ ┏┛┃┏━┓┃┗━━┓┃┃┏━┛┗┓ ┏┛┃┃┃ ┃┃ ┃┏┛┗━┛┗━━┓┃┃┃┃┏┓┏┛ ┗━┛┃┃ ┃┃┏┓ ┃┃┏━━┓┏━━┛┃┃┃┃┗┛┃ ┏┛┃ ┃┃┃┗━━┓ ┗┛┗━━┛┗━━━┛┗┛┗━━┛ ┗━┛ ┗┛┗━━━┛
bot/PIDcontroller/PID_controller.cpp@6:fe9767a50891, 2017-09-06 (annotated)
- Committer:
- uchitake
- Date:
- Wed Sep 06 18:39:19 2017 +0900
- Revision:
- 6:fe9767a50891
- Parent:
- 5:16ea97725085
- Child:
- 17:79fa65706f92
fix coding
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 |
6:fe9767a50891 | 11 | axisOffSetDegree(0), |
uchitake |
6:fe9767a50891 | 12 | planeOffSetDegree(0), |
uchitake |
1:845af5425eec | 13 | turnOverNumber(0), |
uchitake |
1:845af5425eec | 14 | beforeDegree(0), |
uchitake |
6:fe9767a50891 | 15 | rawDegree(0), |
uchitake |
6:fe9767a50891 | 16 | calculationResult(0), |
uchitake |
6:fe9767a50891 | 17 | axisCurrentDegree(0), |
uchitake |
6:fe9767a50891 | 18 | planeCurrentDegree(0) |
uchitake |
1:845af5425eec | 19 | { |
uchitake |
1:845af5425eec | 20 | PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT); |
uchitake |
1:845af5425eec | 21 | PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT); |
uchitake |
1:845af5425eec | 22 | PID::setBias(BIAS); |
uchitake |
1:845af5425eec | 23 | PID::setMode(AUTO_MODE); |
uchitake |
1:845af5425eec | 24 | PID::setSetPoint(0.0); |
uchitake |
1:845af5425eec | 25 | |
uchitake |
5:16ea97725085 | 26 | wait(0.1); |
uchitake |
1:845af5425eec | 27 | HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20); |
uchitake |
5:16ea97725085 | 28 | wait(0.1); |
uchitake |
1:845af5425eec | 29 | rawDegree = HMC6352::sample(); |
uchitake |
5:16ea97725085 | 30 | beforeDegree = rawDegree; |
uchitake |
6:fe9767a50891 | 31 | planeOffSetDegree = rawDegree; |
uchitake |
6:fe9767a50891 | 32 | axisOffSetDegree = rawDegree; |
uchitake |
1:845af5425eec | 33 | // this -> attach(this, &PIDC::updateOutput, INTERVAL); |
uchitake |
1:845af5425eec | 34 | } |
uchitake |
1:845af5425eec | 35 | |
uchitake |
1:845af5425eec | 36 | PIDC::PIDC(PinName sda, PinName scl, float kc, float ti, float td, float interval) : |
uchitake |
1:845af5425eec | 37 | PID(kc, ti, td, interval), |
uchitake |
1:845af5425eec | 38 | HMC6352(sda, scl), |
uchitake |
6:fe9767a50891 | 39 | axisOffSetDegree(0), |
uchitake |
6:fe9767a50891 | 40 | planeOffSetDegree(0), |
uchitake |
1:845af5425eec | 41 | turnOverNumber(0), |
uchitake |
1:845af5425eec | 42 | beforeDegree(0), |
uchitake |
6:fe9767a50891 | 43 | rawDegree(0), |
uchitake |
6:fe9767a50891 | 44 | calculationResult(0), |
uchitake |
6:fe9767a50891 | 45 | axisCurrentDegree(0), |
uchitake |
6:fe9767a50891 | 46 | planeCurrentDegree(0) |
uchitake |
1:845af5425eec | 47 | { |
uchitake |
1:845af5425eec | 48 | PID::setInputLimits(-INPUT_LIMIT, INPUT_LIMIT); |
uchitake |
1:845af5425eec | 49 | PID::setOutputLimits(-OUTPUT_LIMIT, OUTPUT_LIMIT); |
uchitake |
1:845af5425eec | 50 | PID::setBias(BIAS); |
uchitake |
1:845af5425eec | 51 | PID::setMode(AUTO_MODE); |
uchitake |
1:845af5425eec | 52 | PID::setSetPoint(0.0); |
uchitake |
1:845af5425eec | 53 | |
uchitake |
5:16ea97725085 | 54 | wait(0.1); |
uchitake |
1:845af5425eec | 55 | HMC6352::setOpMode(HMC6352_CONTINUOUS, 1, 20); |
uchitake |
5:16ea97725085 | 56 | wait(0.1); |
uchitake |
1:845af5425eec | 57 | rawDegree = HMC6352::sample(); |
uchitake |
5:16ea97725085 | 58 | beforeDegree = rawDegree; |
uchitake |
6:fe9767a50891 | 59 | planeOffSetDegree = rawDegree; |
uchitake |
6:fe9767a50891 | 60 | axisOffSetDegree = rawDegree; |
uchitake |
1:845af5425eec | 61 | // this -> attach(this, &PIDC::updateOutput, INTERVAL); |
uchitake |
1:845af5425eec | 62 | } |
uchitake |
1:845af5425eec | 63 | |
uchitake |
1:845af5425eec | 64 | |
uchitake |
1:845af5425eec | 65 | void PIDC::confirm() |
uchitake |
1:845af5425eec | 66 | { |
uchitake |
1:845af5425eec | 67 | rawDegree = HMC6352::sample(); |
uchitake |
6:fe9767a50891 | 68 | if(rawDegree - beforeDegree < -SENSED_THRESHOLD) ++turnOverNumber; |
uchitake |
6:fe9767a50891 | 69 | if(rawDegree - beforeDegree > SENSED_THRESHOLD) --turnOverNumber; |
uchitake |
6:fe9767a50891 | 70 | axisCurrentDegree = rawDegree - axisOffSetDegree + turnOverNumber * 3600; |
uchitake |
6:fe9767a50891 | 71 | planeCurrentDegree = rawDegree - planeOffSetDegree + turnOverNumber * 3600; |
uchitake |
1:845af5425eec | 72 | beforeDegree = HMC6352::sample(); |
uchitake |
6:fe9767a50891 | 73 | PID::setProcessValue(axisCurrentDegree / 10.0); |
uchitake |
6:fe9767a50891 | 74 | calculationResult = PID::compute(); |
uchitake |
1:845af5425eec | 75 | } |
uchitake |
1:845af5425eec | 76 | |
uchitake |
6:fe9767a50891 | 77 | float PIDC::getCalculationResult() const |
uchitake |
1:845af5425eec | 78 | { |
uchitake |
6:fe9767a50891 | 79 | return calculationResult; |
uchitake |
1:845af5425eec | 80 | } |
uchitake |
1:845af5425eec | 81 | |
uchitake |
6:fe9767a50891 | 82 | int PIDC::getCurrentDegree() const |
uchitake |
3:369d9ee17e84 | 83 | { |
uchitake |
6:fe9767a50891 | 84 | return planeCurrentDegree; |
uchitake |
3:369d9ee17e84 | 85 | } |
uchitake |
3:369d9ee17e84 | 86 | |
uchitake |
6:fe9767a50891 | 87 | void PIDC::resetAxisOffset() |
uchitake |
5:16ea97725085 | 88 | { |
uchitake |
5:16ea97725085 | 89 | beforeDegree = HMC6352::sample(); |
uchitake |
6:fe9767a50891 | 90 | axisOffSetDegree = HMC6352::sample(); |
uchitake |
5:16ea97725085 | 91 | turnOverNumber = 0; |
uchitake |
5:16ea97725085 | 92 | } |
uchitake |
5:16ea97725085 | 93 | |
uchitake |
6:fe9767a50891 | 94 | void PIDC::resetPlaneOffset() |
uchitake |
5:16ea97725085 | 95 | { |
uchitake |
5:16ea97725085 | 96 | beforeDegree = HMC6352::sample(); |
uchitake |
6:fe9767a50891 | 97 | planeOffSetDegree = HMC6352::sample(); |
uchitake |
5:16ea97725085 | 98 | turnOverNumber = 0; |
uchitake |
5:16ea97725085 | 99 | } |
uchitake |
1:845af5425eec | 100 | |
uchitake |
1:845af5425eec | 101 | void PIDC::calibration(int mode) |
uchitake |
1:845af5425eec | 102 | { |
uchitake |
1:845af5425eec | 103 | setCalibrationMode(mode); |
uchitake |
1:845af5425eec | 104 | } |