summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2013-10-27 23:33:11 +1100
committerStanley Huang <stanleyhuangyc@gmail.com>2013-10-27 23:33:11 +1100
commitcabaa89f83d4e6124728f005c50fb0da8cbd0935 (patch)
tree305bb4752548bbce4b44be1b7b84bb26cd7703e3
parent948fac18830154769e8c569bdb04f0f328eb9d64 (diff)
download2021-arduino-obd-cabaa89f83d4e6124728f005c50fb0da8cbd0935.tar.gz
2021-arduino-obd-cabaa89f83d4e6124728f005c50fb0da8cbd0935.tar.bz2
2021-arduino-obd-cabaa89f83d4e6124728f005c50fb0da8cbd0935.zip
add config.h for placing all configurations
-rw-r--r--megalogger/config.h36
-rw-r--r--megalogger/datalogger.h44
-rw-r--r--megalogger/megalogger.cbp1
-rw-r--r--megalogger/megalogger.ino62
4 files changed, 98 insertions, 45 deletions
diff --git a/megalogger/config.h b/megalogger/config.h
new file mode 100644
index 0000000..d247475
--- /dev/null
+++ b/megalogger/config.h
@@ -0,0 +1,36 @@
+#ifndef CONFIG_H_INCLUDED
+#define CONFIG_H_INCLUDED
+
+// configurations
+/**************************************
+* Choose SD pin here
+**************************************/
+//#define SD_CS_PIN 4 // ethernet shield
+//#define SD_CS_PIN 7 // microduino
+//#define SD_CS_PIN 10 // SD breakout
+#define SD_CS_PIN SS
+
+/**************************************
+* Config GPS here
+**************************************/
+#define USE_GPS
+#define MAX_GPS_PROCESS_TIME 50 /* ms */
+#define GPS_BAUDRATE 38400 /* bps */
+//#define GPS_OPEN_BAUDRATE 4800 /* bps */
+
+/**************************************
+* Timeout/interval options
+**************************************/
+//#define OBD_MIN_INTERVAL 200 /* ms */
+#define ACC_DATA_INTERVAL 200 /* ms */
+#define GPS_DATA_TIMEOUT 2000 /* ms */
+
+/**************************************
+* Data logging/streaming options
+**************************************/
+#define ENABLE_DATA_OUT 1
+#define ENABLE_DATA_LOG 1
+#define USE_OBD_BT 1
+#define LOG_FORMAT FORMAT_CSV /* options: FORMAT_CSV, FORMAT_BIN */
+
+#endif
diff --git a/megalogger/datalogger.h b/megalogger/datalogger.h
index caffd51..281d50c 100644
--- a/megalogger/datalogger.h
+++ b/megalogger/datalogger.h
@@ -1,13 +1,3 @@
-// configurations
-#define ENABLE_DATA_OUT 0
-#define ENABLE_DATA_LOG 1
-
-#define FORMAT_BIN 0
-#define FORMAT_CSV 1
-
-//this defines the format of log file
-#define LOG_FORMAT FORMAT_CSV
-
typedef enum {
LOG_TYPE_DEFAULT = 0,
LOG_TYPE_0_60,
@@ -24,6 +14,9 @@ typedef enum {
#define FLAG_GPS 0x20
#define FLAG_ACC 0x40
+#define FORMAT_BIN 0
+#define FORMAT_CSV 1
+
typedef struct {
uint32_t time;
uint16_t pid;
@@ -100,7 +93,7 @@ typedef struct {
#define FILE_NAME_FORMAT "/DAT%05d.CSV"
#endif
-#if ENABLE_DATA_OUT
+#if ENABLE_DATA_OUT && !USE_OBD_BT
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
SoftwareSerial mySerial(A8, A9); /* for BLE Shield on MEGA*/
#elif defined(__AVR_ATmega644P__)
@@ -110,13 +103,20 @@ typedef struct {
#endif
#endif
+void btInit(int baudrate);
+void btSend(byte* data, byte length);
+
class CDataLogger {
public:
void initSender()
{
#if ENABLE_DATA_OUT
+#if USE_OBD_BT
+ btInit(9600);
+#else
mySerial.begin(9600);
#endif
+#endif
#if ENABLE_DATA_LOG && LOG_FORMAT == FORMAT_CSV
m_lastDataTime = 0;
#endif
@@ -140,17 +140,26 @@ public:
info.logType = hdr.logType;
info.logFlags = hdr.flags;
info.checksum = getChecksum((char*)&info, sizeof(info));
+#if USE_OBD_BT
+ btSend((uint8_t*)&info, sizeof(info));
+#else
mySerial.write((uint8_t*)&info, sizeof(info));
+#endif
}
void sendCommand(byte message, void* data = 0, byte bytes = 0)
{
LOG_DATA_COMMAND msg = {0, PID_MESSAGE, message};
if (data) memcpy(msg.data, data, bytes);
msg.checksum = getChecksum((char*)&msg, sizeof(msg));
+#if USE_OBD_BT
+ btSend((uint8_t*)&msg, sizeof(msg));
+#else
mySerial.write((uint8_t*)&msg, sizeof(msg));
+#endif
}
bool receiveCommand(LOG_DATA_COMMAND& msg)
{
+#if !USE_OBD_BT
if (!mySerial.available())
return false;
@@ -162,6 +171,7 @@ public:
if (getChecksum((char*)&msg, sizeof(msg)) != msg.checksum) {
return false;
}
+#endif
return true;
}
#endif
@@ -170,8 +180,12 @@ public:
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
#if ENABLE_DATA_OUT
+#if USE_OBD_BT
+ btSend((uint8_t*)&ld, 12);
+#else
mySerial.write((uint8_t*)&ld, 12);
#endif
+#endif
#if ENABLE_DATA_LOG
#if LOG_FORMAT == FORMAT_BIN
sdfile.write((uint8_t*)&ld, 12);
@@ -192,8 +206,12 @@ public:
LOG_DATA_COMM ld = {dataTime, pid, 2, 0, {value1, value2}};
ld.checksum = getChecksum((char*)&ld, 16);
#if ENABLE_DATA_OUT
+#if USE_OBD_BT
+ btSend((uint8_t*)&ld, 16);
+#else
mySerial.write((uint8_t*)&ld, 16);
#endif
+#endif
#if ENABLE_DATA_LOG
#if LOG_FORMAT == FORMAT_BIN
sdfile.write((uint8_t*)&ld, 16);
@@ -216,8 +234,12 @@ public:
LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}};
ld.checksum = getChecksum((char*)&ld, 20);
#if ENABLE_DATA_OUT
+#if USE_OBD_BT
+ btSend((uint8_t*)&ld, 20);
+#else
mySerial.write((uint8_t*)&ld, 20);
#endif
+#endif
#if ENABLE_DATA_LOG
#if LOG_FORMAT == FORMAT_BIN
sdfile.write((uint8_t*)&ld, 20);
diff --git a/megalogger/megalogger.cbp b/megalogger/megalogger.cbp
index 6d01560..1606205 100644
--- a/megalogger/megalogger.cbp
+++ b/megalogger/megalogger.cbp
@@ -127,6 +127,7 @@
<Compiler>
<Add directory="." />
</Compiler>
+ <Unit filename="config.h" />
<Unit filename="datalogger.h" />
<Unit filename="images.h" />
<Unit filename="megalogger.ino">
diff --git a/megalogger/megalogger.ino b/megalogger/megalogger.ino
index d09091a..1f4c34b 100644
--- a/megalogger/megalogger.ino
+++ b/megalogger/megalogger.ino
@@ -13,6 +13,7 @@
#include <TinyGPS.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
+#include "config.h"
#include "images.h"
#include "datalogger.h"
@@ -20,28 +21,6 @@
#error This sketch requires Arduino MEGA to work
#endif
-/**************************************
-* Choose SD pin here
-**************************************/
-//#define SD_CS_PIN 4 // ethernet shield
-//#define SD_CS_PIN 7 // microduino
-//#define SD_CS_PIN 10 // SD breakout
-#define SD_CS_PIN SS
-
-/**************************************
-* Config GPS here
-**************************************/
-#define USE_GPS
-#define GPS_BAUDRATE 38400 /* bps */
-//#define GPS_OPEN_BAUDRATE 4800 /* bps */
-
-/**************************************
-* Other options
-**************************************/
-//#define OBD_MIN_INTERVAL 200 /* ms */
-#define ACC_DATA_INTERVAL 100 /* ms */
-#define GPS_DATA_TIMEOUT 2000 /* ms */
-
// logger states
#define STATE_SD_READY 0x1
#define STATE_OBD_READY 0x2
@@ -213,14 +192,13 @@ public:
lcd.print(buf);
lcd.setCursor(250, 11);
lcd.printInt((uint16_t)(dataTime - t) / dataCount);
- lcd.setFont(FONT_SIZE_SMALL);
- lcd.print(" ms");
+ lcd.print("ms ");
dataCount = 0;
lastRefreshTime = dataTime >> 10;
}
- if (errors >= 3) {
+ if (errors >= 10) {
reconnect();
count = 0;
}
@@ -295,11 +273,18 @@ private:
{
// callback while waiting OBD data
if (getState() == OBD_CONNECTED) {
+ if (lastDataTime) {
+ if (state & STATE_ACC_READY) {
+ processAccelerometer();
+ }
#ifdef GPSUART
- if (lastDataTime && GPSUART.available())
- processGPS();
- return;
+ uint32_t t = millis();
+ while (GPSUART.available() && millis() - t < MAX_GPS_PROCESS_TIME) {
+ processGPS();
+ }
#endif
+ }
+ return;
}
// display while initializing
@@ -440,14 +425,12 @@ private:
sendQuery(pid);
- if (state & STATE_ACC_READY) {
- processAccelerometer();
- }
-
pid = 0;
if (!getResponseParsed(pid, value)) {
errors++;
return;
+ } else {
+ errors = 0;
}
dataTime = millis();
@@ -575,8 +558,7 @@ private:
lcd.setFont(FONT_SIZE_MEDIUM);
lcd.setCursor(250, 5);
lcd.printInt(((unsigned int)value - startDistance) % 1000);
- lcd.setFont(FONT_SIZE_SMALL);
- lcd.print(" km");
+ lcd.print("km");
}
break;
}
@@ -644,6 +626,18 @@ private:
static COBDLogger logger;
+#if ENABLE_DATA_OUT && USE_OBD_BT
+void btInit(int baudrate)
+{
+ logger.btInit(baudrate);
+}
+
+void btSend(byte* data, byte length)
+{
+ logger.btSend(data, length);
+}
+#endif
+
void setup()
{
#ifdef GPSUART