diff options
author | Stanley Huang <stanleyhuangyc@live.com> | 2016-07-06 14:57:50 +0800 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@live.com> | 2016-07-06 14:57:50 +0800 |
commit | c5a9fdb80df58015c0705d23e2208cb576affd7c (patch) | |
tree | 9bc8b7957dea05f3130da6821e65fed441802892 | |
parent | 23ea91258a34ab568c5dd2878f2eaa8a0dec6fe7 (diff) | |
download | 2021-arduino-obd-c5a9fdb80df58015c0705d23e2208cb576affd7c.tar.gz 2021-arduino-obd-c5a9fdb80df58015c0705d23e2208cb576affd7c.tar.bz2 2021-arduino-obd-c5a9fdb80df58015c0705d23e2208cb576affd7c.zip |
Renaming read() to readPID() in OBD library
-rw-r--r-- | libraries/OBD/OBD.cpp | 109 | ||||
-rw-r--r-- | libraries/OBD/OBD.h | 21 | ||||
-rw-r--r-- | libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino | 6 | ||||
-rw-r--r-- | libraries/OBD/examples/obd_uart_test/obd_uart_test.ino | 4 | ||||
-rw-r--r-- | libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino | 2 | ||||
-rw-r--r-- | libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino | 2 | ||||
-rw-r--r-- | libraries/OBD2UART/OBD2UART.cpp | 1 | ||||
-rw-r--r-- | megalogger/config.h | 4 | ||||
-rw-r--r-- | megalogger/megalogger.ino | 8 | ||||
-rw-r--r-- | nanologger/config.h | 2 | ||||
-rw-r--r-- | nanologger/nanologger.ino | 34 | ||||
-rw-r--r-- | nanotimer/nanotimer.ino | 4 | ||||
-rw-r--r-- | tester/tester.ino | 4 |
13 files changed, 126 insertions, 75 deletions
diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 162ca85..d551f93 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -73,7 +73,7 @@ void COBD::sendQuery(byte pid) write(cmd); } -bool COBD::read(byte pid, int& result) +bool COBD::readPID(byte pid, int& result) { // send a query command sendQuery(pid); @@ -81,7 +81,7 @@ bool COBD::read(byte pid, int& result) return getResult(pid, result); } -byte COBD::read(const byte pid[], byte count, int result[]) +byte COBD::readPID(const byte pid[], byte count, int result[]) { // send a multiple query command char buffer[128]; @@ -249,11 +249,26 @@ void COBD::sleep() sendCommand("ATLP\r", buf, sizeof(buf)); } +char* COBD::getResultValue(char* buf) +{ + char* p = buf; + for (;;) { + if (isdigit(*p) || *p == '-') { + return p; + } + p = strchr(p, '\r'); + if (!p) break; + if (*(++p) == '\n') p++; + } + return 0; +} + float COBD::getVoltage() { char buf[32]; if (sendCommand("ATRV\r", buf, sizeof(buf)) > 0) { - return atof(buf); + char* p = getResultValue(buf); + if (p) return atof(p); } return 0; } @@ -295,6 +310,16 @@ void COBD::begin() DEBUG.begin(115200); #endif recover(); + + char buffer[32]; + version = 0; + if (sendCommand("ATI\r", buffer, sizeof(buffer), 200)) { + char *p = strstr(buffer, "OBDUART"); + if (p) { + p += 9; + version = (*p - '0') * 10 + (*(p + 2) - '0'); + } + } } byte COBD::receive(char* buffer, byte bufsize, int timeout) @@ -343,15 +368,6 @@ bool COBD::init(OBD_PROTOCOLS protocol) m_state = OBD_CONNECTING; - write("ATI\r"); - if (receive(buffer, sizeof(buffer), 100)) { - char *p = strstr(buffer, "OBDUART"); - if (p) { - p += 9; - version = (*p - '0') * 10 + (*(p + 2) - '0'); - } - } - for (unsigned char i = 0; i < sizeof(initcmd) / sizeof(initcmd[0]); i++) { #ifdef DEBUG debugOutput(initcmd[i]); @@ -408,6 +424,50 @@ bool COBD::setBaudRate(unsigned long baudrate) } +float COBD::getTemperature() +{ + char buf[32]; + if (sendCommand("ATTEMP\r", buf, sizeof(buf)) > 0) { + char* p = getResultValue(buf); + if (p) return (float)(atoi(p) + 12412) / 340; + } + else { + return -1000; + } +} + +bool COBD::readAccel(int& x, int& y, int& z) +{ + char buf[32]; + if (sendCommand("ATACL\r", buf, sizeof(buf)) > 0) do { + char* p = getResultValue(buf); + if (!p) break; + x = atoi(p++); + if (!(p = strchr(p, ','))) break; + y = atoi(++p); + if (!(p = strchr(p, ','))) break; + z = atoi(++p); + return true; + } while (0); + return false; +} + +bool COBD::readGyro(int& x, int& y, int& z) +{ + char buf[32]; + if (sendCommand("ATGYRO\r", buf, sizeof(buf)) > 0) do { + char* p = getResultValue(buf); + if (!p) break; + x = atoi(p++); + if (!(p = strchr(p, ','))) break; + y = atoi(++p); + if (!(p = strchr(p, ','))) break; + z = atoi(++p); + return true; + } while (0); + return false; +} + #ifdef DEBUG void COBD::debugOutput(const char *s) { @@ -429,6 +489,16 @@ void COBDI2C::begin() DEBUG.begin(115200); #endif recover(); + + char buffer[32]; + version = 0; + if (sendCommand("ATI\r", buffer, sizeof(buffer), 200)) { + char *p = strstr(buffer, "OBDUART"); + if (p) { + p += 9; + version = (*p - '0') * 10 + (*(p + 2) - '0'); + } + } } void COBDI2C::end() @@ -436,7 +506,7 @@ void COBDI2C::end() m_state = OBD_DISCONNECTED; } -bool COBDI2C::read(byte pid, int& result) +bool COBDI2C::readPID(byte pid, int& result) { sendQuery(pid); dataIdleLoop(); @@ -469,7 +539,7 @@ byte COBDI2C::receive(char* buffer, byte bufsize, int timeout) do { Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)1); int c = Wire.read(); - if (offset == 0 && (c == 0 || c == -1)) { + if (offset == 0 && c < 0xa) { // data not ready dataIdleLoop(); continue; @@ -492,21 +562,22 @@ byte COBDI2C::receive(char* buffer, byte bufsize, int timeout) } } } while(millis() - start < timeout); + if (buffer) buffer[offset] = 0; return 0; } -byte COBDI2C::read(const byte pid[], byte count, int result[]) +byte COBDI2C::readPID(const byte pid[], byte count, int result[]) { byte results = 0; for (byte n = 0; n < count; n++) { - if (read(pid[n], result[n])) { + if (readPID(pid[n], result[n])) { results++; } } return results; } -void COBDI2C::setPID(byte pid, byte obdPid[]) +void COBDI2C::setQueryPID(byte pid, byte obdPid[]) { byte n = 0; for (; n < MAX_PIDS && obdPid[n]; n++) { @@ -520,13 +591,13 @@ void COBDI2C::setPID(byte pid, byte obdPid[]) obdPid[n] = pid; } -void COBDI2C::applyPIDs(byte obdPid[]) +void COBDI2C::applyQueryPIDs(byte obdPid[]) { sendCommandBlock(CMD_APPLY_OBD_PIDS, 0, (byte*)obdPid, sizeof(obdPid[0])* MAX_PIDS); delay(200); } -void COBDI2C::loadData(PID_INFO obdInfo[]) +void COBDI2C::loadQueryData(PID_INFO obdInfo[]) { sendCommandBlock(CMD_LOAD_OBD_DATA); dataIdleLoop(); diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 8e8185e..4109533 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -113,9 +113,9 @@ public: // get connection state
virtual OBD_STATES getState() { return m_state; }
// read specified OBD-II PID value
- virtual bool read(byte pid, int& result);
+ virtual bool readPID(byte pid, int& result);
// read multiple (up to 8) OBD-II PID values, return number of values obtained
- virtual byte read(const byte pid[], byte count, int result[]);
+ virtual byte readPID(const byte pid[], byte count, int result[]);
// set device into
virtual void sleep();
// set working protocol (default auto)
@@ -128,6 +128,12 @@ public: virtual float getVoltage();
// get VIN as a string, buffer length should be >= OBD_RECV_BUF_SIZE
virtual bool getVIN(char* buffer, byte bufsize);
+ // get device temperature
+ virtual float getTemperature();
+ // get accelerometer data
+ virtual bool readAccel(int& x, int& y, int& z);
+ // get gyroscope data
+ virtual bool readGyro(int& x, int& y, int& z);
// send query for specified PID
virtual void sendQuery(byte pid);
// retrive and parse the response of specifie PID
@@ -170,6 +176,7 @@ private: {
return (int)hex2uint8(data) - 40;
}
+ char* getResultValue(char* buf);
};
#define I2C_ADDR 0x62
@@ -199,15 +206,15 @@ class COBDI2C : public COBD { public:
void begin();
void end();
- bool read(byte pid, int& result);
- byte read(const byte pid[], byte count, int result[]);
+ bool readPID(byte pid, int& result);
+ byte readPID(const byte pid[], byte count, int result[]);
void write(const char* s);
// API not applicable
bool setBaudRate(unsigned long baudrate) { return false; }
// Asynchronized access API
- void setPID(byte pid, byte obdPid[]);
- void applyPIDs(byte obdPid[]);
- void loadData(PID_INFO obdInfo[]);
+ void setQueryPID(byte pid, byte obdPid[]);
+ void applyQueryPIDs(byte obdPid[]);
+ void loadQueryData(PID_INFO obdInfo[]);
protected:
byte receive(char* buffer, byte bufsize, int timeout = OBD_TIMEOUT_SHORT);
bool sendCommandBlock(byte cmd, uint8_t data = 0, byte* payload = 0, byte payloadBytes = 0);
diff --git a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino index fe9141a..c5b89fe 100644 --- a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino +++ b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino @@ -74,7 +74,7 @@ void readMEMS() Serial.println(gz); } -void readPID() +void readPIDs() { static const byte pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL}; Serial.print('['); @@ -87,7 +87,7 @@ void readPID() Serial.print('='); if (valid) { int value; - if (obd.read(pid, value)) { + if (obd.readPID(pid, value)) { Serial.print(value); } } @@ -117,7 +117,7 @@ void setup() { } void loop() { - readPID(); + readPIDs(); readMEMS(); } diff --git a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino index 484262e..08106ec 100644 --- a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino +++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino @@ -55,7 +55,7 @@ void readPIDSingle() mySerial.print(millis()); mySerial.print(']'); mySerial.print("RPM="); - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { mySerial.print(value); } mySerial.println(); @@ -65,7 +65,7 @@ void readPIDMultiple() { static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP, PID_INTAKE_TEMP}; int values[sizeof(pids)]; - if (obd.read(pids, sizeof(pids), values) == sizeof(pids)) { + if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) { for (byte i = 0; i < sizeof(pids) ; i++) { mySerial.print('['); mySerial.print(millis()); diff --git a/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino b/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino index d999159..619ae32 100644 --- a/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino +++ b/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino @@ -24,7 +24,7 @@ void setup() void loop() { int value; - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { // RPM is successfully read and its value stored in variable 'value' // light on LED when RPM exceeds 3000 digitalWrite(13, value > 3000 ? HIGH : LOW); diff --git a/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino b/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino index 691ee9c..a248307 100644 --- a/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino +++ b/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino @@ -24,7 +24,7 @@ void setup() void loop() { int value; - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { // RPM is successfully read and its value stored in variable 'value' // light on LED when RPM exceeds 3000 digitalWrite(13, value > 3000 ? HIGH : LOW); diff --git a/libraries/OBD2UART/OBD2UART.cpp b/libraries/OBD2UART/OBD2UART.cpp index c2a28fe..f756705 100644 --- a/libraries/OBD2UART/OBD2UART.cpp +++ b/libraries/OBD2UART/OBD2UART.cpp @@ -465,7 +465,6 @@ bool COBD::readGyro(int& x, int& y, int& z) return false; } - #ifdef DEBUG void COBD::debugOutput(const char *s) { diff --git a/megalogger/config.h b/megalogger/config.h index 99cf04d..e89f0e3 100644 --- a/megalogger/config.h +++ b/megalogger/config.h @@ -59,8 +59,8 @@ /************************************** * LCD module (uncomment only one) **************************************/ -LCD_R61581 lcd; /* 3.5" CTE35IPS/R61581 based LCD */ -//LCD_SSD1289 lcd; /* 3.2" SSD12389 based LCD */ +//LCD_R61581 lcd; /* 3.5" CTE35IPS/R61581 based LCD */ +LCD_SSD1289 lcd; /* 3.2" SSD12389 based LCD */ //LCD_ILI9325D lcd; /* 2.8" ILI9325 based LCD */ //LCD_ILI9341 lcd; /* 2.4" ILI9341 based SPI LCD */ //LCD_Null lcd; diff --git a/megalogger/megalogger.ino b/megalogger/megalogger.ino index 83803cc..53b6475 100644 --- a/megalogger/megalogger.ino +++ b/megalogger/megalogger.ino @@ -595,7 +595,7 @@ void showECUCap() if (obd.isValidPID(pid)) { int value; lcd.setCursor(280 , n++); - if (obd.read(pid, value)) { + if (obd.readPID(pid, value)) { if (!scanned || value == values[i]) lcd.setColor(RGB16_CYAN); else if (value > values[i]) @@ -633,7 +633,7 @@ void reconnect() continue; int value; - if (obd.read(PID_RPM, value)) + if (obd.readPID(PID_RPM, value)) break; obd.sleep(); @@ -803,7 +803,7 @@ void loop() int values[sizeof(pids)] = {0}; uint32_t pidTime = millis(); // read multiple OBD-II PIDs - byte results = obd.read(pids, sizeof(pids), values); + byte results = obd.readPID(pids, sizeof(pids), values); pidTime = millis() - pidTime; if (results == sizeof(pids)) { for (byte n = 0; n < sizeof(pids); n++) { @@ -814,7 +814,7 @@ void loop() // check validation and read a single OBD-II PID if (obd.isValidPID(pid)) { int value; - if (obd.read(pid, value)) { + if (obd.readPID(pid, value)) { logOBDData(pid, value); } } diff --git a/nanologger/config.h b/nanologger/config.h index f283d1c..6ac97a1 100644 --- a/nanologger/config.h +++ b/nanologger/config.h @@ -5,7 +5,7 @@ * Data logging/streaming out **************************************/ #define ENABLE_DATA_OUT 0 -#define ENABLE_DATA_LOG 1 +#define ENABLE_DATA_LOG 0 #define USE_SOFTSERIAL 0 //this defines the format of log file #define LOG_FORMAT FORMAT_CSV diff --git a/nanologger/nanologger.ino b/nanologger/nanologger.ino index 0a4ef0f..d299e09 100644 --- a/nanologger/nanologger.ino +++ b/nanologger/nanologger.ino @@ -7,11 +7,10 @@ #include <Arduino.h> #include <Wire.h> -#include <OBD.h> #include <SPI.h> #include <SD.h> -#include <MPU6050.h> -#include "MicroLCD.h" +#include <OBD.h> +#include <MicroLCD.h> #include "images.h" #include "config.h" #if USE_SOFTSERIAL @@ -38,11 +37,9 @@ static int lastValue = 0; static byte pidTier1[]= {PID_RPM, PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE}; static byte pidTier2[] = {PID_INTAKE_MAP, PID_MAF_FLOW, PID_TIMING_ADVANCE}; -static byte pidTier3[] = {PID_COOLANT_TEMP, PID_INTAKE_TEMP, PID_AMBIENT_TEMP, PID_FUEL_LEVEL}; #define TIER_NUM1 sizeof(pidTier1) #define TIER_NUM2 sizeof(pidTier2) -#define TIER_NUM3 sizeof(pidTier3) byte pidValue[TIER_NUM1]; @@ -54,14 +51,6 @@ public: { showStates(); -#if USE_MPU6050 - Wire.begin(); - if (MPU6050_init() == 0) { - state |= STATE_ACC_READY; - showStates(); - } -#endif - do { showStates(); } while (!init()); @@ -110,8 +99,6 @@ public: index = 0; if (index2 == TIER_NUM2) { index2 = 0; - logOBDData(pidTier3[index3]); - index3 = (index3 + 1) % TIER_NUM3; } else { logOBDData(pidTier2[index2++]); } @@ -214,24 +201,11 @@ private: lastPid = 0; } } -#if USE_MPU6050 - void processAccelerometer() - { - accel_t_gyro_union data; - MPU6050_readout(&data); - dataTime = millis(); - // log x/y/z of accelerometer - logData(PID_ACC, data.value.x_accel, data.value.y_accel, data.value.z_accel); - //showGForce(data.value.y_accel); - // log x/y/z of gyro meter - logData(PID_GYRO, data.value.x_gyro, data.value.y_gyro, data.value.z_gyro); - } -#endif int logOBDData(byte pid) { int value = 0; // send a query to OBD adapter for specified OBD-II pid - if (read(pid, value)) { + if (readPID(pid, value)) { dataTime = millis(); // log data to SD card logData(0x100 | pid, value); @@ -259,7 +233,7 @@ private: } if (init()) { int value; - if (read(PID_RPM, value) && value > 0) + if (readPID(PID_RPM, value) && value > 0) break; } } diff --git a/nanotimer/nanotimer.ino b/nanotimer/nanotimer.ino index b9e20b9..b4a1013 100644 --- a/nanotimer/nanotimer.ino +++ b/nanotimer/nanotimer.ino @@ -8,9 +8,9 @@ #include <Arduino.h> #include <Wire.h> #include <SPI.h> -#include <OBD.h> #include <SD.h> #include <MicroLCD.h> +#include <OBD.h> #include "config.h" #if USE_SOFTSERIAL #include <SoftwareSerial.h> @@ -188,7 +188,7 @@ private: uint32_t dataTime; #endif int speed; - if (!read(PID_SPEED, speed)) + if (!readPID(PID_SPEED, speed)) return; dataTime = millis(); diff --git a/tester/tester.ino b/tester/tester.ino index 8e141ad..2ec1bf9 100644 --- a/tester/tester.ino +++ b/tester/tester.ino @@ -200,7 +200,7 @@ bool checkSD() const byte pids[]= {PID_RPM, PID_SPEED, PID_THROTTLE, PID_ENGINE_LOAD}; int values[sizeof(pids)]; // read multiple OBD-II PIDs - if (read(pids, sizeof(pids), values) == sizeof(pids)) { + if (readPID(pids, sizeof(pids), values) == sizeof(pids)) { dataTime = millis(); for (byte n = 0; n < sizeof(pids); n++) { logData((uint16_t)pids[n] | 0x100, values[n]); @@ -215,7 +215,7 @@ bool checkSD() int value; byte pid = pids2[index2 = (index2 + 1) % (sizeof(pids2))]; // read single OBD-II PID - if (isValidPID(pid) && read(pid, value)) { + if (isValidPID(pid) && readPID(pid, value)) { dataTime = millis(); logData((uint16_t)pid | 0x100, value); lastSec = sec; |