Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
21 | |||
22 | /** |
||
23 | ------------ |
||
24 | CVS : $Id: auto.c,v 1.1.1.1 2002-09-02 09:37:42 pj Exp $ |
||
25 | |||
26 | File: $File$ |
||
27 | Revision: $Revision: 1.1.1.1 $ |
||
28 | Last update: $Date: 2002-09-02 09:37:42 $ |
||
29 | ------------ |
||
30 | **/ |
||
31 | |||
32 | /* |
||
33 | * Copyright (C) 2000 Marco Dallera and Marco Fiocca |
||
34 | * |
||
35 | * This program is free software; you can redistribute it and/or modify |
||
36 | * it under the terms of the GNU General Public License as published by |
||
37 | * the Free Software Foundation; either version 2 of the License, or |
||
38 | * (at your option) any later version. |
||
39 | * |
||
40 | * This program is distributed in the hope that it will be useful, |
||
41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
43 | * GNU General Public License for more details. |
||
44 | * |
||
45 | * You should have received a copy of the GNU General Public License |
||
46 | * along with this program; if not, write to the Free Software |
||
47 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
48 | * |
||
49 | */ |
||
50 | |||
51 | /* |
||
52 | * AUTO |
||
53 | * |
||
54 | * Another Unuseful Track simulatOr |
||
55 | * |
||
56 | * Authors: Marco Dallera |
||
57 | * Marco Fiocca |
||
58 | * |
||
59 | */ |
||
60 | |||
61 | #include "include/auto.h" |
||
62 | #include "include/const.h" |
||
63 | #include "include/utils.h" |
||
64 | |||
65 | int car_number; |
||
66 | car_params cars[MAX_CAR_NUMBER]; |
||
67 | char drivers[DRIVERS_NUMBER][MAX_DRIVER_NAME_LENGTH] = { |
||
68 | "M.Schumacher", |
||
69 | "D.Coulthard", |
||
70 | "R.Schumacher", |
||
71 | "M.Hakkinen", |
||
72 | "R.Barrichello", |
||
73 | "J.P.Montoya", |
||
74 | "E.Irvine", |
||
75 | "J.Trulli", |
||
76 | "H.Frentzen", |
||
77 | "J.Villeneuve", |
||
78 | "J.Alesi", |
||
79 | "G.Fisichella", |
||
80 | "O.Panis", |
||
81 | "O.Wurz", |
||
82 | "J.Button", |
||
83 | "P.P.Diniz", |
||
84 | "P.De La Rosa", |
||
85 | "T.Marques", |
||
86 | "R.Zonta", |
||
87 | "G.Mazzacane" |
||
88 | }; |
||
89 | |||
90 | extern char sprite[18][CAR_WIDTH*CAR_HEIGHT]; |
||
91 | |||
92 | /* Semaphores */ |
||
93 | sem_t grx_mutex; |
||
94 | |||
95 | /* Useful colors */ |
||
96 | int red = rgb16(255, 0, 0); |
||
97 | int green = rgb16( 0, 255, 0); |
||
98 | int blue = rgb16( 0, 0, 255); |
||
99 | int lightgray = rgb16(192, 192, 192); |
||
100 | |||
101 | /* JetCTRL Initialization */ |
||
102 | void init_jetcontrol(void); |
||
103 | |||
104 | /* -------------------------------------------------------*/ |
||
105 | |||
106 | /* --------------------- */ |
||
107 | /* Handling car status */ |
||
108 | /* --------------------- */ |
||
109 | |||
110 | int set_car_status(car_status cs, CAB cab_id) { |
||
111 | char *msg; |
||
112 | |||
113 | msg = cab_reserve(cab_id); |
||
114 | memcpy(msg, &cs, CAR_MSG_DIM); |
||
115 | return(cab_putmes(cab_id, msg)); |
||
116 | } |
||
117 | |||
118 | car_status get_car_status(CAB cab_id) { |
||
119 | char *msg; |
||
120 | car_status status; |
||
121 | |||
122 | msg = cab_getmes(cab_id); |
||
123 | memcpy(&status, msg, CAR_MSG_DIM); |
||
124 | cab_unget(cab_id, msg); |
||
125 | |||
126 | return (status); |
||
127 | } |
||
128 | |||
129 | /* ---------------------------- */ |
||
130 | /* Handling road informations */ |
||
131 | /* ---------------------------- */ |
||
132 | |||
133 | int set_road_info(road_info info, CAB road_status_cab) { |
||
134 | char *msg; |
||
135 | |||
136 | msg = cab_reserve(road_status_cab); |
||
137 | memcpy(msg, &info, ROAD_MSG_DIM); |
||
138 | return(cab_putmes(road_status_cab, msg)); |
||
139 | } |
||
140 | |||
141 | road_info get_road_info(CAB road_status_cab) { |
||
142 | char *msg; |
||
143 | road_info info; |
||
144 | |||
145 | msg = cab_getmes(road_status_cab); |
||
146 | memcpy(&info, msg, ROAD_MSG_DIM); |
||
147 | cab_unget(road_status_cab, msg); |
||
148 | |||
149 | return (info); |
||
150 | } |
||
151 | |||
152 | /* -------------------------------------------------------*/ |
||
153 | |||
154 | /* Closing function */ |
||
155 | void byebye() |
||
156 | { |
||
157 | grx_close(); |
||
158 | |||
159 | // we need clear to reposition the cursor in the right place after |
||
160 | // going back from the graphical mode. |
||
161 | clear(); |
||
162 | |||
163 | kern_printf("Race is over!\n"); |
||
164 | } |
||
165 | |||
166 | /* -------------------------------------------------------*/ |
||
167 | |||
168 | /* ------------------- */ |
||
169 | /* Hard car creation */ |
||
170 | /* ------------------- */ |
||
171 | |||
172 | void hard_car_create() { |
||
173 | car_params cp; |
||
174 | HARD_TASK_MODEL sensor_m, control_m; |
||
175 | PID sensor_pid, control_pid; |
||
176 | TIME seed; /* used to init the random seed */ |
||
177 | int drv_ind; |
||
178 | |||
179 | if (car_number >= MAX_CAR_NUMBER) return; |
||
180 | |||
181 | /* Randomize!!!! */ |
||
182 | seed = sys_gettime(NULL); |
||
183 | srand(seed); |
||
184 | |||
185 | /* CAB creation */ |
||
186 | cp.road_status_cab = cab_create("road_cab", ROAD_MSG_DIM, ROAD_MSG_READER); |
||
187 | cp.car_status_cab = cab_create("car_cab", CAR_MSG_DIM, CAR_MSG_READER); |
||
188 | |||
189 | /* Car parameters initialization */ |
||
190 | cp.max_speed = rand_01(); |
||
191 | cp.min_acc = cp.max_speed; |
||
192 | cp.max_acc = cp.max_speed; |
||
193 | cp.rage = cp.max_speed; |
||
194 | cp.color = 3+car_number; |
||
195 | cp.number = car_number; |
||
196 | drv_ind = DRIVERS_NUMBER-1 - (int)(((cp.rage-0.8+0.005) * (float)DRIVERS_NUMBER) / 0.2); |
||
197 | |||
198 | /* Sets driver's name without duplications */ |
||
199 | strcpy(cp.driver, drivers[drv_ind]); |
||
200 | while (!strcmp(cp.driver,"***")) |
||
201 | strcpy(cp.driver, drivers[++drv_ind%DRIVERS_NUMBER]); |
||
202 | strcpy(drivers[drv_ind],"***"); |
||
203 | |||
204 | cars[car_number] = cp; |
||
205 | |||
206 | /* ------------ */ |
||
207 | /* Task Calls */ |
||
208 | /* ------------ */ |
||
209 | |||
210 | /* sensor task creation */ |
||
211 | hard_task_default_model(sensor_m); |
||
212 | |||
213 | hard_task_def_arg(sensor_m, (void *)car_number); |
||
214 | hard_task_def_wcet(sensor_m, SENSOR_WCET); |
||
215 | hard_task_def_mit(sensor_m, SENSOR_PERIOD); |
||
216 | hard_task_def_usemath(sensor_m); |
||
217 | sensor_pid = task_create("camera", sensor, &sensor_m, NULL); |
||
218 | if(sensor_pid == -1) |
||
219 | sys_end(); |
||
220 | |||
221 | /* control task creation */ |
||
222 | hard_task_default_model(control_m); |
||
223 | hard_task_def_arg(control_m, (void *)car_number); |
||
224 | hard_task_def_wcet(control_m, CONTROL_WCET); |
||
225 | hard_task_def_mit(control_m, CONTROL_PERIOD); |
||
226 | hard_task_def_usemath(control_m); |
||
227 | control_pid = task_create("controller", control, &control_m, NULL); |
||
228 | if(control_pid == -1) |
||
229 | sys_end(); |
||
230 | |||
231 | cars[car_number].sensor_pid = sensor_pid; |
||
232 | cars[car_number].control_pid = control_pid; |
||
233 | car_number++; // increases cars number |
||
234 | |||
235 | task_activate(sensor_pid); |
||
236 | task_activate(control_pid); |
||
237 | } |
||
238 | |||
239 | /* -------------------------------------------------------*/ |
||
240 | |||
241 | /* ------------------- */ |
||
242 | /* Soft car creation */ |
||
243 | /* ------------------- */ |
||
244 | |||
245 | void soft_car_create() { |
||
246 | car_params cp; |
||
247 | SOFT_TASK_MODEL sensor_m, control_m; |
||
248 | PID sensor_pid, control_pid; |
||
249 | TIME seed; /* used to init the random seed */ |
||
250 | int drv_ind; |
||
251 | |||
252 | if (car_number >= MAX_CAR_NUMBER) return; |
||
253 | |||
254 | /* Randomize!!!! */ |
||
255 | seed = sys_gettime(NULL); |
||
256 | srand(seed); |
||
257 | |||
258 | /* CAB creation */ |
||
259 | cp.road_status_cab = cab_create("road_cab", ROAD_MSG_DIM, ROAD_MSG_READER); |
||
260 | cp.car_status_cab = cab_create("car_cab", CAR_MSG_DIM, CAR_MSG_READER); |
||
261 | |||
262 | /* Car parameters initialization */ |
||
263 | cp.max_speed = rand_01(); |
||
264 | cp.min_acc = cp.max_speed; |
||
265 | cp.max_acc = cp.max_speed; |
||
266 | cp.rage = cp.max_speed; |
||
267 | cp.color = 8+car_number; |
||
268 | cp.number = car_number; |
||
269 | drv_ind = DRIVERS_NUMBER-1 - (int)(((cp.rage-0.8+0.005) * (float)DRIVERS_NUMBER) / 0.2); |
||
270 | |||
271 | /* Sets driver's name without duplications */ |
||
272 | strcpy(cp.driver, drivers[drv_ind]); |
||
273 | while (!strcmp(cp.driver,"***")) |
||
274 | strcpy(cp.driver, drivers[++drv_ind%DRIVERS_NUMBER]); |
||
275 | strcpy(drivers[drv_ind],"***"); |
||
276 | cars[car_number] = cp; |
||
277 | |||
278 | /* ------------ */ |
||
279 | /* Task Calls */ |
||
280 | /* ------------ */ |
||
281 | |||
282 | /* sensor task creation */ |
||
283 | soft_task_default_model(sensor_m); |
||
284 | |||
285 | soft_task_def_arg(sensor_m, (void *)car_number); |
||
286 | soft_task_def_met(sensor_m, SENSOR_WCET); |
||
287 | soft_task_def_period(sensor_m, SENSOR_PERIOD); |
||
288 | soft_task_def_usemath(sensor_m); |
||
289 | sensor_pid = task_create("camera", sensor, &sensor_m, NULL); |
||
290 | if(sensor_pid == -1) |
||
291 | sys_end(); |
||
292 | |||
293 | /* control task creation */ |
||
294 | soft_task_default_model(control_m); |
||
295 | soft_task_def_arg(control_m, (void *)car_number); |
||
296 | soft_task_def_met(control_m, CONTROL_WCET); |
||
297 | soft_task_def_period(control_m, CONTROL_PERIOD); |
||
298 | soft_task_def_usemath(control_m); |
||
299 | control_pid = task_create("controller", control, &control_m, NULL); |
||
300 | if(control_pid == -1) |
||
301 | sys_end(); |
||
302 | |||
303 | cars[car_number].sensor_pid = sensor_pid; |
||
304 | cars[car_number].control_pid = control_pid; |
||
305 | car_number++; // increases cars number |
||
306 | |||
307 | task_activate(sensor_pid); |
||
308 | task_activate(control_pid); |
||
309 | } |
||
310 | |||
311 | /* -------------------------------------------------------*/ |
||
312 | |||
313 | /* ------------------- */ |
||
314 | /* The main function */ |
||
315 | /* ------------------- */ |
||
316 | |||
317 | int main(int argc, char **argv) { |
||
318 | TIME seed; /* used to init the random seed */ |
||
319 | |||
320 | if (MAX_CAB < 30) { |
||
321 | cprintf("The application needs at least 30 CABS.\n" |
||
322 | "Please set the MAX_CAB #define to 30 or more...\n" |
||
323 | "...(see include/modules/cabs.h)\n"); |
||
324 | sys_end(); |
||
325 | } |
||
326 | |||
327 | /* Set the exception handler */ |
||
328 | set_exchandler_grx(); |
||
329 | |||
330 | /* Set the closing function */ |
||
331 | sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT); |
||
332 | |||
333 | /* Keyboard handling */ |
||
334 | keyb_handler(); |
||
335 | kern_printf("KEYBOARD initialized...\n"); |
||
336 | |||
337 | /* Graphics mutex */ |
||
338 | sem_init(&grx_mutex, 0, 1); |
||
339 | |||
340 | /* Graphics init */ |
||
341 | grx_init(); |
||
342 | grx_open(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BIT_COLORS); |
||
343 | |||
344 | /* Track drawing */ |
||
345 | sem_wait(&grx_mutex); |
||
346 | grx_setcolor(46,0,0,0); |
||
347 | grx_setcolor(43,255,255,255); |
||
348 | grx_setcolor(26,255,74,33); |
||
349 | grx_setcolor(27,255,255,255); |
||
350 | grx_setcolor(1,8,8,8); |
||
351 | grx_setcolor(3,255,0,0); |
||
352 | grx_setcolor(28,255,50,0); |
||
353 | grx_setcolor(29,255,90,0); |
||
354 | grx_setcolor(30,255,130,0); |
||
355 | grx_setcolor(31,255,170,0); |
||
356 | grx_setcolor(32,255,210,0); |
||
357 | grx_setcolor(33,48,48,48); |
||
358 | |||
359 | grx_putimage(TRACK_X1, TRACK_Y1, TRACK_X2, TRACK_Y2, track_img); |
||
360 | sem_post(&grx_mutex); |
||
361 | |||
362 | /* Command and info display */ |
||
363 | cmd_display(); |
||
364 | |||
365 | /* JetCTRL Initialization */ |
||
366 | init_jetcontrol(); |
||
367 | |||
368 | /* Randomize!!!! */ |
||
369 | seed = sys_gettime(NULL); |
||
370 | srand(seed); |
||
371 | |||
372 | car_number = 0; |
||
373 | |||
374 | /* ------------ */ |
||
375 | /* Task Calls */ |
||
376 | /* ------------ */ |
||
377 | |||
378 | hard_car_create(); |
||
379 | |||
380 | return 0; |
||
381 | } |
||
382 | |||
383 | /* -------------------------------------------------------*/ |
||
384 | |||
385 | void read_track(int i) |
||
386 | { |
||
387 | DOS_FILE *f; // DOS file descriptor |
||
388 | int file_length; |
||
389 | int index; |
||
390 | char filename[100]; |
||
391 | int err; // Error code |
||
392 | |||
393 | /* Init track_list */ |
||
394 | track_init(); |
||
395 | |||
396 | /* Set the selected track from track_list */ |
||
397 | if (i < TRACK_NUMBER) |
||
398 | index = i; |
||
399 | else |
||
400 | index = 0; |
||
401 | cprintf("Selected index = %d\n", index); |
||
402 | |||
403 | strcpy(filename, strcat("tracks/", track_list[index].name)); |
||
404 | cprintf("File to open = %s\n", filename); |
||
405 | track_list[index].selected = 1; |
||
406 | |||
407 | /* open the DOS file for reading (you can specify only "r" or "w") */ |
||
408 | f = DOS_fopen(filename,"r"); |
||
409 | |||
410 | /* check for open errors */ |
||
411 | if (!f) { |
||
412 | err = DOS_error(); // error!! |
||
413 | |||
414 | /* note that if you call DOS_error() here, it return 0!!! */ |
||
415 | cprintf("Error %d opening %s...\n", err, filename); |
||
416 | file_length = 0; |
||
417 | byebye(); |
||
418 | return; |
||
419 | } |
||
420 | |||
421 | /* read track file */ |
||
422 | file_length = DOS_fread(&track_img, 1, TRACK_WIDTH*TRACK_HEIGHT, f); |
||
423 | |||
424 | /* check for errors */ |
||
425 | err = DOS_error(); |
||
426 | |||
427 | cprintf("Read %d bytes from %s...\n", file_length, filename); |
||
428 | |||
429 | if (err) { |
||
430 | cprintf("Error %d reading %s...\n", err, filename); |
||
431 | file_length = 0; |
||
432 | /* there is not return because I want to close the file! */ |
||
433 | } |
||
434 | |||
435 | /* Close the file */ |
||
436 | cprintf("Closing file %s...\n", filename); |
||
437 | DOS_fclose(f); |
||
438 | cprintf("File %s closed successfully!\n", filename); |
||
439 | |||
440 | } |
||
441 | |||
442 | /* -------------------------------------------------------*/ |
||
443 | |||
444 | void read_sprites() |
||
445 | { |
||
446 | /* DOS file descriptor */ |
||
447 | DOS_FILE *f; |
||
448 | int myfilebuf_length,i; |
||
449 | char filename[100]; |
||
450 | |||
451 | /* Error code */ |
||
452 | int err; |
||
453 | i=0; |
||
454 | |||
455 | for ( i=0; i < 16; i++) { |
||
456 | sprintf(filename,"cars/car%d.raw",i); |
||
457 | f = DOS_fopen(filename,"r"); |
||
458 | |||
459 | // check for open errors |
||
460 | if (!f) { |
||
461 | // error!! |
||
462 | err = DOS_error(); |
||
463 | |||
464 | // note that if you call DOS_error() here, it return 0!!! |
||
465 | cprintf("Error %d opening %s...\n", err, filename); |
||
466 | myfilebuf_length = 0; |
||
467 | return; |
||
468 | } |
||
469 | |||
470 | // read from file |
||
471 | myfilebuf_length = DOS_fread(&sprite[i][0],1,CAR_WIDTH*CAR_HEIGHT,f); |
||
472 | |||
473 | // check for errors |
||
474 | err = DOS_error(); |
||
475 | |||
476 | cprintf("Read %d bytes from %s...\n", myfilebuf_length, filename); |
||
477 | |||
478 | if (err) { |
||
479 | cprintf("Error %d reading %s...\n", err, filename); |
||
480 | myfilebuf_length = 0; |
||
481 | // there is not return because I want to close the file! |
||
482 | } |
||
483 | |||
484 | // Close the file |
||
485 | DOS_fclose(f); |
||
486 | } |
||
487 | |||
488 | f = DOS_fopen("cars/boom.raw","r"); |
||
489 | if (!f) { |
||
490 | err = DOS_error(); |
||
491 | |||
492 | cprintf("Error %d opening boom.raw...\n", err); |
||
493 | myfilebuf_length = 0; |
||
494 | return; |
||
495 | } |
||
496 | |||
497 | myfilebuf_length = DOS_fread(&sprite[16][0],1,CAR_WIDTH*CAR_HEIGHT,f); |
||
498 | |||
499 | err = DOS_error(); |
||
500 | |||
501 | cprintf("Read %d bytes from boom.raw...\n", myfilebuf_length); |
||
502 | |||
503 | if (err) { |
||
504 | cprintf("Error %d reading boom.raw...\n", err); |
||
505 | myfilebuf_length = 0; |
||
506 | } |
||
507 | |||
508 | DOS_fclose(f); |
||
509 | |||
510 | f = DOS_fopen("cars/fumo.raw","r"); |
||
511 | if (!f) { |
||
512 | err = DOS_error(); |
||
513 | |||
514 | cprintf("Error %d opening fumo.raw...\n", err); |
||
515 | myfilebuf_length = 0; |
||
516 | return; |
||
517 | } |
||
518 | |||
519 | myfilebuf_length = DOS_fread(&sprite[17][0],1,CAR_WIDTH*CAR_HEIGHT,f); |
||
520 | |||
521 | err = DOS_error(); |
||
522 | |||
523 | cprintf("Read %d bytes from fumo.raw...\n", myfilebuf_length); |
||
524 | |||
525 | if (err) { |
||
526 | cprintf("Error %d reading fumo.raw...\n", err); |
||
527 | myfilebuf_length = 0; |
||
528 | } |
||
529 | |||
530 | DOS_fclose(f); |
||
531 | |||
532 | } |
||
533 |