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
- Committer:
- mwgold
- Date:
- 2019-12-12
- Revision:
- 8:c40e805eecba
- Parent:
- 7:15c2ed6e5162
File content as of revision 8:c40e805eecba:
#include "AccCar.h"
#include <stdlib.h>
#include <algorithm>
#define TICK 1000
#define MONITOR_DIST 17
#define SAFETY_GAP 2
AccCar::AccCar(int id, Road* road, int flag) {
this->id = id;
this->road = road;
this->flag = flag;
waited = false;
this->thread = NULL;
}
void AccCar::set_forward_car(AccCar* car) {
this->forward_car = car;
}
void AccCar::set_target_speed(int speed) {
this->target_speed = speed;
this->speed = speed;
}
void AccCar::update() {
do {
ThisThread::sleep_for(TICK);
road->go_flags.wait_all(flag);
cycle++;
position = position + speed;
if (cycle % 5 == 0) {
target_speed = rand() % 11 + 5;
}
if (forward_car != NULL && forward_car->position > -1) {
if (forward_car->position - position < MONITOR_DIST) {
road->done_flags.wait_all(forward_car->flag, osWaitForever, false);
int diff = forward_car->position - position;
int maxSafeSpeed = diff - SAFETY_GAP;
speed = std::min(maxSafeSpeed, target_speed);
} else {
speed = target_speed;
}
} else {
speed = target_speed;
}
road->done_flags.set(flag);
} while( position < 100 );
}
void AccCar::reset() {
road->done_flags.clear(flag);
if (thread != NULL) {
thread->terminate();
}
thread = new Thread();
thread->start( callback(this, &AccCar::update) );
this->position = 0;
this->cycle = 0;
}
