Betterfrost / Mbed 2 deprecated timer_based_1kHz_ramp

Dependencies:   mbed

Fork of BoxBrovoEcho_OCt3 by Betterfrost

Revision:
3:d56766563719
Parent:
2:98bc0c7668e3
Child:
4:2c5dbea00157
--- a/main.cpp	Wed Sep 05 21:06:19 2018 +0000
+++ b/main.cpp	Tue Sep 11 17:18:10 2018 +0000
@@ -18,24 +18,27 @@
 DigitalOut led1(LED1, 0);
 DigitalOut led2(LED2, 0);
 DigitalOut led3(LED3, 0);
-DigitalOut RemoteLED(LED4, 0);
+DigitalOut led4(LED4, 0);
 
 InterruptIn ButtonPress(p21);
 
 AnalogIn current(p15);
+AnalogIn voltage(p16);
 
 // ------------------------- Main Parameters  ------------------------------- //
-float resistance = 15.0;
+float resistance = 10.0;
 float V_batt = 160.0;
-float I_out = 7.0;
+float I_out = 10.0;
 float V_out = 0.0;
 float freq = 50;     //switching frequency in Hz
 
 // -------------------- Current Sensor Parameters  --------------------------- //
-float HSens_gain = 39.85;
-float I_LIMIT = 13.0;
-float i_offset = 0.57;
+float HSens_gain = 31;
+float I_LIMIT = 9.0;
+float i_offset = 0.58;
 float i_load = 0;
+float i_load_sum = 0;
+float i_load_avg = 0;
 float i_loadpre = 0;
 float i_avg = 0;
 float i_sum = 0;
@@ -43,10 +46,13 @@
 int count = 0;
 int c_i = 0;
 int a = 0;
+int avg_c = 0;
+float v_in = 0;
+int once = 0;
 
 // ------------------------- Ramp up parameters  ----------------------------- //
-float N = 10;        // number of steps
-float tramp = 10000;  // ramp time in ms
+float N = 20;        // number of steps
+float tramp = 5000;  // ramp time in ms
 float tstep = 0;     // step time
 float toff = 1;    // cycle off time
 float ton_sat = 0;   // ON time saturation value
@@ -64,16 +70,40 @@
 // ----------------Current reading and limit testing---------------------------
 void task1()
 {
-    task_1_pin = !task_1_pin;
-    i_load = HSens_gain *( (1.0-current.read())- i_offset );
+    task_1_pin = !task_1_pin;    
+    i_load = HSens_gain *3.3*(v_in/3)*( (1.0-current.read())- (1-(v_in*0.5/3)));
 
-    if( (i_load > I_LIMIT) || (i_load > i_loadpre+0.5) ) {
+    
+    //if((i_load > I_LIMIT)||(abs(i_load_avg-i_loadpre)>1)&&(i_loadpre != 0)) //Overcurrrent + resistance checking + first real value of i_loadpre
+    if(i_load_avg > I_LIMIT) //Overcurrrent
+    {
         c_i++;
-        if(c_i == 5) {
-            led3 = 1;
+        if(c_i == 5) 
+        {
+            
+            led3 = 1;            
             c_i = 0;
         }
     }
+    
+    if((abs(i_load_avg-i_loadpre) > 1)&&(i_loadpre != 0)) // resistance checking
+    {
+        led2 = 1;
+    }
+    
+    
+    
+    
+    
+    avg_c++;
+    i_load_sum = i_load_sum + i_load;   //integration
+    if(avg_c == 200)
+    {
+        avg_c = 0;
+        i_load_avg = (i_load_sum/200);
+        i_load_sum = 0;
+    }
+    
 }
 
 
@@ -82,7 +112,9 @@
 void task2()
 {
     task_2_pin = !task_2_pin;
-    pc.printf("\n\r Current = %f", i_load );
+    pc.printf("\r %f", i_load_avg );
+    //pc.printf("\r %f", v_in);
+    //pc.printf("\r %f", i_loadpre );
 }
 
 
@@ -90,13 +122,13 @@
 //----------------------------Remote Control----------------------------------
 void button()
 {
-    led1 = 1;
+    led1 = !led1;
 }
 void task3()
 {
     task_3_pin = !task_3_pin;
-    ButtonPress.rise(&button);
-    led1 = 0;
+    ButtonPress.rise(&button);   
+    v_in = 3.3*(voltage.read());
 }
 
 
@@ -104,7 +136,7 @@
 //----------------------------XXXXXXXXXXXXXXX----------------------------------
 void task4()
 {
-    led2 = !led2;
+    //led2 = !led2;
 }
 
 // -------------------------- Power Convertor ------------------------------- //
@@ -121,29 +153,6 @@
     wait_ms(toff);
 }
 
-// ------------------------------ Setup  ------------------------------------- //
-// ------------------------ Initial Current value ---------------------------- //
-void setup()
-{
-
-    count = 0;
-    while(count < NUM_SAMPLES) {
-        IGBT_G= 1;          // set IGBT Ground side pin to high
-        wait_us(50);
-        THY_S= 1;
-        wait_us(10);
-        THY_S= 0;
-        wait_us(200);
-        i_loadpre = HSens_gain *( (1.0-current.read())- i_offset );
-        wait_ms(ton);
-
-        IGBT_G.write(0);
-        wait_ms(toff);
-        count++;
-    }
-    count = 0;
-}
-
 // ------------------------------- MAIN ------------------------------------- //
 int main()
 {
@@ -159,6 +168,7 @@
     led1 = 0;
     led2 = 0;
     led3 = 0;
+    led4 = 0;
 
     scheduler1.attach(&task1, 0.0001); // R check
     scheduler2.attach(&task2, 0.2);    // Reading Hall Sensor
@@ -167,20 +177,18 @@
 
 
     /* Remote Start Prompt */
-    RemoteLED=0;
+    
     while(1) {
         if(led1==1) {
             break; // Waiting for start Button (pin21)
         }
     }
     led1 = 0;
-    RemoteLED=1;
+    led2 = 0;    
+    wait_ms(1);    
 
 
-    setup();
-
-
-    //--------RAMP UP ----------//
+//--------Ramp up Start ----------//
 
     d_sat = resistance*I_out/V_batt;
 
@@ -197,15 +205,13 @@
     tstep = tramp/N;                     //step time calculation
     while(c < (int)N) {
         
-        d = d + (d_sat/(tramp/1000));           //duty cycle increment
+        //d = d + (d_sat/(tramp/1000));           //duty cycle increment
+        d = d + (d_sat/N);           //duty cycle increment
         if (d>d_sat){d=d_sat;}
         ton = d/freq;
         toff = (1/freq)-ton;             //calculation of time off
         ton = ton*1000;
-        toff = toff*1000;
-        if(toff < toff_sat) {            //toff saturation
-            toff = toff_sat;
-        }
+        toff = toff*1000;        
         Ncycles = (int)(tstep/(ton+toff));   //calculation of the number of cycles
         i = 0;
         while(i < Ncycles) {
@@ -214,8 +220,9 @@
         }
         c++;
     }
-
-
+    //led3 = 0;
+    led4 = 1;   //indicates the ramp up is done
+//--------Ramp up End ----------//
 
     while(1) {
 
@@ -224,11 +231,28 @@
         toff = (1/freq)-ton;
         ton = ton*1000;
         toff = toff*1000;
-        if( (led3==1)||(led1==1) ) {
+        
+        if((led1 == 1)||(led2 == 1)||(led3 == 1)) //test conditions for break
+        {
             break;
-        }
+        }          
+                
         pw();
+        if(once==0)
+        {
+            once = 1;
+            i_loadpre = i_load_avg; //acquire reference value
+        } 
     }
-    RemoteLED = 0;
-    NVIC_SystemReset();
+    
+    while(1) // wait for the remote signal before resetting
+    {
+        if(led1 == 1)
+        {
+            break;
+        }  
+    }
+    
+    NVIC_SystemReset(); 
+      
 }
\ No newline at end of file