move to charger.cpp complete, stubs & TODOs for missing tests

This commit is contained in:
Janosch
2022-10-14 13:24:43 +01:00
parent 100821e68c
commit 3ba4f26b43
8 changed files with 178 additions and 165 deletions
+164
View File
@@ -208,3 +208,167 @@ void CalcEnable()
Param::SetInt(Param::enable, enable);
}
// TODO: no unit tests below here (see test_logic.h and implement there)
bool CheckChargerFaults()
{
const int acPresentThresh = 70;
const int timeout = 20;
static int counters[3] = { timeout, timeout, timeout };
int configuredChargers = Param::GetInt(Param::chargerena);
bool timeouts[3];
bool active1 = (configuredChargers & 1) && (Param::GetInt(Param::c1uac) > acPresentThresh);
bool active2 = (configuredChargers & 2) && (Param::GetInt(Param::c2uac) > acPresentThresh);
bool active3 = (configuredChargers & 4) && (Param::GetInt(Param::c3uac) > acPresentThresh);
timeouts[0] = (Param::GetInt(Param::c1flag) & FLAG_CHECK) != 0;
timeouts[1] = (Param::GetInt(Param::c2flag) & FLAG_CHECK) != 0;
timeouts[2] = (Param::GetInt(Param::c3flag) & FLAG_CHECK) != 0;
for (int i = 0; i < 3; i++)
{
if (timeouts[i])
{
if (counters[i] > 0)
{
counters[i]--;
timeouts[i] = false;
}
else
{
ErrorMessage::Post(ERR_CHARGERCAN);
}
}
else
{
counters[i] = timeout;
}
}
//Set check flag. By the next call this should be deleted by the CAN module
Param::SetInt(Param::c1flag, Param::GetInt(Param::c1flag) | FLAG_CHECK);
Param::SetInt(Param::c2flag, Param::GetInt(Param::c2flag) | FLAG_CHECK);
Param::SetInt(Param::c3flag, Param::GetInt(Param::c3flag) | FLAG_CHECK);
return (active1 && ((Param::GetInt(Param::c1flag) & FLAG_FAULT) || timeouts[0])) ||
(active2 && ((Param::GetInt(Param::c2flag) & FLAG_FAULT) || timeouts[1])) ||
(active3 && ((Param::GetInt(Param::c3flag) & FLAG_FAULT) || timeouts[2]));
}
void CalcAcCurrentLimit()
{
int configuredChargers = Param::GetInt(Param::chargerena);
float iacLim = Param::GetFloat(Param::iaclim);
float hwaclim = Param::GetFloat(Param::hwaclim);
float evseLim = Param::GetFloat(Param::evselim);
float cableLim = Param::GetFloat(Param::cablelim);
int activeModules = ((configuredChargers & 1) > 0) + ((configuredChargers & 2) > 0) + ((configuredChargers & 4) > 0);
if (IsEvseInput())
{
iacLim = MIN(iacLim, MIN(evseLim, cableLim));
}
if (Param::GetInt(Param::opmode) == 0)
{
dcCurController.ResetIntegrator();
iacLim = 0;
}
else
{
dcCurController.SetMinMaxY(0, iacLim);
iacLim = dcCurController.Run(Param::Get(Param::idc));
}
if (Param::GetInt(Param::inputype) == INP_MANUAL ||
Param::GetInt(Param::inputype) == INP_TYPE1 ||
Param::GetInt(Param::inputype) == INP_TYPE2 ||
(Param::GetInt(Param::inputype) == INP_TYPE2_AUTO && !DigIo::threep_in.Get()))
{
iacLim /= (float)activeModules;
}
iacLim = MIN(iacLim, hwaclim);
Param::SetFloat(Param::aclim, iacLim);
}
void ChargerStateMachine()
{
static states state = OFF;
int configuredChargers = Param::GetInt(Param::chargerena);
if (!Param::GetBool(Param::enable))
{
state = OFF;
}
switch (state)
{
default:
case OFF:
Param::SetInt(Param::opmode, 0);
DisableAll();
if (CheckStartCondition())
{
startTime = rtc_get_counter_val();
state = WAITSTART;
}
break;
case WAITSTART:
if (CheckDelay())
state = ENABLE;
break;
case ENABLE:
DigIo::hvena_out.Set();
if (configuredChargers & 1)
DigIo::ch1ena_out.Set();
if (configuredChargers & 2)
DigIo::ch2ena_out.Set();
if (configuredChargers & 4)
DigIo::ch3ena_out.Set();
state = ACTIVATE;
break;
case ACTIVATE:
Param::SetInt(Param::opmode, 1);
if (configuredChargers & 1)
DigIo::ch1act_out.Set();
if (configuredChargers & 2)
DigIo::ch2act_out.Set();
if (configuredChargers & 4)
DigIo::ch3act_out.Set();
startTime = rtc_get_counter_val();
state = EVSEACTIVATE;
break;
case EVSEACTIVATE:
DigIo::evseact_out.Set();
DigIo::acpres_out.Set();
if (CheckVoltage() || CheckTimeout())
state = STOP;
if (CheckUnplugged())
{
DigIo::acpres_out.Clear();
DigIo::evseact_out.Clear();
state = OFF;
}
if (CheckChargerFaults())
{
DigIo::acpres_out.Clear();
state = OFF;
}
break;
case STOP:
DisableAll();
Param::SetInt(Param::opmode, 0);
if (CheckUnplugged())
state = OFF;
break;
}
Param::SetInt(Param::state, state);
}