Kosaka Lab / Mbed 2 deprecated BLDCmotor

Dependencies:   QEI mbed-rtos mbed

Fork of BLDCmotor by manabu kosaka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers controller.h Source File

controller.h

00001 #ifndef __controller_h
00002 #define __controller_h
00003 
00004 //#define PI 3.14159265358979 // def. of PI
00005 /*********** User setting for control parameters (begin) ***************/
00006 //#define SIMULATION          // Comment this line if not simulation
00007 //#define USE_CURRENT_CONTROL // Current control on. Comment if current control off.
00008 #define DEADZONE_PLUS   1.  // deadzone of plus side
00009 #define DEADZONE_MINUS -1.5 // deadzone of minus side
00010     // encoder
00011 #define N_ENC   (360*4)     // "*4": QEI::X4_ENCODING. Number of pulses in one revolution(=360 deg) of rotary encoder.
00012 //#define N_ENC   (24*4)     // "*4": QEI::X4_ENCODING. Number of pulses in one revolution(=360 deg) of rotary encoder.
00013 #define CH_A    p29         // A phase port
00014 #define CH_B    p30         // A phase port
00015 
00016 #define DA_PORT p18           // analog out (DA) port of mbed
00017 
00018 #define PWM_FREQ 1000.0  //[Hz], pwm freq. (> 1/(DEAD_TIME*10))
00019 #define DEADTIME    0.0001  // [s], deadtime to be set between plus volt. to/from minus
00020 #define TS0     0.001//08//8      // [s], sampling time (priority highest: Ticker IRQ) of motor current i control PID using timer interrupt
00021 #define TS1     0.002//0.01       // [s], sampling time (priority high: RtosTimer) of motor angle th PID using rtos-timer
00022 #define TS2     0.2        // [s], sampling time (priority =main(): precision 4ms) to save data to PC using thread. But, max data length is 1000.
00023 #define TS3     0.002        // [s], sampling time (priority low: precision 4ms)
00024 #define TS4     0.2         // [s], sampling time (priority lowest: precision 4ms)  to display data to PC tera term
00025 //void    timerTS1(void const *argument), CallTimerTS3(void const *argument), CallTimerTS4(void const *argument);
00026 //    RtosTimer RtosTimerTS1(timerTS1);  // RtosTimer priority is osPriorityAboveNormal, just one above main()
00027 //    Thread ThreadTimerTS3(CallTimerTS3,NULL,osPriorityBelowNormal);
00028 //    Thread ThreadTimerTS4(CallTimerTS4,NULL,osPriorityLow);
00029 #define TMAX    15          // [s], experiment starts from 0[s] to TMAX[s]
00030 #define TMAX_FIND_ORIGIN    9//0.1//1.0          // [s], finding th origin starts from 0[s] to TMAX[s]
00031 
00032     // 電流制御マイナーループ
00033 #define iKPd    10./2     // 電流制御d軸PIDのPゲイン (d-axis)
00034 #define iKId    100./2    // 電流制御d軸PIDのIゲイン (d-axis)
00035 #define iKDd    0         // 電流制御d軸PIDのDゲイン (d-axis)
00036 #define iKPq    10./2     // 電流制御q軸PIDのPゲイン (q-axis)
00037 #define iKIq    100./2    // 電流制御q軸PIDのIゲイン (q-axis)
00038 #define iKDq    0         // 電流制御q軸PIDのDゲイン (q-axis)
00039 
00040 #define vdqMAX  3.3     // Vcc
00041 #define SQRvdqMAX (vdqMAX*vdqMAX)   // [V^2] vdqの大きさの最大値の二乗
00042 
00043     // 速度制御メインループ
00044 #ifdef USE_CURRENT_CONTROL
00045  #define wKp 0.05        // 速度制御PIDのPゲイン
00046  #define wKi 2.50        // 速度制御PIDのIゲイン
00047  #define wKd 0           // 速度制御PIDのDゲイン
00048 #else
00049  #define wKp 0.05       // 速度制御PIDのPゲイン
00050  #define wKi 0.05         // 速度制御PIDのIゲイン
00051 // #define wKp 0.005       // 速度制御PIDのPゲイン
00052 // #define wKi 0.2         // 速度制御PIDのIゲイン
00053  #define wKd 0           // 速度制御PIDのDゲイン
00054 #endif
00055 
00056 #define iLPF    0.99//0.95//0.9     // 0-1, 速度に対する1次LPF; Low Pass Filter, G(z)=(1-a)/(z-a)
00057 #define iqMAX   4//100       // [A], q軸電流指令のMAX制限(異常に大きい指令値を制限する)
00058 
00059 /*********** User setting for control parameters (end) ***************/
00060 
00061 
00062 typedef struct struct_motor_parameters{
00063     // モータの定数、信号など
00064  #ifdef SIMULATION // シミュレーションのとき
00065     float  Ld;     // [H], d軸インダクタンス
00066     float  Lq;     // [H], q軸インダクタンス
00067     float  Lq0;    // 磁気飽和を考慮 (Lq = Lq0 - Lq1*iq)
00068     float  Lq1;    //
00069     float  R;      // [Ω], モータ各相巻線抵抗
00070     float  phi;    // [V s], 永久磁石の鎖交磁束
00071     float  Jm;     // [Nms^2], イナーシャ
00072     float  Tm;     // [Nm], モータトルク
00073     float  TL;     // [Nm], 負荷トルク
00074  #endif
00075     float  th[2];  // [rad],   ロータの位置, th[0]=th(t), th[1]=th(t-TS0)
00076     float  th_const;// [rad],   ロータの位置 with constant angular velosity
00077     float  w;      // [rad/s], モータ速度
00078     float  w_lpf;  // [rad/s], フィルタで高周波ノイズを除去したモータ速度
00079     float  iab[2]; // [A], αβ軸電流 iab = [iα;iβ];
00080     float  idq[2]; // [A], dq軸電流 idq = [id;iq];
00081     float  vab[2]; // [V], αβ軸電圧 vab = [vα;vβ];
00082     float  vuvw[3];// [V], UVW相電圧 vuvw = [vu;vv;vw];
00083     float  iuvw[3];// [A], UVW相電流 iuvw = [iu;iv;iw];
00084     float  p;      // 極対数
00085     float  Cuvw[2][3]; // UVW座標からαβ座標への変換行列Cuvw
00086 }motor_parameters;
00087 
00088 typedef struct struct_current_loop_parameters{
00089     // 電流制御マイナーループの定数、変数
00090     float  idq_ref[2]; // idqの目標値
00091     float  vdq_ref[2]; // vdqの目標値
00092     float  eI_idq[2];  // 電流制御用偏差の積分値(積分項)
00093     float  e_old[2];   // 電流制御用偏差の1サンプル過去の値
00094 }current_loop_parameters;
00095 
00096 typedef struct struct_velocity_loop_parameters{
00097     // 速度制御メインループの定数、変数
00098     float  w_lpf;          // [rad/s], モータ速度(LPF通過後)
00099     float  w_ref;          // [rad/s], モータ目標速度
00100     float  tan_beta_ref;   // [rad],   モータ電流位相
00101     float  iq_ref;         // q軸電流指令[A]
00102     float  eI;             // 速度制御用偏差の積分値(積分項)
00103     float  e_old;          // 速度制御用偏差の1サンプル過去の値
00104 }velocity_loop_parameters;
00105 
00106 extern void timerTS0();    // timer called every TS0[s].
00107 extern void timerTS1(void const *argument);    // timer called every TS1[s].
00108 extern void timerTS2();    // timer called every TS2[s].
00109 extern void timerTS3();    // timer called every TS3[s].
00110 extern void timerTS4();    // timer called every TS4[s].
00111 
00112 extern void init_parameters();    // IPMSMの機器定数等の設定, 制御器の初期化
00113 
00114 extern unsigned long _count;   // sampling number
00115 extern float   _time;          // time[s]
00116 
00117 extern unsigned short  f_find_origin;  // flag to find the origin of the rotor angle theta
00118 
00119 extern motor_parameters            p;  // モータの定数、信号など
00120 extern current_loop_parameters     il; // 電流制御マイナーループの定数、変数
00121 extern velocity_loop_parameters    vl; // 速度制御メインループの定数、変数
00122 
00123 extern float data[][5];    // memory to save data offline instead of "online fprintf".
00124 extern unsigned short _count_data;  // counter for data[1000][5]
00125 extern float   debug[20];           // for debug
00126 #endif