From 766d5e9ca8466a9bb8a43eeab7a8a40f00b0ecbd Mon Sep 17 00:00:00 2001 From: Oliver Walter Date: Tue, 28 May 2024 03:52:03 +0200 Subject: [PATCH] refactor Functions to group everything connected to the vehicle to a central location --- Application/CLS | 2 +- Application/CLS_BSP/CLS_BSP.c | 8 ++- Application/CLS_BSP/CMakeLists.txt | 2 +- Application/CMakeLists.txt | 3 +- Application/Tasks/CMakeLists.txt | 2 +- Application/Tasks/CanDataTask.c | 102 ++------------------------ Application/Tasks/LightState.c | 79 +-------------------- Application/Tasks/LightTask.c | 14 ++-- Application/Vehicle/CMakeLists.txt | 32 +++++++++ Application/Vehicle/Vehicle.c | 79 +++++++++++++++++++++ Application/Vehicle/Vehicle.h | 22 ++++++ Application/Vehicle/Vehicle_can.c | 110 +++++++++++++++++++++++++++++ CMakeLists.txt | 2 +- Core/Src/freertos.c | 3 +- 14 files changed, 274 insertions(+), 186 deletions(-) create mode 100644 Application/Vehicle/CMakeLists.txt create mode 100644 Application/Vehicle/Vehicle.c create mode 100644 Application/Vehicle/Vehicle.h create mode 100644 Application/Vehicle/Vehicle_can.c diff --git a/Application/CLS b/Application/CLS index 7080707..4620d0a 160000 --- a/Application/CLS +++ b/Application/CLS @@ -1 +1 @@ -Subproject commit 7080707f9d89b3c66f38c2a16dd9e029e340e92d +Subproject commit 4620d0a7ede7812eb1e1cbcf5ba6528e494ceab8 diff --git a/Application/CLS_BSP/CLS_BSP.c b/Application/CLS_BSP/CLS_BSP.c index b5eaeec..88fb443 100644 --- a/Application/CLS_BSP/CLS_BSP.c +++ b/Application/CLS_BSP/CLS_BSP.c @@ -1,4 +1,5 @@ #include "CLS_BSP.h" +#include "CLSAddress.h" #ifdef CLS_BSP_FDCAN @@ -67,4 +68,9 @@ CLS_Position_t CLS_BSP_GetPosition(void) { } void CLS_BSP_SetPosition(CLS_Position_t position) { -} \ No newline at end of file +} + + + +#include "BSP_GPIO.h" + diff --git a/Application/CLS_BSP/CMakeLists.txt b/Application/CLS_BSP/CMakeLists.txt index 72395eb..218ec74 100644 --- a/Application/CLS_BSP/CMakeLists.txt +++ b/Application/CLS_BSP/CMakeLists.txt @@ -12,4 +12,4 @@ target_sources(${PROJECT_NAME} ) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_link_libraries(${PROJECT_NAME} PRIVATE CLS) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} PRIVATE CLS BSP Vehicle) \ No newline at end of file diff --git a/Application/CMakeLists.txt b/Application/CMakeLists.txt index 7fd0300..e956b22 100644 --- a/Application/CMakeLists.txt +++ b/Application/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(Tasks) add_subdirectory(CLS) add_subdirectory(CLS_BSP) add_subdirectory(BSP) -add_subdirectory(ram_loader) \ No newline at end of file +add_subdirectory(ram_loader) +add_subdirectory(Vehicle) \ No newline at end of file diff --git a/Application/Tasks/CMakeLists.txt b/Application/Tasks/CMakeLists.txt index 0f1b3a0..d9ca746 100644 --- a/Application/Tasks/CMakeLists.txt +++ b/Application/Tasks/CMakeLists.txt @@ -22,4 +22,4 @@ target_sources(${PROJECT_NAME} target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(${PROJECT_NAME} PUBLIC PROTOS CLS) -target_link_libraries(${PROJECT_NAME} PRIVATE Revision CLS_BSP BSP ulog ram_loader) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} PRIVATE Revision CLS_BSP BSP ulog Vehicle ram_loader) \ No newline at end of file diff --git a/Application/Tasks/CanDataTask.c b/Application/Tasks/CanDataTask.c index e45f00f..e35aebe 100644 --- a/Application/Tasks/CanDataTask.c +++ b/Application/Tasks/CanDataTask.c @@ -11,6 +11,7 @@ #include "ulog.h" #include "BSP_POWER.h" #include "BSP_GPIO.h" +#include "Vehicle.h" // Define thread flags #define FLAG_FDCAN_RX_FIFO0 (1<<0) #define FLAG_FDCAN_RX_FIFO1 (1<<1) @@ -46,7 +47,7 @@ const osThreadAttr_t CarCanTask_attr = { .priority = osPriorityNormal, }; - +static uint64_t last_car_message_time = 0; uint32_t dlcDecode(uint32_t dlcCode) { switch(dlcCode) { @@ -130,11 +131,6 @@ void CanDataTask_func(void *argument) { } } -static uint64_t last_unlock_message_time = UINT64_MAX; -static uint64_t last_car_message_time = 0; -static uint8_t car_can_brightness = 255; -static float car_can_speed = 0; -static int car_can_direction = 0; // convert byte to 2 hex characters void byteToHex(uint8_t byte, char * hex) { @@ -152,20 +148,8 @@ void CarCanTask_func(void *argument) { } - - FDCAN_FilterTypeDef sFilterConfig; - sFilterConfig.IdType = FDCAN_STANDARD_ID; - sFilterConfig.FilterIndex = 0; - sFilterConfig.FilterType = CLS_BSP_CAN_FILTER_LIST; - sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1; - sFilterConfig.FilterID1 = 0x391; - sFilterConfig.FilterID2 = 0x395; - HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); - - sFilterConfig.FilterIndex = 1; - sFilterConfig.FilterID1 = 0x351; - sFilterConfig.FilterID2 = 0x635; - HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); + Vehicle_Setup_CAN(); + /* Start the FDCAN module */ if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK){ @@ -263,69 +247,7 @@ void CarCanTask_func(void *argument) { } else { char msg[17] = {0}; - - // do something with the can data - - - if(RxHeader.Identifier == 0x391) { - byteToHex(RxData[0], &msg[0]); - byteToHex(RxData[1], &msg[2]); - byteToHex(RxData[2], &msg[4]); - if (RxData[1] == 0x04) - { - // car was unlocked - last_unlock_message_time = osKernelGetTickCount(); - } - else if (RxData[1] ==0x80) - { - // car was locked - if (!BSP_GPIO_K15isSet()) { - NVIC_SystemReset(); - } - } - - } - - if (RxHeader.Identifier == 0x395) { - byteToHex(RxData[0], &msg[0]); - - // send the unlock message to the car - if ((RxData[0] & 0x0F) == 0x01) { - // car was unlocked - last_unlock_message_time = osKernelGetTickCount(); - ULOG_DEBUG("Unlock message received"); - - } else if ((RxData[0] & 0x0F) == 0x02) { - // car was locked - if (!BSP_GPIO_K15isSet()) { - NVIC_SystemReset(); - } - } - - } - - // speed signal - // AA BB XX YY 00 00 00 00 - // Speed (XX*(2^8)+(YY-1))/190 - // direction AA = 0x00 forward, 0x02 backward - if (RxHeader.Identifier == 0x351) { - - uint16_t speed = (RxData[2] << 8) + RxData[3]; - float speed_kmh = (speed - 1) / 190.0; - car_can_speed = speed_kmh; - car_can_direction = RxData[0]; - ULOG_DEBUG("Speed: %f, Direction: %d", car_can_speed, car_can_direction); - - - } - - // brightness knob in 0 - 100 - if (RxHeader.Identifier == 0x635) { - // scale the brightness to 0 - 255 only using integer math - car_can_brightness = ((uint32_t)RxData[0] * 255) / 100; - ULOG_DEBUG("Brightness: %d", car_can_brightness); - - } + Vehicle_Receive_CAN(RxHeader, RxData); //ULOG_DEBUG("Used Car MSG: %x, %d %s", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength), msg ); } @@ -339,9 +261,6 @@ void CarCanTask_func(void *argument) { * @return true if a car can message has been received * @return false if no car can message has been received */ -bool CanDataTask_gotCarCanMessage() { - return last_unlock_message_time != UINT64_MAX; -} bool CanDataTask_CarCanActive() { @@ -350,20 +269,9 @@ bool CanDataTask_CarCanActive() { } return osKernelGetTickCount() - last_car_message_time < 1000; - } -uint8_t CanDataTask_CarCanBrightness() { - return car_can_brightness; -} -float CanDataTask_CarCanSpeed() { - return car_can_speed; -} - -int CanDataTask_CarCanDirectionIsForward() { - return car_can_direction == 0; -} void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) { // Notify the thread diff --git a/Application/Tasks/LightState.c b/Application/Tasks/LightState.c index 50e3759..c782b8d 100644 --- a/Application/Tasks/LightState.c +++ b/Application/Tasks/LightState.c @@ -5,6 +5,7 @@ #include "LightTask.h" #include "ulog.h" #include "stdbool.h" +#include "Vehicle.h" // Create the task with a specific priority and stack size osThreadAttr_t task_attr = { @@ -24,89 +25,15 @@ void LightStateTask_start(void) -// Check if K15 is on for switching the light state -static bool isK15On() -{ - return BSP_GPIO_K15isSet(); -} - - -// Define the threshold voltage for engine running -#define ENGINE_RUNNING_THRESHOLD 13.3 // 13.5V - -// 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() -{ - 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() -{ - return BSP_GPIO_HeadLightIsSet(); -} - - - // Function to determine the next state of the light // return 1 as default state // return 2 if the engine is running // return 3 if the headlight and engine is running static int determineNextState() { - if (isHeadlightOn() && isEngineRunning()) { + if (Vehicle_isHeadlightOn() && Vehicle_isEngineRunning()) { return 2; - } else if (isEngineRunning()) { + } else if (Vehicle_isEngineRunning()) { return 1; } else { return 0; diff --git a/Application/Tasks/LightTask.c b/Application/Tasks/LightTask.c index 3403280..39ef6d3 100644 --- a/Application/Tasks/LightTask.c +++ b/Application/Tasks/LightTask.c @@ -11,6 +11,7 @@ #include "BSP_GPIO.h" #include "CanDataTask.h" +#include "Vehicle.h" #include "BSP_ADC.h" #define DIMM_DEADZONE_VOLTAGE 0.7 @@ -82,14 +83,14 @@ void LightTask_func(void *argument) { - float dimmfactor = 1.0; + uint8_t dimm = 255; + // currenlty not working // only dimm if the headlight is on - if (BSP_GPIO_HeadLightIsSet()) { + //if (Vehicle_isHeadlightOn()) { // new version over CAN - uint8_t precent = CanDataTask_CarCanBrightness(); - dimmfactor = (float)precent / 100.0; + //dimm = Vehicle_Brightness(); // old version over ADC // calculate the dimmfactor based on the battery voltage and the dimmer voltage @@ -104,9 +105,10 @@ void LightTask_func(void *argument) { //} else { // dimmfactor = (v_dimm - 4.0) / (v_bus - 4.0); //} - } - uint8_t adjustedBrightness = (uint8_t)(brightness * dimmfactor); + //} + + uint8_t adjustedBrightness = (brightness * dimm)/ 255; lightSettings_dimmed.brightness = adjustedBrightness; lightSettings_dimmed.theme = lightSettings.theme; diff --git a/Application/Vehicle/CMakeLists.txt b/Application/Vehicle/CMakeLists.txt new file mode 100644 index 0000000..290e989 --- /dev/null +++ b/Application/Vehicle/CMakeLists.txt @@ -0,0 +1,32 @@ +# Set the minimum required CMake version +cmake_minimum_required(VERSION 3.12) + +# Set the project name +project(Vehicle) + +# Add the source files for the library +set(SOURCES +Vehicle.c +Vehicle_can.c +) + +# Add the header files for the library +set(HEADERS + Vehicle.h +) + +# Create the library target +add_library(Vehicle ${SOURCES} ${HEADERS}) + +# Set the include directories for the library +target_include_directories(Vehicle PUBLIC ./) + +# Set any additional compiler flags or options +# target_compile_options(Vehicle PRIVATE ...) + +# Set any additional linker flags or options +# target_link_options(Vehicle PRIVATE ...) + +# Specify any dependencies for the library +target_link_libraries(Vehicle BSP CLS CLS_BSP) + diff --git a/Application/Vehicle/Vehicle.c b/Application/Vehicle/Vehicle.c new file mode 100644 index 0000000..7f82dab --- /dev/null +++ b/Application/Vehicle/Vehicle.c @@ -0,0 +1,79 @@ + +#include "Vehicle.h" +#include "BSP_GPIO.h" +#include "stdbool.h" +#include "BSP_ADC.h" + +// Define the threshold voltage for engine running +#define ENGINE_RUNNING_THRESHOLD 13.0 // 13.5V + +// Define thresholds with hysteresis +#define ENGINE_RUNNING_THRESHOLD_HIGH (ENGINE_RUNNING_THRESHOLD + 0.225) +#define ENGINE_RUNNING_THRESHOLD_LOW (ENGINE_RUNNING_THRESHOLD - 0.225) + +// Global variable to store the current engine state +static bool engineRunning = false; + + +void Vehicle_Init() { + engineRunning = false; +} + +bool Vehicle_isHeadlightOn() +{ + return BSP_GPIO_HeadLightIsSet(); +} + +bool Vehicle_isEngineRunning() { + + + 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; +} + +bool Vehicle_isK15On() { + return BSP_GPIO_K15isSet(); +} + + + + +#include "CLS.h" +#include "CLS_BSP.h" +#include "CLSAddress.h" + +static CLS_VehicleStatus_t status = {0}; +_Static_assert(sizeof(status) == 8, "CLS_HeatbeatData_t is not 8 bytes"); + +void CLS_VehicleHeatbeat(void *argument) { + CLS_BSP_TxHeaderType cls_vehicle_header = CREATE_BSP_CAN_HEADER(GENERATE_CLS_ADDRESS(CLS_CODE_STATUS,0,CLS_CH_STA_VEHICLE), CLS_BSP_DLC_BYTES_8); + + + status.k15 = Vehicle_isK15On(); + status.headlight = Vehicle_isHeadlightOn(); + status.engine = Vehicle_isEngineRunning(); + status.speed = (uint8_t)Vehicle_Speed(); + + CLS_BSP_CAN_AddMessageToSend(&cls_vehicle_header, (uint8_t*)&status); + +} \ No newline at end of file diff --git a/Application/Vehicle/Vehicle.h b/Application/Vehicle/Vehicle.h new file mode 100644 index 0000000..27d591a --- /dev/null +++ b/Application/Vehicle/Vehicle.h @@ -0,0 +1,22 @@ + +#include "fdcan.h" +#include "stdint.h" + +void Vehicle_Init(); + +bool Vehicle_isHeadlightOn(); + +bool Vehicle_isEngineRunning(); + +bool Vehicle_isK15On(); + + +void Vehicle_Setup_CAN(); + +void Vehicle_Receive_CAN( FDCAN_RxHeaderTypeDef header, uint8_t* data); + + +bool Vehicle_gotUnlockMessage(); +uint8_t Vehicle_Brightness(); +float Vehicle_Speed(); +int Vehicle_DirectionIsForward(); \ No newline at end of file diff --git a/Application/Vehicle/Vehicle_can.c b/Application/Vehicle/Vehicle_can.c new file mode 100644 index 0000000..ff603c9 --- /dev/null +++ b/Application/Vehicle/Vehicle_can.c @@ -0,0 +1,110 @@ +#include "Vehicle.h" +#include "fdcan.h" +#include "CLS.h" +#include "CLS_BSP.h" +#include "BSP_GPIO.h" +#include "cmsis_os2.h" + +static uint64_t last_unlock_message_time = UINT64_MAX; +static uint8_t car_can_brightness = 255; +static float car_can_speed = 0; +static int car_can_direction = 0; + + +void Vehicle_Setup_CAN() { + + + FDCAN_FilterTypeDef sFilterConfig; + sFilterConfig.IdType = FDCAN_STANDARD_ID; + sFilterConfig.FilterIndex = 0; + sFilterConfig.FilterType = CLS_BSP_CAN_FILTER_LIST; + sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1; + sFilterConfig.FilterID1 = 0x391; + sFilterConfig.FilterID2 = 0x395; + HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); + + sFilterConfig.FilterIndex = 1; + sFilterConfig.FilterID1 = 0x351; + sFilterConfig.FilterID2 = 0x635; + HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); + +} + + +static void rx_unlock(uint8_t * RxData ) { + if (RxData[1] == 0x04) + { + // car was unlocked + last_unlock_message_time = osKernelGetTickCount(); + } + else if (RxData[1] ==0x80) + { + // car was locked + if (!BSP_GPIO_K15isSet()) { + NVIC_SystemReset(); + } + } + +} + + +static void rx_speed(uint8_t * RxData) { + // speed signal + // AA BB XX YY 00 00 00 00 + // Speed (XX*(2^8)+(YY-1))/190 + // direction AA = 0x00 forward, 0x02 backward + uint16_t speed = (RxData[2] << 8) + RxData[3]; + float speed_kmh = (speed - 1) / 190.0; + car_can_speed = speed_kmh; + car_can_direction = RxData[0]; + //ULOG_DEBUG("Speed: %f, Direction: %d", car_can_speed, car_can_direction); + +} + +static void rx_brightness(uint8_t* RxData) { + // scale the brightness to 0 - 255 only using integer math + car_can_brightness = ((uint32_t)RxData[0] * 255) / 100; + //ULOG_DEBUG("Brightness: %d", car_can_brightness); +} + +void Vehicle_Receive_CAN( FDCAN_RxHeaderTypeDef RxHeader, uint8_t* RxData) { + + if(RxHeader.Identifier == 0x391) { + rx_unlock(RxData); + } + + if (RxHeader.Identifier == 0x395) { + rx_unlock(RxData); + } + + + if (RxHeader.Identifier == 0x351) { + rx_speed(RxData); + } + + // brightness knob in 0 - 100 + if (RxHeader.Identifier == 0x635) { + rx_brightness(RxData); + } + +} + + + +bool Vehicle_gotUnlockMessage() { + return last_unlock_message_time != UINT64_MAX; +} + + + +uint8_t Vehicle_Brightness() { + return car_can_brightness; +} + +float Vehicle_Speed() { + return car_can_speed; +} + +int Vehicle_DirectionIsForward() { + return car_can_direction == 0; +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b90e48..f124fa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,7 +140,7 @@ add_subdirectory("Application") add_executable(${CMAKE_PROJECT_NAME}) target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${sources_SRCS}) -target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC Tasks CLS CLS_BSP BSP EE24 INA219 ulog ram_loader) +target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC Tasks CLS CLS_BSP BSP EE24 INA219 ulog ram_loader Vehicle) #target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC PROTOS) target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC uart_driver) #target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC lwrb) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 608a6ce..3ae0f44 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -40,6 +40,7 @@ #include "BSP_GPIO.h" #include "BSP_ADC.h" #include "BSP_SDLogger.h" +#include "Vehicle.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -236,7 +237,7 @@ void WaitForStartConfirm_Task(void *argument) { while(1) { osDelayUntil(tick); tick += delayTime; - if(BSP_GPIO_K15isSet() || CanDataTask_gotCarCanMessage()) { + if(BSP_GPIO_K15isSet() || Vehicle_gotUnlockMessage()) { BSP_POWER_FullPowerMode(); ULOG_INFO("Power systems started"); osThreadExit();