From bd86ac57fe9fa492e7e40e09d81de31bed1f449c Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Tue, 30 Jan 2018 22:37:56 +1100 Subject: Add support for Freematics OBD-II UART Adapter V2.1 --- libraries/OBD2UART/OBD2UART.h | 74 +++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 45 deletions(-) (limited to 'libraries/OBD2UART/OBD2UART.h') diff --git a/libraries/OBD2UART/OBD2UART.h b/libraries/OBD2UART/OBD2UART.h index 62ac18b..2674274 100644 --- a/libraries/OBD2UART/OBD2UART.h +++ b/libraries/OBD2UART/OBD2UART.h @@ -8,8 +8,7 @@ #include #define OBD_TIMEOUT_SHORT 1000 /* ms */ -#define OBD_TIMEOUT_LONG 5000 /* ms */ -#define OBD_TIMEOUT_GPS 200 /* ms */ +#define OBD_TIMEOUT_LONG 10000 /* ms */ #ifndef OBDUART #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168P__) @@ -19,6 +18,10 @@ #endif #endif +#ifdef ESP32 +extern HardwareSerial Serial1; +#endif + // Mode 1 PIDs #define PID_ENGINE_LOAD 0x04 #define PID_COOLANT_TEMP 0x05 @@ -73,23 +76,12 @@ #define PID_ENGINE_REF_TORQUE 0x63 // non-OBD/custom PIDs (no mode number) -#define PID_GPS_LATITUDE 0xA -#define PID_GPS_LONGITUDE 0xB -#define PID_GPS_ALTITUDE 0xC -#define PID_GPS_SPEED 0xD -#define PID_GPS_HEADING 0xE -#define PID_GPS_SAT_COUNT 0xF -#define PID_GPS_TIME 0x10 -#define PID_GPS_DATE 0x11 #define PID_ACC 0x20 #define PID_GYRO 0x21 #define PID_COMPASS 0x22 #define PID_MEMS_TEMP 0x23 #define PID_BATTERY_VOLTAGE 0x24 -// custom PIDs for calculated data -#define PID_TRIP_DISTANCE 0x30 - typedef enum { PROTO_AUTO = 0, PROTO_ISO_9141_2 = 3, @@ -115,13 +107,16 @@ uint8_t hex2uint8(const char *p); class COBD { public: - COBD():dataMode(1),errors(0),m_state(OBD_DISCONNECTED) {} // begin serial UART virtual byte begin(); + // terminate communication channel + virtual void end(); // initialize OBD-II connection virtual bool init(OBD_PROTOCOLS protocol = PROTO_AUTO); + // reset OBD-II connection + virtual void reset(); // un-initialize OBD-II connection - virtual void end(); + virtual void uninit(); // set serial baud rate virtual bool setBaudRate(unsigned long baudrate); // get connection state @@ -144,12 +139,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); - // initialize MEMS sensor - virtual bool memsInit(); + // initialize MEMS sensor (enable or disable sensor fusion by quanterion algorithm) + virtual bool memsInit(bool fusion = false); // read out MEMS data (acc for accelerometer, gyr for gyroscope, temp in 0.1 celcius degree) virtual bool memsRead(int16_t* acc, int16_t* gyr = 0, int16_t* mag = 0, int16_t* temp = 0); - // send query for specified PID - virtual void sendQuery(byte pid); + // get computed orientation values + virtual bool memsOrientation(float& yaw, float& pitch, float& roll); // retrive and parse the response of specifie PID virtual bool getResult(byte& pid, int& result); // determine if the PID is supported @@ -157,37 +152,26 @@ public: // get adapter firmware version virtual byte getVersion(); // set current PID mode - byte dataMode; + byte dataMode = 1; // occurrence of errors - byte errors; + byte errors = 0; // bit map of supported PIDs - byte pidmap[4 * 4]; + byte pidmap[4 * 4] = {0}; protected: virtual char* getResponse(byte& pid, char* buffer, byte bufsize); - virtual byte receive(char* buffer, byte bufsize, int timeout = OBD_TIMEOUT_SHORT); + virtual int receive(char* buffer, int bufsize, unsigned int timeout = OBD_TIMEOUT_SHORT); virtual void write(const char* s); - virtual void dataIdleLoop() {} - void recover(); - void debugOutput(const char* s); - int normalizeData(byte pid, char* data); - OBD_STATES m_state; + virtual uint8_t getPercentageValue(char* data); + virtual uint16_t getLargeValue(char* data); + virtual uint8_t getSmallValue(char* data); + virtual int16_t getTemperatureValue(char* data); + virtual int normalizeData(byte pid, char* data); + virtual byte checkErrorMessage(const char* buffer); + virtual char* getResultValue(char* buf); + OBD_STATES m_state = OBD_DISCONNECTED; private: - virtual uint8_t getPercentageValue(char* data) - { - return (uint16_t)hex2uint8(data) * 100 / 255; - } - virtual uint16_t getLargeValue(char* data) - { - return hex2uint16(data); - } - virtual uint8_t getSmallValue(char* data) - { - return hex2uint8(data); - } - virtual int16_t getTemperatureValue(char* data) - { - return (int)hex2uint8(data) - 40; - } - char* getResultValue(char* buf); + void recover(); + virtual void idleTasks() {} + bool m_fusion = false; }; -- cgit v1.2.3