Add full automotive RTOS project
Add kernel (Cortex-M0/M3/M4, Tricore, S32K, RISC-V ports), drivers, middleware (CAN stack, diagnostics, safety), applications, board support, build/test tooling, and documentation.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @file brake_control.h
|
||||
* @brief Brake control module interface
|
||||
*/
|
||||
|
||||
#ifndef BRAKE_CONTROL_H
|
||||
#define BRAKE_CONTROL_H
|
||||
|
||||
#include "kernel.h"
|
||||
|
||||
/* Brake Control Parameters */
|
||||
#define BRAKE_CONTROL_PERIOD_MS 5
|
||||
#define ABS_CONTROL_PERIOD_MS 1
|
||||
#define WHEEL_SPEED_SENSOR_COUNT 4
|
||||
#define MAX_BRAKE_PRESSURE 20000 /* kPa */
|
||||
#define ABS_SLIP_THRESHOLD 0.2f
|
||||
#define ABS_DECELERATION_THRESHOLD -10.0f /* m/s² */
|
||||
|
||||
/* Brake States */
|
||||
typedef enum {
|
||||
BRAKE_STATE_IDLE = 0,
|
||||
BRAKE_STATE_NORMAL_BRAKING = 1,
|
||||
BRAKE_STATE_ABS_ACTIVE = 2,
|
||||
BRAKE_STATE_EMERGENCY = 3,
|
||||
BRAKE_STATE_FAULT = 4
|
||||
} BrakeState_t;
|
||||
|
||||
/* Wheel Speed Data */
|
||||
typedef struct {
|
||||
float wheel_speed[WHEEL_SPEED_SENSOR_COUNT]; /* km/h */
|
||||
float wheel_acceleration[WHEEL_SPEED_SENSOR_COUNT]; /* m/s² */
|
||||
uint16_t wheel_sensor_raw[WHEEL_SPEED_SENSOR_COUNT];
|
||||
bool sensor_fault[WHEEL_SPEED_SENSOR_COUNT];
|
||||
} WheelSpeedData_t;
|
||||
|
||||
/* Brake System Data */
|
||||
typedef struct {
|
||||
uint16_t brake_pedal_position;
|
||||
uint16_t brake_pressure;
|
||||
float vehicle_speed;
|
||||
float vehicle_deceleration;
|
||||
bool abs_active;
|
||||
bool brake_light;
|
||||
uint8_t abs_fault_code;
|
||||
} BrakeSystemData_t;
|
||||
|
||||
/* Brake Control Functions */
|
||||
KernelStatus_t brake_control_init(void);
|
||||
KernelStatus_t brake_control_start(void);
|
||||
KernelStatus_t brake_control_stop(void);
|
||||
KernelStatus_t brake_control_get_data(BrakeSystemData_t* data);
|
||||
KernelStatus_t brake_control_get_wheel_data(WheelSpeedData_t* data);
|
||||
BrakeState_t brake_control_get_state(void);
|
||||
void brake_control_task(void* parameters);
|
||||
void abs_control_task(void* parameters);
|
||||
|
||||
#endif /* BRAKE_CONTROL_H */
|
||||
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* @file abs_control.c
|
||||
* @brief Anti-lock Braking System control
|
||||
*/
|
||||
|
||||
#include "brake_control.h"
|
||||
#include "gpio_driver.h"
|
||||
#include <math.h>
|
||||
|
||||
/* ABS Control State */
|
||||
typedef struct {
|
||||
bool initialized;
|
||||
bool abs_active;
|
||||
float reference_speed;
|
||||
float slip[WHEEL_SPEED_SENSOR_COUNT];
|
||||
uint8_t control_phase; /* 0=increase, 1=hold, 2=decrease */
|
||||
uint32_t phase_timer;
|
||||
Mutex_t mutex;
|
||||
} AbsControlState_t;
|
||||
|
||||
static AbsControlState_t abs_control;
|
||||
|
||||
/* Initialize ABS */
|
||||
KernelStatus_t abs_control_init(void) {
|
||||
if (abs_control.initialized) {
|
||||
return KERNEL_ERROR;
|
||||
}
|
||||
|
||||
abs_control.abs_active = false;
|
||||
abs_control.reference_speed = 0;
|
||||
abs_control.control_phase = 0;
|
||||
abs_control.phase_timer = 0;
|
||||
|
||||
for (int i = 0; i < WHEEL_SPEED_SENSOR_COUNT; i++) {
|
||||
abs_control.slip[i] = 0;
|
||||
}
|
||||
|
||||
mutex_create(&abs_control.mutex, false);
|
||||
|
||||
abs_control.initialized = true;
|
||||
return KERNEL_OK;
|
||||
}
|
||||
|
||||
/* ABS Control Task */
|
||||
void abs_control_task(void* parameters) {
|
||||
(void)parameters;
|
||||
|
||||
while (1) {
|
||||
/* Wait for next ABS control period */
|
||||
kernel_delay(ABS_CONTROL_PERIOD_MS);
|
||||
|
||||
WheelSpeedData_t wheels;
|
||||
brake_control_get_wheel_data(&wheels);
|
||||
|
||||
/* Check if ABS should be active */
|
||||
if (brake_control_get_state() == BRAKE_STATE_NORMAL_BRAKING &&
|
||||
check_abs_activation(&wheels)) {
|
||||
|
||||
abs_control.abs_active = true;
|
||||
control_abs(&wheels);
|
||||
} else {
|
||||
abs_control.abs_active = false;
|
||||
abs_control.control_phase = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check ABS Activation Conditions */
|
||||
static bool check_abs_activation(const WheelSpeedData_t* wheels) {
|
||||
/* Calculate reference speed (maximum wheel speed) */
|
||||
float max_speed = 0;
|
||||
for (int i = 0; i < WHEEL_SPEED_SENSOR_COUNT; i++) {
|
||||
if (wheels->wheel_speed[i] > max_speed) {
|
||||
max_speed = wheels->wheel_speed[i];
|
||||
}
|
||||
}
|
||||
|
||||
abs_control.reference_speed = max_speed;
|
||||
|
||||
/* Calculate slip for each wheel */
|
||||
for (int i = 0; i < WHEEL_SPEED_SENSOR_COUNT; i++) {
|
||||
if (max_speed > 5.0f) { /* Only calculate above 5 km/h */
|
||||
abs_control.slip[i] = (max_speed - wheels->wheel_speed[i]) / max_speed;
|
||||
} else {
|
||||
abs_control.slip[i] = 0;
|
||||
}
|
||||
|
||||
/* Check if slip exceeds threshold */
|
||||
if (abs_control.slip[i] > ABS_SLIP_THRESHOLD) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Check for excessive deceleration */
|
||||
if (wheels->wheel_acceleration[i] < ABS_DECELERATION_THRESHOLD) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ABS Control Algorithm */
|
||||
static void control_abs(const WheelSpeedData_t* wheels) {
|
||||
mutex_lock(&abs_control.mutex, 100);
|
||||
|
||||
/* Simple ABS control algorithm */
|
||||
switch (abs_control.control_phase) {
|
||||
case 0: /* Increase pressure */
|
||||
if (abs_control.slip[0] > ABS_SLIP_THRESHOLD) {
|
||||
abs_control.control_phase = 2; /* Switch to decrease */
|
||||
abs_control.phase_timer = kernel_get_tick_count();
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: /* Hold pressure */
|
||||
if ((kernel_get_tick_count() - abs_control.phase_timer) > 10) {
|
||||
abs_control.control_phase = 0; /* Switch to increase */
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: /* Decrease pressure */
|
||||
if (abs_control.slip[0] < ABS_SLIP_THRESHOLD * 0.5f) {
|
||||
abs_control.control_phase = 1; /* Switch to hold */
|
||||
abs_control.phase_timer = kernel_get_tick_count();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
mutex_unlock(&abs_control.mutex);
|
||||
|
||||
/* Control brake pressure valves */
|
||||
control_pressure_valves();
|
||||
}
|
||||
|
||||
/* Control Pressure Valves */
|
||||
static void control_pressure_valves(void) {
|
||||
switch (abs_control.control_phase) {
|
||||
case 0: /* Increase pressure */
|
||||
gpio_write(0, 0, false); /* Inlet valve open */
|
||||
gpio_write(0, 1, false); /* Outlet valve closed */
|
||||
break;
|
||||
|
||||
case 1: /* Hold pressure */
|
||||
gpio_write(0, 0, true); /* Inlet valve closed */
|
||||
gpio_write(0, 1, false); /* Outlet valve closed */
|
||||
break;
|
||||
|
||||
case 2: /* Decrease pressure */
|
||||
gpio_write(0, 0, true); /* Inlet valve closed */
|
||||
gpio_write(0, 1, true); /* Outlet valve open */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get ABS Status */
|
||||
bool abs_control_is_active(void) {
|
||||
return abs_control.abs_active;
|
||||
}
|
||||
|
||||
/* Get Slip Values */
|
||||
KernelStatus_t abs_control_get_slip(float* slip, uint8_t* count) {
|
||||
if (slip == NULL || count == NULL) {
|
||||
return KERNEL_ERROR;
|
||||
}
|
||||
|
||||
mutex_lock(&abs_control.mutex, 100);
|
||||
for (int i = 0; i < WHEEL_SPEED_SENSOR_COUNT; i++) {
|
||||
slip[i] = abs_control.slip[i];
|
||||
}
|
||||
*count = WHEEL_SPEED_SENSOR_COUNT;
|
||||
mutex_unlock(&abs_control.mutex);
|
||||
|
||||
return KERNEL_OK;
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* @file brake_task.c
|
||||
* @brief Main brake control task
|
||||
*/
|
||||
|
||||
#include "brake_control.h"
|
||||
#include "adc_driver.h"
|
||||
#include "gpio_driver.h"
|
||||
#include "pwm_driver.h"
|
||||
#include "dtc_manager.h"
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
/* Brake Control State */
|
||||
typedef struct {
|
||||
BrakeState_t state;
|
||||
BrakeSystemData_t data;
|
||||
WheelSpeedData_t wheels;
|
||||
uint16_t target_pressure;
|
||||
uint16_t actual_pressure;
|
||||
Mutex_t data_mutex;
|
||||
bool initialized;
|
||||
} BrakeControlState_t;
|
||||
|
||||
static BrakeControlState_t brake_control;
|
||||
|
||||
/* Initialize Brake Control */
|
||||
KernelStatus_t brake_control_init(void) {
|
||||
if (brake_control.initialized) {
|
||||
return KERNEL_ERROR;
|
||||
}
|
||||
|
||||
memset(&brake_control, 0, sizeof(BrakeControlState_t));
|
||||
brake_control.state = BRAKE_STATE_IDLE;
|
||||
|
||||
mutex_create(&brake_control.data_mutex, false);
|
||||
|
||||
/* Initialize ADC for brake sensors */
|
||||
adc_init(1, &(AdcConfig_t){
|
||||
.resolution = ADC_RESOLUTION_12BIT,
|
||||
.mode = ADC_MODE_CONTINUOUS,
|
||||
.channel_count = 2,
|
||||
.channels = {
|
||||
{.channel = 0}, /* Brake pedal position */
|
||||
{.channel = 1} /* Brake pressure */
|
||||
}
|
||||
});
|
||||
|
||||
/* Initialize PWM for brake pressure control */
|
||||
pwm_init(1, &(PwmConfig_t){
|
||||
.frequency_hz = 2000,
|
||||
.channel_count = 1,
|
||||
.channels = {
|
||||
{.channel = 0, .duty_cycle = 0} /* Brake pressure valve */
|
||||
}
|
||||
});
|
||||
|
||||
brake_control.initialized = true;
|
||||
return KERNEL_OK;
|
||||
}
|
||||
|
||||
/* Brake Control Task */
|
||||
void brake_control_task(void* parameters) {
|
||||
(void)parameters;
|
||||
|
||||
while (1) {
|
||||
/* Wait for next brake control period */
|
||||
kernel_delay(BRAKE_CONTROL_PERIOD_MS);
|
||||
|
||||
/* Read brake sensors */
|
||||
read_brake_sensors();
|
||||
|
||||
/* Process brake logic */
|
||||
process_brake_logic();
|
||||
|
||||
/* Check for faults */
|
||||
check_brake_faults();
|
||||
}
|
||||
}
|
||||
|
||||
/* Read Brake Sensors */
|
||||
static void read_brake_sensors(void) {
|
||||
uint16_t brake_pedal = 0;
|
||||
uint16_t brake_pressure = 0;
|
||||
|
||||
adc_read_channel(1, 0, &brake_pedal, 10);
|
||||
adc_read_channel(1, 1, &brake_pressure, 10);
|
||||
|
||||
mutex_lock(&brake_control.data_mutex, 100);
|
||||
brake_control.data.brake_pedal_position = brake_pedal;
|
||||
brake_control.data.brake_pressure = brake_pressure;
|
||||
mutex_unlock(&brake_control.data_mutex);
|
||||
}
|
||||
|
||||
/* Process Brake Logic */
|
||||
static void process_brake_logic(void) {
|
||||
mutex_lock(&brake_control.data_mutex, 100);
|
||||
|
||||
/* Check brake pedal position */
|
||||
if (brake_control.data.brake_pedal_position > 100) {
|
||||
brake_control.state = BRAKE_STATE_NORMAL_BRAKING;
|
||||
brake_control.data.brake_light = true;
|
||||
|
||||
/* Calculate target pressure based on pedal position */
|
||||
brake_control.target_pressure =
|
||||
(brake_control.data.brake_pedal_position - 100) * 200;
|
||||
|
||||
/* Limit pressure */
|
||||
if (brake_control.target_pressure > MAX_BRAKE_PRESSURE) {
|
||||
brake_control.target_pressure = MAX_BRAKE_PRESSURE;
|
||||
}
|
||||
} else {
|
||||
brake_control.state = BRAKE_STATE_IDLE;
|
||||
brake_control.data.brake_light = false;
|
||||
brake_control.target_pressure = 0;
|
||||
}
|
||||
|
||||
/* Check for emergency braking */
|
||||
if (brake_control.data.brake_pedal_position > 900 &&
|
||||
brake_control.data.vehicle_deceleration < -8.0f) {
|
||||
brake_control.state = BRAKE_STATE_EMERGENCY;
|
||||
|
||||
/* Maximum braking force */
|
||||
brake_control.target_pressure = MAX_BRAKE_PRESSURE;
|
||||
}
|
||||
|
||||
mutex_unlock(&brake_control.data_mutex);
|
||||
|
||||
/* Update brake pressure control */
|
||||
update_brake_pressure();
|
||||
}
|
||||
|
||||
/* Update Brake Pressure */
|
||||
static void update_brake_pressure(void) {
|
||||
/* Simple PID pressure control */
|
||||
int32_t pressure_error = brake_control.target_pressure -
|
||||
brake_control.actual_pressure;
|
||||
|
||||
/* Calculate valve duty cycle */
|
||||
uint16_t valve_duty = 0;
|
||||
|
||||
if (pressure_error > 100) {
|
||||
valve_duty = 10000; /* Full open */
|
||||
} else if (pressure_error > 0) {
|
||||
valve_duty = (uint16_t)((pressure_error * 10000) / 100);
|
||||
} else {
|
||||
valve_duty = 0; /* Closed */
|
||||
}
|
||||
|
||||
/* Update PWM */
|
||||
pwm_set_duty_cycle(1, 0, valve_duty);
|
||||
|
||||
/* Update actual pressure */
|
||||
brake_control.actual_pressure += pressure_error / 10;
|
||||
}
|
||||
|
||||
/* Check Brake Faults */
|
||||
static void check_brake_faults(void) {
|
||||
/* Check wheel speed sensors */
|
||||
for (int i = 0; i < WHEEL_SPEED_SENSOR_COUNT; i++) {
|
||||
if (brake_control.wheels.sensor_fault[i]) {
|
||||
brake_control.state = BRAKE_STATE_FAULT;
|
||||
brake_control.data.abs_fault_code = i + 1;
|
||||
|
||||
/* Add DTC */
|
||||
DtcCode_t dtc_code = {
|
||||
.high_byte = 0x01, /* Chassis */
|
||||
.middle_byte = 0x00,
|
||||
.low_byte = i + 1
|
||||
};
|
||||
dtc_manager_add_dtc(&dtc_code, 4); /* High severity */
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user