Skip to content

Commit

Permalink
fix heater control timer test
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Sep 25, 2024
1 parent 47a8270 commit 4e3e4d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions firmware/util/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ int64_t Timer::getTimestamp() const {
Timer::mockTimeStamp = stamp;
}

/*static*/ void Timer::advanceMockTime(int64_t increment) {
Timer::mockTimeStamp += increment;
}

#else
#include "ch.hpp"

Expand Down
1 change: 1 addition & 0 deletions firmware/util/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Timer final {
float getElapsedSecondsAndReset();

static void setMockTime(int64_t stamp);
static void advanceMockTime(int64_t increment);

private:
int64_t getTimestamp() const;
Expand Down
24 changes: 22 additions & 2 deletions test/tests/test_heater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,35 @@ TEST(HeaterStateMachine, WarmupTimeout)
TEST(HeaterStateMachine, ClosedLoop)
{
MockHeater dut;
Timer::setMockTime(0);
dut.Configure(780, 300);

// Temperature is reasonable, stay in closed loop
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780));
Timer::advanceMockTime(10e6);
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780));

// Allow too hot briefly
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000));
Timer::advanceMockTime(0.1e6);
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000));

// Temperature is too hot, overheat
// Wait too long, overheat not allowed
Timer::advanceMockTime(1e6);
EXPECT_EQ(HeaterState::Stopped, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000));

// Temperature is too cold, underheat
// Back to normal
dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780);
Timer::advanceMockTime(1e6);
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780));

// Allow too cold briefly
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600));
Timer::advanceMockTime(0.1e6);
EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600));

// Wait too long, underheat not allowed
Timer::advanceMockTime(1e6);
EXPECT_EQ(HeaterState::Stopped, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600));
}

Expand Down

0 comments on commit 4e3e4d7

Please sign in to comment.