Checkpoint before trying to convert to a state machine
authorIra W. Snyder <devel@irasnyder.com>
Fri, 5 Oct 2007 23:38:27 +0000 (16:38 -0700)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 5 Oct 2007 23:38:27 +0000 (16:38 -0700)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
Makefile
elevator.cpp
elevator.hpp
stop.cpp
stop.hpp
test.cpp

index c13f766..8062d9a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-test: test.o elevator.o position.o stop.o
+test: position.o stop.o elevator.o test.o
        g++ -o $@ $^
 
 run: test
index 3be175d..f034f63 100644 (file)
@@ -10,15 +10,57 @@ Elevator::Elevator ()
        // Intentionally Left Empty
 }
 
+Elevator::Elevator (int starting_floor)
+       : door_(CLOSED)
+       , direction_(IDLE)
+       , current_position_(starting_floor)
+       , stops_()
+       , ELEVATOR_STEP(0.1)
+{
+       // Intentionally Left Empty
+}
+
 bool Elevator::currently_at_stop () const
 {
        StopList::const_iterator it;
        Stop current(current_position_, direction_);
 
+       /* Calculate the number of Stops above and below our
+        * current position */
+       int stops_above = 0;
+       int stops_below = 0;
+
+       for (it = stops_.begin(); it != stops_.end(); it++)
+       {
+               if (current < *it)
+                       ++stops_above;
+
+               if (current > *it)
+                       ++stops_below;
+       }
+
+       /* Check if we are at the top. If so, only the position needs to match */
+       if (direction_ == UP && stops_above == 0)
+       {
+               for (it = stops_.begin (); it != stops_.end (); it++)
+                       if (it->getPosition() == current_position_)
+                               return true;
+       }
+
+       /* Check if we are at the bottom. If so, only the position needs to match */
+       if (direction_ == DOWN && stops_below == 0)
+       {
+               for (it = stops_.begin (); it != stops_.end (); it++)
+                       if (it->getPosition() == current_position_)
+                               return true;
+       }
+
+       /* Check if we match exactly */
        for (it = stops_.begin (); it != stops_.end (); it++)
                if (*it == current)
                        return true;
 
+       /* No match */
        return false;
 }
 
@@ -51,6 +93,12 @@ float Elevator::distance_from (Position &pos) const
        return pos - current_position_;
 }
 
+#include <string>
+static void debug (const std::string& s)
+{
+       std::cout << s << std::endl;
+}
+
 void Elevator::move ()
 {
        static int wait = 0;
@@ -64,6 +112,7 @@ void Elevator::move ()
         */
        if (wait > 0)
        {
+               debug ("waiting");
                --wait;
                return;
        }
@@ -71,19 +120,13 @@ void Elevator::move ()
        /* close the door if it is open. This is a requirement to move */
        if (door_ != CLOSED)
        {
+               debug ("closing door");
                wait = 10;
+               door_ = CLOSED;
                close_door ();
                return;
        }
 
-       if (currently_at_stop ())
-       {
-               wait = 10;                                                                      // delay around for 10 steps
-               stops_.remove (Stop(current_position_, direction_));
-               open_door ();                                                           // call into the GUI to open the door
-               return;
-       }
-
        /* Calculate the number of Stops above and below our
         * current position */
        StopList::const_iterator it;
@@ -91,7 +134,7 @@ void Elevator::move ()
        int stops_above = 0;
        int stops_below = 0;
 
-       for (it = stops_.begin(); it != stops_.begin(); it++)
+       for (it = stops_.begin(); it != stops_.end(); it++)
        {
                if (current < *it)
                        ++stops_above;
@@ -100,15 +143,59 @@ void Elevator::move ()
                        ++stops_below;
        }
 
+       /* Check if we are currently at a stop */
+       if (currently_at_stop ())
+       {
+               std::cout << "At A Stop: " << Stop(current_position_, direction_) << std::endl;
+               std::cout << "above=" << stops_above << " below=" << stops_below << std::endl;
+               wait = 10;
+
+               /* Remove all stops here if we are switching direction */
+               if (stops_above == 0 && direction_ == UP)
+                       stops_.remove (Stop(current_position_, ALL));
+
+               if (stops_below == 0 && direction_ == DOWN)
+                       stops_.remove (Stop(current_position_, ALL));
+
+               stops_.remove (Stop(current_position_, direction_));
+
+               /* Open the door */
+               door_ = OPEN;
+               open_door ();
+
+               return;
+       }
+
        /* Check if we need to change direction */
-       if (direction_ == UP && stops_above == 0 && stops_below > 0)
+       if (stops_above == 0 && stops_below > 0 && direction_ == UP)
+       {
+               debug ("1: DOWN");
                direction_ = DOWN;
+       }
 
-       if (direction_ == DOWN && stops_below == 0 && stops_above > 0)
+       if (stops_below == 0 && stops_above > 0 && direction_ == DOWN)
+       {
+               debug ("2: UP");
                direction_ = UP;
+       }
 
-       if (stops_above == 0 && stops_below == 0)
+       if (stops_above == 0 && stops_below == 0 && direction_ != IDLE)
+       {
+               debug ("3: IDLE");
                direction_ = IDLE;
+       }
+
+       if (direction_ == IDLE && stops_above > 0)
+       {
+               debug ("4: UP");
+               direction_ = UP;
+       }
+
+       if (direction_ == IDLE && stops_below > 0)
+       {
+               debug ("5: DOWN");
+               direction_ = DOWN;
+       }
 
 
        /* Move in the correct direction */
@@ -138,7 +225,7 @@ bool Elevator::is_idle () const
 
 void Elevator::update_position () const
 {
-       std::cout << "Updating the GUI with our position" << std::endl;
+       std::cout << "Updating the GUI with our position: " << current_position_ << std::endl;
 }
 
 void Elevator::open_door () const
index 1b5c599..6f8b5a3 100644 (file)
@@ -20,6 +20,7 @@ class Elevator
 {
        public:
                Elevator ();
+               Elevator (int starting_floor);
 
                void stop_at (Stop &stop);
                float distance_from (Position& pos) const;
index 620a6e1..d4cb7f7 100644 (file)
--- a/stop.cpp
+++ b/stop.cpp
@@ -22,7 +22,7 @@ bool Stop::operator< (const Stop& rhs) const
 {
        /* If we do not use the direction to help differentiate, then it is
         * possible that an object can be neither less, greater, or equal */
-       return ((position_ < rhs.position_) || (direction_ < rhs.direction_));
+       return (position_ < rhs.position_);
 }
 
 bool Stop::operator> (const Stop& rhs) const
@@ -35,9 +35,35 @@ const Direction Stop::getDirection () const
        return direction_;
 }
 
+const Position Stop::getPosition () const
+{
+       return position_;
+}
+
 std::ostream& operator<< (std::ostream& os, const Stop& rhs)
 {
-       os << "Stop(" << rhs.position_ << ", " << rhs.direction_ << ")";
+       os << "Stop(" << rhs.position_ << ", "; // << rhs.direction_ << ")";
+
+       switch (rhs.direction_)
+       {
+               case IDLE:
+                       os << "IDLE";
+                       break;
+               case UP:
+                       os << "UP";
+                       break;
+               case DOWN:
+                       os << "DOWN";
+                       break;
+               case ALL:
+                       os << "ALL";
+                       break;
+               default:
+                       os << "UNHANDLED";
+                       break;
+       }
+
+       os << ")";
        return os;
 }
 
index 851c691..05f672a 100644 (file)
--- a/stop.hpp
+++ b/stop.hpp
@@ -22,6 +22,7 @@ class Stop
                bool operator> (const Stop& rhs) const;
 
                const Direction getDirection () const;
+               const Position getPosition () const;
 
        private:
                Position        position_;
index a7e65d9..bcc17b9 100644 (file)
--- a/test.cpp
+++ b/test.cpp
@@ -9,10 +9,147 @@ using namespace std;
 
 int main (int argc, char *argv[])
 {
-       Elevator e;
+       Elevator e(2);
 
-       Stop s(1, UP);
-       e.stop_at (s);
+       Stop s2(3, DOWN);
+       e.stop_at (s2);
+
+       Stop s3(1, UP);
+       e.stop_at (s3);
+
+       Stop s4(4, DOWN);
+       e.stop_at (s4);
+
+
+
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
+       e.move ();
        e.move ();
        e.move ();
        e.move ();