UVW 3 phases Brushless DC motor control
Dependencies: QEI mbed-rtos mbed
Fork of BLDCmotor by
main.cpp@9:88d6270f95bc, 2012-11-24 (annotated)
- Committer:
- kosaka
- Date:
- Sat Nov 24 01:21:59 2012 +0000
- Revision:
- 9:88d6270f95bc
- Parent:
- 8:b8b31e9b60c2
- Child:
- 10:0984c90b820b
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kosaka | 1:b91aeb5673f3 | 1 | // DC motor control program using H-bridge driver (ex. TA7291P) and 360 resolution rotary encoder with A, B phase. |
kosaka | 9:88d6270f95bc | 2 | // ver. 121123b by Kosaka lab. |
kosaka | 0:fe068497f773 | 3 | #include "mbed.h" |
kosaka | 0:fe068497f773 | 4 | #include "rtos.h" |
kosaka | 0:fe068497f773 | 5 | #include "QEI.h" |
kosaka | 0:fe068497f773 | 6 | #define PI 3.14159265358979 // def. of PI |
kosaka | 0:fe068497f773 | 7 | /*********** User setting for control parameters (begin) ***************/ |
kosaka | 4:6ccbf4d3cb6d | 8 | //#define SIMULATION // Comment this line if not simulation |
kosaka | 3:b6b9b8c7dce6 | 9 | #define USE_PWM // H bridge PWM mode: Vref=Vcc, FIN,2 = PWM or 0. Comment if use Vref=analog mode |
kosaka | 8:b8b31e9b60c2 | 10 | #define PWM_FREQ 10000.0 //[Hz], pwm freq. available if USE_PWM is defined. |
kosaka | 8:b8b31e9b60c2 | 11 | #define USE_CURRENT_CONTROL // Current control on. Comment if current control off. |
kosaka | 8:b8b31e9b60c2 | 12 | #define CONTROL_MODE 0 // 0:PID control, 1:Frequency response, 2:Step response, 3. u=Rand to identify G(s), 4) FFT identification |
kosaka | 0:fe068497f773 | 13 | #define GOOD_DATA // Comment this line if the length of data TMAX/TS2 > 1000 |
kosaka | 6:16bee943a9fa | 14 | //#define R_SIN // Comment this line if r=step, not r = sin |
kosaka | 0:fe068497f773 | 15 | float _freq_u = 0.3; // [Hz], freq. of Frequency response, or Step response |
kosaka | 1:b91aeb5673f3 | 16 | float _rmax=100./180.*PI; // [rad], max. of reference signal |
kosaka | 8:b8b31e9b60c2 | 17 | float _Kp4th=20; // P gain for PID from motor volt. to angle. |
kosaka | 8:b8b31e9b60c2 | 18 | float _Ki4th=20; // I gain for PID from motor volt. to angle. |
kosaka | 8:b8b31e9b60c2 | 19 | float _Kd4th=5; // D gain for PID from motor volt. to angle. |
kosaka | 8:b8b31e9b60c2 | 20 | float _Kp4i=10.0; // P gain for PID from motor volt. to motor current. |
kosaka | 8:b8b31e9b60c2 | 21 | float _Ki4i=10.0; // I gain for PID from motor volt. to motor current. |
kosaka | 8:b8b31e9b60c2 | 22 | float _Kd4i=0.0; // D gain for PID from motor volt. to motor current. |
kosaka | 8:b8b31e9b60c2 | 23 | #define iTS 0.001 // [s], iTS, sampling time[s] of motor current i control PID using timer interrupt |
kosaka | 8:b8b31e9b60c2 | 24 | #define thTS 0.01 // [s], thTS>=0.001[s], sampling time[s] of motor angle th PID using rtos-timer |
kosaka | 8:b8b31e9b60c2 | 25 | #define TS2 0.01 // [s], TS2>=0.001[s], sampling time[s] to save data to PC using thread. But, max data length is 1000. |
kosaka | 0:fe068497f773 | 26 | #define TMAX 10 // [s], experiment starts from 0[s] to TMAX[s] |
kosaka | 0:fe068497f773 | 27 | #define UMAX 3.3 // [V], max of control input u |
kosaka | 0:fe068497f773 | 28 | #define UMIN -3.3 // [V], max of control input u |
kosaka | 8:b8b31e9b60c2 | 29 | #define IMAX 0.5 // [A], max of motor current i |
kosaka | 8:b8b31e9b60c2 | 30 | #define IMIN -0.5 // [A], max of motor current i |
kosaka | 3:b6b9b8c7dce6 | 31 | #define DEADTIME 0.0001 // [s], deadtime to be set between plus volt. to/from minus |
kosaka | 3:b6b9b8c7dce6 | 32 | // H bridge port setting |
kosaka | 3:b6b9b8c7dce6 | 33 | #define FIN_PORT p21 // FIN (IN1) port of mbed |
kosaka | 3:b6b9b8c7dce6 | 34 | #define RIN_PORT p22 // RIN (IN2) port of mbed |
kosaka | 3:b6b9b8c7dce6 | 35 | #define VREF_PORT p18 // Vref port of mbed (available if USE_PWM is not defined) |
kosaka | 0:fe068497f773 | 36 | DigitalOut debug_p17(p17); // p17 for debug |
kosaka | 8:b8b31e9b60c2 | 37 | AnalogIn v_shunt_r(p19); // *3.3 [V], Volt of shunt R_SHUNT[Ohm]. The motor current i = v_shunt_r/R_SHUNT [A] |
kosaka | 8:b8b31e9b60c2 | 38 | #define R_SHUNT 1.25 // [Ohm], shunt resistanse |
kosaka | 8:b8b31e9b60c2 | 39 | //AnalogIn VCC(p19); // *3.3 [V], Volt of VCC for motor |
kosaka | 8:b8b31e9b60c2 | 40 | //AnalogIn VCC2(p20); // *3.3 [V], Volt of (VCC-R i), R=2.5[Ohm]. R is for preventing too much i when deadtime is failed. |
kosaka | 0:fe068497f773 | 41 | |
kosaka | 5:2e53814aae4c | 42 | #define N_ENC (24*4) // "*4": QEI::X4_ENCODING. Number of pulses in one revolution(=360 deg) of rotary encoder. |
kosaka | 0:fe068497f773 | 43 | QEI encoder (p29, p30, NC, N_ENC, QEI::X4_ENCODING); |
kosaka | 0:fe068497f773 | 44 | // QEI(PinName channelA, mbed pin for channel A input. |
kosaka | 0:fe068497f773 | 45 | // PinName channelB, mbed pin for channel B input. |
kosaka | 0:fe068497f773 | 46 | // PinName index, mbed pin for channel Z input. (index channel input Z phase th=0), (pass NC if not needed). |
kosaka | 0:fe068497f773 | 47 | // int pulsesPerRev, Number of pulses in one revolution(=360 deg). |
kosaka | 0:fe068497f773 | 48 | // Encoding encoding = X2_ENCODING, X2 is default. X2 uses interrupts on the rising and falling edges of only channel A where as |
kosaka | 0:fe068497f773 | 49 | // X4 uses them on both channels. |
kosaka | 0:fe068497f773 | 50 | // ) |
kosaka | 0:fe068497f773 | 51 | // void reset (void) |
kosaka | 0:fe068497f773 | 52 | // Reset the encoder. |
kosaka | 0:fe068497f773 | 53 | // int getCurrentState (void) |
kosaka | 0:fe068497f773 | 54 | // Read the state of the encoder. |
kosaka | 0:fe068497f773 | 55 | // int getPulses (void) |
kosaka | 0:fe068497f773 | 56 | // Read the number of pulses recorded by the encoder. |
kosaka | 0:fe068497f773 | 57 | // int getRevolutions (void) |
kosaka | 0:fe068497f773 | 58 | // Read the number of revolutions recorded by the encoder on the index channel. |
kosaka | 0:fe068497f773 | 59 | /*********** User setting for control parameters (end) ***************/ |
kosaka | 0:fe068497f773 | 60 | |
kosaka | 0:fe068497f773 | 61 | |
kosaka | 0:fe068497f773 | 62 | Serial pc(USBTX, USBRX); // Display on tera term in PC |
kosaka | 0:fe068497f773 | 63 | LocalFileSystem local("local"); // save data to mbed USB disk drive in PC |
kosaka | 0:fe068497f773 | 64 | //Semaphore semaphore1(1); // wait and release to protect memories and so on |
kosaka | 0:fe068497f773 | 65 | //Mutex stdio_mutex; // wait and release to protect memories and so on |
kosaka | 8:b8b31e9b60c2 | 66 | Ticker controller_ticker; // Timer interrupt using TIMER3, TS<0.001 is OK. Priority is higher than rtosTimer. |
kosaka | 0:fe068497f773 | 67 | |
kosaka | 3:b6b9b8c7dce6 | 68 | #ifdef USE_PWM // H bridge PWM mode: Vref=Vcc, FIN,2 = PWM or 0. |
kosaka | 3:b6b9b8c7dce6 | 69 | PwmOut FIN(FIN_PORT); // PWM for FIN, RIN=0 when forward rotation. H bridge driver PWM mode |
kosaka | 3:b6b9b8c7dce6 | 70 | PwmOut RIN(RIN_PORT); // PWM for RIN, FIN=0 when reverse rotation. H bridge driver PWM mode |
kosaka | 3:b6b9b8c7dce6 | 71 | #else // H bridge Vref=analog mode |
kosaka | 3:b6b9b8c7dce6 | 72 | DigitalOut FIN(FIN_PORT);// FIN for DC motor H bridge driver. FIN=1, RIN=0 then forward rotation |
kosaka | 3:b6b9b8c7dce6 | 73 | DigitalOut RIN(RIN_PORT);// RIN for DC motor H bridge driver. FIN=0, RIN=1 then reverse rotation |
kosaka | 3:b6b9b8c7dce6 | 74 | #endif |
kosaka | 4:6ccbf4d3cb6d | 75 | AnalogOut analog_out(VREF_PORT);// Vref for DC motor H bridge driver. DA converter for control input [0.0-1.0]% in the output range of 0.0 to 3.3[V] |
kosaka | 4:6ccbf4d3cb6d | 76 | |
kosaka | 0:fe068497f773 | 77 | unsigned long _count; // sampling number |
kosaka | 0:fe068497f773 | 78 | float _time; // time[s] |
kosaka | 1:b91aeb5673f3 | 79 | float _r; // reference signal |
kosaka | 8:b8b31e9b60c2 | 80 | float _th=0; // [rad], motor angle, control output of angle controller |
kosaka | 8:b8b31e9b60c2 | 81 | float _i=0; // [A], motor current, control output of current controller |
kosaka | 0:fe068497f773 | 82 | float _e=0; // e=r-y for PID controller |
kosaka | 0:fe068497f773 | 83 | float _eI=0; // integral of e for PID controller |
kosaka | 8:b8b31e9b60c2 | 84 | float _iref; // reference current iref [A], output of angle th_contorller |
kosaka | 8:b8b31e9b60c2 | 85 | float _u; // control input[V], motor input volt. |
kosaka | 8:b8b31e9b60c2 | 86 | float _ei=0; // e=r-y for current PID controller |
kosaka | 8:b8b31e9b60c2 | 87 | float _eiI=0; // integral of e for current PID controller |
kosaka | 0:fe068497f773 | 88 | unsigned char _f_u_plus=1;// sign(u) |
kosaka | 0:fe068497f773 | 89 | unsigned char _f_umax=0;// flag showing u is max or not |
kosaka | 8:b8b31e9b60c2 | 90 | unsigned char _f_imax=0;// flag showing i is max or not |
kosaka | 0:fe068497f773 | 91 | float debug[10]; // for debug |
kosaka | 0:fe068497f773 | 92 | float disp[10]; // for printf to avoid interrupted by quicker process |
kosaka | 0:fe068497f773 | 93 | #ifdef GOOD_DATA |
kosaka | 0:fe068497f773 | 94 | float data[1000][5]; // memory to save data offline instead of "online fprintf". |
kosaka | 0:fe068497f773 | 95 | unsigned int count3; // |
kosaka | 8:b8b31e9b60c2 | 96 | unsigned int count2=(int)(TS2/iTS); // |
kosaka | 0:fe068497f773 | 97 | #endif |
kosaka | 0:fe068497f773 | 98 | |
kosaka | 7:613febb8f028 | 99 | extern "C" void mbed_reset(); |
kosaka | 3:b6b9b8c7dce6 | 100 | |
kosaka | 3:b6b9b8c7dce6 | 101 | void u2Hbridge(float u){// input u to H bridge driver |
kosaka | 3:b6b9b8c7dce6 | 102 | float duty; |
kosaka | 3:b6b9b8c7dce6 | 103 | unsigned int f_deadtime, f_in, r_in; |
kosaka | 0:fe068497f773 | 104 | |
kosaka | 0:fe068497f773 | 105 | if( u > 0 ){ // forward: rotate to plus |
kosaka | 3:b6b9b8c7dce6 | 106 | duty = u/3.3; // Vref |
kosaka | 8:b8b31e9b60c2 | 107 | if(_f_u_plus==0){ // if plus to/from minus, set FIN=RIN=0/1 for deadtime 100[us]. |
kosaka | 3:b6b9b8c7dce6 | 108 | f_deadtime = 1; // deadtime is required |
kosaka | 3:b6b9b8c7dce6 | 109 | _f_u_plus=1; |
kosaka | 3:b6b9b8c7dce6 | 110 | }else{ |
kosaka | 3:b6b9b8c7dce6 | 111 | f_deadtime = 0; // deadtime is required |
kosaka | 3:b6b9b8c7dce6 | 112 | } |
kosaka | 3:b6b9b8c7dce6 | 113 | f_in=1; r_in=0; // set forward direction |
kosaka | 0:fe068497f773 | 114 | }else if( u < 0 ){ // reverse: rotate to minus |
kosaka | 3:b6b9b8c7dce6 | 115 | duty = -u/3.3; |
kosaka | 8:b8b31e9b60c2 | 116 | if(_f_u_plus==1){ // if plus to/from minus, set FIN=RIN=0/1 for deadtime 100[us]. |
kosaka | 3:b6b9b8c7dce6 | 117 | f_deadtime = 1; // deadtime is required |
kosaka | 3:b6b9b8c7dce6 | 118 | _f_u_plus=0; |
kosaka | 3:b6b9b8c7dce6 | 119 | }else{ |
kosaka | 3:b6b9b8c7dce6 | 120 | f_deadtime = 0; // deadtime is required |
kosaka | 3:b6b9b8c7dce6 | 121 | } |
kosaka | 3:b6b9b8c7dce6 | 122 | f_in=0; r_in=1; // set reverse direction |
kosaka | 0:fe068497f773 | 123 | }else{// if( u == 0 ){ // stop mode |
kosaka | 3:b6b9b8c7dce6 | 124 | duty = 0; |
kosaka | 3:b6b9b8c7dce6 | 125 | f_deadtime = 0; // deadtime is required |
kosaka | 3:b6b9b8c7dce6 | 126 | f_in=0; r_in=0; // set FIN & RIN |
kosaka | 0:fe068497f773 | 127 | } |
kosaka | 3:b6b9b8c7dce6 | 128 | |
kosaka | 3:b6b9b8c7dce6 | 129 | if( f_deadtime==1 ){// making deadtime |
kosaka | 3:b6b9b8c7dce6 | 130 | FIN=0; RIN=0; // set upper&lower arm zero |
kosaka | 3:b6b9b8c7dce6 | 131 | wait(DEADTIME); |
kosaka | 3:b6b9b8c7dce6 | 132 | } |
kosaka | 3:b6b9b8c7dce6 | 133 | #ifdef USE_PWM // H bridge PWM mode: Vref=Vcc, FIN,2 = PWM or 0 |
kosaka | 3:b6b9b8c7dce6 | 134 | FIN = duty*(float)f_in; RIN = duty*(float)r_in; // setting pwm FIN & RIN |
kosaka | 4:6ccbf4d3cb6d | 135 | analog_out = 1; // setting Vref=UMAX, but Vref=Vcc is better. |
kosaka | 3:b6b9b8c7dce6 | 136 | #else // Analog mode: Vref=analog, FIN, RIN = 1 or 0) |
kosaka | 3:b6b9b8c7dce6 | 137 | FIN = f_in; RIN = r_in; // setting FIN & RIN |
kosaka | 3:b6b9b8c7dce6 | 138 | analog_out = duty; // setting Vref : PID write DA, range is 0-1. Output voltage 0-3.3v |
kosaka | 3:b6b9b8c7dce6 | 139 | #endif |
kosaka | 0:fe068497f773 | 140 | } |
kosaka | 0:fe068497f773 | 141 | |
kosaka | 8:b8b31e9b60c2 | 142 | void th_controller(void const *argument) { // if rtos. current controller & velocity controller |
kosaka | 0:fe068497f773 | 143 | float e_old, wt; |
kosaka | 8:b8b31e9b60c2 | 144 | float y, u; |
kosaka | 0:fe068497f773 | 145 | |
kosaka | 8:b8b31e9b60c2 | 146 | // y_old = _th; // y_old=y(t-iTS) is older than y by 1 sampling time iTS[s]. update data |
kosaka | 0:fe068497f773 | 147 | #ifdef SIMULATION |
kosaka | 8:b8b31e9b60c2 | 148 | y = _th + iTS/0.1*(0.2*_iref*100-_th); //=(1-iTS/0.1)*_y + 0.2*iTS/0.1*_iref; // G = 0.2/(0.1s+1) |
kosaka | 0:fe068497f773 | 149 | #else |
kosaka | 0:fe068497f773 | 150 | // semaphore1.wait(); // |
kosaka | 0:fe068497f773 | 151 | y = (float)encoder.getPulses()/(float)N_ENC*2.0*PI; // get angle [rad] from encoder |
kosaka | 0:fe068497f773 | 152 | // semaphore1.release(); // |
kosaka | 0:fe068497f773 | 153 | #endif |
kosaka | 8:b8b31e9b60c2 | 154 | #define RMIN 0 |
kosaka | 0:fe068497f773 | 155 | wt = _freq_u *2.0*PI*_time; |
kosaka | 0:fe068497f773 | 156 | if(wt>2*PI){ wt -= 2*PI*(float)((int)(wt/(2.0*PI)));} |
kosaka | 1:b91aeb5673f3 | 157 | _r = sin(wt ) * (_rmax-RMIN)/2.0 + (_rmax+RMIN)/2.0; |
kosaka | 1:b91aeb5673f3 | 158 | #ifndef R_SIN |
kosaka | 1:b91aeb5673f3 | 159 | if( _r>=(_rmax+RMIN)/2.0 ) _r = _rmax; |
kosaka | 1:b91aeb5673f3 | 160 | else _r = 0; |
kosaka | 0:fe068497f773 | 161 | #endif |
kosaka | 8:b8b31e9b60c2 | 162 | e_old = _e; // e_old=e(t-iTS) is older than e by 1 sampling time iTS[s]. update data |
kosaka | 0:fe068497f773 | 163 | _e = _r - y; // error e(t) |
kosaka | 8:b8b31e9b60c2 | 164 | if( _f_imax==0 ){ // u is saturated? |
kosaka | 8:b8b31e9b60c2 | 165 | if( _e>((360.0/N_ENC)/180*PI) || _e<-((360.0/N_ENC)/180*PI) ){ // e is inside minimum precision? |
kosaka | 8:b8b31e9b60c2 | 166 | _eI = _eI + thTS*_e; // integral of e(t) |
kosaka | 8:b8b31e9b60c2 | 167 | } |
kosaka | 0:fe068497f773 | 168 | } |
kosaka | 8:b8b31e9b60c2 | 169 | u = _Kp4th*_e + _Kd4th*(_e-e_old)/iTS + _Ki4th*_eI; // PID output u(t) |
kosaka | 0:fe068497f773 | 170 | |
kosaka | 6:16bee943a9fa | 171 | #if CONTROL_MODE==1||CONTROL_MODE==2 // frequency response, or Step response |
kosaka | 0:fe068497f773 | 172 | wt = _freq_u *2.0*PI*_time; |
kosaka | 0:fe068497f773 | 173 | if(wt>2*PI) wt -= 2*PI*(float)((int)(wt/2.0*PI)); |
kosaka | 0:fe068497f773 | 174 | u = sin(wt ) * (UMAX-UMIN)/2.0 + (UMAX+UMIN)/2.0; |
kosaka | 0:fe068497f773 | 175 | #endif |
kosaka | 0:fe068497f773 | 176 | #if CONTROL_MODE==2 // Step response |
kosaka | 0:fe068497f773 | 177 | if( u>=0 ) u = UMAX; |
kosaka | 0:fe068497f773 | 178 | else u = UMIN; |
kosaka | 0:fe068497f773 | 179 | #endif |
kosaka | 6:16bee943a9fa | 180 | #if CONTROL_MODE==3 // u=rand() to identify motor transfer function G(s) from V to angle |
kosaka | 8:b8b31e9b60c2 | 181 | if(count2==(int)(TS2/iTS)){ |
kosaka | 6:16bee943a9fa | 182 | u = ((float)rand()/RAND_MAX*2.0-1.0) * (UMAX-1.5)/2.0 + (UMAX+1.5)/2.0; |
kosaka | 6:16bee943a9fa | 183 | }else{ |
kosaka | 8:b8b31e9b60c2 | 184 | u = _iref; |
kosaka | 6:16bee943a9fa | 185 | } |
kosaka | 6:16bee943a9fa | 186 | #endif |
kosaka | 7:613febb8f028 | 187 | #if CONTROL_MODE==4 // FFT identification, u=repetive signal |
kosaka | 8:b8b31e9b60c2 | 188 | if(count2==(int)(TS2/iTS)){ |
kosaka | 7:613febb8f028 | 189 | u = data[count3][4]; |
kosaka | 7:613febb8f028 | 190 | }else{ |
kosaka | 8:b8b31e9b60c2 | 191 | u = _iref; |
kosaka | 7:613febb8f028 | 192 | } |
kosaka | 7:613febb8f028 | 193 | #endif |
kosaka | 8:b8b31e9b60c2 | 194 | // u is saturated? for anti-windup |
kosaka | 8:b8b31e9b60c2 | 195 | if( u>IMAX ){ |
kosaka | 8:b8b31e9b60c2 | 196 | _eI -= (u-IMAX)/_Ki4th; if(_eI<0){ _eI=0;} |
kosaka | 8:b8b31e9b60c2 | 197 | u = IMAX; |
kosaka | 8:b8b31e9b60c2 | 198 | // _f_imax = 1; |
kosaka | 8:b8b31e9b60c2 | 199 | } else if( u<IMIN ){ |
kosaka | 8:b8b31e9b60c2 | 200 | _eI -= (u-IMIN)/_Ki4th; if(_eI>0){ _eI=0;} |
kosaka | 8:b8b31e9b60c2 | 201 | u = IMIN; |
kosaka | 8:b8b31e9b60c2 | 202 | // _f_imax = 1; |
kosaka | 8:b8b31e9b60c2 | 203 | }else{ |
kosaka | 8:b8b31e9b60c2 | 204 | _f_imax = 0; |
kosaka | 8:b8b31e9b60c2 | 205 | } |
kosaka | 8:b8b31e9b60c2 | 206 | //-------- update data |
kosaka | 8:b8b31e9b60c2 | 207 | _th = y; |
kosaka | 8:b8b31e9b60c2 | 208 | _iref = u; |
kosaka | 8:b8b31e9b60c2 | 209 | } |
kosaka | 8:b8b31e9b60c2 | 210 | void i_controller() { // if ticker. current controller & velocity controller |
kosaka | 8:b8b31e9b60c2 | 211 | void u2Hbridge(float); // input u to H bridge (full bridge) driver |
kosaka | 8:b8b31e9b60c2 | 212 | #ifdef USE_CURRENT_CONTROL |
kosaka | 8:b8b31e9b60c2 | 213 | float e_old; |
kosaka | 8:b8b31e9b60c2 | 214 | float y, u; |
kosaka | 8:b8b31e9b60c2 | 215 | |
kosaka | 8:b8b31e9b60c2 | 216 | // _iref=_r*180/PI; // step response from v to i, useful to tune PID gains. |
kosaka | 8:b8b31e9b60c2 | 217 | debug_p17 = 1; // for debug: processing time check |
kosaka | 8:b8b31e9b60c2 | 218 | // if(debug_p17 == 1) debug_p17=0;else debug_p17=1; // for debug: sampling time check |
kosaka | 8:b8b31e9b60c2 | 219 | |
kosaka | 8:b8b31e9b60c2 | 220 | _count+=1; |
kosaka | 8:b8b31e9b60c2 | 221 | // current PID controller |
kosaka | 8:b8b31e9b60c2 | 222 | y = v_shunt_r/R_SHUNT; // get i [A] from shunt resistance |
kosaka | 8:b8b31e9b60c2 | 223 | if(_f_u_plus==0){ y=-y;} |
kosaka | 8:b8b31e9b60c2 | 224 | |
kosaka | 8:b8b31e9b60c2 | 225 | e_old = _ei; // e_old=e(t-iTS) is older than e by 1 sampling time iTS[s]. update data |
kosaka | 8:b8b31e9b60c2 | 226 | _ei = _iref - y; // error e(t) |
kosaka | 8:b8b31e9b60c2 | 227 | if( _f_umax==0 ){ |
kosaka | 8:b8b31e9b60c2 | 228 | _eiI = _eiI + iTS*_ei; // integral of e(t) |
kosaka | 8:b8b31e9b60c2 | 229 | } |
kosaka | 8:b8b31e9b60c2 | 230 | u = _Kp4i*_e + _Kd4i*(_ei-e_old)/iTS + _Ki4i*_eiI; // PID output u(t) |
kosaka | 8:b8b31e9b60c2 | 231 | |
kosaka | 8:b8b31e9b60c2 | 232 | // u is saturated? for anti-windup |
kosaka | 8:b8b31e9b60c2 | 233 | if( u>UMAX ){ |
kosaka | 8:b8b31e9b60c2 | 234 | _eiI -= (u-UMAX)/_Ki4i; if(_eiI<0){ _eiI=0;} |
kosaka | 8:b8b31e9b60c2 | 235 | u = UMAX; |
kosaka | 8:b8b31e9b60c2 | 236 | // _f_umax = 1; |
kosaka | 8:b8b31e9b60c2 | 237 | } else if( u<UMIN ){ |
kosaka | 8:b8b31e9b60c2 | 238 | _eiI -= (u-UMIN)/_Ki4i; if(_eiI>0){ _eiI=0;} |
kosaka | 8:b8b31e9b60c2 | 239 | u = UMIN; |
kosaka | 8:b8b31e9b60c2 | 240 | // _f_umax = 1; |
kosaka | 8:b8b31e9b60c2 | 241 | }else{ |
kosaka | 8:b8b31e9b60c2 | 242 | _f_umax = 0; |
kosaka | 8:b8b31e9b60c2 | 243 | } |
kosaka | 8:b8b31e9b60c2 | 244 | //-------- update data |
kosaka | 8:b8b31e9b60c2 | 245 | _i = y; |
kosaka | 8:b8b31e9b60c2 | 246 | _u = u; |
kosaka | 8:b8b31e9b60c2 | 247 | #else |
kosaka | 8:b8b31e9b60c2 | 248 | _u = _iref; // without current control. |
kosaka | 8:b8b31e9b60c2 | 249 | #endif |
kosaka | 8:b8b31e9b60c2 | 250 | |
kosaka | 8:b8b31e9b60c2 | 251 | u2Hbridge(_u); // input u to TA7291 driver |
kosaka | 0:fe068497f773 | 252 | |
kosaka | 0:fe068497f773 | 253 | //-------- update data |
kosaka | 8:b8b31e9b60c2 | 254 | _time += iTS; // time |
kosaka | 8:b8b31e9b60c2 | 255 | debug[0]=v_shunt_r; if(_f_u_plus==0){ debug[0]=-debug[0];} |
kosaka | 0:fe068497f773 | 256 | #ifdef GOOD_DATA |
kosaka | 8:b8b31e9b60c2 | 257 | if(count2==(int)(TS2/iTS)){ |
kosaka | 8:b8b31e9b60c2 | 258 | // j=0; if(_count>=j&&_count<j+1000){i=_count-j; data[i][0]=_r; data[i][1]=debug[0]; data[i][2]=_th; data[i][3]=_time; data[i][4]=_u;} |
kosaka | 2:e056793d6fc5 | 259 | if( count3<1000 ){ |
kosaka | 8:b8b31e9b60c2 | 260 | data[count3][0]=_r; data[count3][1]=debug[0]; data[count3][2]=_th; data[count3][3]=_time; data[count3][4]=_u; |
kosaka | 8:b8b31e9b60c2 | 261 | // data[count3][0]=_iref; data[count3][1]=debug[0]; data[count3][2]=_i; data[count3][3]=_time; data[count3][4]=_u; |
kosaka | 2:e056793d6fc5 | 262 | count3++; |
kosaka | 2:e056793d6fc5 | 263 | } |
kosaka | 0:fe068497f773 | 264 | count2 = 0; |
kosaka | 0:fe068497f773 | 265 | } |
kosaka | 0:fe068497f773 | 266 | count2++; |
kosaka | 0:fe068497f773 | 267 | #endif |
kosaka | 0:fe068497f773 | 268 | //-------- update data |
kosaka | 0:fe068497f773 | 269 | |
kosaka | 0:fe068497f773 | 270 | debug_p17 = 0; // for debug: processing time check |
kosaka | 0:fe068497f773 | 271 | } |
kosaka | 0:fe068497f773 | 272 | |
kosaka | 0:fe068497f773 | 273 | void main1() { |
kosaka | 8:b8b31e9b60c2 | 274 | RtosTimer timer_controller(th_controller); |
kosaka | 0:fe068497f773 | 275 | FILE *fp; // save data to PC |
kosaka | 0:fe068497f773 | 276 | #ifdef GOOD_DATA |
kosaka | 0:fe068497f773 | 277 | int i; |
kosaka | 0:fe068497f773 | 278 | |
kosaka | 0:fe068497f773 | 279 | count3=0; |
kosaka | 0:fe068497f773 | 280 | #endif |
kosaka | 3:b6b9b8c7dce6 | 281 | u2Hbridge(0); // initialize H bridge to stop mode |
kosaka | 0:fe068497f773 | 282 | _count=0; |
kosaka | 0:fe068497f773 | 283 | _time = 0; // time |
kosaka | 8:b8b31e9b60c2 | 284 | _eI = _eiI = 0; // reset integrater |
kosaka | 1:b91aeb5673f3 | 285 | encoder.reset(); // set encoder counter zero |
kosaka | 8:b8b31e9b60c2 | 286 | _th = (float)encoder.getPulses()/(float)N_ENC*2.0*PI; // get angle [rad] from encoder |
kosaka | 8:b8b31e9b60c2 | 287 | _r = _r + _th; |
kosaka | 1:b91aeb5673f3 | 288 | // if( _r>2*PI ) _r -= _r-2*PI; |
kosaka | 0:fe068497f773 | 289 | |
kosaka | 0:fe068497f773 | 290 | pc.printf("Control start!!\r\n"); |
kosaka | 0:fe068497f773 | 291 | if ( NULL == (fp = fopen( "/local/data.csv", "w" )) ){ error( "" );} // save data to PC |
kosaka | 3:b6b9b8c7dce6 | 292 | #ifdef USE_PWM |
kosaka | 3:b6b9b8c7dce6 | 293 | FIN.period( 1.0 / PWM_FREQ ); // PWM period [s]. Common to all PWM |
kosaka | 3:b6b9b8c7dce6 | 294 | #endif |
kosaka | 8:b8b31e9b60c2 | 295 | controller_ticker.attach(&i_controller, iTS ); // Sampling period[s] of i_controller |
kosaka | 8:b8b31e9b60c2 | 296 | timer_controller.start((unsigned int)(iTS*1000.)); // Sampling period[ms] of th controller |
kosaka | 0:fe068497f773 | 297 | |
kosaka | 8:b8b31e9b60c2 | 298 | // for ( i = 0; i < (unsigned int)(TMAX/iTS2); i++ ) { |
kosaka | 0:fe068497f773 | 299 | while ( _time <= TMAX ) { |
kosaka | 0:fe068497f773 | 300 | // BUG!! Dangerous if TS2<0.1 because multi interrupt by fprintf is not prohibited! 1st aug of fprintf will be destroyed. |
kosaka | 0:fe068497f773 | 301 | // fprintf returns before process completed. |
kosaka | 0:fe068497f773 | 302 | //BUG fprintf( fp, "%8.2f, %8.4f,\t%8.1f,\t%8.2f\r\n", disp[3], disp[1], disp[0], tmp); // save data to PC (para, y, time, u) |
kosaka | 0:fe068497f773 | 303 | //OK? fprintf( fp, "%f, %f, %f, %f, %f\r\n", _time, debug[0], debug[3], (_y/(2*PI)*360.0),_u); // save data to PC (para, y, time, u) |
kosaka | 0:fe068497f773 | 304 | #ifndef GOOD_DATA |
kosaka | 0:fe068497f773 | 305 | fprintf( fp, "%f, %f, %f, %f, %f\r\n", _r, debug[0], _y, _time, _u); // save data to PC (para, y, time, u) |
kosaka | 0:fe068497f773 | 306 | #endif |
kosaka | 0:fe068497f773 | 307 | Thread::wait((unsigned int)(TS2*1000.)); //[ms] |
kosaka | 0:fe068497f773 | 308 | } |
kosaka | 8:b8b31e9b60c2 | 309 | controller_ticker.detach(); // timer interrupt stop |
kosaka | 0:fe068497f773 | 310 | timer_controller.stop(); // rtos timer stop |
kosaka | 3:b6b9b8c7dce6 | 311 | u2Hbridge(0); // initialize H bridge to stop mode |
kosaka | 8:b8b31e9b60c2 | 312 | _eI = _eiI = 0; // reset integrater |
kosaka | 0:fe068497f773 | 313 | #ifdef GOOD_DATA |
kosaka | 0:fe068497f773 | 314 | for(i=0;i<1000;i++){ fprintf( fp, "%f, %f, %f, %f, %f\r\n", data[i][0],data[i][1],data[i][2],data[i][3],data[i][4]);} // save data to PC (para, y, time, u) |
kosaka | 0:fe068497f773 | 315 | #endif |
kosaka | 0:fe068497f773 | 316 | fclose( fp ); // release mbed USB drive |
kosaka | 0:fe068497f773 | 317 | pc.printf("Control completed!!\r\n\r\n"); |
kosaka | 0:fe068497f773 | 318 | } |
kosaka | 0:fe068497f773 | 319 | |
kosaka | 0:fe068497f773 | 320 | void thread_print2PC(void const *argument) { |
kosaka | 0:fe068497f773 | 321 | while (true) { |
kosaka | 8:b8b31e9b60c2 | 322 | pc.printf("%8.1f[s]\t%8.5f[V]\t%4d [deg]\t%8.2f\r\n", _time, _u, (int)(_th/(2*PI)*360.0), debug[0]*3.3/R_SHUNT); // print to tera term |
kosaka | 0:fe068497f773 | 323 | Thread::wait(200); |
kosaka | 0:fe068497f773 | 324 | } |
kosaka | 0:fe068497f773 | 325 | } |
kosaka | 0:fe068497f773 | 326 | |
kosaka | 0:fe068497f773 | 327 | void main2(void const *argument) { |
kosaka | 0:fe068497f773 | 328 | #if CONTROL_MODE==0 // PID control |
kosaka | 0:fe068497f773 | 329 | char f; |
kosaka | 0:fe068497f773 | 330 | float val; |
kosaka | 0:fe068497f773 | 331 | #endif |
kosaka | 7:613febb8f028 | 332 | #if CONTROL_MODE==4 // FFT identification, u=repetive signal |
kosaka | 7:613febb8f028 | 333 | int i, j; |
kosaka | 7:613febb8f028 | 334 | float max_u; |
kosaka | 7:613febb8f028 | 335 | #endif |
kosaka | 7:613febb8f028 | 336 | |
kosaka | 0:fe068497f773 | 337 | while(true){ |
kosaka | 7:613febb8f028 | 338 | #if CONTROL_MODE==4 // FFT identification, u=repetive signal |
kosaka | 7:613febb8f028 | 339 | max_u = 0; |
kosaka | 7:613febb8f028 | 340 | for( i=0;i<1000;i++ ){ // u=data[i][4]: memory for FFT identification input signal. |
kosaka | 7:613febb8f028 | 341 | data[i][4] = sin(_freq_u*2*PI * i*TS2); // _u_freq = 10/2 * i [Hz] |
kosaka | 7:613febb8f028 | 342 | if( data[i][4]>max_u ){ max_u=data[i][4];} |
kosaka | 7:613febb8f028 | 343 | } |
kosaka | 7:613febb8f028 | 344 | for( j=1;j<50;j++ ){ |
kosaka | 7:613febb8f028 | 345 | for( i=0;i<1000;i++ ){ |
kosaka | 7:613febb8f028 | 346 | data[i][4] += sin((float)(j+1)*_freq_u*2*PI * i*TS2); |
kosaka | 7:613febb8f028 | 347 | if( data[i][4]>max_u ){ max_u=data[i][4];} |
kosaka | 7:613febb8f028 | 348 | } |
kosaka | 7:613febb8f028 | 349 | } |
kosaka | 7:613febb8f028 | 350 | for( i=0;i<1000;i++ ){ |
kosaka | 7:613febb8f028 | 351 | // data[i][4] *= UMAX/max_u; |
kosaka | 7:613febb8f028 | 352 | data[i][4] = (data[i][4]/max_u+3)/4*UMAX; |
kosaka | 7:613febb8f028 | 353 | } |
kosaka | 7:613febb8f028 | 354 | #endif |
kosaka | 0:fe068497f773 | 355 | main1(); |
kosaka | 0:fe068497f773 | 356 | |
kosaka | 0:fe068497f773 | 357 | #if CONTROL_MODE>=1 // frequency response, or Step response |
kosaka | 7:613febb8f028 | 358 | pc.printf("Input u(t) Frequency[Hz]? (if 9, reset mbed)..."); |
kosaka | 0:fe068497f773 | 359 | pc.scanf("%f",&_freq_u); |
kosaka | 0:fe068497f773 | 360 | pc.printf("%8.3f[Hz]\r\n", _freq_u); // print to tera term |
kosaka | 7:613febb8f028 | 361 | if(_freq_u==9){ mbed_reset();} |
kosaka | 0:fe068497f773 | 362 | #else // PID control |
kosaka | 1:b91aeb5673f3 | 363 | // #ifdef R_SIN |
kosaka | 1:b91aeb5673f3 | 364 | // pc.printf("Reference signal r(t) Frequency[Hz]?..."); |
kosaka | 1:b91aeb5673f3 | 365 | // pc.scanf("%f",&_freq_u); |
kosaka | 1:b91aeb5673f3 | 366 | // pc.printf("%8.3f[Hz]\r\n", _freq_u); // print to tera term |
kosaka | 1:b91aeb5673f3 | 367 | // #endif |
kosaka | 8:b8b31e9b60c2 | 368 | pc.printf("th-loop: Kp=%f, Ki=%f, Kd=%f, r=%f[deg], %f Hz\r\n",_Kp4th, _Ki4th, _Kd4th, _rmax*180./PI, _freq_u); |
kosaka | 8:b8b31e9b60c2 | 369 | pc.printf(" i-loop: Kp=%f, Ki=%f, Kd=%f\r\n",_Kp4i, _Ki4i, _Kd4i); |
kosaka | 8:b8b31e9b60c2 | 370 | pc.printf("Which number do you like to change?\r\n ... 0)no change, 1)Kp, 2)Ki, 3)Kd, 4)r(t) freq.[Hz], 5)r(t) amp.[deg].\r\n 6)iKp, 7)iKi, 8)iKd, 9)reset mbed ?"); |
kosaka | 0:fe068497f773 | 371 | f=pc.getc()-48; //int = char-48 |
kosaka | 0:fe068497f773 | 372 | pc.printf("\r\n Value?... "); |
kosaka | 1:b91aeb5673f3 | 373 | if(f>=1&&f<=5){ pc.scanf("%f",&val);} |
kosaka | 0:fe068497f773 | 374 | pc.printf("%8.3f\r\n", val); // print to tera term |
kosaka | 8:b8b31e9b60c2 | 375 | if(f==1){ _Kp4th = val;} |
kosaka | 8:b8b31e9b60c2 | 376 | if(f==2){ _Ki4th = val;} |
kosaka | 8:b8b31e9b60c2 | 377 | if(f==3){ _Kd4th = val;} |
kosaka | 1:b91aeb5673f3 | 378 | if(f==4){ _freq_u = val;} |
kosaka | 1:b91aeb5673f3 | 379 | if(f==5){ _rmax = val/180.*PI;} |
kosaka | 8:b8b31e9b60c2 | 380 | if(f==6){ _Kp4i = val;} |
kosaka | 8:b8b31e9b60c2 | 381 | if(f==7){ _Ki4i = val;} |
kosaka | 8:b8b31e9b60c2 | 382 | if(f==8){ _Kd4i = val;} |
kosaka | 7:613febb8f028 | 383 | if(f==9){ mbed_reset();} |
kosaka | 8:b8b31e9b60c2 | 384 | pc.printf("th-loop: Kp=%f, Ki=%f, Kd=%f, r=%f[deg], %f Hz\r\n",_Kp4th, _Ki4th, _Kd4th, _rmax*180./PI, _freq_u); |
kosaka | 8:b8b31e9b60c2 | 385 | pc.printf(" i-loop: Kp=%f, Ki=%f, Kd=%f\r\n",_Kp4i, _Ki4i, _Kd4i); |
kosaka | 0:fe068497f773 | 386 | #endif |
kosaka | 0:fe068497f773 | 387 | } |
kosaka | 0:fe068497f773 | 388 | } |
kosaka | 0:fe068497f773 | 389 | int main() { |
kosaka | 0:fe068497f773 | 390 | // void main1(); |
kosaka | 0:fe068497f773 | 391 | Thread save2PC(main2,NULL,osPriorityBelowNormal); |
kosaka | 0:fe068497f773 | 392 | Thread print2PC(thread_print2PC,NULL,osPriorityLow); |
kosaka | 0:fe068497f773 | 393 | |
kosaka | 0:fe068497f773 | 394 | // osStatus set_priority(osPriority osPriorityBelowNormal ); |
kosaka | 0:fe068497f773 | 395 | // Priority of Thread (RtosTimer has no priority?) |
kosaka | 0:fe068497f773 | 396 | // osPriorityIdle = -3, ///< priority: idle (lowest)--> then, mbed ERROR!! |
kosaka | 0:fe068497f773 | 397 | // osPriorityLow = -2, ///< priority: low |
kosaka | 0:fe068497f773 | 398 | // osPriorityBelowNormal = -1, ///< priority: below normal |
kosaka | 0:fe068497f773 | 399 | // osPriorityNormal = 0, ///< priority: normal (default) |
kosaka | 0:fe068497f773 | 400 | // osPriorityAboveNormal = +1, ///< priority: above normal |
kosaka | 0:fe068497f773 | 401 | // osPriorityHigh = +2, ///< priority: high |
kosaka | 0:fe068497f773 | 402 | // osPriorityRealtime = +3, ///< priority: realtime (highest) |
kosaka | 0:fe068497f773 | 403 | // osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority |
kosaka | 0:fe068497f773 | 404 | } |