UVW 3 phases Brushless DC motor control

Dependencies:   QEI mbed-rtos mbed

Fork of DCmotor by manabu kosaka

Committer:
kosaka
Date:
Tue Nov 20 09:54:55 2012 +0000
Revision:
7:613febb8f028
Parent:
6:16bee943a9fa
Child:
8:b8b31e9b60c2
CONTROL_MODE 4: FFT identification mode is added

Who changed what in which revision?

UserRevisionLine numberNew 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 7:613febb8f028 2 // ver. 121118a 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 7:613febb8f028 10 #define CONTROL_MODE 4 // 0:PID control, 1:Frequency response, 2:Step response, 3. u=Rand to identify G(s), 4) FFT identification
kosaka 0:fe068497f773 11 #define GOOD_DATA // Comment this line if the length of data TMAX/TS2 > 1000
kosaka 6:16bee943a9fa 12 //#define R_SIN // Comment this line if r=step, not r = sin
kosaka 0:fe068497f773 13 float _freq_u = 0.3; // [Hz], freq. of Frequency response, or Step response
kosaka 1:b91aeb5673f3 14 float _rmax=100./180.*PI; // [rad], max. of reference signal
kosaka 5:2e53814aae4c 15 float _Kp=20; // P gain for PID ... Kp=1, Ki=0, Kd=0 is good.
kosaka 5:2e53814aae4c 16 float _Ki=20; // I gain for PID
kosaka 5:2e53814aae4c 17 float _Kd=5; // D gain for PID
kosaka 3:b6b9b8c7dce6 18 #define TS 0.001 // [s], TS>=0.001[s], sampling time[s] of PID controller
kosaka 3:b6b9b8c7dce6 19 #define TS2 0.01 // [s], TS2>=0.001[s], sampling time[s] of data save to PC. But, max data length is 1000.
kosaka 0:fe068497f773 20 #define TMAX 10 // [s], experiment starts from 0[s] to TMAX[s]
kosaka 0:fe068497f773 21 #define UMAX 3.3 // [V], max of control input u
kosaka 0:fe068497f773 22 #define UMIN -3.3 // [V], max of control input u
kosaka 3:b6b9b8c7dce6 23 #define DEADTIME 0.0001 // [s], deadtime to be set between plus volt. to/from minus
kosaka 3:b6b9b8c7dce6 24 // H bridge port setting
kosaka 3:b6b9b8c7dce6 25 #define FIN_PORT p21 // FIN (IN1) port of mbed
kosaka 3:b6b9b8c7dce6 26 #define RIN_PORT p22 // RIN (IN2) port of mbed
kosaka 3:b6b9b8c7dce6 27 #define VREF_PORT p18 // Vref port of mbed (available if USE_PWM is not defined)
kosaka 0:fe068497f773 28 DigitalOut debug_p17(p17); // p17 for debug
kosaka 0:fe068497f773 29
kosaka 5:2e53814aae4c 30 #define N_ENC (24*4) // "*4": QEI::X4_ENCODING. Number of pulses in one revolution(=360 deg) of rotary encoder.
kosaka 0:fe068497f773 31 QEI encoder (p29, p30, NC, N_ENC, QEI::X4_ENCODING);
kosaka 0:fe068497f773 32 // QEI(PinName channelA, mbed pin for channel A input.
kosaka 0:fe068497f773 33 // PinName channelB, mbed pin for channel B input.
kosaka 0:fe068497f773 34 // PinName index, mbed pin for channel Z input. (index channel input Z phase th=0), (pass NC if not needed).
kosaka 0:fe068497f773 35 // int pulsesPerRev, Number of pulses in one revolution(=360 deg).
kosaka 0:fe068497f773 36 // 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 37 // X4 uses them on both channels.
kosaka 0:fe068497f773 38 // )
kosaka 0:fe068497f773 39 // void reset (void)
kosaka 0:fe068497f773 40 // Reset the encoder.
kosaka 0:fe068497f773 41 // int getCurrentState (void)
kosaka 0:fe068497f773 42 // Read the state of the encoder.
kosaka 0:fe068497f773 43 // int getPulses (void)
kosaka 0:fe068497f773 44 // Read the number of pulses recorded by the encoder.
kosaka 0:fe068497f773 45 // int getRevolutions (void)
kosaka 0:fe068497f773 46 // Read the number of revolutions recorded by the encoder on the index channel.
kosaka 0:fe068497f773 47 /*********** User setting for control parameters (end) ***************/
kosaka 0:fe068497f773 48
kosaka 0:fe068497f773 49
kosaka 0:fe068497f773 50 Serial pc(USBTX, USBRX); // Display on tera term in PC
kosaka 0:fe068497f773 51 LocalFileSystem local("local"); // save data to mbed USB disk drive in PC
kosaka 0:fe068497f773 52 //Semaphore semaphore1(1); // wait and release to protect memories and so on
kosaka 0:fe068497f773 53 //Mutex stdio_mutex; // wait and release to protect memories and so on
kosaka 0:fe068497f773 54 //Ticker controller_ticker; // Timer interrupt using TIMER3, TS<0.001 is OK. Priority is higher than rtosTimer.
kosaka 0:fe068497f773 55
kosaka 3:b6b9b8c7dce6 56 #ifdef USE_PWM // H bridge PWM mode: Vref=Vcc, FIN,2 = PWM or 0.
kosaka 3:b6b9b8c7dce6 57 #define PWM_FREQ 10000.0 //[Hz], pwm freq.
kosaka 3:b6b9b8c7dce6 58 PwmOut FIN(FIN_PORT); // PWM for FIN, RIN=0 when forward rotation. H bridge driver PWM mode
kosaka 3:b6b9b8c7dce6 59 PwmOut RIN(RIN_PORT); // PWM for RIN, FIN=0 when reverse rotation. H bridge driver PWM mode
kosaka 3:b6b9b8c7dce6 60 #else // H bridge Vref=analog mode
kosaka 3:b6b9b8c7dce6 61 DigitalOut FIN(FIN_PORT);// FIN for DC motor H bridge driver. FIN=1, RIN=0 then forward rotation
kosaka 3:b6b9b8c7dce6 62 DigitalOut RIN(RIN_PORT);// RIN for DC motor H bridge driver. FIN=0, RIN=1 then reverse rotation
kosaka 3:b6b9b8c7dce6 63 #endif
kosaka 4:6ccbf4d3cb6d 64 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 65
kosaka 0:fe068497f773 66 unsigned long _count; // sampling number
kosaka 0:fe068497f773 67 float _time; // time[s]
kosaka 1:b91aeb5673f3 68 float _r; // reference signal
kosaka 0:fe068497f773 69 float _y; // control output
kosaka 0:fe068497f773 70 float _e=0; // e=r-y for PID controller
kosaka 0:fe068497f773 71 float _eI=0; // integral of e for PID controller
kosaka 0:fe068497f773 72 float _u; // control input[V]
kosaka 0:fe068497f773 73 unsigned char _f_u_plus=1;// sign(u)
kosaka 0:fe068497f773 74 unsigned char _f_umax=0;// flag showing u is max or not
kosaka 0:fe068497f773 75 float debug[10]; // for debug
kosaka 0:fe068497f773 76 float disp[10]; // for printf to avoid interrupted by quicker process
kosaka 0:fe068497f773 77 #ifdef GOOD_DATA
kosaka 0:fe068497f773 78 float data[1000][5]; // memory to save data offline instead of "online fprintf".
kosaka 0:fe068497f773 79 unsigned int count3; //
kosaka 0:fe068497f773 80 unsigned int count2=(int)(TS2/TS); //
kosaka 0:fe068497f773 81 #endif
kosaka 0:fe068497f773 82
kosaka 7:613febb8f028 83 extern "C" void mbed_reset();
kosaka 3:b6b9b8c7dce6 84
kosaka 3:b6b9b8c7dce6 85 void u2Hbridge(float u){// input u to H bridge driver
kosaka 3:b6b9b8c7dce6 86 float duty;
kosaka 3:b6b9b8c7dce6 87 unsigned int f_deadtime, f_in, r_in;
kosaka 0:fe068497f773 88
kosaka 0:fe068497f773 89 if( u > 0 ){ // forward: rotate to plus
kosaka 3:b6b9b8c7dce6 90 duty = u/3.3; // Vref
kosaka 3:b6b9b8c7dce6 91 if(_f_u_plus==0){ // if plus to/from minus, set FIN=RIN=0/1 for 100[us].
kosaka 3:b6b9b8c7dce6 92 f_deadtime = 1; // deadtime is required
kosaka 3:b6b9b8c7dce6 93 _f_u_plus=1;
kosaka 3:b6b9b8c7dce6 94 }else{
kosaka 3:b6b9b8c7dce6 95 f_deadtime = 0; // deadtime is required
kosaka 3:b6b9b8c7dce6 96 }
kosaka 3:b6b9b8c7dce6 97 f_in=1; r_in=0; // set forward direction
kosaka 0:fe068497f773 98 }else if( u < 0 ){ // reverse: rotate to minus
kosaka 3:b6b9b8c7dce6 99 duty = -u/3.3;
kosaka 3:b6b9b8c7dce6 100 if(_f_u_plus==1){ // if plus to/from minus, set FIN=RIN=0/1 for 100[us].
kosaka 3:b6b9b8c7dce6 101 f_deadtime = 1; // deadtime is required
kosaka 3:b6b9b8c7dce6 102 _f_u_plus=0;
kosaka 3:b6b9b8c7dce6 103 }else{
kosaka 3:b6b9b8c7dce6 104 f_deadtime = 0; // deadtime is required
kosaka 3:b6b9b8c7dce6 105 }
kosaka 3:b6b9b8c7dce6 106 f_in=0; r_in=1; // set reverse direction
kosaka 0:fe068497f773 107 }else{// if( u == 0 ){ // stop mode
kosaka 3:b6b9b8c7dce6 108 duty = 0;
kosaka 3:b6b9b8c7dce6 109 f_deadtime = 0; // deadtime is required
kosaka 3:b6b9b8c7dce6 110 f_in=0; r_in=0; // set FIN & RIN
kosaka 0:fe068497f773 111 }
kosaka 3:b6b9b8c7dce6 112
kosaka 3:b6b9b8c7dce6 113 if( f_deadtime==1 ){// making deadtime
kosaka 3:b6b9b8c7dce6 114 FIN=0; RIN=0; // set upper&lower arm zero
kosaka 3:b6b9b8c7dce6 115 wait(DEADTIME);
kosaka 3:b6b9b8c7dce6 116 }
kosaka 3:b6b9b8c7dce6 117 #ifdef USE_PWM // H bridge PWM mode: Vref=Vcc, FIN,2 = PWM or 0
kosaka 3:b6b9b8c7dce6 118 FIN = duty*(float)f_in; RIN = duty*(float)r_in; // setting pwm FIN & RIN
kosaka 4:6ccbf4d3cb6d 119 analog_out = 1; // setting Vref=UMAX, but Vref=Vcc is better.
kosaka 3:b6b9b8c7dce6 120 #else // Analog mode: Vref=analog, FIN, RIN = 1 or 0)
kosaka 3:b6b9b8c7dce6 121 FIN = f_in; RIN = r_in; // setting FIN & RIN
kosaka 3:b6b9b8c7dce6 122 analog_out = duty; // setting Vref : PID write DA, range is 0-1. Output voltage 0-3.3v
kosaka 3:b6b9b8c7dce6 123 #endif
kosaka 0:fe068497f773 124 }
kosaka 0:fe068497f773 125
kosaka 0:fe068497f773 126 void controller(void const *argument) { // if rtos. current controller & velocity controller
kosaka 0:fe068497f773 127 //void controller() { // if ticker. current controller & velocity controller
kosaka 3:b6b9b8c7dce6 128 void u2Hbridge(float); // input u to TA7291 driver
kosaka 0:fe068497f773 129 float e_old, wt;
kosaka 0:fe068497f773 130 float y, u; // to avoid time shift
kosaka 0:fe068497f773 131
kosaka 0:fe068497f773 132 debug_p17 = 1; // for debug: processing time check
kosaka 0:fe068497f773 133 // if(debug_p17 == 1) debug_p17=0;else debug_p17=1; // for debug: sampling time check
kosaka 0:fe068497f773 134
kosaka 0:fe068497f773 135 _count+=1;
kosaka 0:fe068497f773 136 // y_old = _y; // y_old=y(t-TS) is older than y by 1 sampling time TS[s]. update data
kosaka 0:fe068497f773 137 #ifdef SIMULATION
kosaka 3:b6b9b8c7dce6 138 y = _y + TS/0.1*(0.2*_u*100-_y); //=(1-TS/0.1)*_y + 0.2*TS/0.1*_u; // G = 0.2/(0.1s+1)
kosaka 0:fe068497f773 139 //debug[0]=_u;//plus
kosaka 0:fe068497f773 140 #else
kosaka 0:fe068497f773 141 // semaphore1.wait(); //
kosaka 0:fe068497f773 142 y = (float)encoder.getPulses()/(float)N_ENC*2.0*PI; // get angle [rad] from encoder
kosaka 0:fe068497f773 143 // semaphore1.release(); //
kosaka 0:fe068497f773 144 #endif
kosaka 1:b91aeb5673f3 145 //#ifdef R_SIN
kosaka 1:b91aeb5673f3 146 // #define RMAX (100./180.*PI)
kosaka 0:fe068497f773 147 #define RMIN 0
kosaka 0:fe068497f773 148 wt = _freq_u *2.0*PI*_time;
kosaka 0:fe068497f773 149 if(wt>2*PI){ wt -= 2*PI*(float)((int)(wt/(2.0*PI)));}
kosaka 1:b91aeb5673f3 150 _r = sin(wt ) * (_rmax-RMIN)/2.0 + (_rmax+RMIN)/2.0;
kosaka 1:b91aeb5673f3 151 #ifndef R_SIN
kosaka 1:b91aeb5673f3 152 if( _r>=(_rmax+RMIN)/2.0 ) _r = _rmax;
kosaka 1:b91aeb5673f3 153 else _r = 0;
kosaka 0:fe068497f773 154 #endif
kosaka 0:fe068497f773 155 e_old = _e; // e_old=e(t-TS) is older than e by 1 sampling time TS[s]. update data
kosaka 0:fe068497f773 156 _e = _r - y; // error e(t)
kosaka 0:fe068497f773 157 if( _f_umax==0 ){
kosaka 0:fe068497f773 158 _eI = _eI + TS*_e; // integral of e(t)
kosaka 0:fe068497f773 159 }
kosaka 0:fe068497f773 160
kosaka 0:fe068497f773 161 u = _Kp*_e + _Kd*(_e-e_old)/TS + _Ki*_eI; // PID output u(t)
kosaka 0:fe068497f773 162 //debug[0]=_e;//minus
kosaka 0:fe068497f773 163 //debug[0]=u;//minus
kosaka 0:fe068497f773 164
kosaka 0:fe068497f773 165 // u is saturated? for anti-windup
kosaka 0:fe068497f773 166 if( u>UMAX ){
kosaka 0:fe068497f773 167 _eI -= (u-UMAX)/_Ki; if(_eI<0){ _eI=0;}
kosaka 0:fe068497f773 168 u = UMAX;
kosaka 0:fe068497f773 169 // _f_umax = 1;
kosaka 0:fe068497f773 170 } else if( u<UMIN ){
kosaka 0:fe068497f773 171 _eI -= (u-UMIN)/_Ki; if(_eI>0){ _eI=0;}
kosaka 0:fe068497f773 172 u = UMIN;
kosaka 0:fe068497f773 173 // _f_umax = 1;
kosaka 0:fe068497f773 174 }else{
kosaka 0:fe068497f773 175 _f_umax = 0;
kosaka 0:fe068497f773 176 }
kosaka 0:fe068497f773 177 //#define CONTROL_MODE 2 // 0:PID control, 1:Frequency response, 2:Step response
kosaka 6:16bee943a9fa 178 #if CONTROL_MODE==1||CONTROL_MODE==2 // frequency response, or Step response
kosaka 0:fe068497f773 179 wt = _freq_u *2.0*PI*_time;
kosaka 0:fe068497f773 180 if(wt>2*PI) wt -= 2*PI*(float)((int)(wt/2.0*PI));
kosaka 0:fe068497f773 181 u = sin(wt ) * (UMAX-UMIN)/2.0 + (UMAX+UMIN)/2.0;
kosaka 0:fe068497f773 182 #endif
kosaka 0:fe068497f773 183 #if CONTROL_MODE==2 // Step response
kosaka 0:fe068497f773 184 if( u>=0 ) u = UMAX;
kosaka 0:fe068497f773 185 else u = UMIN;
kosaka 0:fe068497f773 186 #endif
kosaka 6:16bee943a9fa 187 #if CONTROL_MODE==3 // u=rand() to identify motor transfer function G(s) from V to angle
kosaka 6:16bee943a9fa 188 if(count2==(int)(TS2/TS)){
kosaka 6:16bee943a9fa 189 u = ((float)rand()/RAND_MAX*2.0-1.0) * (UMAX-1.5)/2.0 + (UMAX+1.5)/2.0;
kosaka 6:16bee943a9fa 190 }else{
kosaka 6:16bee943a9fa 191 u = _u;
kosaka 6:16bee943a9fa 192 }
kosaka 6:16bee943a9fa 193 #endif
kosaka 7:613febb8f028 194 #if CONTROL_MODE==4 // FFT identification, u=repetive signal
kosaka 7:613febb8f028 195 if(count2==(int)(TS2/TS)){
kosaka 7:613febb8f028 196 u = data[count3][4];
kosaka 7:613febb8f028 197 }else{
kosaka 7:613febb8f028 198 u = _u;
kosaka 7:613febb8f028 199 }
kosaka 7:613febb8f028 200 #endif
kosaka 0:fe068497f773 201 //debug[0]=u;//minus
kosaka 3:b6b9b8c7dce6 202 u2Hbridge(u); // input u to TA7291 driver
kosaka 0:fe068497f773 203
kosaka 0:fe068497f773 204 //-------- update data
kosaka 0:fe068497f773 205 _time += TS; // time
kosaka 0:fe068497f773 206 _y = y;
kosaka 0:fe068497f773 207 _u = u;
kosaka 0:fe068497f773 208 //debug[0]=_u;//minus
kosaka 0:fe068497f773 209 //debug[0]=_eI;
kosaka 0:fe068497f773 210 debug[0]=_r;
kosaka 0:fe068497f773 211 #ifdef GOOD_DATA
kosaka 0:fe068497f773 212 if(count2==(int)(TS2/TS)){
kosaka 0:fe068497f773 213 // j=0; if(_count>=j&&_count<j+1000){i=_count-j; data[i][0]=_r; data[i][1]=debug[0]; data[i][2]=_y; data[i][3]=_time; data[i][4]=_u;}
kosaka 2:e056793d6fc5 214 if( count3<1000 ){
kosaka 2:e056793d6fc5 215 data[count3][0]=_r; data[count3][1]=debug[0]; data[count3][2]=_y; data[count3][3]=_time; data[count3][4]=_u;
kosaka 2:e056793d6fc5 216 count3++;
kosaka 2:e056793d6fc5 217 }
kosaka 0:fe068497f773 218 count2 = 0;
kosaka 0:fe068497f773 219 }
kosaka 0:fe068497f773 220 count2++;
kosaka 0:fe068497f773 221 #endif
kosaka 0:fe068497f773 222 //-------- update data
kosaka 0:fe068497f773 223
kosaka 0:fe068497f773 224 debug_p17 = 0; // for debug: processing time check
kosaka 0:fe068497f773 225 }
kosaka 0:fe068497f773 226
kosaka 0:fe068497f773 227 void main1() {
kosaka 0:fe068497f773 228 RtosTimer timer_controller(controller);
kosaka 0:fe068497f773 229 FILE *fp; // save data to PC
kosaka 0:fe068497f773 230 #ifdef GOOD_DATA
kosaka 0:fe068497f773 231 int i;
kosaka 0:fe068497f773 232
kosaka 0:fe068497f773 233 count3=0;
kosaka 0:fe068497f773 234 #endif
kosaka 3:b6b9b8c7dce6 235 u2Hbridge(0); // initialize H bridge to stop mode
kosaka 0:fe068497f773 236 _count=0;
kosaka 0:fe068497f773 237 _time = 0; // time
kosaka 0:fe068497f773 238 _e = _eI = 0;
kosaka 1:b91aeb5673f3 239 encoder.reset(); // set encoder counter zero
kosaka 0:fe068497f773 240 _y = (float)encoder.getPulses()/(float)N_ENC*2.0*PI; // get angle [rad] from encoder
kosaka 0:fe068497f773 241 _r = _r + _y;
kosaka 1:b91aeb5673f3 242 // if( _r>2*PI ) _r -= _r-2*PI;
kosaka 0:fe068497f773 243
kosaka 0:fe068497f773 244 pc.printf("Control start!!\r\n");
kosaka 0:fe068497f773 245 if ( NULL == (fp = fopen( "/local/data.csv", "w" )) ){ error( "" );} // save data to PC
kosaka 3:b6b9b8c7dce6 246 #ifdef USE_PWM
kosaka 3:b6b9b8c7dce6 247 FIN.period( 1.0 / PWM_FREQ ); // PWM period [s]. Common to all PWM
kosaka 3:b6b9b8c7dce6 248 #endif
kosaka 0:fe068497f773 249 // controller_ticker.attach(&controller, TS ); // period [s]
kosaka 0:fe068497f773 250 timer_controller.start((unsigned int)(TS*1000.)); // Sampling period[ms]
kosaka 0:fe068497f773 251
kosaka 0:fe068497f773 252 // for ( i = 0; i < (unsigned int)(TMAX/TS2); i++ ) {
kosaka 0:fe068497f773 253 while ( _time <= TMAX ) {
kosaka 0:fe068497f773 254 // BUG!! Dangerous if TS2<0.1 because multi interrupt by fprintf is not prohibited! 1st aug of fprintf will be destroyed.
kosaka 0:fe068497f773 255 // fprintf returns before process completed.
kosaka 0:fe068497f773 256 //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 257 //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 258 #ifndef GOOD_DATA
kosaka 0:fe068497f773 259 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 260 #endif
kosaka 0:fe068497f773 261 Thread::wait((unsigned int)(TS2*1000.)); //[ms]
kosaka 0:fe068497f773 262 }
kosaka 0:fe068497f773 263 timer_controller.stop(); // rtos timer stop
kosaka 3:b6b9b8c7dce6 264 u2Hbridge(0); // initialize H bridge to stop mode
kosaka 0:fe068497f773 265 #ifdef GOOD_DATA
kosaka 0:fe068497f773 266 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 267 #endif
kosaka 0:fe068497f773 268 fclose( fp ); // release mbed USB drive
kosaka 0:fe068497f773 269 pc.printf("Control completed!!\r\n\r\n");
kosaka 0:fe068497f773 270 }
kosaka 0:fe068497f773 271
kosaka 0:fe068497f773 272 void thread_print2PC(void const *argument) {
kosaka 0:fe068497f773 273 while (true) {
kosaka 1:b91aeb5673f3 274 pc.printf("%8.1f[s]\t%8.5f[V]\t%4d [deg]\t%8.2f\r\n", _time, _u, (int)(_y/(2*PI)*360.0), debug[0]/(2*PI)*360.0); // print to tera term
kosaka 0:fe068497f773 275 Thread::wait(200);
kosaka 0:fe068497f773 276 }
kosaka 0:fe068497f773 277 }
kosaka 0:fe068497f773 278
kosaka 0:fe068497f773 279 void main2(void const *argument) {
kosaka 0:fe068497f773 280 #if CONTROL_MODE==0 // PID control
kosaka 0:fe068497f773 281 char f;
kosaka 0:fe068497f773 282 float val;
kosaka 0:fe068497f773 283 #endif
kosaka 7:613febb8f028 284 #if CONTROL_MODE==4 // FFT identification, u=repetive signal
kosaka 7:613febb8f028 285 int i, j;
kosaka 7:613febb8f028 286 float max_u;
kosaka 7:613febb8f028 287 #endif
kosaka 7:613febb8f028 288
kosaka 0:fe068497f773 289 while(true){
kosaka 7:613febb8f028 290 #if CONTROL_MODE==4 // FFT identification, u=repetive signal
kosaka 7:613febb8f028 291 max_u = 0;
kosaka 7:613febb8f028 292 for( i=0;i<1000;i++ ){ // u=data[i][4]: memory for FFT identification input signal.
kosaka 7:613febb8f028 293 data[i][4] = sin(_freq_u*2*PI * i*TS2); // _u_freq = 10/2 * i [Hz]
kosaka 7:613febb8f028 294 if( data[i][4]>max_u ){ max_u=data[i][4];}
kosaka 7:613febb8f028 295 }
kosaka 7:613febb8f028 296 for( j=1;j<50;j++ ){
kosaka 7:613febb8f028 297 for( i=0;i<1000;i++ ){
kosaka 7:613febb8f028 298 data[i][4] += sin((float)(j+1)*_freq_u*2*PI * i*TS2);
kosaka 7:613febb8f028 299 if( data[i][4]>max_u ){ max_u=data[i][4];}
kosaka 7:613febb8f028 300 }
kosaka 7:613febb8f028 301 }
kosaka 7:613febb8f028 302 for( i=0;i<1000;i++ ){
kosaka 7:613febb8f028 303 // data[i][4] *= UMAX/max_u;
kosaka 7:613febb8f028 304 data[i][4] = (data[i][4]/max_u+3)/4*UMAX;
kosaka 7:613febb8f028 305 }
kosaka 7:613febb8f028 306 #endif
kosaka 0:fe068497f773 307 main1();
kosaka 0:fe068497f773 308
kosaka 0:fe068497f773 309 #if CONTROL_MODE>=1 // frequency response, or Step response
kosaka 7:613febb8f028 310 pc.printf("Input u(t) Frequency[Hz]? (if 9, reset mbed)...");
kosaka 0:fe068497f773 311 pc.scanf("%f",&_freq_u);
kosaka 0:fe068497f773 312 pc.printf("%8.3f[Hz]\r\n", _freq_u); // print to tera term
kosaka 7:613febb8f028 313 if(_freq_u==9){ mbed_reset();}
kosaka 0:fe068497f773 314 #else // PID control
kosaka 1:b91aeb5673f3 315 // #ifdef R_SIN
kosaka 1:b91aeb5673f3 316 // pc.printf("Reference signal r(t) Frequency[Hz]?...");
kosaka 1:b91aeb5673f3 317 // pc.scanf("%f",&_freq_u);
kosaka 1:b91aeb5673f3 318 // pc.printf("%8.3f[Hz]\r\n", _freq_u); // print to tera term
kosaka 1:b91aeb5673f3 319 // #endif
kosaka 5:2e53814aae4c 320 pc.printf("Kp=%f, Ki=%f, Kd=%f, r=%f[deg], %f Hz\r\n",_Kp, _Ki, _Kd, _rmax*180./PI, _freq_u);
kosaka 7:613febb8f028 321 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]. 9)reset mbed ?");
kosaka 0:fe068497f773 322 f=pc.getc()-48; //int = char-48
kosaka 0:fe068497f773 323 pc.printf("\r\n Value?... ");
kosaka 1:b91aeb5673f3 324 if(f>=1&&f<=5){ pc.scanf("%f",&val);}
kosaka 0:fe068497f773 325 pc.printf("%8.3f\r\n", val); // print to tera term
kosaka 0:fe068497f773 326 if(f==1){ _Kp = val;}
kosaka 0:fe068497f773 327 if(f==2){ _Ki = val;}
kosaka 0:fe068497f773 328 if(f==3){ _Kd = val;}
kosaka 1:b91aeb5673f3 329 if(f==4){ _freq_u = val;}
kosaka 1:b91aeb5673f3 330 if(f==5){ _rmax = val/180.*PI;}
kosaka 7:613febb8f028 331 if(f==9){ mbed_reset();}
kosaka 1:b91aeb5673f3 332 pc.printf("Kp=%f, Ki=%f, Kd=%f, r=%f[deg], %f Hz\r\n",_Kp, _Ki, _Kd, _rmax*180./PI, _freq_u);
kosaka 0:fe068497f773 333 #endif
kosaka 0:fe068497f773 334 }
kosaka 0:fe068497f773 335 }
kosaka 0:fe068497f773 336 int main() {
kosaka 0:fe068497f773 337 // void main1();
kosaka 0:fe068497f773 338 Thread save2PC(main2,NULL,osPriorityBelowNormal);
kosaka 0:fe068497f773 339 Thread print2PC(thread_print2PC,NULL,osPriorityLow);
kosaka 0:fe068497f773 340
kosaka 0:fe068497f773 341 // osStatus set_priority(osPriority osPriorityBelowNormal );
kosaka 0:fe068497f773 342 // Priority of Thread (RtosTimer has no priority?)
kosaka 0:fe068497f773 343 // osPriorityIdle = -3, ///< priority: idle (lowest)--> then, mbed ERROR!!
kosaka 0:fe068497f773 344 // osPriorityLow = -2, ///< priority: low
kosaka 0:fe068497f773 345 // osPriorityBelowNormal = -1, ///< priority: below normal
kosaka 0:fe068497f773 346 // osPriorityNormal = 0, ///< priority: normal (default)
kosaka 0:fe068497f773 347 // osPriorityAboveNormal = +1, ///< priority: above normal
kosaka 0:fe068497f773 348 // osPriorityHigh = +2, ///< priority: high
kosaka 0:fe068497f773 349 // osPriorityRealtime = +3, ///< priority: realtime (highest)
kosaka 0:fe068497f773 350 // osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority
kosaka 0:fe068497f773 351 }