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