Initial sync implementation

Implemented system sync with crank and cam state and simple error
handling
This commit is contained in:
2026-06-03 10:56:28 +02:00
parent 21f0b5a3ea
commit 42e64b673c
6 changed files with 115 additions and 35 deletions

View File

@@ -4,22 +4,37 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "cmsis_os2.h"
#include "macros.h"
#include "main.h"
#include "ring_buffer.h"
#include "tasks.h"
#ifdef DEBUG
#include "SEGGER_RTT.h"
#endif
void camHandler(void *argument) {
for (;;) {
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
#ifdef DEBUG
SEGGER_RTT_printf(0, "Cam pulse detected at: %lu\n\r",
ringBufferRead(&state_g.cam_RB, 0));
#endif /* ifdef DEBUG */
if (state_g.sync == SYNC_OK) {
// TODO complete algorithm for scheduling spark
DEBUG_LOG("Cam pulse detected at: %lu\n\r",
ringBufferRead(&state_g.cam_RB, 0));
// FILTERS
state_g.cam_state = CAM_TRIGD;
state_g.cam_miss_ctr = 0;
switch (state_g.sync_state) {
case SYNC_NOT_OK: {
state_g.sync_state = SYNC_PENDING;
DEBUG_LOG("Sync state updated to pending\n\r");
break;
}
case SYNC_PENDING: {
if (state_g.crank_state == CYCLE_COMPRESSION) {
state_g.sync_state = SYNC_OK;
DEBUG_LOG("Sync state updated to OK\n\r");
}
break;
}
default:
break;
}
}
}

View File

@@ -4,22 +4,63 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "cmsis_os2.h"
#include "macros.h"
#include "main.h"
#include "ring_buffer.h"
#include "tasks.h"
#ifdef DEBUG
#include "SEGGER_RTT.h"
#endif
void crankHandler(void *argument) {
for (;;) {
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
#ifdef DEBUG
SEGGER_RTT_printf(0, "Crank pulse detected at: %lu\n\r",
ringBufferRead(&state_g.crank_RB, 0));
#endif /* ifdef DEBUG */
if (state_g.sync == SYNC_OK) {
// TODO complete algorithm for scheduling spark
DEBUG_LOG("Crank pulse detected at: %lu\n\r",
ringBufferRead(&state_g.crank_RB, 0));
// FILTER
// INCREMENT SWITCH
switch (state_g.crank_state) {
case CYCLE_COMPRESSION: {
if (state_g.cam_state != CAM_TRIGD) {
DEBUG_LOG("Cam miss detected, sync dynamite incoming\n\r");
state_g.cam_miss_ctr++;
if (state_g.cam_miss_ctr > MAX_CAM_MISS) {
state_g.sync_state = SYNC_NOT_OK;
state_g.crank_state = CYCLE_UNKNOWN;
break;
}
}
state_g.crank_state = CYCLE_COMBUSTION;
DEBUG_LOG("Crank state incremented\n\r");
state_g.cam_state = CAM_IDLE;
break;
}
case CYCLE_COMBUSTION:
case CYCLE_EXHAUST:
case CYCLE_INTAKE: {
if (state_g.cam_state == CAM_TRIGD) {
state_g.sync_state = SYNC_NOT_OK;
state_g.crank_state = CYCLE_UNKNOWN;
state_g.cam_state = CAM_IDLE;
break;
}
state_g.crank_state++;
DEBUG_LOG("Crank state incremented\n\r");
break;
}
case CYCLE_UNKNOWN: {
if (state_g.sync_state == SYNC_PENDING &&
state_g.cam_state == CAM_TRIGD) {
state_g.crank_state = CYCLE_COMBUSTION;
state_g.cam_state = CAM_IDLE;
}
break;
}
}
// SPARK SCHEDULE
if (state_g.crank_state == CYCLE_COMPRESSION &&
state_g.sync_state == SYNC_OK) {
DEBUG_LOG("Spark schedule reached, congrats\n\r");
// TODO: schedule spark
}
}
}