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 19... Line 19...
19
    int i;
19
    int i;
20
 
20
 
21
    this->terminate = false;
21
    this->terminate = false;
22
    this->pause = false;
22
    this->pause = false;
23
    this->direction = IDLE;
23
    this->direction = IDLE;
24
    this->current_floor = 1.0;
24
    this->current_floor = 0.0;
25
    this->controller = NULL;
25
    this->controller = NULL;
26
 
26
 
27
    this->stop_at_floors.reserve (num_floors+1);
27
    this->stop_at_floors.reserve (num_floors);
28
 
28
 
29
    for (i=0; i<num_floors+1; i++)
29
    for (i=0; i<num_floors; i++)
30
        this->stop_at_floors.push_back(i);
30
        this->stop_at_floors.push_back(bool(false));
31
 
31
 
32
    printf ("created elevator 0x%x-- numfloors=%d\n", this, this->num_floors);
32
    printf ("created elevator 0x%x-- numfloors=%d\n", this, this->num_floors);
33
}
33
}
34
 
34
 
35
Elevator::~Elevator ()
35
Elevator::~Elevator ()
Line 50... Line 50...
50
int Elevator::floors_above_current ()
50
int Elevator::floors_above_current ()
51
{
51
{
52
    int i = ((int)current_floor) + 1;
52
    int i = ((int)current_floor) + 1;
53
    int count = 0;
53
    int count = 0;
54
 
54
 
55
    for (/* see above */; i<num_floors+1; i++)
55
    for (/* see above */; i<num_floors; i++)
56
        if (stop_at_floors[i])
56
        if (stop_at_floors[i])
57
            count++;
57
            count++;
58
 
58
 
59
    return count;
59
    return count;
60
}
60
}
Line 80... Line 80...
80
    return count;
80
    return count;
81
}
81
}
82
 
82
 
83
void Elevator::push_button (int floor)
83
void Elevator::push_button (int floor)
84
{
84
{
85
    assert (floor <= num_floors && floor > 0);
85
    assert (floor < num_floors);
-
 
86
    assert (floor >= 0);
86
 
87
 
87
    this->stop_at_floors[floor] = true;
88
    this->stop_at_floors[floor] = true;
88
}
89
}
89
 
90
 
90
/**
91
/**
Line 114... Line 115...
114
    }
115
    }
115
}
116
}
116
 
117
 
117
void Elevator::thread_start ()
118
void Elevator::thread_start ()
118
{
119
{
119
    boost::thread t1 (boost::bind (this->thread_entry, this));
120
    boost::thread the_thread (boost::bind (this->thread_entry, this));
120
}
121
}
121
 
122
 
122
void Elevator::run_elevator_logic ()
123
void Elevator::run_elevator_logic ()
123
{
124
{
124
    int i;
125
    int i;
Line 144... Line 145...
144
        case MOVE_UP:
145
        case MOVE_UP:
145
 
146
 
146
            /* Decide if we should stop at the floor we're at */
147
            /* Decide if we should stop at the floor we're at */
147
            if (should_stop_at_current_floor ())
148
            if (should_stop_at_current_floor ())
148
            {
149
            {
-
 
150
#ifndef QUIET
149
                debug_puts ("stop at floor -- while going up");
151
                printf ("stop at floor %d -- while going up\n", (int)(current_floor+0.5));
-
 
152
#endif
150
                stop_at_floors[(int)(current_floor+0.5)] = false;
153
                stop_at_floors[(int)(current_floor+0.5)] = false;
151
                have_user_enter_buttons ();
154
                have_user_enter_buttons ();
152
            }
155
            }
153
 
156
 
154
            /* Decide to sit idle if we have no more floors to stop at */
157
            /* Decide to sit idle if we have no more floors to stop at */
Line 168... Line 171...
168
            /* Since nothing else applies, we should keep moving in the
171
            /* Since nothing else applies, we should keep moving in the
169
             * current direction, up. */
172
             * current direction, up. */
170
            else
173
            else
171
            {
174
            {
172
                current_floor += ELEVATOR_TIME_MOVE_AMOUNT;
175
                current_floor += ELEVATOR_TIME_MOVE_AMOUNT;
173
                debug_puts ("moving up");
176
                //debug_puts ("moving up");
174
 
177
 
175
                // TODO: Add a check here to make sure we don't go too high
178
                // TODO: Add a check here to make sure we don't go too high
176
 
179
 
177
            }
180
            }
178
 
181
 
Line 181... Line 184...
181
        case MOVE_DOWN:
184
        case MOVE_DOWN:
182
 
185
 
183
            /* Decide if we should stop at the floor we're at */
186
            /* Decide if we should stop at the floor we're at */
184
            if (should_stop_at_current_floor ())
187
            if (should_stop_at_current_floor ())
185
            {
188
            {
-
 
189
#ifndef QUIET
186
                debug_puts ("stop at floor -- while going down");
190
                printf ("stop at floor %d -- while going down\n", (int)(current_floor+0.5));
-
 
191
#endif
187
                stop_at_floors[(int)(current_floor)] = false;
192
                stop_at_floors[(int)(current_floor+0.5)] = false;
188
                have_user_enter_buttons ();
193
                have_user_enter_buttons ();
189
            }
194
            }
190
 
195
 
191
            /* Decide to sit idle if we have no more floors to stop at */
196
            /* Decide to sit idle if we have no more floors to stop at */
192
            if (!has_floors_to_stop_at ())
197
            if (!has_floors_to_stop_at ())
Line 205... Line 210...
205
            /* Since nothing else applies, we should keep moving in the
210
            /* Since nothing else applies, we should keep moving in the
206
             * current direction, down. */
211
             * current direction, down. */
207
            else
212
            else
208
            {
213
            {
209
                current_floor -= ELEVATOR_TIME_MOVE_AMOUNT;
214
                current_floor -= ELEVATOR_TIME_MOVE_AMOUNT;
210
                debug_puts ("moving down");
215
                //debug_puts ("moving down");
211
 
216
 
212
                // TODO: Add a check here to make sure we don't go too low
217
                // TODO: Add a check here to make sure we don't go too low
213
 
218
 
214
            }
219
            }
215
 
220
 
Line 248... Line 253...
248
 
253
 
249
bool Elevator::near_a_floor ()
254
bool Elevator::near_a_floor ()
250
{
255
{
251
    int i;
256
    int i;
252
 
257
 
253
    for (i=1; i<=num_floors; i++)
258
    for (i=0; i<num_floors; i++)
254
        if (near_floor (i))
259
        if (near_floor (i))
255
            return true;
260
            return true;
256
 
261
 
257
    return false;
262
    return false;
258
}
263
}
Line 271... Line 276...
271
 
276
 
272
bool Elevator::has_floors_to_stop_at ()
277
bool Elevator::has_floors_to_stop_at ()
273
{
278
{
274
    int i;
279
    int i;
275
 
280
 
276
    for (i=1; i<=num_floors; i++)
281
    for (i=0; i<num_floors; i++)
277
        if (stop_at_floors[i])
282
        if (stop_at_floors[i])
278
            return true;
283
            return true;
279
 
284
 
280
    return false;
285
    return false;
281
}
286
}
282
 
287
 
283
bool Elevator::should_stop_at_current_floor ()
288
bool Elevator::should_stop_at_current_floor ()
284
{
289
{
285
    int i;
290
    int i;
286
 
291
 
287
    for (i=1; i<=num_floors; i++)
292
    for (i=0; i<num_floors; i++)
288
        if (near_floor (i) && stop_at_floors[i] == true)
293
        if (near_floor (i) && stop_at_floors[i] == true)
289
            return true;
294
            return true;
290
 
295
 
291
    return false;
296
    return false;
292
}
297
}
293
 
298
 
294
bool Elevator::button_is_pushed (int floor)
299
bool Elevator::button_is_pushed (int floor)
295
{
300
{
296
    assert (floor <= num_floors);
301
    assert (floor < num_floors);
297
    assert (floor > 0);
302
    assert (floor >= 0);
298
 
303
 
299
    return stop_at_floors[floor];
304
    return stop_at_floors[floor];
300
}
305
}
301
 
306
 
302
float Elevator::get_current_floor ()
307
float Elevator::get_current_floor ()
Line 310... Line 315...
310
}
315
}
311
 
316
 
312
void Elevator::have_user_enter_buttons ()
317
void Elevator::have_user_enter_buttons ()
313
{
318
{
314
    controller->pause_all_elevators ();
319
    controller->pause_all_elevators ();
315
    sleep (1);
320
    usleep (10000);
316
 
321
 
317
    std::string s;
322
    std::string s;
318
 
323
 
319
    std::cout << "Enter floors to stop at for elevator (space seperated): ";
324
    std::cout << "Enter floors to stop at for elevator (space seperated): ";
320
    getline (std::cin, s);
325
    getline (std::cin, s);
Line 326... Line 331...
326
    int i, val;
331
    int i, val;
327
    for (i=0; i<SplitVec.size(); i++)
332
    for (i=0; i<SplitVec.size(); i++)
328
    {
333
    {
329
        val = atoi (SplitVec[i].c_str());
334
        val = atoi (SplitVec[i].c_str());
330
 
335
 
-
 
336
        // Ignore values that are outside of range
331
        if (val > 0 && val <= num_floors)
337
        if (val >= 0 && val < num_floors)
332
            this->push_button (val);
338
            this->push_button (val);
333
    }
339
    }
334
 
340
 
335
    controller->unpause_all_elevators ();
341
    controller->unpause_all_elevators ();
336
}
342
}
337
 
343
 
-
 
344