Skip to content
Snippets Groups Projects
Commit 632e45ad authored by Luca Erbetta's avatar Luca Erbetta :rocket:
Browse files

[FMM-TEST] Added simple test for error state transition

parent 0b15f914
Branches
Tags
No related merge requests found
......@@ -183,7 +183,7 @@ Type: board
BoardId: stm32f429zi_skyward_homeone
BinName: test-fmm
Include: %shared %homeone %logger
Defines: -DDEBUG
Defines:
Main: test-fmm
[test-timer]
......
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_RUNNER
#define CATCH_CONFIG_NO_POSIX_SIGNALS
#define CATCH_CONFIG_NO_CPP11_GENERATED_METHODS
#define CATCH_CONFIG_NO_CPP11_TYPE_TRAITS
#include <miosix.h>
#include <catch.hpp>
#include <string>
#include <vector>
#include "boards/Homeone/Events.h"
#include "boards/Homeone/FlightModeManager/FlightModeManager.h"
#include "boards/Homeone/Topics.h"
#include "drivers/HardwareTimer.h"
#include "events/EventBroker.h"
using namespace HomeoneBoard;
using HomeoneBoard::FMM::FlightModeManager;
using miosix::Thread;
using std::string;
using std::vector;
using Timer = HardwareTimer<uint32_t, 2>;
template <typename TimerType>
class ExecutionProfiler
{
public:
ExecutionProfiler(TimerType& timer) : hw_timer(timer) {}
void start(string msg)
{
current_msg = msg;
printf("%s START\n", msg.c_str());
#ifdef DEBUG
printf("DEBUG is defined. Execution times may not be correct.\n",
msg.c_str());
#endif
hw_timer.stop();
hw_timer.start();
}
int Factorial( int number ) {
return number <= 1 ? number : Factorial( number - 1 ) * number; // fail
// return number <= 1 ? 1 : Factorial( number - 1 ) * number; // pass
void tick()
{
auto tick = hw_timer.tick();
printf("%s\nTime: %.3f (%.3f us)\n", current_msg.c_str(),
hw_timer.toMilliSeconds(tick), hw_timer.toMicroSeconds(tick));
}
void stop()
{
auto tick = hw_timer.stop();
printf("%s\nTotal execution time: %.3f ms (%.3f us)\n",
current_msg.c_str(), hw_timer.toMilliSeconds(tick),
hw_timer.toMicroSeconds(tick));
}
TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) {
private:
string current_msg = "";
TimerType& hw_timer;
};
REQUIRE( Factorial(0) == 1 );
int main()
{
int result = Catch::Session().run();
for (;;)
{
printf("end.\n");
Thread::sleep(10000);
}
}
void checkTransition(FlightModeManager& mgr, FMMState previous,
FMMState expected)
{
for (int i = 0; i < 1000; i++)
{
if (i == 999)
FAIL("Transition timeout\n"); // Fail the test. The state machine
// didn't change the state
if (mgr.getStatus().state != previous)
break;
usleep(100);
}
TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE(mgr.getStatus().state == expected);
}
REQUIRE( Factorial(2) == 2 );
TEST_CASE("FlightModeManager", "[fmm]")
{
Timer& tim = Timer::instance();
tim.setPrescaler(83);
REQUIRE( Factorial(3) == 6 );
ExecutionProfiler<Timer> ep{tim};
REQUIRE( Factorial(10) == 3628800 );
FlightModeManager& mgr = *FlightModeManager::getInstance();
mgr.start();
checkTransition(mgr, FMMState::UNDEFINED, FMMState::INIT);
ep.start("Profiling init error.");
sEventBroker->post({EV_INIT_ERROR}, TOPIC_FLIGHT_EVENTS);
// ep.tick();
checkTransition(mgr, FMMState::INIT, FMMState::ERROR);
ep.stop();
}
\ No newline at end of file
......@@ -29,33 +29,16 @@ using miosix::Thread;
int main()
{
HardwareTimer<uint32_t, 2> timer2 = HardwareTimer<uint32_t, 2>::instance();
HardwareTimer<uint16_t, 10> timer1 =
HardwareTimer<uint16_t, 10>::instance();
/*
timer1.setPrescaler(9999);
timer1.start();
timer2.start();
int i = 0;
for (;;)
{
uint32_t start2 = timer2.tick();
Thread::sleep(10 * pow(10, i));
uint32_t end2 = timer2.tick();
HardwareTimer<uint32_t, 2>& timer2 = HardwareTimer<uint32_t, 2>::instance();
uint16_t start1 = timer1.start();
Thread::sleep(10 * pow(10, i));
uint16_t end1 = timer1.tick();
timer2.start();
uint32_t t2 = end2 - start2;
uint32_t t1 = end1 - start1;
while (true)
{
uint32_t tick = timer2.tick();
printf("%d\t\t(%.3f)\n", tick, timer2.toMilliSeconds(tick));
printf("Timer1: %.6f, %.3f, %.3f\n", timer1.toSeconds(t1),
timer1.toMilliSeconds(t1), timer1.toMicroSeconds(t1));
printf("Timer2: %.6f, %.3f, %.3f\n\n", timer2.toSeconds(t2),
timer2.toMilliSeconds(t2), timer2.toMicroSeconds(t2));
i++;
usleep(100000);
}
*/
return 0;
}
\ No newline at end of file
......@@ -27,6 +27,7 @@
enum class FMMState : uint8_t
{
UNDEFINED,
INIT,
TESTING,
ERROR,
......@@ -42,7 +43,7 @@ enum class FMMState : uint8_t
struct FMMStatus
{
FMMState state;
FMMState state = FMMState::UNDEFINED;
};
#endif /* SRC_SHARED_BOARDS_HOMEONE_FLIGHTMODEMANAGER_FMMSTATUS_H */
......@@ -34,7 +34,7 @@ namespace HomeoneBoard
namespace FMM
{
FlightModeManager::FlightModeManager() : FSM(&FlightModeManager::stateDisarmed)
FlightModeManager::FlightModeManager() : FSM(&FlightModeManager::stateInit)
{
sEventBroker->subscribe(this, TOPIC_TC);
sEventBroker->subscribe(this, TOPIC_FLIGHT_EVENTS);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment