Use Finite State Machine to manage Elevator movement
[cs356-p1-elevator.git] / elevator.hpp
index d78ecd8..df8a228 100644 (file)
@@ -8,78 +8,57 @@
 #define ELEVATOR_HPP
 
 #include <iostream>
-//#include <set>
-#include <vector>
-#include "position.hpp"
+#include <list>
 #include "direction.hpp"
+#include "position.hpp"
 #include "stop.hpp"
 
+typedef std::list<Stop> StopList;
 
-enum door_status { CLOSED, OPEN };
-const int elevator_step = 0.1;
-
-class bad_direction { };
+enum State { STATE_IDLE, STATE_UP, STATE_DOWN, STATE_WAIT, STATE_OPEN_DOOR, STATE_CLOSE_DOOR };
+enum Event { EVT_IDLE, EVT_UP, EVT_DOWN, EVT_WAIT, EVT_OPEN_DOOR, EVT_CLOSE_DOOR };
 
 class Elevator
 {
        public:
-               /*
-                * PURPOSE: Construct a new Elevator object
-                *
-                * REQUIRE: Nothing
-                *
-                * PROMISE: A new Elevator will be constructed
-                */
                Elevator ();
+               Elevator (int starting_floor);
 
-               /*
-                * PURPOSE: Tell the elevator to stop at the given floor,
-                * PURPOSE: going in the given direction.
-                *
-                * REQUIRE: floor is a valid floor
-                * REQUIRE: direction is a valid direction
-                *
-                * PROMISE: The elevator will stop at the floor when it gets there
-                */
-               void stop_at (int floor, enum direction _direction);
-
-               /*
-                * PURPOSE: The elevator will move 1/10th of a floor in the current
-                * PURPOSE: direction.
-                *
-                * REQUIRE: Nothing
-                *
-                * PROMISE: The elevator will move if it has floors to stop at, otherwise
-                * PROMISE: it will sit idle at its current place.
-                */
+               void stop_at (Stop &stop);
+               float distance_from (Position& pos) const;
                void move ();
-
-       protected:
-               /*
-                * PURPOSE: Find the direction we should move in
-                *
-                * REQUIRE: _direction must be IDLE
-                *
-                * PROMISE: The best direction to move will be returned
-                */
-               enum direction find_best_direction ();
+               bool is_idle () const;
 
        private:
-               /* Storage for all of the places that we will be stopping */
-               //std::set<Stop> _stops;
-               std::vector<Stop> _stops;
 
-               /* Storage for the current elevator position */
-               Position _pos;
+               /* State Machine: Helper Functions */
+               Event find_next_event () const;
 
-               /* Stores the current direction */
-               enum direction _direction;
+               /* State Machine: Transition Functions */
+               void transition_move_up ();
+               void transition_move_down ();
+               void transition_move_idle ();
+               void transition_open_door ();
+               void transition_close_door ();
+               void transition_begin_wait ();
+               void transition_continue_wait ();
 
-               /* Stores the current door status */
-               enum door_status _door_status;
+               /* Analyze the list of stops */
+               bool currently_at_stop () const;
 
+               /* Elevator Status Variables */
+               Direction       direction_;
+               Position        position_;
+               StopList        stops_;
+
+               /* State Machine */
+               State state_;
+               int wait_;
+
+               static const float ELEVATOR_STEP = 0.1;
 };
 
+
 #endif /* ELEVATOR_HPP */
 
 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */