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