-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotFunctions.c
317 lines (273 loc) · 8.26 KB
/
botFunctions.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "botFunctions.h"
obstacle_t obstacle;
// Static Variables
botData_t botData;
int move = E; // move east is defautl behavior
int exitFunc = 0;
int nextMoveX = 0;
int nextMoveY = 0;
int obstacleMap[NUM_OBST][COORD] =
{ // X Y X Y
{80, 45, 82, 57}, {87, 45, 88, 46} , {100, 54, 105, 55}, {122, 58, 127, 61}
};
/* Initializes R0 starting point in the grid */
int initRobot()
{
// Initialize robot
botData.botData.location.x = 60;
botData.botData.location.y = 50;
botData.botData.speed = 0;
strcpy(botData.botData.direction,"E");
botData.botData.obstacle[0] = 0;
botData.botData.obstacle[1] = 0;
botData.botData.obstacle[2] = 0;
botData.botData.obstacle[3] = 0;
}
/* Send data to interested listeners, position, speed, obstacles */
void* sendData(void* ptr)
{
printf("SendingData() started\n");
// variables
// botData_t * robotData;
FILE *fwa;
char line[80];
char buf[6]; // buffer to store integer conversions
// memset(&robotData, 0, sizeof(botData_t));
// robotData = botData;
// Open file ready to append current position and velocity
fwa = fopen("ourBotData.txt", "a+");
if (fwa == NULL)
{
printf("Couldn't open file!!\n");
exit(0);
}
// preapare string = posx pos y vel obstN obtsW obstE obstS
strcpy(line,"R0");
snprintf(buf, sizeof(buf), " %d ", botData.botData.location.x);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.location.y);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.speed);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %c ", botData.botData.direction[0]);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.obstacle[0]);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.obstacle[1]);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.obstacle[2]);
strcat(line, buf);
snprintf(buf, sizeof(buf), " %d ", botData.botData.obstacle[3]);
strcat(line, buf);
strcat(line, " \n");
// write current position, vel, nextDir and if obstacles present
fprintf(fwa, "%s", line);
fclose(fwa); /* close the file */
printf("SendingData() correctly run\n");
}
/* Receive data from other bots, position, speed, obstacles at a time instant*/
void* receiveData(void *ptr)
{
// variables
/* botData_t * robotData;
FILE *fr;
char line[80];
char buf[6]; // buffer to store integer conversions
char * ptrChar;
char * ctrlPtr;
int i = 0;
memset(&robotData, 0, sizeof(botData_t));
robotData = botData;
// Open file ready to append current position and velocity
fr = fopen("vector.txt", "r");
if (fr == NULL)
{
printf("Couldn't open file!!\n");
exit(0);
} */
printf("receivingData() correctly run\n");
}
void* mcastSubscribe(void* ptr)
{
printf("mcastSubscribe() started\n");
int j;
int my_coord_x;
int my_coord_y;
int coord_x;
int coord_y;
my_coord_x = botData.botData.location.x;
my_coord_y = botData.botData.location.y;
printf("My position R%d = X=%d,Y=%d\n",0 ,my_coord_x,my_coord_y);
for(j=0; j< TOTAL_BOTS; j++)
{
coord_x = timeInstMatrix[timeInst][j][1];
coord_y = timeInstMatrix[timeInst][j][2];
printf("Check position of robot R%d = X=%d,Y=%d\n",j ,coord_x,coord_y);
if(my_coord_x - coord_x < MAX_RANGE_X)
{
//suscribe
subscribeArr[j] = 1;
}
}
printf("mcastSubscribe() correctly run\n");
}
void* mcastUnSubscribe(void* ptr)
{
printf("mcastUnSubscribe() started\n");
int j;
int my_coord_x;
int my_coord_y;
int coord_x;
int coord_y;
my_coord_x = botData.botData.location.x;
my_coord_y = botData.botData.location.y;
printf("My position R%d = X=%d,Y=%d\n",0 ,my_coord_x,my_coord_y);
for(j=1; j<= TOTAL_BOTS; j++)
{
coord_x = timeInstMatrix[timeInst][j][1];
coord_y = timeInstMatrix[timeInst][j][2];
printf("Check position of robot R%d = X=%d,Y=%d\n",j ,coord_x,coord_y);
if(my_coord_x - coord_x >= MAX_RANGE_X)
{
//Unsuscribe
subscribeArr[j] = 0;
}
}
printf("mcastUnSubscribe() correctly run\n");
}
void* decisionMaking(void* ptr)
{
printf("decisionMaking() started\n");
if(!obstacle.e)
{
move = E;
nextMoveX = botData.botData.location.x+1;
nextMoveY = botData.botData.location.y;
}
else if(!obstacle.n)
{
move = N;
nextMoveX = botData.botData.location.x;
nextMoveY = botData.botData.location.y+1;
}
else if(!obstacle.s)
{
move = S;
nextMoveX = botData.botData.location.x;
nextMoveY = botData.botData.location.y-1;
}
else
{
nextMoveX = botData.botData.location.x;
nextMoveY = botData.botData.location.y;
}
// update
updateRobot();
printf("decisionMaking() correctly run\n");
} // end decisionMaking
void *dataProcessing(void* ptr)
{
int j = 0;
int currentTimeInst = 4; // This is a global variable, will move later
bool collision = false;
int xi, yi, xf, yf,i;
int xactual, yactual;
int dx, dy;
// Check if obstacles are in my next move
xactual = botData.botData.location.x;
yactual = botData.botData.location.y;
obstacle.n = 0;
obstacle.s = 0;
obstacle.e = 0;
// this emulates sensor reads to detect static obstacles
for( i = 0; i < NUM_OBST ; i++)
{
xi = obstacleMap[i][0];
yi = obstacleMap[i][1];
xf = obstacleMap[i][2];
yf = obstacleMap[i][3];
// check for collision at north
if( xactual >= xi && xactual <= xf && yactual+1 >= yi && yactual+1 <= yf)
{
obstacle.n = true;
}
// collision at east
if( xactual+1 >= xi && xactual+1 <= xf && yactual >= yi && yactual <= yf)
{
obstacle.e = true;
}
// collision at south
if( xactual-1 >= xi && xactual-1 <= xf && yactual >= yi && yactual <= yf)
{
obstacle.s = true;
}
}
// Check if other robots are in my way
for(j = 0; j < TOTAL_BOTS; j++)
{
if(subscribeArr[j] == 1)
{
dataCollectionCtr++; // used for data colleciton purposes
if(timeInst == TIME_INSTANTS) { printf("DataCollection Bots processed = %d\n", dataCollectionCtr); };
// check I can move East
switch(timeInstMatrix[currentTimeInst][j][4])
{
case N:
{ // North means adding Y+1
dx = 0;
dy = 1;
break;
}
case E:
{ // East means adding X+1
dx = 1;
dy = 0;
break;
}
case S:
{ // South means substracting Y-1
dx = 0;
dy = -1;
}
} // end switch
// if moving east
if((timeInstMatrix[currentTimeInst][j][1] + dx == xactual+1)
&& (timeInstMatrix[currentTimeInst][j][2]+dy == yactual ))
{
// If true we are going to collide invalidate the option
obstacle.e = true;
}
// if moving north
else if((timeInstMatrix[currentTimeInst][j][1] + dx == xactual)
&& (timeInstMatrix[currentTimeInst][j][2]+dy == yactual+1 ))
{
obstacle.n = true;
}
// if moving south
else if((timeInstMatrix[currentTimeInst][j][1] + dx == xactual)
&& (timeInstMatrix[currentTimeInst][j][2]+ dy == yactual-1 ))
{
obstacle.s = true;
}
} // end if
} // end for
printf("dataProcessing() correctly run\n");
}
void* dataSelection(void* ptr)
{
printf("dataSelection() correctly run\n");
}
int updateRobot()
{
// Initialize robot
botData.botData.location.x = nextMoveX;
botData.botData.location.y = nextMoveY;
botData.botData.speed = 0;
if(move == N) { botData.botData.direction[0] = 'N';};
if(move == S) { botData.botData.direction[0] = 'S';};
if(move == E) { botData.botData.direction[0] = 'E';};
botData.botData.obstacle[0] = 0;
botData.botData.obstacle[1] = 0;
botData.botData.obstacle[2] = 0;
botData.botData.obstacle[3] = 0;
}