1 #include "elevator.hpp"
10 // Intentionally Left Empty
13 Elevator::Elevator (int starting_floor)
17 , position_(starting_floor)
20 // Intentionally Left Empty
23 bool Elevator::currently_at_stop () const
25 StopList::const_iterator it;
26 Stop current(position_, direction_);
28 /* Calculate the number of Stops above and below our current position */
32 for (it = stops_.begin(); it != stops_.end(); it++)
41 /* Check if we are at the top. If so, only the position needs to match */
42 if (direction_ == UP && stops_above == 0)
44 for (it = stops_.begin (); it != stops_.end (); it++)
45 if (it->getPosition() == position_)
49 /* Check if we are at the bottom. If so, only the position needs to match */
50 if (direction_ == DOWN && stops_below == 0)
52 for (it = stops_.begin (); it != stops_.end (); it++)
53 if (it->getPosition() == position_)
57 /* Check if we match exactly */
58 for (it = stops_.begin (); it != stops_.end (); it++)
66 void Elevator::stop_at (Stop &stop)
68 StopList::iterator it;
70 /* If this is an "ALL" Stop, it supercedes all others */
71 if (stop.getDirection() == ALL)
74 stops_.push_back (stop);
78 /* Check if this stop already exists. If so, just leave */
79 for (it = stops_.begin(); it != stops_.end(); it++)
83 /* The stop did not already exist, so add it */
84 stops_.push_back (stop);
87 float Elevator::distance_from (Position &pos) const
90 return position_ - pos;
92 return pos - position_;
95 void Elevator::transition_move_up ()
98 position_ += ELEVATOR_STEP;
100 // TODO: Call into the GUI to update the position
101 std::cout << "Updating the GUI with our position: " << position_ << std::endl;
104 void Elevator::transition_move_down ()
107 position_ -= ELEVATOR_STEP;
109 // TODO: Call into the GUI to update the position
110 std::cout << "Updating the GUI with our position: " << position_ << std::endl;
113 void Elevator::transition_move_idle ()
116 // do not change position while IDLE
119 void Elevator::transition_open_door ()
121 /* Calculate the number of Stops above and below our
122 * current position */
123 StopList::const_iterator it;
124 Stop current = Stop(position_, direction_);
128 for (it = stops_.begin(); it != stops_.end(); it++)
137 /* If we are going to switch direction, clear all stops here,
138 * regardless of direction.
140 * Otherwise, just clear this stop */
141 if (direction_ == UP && stops_above == 0)
142 stops_.remove (Stop(position_, ALL));
143 else if (direction_ == DOWN && stops_below == 0)
144 stops_.remove (Stop(position_, ALL));
146 stops_.remove (Stop(position_, direction_));
148 // TODO: Call into GUI to open the door
149 std::cout << "Opening Door" << std::endl;
152 void Elevator::transition_close_door ()
154 // TODO: Call into the GUI to close the door
155 std::cout << "Closing Door" << std::endl;
158 void Elevator::transition_begin_wait ()
163 void Elevator::transition_continue_wait ()
169 static void debug (const std::string& s)
171 std::cout << s << std::endl;
174 static std::string get_state_name (State s)
181 sname = "STATE_IDLE";
187 sname = "STATE_DOWN";
190 sname = "STATE_WAIT";
192 case STATE_OPEN_DOOR:
193 sname = "STATE_OPEN_DOOR";
195 case STATE_CLOSE_DOOR:
196 sname = "STATE_CLOSE_DOOR";
206 static std::string get_event_name (Event e)
225 ename = "EVT_OPEN_DOOR";
228 ename = "EVT_CLOSE_DOOR";
238 static void bad_transition (State s, Event e)
240 std::cout << "Bad State Transition: " << get_state_name (s)
241 << " -> " << get_event_name (e) << std::endl;
244 Event Elevator::find_next_event () const
246 /* Calculate the number of Stops above and below our
247 * current position */
248 StopList::const_iterator it;
249 Stop current = Stop(position_, direction_);
253 for (it = stops_.begin(); it != stops_.end(); it++)
262 /* Now figure out which state transition to make */
267 if (currently_at_stop ())
268 return EVT_OPEN_DOOR;
281 if (currently_at_stop ())
282 return EVT_OPEN_DOOR;
289 if (currently_at_stop ())
290 return EVT_OPEN_DOOR;
300 return EVT_CLOSE_DOOR;
303 case STATE_OPEN_DOOR:
308 case STATE_CLOSE_DOOR:
310 if (currently_at_stop ())
311 return EVT_OPEN_DOOR;
313 if (direction_ == UP && stops_above > 0)
316 if (direction_ == DOWN && stops_below > 0)
319 /* We need to switch directions */
320 if (direction_ == UP && stops_above == 0 && stops_below > 0)
323 if (direction_ == DOWN && stops_below == 0 && stops_above > 0)
330 std::cout << "find_next_event(): Bad State" << std::endl;
335 void Elevator::move ()
337 /* Generate Events */
338 Event e = find_next_event ();
340 std::cout << "State Transition: " << get_state_name (state_) << " with "
341 << get_event_name (e) << std::endl;
350 transition_move_up ();
354 transition_move_down ();
358 transition_move_idle ();
361 state_ = STATE_OPEN_DOOR;
362 transition_open_door ();
365 bad_transition (state_, e);
375 transition_move_up ();
378 state_ = STATE_OPEN_DOOR;
379 transition_open_door ();
382 bad_transition (state_, e);
392 transition_move_down ();
395 state_ = STATE_OPEN_DOOR;
396 transition_open_door ();
399 bad_transition (state_, e);
409 transition_continue_wait ();
412 state_ = STATE_CLOSE_DOOR;
413 transition_close_door ();
416 bad_transition (state_, e);
421 case STATE_OPEN_DOOR:
426 transition_begin_wait ();
429 bad_transition (state_, e);
434 case STATE_CLOSE_DOOR:
438 state_ = STATE_OPEN_DOOR;
439 transition_open_door ();
443 transition_move_up ();
447 transition_move_down ();
451 transition_move_idle ();
454 bad_transition (state_, e);
460 std::cout << "Bad State: " << get_state_name (state_) << std::endl;
465 bool Elevator::is_idle () const
467 return direction_ == IDLE;
470 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */