Broken spark implementation

This commit is contained in:
2026-06-03 17:45:48 +02:00
parent 1e8d3b766f
commit f8b73ea1bc
6 changed files with 90 additions and 29 deletions

View File

@@ -11,9 +11,13 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "global_state.h"
#include "macros.h"
#include "ring_buffer.h"
#include "stm32h7xx_hal_tim.h"
#include "tasks.h"
#include <assert.h>
#include <stdbool.h>
#ifdef DEBUG
#include "SEGGER_RTT.h"
#endif
@@ -101,6 +105,25 @@ extern void camHandler(void *argument);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
if (state_g.next_spark_state == SPARK_IDLE &&
state_g.current_spark_state == SPARK_CHARGING) {
DEBUG_LOG("Releasing spark");
} else if (state_g.next_spark_state == SPARK_CHARGING &&
state_g.current_spark_state == SPARK_IDLE) {
DEBUG_LOG("Charging spark");
osThreadFlagsSet(crankTaskHandle, 0x02);
}
if (state_g.next_spark_state != SPARK_NONE) {
state_g.current_spark_state = state_g.next_spark_state;
state_g.next_spark_state = SPARK_NONE;
}
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
switch (htim->Channel) {
@@ -164,6 +187,8 @@ int main(void)
state_g.crank_state = CYCLE_UNKNOWN;
state_g.cam_state = CAM_IDLE;
state_g.cam_miss_ctr = 0;
state_g.next_spark_state = SPARK_NONE;
state_g.current_spark_state = SPARK_IDLE;
/* USER CODE END Init */
/* Configure the system clock */
@@ -377,7 +402,7 @@ static void MX_TIM2_Init(void)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_FORCED_INACTIVE;
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "cmsis_os2.h"
#include "global_state.h"
#include "macros.h"
#include "main.h"
#include "global_state.h"
#include "ring_buffer.h"
#include "tasks.h"
@@ -18,10 +18,10 @@ void camHandler(void *argument) {
ringBufferRead(&state_g.cam_RB, 0));
// FILTERS
if (CAM_TRIGD) {
ringBufferRevert(&state_g.cam_RB,1);
return;
}
// if (CAM_TRIGD) {
// ringBufferRevert(&state_g.cam_RB,1);
// return;
// }
state_g.cam_state = CAM_TRIGD;
state_g.cam_miss_ctr = 0;
switch (state_g.sync_state) {

View File

@@ -24,11 +24,19 @@ void crankHandler(void *argument) {
DEBUG_LOG("Crank pulse detected at: %lu\n\r",
ringBufferRead(&state_g.crank_RB, 0));
// FILTER
float delta_percentage = ((float)(D1A - D1B)) / D1B;
if (delta_percentage > 0.4 || delta_percentage < -0.4) {
state_g.sync_state = SYNC_NOT_OK;
state_g.crank_state = CYCLE_UNKNOWN;
continue;
if (state_g.sync_state == SYNC_OK) {
uint32_t d1a = CRANK(0) - CRANK(1);
uint32_t d1b = CRANK(1) - CRANK(2);
int32_t delta_percentage =
((int64_t)d1a - (int64_t)d1b) * 100 / (int64_t)d1b;
DEBUG_LOG("%ld\n\r", delta_percentage);
if (delta_percentage > 40 || delta_percentage < -40) {
// state_g.sync_state = SYNC_NOT_OK;
// state_g.crank_state = CYCLE_UNKNOWN;
// ringBufferRevert(&state_g.crank_RB, 1);
state_g.crank_RB.w_head -= 1;
continue;
}
}
// INCREMENT SWITCH
switch (state_g.crank_state) {
@@ -71,19 +79,39 @@ void crankHandler(void *argument) {
}
// SPARK SCHEDULE
if (state_g.crank_state == CYCLE_COMPRESSION &&
state_g.sync_state == SYNC_OK) {
DEBUG_LOG("Spark schedule reached, congrats\n\r");
uint32_t d1a = CRANK(0) - CRANK(1);
// TODO: Map interpolation rather than SPARK_ADVANCE CONSTANT
uint32_t d_spark = d1a * (45 - SPARK_ADVANCE) / 180;
uint32_t t_spark = CRANK(0) + d_spark;
// TODO: schedule spark
// __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark);
// TIM2_CH3_SetOCMode(t_spark);
} else if (state_g.crank_state == CYCLE_EXHAUST) {
uint32_t t_injection = CRANK(0) + (45+INJECTION_PHASE) * (CRANK(4) - CRANK(0)) / 720;
//TODO Schedule injection
if (state_g.sync_state == SYNC_OK) {
switch (state_g.crank_state) {
case CYCLE_COMPRESSION: {
DEBUG_LOG("Spark schedule reached, congrats\n\r");
uint32_t d1a = CRANK(0) - CRANK(1);
// TODO: Map interpolation rather than SPARK_ADVANCE CONSTANT
uint32_t d_spark = d1a * (45 - SPARK_ADVANCE) / 180;
uint32_t t_spark = CRANK(0) + d_spark;
// TODO: schedule spark
if (state_g.current_spark_state != SPARK_CHARGING) {
uint32_t force_charge_time = __HAL_TIM_GET_COUNTER(&htim2) + 2000;
// TIM2_CH3_SetOCMode(TIM_OCMODE_ACTIVE);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, force_charge_time);
state_g.next_spark_state = SPARK_CHARGING;
osThreadFlagsWait(0x02, osFlagsWaitAny, osWaitForever);
}
DEBUG_LOG("Spark scheduled for: %lu , current time: %lu\n\r", t_spark,
__HAL_TIM_GET_COUNTER(&htim2));
// TIM2_CH3_SetOCMode(TIM_OCMODE_INACTIVE);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark);
state_g.next_spark_state = SPARK_IDLE;
break;
}
case CYCLE_EXHAUST: {
uint32_t t_injection =
CRANK(0) + (45 + INJECTION_PHASE) * (CRANK(4) - CRANK(0)) / 720;
// TODO: Schedule injection
break;
}
default:
break;
}
}
}
}