Motor current controller

Fork of CURRENT_CONTROL by LDSC_Robotics_TAs

Committer:
adam_z
Date:
Fri Apr 22 16:29:35 2016 +0000
Revision:
3:c787d1c5ad6a
Parent:
2:562bd14dfd3a
Child:
4:1a6ba05e7736
basically works

Who changed what in which revision?

UserRevisionLine numberNew 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 3:c787d1c5ad6a 13 pid(Kp,Ki,Kd,samplingTime),
adam_z 3:c787d1c5ad6a 14 lpFilter(samplingTime)
adam_z 3:c787d1c5ad6a 15
adam_z 0:955aa05c968a 16 {
adam_z 1:c5973a56d474 17 pwmIndex_ = pwmIndex;
adam_z 1:c5973a56d474 18
adam_z 0:955aa05c968a 19 Ts = samplingTime;
adam_z 0:955aa05c968a 20 //setup motor PWM parameters
adam_z 1:c5973a56d474 21 MotorPlus.period_us(50);//default period equals to 50 microseconds
adam_z 1:c5973a56d474 22 MotorPlus.write(0.5); //duty ratio = 0.5 in complementary output mode -> static
adam_z 1:c5973a56d474 23 if(pwmIndex_ == PWM1)TIM1->CCER |= 4;
adam_z 1:c5973a56d474 24 else if(pwmIndex_ == PWM2)TIM1->CCER |= 64; //enable complimentary output
adam_z 3:c787d1c5ad6a 25
adam_z 3:c787d1c5ad6a 26
adam_z 3:c787d1c5ad6a 27
adam_z 0:955aa05c968a 28 }
adam_z 0:955aa05c968a 29
adam_z 1:c5973a56d474 30 void CURRENT_CONTROL::Control(float curRef)
adam_z 0:955aa05c968a 31 {
adam_z 2:562bd14dfd3a 32 analogInValue = currentAnalogIn.read();
adam_z 3:c787d1c5ad6a 33 curFeedBack = lpFilter.filter((analogInValue - currentOffset)*analog2Cur, 10*3.14159);
adam_z 0:955aa05c968a 34 pid.Compute(curRef, curFeedBack);
adam_z 0:955aa05c968a 35 MotorPlus = 0.5 - pid.output;
adam_z 1:c5973a56d474 36 if(pwmIndex_ == PWM1)TIM1->CCER |= 4;
adam_z 1:c5973a56d474 37 else if(pwmIndex_ == PWM2)TIM1->CCER |= 64;
adam_z 1:c5973a56d474 38 }
adam_z 1:c5973a56d474 39
adam_z 1:c5973a56d474 40 void CURRENT_CONTROL::SetAnalog2Cur(float ratio)//LTS25-NP current sensor working with STM32F446RE : 3.3*8/0.6
adam_z 1:c5973a56d474 41 {
adam_z 1:c5973a56d474 42 analog2Cur = ratio;
adam_z 1:c5973a56d474 43 }
adam_z 3:c787d1c5ad6a 44
adam_z 3:c787d1c5ad6a 45
adam_z 2:562bd14dfd3a 46 //****************for test************************//
adam_z 1:c5973a56d474 47 void CURRENT_CONTROL::SetPWMDuty(float ratio)
adam_z 1:c5973a56d474 48 {
adam_z 1:c5973a56d474 49 MotorPlus = ratio;
adam_z 1:c5973a56d474 50 if(pwmIndex_ == PWM1)TIM1->CCER |= 4;
adam_z 1:c5973a56d474 51 else if(pwmIndex_ == PWM2)TIM1->CCER |= 64;
adam_z 2:562bd14dfd3a 52 }
adam_z 2:562bd14dfd3a 53
adam_z 2:562bd14dfd3a 54 float CURRENT_CONTROL::GetAnalogIn(void)
adam_z 2:562bd14dfd3a 55 {
adam_z 2:562bd14dfd3a 56 return analogInValue = currentAnalogIn.read();
adam_z 2:562bd14dfd3a 57 }
adam_z 2:562bd14dfd3a 58
adam_z 2:562bd14dfd3a 59 float CURRENT_CONTROL::GetCurrent(void)
adam_z 2:562bd14dfd3a 60 {
adam_z 3:c787d1c5ad6a 61 return curFeedBack;
adam_z 3:c787d1c5ad6a 62 }
adam_z 3:c787d1c5ad6a 63
adam_z 3:c787d1c5ad6a 64 //=====================LPF ====================//
adam_z 3:c787d1c5ad6a 65 LPF::LPF(float samplingTime)
adam_z 3:c787d1c5ad6a 66 {
adam_z 3:c787d1c5ad6a 67 Ts = samplingTime;
adam_z 3:c787d1c5ad6a 68 }
adam_z 3:c787d1c5ad6a 69
adam_z 3:c787d1c5ad6a 70 float LPF::filter(float input, float cutOff)
adam_z 3:c787d1c5ad6a 71 {
adam_z 3:c787d1c5ad6a 72 output = (outputLast + cutOff*Ts)/(1 + cutOff*Ts);
adam_z 3:c787d1c5ad6a 73 outputLast = output;
adam_z 3:c787d1c5ad6a 74 return output;
adam_z 0:955aa05c968a 75 }