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