Motor current controller

Fork of CURRENT_CONTROL by LDSC_Robotics_TAs

CURRENT_CONTROL.h

Committer:
benson516
Date:
2016-12-19
Revision:
7:6794cfba3564
Parent:
6:bae35ca64f10
Child:
8:fd6fb3cb12ec

File content as of revision 7:6794cfba3564:

/*
#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);
    void reset(float input);
    
private:
    float Ts;    
    float cutOff_freq_Hz; // Hz
    float alpha_Ts;
    
    // Flag
    bool Flag_Init;
};


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 OffsetInit(void);
    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 controlOutput;
    float currentOffset;
    float curFeedBack;
    float curFeedBack_filter;
    //
    float voltage_out;
    float delta_output;
    
    //
    AnalogIn currentAnalogIn;
    float analogInValue;
    
    // Ratio for units transformation
    float analog2Cur;
    float voltage2Duty;
    
    //
    float Ke;    
    
    //
    LPF lpFilter;
    
    // Initialization
    bool Flag_Init;
    int Init_count;
    float Accumulated_offset;

    
private:

    float Ts;
    

};


#endif