added more CarCan Messages

This commit is contained in:
2024-05-23 14:00:56 +02:00
parent c7d6ba78d5
commit 13c6a71af6
3 changed files with 67 additions and 22 deletions

View File

@@ -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);
}
}

View File

@@ -7,4 +7,8 @@ void CanDataTask_start();
bool CanDataTask_gotCarCanMessage();
bool CanDataTask_CarCanActive();
bool CanDataTask_CarCanActive();
uint8_t CanDataTask_CarCanBrightness();
float CanDataTask_CarCanSpeed();
int CanDataTask_CarCanDirectionIsForward();

View File

@@ -8,6 +8,7 @@
#include "BSP_EE24.h"
#include "LightTask.h"
#include "CanDataHandler.h"
#include "BSP_ADC.h"
#define DIMM_DEADZONE_VOLTAGE 0.7