added Function to create CAN-Traces from WebUI
Some checks failed
CI-Build/Kettenoeler/pipeline/head There was a failure building this commit

This commit is contained in:
2025-08-26 23:31:35 +02:00
parent c8c67551fd
commit 98629b744d
10 changed files with 1011 additions and 123 deletions

View File

@@ -1,7 +1,12 @@
#include "can_obd2.h"
#include "can_hal.h"
#include "dtc.h"
#include "debugger.h"
#include "globals.h" // falls du später Einstellungen brauchst
#include "globals.h"
#include <stdarg.h>
// Trace-Sink aus webui.cpp (o.ä.)
extern void TRACE_OnObdFrame(uint32_t id, bool rx, const uint8_t *d, uint8_t dlc, const char *note);
// =======================
// Konfiguration (anpassbar)
@@ -14,7 +19,7 @@
// Antwort-Timeout auf eine einzelne Anfrage
#ifndef OBD2_RESP_TIMEOUT_MS
#define OBD2_RESP_TIMEOUT_MS 60 // ~60 ms
#define OBD2_RESP_TIMEOUT_MS 120 // etwas großzügiger für reale ECUs
#endif
// Wenn so lange keine valide Antwort kam, gilt die Geschwindigkeit als stale -> v=0
@@ -32,11 +37,22 @@
#define OBD2_DEBUG_INTERVAL_MS 1000
#endif
// Max. Delta-Zeit fürs Weg-Integrationsglied (Ausreißer-Klemme)
#ifndef OBD2_MAX_DT_MS
#define OBD2_MAX_DT_MS 200
#endif
// Erlaube einmaligen Fallback von funktionaler (0x7DF) auf physische Adresse (0x7E0)
#ifndef OBD2_ALLOW_PHYSICAL_FALLBACK
#define OBD2_ALLOW_PHYSICAL_FALLBACK 1
#endif
// =======================
// OBD-II IDs (11-bit)
// =======================
static constexpr uint16_t OBD_REQ_ID = 0x7DF; // Broadcast-Request
static constexpr uint16_t OBD_RESP_MIN = 0x7E8; // ECUs antworten 0x7E8..0x7EF
static constexpr uint16_t OBD_REQ_ID_FUNCTIONAL = 0x7DF; // Broadcast-Request
static constexpr uint16_t OBD_REQ_ID_PHYSICAL = 0x7E0; // Engine ECU (Antwort 0x7E8)
static constexpr uint16_t OBD_RESP_MIN = 0x7E8; // ECUs antworten 0x7E8..0x7EF
static constexpr uint16_t OBD_RESP_MAX = 0x7EF;
// =======================
@@ -79,7 +95,7 @@ static inline void maybeDebug(uint32_t now, const char *fmt, ...)
s_lastDbgMs = now;
va_list ap;
va_start(ap, fmt);
Debug_pushMessage(fmt, ap);
Debug_pushMessage(fmt, ap); // nimmt va_list
va_end(ap);
#else
(void)now;
@@ -113,15 +129,17 @@ bool Init_CAN_OBD2()
CAN_HAL_SetStdMask11(0, 0x7F0);
CAN_HAL_SetStdMask11(1, 0x7F0);
CanFilter flist[6] = {
CanFilter flist[8] = {
{0x7E8, false},
{0x7E9, false},
{0x7EA, false},
{0x7EB, false},
{0x7EC, false},
{0x7ED, false},
{0x7EE, false},
{0x7EF, false},
};
CAN_HAL_SetFilters(flist, 6);
CAN_HAL_SetFilters(flist, 8);
CAN_HAL_SetMode(MCP_NORMAL);
@@ -151,7 +169,11 @@ uint32_t Process_CAN_OBD2_Speed()
if (s_state == ObdState::Idle && (now - s_lastQueryTime) >= OBD2_QUERY_INTERVAL_MS)
{
uint8_t req[8] = {0x02, 0x01, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00}; // Mode 01, PID 0x0D (Speed)
const uint8_t st = CAN_HAL_Send(OBD_REQ_ID, /*ext=*/false, 8, req);
// Trace: geplanter Request (functional)
TRACE_OnObdFrame(OBD_REQ_ID_FUNCTIONAL, /*rx=*/false, req, 8, "req 01 0D (functional)");
uint8_t st = CAN_HAL_Send(OBD_REQ_ID_FUNCTIONAL, /*ext=*/false, 8, req);
s_lastQueryTime = now;
if (st == CAN_OK)
@@ -161,9 +183,23 @@ uint32_t Process_CAN_OBD2_Speed()
}
else
{
// Senden fehlgeschlagen -> harter Timeout-DTC
MaintainDTC(DTC_OBD2_CAN_TIMEOUT, true);
maybeDebug(now, "OBD2-CAN send failed (%u)\n", st);
#if OBD2_ALLOW_PHYSICAL_FALLBACK
// einmalig physisch versuchen (0x7E0 → Antwort 0x7E8)
TRACE_OnObdFrame(OBD_REQ_ID_PHYSICAL, /*rx=*/false, req, 8, "req 01 0D (physical)");
st = CAN_HAL_Send(OBD_REQ_ID_PHYSICAL, /*ext=*/false, 8, req);
s_lastQueryTime = now;
if (st == CAN_OK)
{
s_state = ObdState::Waiting;
s_requestDeadline = now + OBD2_RESP_TIMEOUT_MS;
}
else
#endif
{
// Senden fehlgeschlagen -> harter Timeout-DTC
MaintainDTC(DTC_OBD2_CAN_TIMEOUT, true);
maybeDebug(now, "OBD2-CAN send failed (%u)\n", st);
}
}
}
@@ -182,9 +218,9 @@ uint32_t Process_CAN_OBD2_Speed()
// Erwartete Formate:
// - Einfache Antwort: 0x41 0x0D <A> ...
// - Mit Längen-Byte: 0x03 0x41 0x0D <A> ...
// - Mit Längen-Byte: 0x03/0x04 0x41 0x0D <A> ...
uint8_t modeResp = 0, pid = 0, speedKmh = 0;
if (rx[0] == 0x03 && len >= 4 && rx[1] == 0x41 && rx[2] == 0x0D)
if ((rx[0] == 0x03 || rx[0] == 0x04) && len >= 4 && rx[1] == 0x41 && rx[2] == 0x0D)
{
modeResp = rx[1];
pid = rx[2];
@@ -198,7 +234,9 @@ uint32_t Process_CAN_OBD2_Speed()
}
else
{
continue; // anderes PID/Format ignorieren
// Nicht das gesuchte PID → optional trotzdem loggen
TRACE_OnObdFrame(rxId, /*rx=*/true, rx, len, "other OBD resp");
continue;
}
if (modeResp == 0x41 && pid == 0x0D)
@@ -211,10 +249,19 @@ uint32_t Process_CAN_OBD2_Speed()
MaintainDTC(DTC_OBD2_CAN_TIMEOUT, false);
MaintainDTC(DTC_OBD2_CAN_NO_RESPONSE, false);
char note[40];
snprintf(note, sizeof(note), "speed=%ukmh", (unsigned)speedKmh);
TRACE_OnObdFrame(rxId, /*rx=*/true, rx, len, note);
maybeDebug(now, "OBD2 speed: %u km/h (%lu mm/s)\n",
(unsigned)speedKmh, (unsigned long)s_lastSpeedMMps);
break; // eine valide Antwort pro Zyklus reicht
}
else
{
// ist zwar OBD-II Antwort, aber nicht unser PID optional loggen
TRACE_OnObdFrame(rxId, /*rx=*/true, rx, len, "other OBD resp");
}
}
// 3) Offene Anfrage: Timeout prüfen
@@ -223,13 +270,17 @@ uint32_t Process_CAN_OBD2_Speed()
// Keine passende Antwort erhalten
MaintainDTC(DTC_OBD2_CAN_NO_RESPONSE, true);
s_state = ObdState::Idle;
TRACE_OnObdFrame(0x000, /*rx=*/true, nullptr, 0, "timeout 01 0D");
}
// 4) Integration (mm) über dt
uint32_t add_mm = 0;
if (s_lastIntegrateMs == 0)
s_lastIntegrateMs = now;
const uint32_t dt_ms = now - s_lastIntegrateMs;
uint32_t raw_dt = now - s_lastIntegrateMs;
if (raw_dt > OBD2_MAX_DT_MS) raw_dt = OBD2_MAX_DT_MS; // Ausreißer klemmen
const uint32_t dt_ms = raw_dt;
s_lastIntegrateMs = now;
// Stale-Schutz: wenn lange keine Antwort -> v=0