Add 10ms delay after closing relays
This commit is contained in:
		@ -12,6 +12,7 @@
 | 
			
		||||
 | 
			
		||||
#include "stm32g441xx.h"
 | 
			
		||||
#include "stm32g4xx_hal.h"
 | 
			
		||||
#include "stm32g4xx_hal_gpio.h"
 | 
			
		||||
 | 
			
		||||
#define TS_INACTIVE 0
 | 
			
		||||
#define TS_PRECHARGE 2
 | 
			
		||||
@ -51,4 +52,8 @@ void AIR_Discharge_Position();
 | 
			
		||||
void AIR_Active_Position();
 | 
			
		||||
void AIR_Error_Position();
 | 
			
		||||
 | 
			
		||||
typedef enum { RELAY_AIR_NEG, RELAY_AIR_POS, RELAY_PRECHARGE } Relay;
 | 
			
		||||
 | 
			
		||||
void Set_Relay_Position(Relay relay, GPIO_PinState position);
 | 
			
		||||
 | 
			
		||||
#endif /* INC_AIR_STATE_MASCHINE_H_ */
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,10 @@
 | 
			
		||||
 | 
			
		||||
#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};
 | 
			
		||||
@ -178,48 +181,67 @@ void Deactivate_TS(AIRStateHandler* airstate) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
  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() {
 | 
			
		||||
  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);
 | 
			
		||||
  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() {
 | 
			
		||||
  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);
 | 
			
		||||
  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
 | 
			
		||||
{
 | 
			
		||||
  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);
 | 
			
		||||
  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() {
 | 
			
		||||
  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);
 | 
			
		||||
  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;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user