Betterfrost / Mbed 2 deprecated timer_based_1kHz_ramp

Dependencies:   mbed

Fork of BoxBrovoEcho_OCt3 by Betterfrost

Revision:
7:11433074d022
Parent:
6:c905afa84d8a
Child:
8:da7fc0c1a28a
diff -r c905afa84d8a -r 11433074d022 main.cpp
--- a/main.cpp	Wed Oct 03 13:51:07 2018 +0000
+++ b/main.cpp	Wed Oct 03 16:25:06 2018 +0000
@@ -30,7 +30,7 @@
 float V_batt = 300.0;
 float I_out = 5.0;
 float V_out = 0.0;
-float freq = 30;     //switching frequency in Hz
+float freq = 1200;     //switching frequency in Hz
 Timer TIMER;
 float RunTime = 60;  // in seconds
 
@@ -54,7 +54,7 @@
 int once = 0;
 
 // ------------------------- Ramp up parameters  ----------------------------- //
-float N = 20;        // number of steps
+float N = 100;        // number of steps
 float tramp = 10000;  // ramp time in ms
 float tstep = 0;     // step time
 float toff = 1;    // cycle off time
@@ -157,11 +157,10 @@
     wait_us(50);
     THY_S= 1;
     wait_us(10);
-    THY_S= 0;
-    wait_us(200);
-    wait_ms(ton);
+    THY_S= 0;    
+    wait_us(ton);
     IGBT_G.write(0);
-    wait_ms(toff);
+    wait_us(toff);
 }
 
 // ------------------------------- MAIN ------------------------------------- //
@@ -184,8 +183,7 @@
     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 */
     
@@ -205,24 +203,19 @@
 
     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;
+    }   
+    ton_sat = (float)((d_sat/freq)*1000000);        
+    toff_sat = (float)((1/freq)*1000000 - ton_sat); 
 
     d = 0;
     c = 0;
-    tstep = tramp/N;                     //step time calculation
+    tstep = tramp*1000/N;                     //step time calculation in us
     while(c < (int)N) {
         
-        //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;        
+        ton = (float)((d/freq)*1000000);        //in us     
+        toff = (float)((1/freq)*1000000 - ton); //in us               
         Ncycles = (int)(tstep/(ton+toff));   //calculation of the number of cycles
         i = 0;
         while(i < Ncycles) {
@@ -237,11 +230,10 @@
 
     while(1) {
 
-        d = d_sat;            // 0>d<1 duty cycle
-        ton = d/freq;
-        toff = (1/freq)-ton;
-        ton = ton*1000;
-        toff = toff*1000;
+        d = d_sat;            // 0>d<1 duty cycle        
+        //d = 0.3;
+        ton = (float)((d/freq)*1000000);        
+        toff = (float)((1/freq)*1000000 - ton);        
         
         if((led1 == 1)||(led2 == 1)||(led3 == 1)||(TIMER.read()>=RunTime)) //test conditions for break
         {