20160814

Fork of CURRENT_CONTROL by LDSC_Robotics_TAs

CURRENT_CONTROL.cpp

Committer:
adam_z
Date:
2016-04-22
Revision:
0:955aa05c968a
Child:
1:c5973a56d474

File content as of revision 0:955aa05c968a:

#include "mbed.h"
#include "CURRENT_CONTROL.h"


void CURRENT_CONTROL::CURRENT_CONTROL(PinName curChannel,
                                      PinName PwmChannel1,
                                      PinName PwmChannel2,
                                      PWMIndex pwmIndex,
                                      float Kp, float Ki, float Kd,
                                      float samplingTime):
    currentAnalogIn(curChannel),
    MotorPlus(PwmChannel1),
    MotorMinus(PwmChannel2),
    pid(Kp,Ki,Kd,samplingTime)
{
    PWMIndex = pwmIndex;
    Ts = samplingTime;
    //setup motor PWM parameters
    MotorPlus.period_us(50);//period equals to 50 microseconds
    MotorPlus.write(0.5);   //duty ratio = 0.5 in complementary output -> static
    TIM1->CCER |= 4; //enable ch1 complimentary output

}

void CURRENT_CONTROL::ControlCompute(float curRef)
{
    curFeedBack = (currentAnalogIn.read() - currentOffset)*3.3*8/0.6;
    pid.Compute(curRef, curFeedBack);
    MotorPlus = 0.5 - pid.output;
    if(PWMIndex == PWM1)TIM1->CCER |= 4;
    else if(PWMIndex == PWM2)TIM1->CCER |= 64;
}