12-OBD-Speed: CAN logger page, shared SD mount, more PSRAM
- CAN log page: ELM327 monitor-mode survey/capture of broadcast frames with marks, written to /canlog on the SD card; serial "ls"/"get" commands to download logs - sd_card: reference-counted SD_MMC mount shared by bg_store and the logger - heap_caps_malloc_extmem_enable(64): internal RAM was down to ~10 KB, SD mounts failed with ESP_ERR_NO_MEM (DMA); now ~23 KB free, 13.8 KB DMA block - docs/canlog: first test drive (Fiesta Mk8 2018) - no broadcast frames on the OBD port, only diagnostic replies (gateway) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
21012adbec
commit
60cd7cf262
16 files changed
+1975
-47
No files matched your search
+384
-33
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <NimBLEDevice.h>
|
||||
#include <Preferences.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "can_log.h"
|
||||
|
||||
#define RETRY_DELAY_MS 3000
|
||||
#define IDLE_RETRY_DELAY_MS 30000
|
||||
@@ -9,6 +12,13 @@
|
||||
#define VOLTAGE_INTERVAL_MS 2000 // ATRV while driving
|
||||
#define IDLE_VOLTAGE_INTERVAL_MS 30000 // ATRV while idle (adapter-internal, doesn't wake the car's bus)
|
||||
#define WAKE_VOLTAGE 13.2f // above this the alternator is charging: engine running
|
||||
#define LOG_OBD_INTERVAL_MS 2000 // speed while logging (between monitor windows)
|
||||
#define LOG_SURVEY_MS 1300 // per ID range; > 1 s so slow (1 Hz) messages are seen
|
||||
#define LOG_SURVEY_MAX_LINES 300 // more in one window: split the range
|
||||
#define LOG_CAPTURE_MS 1300 // waiting for one frame of an ID
|
||||
#define LOG_RAW_MS 2000
|
||||
#define LOG_RESURVEY_MS (10 * 60 * 1000)
|
||||
#define LOG_TARGET_EVERY 8 // the candidate IDs after every 8 others
|
||||
|
||||
// ---------- shared state (protected by `mtx`) ----------
|
||||
static SemaphoreHandle_t mtx;
|
||||
@@ -34,6 +44,19 @@ static ObdDevInfo req_dev;
|
||||
static NimBLEClient *client = nullptr;
|
||||
static NimBLERemoteCharacteristic *chr_write = nullptr, *chr_notify = nullptr;
|
||||
static Preferences prefs;
|
||||
static uint32_t next_attempt = 0, next_voltage = 0;
|
||||
static int fails = 0;
|
||||
|
||||
// CAN logging (see can_log.h), worker-owned
|
||||
enum class LogMode
|
||||
{
|
||||
Off, // not logging (or not set up since the last connect)
|
||||
Survey, // finding the IDs on the bus, range by range
|
||||
Capture, // one frame of every known ID in turn
|
||||
Raw, // adapter can't filter by wildcard: unfiltered monitor windows
|
||||
NoCan, // not a CAN car: only speed/voltage are logged
|
||||
};
|
||||
static LogMode log_mode = LogMode::Off;
|
||||
|
||||
static void lock() { xSemaphoreTake(mtx, portMAX_DELAY); }
|
||||
static void unlock() { xSemaphoreGive(mtx); }
|
||||
@@ -122,14 +145,20 @@ static void on_notify(NimBLERemoteCharacteristic *c, uint8_t *data, size_t len,
|
||||
unlock();
|
||||
}
|
||||
|
||||
static bool elm_write(const String &s)
|
||||
{
|
||||
if (!chr_write) // dropped meanwhile (adapter not answering)
|
||||
return false;
|
||||
return chr_write->writeValue((const uint8_t *)s.c_str(), s.length(), !chr_write->canWriteNoResponse());
|
||||
}
|
||||
|
||||
// Sends a command, waits for the '>' prompt. Returns the reply without prompt/CR, or "" on timeout.
|
||||
static String elm_command(const char *cmd, uint32_t timeout_ms)
|
||||
{
|
||||
lock();
|
||||
elm_rx = "";
|
||||
unlock();
|
||||
String line = String(cmd) + "\r";
|
||||
if (!chr_write->writeValue((const uint8_t *)line.c_str(), line.length(), !chr_write->canWriteNoResponse()))
|
||||
if (!elm_write(String(cmd) + "\r"))
|
||||
return "";
|
||||
uint32_t t0 = millis();
|
||||
String rx;
|
||||
@@ -152,6 +181,51 @@ static String elm_command(const char *cmd, uint32_t timeout_ms)
|
||||
return "";
|
||||
}
|
||||
|
||||
static String elm_command(const String &cmd, uint32_t timeout_ms) { return elm_command(cmd.c_str(), timeout_ms); }
|
||||
|
||||
// Monitor mode (ATMA) with the current CAN filter: collects received lines until `max_lines` arrived or `ms`
|
||||
// passed, then stops it with a CR. Returns false if the adapter never came back with a prompt.
|
||||
static bool elm_monitor(uint32_t ms, int max_lines, std::vector<String> &lines)
|
||||
{
|
||||
lines.clear();
|
||||
lock();
|
||||
elm_rx = "";
|
||||
unlock();
|
||||
if (!elm_write("ATMA\r"))
|
||||
return false;
|
||||
bool prompt = false, stop_sent = false;
|
||||
uint32_t t0 = millis(), t_stop = 0;
|
||||
for (;;)
|
||||
{
|
||||
lock();
|
||||
int cr;
|
||||
while ((cr = elm_rx.indexOf('\r')) >= 0)
|
||||
{
|
||||
String line = elm_rx.substring(0, cr);
|
||||
elm_rx.remove(0, cr + 1);
|
||||
line.trim();
|
||||
if (line.length() && line != "STOPPED")
|
||||
lines.push_back(line);
|
||||
}
|
||||
prompt = elm_rx.indexOf('>') >= 0; // monitoring ended (stopped by us, BUFFER FULL, error)
|
||||
unlock();
|
||||
if (prompt)
|
||||
return true;
|
||||
if (!client->isConnected())
|
||||
return false;
|
||||
if (!stop_sent && (millis() - t0 >= ms || (int)lines.size() >= max_lines))
|
||||
{
|
||||
if (!elm_write("\r"))
|
||||
return false;
|
||||
stop_sent = true;
|
||||
t_stop = millis();
|
||||
}
|
||||
if (stop_sent && millis() - t_stop > 1500)
|
||||
return false;
|
||||
vTaskDelay(pdMS_TO_TICKS(5));
|
||||
}
|
||||
}
|
||||
|
||||
// "410D3C" (spaces/headers removed by ATS0/ATH0; multiple ECUs possible) -> 60
|
||||
static int parse_speed(String reply)
|
||||
{
|
||||
@@ -187,6 +261,12 @@ static void disconnect()
|
||||
if (client && client->isConnected())
|
||||
client->disconnect();
|
||||
speed_kmh = -1;
|
||||
if (log_mode != LogMode::Off)
|
||||
{
|
||||
can_log_line("E disconnected");
|
||||
can_log_set_phase("Adapter not connected");
|
||||
}
|
||||
log_mode = LogMode::Off; // ATZ on reconnect resets the adapter: set up again
|
||||
}
|
||||
|
||||
// Finds the ELM UART characteristics: service FFF0 (verified: FFF1 notify + write), else any notify+write pair
|
||||
@@ -271,6 +351,296 @@ static bool connect_and_init()
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------- speed polling ----------
|
||||
// Counts a missing prompt; after a few the adapter is considered hung and the link is dropped.
|
||||
static void note_no_reply()
|
||||
{
|
||||
if (++fails >= POLL_FAIL_LIMIT)
|
||||
{
|
||||
set_status("Adapter not answering");
|
||||
disconnect();
|
||||
next_attempt = millis() + RETRY_DELAY_MS;
|
||||
}
|
||||
}
|
||||
|
||||
static void poll_speed()
|
||||
{
|
||||
String reply = elm_command("010D", 1500);
|
||||
int v = parse_speed(reply);
|
||||
speed_kmh = v;
|
||||
can_log_line("V " + String(v));
|
||||
if (v >= 0)
|
||||
{
|
||||
fails = 0;
|
||||
last_data_ms = millis();
|
||||
set_status(saved_name.length() ? saved_name : saved_addr);
|
||||
}
|
||||
else if (reply.length() == 0)
|
||||
{
|
||||
// no prompt at all: adapter hung or link dead
|
||||
note_no_reply();
|
||||
}
|
||||
else
|
||||
{
|
||||
// "NO DATA", "UNABLE TO CONNECT", "STOPPED"...: ignition off or ECU busy
|
||||
fails = 0;
|
||||
String r = reply;
|
||||
r.replace("\n", " ");
|
||||
set_status(r.substring(0, 24));
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- CAN logging ----------
|
||||
// Ford candidates from opendbc (ford_lincoln_base_pt.dbc, newer Fords - to be confirmed on the Fiesta Mk8):
|
||||
// 0x165 EngBrakeData: Veh_V_DsplyCcSet (cruise set speed as shown), 0x202: Veh_V_RqCcSet (ACC set speed),
|
||||
// 0x3D9 IPMA_Data2: IsaVLim_D_Rq (speed limit from the camera)
|
||||
static const uint16_t LOG_TARGETS[CANLOG_SLOTS] = {0x165, 0x202, 0x3D9};
|
||||
static std::vector<String> survey_queue; // ID patterns still to listen to ("3XX", "3DX", "3D9")
|
||||
static std::vector<uint16_t> log_ids; // all IDs seen, sorted
|
||||
static size_t capture_index = 0;
|
||||
static uint32_t capture_count = 0;
|
||||
static uint32_t next_log_obd = 0, next_survey = 0;
|
||||
static bool exact_filter = false; // adapter supports ATCRA hhh
|
||||
static int id_digits = 3; // 11-bit IDs; 29-bit in Raw mode
|
||||
static bool caf_on = true; // CAN auto formatting (ISO-TP), the adapter's default
|
||||
|
||||
// Monitoring wants the raw frames (CAF off), OBD requests need CAF on (adds/strips the ISO-TP byte)
|
||||
static void set_caf(bool on)
|
||||
{
|
||||
if (on != caf_on && elm_command(on ? "ATCAF1" : "ATCAF0", 1500).length())
|
||||
caf_on = on;
|
||||
}
|
||||
|
||||
// "3D9FE00000000000" (ATH1, ATS0) -> id "3D9", data "FE00000000000000"
|
||||
static bool parse_frame(String line, String &id, String &data)
|
||||
{
|
||||
line.replace(" ", "");
|
||||
if (line.length() < (unsigned)id_digits || (line.length() - id_digits) % 2)
|
||||
return false;
|
||||
for (unsigned i = 0; i < line.length(); i++)
|
||||
if (!isxdigit((unsigned char)line[i]))
|
||||
return false;
|
||||
line.toUpperCase();
|
||||
id = line.substring(0, id_digits);
|
||||
data = line.substring(id_digits);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void note_id(uint16_t id)
|
||||
{
|
||||
auto it = std::lower_bound(log_ids.begin(), log_ids.end(), id);
|
||||
if (it == log_ids.end() || *it != id)
|
||||
log_ids.insert(it, id);
|
||||
}
|
||||
|
||||
// Logs the received lines: frames (at most `per_id` per ID, 0 = all) and adapter messages
|
||||
static void log_lines(const std::vector<String> &lines, int per_id, bool *overflow)
|
||||
{
|
||||
std::vector<std::pair<String, int>> counts;
|
||||
for (const String &line : lines)
|
||||
{
|
||||
String id, data;
|
||||
if (!parse_frame(line, id, data))
|
||||
{
|
||||
if (line.indexOf("BUFFER FULL") >= 0 && overflow)
|
||||
*overflow = true;
|
||||
can_log_line("E " + line);
|
||||
continue;
|
||||
}
|
||||
if (id_digits == 3)
|
||||
note_id((uint16_t)strtol(id.c_str(), nullptr, 16));
|
||||
int n = 0;
|
||||
for (auto &c : counts)
|
||||
if (c.first == id)
|
||||
n = ++c.second;
|
||||
if (n == 0)
|
||||
counts.push_back({id, n = 1});
|
||||
if (per_id == 0 || n <= per_id)
|
||||
can_log_line("F " + id + " " + data);
|
||||
for (int s = 0; s < CANLOG_SLOTS; s++)
|
||||
if (id_digits == 3 && strtol(id.c_str(), nullptr, 16) == LOG_TARGETS[s])
|
||||
can_log_set_frame(s, data);
|
||||
}
|
||||
}
|
||||
|
||||
static String id_hex(uint16_t id)
|
||||
{
|
||||
char b[4];
|
||||
snprintf(b, sizeof(b), "%03X", id);
|
||||
return b;
|
||||
}
|
||||
|
||||
// Waits for one frame of `id`
|
||||
static void capture_id(uint16_t id)
|
||||
{
|
||||
if (elm_command("ATCRA " + id_hex(id), 1500).length() == 0)
|
||||
return note_no_reply();
|
||||
std::vector<String> lines;
|
||||
if (!elm_monitor(LOG_CAPTURE_MS, 1, lines))
|
||||
return note_no_reply();
|
||||
fails = 0;
|
||||
if (lines.empty())
|
||||
can_log_line("N " + id_hex(id));
|
||||
log_lines(lines, 0, nullptr);
|
||||
}
|
||||
|
||||
// Listens to one ID range ("3XX"); a range too busy for the adapter is split into 16 smaller ones
|
||||
static void survey_range(const String &pattern)
|
||||
{
|
||||
if (elm_command("ATCRA " + pattern, 1500).length() == 0)
|
||||
return note_no_reply();
|
||||
std::vector<String> lines;
|
||||
if (!elm_monitor(LOG_SURVEY_MS, LOG_SURVEY_MAX_LINES, lines))
|
||||
return note_no_reply();
|
||||
fails = 0;
|
||||
bool overflow = (int)lines.size() >= LOG_SURVEY_MAX_LINES;
|
||||
log_lines(lines, 2, &overflow);
|
||||
int x = pattern.indexOf('X');
|
||||
if (overflow && x >= 0)
|
||||
for (int d = 0; d < 16; d++)
|
||||
{
|
||||
String child = pattern;
|
||||
child.setCharAt(x, "0123456789ABCDEF"[d]);
|
||||
survey_queue.push_back(child);
|
||||
}
|
||||
}
|
||||
|
||||
static void start_survey()
|
||||
{
|
||||
survey_queue.clear();
|
||||
for (int i = 0; i < 8; i++)
|
||||
survey_queue.push_back(String(i) + "XX");
|
||||
log_mode = LogMode::Survey;
|
||||
}
|
||||
|
||||
// Called once per connection while logging is on: checks the protocol and what the adapter can filter
|
||||
static void log_setup()
|
||||
{
|
||||
can_log_line("C " + (saved_name.length() ? saved_name : saved_addr) + " " + elm_command("ATI", 1500));
|
||||
String dp = elm_command("ATDPN", 1500); // "A6" = auto, ISO 15765-4 CAN 11 bit 500 kbit
|
||||
can_log_line("P " + dp);
|
||||
char p = dp.length() ? dp[dp.length() - 1] : '?';
|
||||
next_log_obd = 0;
|
||||
caf_on = true; // adapter was just reset (ATZ)
|
||||
if (p < '6' || p > '9')
|
||||
{
|
||||
log_mode = LogMode::NoCan;
|
||||
can_log_set_phase("Not a CAN protocol (" + dp + ")");
|
||||
return;
|
||||
}
|
||||
// silent monitoring: the adapter never acknowledges or disturbs frames on the bus
|
||||
can_log_line("E ATCSM1 " + elm_command("ATCSM1", 1500));
|
||||
elm_command("ATH1", 1500); // IDs in front of the data
|
||||
id_digits = (p == '7' || p == '9') ? 8 : 3;
|
||||
exact_filter = id_digits == 3 && elm_command("ATCRA 3D9", 1500).indexOf('?') < 0;
|
||||
bool wildcard = exact_filter && elm_command("ATCRA 7XX", 1500).indexOf('?') < 0;
|
||||
elm_command("ATAR", 1500);
|
||||
if (wildcard)
|
||||
{
|
||||
start_survey();
|
||||
}
|
||||
else
|
||||
{
|
||||
log_mode = LogMode::Raw;
|
||||
can_log_line(String("E no wildcard filter") + (exact_filter ? "" : ", no ID filter") + ": unfiltered");
|
||||
}
|
||||
}
|
||||
|
||||
static void log_stop()
|
||||
{
|
||||
set_caf(true);
|
||||
elm_command("ATAR", 1500);
|
||||
elm_command("ATH0", 1500);
|
||||
log_mode = LogMode::Off;
|
||||
can_log_set_phase("");
|
||||
}
|
||||
|
||||
// One unit of logging work (one ID range / one ID / one raw window), with speed + voltage every 2 s
|
||||
static void log_step()
|
||||
{
|
||||
if (log_mode == LogMode::Off)
|
||||
log_setup();
|
||||
if ((int32_t)(millis() - next_log_obd) >= 0)
|
||||
{
|
||||
next_log_obd = millis() + LOG_OBD_INTERVAL_MS;
|
||||
elm_command("ATAR", 1500); // the ID filter would hide the ECU's reply
|
||||
set_caf(true);
|
||||
poll_speed();
|
||||
if ((int32_t)(millis() - next_voltage) >= 0)
|
||||
{
|
||||
next_voltage = millis() + VOLTAGE_INTERVAL_MS;
|
||||
read_voltage();
|
||||
can_log_line("B " + String(voltage, 1));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!client->isConnected() || !chr_write)
|
||||
return;
|
||||
if (log_mode != LogMode::NoCan)
|
||||
set_caf(false);
|
||||
|
||||
switch (log_mode)
|
||||
{
|
||||
case LogMode::Survey:
|
||||
if (survey_queue.empty())
|
||||
{
|
||||
String ids;
|
||||
for (uint16_t id : log_ids)
|
||||
ids += " " + id_hex(id);
|
||||
can_log_line("I" + ids);
|
||||
log_mode = LogMode::Capture;
|
||||
next_survey = millis() + LOG_RESURVEY_MS;
|
||||
break;
|
||||
}
|
||||
{
|
||||
String pattern = survey_queue.front();
|
||||
survey_queue.erase(survey_queue.begin());
|
||||
can_log_set_phase("Survey " + pattern + ": " + log_ids.size() + " IDs, " + survey_queue.size() + " ranges left");
|
||||
survey_range(pattern);
|
||||
}
|
||||
break;
|
||||
case LogMode::Capture:
|
||||
if ((int32_t)(millis() - next_survey) >= 0)
|
||||
{
|
||||
start_survey(); // now and then look for IDs that were quiet before
|
||||
break;
|
||||
}
|
||||
if (capture_count++ % (LOG_TARGET_EVERY + 1) == 0 || log_ids.empty())
|
||||
{
|
||||
for (uint16_t t : LOG_TARGETS)
|
||||
capture_id(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t id = log_ids[capture_index++ % log_ids.size()];
|
||||
if (std::find(std::begin(LOG_TARGETS), std::end(LOG_TARGETS), id) == std::end(LOG_TARGETS))
|
||||
capture_id(id);
|
||||
}
|
||||
can_log_set_phase("Capturing " + String(log_ids.size()) + " IDs");
|
||||
break;
|
||||
case LogMode::Raw:
|
||||
{
|
||||
can_log_set_phase("Unfiltered (adapter can't filter)");
|
||||
std::vector<String> lines;
|
||||
if (!elm_monitor(LOG_RAW_MS, LOG_SURVEY_MAX_LINES, lines))
|
||||
return note_no_reply();
|
||||
fails = 0;
|
||||
log_lines(lines, 0, nullptr);
|
||||
if (exact_filter)
|
||||
{
|
||||
for (uint16_t t : LOG_TARGETS)
|
||||
capture_id(t);
|
||||
elm_command("ATAR", 1500);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: // NoCan: only speed and voltage
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- worker task ----------
|
||||
static void load_saved()
|
||||
{
|
||||
@@ -283,9 +653,6 @@ static void load_saved()
|
||||
|
||||
static void worker_task(void *)
|
||||
{
|
||||
uint32_t next_attempt = 0;
|
||||
uint32_t next_voltage = 0;
|
||||
int fails = 0;
|
||||
last_data_ms = millis();
|
||||
for (;;)
|
||||
{
|
||||
@@ -304,6 +671,7 @@ static void worker_task(void *)
|
||||
idle = true;
|
||||
speed_kmh = -1;
|
||||
Serial.println("[obd] idle");
|
||||
can_log_line("E idle");
|
||||
}
|
||||
|
||||
// requests from the UI
|
||||
@@ -381,12 +749,22 @@ static void worker_task(void *)
|
||||
idle = false;
|
||||
last_data_ms = millis();
|
||||
Serial.printf("[obd] wake (%.1f V)\n", voltage);
|
||||
can_log_line("E wake " + String(voltage, 1) + " V");
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
continue;
|
||||
}
|
||||
|
||||
// CAN logging on: monitor windows with speed/voltage in between
|
||||
if (can_log_enabled())
|
||||
{
|
||||
log_step();
|
||||
continue;
|
||||
}
|
||||
if (log_mode != LogMode::Off)
|
||||
log_stop();
|
||||
|
||||
if ((int32_t)(millis() - next_voltage) >= 0)
|
||||
{
|
||||
next_voltage = millis() + VOLTAGE_INTERVAL_MS;
|
||||
@@ -394,34 +772,7 @@ static void worker_task(void *)
|
||||
}
|
||||
|
||||
// connected: poll the vehicle speed
|
||||
String reply = elm_command("010D", 1500);
|
||||
int v = parse_speed(reply);
|
||||
speed_kmh = v;
|
||||
if (v >= 0)
|
||||
{
|
||||
fails = 0;
|
||||
last_data_ms = millis();
|
||||
set_status(saved_name.length() ? saved_name : saved_addr);
|
||||
}
|
||||
else if (reply.length() == 0)
|
||||
{
|
||||
// no prompt at all: adapter hung or link dead
|
||||
if (++fails >= POLL_FAIL_LIMIT)
|
||||
{
|
||||
set_status("Adapter not answering");
|
||||
disconnect();
|
||||
next_attempt = millis() + RETRY_DELAY_MS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// "NO DATA", "UNABLE TO CONNECT", "STOPPED"...: ignition off or ECU busy
|
||||
fails = 0;
|
||||
String r = reply;
|
||||
r.replace("\n", " ");
|
||||
set_status(r.substring(0, 24));
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
poll_speed();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user