Subversion Repositories programming

Rev

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