NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test_state_controller.h
1 // Header for unit testing of the State Controller Library
2 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
3 
4 // Ensure header is only included once
5 #ifndef TEST_STATE_CONTROLLER_H
6 #define TEST_STATE_CONTROLLER_H
7 
8 // Include configurations
9 #ifndef VERBOSE_TEST
10 //#define VERBOSE_TEST // Comment this line out to inhibit printing to console
11 #endif /* VERBOSE_TEST */
12 
13 // Includes
14 #include <iostream>
15 #include <iomanip>
16 #include <gtest/gtest.h>
17 #include <test_utilities/test_utilities.h>
19 
20 // State controller test namespace
21 namespace statecontrollertest
22 {
23  // Namespaces
24  using namespace testutilities;
25  using namespace statecontroller;
26 
27  // Enumerations
28  enum DemoSCStateID
29  {
30  LOOK_FOR_BALL,
31  WALK_TO_BALL,
32  LINE_UP_GOAL,
33  KICK_BALL,
34  TEST_STATE
35  };
36 
37  // Class declarations
38  class DemoSC;
39  class LookForBallState;
40  class WalkToBallState;
41  class LineUpGoalState;
42  class KickBallState;
43  class TestState;
44 
45  //
46  // DemoSC state controller class
47  //
48  class DemoSC : public StateController
49  {
50  public:
51  // Constructors
52  DemoSC() : StateController("DemoSC") // Note: You may also choose to use unique names for each instance of the derived state controller class by implementing passing a name as a constructor parameter
53  , g_state_num_exec(0)
54  , g_state_num_deact(0)
55  , g_test_sc_callbacks(false)
56  , g_tested_term(false)
57  , g_test_exec(false)
58  , g_test_deact(false)
59  {
60  // Initialise arrays (not in initialiser list due to C4351 in MSVC)
61  for(int i = 0; i < 6; i++) { g_scc[i] = 0; }
62  for(int i = 0; i < 3; i++) { g_sc[i][0] = 0; g_sc[i][1] = 0; g_sc[i][2] = 0; }
63  }
64 
65  // Step callbacks
66  virtual bool preStepCallback();
67  virtual void preActivateCallback(bool willCallActivate);
68  virtual void preExecuteCallback();
69  virtual bool postExecuteCallback();
70  virtual void onTerminateCallback();
71  virtual void postStepCallback();
72 
73  // Test variables for test_State unit test
74  int g_state_num_exec;
75  int g_state_num_deact;
76 
77  // Test variables for test_StateController
78  int g_scc[6];
79  int g_sc[3][3];
80  bool g_test_sc_callbacks;
81  bool g_tested_term;
82  bool g_test_exec;
83  bool g_test_deact;
84  };
85 
86  //
87  // State classes
88  //
89 
90  // LookForBallState class
91  // Demonstrates: Deriving directly from State for more flexibility / state with no state parameters / overriding all available state callbacks
92  class LookForBallState : public State
93  {
94  public:
95  // Constructors
96  LookForBallState(DemoSC *sc) : State(LOOK_FOR_BALL, "LOOK_FOR_BALL", sc->name), sc(sc) {}
97 
98  protected:
99  // Protected state properties
100  StateController* scref() const { return static_cast<StateController*>(sc); }
101 
102  // State callbacks
103  void activate(cycle_t cyc);
104  action_t execute(cycle_t cyc);
105  void deactivate(cycle_t cyc, bool wasExecuted);
106 
107  // State controller pointer
108  DemoSC* const sc;
109  };
110 
111  // WalkToBallState class
112  // Demonstrates: Deriving from GenState / state with large number of state parameters / overriding all available state callbacks
113  class WalkToBallState : public GenState<DemoSC>
114  {
115  public:
116  // State parameter data structure type
117  struct StateParams
118  {
119  float target_x;
120  float target_y;
121  int gait_type;
122  float gait_param_1;
123  float gait_param_2;
124  bool use_fast_mode;
125  };
126 
127  // Constructors
128  WalkToBallState(DemoSC *sc, const StateParams& sp) : GenState(sc, WALK_TO_BALL, "WALK_TO_BALL"), sp(sp) {}
129 
130  protected:
131  // State callbacks
132  void activate(cycle_t cyc);
133  action_t execute(cycle_t cyc);
134  void deactivate(cycle_t cyc, bool wasExecuted);
135 
136  // State parameters
137  StateParams sp;
138  };
139 
140  // LineUpGoalState class
141  // Demonstrates: Deriving from GenState / state with no state parameters / overriding all available state callbacks
142  class LineUpGoalState : public GenState<DemoSC>
143  {
144  public:
145  // Constructors
146  LineUpGoalState(DemoSC *sc) : GenState(sc, LINE_UP_GOAL, "LINE_UP_GOAL") {}
147 
148  protected:
149  // State callbacks
150  void activate(cycle_t cyc);
151  action_t execute(cycle_t cyc);
152  void deactivate(cycle_t cyc, bool wasExecuted);
153  };
154 
155  // KickBallState class
156  // Demonstrates: Deriving from GenState / state with small number of state parameters / choosing to override deactivate() callback only in addition to execute()
157  class KickBallState : public GenState<DemoSC>
158  {
159  public:
160  // Constructors
161  KickBallState(DemoSC *sc, int kick_type) : GenState(sc, KICK_BALL, "KICK_BALL"), sp_kick_type(kick_type) {}
162 
163  // Test functions (used in the test_State unit test)
164  void testProtected();
165 
166  protected:
167  // State callbacks
168  action_t execute(cycle_t cyc);
169  void deactivate(cycle_t cyc, bool wasExecuted);
170 
171  // State parameters
172  int sp_kick_type;
173  };
174 
175  // TestState class
176  // Used by unit tests to test special case scenarios
177  class TestState : public GenState<DemoSC>
178  {
179  public:
180  // Constructors
181  TestState(DemoSC* sc) : GenState(sc, TEST_STATE, "TEST_STATE") { DISPLAY(std::cout << "TestState: In constructor" << std::endl); }
182  virtual ~TestState() { DISPLAY(std::cout << "TestState: In destructor" << std::endl); }
183 
184  protected:
185  // State callbacks
186  void activate(cycle_t cyc);
187  action_t execute(cycle_t cyc);
188  void deactivate(cycle_t cyc, bool wasExecuted);
189  };
190 }
191 
192 #endif /* TEST_STATE_CONTROLLER_H */
193 // EOF
Implements the State Controller Library.
Base class for all state controllers.
Definition: state_controller.h:593
Implements a state that a state controller can be in.
Definition: state_controller.h:432
unsigned int cycle_t
Used to count the number of executed state controller cycles.
Definition: state_controller.h:415
bool action_t
Used to represent the state transition actions (HOLD_THIS_STATE and PROCEED_NEXT_STATE) ...
Definition: state_controller.h:416
Specialises the State class using templates to take care of the most common overloads.
Definition: state_controller.h:397