Subversion Repositories programming

Rev

Rev 332 | Rev 335 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 332 Rev 334
Line 4... Line 4...
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
5
{
5
{
6
    assert (num_floors > 2);
6
    assert (num_floors > 2);
7
    assert (num_elevators > 0);
7
    assert (num_elevators > 0);
8
 
8
 
9
    elevators.reserve (num_elevators);
9
    elevator.reserve (num_elevators);
10
 
10
 
11
    for (int i=0; i<num_elevators; i++)
11
    for (int i=0; i<num_elevators; i++)
12
    {
12
    {
13
        elevators.push_back (Elevator (num_floors));
13
        elevator.push_back (new Elevator (num_floors));
14
        elevators[i].set_controller (this);
14
        elevator.at(i) -> set_controller (this);
15
    }
15
    }
16
}
16
}
17
 
17
 
18
Controller::~Controller ()
18
Controller::~Controller ()
19
{
19
{
20
}
20
}
21
 
21
 
22
void Controller::request_elevator (int on_floor, int direction)
22
void Controller::request_elevator (int on_floor, int direction)
23
{
23
{
24
    assert (on_floor <= num_floors);
24
    assert (on_floor < num_floors);
25
    assert (on_floor > 0);
25
    assert (on_floor >= 0);
26
    assert (direction == MOVE_UP || direction == MOVE_DOWN);
26
    assert (direction == MOVE_UP || direction == MOVE_DOWN);
27
 
27
 
28
    float distance = INT_MAX;
28
    float distance = INT_MAX;
29
    int elevator_number;
29
    int elevator_number;
30
 
30
 
Line 32... Line 32...
32
    // to this floor. If there is already one, ignore this
32
    // to this floor. If there is already one, ignore this
33
    // request.
33
    // request.
34
    if (floor_already_requested (on_floor))
34
    if (floor_already_requested (on_floor))
35
        return;
35
        return;
36
 
36
 
-
 
37
    puts("2");
-
 
38
 
37
    // find elevator that is closest, AND NOT moving away
39
    // find elevator that is closest, AND NOT moving away
38
    // "push" it's button
40
    // "push" it's button
39
    elevator_number = find_closest_elevator (on_floor, direction);
41
    elevator_number = find_closest_elevator (on_floor, direction);
40
    elevators[elevator_number].push_button (on_floor);
42
    elevator.at(elevator_number) -> push_button (on_floor);
41
 
43
 
42
    printf ("elevator[%d] gets queued for floor: %d\n", elevator_number, on_floor);
44
    printf ("elevator[%d] gets queued for floor: %d\n", elevator_number, on_floor);
43
}
45
}
44
 
46
 
45
/**
47
/**
Line 49... Line 51...
49
bool Controller::floor_already_requested (int on_floor)
51
bool Controller::floor_already_requested (int on_floor)
50
{
52
{
51
    int i;
53
    int i;
52
 
54
 
53
    for (i=0; i<num_elevators; i++)
55
    for (i=0; i<num_elevators; i++)
54
        if (elevators[i].button_is_pushed (on_floor))
56
        if (elevator.at(i) -> button_is_pushed (on_floor))
55
            return true;
57
            return true;
56
 
58
 
57
    return false;
59
    return false;
58
}
60
}
59
 
61
 
Line 75... Line 77...
75
    float cur_floor;
77
    float cur_floor;
76
    int direction;
78
    int direction;
77
 
79
 
78
    for (i=0; i<num_elevators; i++)
80
    for (i=0; i<num_elevators; i++)
79
    {
81
    {
80
        cur_floor = elevators[i].get_current_floor ();
82
        cur_floor = elevator.at(i) -> get_current_floor ();
81
        direction = elevators[i].get_direction ();
83
        direction = elevator.at(i) -> get_direction ();
82
        temp_distance = fabsf (cur_floor - to_floor);
84
        temp_distance = fabsf (cur_floor - to_floor);
83
 
85
 
-
 
86
        printf ("cur_floor=%e -- direc=%d -- t_dist=%e\n", cur_floor, direction, temp_distance);
-
 
87
 
84
        // Automatically discard elevators that are moving in the wrong
88
        // Automatically discard elevators that are moving in the wrong
85
        // direction, but DO consider ones that are idle.
89
        // direction, but DO consider ones that are idle.
-
 
90
        if (direction == MOVE_DOWN && in_direction == MOVE_UP)
-
 
91
            continue;
-
 
92
 
86
        if (direction != IDLE || direction != in_direction)
93
        if (direction == MOVE_UP && in_direction == MOVE_DOWN)
87
            continue;
94
            continue;
88
 
95
 
89
        // Check that this elevator is closer
96
        // Check that this elevator is closer
90
        if (temp_distance < distance)
97
        if (temp_distance < distance)
91
        {
98
        {
Line 127... Line 134...
127
void Controller::start_all_elevators ()
134
void Controller::start_all_elevators ()
128
{
135
{
129
    int i;
136
    int i;
130
 
137
 
131
    for (i=0; i<num_elevators; i++)
138
    for (i=0; i<num_elevators; i++)
132
        elevators[i].thread_start ();
139
        elevator.at(i) -> thread_start ();
133
}
140
}
134
 
141
 
135
void Controller::stop_all_elevators ()
142
void Controller::stop_all_elevators ()
136
{
143
{
137
    int i;
144
    int i;
138
 
145
 
139
    for (i=0; i<num_elevators; i++)
146
    for (i=0; i<num_elevators; i++)
140
        elevators[i].thread_stop ();
147
        elevator.at(i) -> thread_stop ();
141
}
148
}
142
 
149
 
143
void Controller::pause_all_elevators ()
150
void Controller::pause_all_elevators ()
144
{
151
{
145
    int i;
152
    int i;
146
 
153
 
147
    for (i=0; i<num_elevators; i++)
154
    for (i=0; i<num_elevators; i++)
148
        elevators[i].thread_pause ();
155
        elevator.at(i) -> thread_pause ();
149
}
156
}
150
 
157
 
151
void Controller::unpause_all_elevators ()
158
void Controller::unpause_all_elevators ()
152
{
159
{
153
    int i;
160
    int i;
154
 
161
 
155
    for (i=0; i<num_elevators; i++)
162
    for (i=0; i<num_elevators; i++)
156
        elevators[i].thread_unpause ();
163
        elevator.at(i) -> thread_unpause ();
157
}
164
}
158
 
165
 
-
 
166