Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 00014 waited = false; 00015 00016 this->thread = NULL; 00017 } 00018 00019 void AccCar::set_forward_car(AccCar* car) { 00020 this->forward_car = car; 00021 } 00022 00023 void AccCar::set_target_speed(int speed) { 00024 this->target_speed = speed; 00025 this->speed = speed; 00026 } 00027 00028 void AccCar::update() { 00029 do { 00030 ThisThread::sleep_for(TICK); 00031 road->go_flags.wait_all(flag); 00032 cycle++; 00033 00034 position = position + speed; 00035 00036 if (cycle % 5 == 0) { 00037 target_speed = rand() % 11 + 5; 00038 } 00039 00040 if (forward_car != NULL && forward_car->position > -1) { 00041 if (forward_car->position - position < MONITOR_DIST) { 00042 road->done_flags.wait_all(forward_car->flag, osWaitForever, false); 00043 00044 int diff = forward_car->position - position; 00045 int maxSafeSpeed = diff - SAFETY_GAP; 00046 00047 speed = std::min(maxSafeSpeed, target_speed); 00048 } else { 00049 speed = target_speed; 00050 } 00051 } else { 00052 speed = target_speed; 00053 } 00054 00055 road->done_flags.set(flag); 00056 } while( position < 100 ); 00057 } 00058 00059 void AccCar::reset() { 00060 road->done_flags.clear(flag); 00061 00062 if (thread != NULL) { 00063 thread->terminate(); 00064 } 00065 00066 thread = new Thread(); 00067 thread->start( callback(this, &AccCar::update) ); 00068 00069 this->position = 0; 00070 this->cycle = 0; 00071 }
Generated on Wed Jul 13 2022 14:30:32 by
1.7.2