Initial commit
This commit is contained in:
231
Core/Src/AIR_State_Maschine.c
Normal file
231
Core/Src/AIR_State_Maschine.c
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* AIR_State_Maschine.c
|
||||
*
|
||||
* Created on: Jun 15, 2022
|
||||
* Author: max
|
||||
*/
|
||||
|
||||
#include "AIR_State_Maschine.h"
|
||||
|
||||
ADC_HandleTypeDef* air_current_adc = {0};
|
||||
ADC_HandleTypeDef* sdc_voltage_adc = {0};
|
||||
|
||||
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(ADC_HandleTypeDef* relay_adc,
|
||||
ADC_HandleTypeDef* sc_adc,
|
||||
DMA_HandleTypeDef* relay_dma,
|
||||
DMA_HandleTypeDef* sc_dma) {
|
||||
air_current_adc = relay_adc;
|
||||
sdc_voltage_adc = sc_adc;
|
||||
|
||||
air_current_dma = relay_dma;
|
||||
sdc_voltage_dma = sc_dma;
|
||||
|
||||
AIRStateHandler airstate = {0};
|
||||
|
||||
airstate.targetTSState = TS_INACTIVE;
|
||||
airstate.currentTSState = TS_INACTIVE;
|
||||
airstate.ShutdownCircuitVoltage = 0;
|
||||
airstate.RelaisSupplyVoltage = 0;
|
||||
airstate.AIRNegativeCurrent = 0;
|
||||
airstate.AIRPositiveCurrent = 0;
|
||||
airstate.AIRPrechargeCurrent = 0;
|
||||
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);*/
|
||||
|
||||
uint32_t startmils = HAL_GetTick() + ADC_READ_TIMEOUT;
|
||||
|
||||
{
|
||||
airstate->RelaisSupplyVoltage = 3000;
|
||||
airstate->AIRPositiveCurrent = 0;
|
||||
airstate->AIRNegativeCurrent = 0;
|
||||
airstate->AIRPrechargeCurrent = 0;
|
||||
airstate->ShutdownCircuitVoltage = 3000;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
return TS_ERROR;
|
||||
}
|
||||
|
||||
else if ((airstate->currentTSState == TS_INACTIVE) &&
|
||||
(airstate->targetTSState ==
|
||||
TS_ACTIVE)) // Transition from Inactive to Active via Precharge
|
||||
{
|
||||
if ((airstate->RelaisSupplyVoltage) > SDC_LOWER_THRESHOLD) {
|
||||
airstate->currentTSState = TS_PRECHARGE;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Is it correct that we also go from precharge to discharge?
|
||||
else if ((airstate->currentTSState == TS_ACTIVE ||
|
||||
airstate->currentTSState == TS_PRECHARGE) &&
|
||||
(airstate->targetTSState ==
|
||||
TS_INACTIVE)) // Transition from Active to Inactive via Discharge
|
||||
{
|
||||
airstate->currentTSState = TS_DISCHARGE;
|
||||
}
|
||||
|
||||
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_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.90)) {
|
||||
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_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() {
|
||||
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void AIR_Inactive_Position() {
|
||||
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void AIR_Discharge_Position() {
|
||||
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void AIR_Active_Position() // TODO Deactivate Precharge after a while to
|
||||
// decrease current Consumption
|
||||
{
|
||||
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
||||
GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void AIR_Error_Position() {
|
||||
HAL_GPIO_WritePin(PreCharge_Control_GPIO_Port, PreCharge_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(AIR_negative_Control_GPIO_Port, AIR_negative_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(AIR_Positive_Control_GPIO_Port, AIR_Positive_Control_Pin,
|
||||
GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
|
||||
if (hadc == air_current_adc) {
|
||||
air_adc_complete = 1;
|
||||
HAL_ADC_Stop_DMA(air_current_adc);
|
||||
}
|
||||
if (hadc == sdc_voltage_adc) {
|
||||
sdc_adc_complete = 1;
|
||||
HAL_ADC_Stop_DMA(sdc_voltage_adc);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user