summaryrefslogtreecommitdiff
path: root/megalogger
diff options
context:
space:
mode:
Diffstat (limited to 'megalogger')
-rw-r--r--megalogger/config.h2
-rw-r--r--megalogger/datalogger.h146
-rw-r--r--megalogger/megalogger.ino24
3 files changed, 115 insertions, 57 deletions
diff --git a/megalogger/config.h b/megalogger/config.h
index 4b25e01..8feb1fe 100644
--- a/megalogger/config.h
+++ b/megalogger/config.h
@@ -45,7 +45,7 @@
// 38400bps for G6010 5Hz GPS receiver
// 115200bps for G7020 10Hz GPS receiver
-#define GPS_BAUDRATE 38400 /* bps */
+#define GPS_BAUDRATE 115200 /* bps */
/**************************************
* Accelerometer & Gyro
diff --git a/megalogger/datalogger.h b/megalogger/datalogger.h
index 2a58bf3..49cac28 100644
--- a/megalogger/datalogger.h
+++ b/megalogger/datalogger.h
@@ -1,13 +1,13 @@
/*************************************************************************
* Arduino Data Logger Class
* Distributed under GPL v2.0
-* Copyright (c) 2013-2014 Stanley Huang <stanleyhuangyc@gmail.com>
-* All rights reserved.
+* Written by Stanley Huang <stanleyhuangyc@gmail.com>
* Visit http://freematics.com for more information
*************************************************************************/
#define FORMAT_BIN 0
#define FORMAT_CSV 1
+#define FORMAT_TEXT 2
typedef struct {
uint32_t time;
@@ -17,8 +17,6 @@ typedef struct {
float value[3];
} LOG_DATA_COMM;
-#define HEADER_LEN 128 /* bytes */
-
#define PID_GPS_LATITUDE 0xA
#define PID_GPS_LONGITUDE 0xB
#define PID_GPS_ALTITUDE 0xC
@@ -34,22 +32,16 @@ typedef struct {
#define PID_MEMS_TEMP 0x23
#define PID_BATTERY_VOLTAGE 0x24
+#define PID_DATA_SIZE 0x80
+
#define FILE_NAME_FORMAT "/DAT%05d.CSV"
#if ENABLE_DATA_OUT
#if USE_SOFTSERIAL
-
-#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
- SoftwareSerial SerialBLE(A8, A9); /* for BLE Shield on MEGA*/
+SoftwareSerial SerialRF(A2, A3);
#else
- SoftwareSerial SerialBLE(A2, A3); /* for BLE Shield on UNO/leonardo*/
-#endif
-
-#else
-
-#define SerialBLE Serial3
-
+#define SerialRF Serial
#endif
#endif
@@ -58,34 +50,62 @@ typedef struct {
static File sdfile;
#endif
-static const char* idstr = "FREEMATICS\r";
+typedef struct {
+ uint16_t pid;
+ char name[3];
+} PID_NAME;
+
+const PID_NAME pidNames[] PROGMEM = {
+{PID_ACC, {'A','C','C'}},
+{PID_GYRO, {'G','Y','R'}},
+{PID_COMPASS, {'M','A','G'}},
+{PID_GPS_LATITUDE, {'L','A','T'}},
+{PID_GPS_LONGITUDE, {'L','O','N'}},
+{PID_GPS_ALTITUDE, {'A','L','T'}},
+{PID_GPS_SPEED, {'S','P','D'}},
+{PID_GPS_HEADING, {'C','R','S'}},
+{PID_GPS_SAT_COUNT, {'S','A','T'}},
+{PID_GPS_TIME, {'T','M','E'}},
+{PID_GPS_DATE, {'D','T','E'}},
+{PID_BATTERY_VOLTAGE, {'B','A','T'}},
+{PID_DATA_SIZE, {'D','A','T'}},
+};
class CDataLogger {
public:
void initSender()
{
#if ENABLE_DATA_OUT
- SerialBLE.begin(STREAM_BAUDRATE);
- SerialBLE.print(idstr);
+ SerialRF.begin(STREAM_BAUDRATE);
+ /*
+ SerialRF.print("AT+NAMEFreematics");
+ delay(10);
+ while (SerialRF.available()) SerialRF.read();
+ SerialRF.println();
+ */
+ m_lastSendTime = 0;
#endif
#if ENABLE_DATA_LOG
m_lastDataTime = 0;
#endif
}
- void logTimeElapsed()
+ void recordData(const char* buf)
{
#if ENABLE_DATA_LOG
dataSize += sdfile.print(dataTime - m_lastDataTime);
- sdfile.write(',');
- dataSize++;
+ dataSize += sdfile.write(',');
+ dataSize += sdfile.write(buf);
m_lastDataTime = dataTime;
#endif
+#if MIN_DATA_INTERVAL
+ uint32_t t = millis();
+ uint32_t elapsed = t - m_lastSendTime;
+ if (elapsed < MIN_DATA_INTERVAL) delay(MIN_DATA_INTERVAL - elapsed);
+ m_lastSendTime = t;
+#endif
}
void logData(char c)
{
-#if ENABLE_DATA_OUT && STREAM_FORMAT == FORMAT_CSV
- SerialBLE.write(c);
-#endif
#if ENABLE_DATA_LOG
if (c >= ' ') {
sdfile.write(c);
@@ -96,56 +116,78 @@ public:
void logData(uint16_t pid, int value)
{
char buf[16];
- byte n = sprintf(buf, "%X,%d\r", pid, value);
+#if STREAM_FORMAT == FORMAT_TEXT
+ sprintf(buf + translatePIDName(pid, buf), "%d\r", value);
+#else
+ sprintf(buf, "%X,%d\r", pid, value);
+#endif
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
- SerialBLE.write((uint8_t*)&ld, 12);
+ SerialRF.write((uint8_t*)&ld, 12);
#else
- SerialBLE.write((uint8_t*)buf, n);
+ SerialRF.print(buf);
#endif
#endif
-#if ENABLE_DATA_LOG
- logTimeElapsed();
- dataSize += sdfile.write((uint8_t*)buf, n);
-#endif
+ recordData(buf);
}
void logData(uint16_t pid, int32_t value)
{
char buf[20];
- byte n = sprintf(buf, "%X,%ld\r", pid, value);
+#if STREAM_FORMAT == FORMAT_TEXT
+ sprintf(buf + translatePIDName(pid, buf), "%ld\r", value);
+#else
+ sprintf(buf, "%X,%ld\r", pid, value);
+#endif
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
- SerialBLE.write((uint8_t*)&ld, 12);
+ SerialRF.write((uint8_t*)&ld, 12);
#else
- SerialBLE.write((uint8_t*)buf, n);
+ SerialRF.print(buf);
#endif
#endif
-#if ENABLE_DATA_LOG
- logTimeElapsed();
- dataSize += sdfile.write((uint8_t*)buf, n);
+ recordData(buf);
+ }
+ void logData(uint16_t pid, uint32_t value)
+ {
+ char buf[20];
+#if STREAM_FORMAT == FORMAT_TEXT
+ sprintf(buf + translatePIDName(pid, buf), "%lu\r", value);
+#else
+ sprintf(buf, "%X,%lu\r", pid, value);
#endif
+#if ENABLE_DATA_OUT
+#if STREAM_FORMAT == FORMAT_BIN
+ LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
+ ld.checksum = getChecksum((char*)&ld, 12);
+ SerialRF.write((uint8_t*)&ld, 12);
+#else
+ SerialRF.print(buf);
+#endif
+#endif
+ recordData(buf);
}
void logData(uint16_t pid, int value1, int value2, int value3)
{
char buf[24];
- byte n = sprintf(buf, "%X,%d,%d,%d\r", pid, value1, value2, value3);
+#if STREAM_FORMAT == FORMAT_TEXT
+ sprintf(buf + translatePIDName(pid, buf), "%d,%d,%d\r", value1, value2, value3);
+#else
+ sprintf(buf, "%X,%d,%d,%d\r", pid, value1, value2, value3);
+#endif
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}};
ld.checksum = getChecksum((char*)&ld, 20);
- SerialBLE.write((uint8_t*)&ld, 20);
+ SerialRF.write((uint8_t*)&ld, 20);
#else
- SerialBLE.write((uint8_t*)buf, n);
-#endif
+ SerialRF.print(buf);
#endif
-#if ENABLE_DATA_LOG
- logTimeElapsed();
- dataSize += sdfile.write((uint8_t*)buf, n);
#endif
+ recordData(buf);
}
#if ENABLE_DATA_LOG
uint16_t openFile(uint16_t logFlags = 0, uint32_t dateTime = 0)
@@ -173,6 +215,7 @@ public:
if (!sdfile) {
return 0;
}
+ m_lastDataTime = dateTime;
return fileIndex;
}
void closeFile()
@@ -195,7 +238,24 @@ private:
}
return checksum;
}
+#if STREAM_FORMAT == FORMAT_TEXT
+ byte translatePIDName(uint16_t pid, char* text)
+ {
+ for (uint16_t n = 0; n < sizeof(pidNames) / sizeof(pidNames[0]); n++) {
+ uint16_t id = pgm_read_word(&pidNames[n].pid);
+ if (pid == id) {
+ memcpy_P(text, pidNames[n].name, 3);
+ text[3] = '=';
+ return 4;
+ }
+ }
+ return sprintf(text, "%X=", pid);
+ }
+#endif
#if ENABLE_DATA_LOG
uint32_t m_lastDataTime;
#endif
+#if ENABLE_DATA_OUT
+ uint32_t m_lastSendTime;
+#endif
};
diff --git a/megalogger/megalogger.ino b/megalogger/megalogger.ino
index 8ef548f..629450f 100644
--- a/megalogger/megalogger.ino
+++ b/megalogger/megalogger.ino
@@ -1,22 +1,24 @@
/*************************************************************************
-* Arduino GPS/OBD-II/9-Axis Data Logger
-* Distributed under GPL v2.0
-* Copyright (c) 2013-2015 Stanley Huang <stanleyhuangyc@gmail.com>
-* All rights reserved.
+* Reference code for OBD-II/GPS/9-Axis Data Logger
+* Works with Freematics OBD-II Telematics Advanced Kit
* Visit http://freematics.com for more information
+* Distributed under GPL v2.0
+* Written by Stanley Huang <stanleyhuangyc@gmail.com>
*************************************************************************/
#include <Arduino.h>
#include <Wire.h>
#include <OBD.h>
#include <SPI.h>
-#include <SD.h>
#include <MultiLCD.h>
#include <TinyGPS.h>
#include <I2Cdev.h>
#include <MPU9150.h>
-#include "Narcoleptic.h"
#include "config.h"
+#if ENABLE_DATA_LOG
+#include <SD.h>
+#endif
+#include "Narcoleptic.h"
#include "images.h"
#if ENABLE_DATA_OUT && USE_SOFTSERIAL
#include <SoftwareSerial.h>
@@ -765,11 +767,12 @@ void setup()
lcd.setColor(RGB16_GREEN);
lcd.setFontSize(FONT_SIZE_MEDIUM);
lcd.println("OBD READY!");
- lcd.setColor(RGB16_YELLOW);
char buf[OBD_RECV_BUF_SIZE];
if (obd.getVIN(buf)) {
+ lcd.setColor(RGB16_WHITE);
lcd.print("VIN:");
+ lcd.setColor(RGB16_YELLOW);
lcd.print(buf);
}
@@ -794,13 +797,8 @@ void loop()
static byte index2 = 0;
static byte index3 = 0;
uint32_t t = millis();
- byte pid;
-
- pid = pgm_read_byte(pidTier1 + index);
+ byte pid = pgm_read_byte(pidTier1 + index++);
logOBDData(pid);
- index++;
- t = millis() - t;
-
if (index == TIER_NUM1) {
index = 0;
if (index2 == TIER_NUM2) {