diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2013-04-02 14:08:48 +0800 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2013-04-02 14:08:48 +0800 |
commit | 7af9330e5a976b4715eb406d613dff915dc292d8 (patch) | |
tree | d9eecf81fc0ef1565e07c8d34655ceafdc5d351d | |
parent | 9aa713d5e441a6f428d6fdddbd61065a2ee4c286 (diff) | |
download | 2021-arduino-obd-7af9330e5a976b4715eb406d613dff915dc292d8.tar.gz 2021-arduino-obd-7af9330e5a976b4715eb406d613dff915dc292d8.tar.bz2 2021-arduino-obd-7af9330e5a976b4715eb406d613dff915dc292d8.zip |
improving OBD-II data logger
-rw-r--r-- | obdlogger/MultiLCD.cpp | 1 | ||||
-rw-r--r-- | obdlogger/OBD.cpp | 31 | ||||
-rw-r--r-- | obdlogger/OBD.h | 1 | ||||
-rw-r--r-- | obdlogger/obdlogger.ino | 81 |
4 files changed, 71 insertions, 43 deletions
diff --git a/obdlogger/MultiLCD.cpp b/obdlogger/MultiLCD.cpp index f1e8664..1442ebd 100644 --- a/obdlogger/MultiLCD.cpp +++ b/obdlogger/MultiLCD.cpp @@ -139,6 +139,7 @@ void LCD_OLED::clear() { ScI2cMxFillArea(OLED_ADDRESS, 0, 7, 0, 127, 0); delay(10); + setCursor(0, 0); } void LCD_OLED::begin() diff --git a/obdlogger/OBD.cpp b/obdlogger/OBD.cpp index b3da34e..938523b 100644 --- a/obdlogger/OBD.cpp +++ b/obdlogger/OBD.cpp @@ -240,12 +240,12 @@ bool COBD::Init(bool passive) strcpy_P(cmd, s_initcmd[i]); WriteData(cmd); } - n = 0; - prompted = 0; + n = 0; + prompted = 0; currentMillis = millis(); - for (;;) { - if (DataAvailable()) { - char c = ReadData(); + for (;;) { + if (DataAvailable()) { + char c = ReadData(); if (c == '>') { buffer[n] = 0; prompted++; @@ -254,16 +254,17 @@ bool COBD::Init(bool passive) } } else if (prompted) { break; - } else { - unsigned long elapsed = millis() - currentMillis; - if (elapsed > OBD_TIMEOUT_INIT) { - // init timeout - //WriteData("\r"); - return false; - } - } - } - } + } else { + unsigned long elapsed = millis() - currentMillis; + if (elapsed > OBD_TIMEOUT_INIT) { + // init timeout + //WriteData("\r"); + return false; + } + DataTimeout(); + } + } + } errors = 0; return true; } diff --git a/obdlogger/OBD.h b/obdlogger/OBD.h index 1247c49..e608fee 100644 --- a/obdlogger/OBD.h +++ b/obdlogger/OBD.h @@ -81,4 +81,5 @@ protected: virtual char ReadData(); virtual void WriteData(const char* s); virtual void WriteData(const char c); + virtual void DataTimeout() {} }; diff --git a/obdlogger/obdlogger.ino b/obdlogger/obdlogger.ino index 0e0b541..9d55340 100644 --- a/obdlogger/obdlogger.ino +++ b/obdlogger/obdlogger.ino @@ -14,8 +14,10 @@ #include "MPU6050.h" //#define SD_CS_PIN 4 // ethernet shield with SD -//#define SD_CS_PIN 7 // microduino -#define SD_CS_PIN 10 // SD breakout +#define SD_CS_PIN 7 // microduino +//#define SD_CS_PIN 10 // SD breakout + +#define GPS_BAUDRATE 4800 /* bps */ // addition PIDs (non-OBD) #define PID_GPS_DATETIME 0xF01 @@ -26,13 +28,10 @@ #define DATASET_INTERVAL 1000 /* ms */ // GPS logging can only be enabled when there is additional serial UART -#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega644p) +#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega644P__) #define ENABLE_GPS #endif -// OBD-II -COBD obd; - // GPS #ifdef ENABLE_GPS TinyGPS gps; @@ -53,6 +52,24 @@ static uint32_t datacount = 0; void ProcessGPSData(char c); +class COBD2 : public COBD +{ +public: + void DataTimeout() + { + while (Serial1.available()) { + if (gps.encode(Serial1.read())) { + if (datacount == 0) { + lcd.setCursor(9, 3); + lcd.print("GPS:Yes"); + } + } + } + } +}; + +COBD2 obd; + bool ShowCardInfo() { if (card.init(SPI_HALF_SPEED, SD_CS_PIN)) { @@ -143,38 +160,46 @@ void InitScreen() { lcd.clear(); lcd.backlight(true); - lcd.setCursor(92, 0); + lcd.setCursor(9, 0); lcd.print("kph"); - lcd.setCursor(92, 1); + lcd.setCursor(9, 1); lcd.print("rpm"); } void setup() { - // start serial communication at the adapter defined baudrate - OBDUART.begin(OBD_SERIAL_BAUDRATE); -#ifdef ENABLE_GPS - Serial2.begin(4800); -#endif - lcd.begin(); lcd.clear(); lcd.backlight(true); lcd.print("Initializing"); + // start serial communication at the adapter defined baudrate + OBDUART.begin(OBD_SERIAL_BAUDRATE); + // init SD card pinMode(SS, OUTPUT); CheckSD(); // initiate OBD-II connection until success lcd.setCursor(0, 3); - lcd.print("Waiting OBD Data"); + lcd.print("OBD:No"); + +#ifdef ENABLE_GPS + Serial1.begin(GPS_BAUDRATE); + delay(100); + if (Serial1.available()) { + lcd.setCursor(9, 3); + lcd.print("GPS:No"); + } +#endif while (!obd.Init()); + obd.ReadSensor(PID_DISTANCE, startDistance); lcd.setCursor(0, 3); - lcd.print("OBD Connected! "); + lcd.setCursor(4, 3); + lcd.print("Yes"); delay(1000); InitScreen(); @@ -208,11 +233,11 @@ void ProcessGPSData(char c) len = sprintf(databuf, "%d,F02,%ld %ld\n", (int)(curTime - lastTime), lat, lon); sdfile.write((uint8_t*)databuf, len); // display LAT/LON - sprintf(databuf, "%ld", lat); + sprintf(databuf, "%3d.%ld", (int)(lat / 100000), lat % 100000); lcd.setCursor(0, 2); lcd.print(databuf); - sprintf(databuf, "%ld", lon); - lcd.setCursor(8 * 8, 2); + sprintf(databuf, "%3d.%ld", (int)(lon / 100000), lon % 100000); + lcd.setCursor(0, 3); lcd.print(databuf); } len = sprintf(databuf, "%d,F03,%ld %ld\n", (int)(curTime - lastTime), gps.speed() * 1852 / 100); @@ -236,26 +261,26 @@ void RetrieveData(byte pid) char* buf = databuf; // data in buffer saved, free for other use if (datacount % 100 == 99) { sdfile.flush(); - sprintf(buf, "%4u KB", (int)(filesize >> 10)); - lcd.setCursor(72, 3); + sprintf(buf, "%4uKB", (int)(filesize >> 10)); + lcd.setCursor(10, 3); lcd.print(buf); } switch (pid) { case PID_RPM: - sprintf(buf, "%4d", value); + sprintf(buf, "%4u", (unsigned int)value % 10000); lcd.setCursor(0, 0); lcd.printLarge(buf); break; case PID_SPEED: - sprintf(buf, "%3d", value); - lcd.setCursor(16, 1); + sprintf(buf, "%3u", (unsigned int)value % 1000); + lcd.setCursor(2, 1); lcd.printLarge(buf); break; case PID_DISTANCE: if (value >= startDistance) { - sprintf(buf, "%d km ", value - startDistance); - lcd.setCursor(0, 3); + sprintf(buf, "%4dkm", value - startDistance); + lcd.setCursor(10, 2); lcd.print(buf); } break; @@ -263,8 +288,8 @@ void RetrieveData(byte pid) } #ifdef ENABLE_GPS - while (Serial2.available()) { - ProcessGPSData(Serial2.read()); + while (Serial1.available()) { + ProcessGPSData(Serial1.read()); } #endif |