refactor Functions to group everything connected to the vehicle to a central location

This commit is contained in:
2024-05-28 03:52:03 +02:00
parent 9c489c1a25
commit 766d5e9ca8
14 changed files with 274 additions and 186 deletions

View File

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

View File

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

View File

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

View File

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