248 lines
7.5 KiB
C
248 lines
7.5 KiB
C
/*
|
|
* AIR_State_Maschine.c
|
|
*
|
|
* Created on: Jun 15, 2022
|
|
* Author: max
|
|
*/
|
|
|
|
#include "AIR_State_Maschine.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "stm32g4xx_hal.h"
|
|
#include "stm32g4xx_hal_gpio.h"
|
|
|
|
DMA_HandleTypeDef* air_current_dma = {0};
|
|
DMA_HandleTypeDef* sdc_voltage_dma = {0};
|
|
|
|
uint8_t air_adc_complete = 0;
|
|
uint8_t sdc_adc_complete = 0;
|
|
|
|
AIRStateHandler init_AIR_State_Maschine() {
|
|
AIRStateHandler airstate = {0};
|
|
|
|
airstate.targetTSState = TS_INACTIVE;
|
|
airstate.currentTSState = TS_INACTIVE;
|
|
airstate.BatteryVoltageBatterySide = 0;
|
|
airstate.BatteryVoltageVehicleSide = 0;
|
|
|
|
return airstate;
|
|
}
|
|
|
|
void Update_AIR_Info(AIRStateHandler* airstate) {
|
|
uint16_t relay_adc_buffer[4] = {0};
|
|
uint16_t sdc_adc_buffer[1] = {0};
|
|
|
|
/*//HAL_ADC_Start_DMA(air_current_adc, (uint32_t*)relay_adc_buffer, 4);
|
|
//HAL_ADC_Start_DMA(sdc_voltage_adc, (uint32_t*)sdc_adc_buffer, 1);
|
|
HAL_ADC_Start(sdc_voltage_adc);
|
|
HAL_StatusTypeDef status = HAL_ADC_PollForConversion(sdc_voltage_adc, 10);
|
|
uint32_t adcval1 = HAL_ADC_GetValue(sdc_voltage_adc);
|
|
HAL_ADC_Stop(sdc_voltage_adc);
|
|
|
|
HAL_ADC_Start(air_current_adc);
|
|
status = HAL_ADC_PollForConversion(air_current_adc, 10);
|
|
uint32_t adcval2 = HAL_ADC_GetValue(air_current_adc);
|
|
HAL_ADC_Start(air_current_adc);
|
|
status = HAL_ADC_PollForConversion(air_current_adc, 10);
|
|
uint32_t adcval3 = HAL_ADC_GetValue(air_current_adc);
|
|
HAL_ADC_Start(air_current_adc);
|
|
status = HAL_ADC_PollForConversion(air_current_adc, 10);
|
|
uint32_t adcval4 = HAL_ADC_GetValue(air_current_adc);
|
|
HAL_ADC_Start(air_current_adc);
|
|
status = HAL_ADC_PollForConversion(air_current_adc, 10);
|
|
uint32_t adcval5 = HAL_ADC_GetValue(air_current_adc);
|
|
HAL_ADC_Stop(air_current_adc);*/
|
|
}
|
|
|
|
uint8_t Update_AIR_State(AIRStateHandler* airstate) {
|
|
Update_AIR_Info(airstate);
|
|
|
|
//--------------------------------------------------State Transition
|
|
// Rules----------------------------------------------------------
|
|
|
|
/*if(airstate->currentTSState == airstate->targetTSState) //Target TS
|
|
State is Equal to actual TS State
|
|
{
|
|
return airstate->currentTSState;
|
|
}*/
|
|
|
|
if (airstate->currentTSState == TS_ERROR) // No Escape from TS Error State
|
|
{
|
|
// Don't change anything, but prevent any other if from being entered
|
|
}
|
|
|
|
else if (airstate->targetTSState ==
|
|
TS_ERROR) // Error State is Entered if Target State is Error State
|
|
{
|
|
airstate->currentTSState = TS_ERROR;
|
|
}
|
|
|
|
else if ((airstate->currentTSState == TS_INACTIVE) &&
|
|
(airstate->targetTSState ==
|
|
TS_ACTIVE)) // Transition from Inactive to Active via Precharge
|
|
{
|
|
airstate->currentTSState = TS_PRECHARGE;
|
|
airstate->precharge95ReachedTimestamp = 0;
|
|
}
|
|
|
|
else if ((airstate->currentTSState == TS_INACTIVE) &&
|
|
(airstate->targetTSState == TS_CHARGING)) {
|
|
airstate->currentTSState = TS_CHARGING_CHECK;
|
|
airstate->chargingCheckTimestamp = HAL_GetTick();
|
|
}
|
|
|
|
// TODO: Is it correct that we also go from precharge to discharge?
|
|
else if ((airstate->currentTSState == TS_ACTIVE ||
|
|
airstate->currentTSState == TS_PRECHARGE ||
|
|
airstate->currentTSState == TS_CHARGING_CHECK ||
|
|
airstate->currentTSState == TS_CHARGING) &&
|
|
(airstate->targetTSState ==
|
|
TS_INACTIVE)) // Transition from Active to Inactive via Discharge
|
|
{
|
|
airstate->currentTSState = TS_DISCHARGE;
|
|
}
|
|
|
|
else if ((airstate->targetTSState == TS_CHARGING) &&
|
|
(airstate->currentTSState == TS_CHARGING_CHECK)) {
|
|
if (airstate->BatteryVoltageVehicleSide >
|
|
airstate->BatteryVoltageBatterySide) {
|
|
airstate->currentTSState = TS_CHARGING;
|
|
} else if (HAL_GetTick() > airstate->chargingCheckTimestamp + 2000) {
|
|
airstate->currentTSState = TS_ERROR;
|
|
}
|
|
}
|
|
|
|
else if (airstate->currentTSState ==
|
|
TS_PRECHARGE) // Change from Precharge to Active at 95% TS Voltage at
|
|
// Vehicle Side
|
|
{
|
|
if ((airstate->BatteryVoltageVehicleSide >
|
|
LOWER_VEHICLE_SIDE_VOLTAGE_LIMIT)) {
|
|
if (airstate->BatteryVoltageVehicleSide >
|
|
(airstate->BatteryVoltageBatterySide * 0.95)) {
|
|
if (airstate->precharge95ReachedTimestamp == 0) {
|
|
airstate->precharge95ReachedTimestamp = HAL_GetTick();
|
|
} else if (HAL_GetTick() - airstate->precharge95ReachedTimestamp >=
|
|
PRECHARGE_95_DURATION) {
|
|
airstate->currentTSState = TS_ACTIVE;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
else if (airstate->currentTSState ==
|
|
TS_DISCHARGE) // Change from Discharge to Inactive at 95% TS
|
|
// Voltage at Vehicle Side
|
|
{
|
|
airstate->currentTSState = TS_INACTIVE;
|
|
}
|
|
|
|
//_-----------------------------------------------AIR
|
|
// Positions--------------------------------------------------------
|
|
|
|
if (airstate->currentTSState == TS_PRECHARGE) {
|
|
AIR_Precharge_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_DISCHARGE) {
|
|
AIR_Discharge_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_CHARGING_CHECK) {
|
|
AIR_Precharge_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_CHARGING) {
|
|
AIR_Active_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_ACTIVE) {
|
|
AIR_Active_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_INACTIVE) {
|
|
AIR_Inactive_Position();
|
|
}
|
|
|
|
if (airstate->currentTSState == TS_ERROR) {
|
|
AIR_Error_Position();
|
|
}
|
|
|
|
return airstate->currentTSState;
|
|
}
|
|
|
|
void Activate_TS(AIRStateHandler* airstate) {
|
|
airstate->targetTSState = TS_ACTIVE;
|
|
}
|
|
|
|
void Deactivate_TS(AIRStateHandler* airstate) {
|
|
airstate->targetTSState = TS_INACTIVE;
|
|
}
|
|
|
|
void AIR_Precharge_Position() {
|
|
Set_Relay_Position(RELAY_PRECHARGE, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_NEG, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_POS, GPIO_PIN_RESET);
|
|
}
|
|
|
|
void AIR_Inactive_Position() {
|
|
Set_Relay_Position(RELAY_PRECHARGE, GPIO_PIN_RESET);
|
|
Set_Relay_Position(RELAY_AIR_NEG, GPIO_PIN_RESET);
|
|
Set_Relay_Position(RELAY_AIR_POS, GPIO_PIN_RESET);
|
|
}
|
|
|
|
void AIR_Discharge_Position() {
|
|
Set_Relay_Position(RELAY_PRECHARGE, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_NEG, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_POS, GPIO_PIN_RESET);
|
|
}
|
|
|
|
void AIR_Active_Position() // TODO Deactivate Precharge after a while to
|
|
// decrease current Consumption
|
|
{
|
|
Set_Relay_Position(RELAY_PRECHARGE, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_NEG, GPIO_PIN_SET);
|
|
Set_Relay_Position(RELAY_AIR_POS, GPIO_PIN_SET);
|
|
}
|
|
|
|
void AIR_Error_Position() {
|
|
Set_Relay_Position(RELAY_PRECHARGE, GPIO_PIN_RESET);
|
|
Set_Relay_Position(RELAY_AIR_NEG, GPIO_PIN_RESET);
|
|
Set_Relay_Position(RELAY_AIR_POS, GPIO_PIN_RESET);
|
|
}
|
|
|
|
void Set_Relay_Position(Relay relay, GPIO_PinState position) {
|
|
static GPIO_PinState neg = 0;
|
|
static GPIO_PinState pos = 0;
|
|
static GPIO_PinState precharge = 0;
|
|
// Add a small delay after closing relays in order to not draw too much
|
|
// current
|
|
switch (relay) {
|
|
case RELAY_AIR_NEG:
|
|
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
|
position);
|
|
if (position == GPIO_PIN_SET && neg == GPIO_PIN_RESET) {
|
|
HAL_Delay(10);
|
|
}
|
|
neg = position;
|
|
break;
|
|
case RELAY_AIR_POS:
|
|
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
|
position);
|
|
if (position == GPIO_PIN_SET && pos == GPIO_PIN_RESET) {
|
|
HAL_Delay(10);
|
|
}
|
|
pos = position;
|
|
break;
|
|
case RELAY_PRECHARGE:
|
|
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
|
position);
|
|
if (position == GPIO_PIN_SET && precharge == GPIO_PIN_RESET) {
|
|
HAL_Delay(10);
|
|
}
|
|
precharge = position;
|
|
break;
|
|
}
|
|
}
|