| 1 | /* |
| 2 | * SPDX-FileName: testMonostable.cxx |
| 3 | * SPDX-FileComment: Unit tests for monostable autopilot element |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2023 Huntley Palmer |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "testMonostable.hxx" |
| 9 | |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 13 | |
| 14 | |
| 15 | #include <Autopilot/autopilot.hxx> |
| 16 | #include <Autopilot/digitalfilter.hxx> |
| 17 | #include <Main/fg_props.hxx> |
| 18 | #include <Main/globals.hxx> |
| 19 | |
| 20 | |
| 21 | #include <simgear/math/sg_random.hxx> |
| 22 | #include <simgear/props/props_io.hxx> |
| 23 | |
| 24 | // Set up function for each test. |
| 25 | void MonostableTests::setUp() |
| 26 | { |
| 27 | FGTestApi::setUp::initTestGlobals("ap-monostable"); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | // Clean up after each test. |
| 32 | void MonostableTests::tearDown() |
| 33 | { |
| 34 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | SGPropertyNode_ptr MonostableTests::configFromString(const std::string& s) |
| 39 | { |
| 40 | SGPropertyNode_ptr config = new SGPropertyNode; |
| 41 | |
| 42 | std::istringstream iss(s); |
| 43 | readProperties(iss, config); |
| 44 | return config; |
| 45 | } |
| 46 | |
| 47 | void MonostableTests::testMonostable() |
| 48 | { |
| 49 | sg_srandom(999); |
| 50 | // |
| 51 | // Simple monostable from wiki: |
| 52 | // monostable stable state is false, true when Set, until timeout |
| 53 | // ( S == False, test/S == 0 ) : O/P = 0 |
| 54 | // ( S == True , test/S == 1 ) : O/P = 1 |
| 55 | // ( S == False, test/S == 0 ) : Timer running, O/P = 1 |
| 56 | // ( S == False, test/S == 0 ) : Timed out , O/P = 0 |
| 57 | // |
| 58 | // |
| 59 | auto config = configFromString(R"(<?xml version="1.0" encoding="UTF-8"?> |
| 60 | <PropertyList> |
| 61 | <flipflop> |
| 62 | <type>monostable</type> |
| 63 | <S> |
| 64 | <property>/test/S</property> |
| 65 | </S> |
| 66 | <time> |
| 67 | <value>0.50</value> |
| 68 | </time> |
| 69 | <output>/test/Q</output> |
| 70 | </flipflop> |
| 71 | </PropertyList> |
| 72 | )"); |
| 73 | |
| 74 | auto ap = new FGXMLAutopilot::Autopilot(globals->get_props(), config); |
| 75 | |
| 76 | globals->get_subsystem_mgr()->add("ap", ap); |
| 77 | ap->bind(); |
| 78 | ap->init(); |
| 79 | |
| 80 | // Initially test/S is 0 O/P s.b stable state == 0 |
| 81 | fgSetBool("/test/S", false); |
| 82 | ap->update(0.01); |
| 83 | CPPUNIT_ASSERT_EQUAL(false, fgGetBool("/test/Q")); |
| 84 | |
| 85 | // steady input test/S = 0 s.b. no change |
| 86 | ap->update(0.24); |
| 87 | CPPUNIT_ASSERT_EQUAL(false, fgGetBool("/test/Q")); |
| 88 | |
| 89 | // steady test/S = 0 s.b. no change during timer |
| 90 | ap->update(0.25); |
| 91 | CPPUNIT_ASSERT_EQUAL(false, fgGetBool("/test/Q")); |
| 92 | |
| 93 | // steady test/S = 0 s.b. no change after timeout |
| 94 | ap->update(0.25); |
| 95 | CPPUNIT_ASSERT_EQUAL(false, fgGetBool("/test/Q")); |
| 96 | |
| 97 | // Set S input true == 1 O/P should immediately = 1, true |
| 98 | fgSetBool("/test/S", true); |
| 99 | ap->update(0.01); |
| 100 | CPPUNIT_ASSERT_EQUAL(true, fgGetBool("/test/Q")); |
| 101 | |
| 102 | // steady test/S = 1 s.b. no change during timer |
| 103 | ap->update(0.24); |
| 104 | CPPUNIT_ASSERT_EQUAL(true, fgGetBool("/test/Q")); |
| 105 | |
| 106 | // steady test/S = 1 s.b. no change after timeout |
| 107 | ap->update(0.25); |
| 108 | CPPUNIT_ASSERT_EQUAL(true, fgGetBool("/test/Q")); |
| 109 | |
| 110 | // Reset Input test/S = 0 s.b. no change |
| 111 | fgSetBool("/test/S", false); |
| 112 | ap->update(0.01); |
| 113 | CPPUNIT_ASSERT_EQUAL(true, fgGetBool("/test/Q")); |
| 114 | |
| 115 | // steady test/S = 0 s.b. no change during timer |
| 116 | ap->update(0.24); |
| 117 | CPPUNIT_ASSERT_EQUAL(true, fgGetBool("/test/Q")); |
| 118 | |
| 119 | // steady test/S = 0 O/P resets = 0 after timeout |
| 120 | ap->update(0.25); |
| 121 | CPPUNIT_ASSERT_EQUAL(false, fgGetBool("/test/Q")); |
| 122 | } |