Motor current controller
Fork of CURRENT_CONTROL by
CURRENT_CONTROL.cpp@1:c5973a56d474, 2016-04-22 (annotated)
- Committer:
- adam_z
- Date:
- Fri Apr 22 14:32:01 2016 +0000
- Revision:
- 1:c5973a56d474
- Parent:
- 0:955aa05c968a
- Child:
- 2:562bd14dfd3a
waiting for test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adam_z | 0:955aa05c968a | 1 | #include "mbed.h" |
adam_z | 0:955aa05c968a | 2 | #include "CURRENT_CONTROL.h" |
adam_z | 0:955aa05c968a | 3 | |
adam_z | 0:955aa05c968a | 4 | |
adam_z | 1:c5973a56d474 | 5 | CURRENT_CONTROL::CURRENT_CONTROL(PinName curChannel, |
adam_z | 1:c5973a56d474 | 6 | PinName PwmChannel1, |
adam_z | 1:c5973a56d474 | 7 | PinName PwmChannel2, |
adam_z | 1:c5973a56d474 | 8 | PWMIndex pwmIndex, |
adam_z | 1:c5973a56d474 | 9 | float Kp, float Ki, float Kd, |
adam_z | 1:c5973a56d474 | 10 | float samplingTime) : currentAnalogIn(curChannel), |
adam_z | 0:955aa05c968a | 11 | MotorPlus(PwmChannel1), |
adam_z | 0:955aa05c968a | 12 | MotorMinus(PwmChannel2), |
adam_z | 0:955aa05c968a | 13 | pid(Kp,Ki,Kd,samplingTime) |
adam_z | 0:955aa05c968a | 14 | { |
adam_z | 1:c5973a56d474 | 15 | pwmIndex_ = pwmIndex; |
adam_z | 1:c5973a56d474 | 16 | |
adam_z | 0:955aa05c968a | 17 | Ts = samplingTime; |
adam_z | 0:955aa05c968a | 18 | //setup motor PWM parameters |
adam_z | 1:c5973a56d474 | 19 | MotorPlus.period_us(50);//default period equals to 50 microseconds |
adam_z | 1:c5973a56d474 | 20 | MotorPlus.write(0.5); //duty ratio = 0.5 in complementary output mode -> static |
adam_z | 1:c5973a56d474 | 21 | if(pwmIndex_ == PWM1)TIM1->CCER |= 4; |
adam_z | 1:c5973a56d474 | 22 | else if(pwmIndex_ == PWM2)TIM1->CCER |= 64; //enable complimentary output |
adam_z | 0:955aa05c968a | 23 | |
adam_z | 0:955aa05c968a | 24 | } |
adam_z | 0:955aa05c968a | 25 | |
adam_z | 1:c5973a56d474 | 26 | void CURRENT_CONTROL::Control(float curRef) |
adam_z | 0:955aa05c968a | 27 | { |
adam_z | 1:c5973a56d474 | 28 | curFeedBack = (currentAnalogIn.read() - currentOffset)*analog2Cur; |
adam_z | 0:955aa05c968a | 29 | pid.Compute(curRef, curFeedBack); |
adam_z | 0:955aa05c968a | 30 | MotorPlus = 0.5 - pid.output; |
adam_z | 1:c5973a56d474 | 31 | if(pwmIndex_ == PWM1)TIM1->CCER |= 4; |
adam_z | 1:c5973a56d474 | 32 | else if(pwmIndex_ == PWM2)TIM1->CCER |= 64; |
adam_z | 1:c5973a56d474 | 33 | } |
adam_z | 1:c5973a56d474 | 34 | |
adam_z | 1:c5973a56d474 | 35 | void CURRENT_CONTROL::SetAnalog2Cur(float ratio)//LTS25-NP current sensor working with STM32F446RE : 3.3*8/0.6 |
adam_z | 1:c5973a56d474 | 36 | { |
adam_z | 1:c5973a56d474 | 37 | analog2Cur = ratio; |
adam_z | 1:c5973a56d474 | 38 | } |
adam_z | 1:c5973a56d474 | 39 | |
adam_z | 1:c5973a56d474 | 40 | void CURRENT_CONTROL::SetPWMDuty(float ratio) |
adam_z | 1:c5973a56d474 | 41 | { |
adam_z | 1:c5973a56d474 | 42 | MotorPlus = ratio; |
adam_z | 1:c5973a56d474 | 43 | if(pwmIndex_ == PWM1)TIM1->CCER |= 4; |
adam_z | 1:c5973a56d474 | 44 | else if(pwmIndex_ == PWM2)TIM1->CCER |= 64; |
adam_z | 0:955aa05c968a | 45 | } |