NimbRo ROS Soccer Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sitting_demo_fsm.h
1 // Finite state machine for motion demonstration on a sitting robot
2 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
3 
4 // Ensure header is only included once
5 #ifndef SITTING_DEMO_FSM_H
6 #define SITTING_DEMO_FSM_H
7 
8 // Includes
10 #include <limb_control/PlayCommandsSrv.h>
11 #include <ros/time.h>
12 
13 // Sitting demo namespace
14 namespace sitting_demo
15 {
16  // Namespaces
17  using namespace statecontroller;
18 
19  // Class forward-declarations
20  class SittingDemo;
21 
22  // State controller
23  class SittingDemoSC;
24 
25  // States
26  class InitDemoState;
27  class WaitForSittingState;
28  class WaitForTimeState;
29  class WaitForMotionPlayerState;
30  class PlanDemoActionState;
31 
32  // State IDs
33  enum SDSCStateID
34  {
35  INIT_DEMO, // Initialise the sitting demo
36  WAIT_FOR_SITTING, // Wait until the robot enters the sitting state
37  WAIT_FOR_TIME, // Wait for a given number of seconds, or until a certain absolute time has been reached
38  WAIT_FOR_MOTION_PLAYER, // Wait for the motion player to finish whatever it's doing
39  WAIT_FOR_LIMB_CONTROL, // Wait for limb control to finish doing whatever it's doing
40  PLAN_DEMO_ACTION, // Plan a sequence of states that comprises a demo action
41  RETURN_TO_SITTING, // Return to the sitting pose in terms of the head, arm, knee and ankle servos
42  START_MOTION_PLAYBACK, // Start the playback of a motion using the motion player
43  STOP_MOTION_PLAYBACK, // Wait for motion playback using the motion player to cease
44  START_LEG_DANGLING, // Start the leg dangling background behaviour
45  STOP_LEG_DANGLING, // Stop the leg dangling background behaviour
46  START_HEAD_IDLING, // Start the head idling background behaviour
47  STOP_HEAD_IDLING // Stop the head idling background behaviour
48  };
49 
50  // Sitting demo state controller
51  class SittingDemoSC : public StateController
52  {
53  public:
54  // Constructors
55  SittingDemoSC(SittingDemo* SD);
56 
57  // Pointer to main sitting demo object
58  SittingDemo* const m_SD;
59 
60  // State controller callbacks
61  virtual bool preStepCallback();
62  virtual void preActivateCallback(bool willCallActivate);
63  virtual void preExecuteCallback();
64 
65  // Get functions
66  inline const ros::Time& now() const { return m_curStepTime; }
67 
68  protected:
69  // Background motion enable/disable functions
70  void enableLegDangle();
71  void disableLegDangle();
72  void enableHeadIdle();
73  void disableHeadIdle();
74 
75  // Background motion handlers
76  void handleLegDangle();
77  void handleHeadIdle();
78 
79  // Leg dangle variables
80  bool m_legDangleEnabled;
81 
82  // Head idle variables
83  bool m_headIdleEnabled;
84 
85  // Internal variables
86  ros::Time m_curStepTime;
87  };
88 
89  // Init demo state
90  class InitDemoState : public GenState<SittingDemoSC>
91  {
92  public:
93  // Constructor
94  InitDemoState(SittingDemoSC *sc) : GenState(sc, INIT_DEMO, "INIT_DEMO") {}
95 
96  protected:
97  // State callbacks
98  virtual action_t execute(cycle_t cyc);
99  };
100 
101  // Wait for sitting state
102  class WaitForSittingState : public GenState<SittingDemoSC>
103  {
104  public:
105  // Constructor
106  WaitForSittingState(SittingDemoSC *sc) : GenState(sc, WAIT_FOR_SITTING, "WAIT_FOR_SITTING") {}
107 
108  protected:
109  // State callbacks
110  virtual action_t execute(cycle_t cyc);
111  };
112 
113  // Wait for time state
114  class WaitForTimeState : public GenState<SittingDemoSC>
115  {
116  public:
117  // Constants
118  static const ros::Time NullROSTime;
119 
120  // Constructor
121  WaitForTimeState(SittingDemoSC* sc, double waitTime, ros::Time endTime = NullROSTime) : GenState(sc, WAIT_FOR_TIME, "WAIT_FOR_TIME"), m_waitTime(waitTime), m_endTime(endTime) {}
122 
123  protected:
124  // State callbacks
125  virtual void activate(cycle_t cyc);
126  virtual action_t execute(cycle_t cyc);
127 
128  // State parameters
129  double m_waitTime;
130  ros::Time m_endTime;
131  };
132 
133  // Wait for motion player state
134  class WaitForMotionPlayerState : public GenState<SittingDemoSC>
135  {
136  public:
137  // Constructor
138  WaitForMotionPlayerState(SittingDemoSC *sc) : GenState(sc, WAIT_FOR_MOTION_PLAYER, "WAIT_FOR_MOTION_PLAYER") {}
139 
140  protected:
141  // State callbacks
142  virtual action_t execute(cycle_t cyc);
143  };
144 
145  // Wait for limb control state
146  class WaitForLimbControlState : public GenState<SittingDemoSC>
147  {
148  public:
149  // Constructor
150  WaitForLimbControlState(SittingDemoSC *sc) : GenState(sc, WAIT_FOR_LIMB_CONTROL, "WAIT_FOR_LIMB_CONTROL") {}
151 
152  protected:
153  // State callbacks
154  virtual action_t execute(cycle_t cyc);
155 
156  // State variables
157  const limb_control::PlayCommandsRequest PCmdReqNull;
158  };
159 
160  // Plan action state
161  class PlanDemoActionState : public GenState<SittingDemoSC>
162  {
163  public:
164  // Constructor
165  PlanDemoActionState(SittingDemoSC* sc) : GenState(sc, PLAN_DEMO_ACTION, "PLAN_DEMO_ACTION") {}
166 
167  protected:
168  // State callbacks
169  virtual action_t execute(cycle_t cyc);
170  };
171 
172  // Return to sitting state
173  class ReturnToSittingState : public GenState<SittingDemoSC>
174  {
175  public:
176  // Constructor
177  ReturnToSittingState(SittingDemoSC* sc) : GenState(sc, RETURN_TO_SITTING, "RETURN_TO_SITTING") {}
178 
179  protected:
180  // State callbacks
181  virtual void activate(cycle_t cyc);
182  virtual action_t execute(cycle_t cyc);
183 
184  // State variables
185  limb_control::PlayCommandsRequest PCmdReq;
186  };
187 
188  // Start motion playback state
189  class StartMotionPlaybackState : public GenState<SittingDemoSC>
190  {
191  public:
192  // Constructor
193  StartMotionPlaybackState(SittingDemoSC *sc, int motion) : GenState(sc, START_MOTION_PLAYBACK, "START_MOTION_PLAYBACK"), m_motion(motion) {}
194 
195  protected:
196  // State callbacks
197  virtual void activate(cycle_t cyc);
198  virtual action_t execute(cycle_t cyc);
199 
200  // State parameters
201  const int m_motion;
202  };
203 
204  // Stop motion playback state
205  class StopMotionPlaybackState : public GenState<SittingDemoSC>
206  {
207  public:
208  // Constructor
209  StopMotionPlaybackState(SittingDemoSC *sc) : GenState(sc, STOP_MOTION_PLAYBACK, "STOP_MOTION_PLAYBACK") {}
210 
211  protected:
212  // State callbacks
213  virtual action_t execute(cycle_t cyc);
214  };
215 
216  // Start leg dangling state
217  class StartLegDanglingState : public GenState<SittingDemoSC>
218  {
219  public:
220  // Constructor
221  StartLegDanglingState(SittingDemoSC *sc) : GenState(sc, START_LEG_DANGLING, "START_LEG_DANGLING") {}
222 
223  protected:
224  // State callbacks
225  virtual void activate(cycle_t cyc);
226  virtual action_t execute(cycle_t cyc);
227  };
228 
229  // Stop leg dangling state
230  class StopLegDanglingState : public GenState<SittingDemoSC>
231  {
232  public:
233  // Constructor
234  StopLegDanglingState(SittingDemoSC *sc) : GenState(sc, STOP_LEG_DANGLING, "STOP_LEG_DANGLING") {}
235 
236  protected:
237  // State callbacks
238  virtual void activate(cycle_t cyc);
239  virtual action_t execute(cycle_t cyc);
240  };
241 
242  // Start head idling state
243  class StartHeadIdlingState : public GenState<SittingDemoSC>
244  {
245  public:
246  // Constructor
247  StartHeadIdlingState(SittingDemoSC *sc) : GenState(sc, START_HEAD_IDLING, "START_HEAD_IDLING") {}
248 
249  protected:
250  // State callbacks
251  virtual void activate(cycle_t cyc);
252  virtual action_t execute(cycle_t cyc);
253  };
254 
255  // Stop head idling state
256  class StopHeadIdlingState : public GenState<SittingDemoSC>
257  {
258  public:
259  // Constructor
260  StopHeadIdlingState(SittingDemoSC *sc) : GenState(sc, STOP_HEAD_IDLING, "STOP_HEAD_IDLING") {}
261 
262  protected:
263  // State callbacks
264  virtual void activate(cycle_t cyc);
265  virtual action_t execute(cycle_t cyc);
266  };
267 }
268 
269 #endif /* SITTING_DEMO_FSM_H */
270 // EOF
Implements the State Controller Library.
Base class for all state controllers.
Definition: state_controller.h:593
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