12/mar ignicao funcionando
Dependencies: mbed reScale TextLCD
Diff: main.cpp
- Revision:
- 5:e04419b1e369
- Parent:
- 4:9da4a46f38bc
- Child:
- 6:111a7f1b978f
diff -r 9da4a46f38bc -r e04419b1e369 main.cpp --- a/main.cpp Thu Apr 26 18:54:15 2018 +0000 +++ b/main.cpp Wed May 02 11:18:49 2018 +0000 @@ -1,38 +1,53 @@ #include "mbed.h" #include "TextLCD.h" +#include "reScale.h" +#include "eeprom.h" + +#define tole_min 10 //tolerancia minima em km/h +#define tole_max 100 //tolerancia maxima em km/h +#define ndentes 60 //numero de dentes do sensor + + I2C i2c_lcd(PTC9,PTC8); // SDA, SCL +EEPROM e2prom(PTC11,PTC10,0x0,EEPROM::T24C04); + InterruptIn motora(D0); InterruptIn movida(D1); -DigitalOut ign(LED1); -PwmOut boost(LED2); +DigitalOut ign(LED2); +PwmOut boost(PTC1); -Timer tbase; +Timer t1; +Timer t2; Ticker call; Serial pc(USBTX, USBRX); // tx, rx -unsigned long int t_motora, t_movida, delta; +reScale _scale(tole_min,tole_max,0,1); -TextLCD_I2C lcd(&i2c_lcd, 0x7E, TextLCD::LCD16x2); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type +float t_motora=0, t_movida=0; +float perimetro_motora=0, perimetro_movida=0; + + +TextLCD_I2C lcd(&i2c_lcd, 0x7E, TextLCD::LCD20x4); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type void tracao() { - tbase.stop(); - t_motora = tbase.read_us(); - tbase.reset(); - tbase.start(); + t1.stop(); + t_motora = t1.read_us(); + t1.reset(); + t1.start(); } void livre() { - tbase.stop(); - t_movida = tbase.read_us(); - tbase.reset(); - tbase.start(); + t2.stop(); + t_movida = t2.read_us(); + t2.reset(); + t2.start(); } void send() @@ -40,12 +55,72 @@ pc.printf("Tempo: %.3f Hz \n\r"); } +void le_memoria() +{ + e2prom.read(12, perimetro); + e2prom.ready(); +} + +void calc_perimetro_motora() +{ + float raio_dinamico=0; + float pneu_largura=205; + float pneu_perfil=50; + float pneu_aro=15; + /* + e2prom.read(0,pneu_largura); + e2prom.ready(); + e2prom.read(4,pneu_perfil); + e2prom.ready(); + e2prom.read(8,pneu_aro); + e2prom.ready(); + */ + raio_dinamico = pneu_largura * (pneu_perfil /100); + raio_dinamico = raio_dinamico + (pneu_aro * 25.4); + perimetro_motora = raio_dinamico * 3.14; //resultado em milimetros + perimetro_motora = perimetro_motora / 1000; //converter em metros + + //e2prom.write(12, perimetro_motora); + //e2prom.ready(); +} + +void calc_perimetro_movida() +{ + float raio_dinamico=0; + float pneu_largura=205; + float pneu_perfil=50; + float pneu_aro=15; + /* + e2prom.read(0,pneu_largura); + e2prom.ready(); + e2prom.read(4,pneu_perfil); + e2prom.ready(); + e2prom.read(8,pneu_aro); + e2prom.ready(); + */ + raio_dinamico = pneu_largura * (pneu_perfil /100); + raio_dinamico = raio_dinamico + (pneu_aro * 25.4); + perimetro_movida = raio_dinamico * 3.14; //resultado em milimetros + perimetro_movida = perimetro_movida / 1000; //converter em metros + + //e2prom.write(12, perimetro_movida); + //e2prom.ready(); +} + int main() { + float delta=0; + float vel_movida=0; + float vel_motora=0; + float pwmturbo=0; + motora.fall(&tracao); // attach the address of the measure function to the rising edge movida.fall(&livre); // attach the address of the measure function to the rising edge - + + + boost.period(0.01f); + lcd.setBacklight(TextLCD::LightOn); lcd.setCursor(TextLCD::CurOff_BlkOff); lcd.setAddress(0,0); @@ -54,15 +129,42 @@ lcd.printf("TRACAO"); wait(1.5); lcd.cls(); + + /* + if( USUARIO DESEJA CALCULAR O PERIMETRO ) usar while com timeout!!!!!!!!!!!!!!!!!! + { + calc_perimetro_movida(); + calc_perimetro_motora(); + e2prom.read(12, perimetro); + e2prom.ready(); + } + + le_memoria(); + */ + + calc_perimetro_motora(); + calc_perimetro_movida(); while(1) - { + { + vel_motora = perimetro / ((t_motora*ndentes) / 1000000); + vel_motora = vel_motora * 3.6; + + vel_movida = perimetro / ((t_movida*ndentes) / 1000000); + vel_movida = vel_movida * 3.6; - delta = t_motora - t_movida; - boost.pulsewidth_ms(delta); + delta = vel_motora - vel_movida; + + if(delta<=0) + { + delta=0; + } - if(t_movida < t_motora) + pwmturbo =_scale.from(delta); + boost.write(pwmturbo); + + if(delta > tole_min) { ign=0; wait(0.1); @@ -76,5 +178,14 @@ lcd.setAddress(0,0); lcd.printf("Normal "); } + + + lcd.setAddress(0,1); + lcd.printf("Motora: %.1f Km/h ",vel_motora); + lcd.setAddress(0,2); + lcd.printf("Movida: %.1f Km/h ",vel_movida); + lcd.setAddress(0,3); + lcd.printf("D:%.1f Km/h B:%.1f ",delta, pwmturbo); + } } \ No newline at end of file