12/mar ignicao funcionando

Dependencies:   mbed reScale TextLCD

Files at this revision

API Documentation at this revision

Comitter:
Marcelocostanzo
Date:
Tue Mar 12 19:01:55 2019 +0000
Parent:
6:111a7f1b978f
Commit message:
Ignicao funcionando 12/mar

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 111a7f1b978f -r 5cd0b64dea2a main.cpp
--- a/main.cpp	Fri Mar 01 10:07:05 2019 +0000
+++ b/main.cpp	Tue Mar 12 19:01:55 2019 +0000
@@ -1,31 +1,25 @@
 #include "mbed.h"
 #include "reScale.h"
 
-#define KP 0.8f
-#define KI 1.0f
-#define KD 0.01f
-#define offset 0.02f
+Ticker flipper;
+
+DigitalOut  controlLED(D13); 
+DigitalOut  Splug_1(D2);
+DigitalOut  Splug_2(D3);
+DigitalOut  Splug_3(D4);
+DigitalOut  Splug_4(D5);
+
+//Timer t1;
+Timer t2;
+
+Serial device(USBTX, USBRX); // tx, rx
+
+bool flag;
+float time_rpm = 0;
+int dwell = 2500;
 
 
-AnalogIn SETPOINT(A0);
-AnalogIn TPS1(A2);
-AnalogIn TPS2(A3);
-Ticker flipper;
-DigitalOut  controlLED(LED2); 
-DigitalOut  IN1A(D10);
-DigitalOut  IN2A(D8);
-PwmOut TBI(D9);
 
-//Timer t1;
-
-//Serial device(PB_6, PB_7); // tx, rx
-Serial device(USBTX, USBRX); // tx, rx
-
-reScale _scale(0.0f,1.0f,0.09f,0.83f); 
-
-float pwm=0, erro=0, proporcional=0, integrador=0;
-float TPS1_VAL, SETPOINT_VAL=0, SETPOINT_scaled=0;
-bool flag=0; 
 
 void flip() 
 {
@@ -33,85 +27,182 @@
     controlLED=!controlLED;
 }
 
-void open()
-{
-    IN1A=1;
-    IN2A=0;
+void flushSerialBuffer(void) //rotina para limpar o buffer da serial
+{ 
+    char char1 = 0;
+    while (device.readable()) 
+    { 
+        char1 = device.getc();
+    } return; 
 }
 
-void close()
+void set_parameters()
 {
-    IN1A=0;
-    IN2A=1;
-}
+    int uni, dez, cen, mil;
+    char c[5];
+    int i = 0;
+    
+    c[i] = device.getc();
+    i++;
+       
+    if(c[0] == 'T' || 'R')
+    {
+        while(i<5)
+        {
+            c[i] = device.getc();
+            i++;
+        }
+        
+        device.printf("%c %c %c %c %c \n\r",c[0], c[1], c[2], c[3], c[4]);
+    }
+    
+    else
+    {
+        device.printf("Invalid\n\r");
+        flushSerialBuffer();
+    }
+        
+    if(c[0] == 'T') 
+    {      
     
+        mil = atoi(&c[1]);
+        //mil = mil * 1000;
+        dwell = mil;
+        device.printf("%i \n\r",mil);
+        
+        cen = atoi(&c[2]);
+        //cen = cen * 100;
+        device.printf("%i \n\r",cen);
+        
+        dez = atoi(&c[3]);
+        //dez = dez * 10;
+        device.printf("%i \n\r",dez);
+        
+        uni  = atoi(&c[4]);
+        device.printf("%i \n\r",uni);
+                
+        //dwell = mil + cen + dez + uni;
+        device.printf("Dwell %i uS\n\r",dwell);
+        flushSerialBuffer();
+    } 
+    
+    if(c[0] == 'R') 
+    {     
+        mil = atoi(&c[1]);
+        //mil = mil * 1000;
+        time_rpm = mil;
+        //device.printf("%i \n\r",mil);
+        
+        cen = atoi(&c[2]);
+        //cen = cen * 100;
+        //device.printf("%i \n\r",cen);
+        
+        dez = atoi(&c[3]);
+        //dez = dez * 10;
+        //device.printf("%i \n\r",dez);
+         
+        uni  = atoi(&c[4]);
+        //device.printf("%i \n\r",uni);
+          
+        //time_rpm = mil + cen + dez + uni;
+               
+        time_rpm = 1/time_rpm;       
+        
+        device.printf("%f S\n\r",time_rpm);
+        
+        flipper.attach(&flip, time_rpm);
+        flushSerialBuffer();
+    } 
+}    
+
 int main() 
 {   
-    flipper.attach(&flip, 0.2);
-    TBI.period(0.01f);
+    bool valid_pulse;
+    int Splug_counter = 1;
     
-    IN1A=0;
-    IN2A=0;
+    time_rpm = 0.02;
+    flipper.attach(&flip, time_rpm);
     
-    while(1) 
+    device.printf("Start\n\r");
+    
+    while(1)    
     {   
         
     //t1.reset();
-    //t1.start(); 
-        
-        TPS1_VAL = TPS1.read();
-        SETPOINT_VAL = SETPOINT.read();
+    //t1.start();     
+            
+        if(device.readable())
+        {
+            set_parameters();   
+        }
         
-        SETPOINT_scaled = _scale.from(SETPOINT_VAL);
         
-        if(SETPOINT_scaled < 0.185f)
+        if(Splug_counter > 4)
         {
-            close();
-            erro =  TPS1_VAL - SETPOINT_scaled;        
-            proporcional = erro * KP;
+            Splug_counter = 1;
         }
         
-        else
+        if(t2.read_us() > dwell)
+        {   
+            t2.stop();
+            Splug_1 = 0;
+            Splug_2 = 0;
+            Splug_3 = 0;
+            Splug_4 = 0;
+        }
+        if((Splug_counter == 1) && (valid_pulse == 1))
         {
-            open();
-            erro = SETPOINT_scaled - TPS1_VAL;        
-            proporcional = erro * KP;
+            valid_pulse = 0; 
+            Splug_1 = 1;
+            Splug_2 = 0;
+            Splug_3 = 0;
+            Splug_4 = 0;
+            t2.reset();
+            t2.start();
         }
-                
-        if(erro>1 || erro<-1)
+         
+        if((Splug_counter == 2) && (valid_pulse == 1))
         {
-            //do nothing
+            valid_pulse = 0; 
+            Splug_1 = 0;
+            Splug_2 = 0;
+            Splug_3 = 1;
+            Splug_4 = 0;
+            t2.reset();
+            t2.start();
         }
         
-        else
+        if((Splug_counter == 3) && (valid_pulse == 1))
         {
-            integrador =  integrador + ((erro * KI)*0.01f);
+            valid_pulse = 0; 
+            Splug_1 = 0;
+            Splug_2 = 0;
+            Splug_3 = 0;
+            Splug_4 = 1;
+            t2.reset();
+            t2.start();
         }
         
-        if(integrador >=1)
-        integrador = 1;
-        
-        if(integrador <=-1)
-        integrador = -1;
-        
-                
-        pwm = proporcional + integrador;
-        TBI = pwm + offset;
+        if((Splug_counter == 4) && (valid_pulse == 1))
+        {
+            valid_pulse = 0; 
+            Splug_1 = 0;
+            Splug_2 = 1;
+            Splug_3 = 0;
+            Splug_4 = 0;
+            t2.reset();
+            t2.start();
+        }
         
         
         if(flag==1)
         {
-            device.printf("\n\rPedal: %.4f",SETPOINT_scaled);
-            device.printf("\n\rTPS: %.4f",TPS1_VAL);
-            device.printf("\n\rErro: %.4f",erro);
-            device.printf("\n\rPWM: %.4f",pwm);
-            //device.printf("integrador: %f\n",integrador);
-            //device.printf("proporcional: %f\n",proporcional);
+            Splug_counter++;
             flag=0;
+            valid_pulse = 1;
         }
         
         //t1.stop();
         //printf("O ciclo levou: %i uS \n\r", t1.read_us());
-        wait(0.01);
     }
 }
\ No newline at end of file
diff -r 111a7f1b978f -r 5cd0b64dea2a mbed.bld
--- a/mbed.bld	Fri Mar 01 10:07:05 2019 +0000
+++ b/mbed.bld	Tue Mar 12 19:01:55 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/5aab5a7997ee
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file