enabled dimming over ADC input
This commit is contained in:
@@ -3,9 +3,15 @@
|
||||
#include "adc.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
#define ADC_CONVERTED_DATA_BUFFER_SIZE ((uint32_t) 16) /* Size of array aADCxConvertedData[] */
|
||||
|
||||
#define TOTAL_ADC_CHANNELS 4
|
||||
#define ADC_HISTORY_SIZE 4
|
||||
#define ADC_BUFFER_SIZE (TOTAL_ADC_CHANNELS * ADC_HISTORY_SIZE)
|
||||
#define ADC_CONVERTED_DATA_BUFFER_SIZE ((uint32_t) ADC_BUFFER_SIZE) /* Size of array aADCxConvertedData[] */
|
||||
ALIGN_32BYTES (static uint16_t aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]);
|
||||
|
||||
#define ADC_CHANNEL_DIMMER 1
|
||||
#define ADC_CHANNEL_BUS 3
|
||||
|
||||
static bool adcIsInitialized = false;
|
||||
|
||||
@@ -53,4 +59,34 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
/* Invalidate Data Cache to get the updated content of the SRAM on the second half of the ADC converted data buffer: 32 bytes */
|
||||
SCB_InvalidateDCache_by_Addr((uint32_t *) &aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE/2], ADC_CONVERTED_DATA_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
float BSP_ADC_ReadValue( uint32_t channel, float factor) {
|
||||
if (!adcIsInitialized) {
|
||||
return 0;
|
||||
}
|
||||
// average the last 4 values of the dimmer ADC
|
||||
uint32_t sum = 0;
|
||||
for (int i = 0; i < ADC_HISTORY_SIZE; i++) {
|
||||
sum += aADCxConvertedData[channel + i * TOTAL_ADC_CHANNELS];
|
||||
}
|
||||
|
||||
|
||||
//convert raw ADC value to voltage
|
||||
float voltage = (float)sum / (float)ADC_HISTORY_SIZE / factor; // gemessan an sample pcb
|
||||
|
||||
return voltage;
|
||||
|
||||
}
|
||||
|
||||
float BSP_ADC_ReadDimmerValue() {
|
||||
return BSP_ADC_ReadValue(ADC_CHANNEL_DIMMER, 4319.619048);
|
||||
|
||||
}
|
||||
|
||||
|
||||
float BSP_ADC_ReadBusValue() {
|
||||
return BSP_ADC_ReadValue(ADC_CHANNEL_BUS, 4046.87186);
|
||||
}
|
||||
@@ -135,6 +135,9 @@ void BSP_POWER_FullPowerMode() {
|
||||
|
||||
BSP_GPIO_ClsOn();
|
||||
BSP_GPIO_PeriperalsOn();
|
||||
BSP_GPIO_RadioOn();
|
||||
BSP_ADC_Start();
|
||||
osDelay(10);
|
||||
StartPowerTasks();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "cmsis_os2.h"
|
||||
#include "LightState.h"
|
||||
#include "BSP_GPIO.h"
|
||||
#include "BSP_INA.h"
|
||||
#include "BSP_ADC.h"
|
||||
#include "LightTask.h"
|
||||
#include "ulog.h"
|
||||
|
||||
@@ -31,16 +31,63 @@ static bool isK15On()
|
||||
|
||||
|
||||
// Define the threshold voltage for engine running
|
||||
#define ENGINE_RUNNING_THRESHOLD 13500 // 13.5V = 13500mV
|
||||
#define ENGINE_RUNNING_THRESHOLD 13.3 // 13.5V
|
||||
|
||||
// Check if the engine is running by checking the voltage of the battery, if above the threshold, the engine is running
|
||||
static bool isEngineRunning()
|
||||
// Define thresholds with hysteresis
|
||||
#define ENGINE_RUNNING_THRESHOLD_HIGH (ENGINE_RUNNING_THRESHOLD + 0.125)
|
||||
#define ENGINE_RUNNING_THRESHOLD_LOW (ENGINE_RUNNING_THRESHOLD - 0.125)
|
||||
|
||||
// Global variable to store the current engine state
|
||||
static bool engineRunning = false;
|
||||
|
||||
// Function to initialize the engine state based on the initial voltage
|
||||
static void initializeEngineState()
|
||||
{
|
||||
return BSP_INA_Voltage() > ENGINE_RUNNING_THRESHOLD;
|
||||
float initialVoltage = BSP_ADC_ReadBusValue();
|
||||
if (initialVoltage > ENGINE_RUNNING_THRESHOLD)
|
||||
{
|
||||
engineRunning = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
engineRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the engine state is initialized once
|
||||
static bool isEngineInitialized = false;
|
||||
|
||||
static bool isEngineRunning()
|
||||
{
|
||||
if (!isEngineInitialized)
|
||||
{
|
||||
initializeEngineState();
|
||||
isEngineInitialized = true;
|
||||
}
|
||||
|
||||
float voltage = BSP_ADC_ReadBusValue();
|
||||
|
||||
if (engineRunning)
|
||||
{
|
||||
// If engine is currently running, use the lower threshold to turn it off
|
||||
if (voltage < ENGINE_RUNNING_THRESHOLD_LOW)
|
||||
{
|
||||
engineRunning = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If engine is currently off, use the higher threshold to turn it on
|
||||
if (voltage > ENGINE_RUNNING_THRESHOLD_HIGH)
|
||||
{
|
||||
engineRunning = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
return engineRunning;
|
||||
}
|
||||
// check if the headlight is on and the engine is running to switch the light state
|
||||
// we can check the headlight using the GPIO functions
|
||||
static bool isHeadlightOn()
|
||||
@@ -84,6 +131,6 @@ void LightStateTask(void *argument)
|
||||
}
|
||||
|
||||
// Delay the task for a certain amount of time (in milliseconds)
|
||||
osDelay(500);
|
||||
osDelay(50);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "BSP_EE24.h"
|
||||
#include "LightTask.h"
|
||||
#include "CanDataHandler.h"
|
||||
#include "BSP_ADC.h"
|
||||
#define DIMM_DEADZONE_VOLTAGE 0.7
|
||||
|
||||
// Memory for the task
|
||||
StaticTask_t LightTask_cb;
|
||||
@@ -30,10 +32,13 @@ static cls_light_ThemeSettings msg_cls_light_ThemeSettings;
|
||||
static cls_light_SaveThemeSettings msg_cls_light_SaveThemeSettings;
|
||||
static cls_light_RequestThemeSetting msg_cls_light_RequestThemeSetting;
|
||||
|
||||
static volatile struct LightSettings_s {
|
||||
typedef struct LightSettings_s {
|
||||
volatile uint8_t brightness;
|
||||
volatile uint8_t theme;
|
||||
} lightSettings;
|
||||
} LightSettings_t;
|
||||
|
||||
static volatile LightSettings_t lightSettings = {0};
|
||||
static volatile LightSettings_t lightSettings_dimmed = {0};
|
||||
|
||||
void LightTask_setBrightness(uint8_t brightness) {
|
||||
lightSettings.brightness = brightness;
|
||||
@@ -57,8 +62,6 @@ void LightTask_start() {
|
||||
|
||||
|
||||
void LightTask_func(void *argument) {
|
||||
osDelay(10);
|
||||
|
||||
BSP_EE24_PartRead(BSP_EE24_PART_GLOBAL_LIGHT, (uint8_t*) &lightSettings, sizeof(lightSettings));
|
||||
uint16_t msg_global_light = GENERATE_CLS_ADDRESS(CLS_CODE_MESSAGE, GLOBAL_CAST_CLS_ADDRESS, CLS_CH_MSG_LIGHT);
|
||||
CLS_BSP_TxHeaderType can_header = CREATE_BSP_CAN_HEADER(msg_global_light, CLS_BSP_DLC_BYTES_2);
|
||||
@@ -68,7 +71,32 @@ void LightTask_func(void *argument) {
|
||||
{
|
||||
tick += 50;
|
||||
osDelayUntil(tick);
|
||||
CLS_BSP_CAN_AddMessageToSend(&can_header,(uint8_t*)&lightSettings);
|
||||
|
||||
|
||||
// calculate the brightness to send
|
||||
uint8_t brightness = lightSettings.brightness;
|
||||
// Read ADC battery voltage and dimmer voltage
|
||||
float v_bus = BSP_ADC_ReadBusValue() - DIMM_DEADZONE_VOLTAGE;
|
||||
float v_dimm = BSP_ADC_ReadDimmerValue();
|
||||
|
||||
// calculate the dimmfactor based on the battery voltage and the dimmer voltage
|
||||
// the dimmfactor shoudl be 0 if the dimmer voltage is 4V.
|
||||
// the dimmfactor should be 1 if the dimmer voltage is the same as the battery voltage
|
||||
float dimmfactor;
|
||||
if (v_dimm >= v_bus) {
|
||||
dimmfactor = 1.0;
|
||||
} else if (v_dimm <= 4.0) {
|
||||
dimmfactor = 0.0;
|
||||
} else {
|
||||
dimmfactor = (v_dimm - 4.0) / (v_bus - 4.0);
|
||||
}
|
||||
|
||||
uint8_t adjustedBrightness = (uint8_t)(brightness * dimmfactor);
|
||||
lightSettings_dimmed.brightness = adjustedBrightness;
|
||||
lightSettings_dimmed.theme = lightSettings.theme;
|
||||
|
||||
|
||||
CLS_BSP_CAN_AddMessageToSend(&can_header,(uint8_t*)&lightSettings_dimmed);
|
||||
|
||||
if( settingChangeTime !=0 && tick > settingChangeTime + settingSaveTimeout) {
|
||||
BSP_EE24_PartWrite(BSP_EE24_PART_GLOBAL_LIGHT, (uint8_t*) &lightSettings, sizeof(lightSettings));
|
||||
|
||||
Reference in New Issue
Block a user