forked from achamaikin/CCSModuleSW30Web
Add PSU fast discharge API for DC precharge step-down (v1.0.23).
Policy/executor hooks, event-driven OFF/ON state machine, and release binaries. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+148
-9
@@ -6,6 +6,7 @@
|
||||
#include "charger_control.h"
|
||||
#include "debug.h"
|
||||
#include "isr_opt.h"
|
||||
#include "power_setpoint_executor.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -41,6 +42,24 @@ static uint32_t PSU_StateTime(void){
|
||||
return HAL_GetTick() - PSU0.statetick;
|
||||
}
|
||||
|
||||
static void PSU_FastDischarge_PushTarget(void)
|
||||
{
|
||||
PowerSetpoint_t fd_sp;
|
||||
|
||||
PowerSetpoint_GetFastDischargeTarget(&fd_sp);
|
||||
PSU_SetVoltageCurrent(0, fd_sp.voltage_V, fd_sp.current_0p1A);
|
||||
PSU_Enable(0, 1);
|
||||
}
|
||||
|
||||
static void PSU_FastDischarge_AbortToConnected(void)
|
||||
{
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
if (!PSU0.PSU_enabled) {
|
||||
PSU_FastDischarge_PushTarget();
|
||||
}
|
||||
PSU_SwitchState(PSU_CONNECTED);
|
||||
}
|
||||
|
||||
ISR_FAST void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan){
|
||||
|
||||
static CAN_RxHeaderTypeDef RxHeader;
|
||||
@@ -154,6 +173,7 @@ void PSU_Init(){
|
||||
PSU0.hv_tick = 0;
|
||||
|
||||
PSU_Enable(0, 0);
|
||||
PowerSetpoint_Init();
|
||||
}
|
||||
|
||||
void PSU_Enable(uint8_t addr, uint8_t enable){
|
||||
@@ -258,20 +278,34 @@ void PSU_ReadWrite(){
|
||||
}
|
||||
CONN.RequestedPower = CONN.RequestedCurrent * CONN.RequestedVoltage / 10;
|
||||
|
||||
if(PSU0.ready){
|
||||
if (CONN.RequestedVoltage == FAKE_EVREQ_VOLTAGE_V) {
|
||||
PSU_SetVoltageCurrent(0, (uint16_t)FAKE_PSU_VOLTAGE_V, (uint16_t)FAKE_PSU_CURRENT_0P1A);
|
||||
}else{
|
||||
PSU_SetVoltageCurrent(0, CONN.RequestedVoltage, CONN.RequestedCurrent); // Normal mode
|
||||
#if PSD_ENABLE
|
||||
{
|
||||
uint8_t block_setpoint = PowerSetpoint_HasPending() || PowerSetpoint_IsBusy();
|
||||
|
||||
if (PSU0.ready && !block_setpoint) {
|
||||
if (CONN.RequestedVoltage == FAKE_EVREQ_VOLTAGE_V) {
|
||||
PSU_SetVoltageCurrent(0, (uint16_t)FAKE_PSU_VOLTAGE_V, (uint16_t)FAKE_PSU_CURRENT_0P1A);
|
||||
} else {
|
||||
PSU_SetVoltageCurrent(0, CONN.RequestedVoltage, CONN.RequestedCurrent); // Normal mode
|
||||
}
|
||||
ED_Delay(CAN_DELAY);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (PSU0.ready) {
|
||||
PSU_SetVoltageCurrent(0, CONN.RequestedVoltage, CONN.RequestedCurrent); // Normal mode
|
||||
ED_Delay(CAN_DELAY);
|
||||
if(CONN.MeasuredVoltage > 490){
|
||||
if(PSU0.hv_tick == 0){
|
||||
}
|
||||
#endif
|
||||
|
||||
if (PSU0.ready) {
|
||||
if (CONN.MeasuredVoltage > 490) {
|
||||
if (PSU0.hv_tick == 0) {
|
||||
PSU0.hv_tick = HAL_GetTick();
|
||||
}else if((HAL_GetTick() - PSU0.hv_tick) >= 10000){
|
||||
} else if ((HAL_GetTick() - PSU0.hv_tick) >= 10000) {
|
||||
PSU0.hv_mode = 1;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
PSU0.hv_tick = 0;
|
||||
}
|
||||
}
|
||||
@@ -284,6 +318,18 @@ void PSU_ReadWrite(){
|
||||
void PSU_Task(void){
|
||||
static uint32_t psu_on_tick = 0;
|
||||
static uint32_t cont_ok_tick = 0;
|
||||
static uint32_t fd_on_last_cmd_ms = 0;
|
||||
PowerSetpointTryResult_t psd_result;
|
||||
|
||||
#if PSD_ENABLE
|
||||
PowerSetpoint_UpdateDeliveryLatch();
|
||||
if (PowerSetpoint_HasPending()) {
|
||||
psd_result = PowerSetpoint_TryApply();
|
||||
if (psd_result == PSD_TRY_START_FAST_DISCHARGE) {
|
||||
PSU_SwitchState(PSU_FAST_DISCHARGE_OFF);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Обновляем ONLINE/READY по таймауту
|
||||
if((HAL_GetTick() - can_lastpacket) > PSU_ONLINE_TIMEOUT){
|
||||
@@ -403,6 +449,99 @@ void PSU_Task(void){
|
||||
}
|
||||
break;
|
||||
|
||||
case PSU_FAST_DISCHARGE_OFF:
|
||||
if (!CONN.EnableOutput || !PSU0.ready || PSU0.cont_fault || PSU0.psu_fault) {
|
||||
log_printf(LOG_WARN, "Fast discharge abort -> stop\n");
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
PSU_SwitchState(PSU_CURRENT_DROP);
|
||||
break;
|
||||
}
|
||||
PSU_Enable(0, 0);
|
||||
PSU_SwitchState(PSU_FAST_DISCHARGE_WAIT);
|
||||
break;
|
||||
|
||||
case PSU_FAST_DISCHARGE_WAIT: {
|
||||
PowerSetpoint_t fd_sp;
|
||||
|
||||
if (!CONN.EnableOutput || !PSU0.ready || PSU0.cont_fault || PSU0.psu_fault) {
|
||||
log_printf(LOG_WARN, "Fast discharge abort -> stop\n");
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
PSU_SwitchState(PSU_CURRENT_DROP);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Как PSU_WAIT_ACK_OFF: 0–20 V на телеметрии = модуль выключен */
|
||||
if (!PSU0.PSU_enabled) {
|
||||
PowerSetpoint_GetFastDischargeTarget(&fd_sp);
|
||||
log_printf(LOG_INFO, "Fast discharge off ack, V=%u -> set %u\n",
|
||||
(unsigned)CONN.MeasuredVoltage, (unsigned)fd_sp.voltage_V);
|
||||
PSU_SwitchState(PSU_FAST_DISCHARGE_SET);
|
||||
} else if (PSU_StateTime() > PSD_OFF_TIMEOUT_MS) {
|
||||
log_printf(LOG_WARN, "Fast discharge off timeout, V=%u\n",
|
||||
(unsigned)CONN.MeasuredVoltage);
|
||||
PSU_FastDischarge_AbortToConnected();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case PSU_FAST_DISCHARGE_SET: {
|
||||
PowerSetpoint_t fd_sp;
|
||||
|
||||
if (!CONN.EnableOutput || !PSU0.ready || PSU0.cont_fault || PSU0.psu_fault) {
|
||||
log_printf(LOG_WARN, "Fast discharge abort -> stop\n");
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
PSU_SwitchState(PSU_CURRENT_DROP);
|
||||
break;
|
||||
}
|
||||
PowerSetpoint_GetFastDischargeTarget(&fd_sp);
|
||||
PSU_SetVoltageCurrent(0, fd_sp.voltage_V, fd_sp.current_0p1A);
|
||||
PSU_SwitchState(PSU_FAST_DISCHARGE_ON);
|
||||
break;
|
||||
}
|
||||
|
||||
case PSU_FAST_DISCHARGE_ON:
|
||||
if (!CONN.EnableOutput || !PSU0.ready || PSU0.cont_fault || PSU0.psu_fault) {
|
||||
log_printf(LOG_WARN, "Fast discharge abort -> stop\n");
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
PSU_SwitchState(PSU_CURRENT_DROP);
|
||||
break;
|
||||
}
|
||||
PSU_Enable(0, 1);
|
||||
fd_on_last_cmd_ms = HAL_GetTick();
|
||||
PSU_SwitchState(PSU_FAST_DISCHARGE_ON_WAIT);
|
||||
break;
|
||||
|
||||
case PSU_FAST_DISCHARGE_ON_WAIT: {
|
||||
PowerSetpoint_t fd_sp;
|
||||
uint16_t v_low;
|
||||
|
||||
if (!CONN.EnableOutput || !PSU0.ready || PSU0.cont_fault || PSU0.psu_fault) {
|
||||
log_printf(LOG_WARN, "Fast discharge abort -> stop\n");
|
||||
PowerSetpoint_OnFastDischargeAbort();
|
||||
PSU_SwitchState(PSU_CURRENT_DROP);
|
||||
break;
|
||||
}
|
||||
PowerSetpoint_GetFastDischargeTarget(&fd_sp);
|
||||
v_low = (fd_sp.voltage_V > PSD_PRECHARGE_TOLERANCE_V) ?
|
||||
(uint16_t)(fd_sp.voltage_V - PSD_PRECHARGE_TOLERANCE_V) : 0u;
|
||||
|
||||
if (PSU0.PSU_enabled && CONN.MeasuredVoltage >= v_low) {
|
||||
PowerSetpoint_OnFastDischargeComplete();
|
||||
PSU_SwitchState(PSU_CONNECTED);
|
||||
} else {
|
||||
if ((HAL_GetTick() - fd_on_last_cmd_ms) >= PSD_ON_RETRY_MS) {
|
||||
PSU_FastDischarge_PushTarget();
|
||||
fd_on_last_cmd_ms = HAL_GetTick();
|
||||
}
|
||||
if (PSU_StateTime() > PSD_ON_TIMEOUT_MS) {
|
||||
log_printf(LOG_WARN, "Fast discharge on timeout, V=%u (target %u)\n",
|
||||
(unsigned)CONN.MeasuredVoltage, (unsigned)fd_sp.voltage_V);
|
||||
PSU_FastDischarge_AbortToConnected();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case PSU_CURRENT_DROP:
|
||||
// снижаем ток до нуля перед отключением DC
|
||||
CONN.RequestedCurrent = 0;
|
||||
|
||||
Reference in New Issue
Block a user