機械工学実験1

Dependencies:   PID QEI mbed

Revision:
2:d5f964932fce
Parent:
1:35d14e37db1b
Child:
3:2a9a433d93c3
--- a/main.cpp	Thu Oct 23 08:33:07 2014 +0000
+++ b/main.cpp	Fri Oct 24 05:33:28 2014 +0000
@@ -7,6 +7,7 @@
 //define
 #define PULSE_PER_REVOLUTION 200
 #define PID_RATE 10     //wait **ms. If you change this value, response will be slightly different.
+#define GEAR_RATIO 2
 
 #define KP_SCALE 10
 #define TI_SCALE 20
@@ -32,8 +33,8 @@
 
 //structure of experimentation result
 typedef struct{
-    float t;
-    float rpm;
+    unsigned short t;
+    short rpm;
 } result;
 
 //prototype declaration
@@ -80,7 +81,7 @@
                     pwm = vol;
                     if(i1 >= 20){           //show duty ratio and rpm in PC every sec
                         t = timer.read();
-                        rpm = (float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / t * 2;  //reduction ratio 1/2
+                        rpm = (float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / t * GEAR_RATIO;  //reduction ratio 1/2
                         pc.printf("Duty Ratio = %.3f , %6d RPM\n", vol, (int)rpm);
                         encoder.reset();
                         i1 = 0;
@@ -93,6 +94,7 @@
             }
             /*********************mode 2**********************/
             case 2:{        //mode 2: step input
+                float dt = 0, tnow = 0, tpre = 0;
                 exp[0].t = 0;
                 exp[0].rpm = 0;
                 encoder.reset();
@@ -101,8 +103,11 @@
                 pwm = 1;        //duty ratio 1
                 for(int i = 0; i < 15*1000/PID_RATE; i++){      //variable assignment in every PID_RATE
                     wait_ms(PID_RATE);
-                    exp[i+1].t = timer.read();
-                    exp[i+1].rpm = (float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / (exp[i+1].t - exp[i].t) * 2;
+                    tpre = tnow;
+                    tnow = timer.read();
+                    exp[i+1].t = (unsigned short)(tnow * 1000);
+                    dt = tnow - tpre;
+                    exp[i+1].rpm = (short)((float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / dt * GEAR_RATIO);
                     encoder.reset();
                 }
                 for(int i = 100; i >= 0; i--){      //stopping motor slowly
@@ -118,7 +123,7 @@
                 }
                 fprintf(fp,"Time , RPM\n");
                 for(int i = 0; i <= 15*1000/PID_RATE; i++){     //output results
-                    fprintf(fp,"%f , %f\n", exp[i].t, exp[i].rpm);
+                    fprintf(fp,"%f , %d\n", (float)exp[i].t / 1000.0, exp[i].rpm);
                 }
                 fclose(fp);
                 Buzzer(4);      //ring finish buzzer
@@ -126,7 +131,7 @@
             }
             /*********************mode 3**********************/
             case 3:{        //mode 3: PID control
-                float kp = 0, ti = 0, td = 0, max_rpm = 0, target_rpm = 0, reduction = 0;
+                float kp = 0, ti = 0, td = 0, max_rpm = 0, target_rpm = 0, reduction = 0, dt = 0, tnow = 0, tpre = 0, rpmnow = 0;
                 encoder.reset();
                 timer.reset();
                 
@@ -146,9 +151,13 @@
                 exp[0].rpm = 0;
                 timer.start();      //count start
                 for(int i = 0; i < 15*1000/PID_RATE; i++){      //PID control loop(15 seconds)
-                    exp[i+1].t = timer.read();
-                    exp[i+1].rpm = (float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / (exp[i+1].t - exp[i].t) * 2;
-                    pid.setProcessValue(exp[i+1].rpm);
+                    tpre = tnow;
+                    tnow = timer.read();
+                    exp[i+1].t = (unsigned short)(tnow * 1000);
+                    dt = tnow - tpre;
+                    rpmnow = (float)encoder.getPulses() / 2 / PULSE_PER_REVOLUTION * 60 / dt * GEAR_RATIO;
+                    exp[i+1].rpm = (short)rpmnow;
+                    pid.setProcessValue(rpmnow);
                     pwm = pid.compute();
                     encoder.reset();
                     wait_ms(PID_RATE);
@@ -167,7 +176,7 @@
                 fprintf(fp,", kp = %.6f  ki = %.6f  kd = %.6f\n", (float)KP, (float)TI, (float)TD);
                 fprintf(fp,"Time , RPM\n");
                 for(int i = 0; i <= 15*1000/PID_RATE; i++){     //output results
-                    fprintf(fp,"%f , %f\n", exp[i].t, exp[i].rpm);
+                    fprintf(fp,"%f , %d\n", (float)exp[i].t / 1000.0, exp[i].rpm);
                 }
                 fclose(fp);
                 Buzzer(5);      //ring finish buzzer