-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.cpp
More file actions
274 lines (214 loc) · 7.46 KB
/
Copy pathcar.cpp
File metadata and controls
274 lines (214 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "car.h"
#include "road.h"
#include <iostream>
#include <ctime>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
Car::Car(int id, Road* start_road, mqtt::async_client* client) {
this->id = id;
active = 0;
time = 0;
clock = start_road->clock;
this->start_road = start_road;
this->current_road = start_road;
this->client = client;
position = -1.0f;
prevPosition = -1.0f;
targetSpeed = 0.0f;
prevSpeed = 0.0f;
speed = 0.0f;
timeWaitedToEnter = 0;
forwardCar = NULL;
pthread_mutex_init(&positionMutex, NULL);
pthread_cond_init(&positionCond, NULL);
pthread_mutex_init(&speedMutex, NULL);
pthread_cond_init(&speedCond, NULL);
clock->registerExternalEvent();
}
void Car::start() {
active = 2;
pthread_create(&thread, NULL, Car::threadHelper, this);
}
float Car::getPosition(int time) {
float position = -1.0f;
pthread_mutex_lock(&positionMutex);
if(time == this->time) {
position = this->position;
} else if(time == this->time - 1) {
position = this->prevPosition;
} else if(time == this->time+1) {
pthread_cond_wait(&positionCond, &positionMutex);
position = this->position;
} else{
printf("This should never happen. Requested position for time %d but Car %d is at time %d\n", time, id, this->time);
}
pthread_mutex_unlock(&positionMutex);
return position;
}
void Car::setSpeed(float s) {
pthread_mutex_lock(&speedMutex);
this->speed = s;
pthread_mutex_unlock(&speedMutex);
}
float Car::getSpeed(int time) {
float speed = -1.0f;
pthread_mutex_lock(&speedMutex);
if(time == this->time) {
speed = this->speed;
} else if(time == this->time - 1) {
speed = this->prevSpeed;
} else if(time == this->time+1) {
pthread_cond_wait(&speedCond, &speedMutex);
speed = this->speed;
} else{
printf("This should never happen. Requested speed for time %d but Car %d is at time %d\n", time, id, this->time);
}
pthread_mutex_unlock(&speedMutex);
return speed;
}
void Car::cycle() {
srand(std::time(NULL));
time = clock->getTime();
enterRoad();
drive();
clock->waitForTick(time);
clock->unregisterExternalEvent();
active = 0;
}
void Car::enterRoad() {
while(active == 2) {
waitEnterRoad();
if(start_road->tryEnterRoad(time)) {
position = 0.0f;
forwardCar = start_road->lastEntered;
start_road->lastEntered = this;
active = 1;
}
}
clock->notifyExternalEvent();
}
void Car::changeRoad() {
/* current_road->setCarNum(current_road->getCarNum() - 1);
(current_road->cars).erase(current_road->cars.begin()+this->id);
forwardCar = dest_road->lastEntered;
dest_road->lastEntered = this;
(dest_road->cars).push_back(this);
current_road = dest_road;
dest_road->setCarNum(current_road->getCarNum() + 1);*/
// string t = to_string(this->id);
// auto m = mqtt::make_message("v1/gateway/connect","{\"device\":\"Group#8Car"+t+"\",\"type\":\"ProjectGroup#8\"}",1,false);
// current_road->getClient()->publish(m)->wait();
}
void Car::enterIntersection() {
/* while(active == 2) {
if(dest_road->tryEnterIntersection(time)) {
position = 0.0f;
if (current_road->getRdID() != dest_road->getRdID()) {
changeRoad();
}
active = 1;
}
waitEnterRoad();
}
clock->notifyExternalEvent();
clock->waitForTick(time);*/
}
void Car::waitEnterRoad() {
int waitTime = rand() % (MAX_WAIT_TIME - MIN_WAIT_TIME) + MIN_WAIT_TIME;
while(waitTime > 0) {
time++;
clock->notifyExternalEvent();
clock->waitForTick(time);
timeWaitedToEnter += 1;
waitTime -= 1;
}
}
void Car::drive() {
targetSpeed = MIN_TARGET_SPEED + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(MAX_TARGET_SPEED-MIN_TARGET_SPEED)));
int timeCount = SPEED_CHANGE_PERIOD;
float speedUpdate = 0.0f;
while(position < road->endPosition) {
//Wait for clock then move
clock->waitForTick(time);
move();
//Do speed calc for next loop
if (position > 30.0 && position < 52.0) {
//Wait for message from Cross Man
//Decode Message
//Set New Speed
client->subscribe("v1/gateway/attributes", 1);
client->start_consuming();
//uto msg = client->consume_message();
// if (msg){
// string msgTemp = msg->to_string();
// vector <string> tokens;
// // stringstream class check1
// stringstream check1(msgTemp);
// string intermediate;
// while(getline(check1, intermediate, ','))
// {
// tokens.push_back(intermediate);
// }
// vector <vector<float>> info;
// for(unsigned int i = 0; i < tokens.size(); i++){
// vector<float> temp;
// for(int t = 0; t < 5 ; t++) {
// temp.push_back(0.0);
// }
// info.push_back(temp);
// }
// //Store Data
// for(unsigned int i = 0; i < tokens.size(); i++){
// sscanf(tokens[i].c_str(),"%f %f %f %f %f", &info[i][0], &info[i][1], &info[i][2], &info[i][3], &info[i][4]);
// }
// targetSpeed = info[this->id][4];
// }
} else {
//Free Drive
//Randomly change speed every SPEED_CHANGE_PERIOD
timeCount -= 1;
if(timeCount <= 0) {
targetSpeed = MIN_TARGET_SPEED + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(MAX_TARGET_SPEED-MIN_TARGET_SPEED)));
timeCount = SPEED_CHANGE_PERIOD;
}
}
speedUpdate = targetSpeed;
//Check if car is too close
if(forwardCar != NULL && forwardCar->active) {
float forwardPosition = forwardCar->getPosition(time);
float distance = forwardPosition - position - BUFFER_SPACING;
if(distance < speedUpdate) {
//printf("CAR %d SLOWING DOWN\n", this->id);
speedUpdate = distance;
}
}
//Change speed
adjustSpeed(speedUpdate);
//printf("CAR %d POSITION %f SPEED %f at time %d\n", this->id, this->position, this->speed, time);
time++;
clock->notifyExternalEvent();
}
}
void Car::move() {
pthread_mutex_lock(&positionMutex);
time = clock->getTime();
prevPosition = position;
position += speed;
pthread_mutex_unlock(&positionMutex);
pthread_cond_broadcast(&positionCond);
}
void Car::adjustSpeed(float speed) {
pthread_mutex_lock(&speedMutex);
prevSpeed = this->speed;
this->speed = speed;
pthread_mutex_unlock(&speedMutex);
pthread_cond_broadcast(&speedCond);
}
void* Car::threadHelper(void* context) {
Car* car = (Car*) context;
car->cycle();
return NULL;
}