For Evans
Embed:
(wiki syntax)
Show/hide line numbers
AccCar.cpp
00001 #include "AccCar.h" 00002 #include <stdlib.h> 00003 #include <algorithm> 00004 00005 #define TICK 1000 00006 #define MONITOR_DIST 17 00007 #define SAFETY_GAP 2 00008 00009 AccCar::AccCar(int id, Road* road, int flag) { 00010 this->id = id; 00011 this->road = road; 00012 this->flag = flag; 00013 cycle = 0; 00014 this->thread = NULL; 00015 } 00016 00017 void AccCar::set_forward_car(AccCar* car) { 00018 this->forward_car = car; 00019 } 00020 00021 void AccCar::update() { 00022 while (true) { 00023 ThisThread::sleep_for(TICK); 00024 road->go_flags.wait_all(flag); 00025 cycle++; 00026 00027 position = position + speed; 00028 00029 if (cycle % 5 == 0) { 00030 speed = rand() % 11 + 5; 00031 } 00032 00033 if (forward_car->position - position < MONITOR_DIST) { 00034 road->done_flags.wait_all(forward_car->flag, osWaitForever, false); 00035 00036 int diff = forward_car->position - position; 00037 int maxSafeSpeed = diff - SAFETY_GAP; 00038 00039 speed = std::min(maxSafeSpeed, target_speed); 00040 } else { 00041 speed = target_speed; 00042 } 00043 00044 road->done_flags.set(flag); 00045 } 00046 } 00047 00048 void AccCar::reset() { 00049 road->done_flags.clear(flag); 00050 00051 if (thread != NULL) { 00052 thread->terminate(); 00053 } 00054 00055 thread = new Thread(); 00056 thread->start( callback(this, &AccCar::update) ); 00057 00058 this->position = 0; 00059 this->speed = rand() % 11 + 5; 00060 this->target_speed = speed; 00061 } 00062 00063 void AccCar::stop() { 00064 if (thread != NULL) { 00065 thread->terminate(); 00066 } 00067 }
Generated on Wed Jul 27 2022 10:46:49 by
1.7.2