Betterfrost / Mbed 2 deprecated timer_based_1kHz_ramp

Dependencies:   mbed

Fork of BoxBrovoEcho_OCt3 by Betterfrost

Revision:
1:fa78d980522c
Parent:
0:b265ed6faf6c
Child:
2:98bc0c7668e3
diff -r b265ed6faf6c -r fa78d980522c main.cpp
--- a/main.cpp	Wed Aug 29 17:35:19 2018 +0000
+++ b/main.cpp	Tue Sep 04 16:18:52 2018 +0000
@@ -1,24 +1,95 @@
 #include "mbed.h"
+#include "math.h"
  
 Ticker scheduler1;
 Ticker scheduler2;
 Ticker scheduler3;
 Ticker scheduler4;
 
+Serial pc(USBTX, USBRX); // tx, rx
+
+DigitalOut task_1_pin(p8, 0);
+DigitalOut task_2_pin(p9, 0);
+
 DigitalOut THY_S(p5, 0);  
-DigitalOut IGBT_G(p6, 0);  
+DigitalOut IGBT_G(p6, 0); 
+ 
 DigitalOut led1(LED1, 0);
 DigitalOut led2(LED2, 0);
+DigitalOut led3(LED3, 0);
 
- 
+DigitalIn ButtonPress(p21);
+
+AnalogIn current(p15);
+
+
+
+
+
+
+float i_load = 0;
+int c_i = 0;
+
+// ------------------------- Ramp up parameters  ----------------------------- //
+float N = 10;        // number of steps
+float tramp = 5000;  // 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 d = 0;         // duty cycle starting point
+float d_sat = 0.9;   // 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
+
+// -------------------------- RampUp RunTime  ------------------------------- //
+int RunTime = 300;
+int X = 0;
+int Y = 0;
+int RampUp = 0;
+
+
+
+
+
+
+// Current reading and limit testing 
 void task1() {    
-    THY_S = !THY_S;
+    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() {    
-    IGBT_G = !IGBT_G;
+    task_2_pin = !task_2_pin;
+    pc.printf("\n\r Current = %f", i_load );    
+    
 }
 
+//
 void task3() {    
     led1 = !led1;
 }
@@ -28,18 +99,44 @@
 }
  
 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;     
+    led2 = 0;  
+    led3 = 0;   
     
-    scheduler1.attach(&task1, 0.005); // Four independent tasks
-    scheduler2.attach(&task2, 0.05); // 
-    scheduler3.attach(&task3, 0.5); // 
-    scheduler4.attach(&task4, 1); // 
+    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;
+        ton = d/freq;
+        toff = (1/freq)-ton;
+        ton = ton*1000;
+        toff = toff*1000;
+        if(led3 == 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);          
+        
     }
 }
\ No newline at end of file