Motor current controller

Fork of CURRENT_CONTROL by LDSC_Robotics_TAs

CURRENT_CONTROL.h

Committer:
benson516
Date:
2016-12-15
Revision:
6:bae35ca64f10
Parent:
5:7ccd2fb7ce7e
Child:
7:6794cfba3564

File content as of revision 6:bae35ca64f10:

/*
#ifndef PI
#define PI = 3.1415926
#endif
*/

#ifndef __CURRENT_CONTROL_H__
#define __CURRENT_CONTROL_H__

#include "mbed.h"
#include "PID.h"




class LPF
{public:
    float output;
    
    LPF(float samplingTime, float cutOff_freq_Hz_in); // cutOff_freq_Hz_in is in "Hz"
    float filter(float input);
    
private:
    float Ts;    
    float cutOff_freq_Hz; // Hz
    float alpha_Ts;
};


class CURRENT_CONTROL
{
public:
    typedef enum {
        PWM1,
        PWM2
    } PWMIndex;

    CURRENT_CONTROL(PinName curChannel, PinName PwmChannel1, PinName PwmChannel2, PWMIndex pwmIndex, float Kp, float Ki, float Kd, float Ka, float samplingTime);
    void SetParams(float Analog2Cur, float angSpeed2BackEmf, float voltage2DutyRatio);
    float saturation(float input_value, float &delta, const float &limit_H, const float &limit_L);
    void Control(float curRef, float speed);
        
    // Back emf as the function of rotational speed
    float func_back_emf(const float &W_in);
    
    //functions for test////////
    void ChangePwmPeriod(float microSeconds);
    void SetPWMDuty(float ratio);
    float GetAnalogIn(void);
    float GetCurrent(void);
    
    
    //////////////////////////
    PWMIndex pwmIndex_;
    PID pid;
    PwmOut MotorPlus;
    PwmOut MotorMinus;
    
    //
    float delta_output;
    float controlOutput;
    float currentOffset;
    float curFeedBack;
    
    //
    AnalogIn currentAnalogIn;
    float analogInValue;
    //
    float analog2Cur;
    float voltage2Duty;
    
    //
    float Ke;    
    
    //
    LPF lpFilter;
    

    
private:

    float Ts;
    

};


#endif