PowerSet self test

This commit is contained in:
Tester23
2025-01-08 10:43:44 +01:00
parent a5e439f03b
commit 5f53e20cd5
3 changed files with 76 additions and 22 deletions

View File

@ -54,6 +54,54 @@ void Test_EnergyMeter_Basic() {
SIM_ClearMQTTHistory();
}
// this takes values like 230V, etc
// and creates a fake BL0942 packet that is added to UART,
// it uses "default" calibration values
void Sim_SendFakeBL0942Packet(float v, float c, float p);
void Test_EnergyMeter_BL0942() {
SIM_ClearOBK(0);
SIM_ClearAndPrepareForMQTTTesting("miscDevice", "bekens");
PIN_SetPinRoleForPinIndex(9, IOR_Relay);
PIN_SetPinChannelForPinIndex(9, 1);
CMD_ExecuteCommand("startDriver BL0942", 0);
// set initial values reported by BL0942 via spoofed UART packets
Sim_SendFakeBL0942Packet(230, 0.26, 60);
Sim_RunSeconds(1, false);
// simulate using doing calibration
CMD_ExecuteCommand("PowerSet 60", 0);
CMD_ExecuteCommand("VoltageSet 230", 0);
CMD_ExecuteCommand("CurrentSet 0.26", 0);
Sim_SendFakeBL0942Packet(230, 0.26, 60);
Sim_RunSeconds(1, false);
// verify calibration worked
SELFTEST_ASSERT_EXPRESSION("$voltage", 230);
SELFTEST_ASSERT_EXPRESSION("$current", 0.26f);
SELFTEST_ASSERT_EXPRESSION("$power", 60);
// test different current
Sim_SendFakeBL0942Packet(230, 0.13, 30);
Sim_RunSeconds(1, false);
SELFTEST_ASSERT_EXPRESSION("$voltage", 230);
SELFTEST_ASSERT_EXPRESSION("$current", 0.13f);
SELFTEST_ASSERT_EXPRESSION("$power", 30);
// simulate user calibrating 30W from BL0942 to be 60W IRL
CMD_ExecuteCommand("PowerSet 60", 0);
Sim_SendFakeBL0942Packet(230, 0.13, 30);
Sim_RunSeconds(1, false);
SELFTEST_ASSERT_EXPRESSION("$power", 60);
Sim_RunSeconds(1, false);
// now. if 30W from BL0942 is 60W IRL,
// then 60W frm BL0942 should give us 120W
Sim_SendFakeBL0942Packet(230, 0.13, 60);
Sim_RunSeconds(1, false);
SELFTEST_ASSERT_EXPRESSION("$power", 120);
SIM_ClearMQTTHistory();
}
void Test_EnergyMeter_Events() {
SIM_ClearOBK(0);
SIM_ClearAndPrepareForMQTTTesting("miscDevice", "bekens");
@ -276,6 +324,7 @@ void Test_EnergyMeter_TurnOffScript() {
SIM_ClearMQTTHistory();
}
void Test_EnergyMeter() {
Test_EnergyMeter_BL0942();
Test_EnergyMeter_Basic();
Test_EnergyMeter_Tasmota();
Test_EnergyMeter_Events();

View File

@ -11,33 +11,18 @@ static float BL0942_PREF = 598;
static float BL0942_UREF = 15188;
static float BL0942_IREF = 251210;
void CControllerBL0942::onDrawn() {
int currentPending = UART_GetDataSize();
if (currentPending > 0) {
return;
}
// Now you must do it by hand in simulation (or put in autoexec.bat)
//CMD_ExecuteCommand("startDriver BL0942", 0);
if (txt_voltage->isBeingEdited() == false) {
realVoltage = txt_voltage->getFloat();
}
if (txt_power->isBeingEdited() == false) {
realPower = txt_power->getFloat();
}
if (txt_current->isBeingEdited() == false) {
realCurrent = txt_current->getFloat();
}
extern "C" void Sim_SendFakeBL0942Packet(float v, float c, float p) {
int i;
byte data[BL0942_PACKET_LEN];
memset(data, 0, sizeof(data));
data[0] = 0x55;
byte checksum = BL0942_READ_COMMAND;
int bl_current = (int)(BL0942_IREF * realCurrent);
int bl_power = (int)(BL0942_PREF * realPower);
int bl_voltage = (int)(BL0942_UREF * realVoltage);
int bl_current = (int)(BL0942_IREF * c);
int bl_power = (int)(BL0942_PREF * p);
int bl_voltage = (int)(BL0942_UREF * v);
data[1] = (byte)(bl_current);
data[2] = (byte)(bl_current >> 8);
@ -62,13 +47,33 @@ void CControllerBL0942::onDrawn() {
printf("%02X ", data[i]);
}
printf("\n");
printf("Voltage = %.2f, Current = %.3f, Power = %.2f\n", realVoltage, realCurrent, realPower);
printf("Voltage = %.2f, Current = %.3f, Power = %.2f\n", v, c, p);
}
for (i = 0; i < BL0942_PACKET_LEN; i++) {
UART_AppendByteToReceiveRingBuffer(data[i]);
}
}
void CControllerBL0942::onDrawn() {
int currentPending = UART_GetDataSize();
if (currentPending > 0) {
return;
}
// Now you must do it by hand in simulation (or put in autoexec.bat)
//CMD_ExecuteCommand("startDriver BL0942", 0);
if (txt_voltage->isBeingEdited() == false) {
realVoltage = txt_voltage->getFloat();
}
if (txt_power->isBeingEdited() == false) {
realPower = txt_power->getFloat();
}
if (txt_current->isBeingEdited() == false) {
realCurrent = txt_current->getFloat();
}
Sim_SendFakeBL0942Packet(realVoltage, realPower, realCurrent);
}
class CControllerBase *CControllerBL0942::cloneController(class CShape *origOwner, class CShape *newOwner) {
CControllerBL0942 *r = new CControllerBL0942();
if (tx) {

View File

@ -129,6 +129,7 @@ void SIM_ClearOBK(const char *flashPath) {
Main_Init();
}
void Win_DoUnitTests() {
Test_EnergyMeter();
Test_TuyaMCU_Boolean();
Test_TuyaMCU_DP22();
@ -143,7 +144,6 @@ void Win_DoUnitTests() {
Test_Command_If_Else();
Test_MQTT();
Test_ChargeLimitDriver();
Test_EnergyMeter();
// this is slowest
Test_TuyaMCU_Basic();
Test_TuyaMCU_Mult();