rusefi/unit_tests/tests/test_pwm_generator.cpp

152 lines
4.7 KiB
C++
Raw Normal View History

2018-12-08 15:11:28 -08:00
/*
* test_pwm_generator.cpp
*
* @date Dec 8, 2018
2022-01-05 17:12:38 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2018-12-08 15:11:28 -08:00
*/
#include "pch.h"
2018-12-08 15:11:28 -08:00
2018-12-08 20:12:37 -08:00
#define LOW_VALUE 0
#define HIGH_VALUE 1
2018-12-08 15:11:28 -08:00
extern int timeNowUs;
2018-12-08 19:57:00 -08:00
static int expectedTimeOfNextEvent;
2018-12-08 15:11:28 -08:00
static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor *executor, OutputPin& pin) {
printf("PWM_test: Asserting event [%s]\r\n", msg);
2018-12-08 16:15:24 -08:00
// only one action expected in queue
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 1, executor->size()) << "PWM_test: schedulingQueue size";
2018-12-08 16:15:24 -08:00
// move time to next event timestamp
timeNowUs = expectedTimeOfNextEvent;
2018-12-08 16:15:24 -08:00
// execute pending actions and assert that only one action was executed
2024-04-26 12:49:37 -07:00
ASSERT_EQ(1, executor->executeAll(timeNowUs) << msg << " executed";
ASSERT_EQ(expectedPinState, pin.currentLogicValue) << msg << " pin state";
2018-12-08 16:15:24 -08:00
// assert that we have one new action in queue
2024-04-26 12:49:37 -07:00
ASSERT_EQ(1, executor->size()) << "PWM_test: queue.size";
2018-12-08 15:11:28 -08:00
}
2018-12-08 19:57:00 -08:00
static void test100dutyCycle() {
printf("*************************************** test100dutyCycle\r\n");
2018-12-08 15:11:28 -08:00
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent = timeNowUs = 0;
2018-12-08 19:57:00 -08:00
OutputPin pin;
SimplePwm pwm("test PWM1");
TestExecutor executor;
2018-12-08 19:57:00 -08:00
startSimplePwm(&pwm, "unit_test",
&executor,
2018-12-08 19:57:00 -08:00
&pin,
1000 /* frequency */,
1.0 /* duty cycle */);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
assertEqualsM2("1@1000/100", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 15:11:28 -08:00
assertNextEvent("exec@100", HIGH_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
assertNextEvent("exec2@100", HIGH_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
assertNextEvent("exec3@100", HIGH_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
}
static void testSwitchToNanPeriod() {
printf("*************************************** testSwitchToNanPeriod\r\n");
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent = timeNowUs = 0;
2018-12-08 15:11:28 -08:00
OutputPin pin;
SimplePwm pwm("test PWM1");
TestExecutor executor;
2018-12-08 15:11:28 -08:00
2018-12-08 19:57:00 -08:00
startSimplePwm(&pwm, "unit_test",
&executor,
2018-12-08 19:57:00 -08:00
&pin,
1000 /* frequency */,
0.60 /* duty cycle */);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 600;
assertEqualsM2("1@1000/70", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 19:57:00 -08:00
assertNextEvent("exec@70", LOW_VALUE, &executor, pin);
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 600, timeNowUs) << "time1";
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 400;
assertNextEvent("exec2@70", HIGH_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
pwm.setFrequency(NAN);
expectedTimeOfNextEvent += 600;
assertEqualsM2("1@1000/NAN", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
assertNextEvent("exec2@NAN", LOW_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += MS2US(NAN_FREQUENCY_SLEEP_PERIOD_MS);
assertEqualsM2("2@1000/NAN", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
assertNextEvent("exec3@NAN", LOW_VALUE, &executor, pin);
2018-12-08 19:57:00 -08:00
}
TEST(PWM, testPwmGenerator) {
2018-12-08 19:57:00 -08:00
test100dutyCycle();
testSwitchToNanPeriod();
expectedTimeOfNextEvent = timeNowUs = 0;
2018-12-08 19:57:00 -08:00
OutputPin pin;
SimplePwm pwm("test PWM3");
TestExecutor executor;
2018-12-08 15:11:28 -08:00
startSimplePwm(&pwm,
"unit_test",
&executor,
2018-12-08 15:11:28 -08:00
&pin,
2018-12-08 16:15:24 -08:00
1000 /* frequency */,
0.80 /* duty cycle */);
2018-12-08 15:11:28 -08:00
expectedTimeOfNextEvent += 800;
assertEqualsM2("1@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 15:11:28 -08:00
assertNextEvent("exec@0", LOW_VALUE, &executor, pin);
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 800, timeNowUs) << "time1";
2018-12-08 16:15:24 -08:00
expectedTimeOfNextEvent += 200;
assertEqualsM2("2@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 16:15:24 -08:00
// above we had vanilla duty cycle, now let's handle a special case
2018-12-08 16:15:24 -08:00
pwm.setSimplePwmDutyCycle(0);
assertEqualsM2("2@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
assertNextEvent("exec@1", LOW_VALUE, &executor, pin);
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 1000, timeNowUs) << "time2";
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
assertEqualsM2("3@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
assertNextEvent("exec@2", LOW_VALUE /* pin value */, &executor, pin);
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 2000, timeNowUs) << "time3";
expectedTimeOfNextEvent += 1000;
assertEqualsM2("4@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 16:15:24 -08:00
assertNextEvent("exec@3", LOW_VALUE /* pin value */, &executor, pin);
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 3000, timeNowUs) << "time4";
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
assertEqualsM2("5@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 16:15:24 -08:00
assertNextEvent("exec@4", LOW_VALUE /* pin value */, &executor, pin);
expectedTimeOfNextEvent += 1000;
assertEqualsM2("6@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 16:15:24 -08:00
assertNextEvent("exec@5", LOW_VALUE /* pin value */, &executor, pin);
2018-12-08 19:57:00 -08:00
expectedTimeOfNextEvent += 1000;
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 5000, timeNowUs) << "time4";
assertEqualsM2("7@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
2018-12-08 16:15:24 -08:00
assertNextEvent("exec@6", LOW_VALUE /* pin value */, &executor, pin);
2018-12-08 15:11:28 -08:00
}