Betterfrost / Mbed 2 deprecated timer_based_1kHz_ramp

Dependencies:   mbed

Fork of BoxBrovoEcho_OCt3 by Betterfrost

Revision:
2:98bc0c7668e3
Parent:
1:fa78d980522c
Child:
3:d56766563719
diff -r fa78d980522c -r 98bc0c7668e3 main.cpp
--- a/main.cpp	Tue Sep 04 16:18:52 2018 +0000
+++ b/main.cpp	Wed Sep 05 21:06:19 2018 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "math.h"
- 
+
 Ticker scheduler1;
 Ticker scheduler2;
 Ticker scheduler3;
@@ -10,133 +10,225 @@
 
 DigitalOut task_1_pin(p8, 0);
 DigitalOut task_2_pin(p9, 0);
+DigitalOut task_3_pin(p10, 0);
 
-DigitalOut THY_S(p5, 0);  
-DigitalOut IGBT_G(p6, 0); 
- 
+DigitalOut THY_S(p5, 0);
+DigitalOut IGBT_G(p6, 0);
+
 DigitalOut led1(LED1, 0);
 DigitalOut led2(LED2, 0);
 DigitalOut led3(LED3, 0);
+DigitalOut RemoteLED(LED4, 0);
 
-DigitalIn ButtonPress(p21);
+InterruptIn ButtonPress(p21);
 
 AnalogIn current(p15);
 
-
-
-
+// ------------------------- Main Parameters  ------------------------------- //
+float resistance = 15.0;
+float V_batt = 160.0;
+float I_out = 7.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 i_load = 0;
+float i_loadpre = 0;
+float i_avg = 0;
+float i_sum = 0;
+int NUM_SAMPLES = 5;
+int count = 0;
 int c_i = 0;
+int a = 0;
 
 // ------------------------- Ramp up parameters  ----------------------------- //
 float N = 10;        // number of steps
-float tramp = 5000;  // ramp time in ms
+float tramp = 10000;  // ramp time in ms
 float tstep = 0;     // step time
-float toff = 0;      // cycle off time
-float toff_sat = 1;  // off time saturation value - determines the final duty cycle
-float ton = 0;      // fixed on time
+float toff = 1;    // cycle off time
+float ton_sat = 0;   // ON time saturation value
+float toff_sat = 0;  // OFF time saturation value - determines the final duty cycle
+float ton = 1;     // on time
 float d = 0;         // duty cycle starting point
-float d_sat = 0.9;   // determines the final duty cycle
+float d_sat = 0.0;   // determines the final duty cycle
 int c = 0;           // step counter
 int i = 0;           // cycle counter
 int Ncycles = 0;     // Number of cycles
 
 
-float resistance = 15.0;
-float V_batt = 160.0;
-float I_out = 10.0;
-float V_out = 0.0;
-float freq = 50;    //switching frequency in Hz
+
+// ----------------------------- Task 1 ------------------------------------- //
+// ----------------Current reading and limit testing---------------------------
+void task1()
+{
+    task_1_pin = !task_1_pin;
+    i_load = HSens_gain *( (1.0-current.read())- i_offset );
+
+    if( (i_load > I_LIMIT) || (i_load > i_loadpre+0.5) ) {
+        c_i++;
+        if(c_i == 5) {
+            led3 = 1;
+            c_i = 0;
+        }
+    }
+}
+
 
-// -------------------------- RampUp RunTime  ------------------------------- //
-int RunTime = 300;
-int X = 0;
-int Y = 0;
-int RampUp = 0;
+// ----------------------------- Task 2 ------------------------------------- //
+//------------------------Serial communication----------------------------------
+void task2()
+{
+    task_2_pin = !task_2_pin;
+    pc.printf("\n\r Current = %f", i_load );
+}
+
+
+// ------------------------------ Task 3 ------------------------------------- //
+//----------------------------Remote Control----------------------------------
+void button()
+{
+    led1 = 1;
+}
+void task3()
+{
+    task_3_pin = !task_3_pin;
+    ButtonPress.rise(&button);
+    led1 = 0;
+}
 
 
+// ------------------------------ Task 4 ------------------------------------- //
+//----------------------------XXXXXXXXXXXXXXX----------------------------------
+void task4()
+{
+    led2 = !led2;
+}
 
+// -------------------------- Power Convertor ------------------------------- //
+void pw()
+{
+    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);
+    wait_ms(ton);
+    IGBT_G.write(0);
+    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()
+{
+    pc.baud (115200);
+    NVIC_SetPriority(TIMER3_IRQn, 0);
+    // set mbed tickers to higher priority than other things
+
+    task_1_pin = 0;
+    task_2_pin = 0;
+    task_2_pin = 0;
+    THY_S = 0;
+    IGBT_G = 0;
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+
+    scheduler1.attach(&task1, 0.0001); // R check
+    scheduler2.attach(&task2, 0.2);    // Reading Hall Sensor
+    scheduler3.attach(&task3, 0.5);    // Turn OFF Power
+    //scheduler4.attach(&task4, 0.01); //
+
+
+    /* Remote Start Prompt */
+    RemoteLED=0;
+    while(1) {
+        if(led1==1) {
+            break; // Waiting for start Button (pin21)
+        }
+    }
+    led1 = 0;
+    RemoteLED=1;
+
+
+    setup();
+
+
+    //--------RAMP UP ----------//
+
+    d_sat = resistance*I_out/V_batt;
+
+    if (d_sat > 0.99) {
+        d_sat = 0.99; // duty cycle maximum value
+    }
+    ton_sat = d_sat/freq;
+    toff_sat = (1/freq)-ton_sat;
+    ton_sat = ton_sat*1000;
+    toff_sat = toff_sat*1000;
+
+    d = 0;
+    c = 0;
+    tstep = tramp/N;                     //step time calculation
+    while(c < (int)N) {
+        
+        d = d + (d_sat/(tramp/1000));           //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;
+        }
+        Ncycles = (int)(tstep/(ton+toff));   //calculation of the number of cycles
+        i = 0;
+        while(i < Ncycles) {
+            pw();
+            i++;
+        }
+        c++;
+    }
 
 
 
-// Current reading and limit testing 
-void task1() {    
-    task_1_pin = !task_1_pin;
-    i_load = current.read();
-    i_load = i_load*3.3;    
-    
-    if(i_load > 1.6)
-        {
-            c_i++;
-            if(c_i == 5)
-            {
-                led3 = 1;
-                c_i = 0;
-            }  
-        }
-        else
-        {
-            led3 = 0;
-        }
-}
-
-//Serial communication
-void task2() {    
-    task_2_pin = !task_2_pin;
-    pc.printf("\n\r Current = %f", i_load );    
-    
-}
+    while(1) {
 
-//
-void task3() {    
-    led1 = !led1;
-}
-
-void task4() {    
-    led2 = !led2;
-}
- 
-int main() {
-    pc.baud (115200);
-    NVIC_SetPriority(TIMER3_IRQn, 0); // set mbed tickers to higher priority than other things
-    
-    task_1_pin = 0;
-    task_2_pin = 0;
-    THY_S = 0;
-    IGBT_G = 0;  
-    led1 = 0;
-    led2 = 0;  
-    led3 = 0;   
-    
-    scheduler1.attach(&task1, 0.0001); // Four independent tasks
-    scheduler2.attach(&task2, 0.2); // 
-    //scheduler3.attach(&task3, 0.02); // 
-    //scheduler4.attach(&task4, 0.01); // 
- 
-    
-    while(1) {
-        
-        d = 0.5;
+        d = d_sat;            // 0>d<1 duty cycle
         ton = d/freq;
         toff = (1/freq)-ton;
         ton = ton*1000;
         toff = toff*1000;
-        if(led3 == 1)
-        {
+        if( (led3==1)||(led1==1) ) {
             break;
-        }        
-            
-        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);            
-        wait_ms(ton);
-        IGBT_G.write(0);        
-        wait_ms(toff);          
-        
+        }
+        pw();
     }
+    RemoteLED = 0;
+    NVIC_SystemReset();
 }
\ No newline at end of file