Subversion Repositories programming

Rev

Rev 329 | Rev 332 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
321 ira 1
 
2
#include "controller.h"
3
 
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
5
{
6
    assert (num_floors > 2);
7
    assert (num_elevators > 0);
8
 
328 ira 9
    elevators = std::vector<Elevator> (num_elevators, Elevator (num_floors));
331 ira 10
 
11
    for (int i=0; i<num_elevators; i++)
12
        elevators[i].set_controller (this);
321 ira 13
}
14
 
15
Controller::~Controller ()
16
{
17
}
18
 
329 ira 19
void Controller::request_elevator (int on_floor, int direction)
321 ira 20
{
328 ira 21
    assert (on_floor <= num_floors);
22
    assert (on_floor > 0);
331 ira 23
    assert (direction == MOVE_UP || direction == MOVE_DOWN);
328 ira 24
 
25
    float distance = INT_MAX;
26
    int elevator_number;
27
 
28
    // check that there is not already an elevator requested
29
    // to this floor. If there is already one, ignore this
30
    // request.
31
    if (floor_already_requested (on_floor))
32
        return;
33
 
34
    // find elevator that is closest, AND NOT moving away
35
    // "push" it's button
36
    elevator_number = find_closest_elevator (on_floor);
37
    elevators[elevator_number].push_button (on_floor);
321 ira 38
}
39
 
328 ira 40
/**
41
 * Check if there is an elevator already in the queue to stop
42
 * at the floor given as a parameter.
43
 */
44
bool Controller::floor_already_requested (int on_floor)
45
{
46
    int i;
47
 
48
    for (i=0; i<num_elevators; i++)
49
        if (elevators[i].button_is_pushed (on_floor))
50
            return true;
51
 
52
    return false;
53
}
54
 
55
/**
56
 * Return the number of the closest elevator to the
57
 * floor given as a parameter.
58
 *
59
 * We only want to choose elevators that are close and
60
 * heading in the right direction, or the closest elevator
61
 * if it is sitting idle.
62
 */
63
int Controller::find_closest_elevator (int to_floor)
64
{
65
    int i;
66
    float distance = INT_MAX;
67
    float temp_distance;
68
    int answer;
69
 
70
    float cur_floor;
71
    int direction;
72
 
73
    for (i=0; i<num_elevators; i++)
74
    {
75
        cur_floor = elevators[i].get_current_floor ();
76
        direction = elevators[i].get_direction ();
77
        temp_distance = fabsf (cur_floor - to_floor);
78
 
79
        // Check that this elevator is closer
80
        if (temp_distance < distance)
81
        {
82
            // If we are below the floor we want to go to and we
83
            // are moving up, then this is an ok choice
84
            if (cur_floor < to_floor && direction == MOVE_UP)
85
            {
86
                answer = i;
87
                distance = temp_distance;
88
            }
89
            // If we are above the floor we want to go to and we
90
            // are moving down, then this is an ok choice
91
            else if (cur_floor > to_floor && direction == MOVE_DOWN)
92
            {
93
                answer = i;
94
                distance = temp_distance;
95
            }
96
            // If we are closest, but idle, then this is an ok choice
97
            else
98
            {
99
                answer = i;
100
                distance = temp_distance;
101
            }
102
        }
103
    }
104
 
105
    return answer;
106
}
107
 
329 ira 108
void Controller::disable_elevator (int elevator_number)
109
{
110
}
111
 
112
void Controller::enable_elevator (int elevator_number)
113
{
114
    // make sure elevator is enabled first
115
}
116
 
331 ira 117
void Controller::start_all_elevators ()
118
{
119
    int i;
120
 
121
    for (i=0; i<num_elevators; i++)
122
        elevators[i].thread_start ();
123
}
124
 
125
void Controller::stop_all_elevators ()
126
{
127
    int i;
128
 
129
    for (i=0; i<num_elevators; i++)
130
        elevators[i].thread_stop ();
131
}
132
 
133
void Controller::pause_all_elevators ()
134
{
135
    int i;
136
 
137
    for (i=0; i<num_elevators; i++)
138
        elevators[i].thread_pause ();
139
}
140
 
141
void Controller::unpause_all_elevators ()
142
{
143
    int i;
144
 
145
    for (i=0; i<num_elevators; i++)
146
        elevators[i].thread_unpause ();
147
}
148