UVW 3 phases Brushless DC motor control

Dependencies:   QEI mbed-rtos mbed

Fork of BLDCmotor by manabu kosaka

Committer:
kosaka
Date:
Fri Dec 21 22:06:56 2012 +0000
Revision:
12:a4b17bb682eb
Child:
13:791e20f1af43
121222a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kosaka 12:a4b17bb682eb 1 #ifndef __controller_h
kosaka 12:a4b17bb682eb 2 #define __controller_h
kosaka 12:a4b17bb682eb 3
kosaka 12:a4b17bb682eb 4 //#define PI 3.14159265358979 // def. of PI
kosaka 12:a4b17bb682eb 5 /*********** User setting for control parameters (begin) ***************/
kosaka 12:a4b17bb682eb 6 #define SIMULATION // Comment this line if not simulation
kosaka 12:a4b17bb682eb 7 #define PWM_FREQ 1000.0 //[Hz], pwm freq. (> 1/(DEAD_TIME*10))
kosaka 12:a4b17bb682eb 8 #define DEADTIME 0.0001 // [s], deadtime to be set between plus volt. to/from minus
kosaka 12:a4b17bb682eb 9 #define USE_CURRENT_CONTROL // Current control on. Comment if current control off.
kosaka 12:a4b17bb682eb 10 #define CONTROL_MODE 0 // 0:PID control, 1:Frequency response, 2:Step response, 3. u=Rand to identify G(s), 4) FFT identification
kosaka 12:a4b17bb682eb 11 #define DEADZONE_PLUS 1. // deadzone of plus side
kosaka 12:a4b17bb682eb 12 #define DEADZONE_MINUS -1.5 // deadzone of minus side
kosaka 12:a4b17bb682eb 13 #define GOOD_DATA // Comment this line if the length of data TMAX/TS2 > 1000
kosaka 12:a4b17bb682eb 14 // encoder
kosaka 12:a4b17bb682eb 15 #define N_ENC (24*4) // "*4": QEI::X4_ENCODING. Number of pulses in one revolution(=360 deg) of rotary encoder.
kosaka 12:a4b17bb682eb 16 #define CH_A p29 // A phase port
kosaka 12:a4b17bb682eb 17 #define CH_B p30 // A phase port
kosaka 12:a4b17bb682eb 18
kosaka 12:a4b17bb682eb 19 #define DA_PORT p18 // analog out (DA) port of mbed
kosaka 12:a4b17bb682eb 20
kosaka 12:a4b17bb682eb 21 #define TS0 0.001//08//8 // [s], sampling time (priority highest: Ticker IRQ) of motor current i control PID using timer interrupt
kosaka 12:a4b17bb682eb 22 #define TS1 0.002//0.01 // [s], sampling time (priority high: RtosTimer) of motor angle th PID using rtos-timer
kosaka 12:a4b17bb682eb 23 #define TS2 0.2 // [s], sampling time (priority =main(): precision 4ms) to save data to PC using thread. But, max data length is 1000.
kosaka 12:a4b17bb682eb 24 #define TS3 0.002 // [s], sampling time (priority low: precision 4ms)
kosaka 12:a4b17bb682eb 25 #define TS4 0.1 // [s], sampling time (priority lowest: precision 4ms) to display data to PC tera term
kosaka 12:a4b17bb682eb 26 //void timerTS1(void const *argument), CallTimerTS3(void const *argument), CallTimerTS4(void const *argument);
kosaka 12:a4b17bb682eb 27 // RtosTimer RtosTimerTS1(timerTS1); // RtosTimer priority is osPriorityAboveNormal, just one above main()
kosaka 12:a4b17bb682eb 28 // Thread ThreadTimerTS3(CallTimerTS3,NULL,osPriorityBelowNormal);
kosaka 12:a4b17bb682eb 29 // Thread ThreadTimerTS4(CallTimerTS4,NULL,osPriorityLow);
kosaka 12:a4b17bb682eb 30 #define TMAX 3.0 // [s], experiment starts from 0[s] to TMAX[s]
kosaka 12:a4b17bb682eb 31
kosaka 12:a4b17bb682eb 32 // 電流制御マイナーループ
kosaka 12:a4b17bb682eb 33 #define iKPd 10./2 // 電流制御d軸PIDのPゲイン (d-axis)
kosaka 12:a4b17bb682eb 34 #define iKId 100./2 // 電流制御d軸PIDのIゲイン (d-axis)
kosaka 12:a4b17bb682eb 35 #define iKPq 10./2 // 電流制御q軸PIDのPゲイン (q-axis)
kosaka 12:a4b17bb682eb 36 #define iKIq 100./2 // 電流制御q軸PIDのIゲイン (q-axis)
kosaka 12:a4b17bb682eb 37
kosaka 12:a4b17bb682eb 38 #define vdqMAX 300.
kosaka 12:a4b17bb682eb 39 #define SQRvdqMAX (vdqMAX*vdqMAX) // [V^2] vdqの大きさの最大値の二乗
kosaka 12:a4b17bb682eb 40
kosaka 12:a4b17bb682eb 41 // 速度制御メインループ
kosaka 12:a4b17bb682eb 42 #ifdef USE_CURRENT_CONTROL
kosaka 12:a4b17bb682eb 43 #define wKp 0.05 // 速度制御PIDのPゲイン
kosaka 12:a4b17bb682eb 44 #define wKi 2.50 // 速度制御PIDのIゲイン
kosaka 12:a4b17bb682eb 45 #else
kosaka 12:a4b17bb682eb 46 #define wKp 0.005//0.05 // 速度制御PIDのPゲイン
kosaka 12:a4b17bb682eb 47 #define wKi 0.2//2.50 // 速度制御PIDのIゲイン
kosaka 12:a4b17bb682eb 48 #endif
kosaka 12:a4b17bb682eb 49
kosaka 12:a4b17bb682eb 50 #define iLPF 0.9 // 0-1, 速度に対する1次LPF; Low Pass Filter, G(z)=(1-a)/(z-a)
kosaka 12:a4b17bb682eb 51 #define iqMAX 100 // [A], q軸電流指令のMAX制限(異常に大きい指令値を制限する)
kosaka 12:a4b17bb682eb 52
kosaka 12:a4b17bb682eb 53 #define R_SHUNT 1.25 // [Ohm], shunt resistanse
kosaka 12:a4b17bb682eb 54 /*********** User setting for control parameters (end) ***************/
kosaka 12:a4b17bb682eb 55
kosaka 12:a4b17bb682eb 56
kosaka 12:a4b17bb682eb 57 typedef struct struct_motor_parameters{
kosaka 12:a4b17bb682eb 58 // モータの定数、信号など
kosaka 12:a4b17bb682eb 59 #ifdef SIMULATION // シミュレーションのとき
kosaka 12:a4b17bb682eb 60 float Ld; // [H], d軸インダクタンス
kosaka 12:a4b17bb682eb 61 float Lq; // [H], q軸インダクタンス
kosaka 12:a4b17bb682eb 62 float Lq0; // 磁気飽和を考慮 (Lq = Lq0 - Lq1*iq)
kosaka 12:a4b17bb682eb 63 float Lq1; //
kosaka 12:a4b17bb682eb 64 float R; // [Ω], モータ各相巻線抵抗
kosaka 12:a4b17bb682eb 65 float phi; // [V s], 永久磁石の鎖交磁束
kosaka 12:a4b17bb682eb 66 float Jm; // [Nms^2], イナーシャ
kosaka 12:a4b17bb682eb 67 float Tm; // [Nm], モータトルク
kosaka 12:a4b17bb682eb 68 float TL; // [Nm], 負荷トルク
kosaka 12:a4b17bb682eb 69 #endif
kosaka 12:a4b17bb682eb 70 float th[2]; // [rad]. ロータの位置, th[0]=th(t), th[1]=th(t-TS0)
kosaka 12:a4b17bb682eb 71 float w; // [rad/s], モータ速度
kosaka 12:a4b17bb682eb 72 float w_lpf; // [rad/s], フィルタで高周波ノイズを除去したモータ速度
kosaka 12:a4b17bb682eb 73 float iab[2]; // [A], αβ軸電流 iab = [iα;iβ];
kosaka 12:a4b17bb682eb 74 float idq[2]; // [A], dq軸電流 idq = [id;iq];
kosaka 12:a4b17bb682eb 75 float vab[2]; // [V], αβ軸電圧 vab = [vα;vβ];
kosaka 12:a4b17bb682eb 76 float vuvw[3];// [V], UVW相電圧 vuvw = [vu;vv;vw];
kosaka 12:a4b17bb682eb 77 float iuvw[3];// [A], UVW相電流 iuvw = [iu;iv;iw];
kosaka 12:a4b17bb682eb 78 float p; // 極対数
kosaka 12:a4b17bb682eb 79 float Cuvw[2][3]; // UVW座標からαβ座標への変換行列Cuvw
kosaka 12:a4b17bb682eb 80 }motor_parameters;
kosaka 12:a4b17bb682eb 81
kosaka 12:a4b17bb682eb 82 typedef struct struct_current_loop_parameters{
kosaka 12:a4b17bb682eb 83 // 電流制御マイナーループの定数、変数
kosaka 12:a4b17bb682eb 84 float idq_ref[2]; // idqの目標値
kosaka 12:a4b17bb682eb 85 float vdq_ref[2]; // vdqの目標値
kosaka 12:a4b17bb682eb 86 float eI_idq[2]; // 電流制御用偏差の積分値(積分項)
kosaka 12:a4b17bb682eb 87 }current_loop_parameters;
kosaka 12:a4b17bb682eb 88
kosaka 12:a4b17bb682eb 89 typedef struct struct_velocity_loop_parameters{
kosaka 12:a4b17bb682eb 90 // 速度制御メインループの定数、変数
kosaka 12:a4b17bb682eb 91 float w_lpf; // [rad/s], モータ速度(LPF通過後)
kosaka 12:a4b17bb682eb 92 float w_ref; // [rad/s], モータ目標速度
kosaka 12:a4b17bb682eb 93 float tan_beta_ref; // [rad], モータ電流位相
kosaka 12:a4b17bb682eb 94 float iq_ref; // q軸電流指令[A]
kosaka 12:a4b17bb682eb 95 float eI_w; // 速度制御用偏差の積分値(積分項)
kosaka 12:a4b17bb682eb 96 }velocity_loop_parameters;
kosaka 12:a4b17bb682eb 97
kosaka 12:a4b17bb682eb 98 extern void timerTS0(); // timer called every TS0[s].
kosaka 12:a4b17bb682eb 99 extern void timerTS1(void const *argument); // timer called every TS1[s].
kosaka 12:a4b17bb682eb 100 extern void timerTS2(); // timer called every TS2[s].
kosaka 12:a4b17bb682eb 101 extern void timerTS3(); // timer called every TS3[s].
kosaka 12:a4b17bb682eb 102 extern void timerTS4(); // timer called every TS4[s].
kosaka 12:a4b17bb682eb 103
kosaka 12:a4b17bb682eb 104 extern void init_parameters(); // IPMSMの機器定数等の設定, 制御器の初期化
kosaka 12:a4b17bb682eb 105
kosaka 12:a4b17bb682eb 106 extern unsigned long _count; // sampling number
kosaka 12:a4b17bb682eb 107 extern float _time; // time[s]
kosaka 12:a4b17bb682eb 108
kosaka 12:a4b17bb682eb 109 extern unsigned short f_find_origin; // flag to find the origin of the rotor angle theta
kosaka 12:a4b17bb682eb 110
kosaka 12:a4b17bb682eb 111 extern motor_parameters p; // モータの定数、信号など
kosaka 12:a4b17bb682eb 112 extern current_loop_parameters il; // 電流制御マイナーループの定数、変数
kosaka 12:a4b17bb682eb 113 extern velocity_loop_parameters vl; // 速度制御メインループの定数、変数
kosaka 12:a4b17bb682eb 114
kosaka 12:a4b17bb682eb 115 extern float data[][5]; // memory to save data offline instead of "online fprintf".
kosaka 12:a4b17bb682eb 116 extern unsigned short _count_data; // counter for data[1000][5]
kosaka 12:a4b17bb682eb 117
kosaka 12:a4b17bb682eb 118 #endif