From 13c6a71af6269f8b39da6ad749400c4df8c52d1f Mon Sep 17 00:00:00 2001 From: Oliver Walter Date: Thu, 23 May 2024 14:00:56 +0200 Subject: [PATCH] added more CarCan Messages --- Application/Tasks/CanDataTask.c | 82 ++++++++++++++++++++++++--------- Application/Tasks/CanDataTask.h | 6 ++- Application/Tasks/LightTask.c | 1 + 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/Application/Tasks/CanDataTask.c b/Application/Tasks/CanDataTask.c index 9f3c259..e45f00f 100644 --- a/Application/Tasks/CanDataTask.c +++ b/Application/Tasks/CanDataTask.c @@ -132,6 +132,9 @@ 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) { @@ -148,6 +151,8 @@ void CarCanTask_func(void *argument) { Error_Handler(); } + + FDCAN_FilterTypeDef sFilterConfig; sFilterConfig.IdType = FDCAN_STANDARD_ID; sFilterConfig.FilterIndex = 0; @@ -157,6 +162,11 @@ void CarCanTask_func(void *argument) { sFilterConfig.FilterID2 = 0x395; HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); + sFilterConfig.FilterIndex = 1; + sFilterConfig.FilterID1 = 0x351; + sFilterConfig.FilterID2 = 0x635; + HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig); + /* Start the FDCAN module */ if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK){ Error_Handler(); @@ -243,7 +253,7 @@ void CarCanTask_func(void *argument) { break; } - ULOG_DEBUG("Car MSG: %x, %d %s", RxHeader.Identifier,CLS_BSP_DLC_ToBytes(RxHeader.DataLength) , msg); + //ULOG_DEBUG("Car MSG: %x, %d %s", RxHeader.Identifier,CLS_BSP_DLC_ToBytes(RxHeader.DataLength) , msg); } } @@ -258,54 +268,66 @@ void CarCanTask_func(void *argument) { if(RxHeader.Identifier == 0x391) { - if (RxData[1] == 4) + 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(); - byteToHex(RxData[0], &msg[0]); - byteToHex(RxData[1], &msg[2]); - byteToHex(RxData[2], &msg[4]); - } - - - if (RxData[1] ==0x80) + } + else if (RxData[1] ==0x80) { // car was locked if (!BSP_GPIO_K15isSet()) { NVIC_SystemReset(); } - - byteToHex(RxData[0], &msg[0]); - byteToHex(RxData[1], &msg[2]); - byteToHex(RxData[2], &msg[4]); } } if (RxHeader.Identifier == 0x395) { + byteToHex(RxData[0], &msg[0]); + // send the unlock message to the car if ((RxData[0] & 0x0F) == 0x01) { // car was unlocked - byteToHex(RxData[0], &msg[0]); last_unlock_message_time = osKernelGetTickCount(); + ULOG_DEBUG("Unlock message received"); - } - - if ((RxData[0] & 0x0F) == 0x02) { + } else if ((RxData[0] & 0x0F) == 0x02) { // car was locked if (!BSP_GPIO_K15isSet()) { NVIC_SystemReset(); } - - byteToHex(RxData[0], &msg[0]); } } + // 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) { - ULOG_DEBUG("Car LOCK MSG: %x, %d %s", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength), msg ); + 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); + + } + + //ULOG_DEBUG("Used Car MSG: %x, %d %s", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength), msg ); } } } @@ -331,6 +353,18 @@ bool CanDataTask_CarCanActive() { } +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 if(hfdcan == &hfdcan1) { @@ -339,8 +373,14 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) if(hfdcan == &hfdcan2) { last_car_message_time = osKernelGetTickCount(); - osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO0); + while (HAL_FDCAN_GetRxFifoFillLevel(&hfdcan2, FDCAN_RX_FIFO0) > 0 ) { + FDCAN_RxHeaderTypeDef RxHeader; + uint8_t RxData[8]; + HAL_FDCAN_GetRxMessage(&hfdcan2, FDCAN_RX_FIFO0, &RxHeader, RxData); + //ignore the message for now + } + //osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO0); } } diff --git a/Application/Tasks/CanDataTask.h b/Application/Tasks/CanDataTask.h index cb39427..9d674de 100644 --- a/Application/Tasks/CanDataTask.h +++ b/Application/Tasks/CanDataTask.h @@ -7,4 +7,8 @@ void CanDataTask_start(); bool CanDataTask_gotCarCanMessage(); -bool CanDataTask_CarCanActive(); \ No newline at end of file +bool CanDataTask_CarCanActive(); + +uint8_t CanDataTask_CarCanBrightness(); +float CanDataTask_CarCanSpeed(); +int CanDataTask_CarCanDirectionIsForward(); \ No newline at end of file diff --git a/Application/Tasks/LightTask.c b/Application/Tasks/LightTask.c index 1622d85..c29cffd 100644 --- a/Application/Tasks/LightTask.c +++ b/Application/Tasks/LightTask.c @@ -8,6 +8,7 @@ #include "BSP_EE24.h" #include "LightTask.h" #include "CanDataHandler.h" + #include "BSP_ADC.h" #define DIMM_DEADZONE_VOLTAGE 0.7