Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 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 2004-05-24 18:03:39 giacomo Exp $
25
 
26
 File:        $File$
27
 Revision:    $Revision: 1.1.1.1 $
28
 Last update: $Date: 2004-05-24 18:03:39 $
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 closing function */
328
  sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);
329
 
330
  /* Keyboard handling */
331
  keyb_handler();
332
  kern_printf("KEYBOARD initialized...\n");
333
 
334
  /* Graphics mutex */
335
  sem_init(&grx_mutex, 0, 1);
336
 
337
  /* Graphics init */
338
  grx_init();
339
  grx_open(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BIT_COLORS);
340
 
341
  /* Track drawing */
342
  sem_wait(&grx_mutex);
343
  grx_setcolor(46,0,0,0);
344
  grx_setcolor(43,255,255,255);
345
  grx_setcolor(26,255,74,33);
346
  grx_setcolor(27,255,255,255);
347
  grx_setcolor(1,8,8,8);
348
  grx_setcolor(3,255,0,0);
349
  grx_setcolor(28,255,50,0);
350
  grx_setcolor(29,255,90,0);
351
  grx_setcolor(30,255,130,0);
352
  grx_setcolor(31,255,170,0);
353
  grx_setcolor(32,255,210,0);
354
  grx_setcolor(33,48,48,48);
355
 
356
  grx_putimage(TRACK_X1, TRACK_Y1, TRACK_X2, TRACK_Y2, track_img);
357
  sem_post(&grx_mutex);
358
 
359
  /* Command and info display */
360
  cmd_display();
361
 
362
  /* JetCTRL Initialization */
363
  init_jetcontrol();
364
 
365
  /* Randomize!!!! */
366
  seed = sys_gettime(NULL);
367
  srand(seed);
368
 
369
  car_number = 0;
370
 
371
  /* ------------ */
372
  /*  Task Calls  */
373
  /* ------------ */
374
 
375
  hard_car_create();
376
 
377
  return 0;
378
}
379
 
380
/* -------------------------------------------------------*/
381
 
382
void read_track(int i)
383
{
384
  DOS_FILE *f;  // DOS file descriptor
385
  int file_length;
386
  int index;
387
  char filename[100];
388
  int err;      // Error code
389
 
390
  /* Init track_list */
391
  track_init();
392
 
393
  /* Set the selected track from track_list */
394
  if (i < TRACK_NUMBER)
395
    index = i;
396
  else
397
    index = 0;
398
  cprintf("Selected index = %d\n", index);
399
 
400
  strcpy(filename, strcat("tracks/", track_list[index].name));
401
  cprintf("File to open = %s\n", filename);
402
  track_list[index].selected = 1;
403
 
404
  /* open the DOS file for reading      (you can specify only "r" or "w") */
405
  f = DOS_fopen(filename,"r");
406
 
407
  /* check for open errors */
408
  if (!f) {
409
    err = DOS_error();  // error!!
410
 
411
    /* note that if you call DOS_error() here, it return 0!!! */
412
    cprintf("Error %d opening %s...\n", err, filename);
413
    file_length = 0;
414
    byebye();
415
    return;
416
  }
417
 
418
  /* read track file */
419
  file_length = DOS_fread(&track_img, 1, TRACK_WIDTH*TRACK_HEIGHT, f);
420
 
421
  /* check for errors */
422
  err = DOS_error();
423
 
424
  cprintf("Read %d bytes from %s...\n", file_length, filename);
425
 
426
  if (err) {
427
    cprintf("Error %d reading %s...\n", err, filename);
428
    file_length = 0;
429
    /* there is not return because I want to close the file! */
430
  }
431
 
432
  /* Close the file */
433
  cprintf("Closing file %s...\n", filename);
434
  DOS_fclose(f);
435
  cprintf("File %s closed successfully!\n", filename);
436
 
437
}
438
 
439
/* -------------------------------------------------------*/
440
 
441
void read_sprites()
442
{
443
  /* DOS file descriptor */
444
  DOS_FILE *f;
445
  int myfilebuf_length,i;
446
  char filename[100];
447
 
448
        /* Error code */
449
  int err;
450
  i=0;
451
 
452
  for ( i=0; i < 16; i++) {
453
    sprintf(filename,"cars/car%d.raw",i);
454
    f = DOS_fopen(filename,"r");
455
 
456
    // check for open errors
457
    if (!f) {
458
      // error!!
459
      err = DOS_error();
460
 
461
      // note that if you call DOS_error() here, it return 0!!!
462
      cprintf("Error %d opening %s...\n", err, filename);
463
      myfilebuf_length = 0;
464
      return;
465
    }
466
 
467
    // read from file
468
    myfilebuf_length = DOS_fread(&sprite[i][0],1,CAR_WIDTH*CAR_HEIGHT,f);
469
 
470
    // check for errors
471
    err = DOS_error();
472
 
473
    cprintf("Read %d bytes from %s...\n", myfilebuf_length, filename);
474
 
475
    if (err) {
476
      cprintf("Error %d reading %s...\n", err, filename);
477
      myfilebuf_length = 0;
478
      // there is not return because I want to close the file!
479
    }
480
 
481
    // Close the file
482
    DOS_fclose(f);
483
  }
484
 
485
  f = DOS_fopen("cars/boom.raw","r");
486
  if (!f) {
487
    err = DOS_error();
488
 
489
    cprintf("Error %d opening boom.raw...\n", err);
490
    myfilebuf_length = 0;
491
    return;
492
  }
493
 
494
  myfilebuf_length = DOS_fread(&sprite[16][0],1,CAR_WIDTH*CAR_HEIGHT,f);
495
 
496
  err = DOS_error();
497
 
498
  cprintf("Read %d bytes from boom.raw...\n", myfilebuf_length);
499
 
500
  if (err) {
501
    cprintf("Error %d reading boom.raw...\n", err);
502
    myfilebuf_length = 0;
503
  }
504
 
505
  DOS_fclose(f);
506
 
507
  f = DOS_fopen("cars/fumo.raw","r");
508
  if (!f) {
509
    err = DOS_error();
510
 
511
    cprintf("Error %d opening fumo.raw...\n", err);
512
    myfilebuf_length = 0;
513
    return;
514
  }
515
 
516
  myfilebuf_length = DOS_fread(&sprite[17][0],1,CAR_WIDTH*CAR_HEIGHT,f);
517
 
518
  err = DOS_error();
519
 
520
  cprintf("Read %d bytes from fumo.raw...\n", myfilebuf_length);
521
 
522
  if (err) {
523
    cprintf("Error %d reading fumo.raw...\n", err);
524
    myfilebuf_length = 0;
525
  }
526
 
527
  DOS_fclose(f);
528
 
529
}
530