Marcelo Costanzo Miranda
/
Fuel_injector_high_v3_teste_11_abr
Programa sem limites de tempo de injecao e rotacao
Diff: main.cpp
- Revision:
- 7:8575dc24281f
- Parent:
- 6:111a7f1b978f
--- a/main.cpp Fri Mar 01 10:07:05 2019 +0000 +++ b/main.cpp Thu Apr 11 13:21:15 2019 +0000 @@ -1,31 +1,21 @@ #include "mbed.h" -#include "reScale.h" -#define KP 0.8f -#define KI 1.0f -#define KD 0.01f -#define offset 0.02f - +Ticker flipper; -AnalogIn SETPOINT(A0); -AnalogIn TPS1(A2); -AnalogIn TPS2(A3); -Ticker flipper; -DigitalOut controlLED(LED2); -DigitalOut IN1A(D10); -DigitalOut IN2A(D8); -PwmOut TBI(D9); +DigitalOut controlLED(D13); +DigitalOut inj_1(D2); +DigitalOut inj_2(D3); +DigitalOut inj_3(D4); +DigitalOut inj_4(D5); //Timer t1; +Timer t2; -//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; +bool flag; +float RPS = 0; +int inj_time = 10000; void flip() { @@ -33,85 +23,145 @@ controlLED=!controlLED; } -void open() +void set_parameters() { - IN1A=1; - IN2A=0; + char c[8]; + char parameter = 0x00; + int i = 0; + int value; + int RPM; + + parameter = device.getc(); + + if((parameter == 'T') || (parameter == 'R')) //T or R + { + while(c[i-1] != '\n') + { + c[i] = device.getc(); + i++; + } + } + + else + { + device.printf("Invalid command. Txxxxx to set injection time, Rxxxxx to set RPM\n\r"); + device.fsync(); + } + + if(parameter == 'T') + { + value = atoi(c); + inj_time = value; + + device.printf("Inj Time %i uS\n\r",inj_time); + } + + if(parameter == 'R') + { + value = atoi(c); + + RPM = value; + + RPS = RPM / 60.0f; + + RPS = RPS * 2; //multiplicador por 2 pois um ciclo otto são 2 voltas + + RPS = 1.0f / RPS; + + flipper.attach(&flip, RPS); + + device.printf("%f S\n\r",RPS); + } } -void close() -{ - IN1A=0; - IN2A=1; -} - int main() { - flipper.attach(&flip, 0.2); - TBI.period(0.01f); + bool valid_pulse; + int inj_counter = 1; - IN1A=0; - IN2A=0; + RPS = 1.0f; + flipper.attach(&flip, RPS); - 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(inj_counter > 4) { - close(); - erro = TPS1_VAL - SETPOINT_scaled; - proporcional = erro * KP; + inj_counter = 1; } - else + if(t2.read_us() > inj_time) + { + t2.stop(); + inj_1 = 0; + inj_2 = 0; + inj_3 = 0; + inj_4 = 0; + } + if((inj_counter == 1) && (valid_pulse == 1)) { - open(); - erro = SETPOINT_scaled - TPS1_VAL; - proporcional = erro * KP; + valid_pulse = 0; + inj_1 = 1; + inj_2 = 0; + inj_3 = 0; + inj_4 = 0; + t2.reset(); + t2.start(); } - - if(erro>1 || erro<-1) + + if((inj_counter == 2) && (valid_pulse == 1)) { - //do nothing + valid_pulse = 0; + inj_1 = 0; + inj_2 = 0; + inj_3 = 1; + inj_4 = 0; + t2.reset(); + t2.start(); } - else + if((inj_counter == 3) && (valid_pulse == 1)) { - integrador = integrador + ((erro * KI)*0.01f); + valid_pulse = 0; + inj_1 = 0; + inj_2 = 0; + inj_3 = 0; + inj_4 = 1; + t2.reset(); + t2.start(); } - if(integrador >=1) - integrador = 1; - - if(integrador <=-1) - integrador = -1; - - - pwm = proporcional + integrador; - TBI = pwm + offset; + if((inj_counter == 4) && (valid_pulse == 1)) + { + valid_pulse = 0; + inj_1 = 0; + inj_2 = 1; + inj_3 = 0; + inj_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); + inj_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