Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */



/**
 ------------
 CVS :        $Id: auto.c,v 1.1.1.1 2002-09-02 09:37:42 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.1.1.1 $
 Last update: $Date: 2002-09-02 09:37:42 $
 ------------
**/


/*
 * Copyright (C) 2000 Marco Dallera and Marco Fiocca
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


/*
 *              AUTO
 *
 * Another Unuseful Track simulatOr
 *
 * Authors: Marco Dallera
 *          Marco Fiocca
 *
 */


#include "include/auto.h"
#include "include/const.h"
#include "include/utils.h"

int car_number;
car_params cars[MAX_CAR_NUMBER];
char drivers[DRIVERS_NUMBER][MAX_DRIVER_NAME_LENGTH] = {
  "M.Schumacher",
  "D.Coulthard",
  "R.Schumacher",
  "M.Hakkinen",
  "R.Barrichello",
  "J.P.Montoya",
  "E.Irvine",
  "J.Trulli",
  "H.Frentzen",
  "J.Villeneuve",
  "J.Alesi",
  "G.Fisichella",
  "O.Panis",
  "O.Wurz",
  "J.Button",
  "P.P.Diniz",
  "P.De La Rosa",
  "T.Marques",
  "R.Zonta",
  "G.Mazzacane"
};

extern char sprite[18][CAR_WIDTH*CAR_HEIGHT];

/* Semaphores */
sem_t grx_mutex;

/* Useful colors */
int red       = rgb16(255,   0,   0);
int green     = rgb16(  0, 255,   0);
int blue      = rgb16(  0,   0, 255);
int lightgray = rgb16(192, 192, 192);

/* JetCTRL Initialization */
void init_jetcontrol(void);

/* -------------------------------------------------------*/

/* --------------------- */
/*  Handling car status  */
/* --------------------- */

int set_car_status(car_status cs, CAB cab_id) {
  char *msg;

  msg = cab_reserve(cab_id);
  memcpy(msg, &cs, CAR_MSG_DIM);
  return(cab_putmes(cab_id, msg));
}

car_status get_car_status(CAB cab_id) {
  char *msg;
  car_status status;

  msg = cab_getmes(cab_id);
  memcpy(&status, msg, CAR_MSG_DIM);
  cab_unget(cab_id, msg);

  return (status);
}

/* ---------------------------- */
/*  Handling road informations  */
/* ---------------------------- */

int set_road_info(road_info info, CAB road_status_cab) {
  char *msg;

  msg = cab_reserve(road_status_cab);
  memcpy(msg, &info, ROAD_MSG_DIM);
  return(cab_putmes(road_status_cab, msg));
}

road_info get_road_info(CAB road_status_cab) {
  char *msg;
  road_info info;

  msg = cab_getmes(road_status_cab);
  memcpy(&info, msg, ROAD_MSG_DIM);
  cab_unget(road_status_cab, msg);

  return (info);
}

/* -------------------------------------------------------*/

/* Closing function */
void byebye()
{
  grx_close();

  // we need clear to reposition the cursor in the right place after
  // going back from the graphical mode.
  clear();

  kern_printf("Race is over!\n");
}

/* -------------------------------------------------------*/

/* ------------------- */
/*  Hard car creation  */
/* ------------------- */

void hard_car_create() {
  car_params cp;
  HARD_TASK_MODEL sensor_m, control_m;
  PID sensor_pid, control_pid;
  TIME seed;          /* used to init the random seed */
  int drv_ind;

  if (car_number >= MAX_CAR_NUMBER)     return;

  /* Randomize!!!! */
  seed = sys_gettime(NULL);
  srand(seed);

  /* CAB creation */
  cp.road_status_cab = cab_create("road_cab", ROAD_MSG_DIM, ROAD_MSG_READER);
  cp.car_status_cab = cab_create("car_cab", CAR_MSG_DIM, CAR_MSG_READER);

  /* Car parameters initialization */
  cp.max_speed = rand_01();
  cp.min_acc = cp.max_speed;
  cp.max_acc = cp.max_speed;
  cp.rage = cp.max_speed;
  cp.color = 3+car_number;
  cp.number = car_number;
  drv_ind = DRIVERS_NUMBER-1 - (int)(((cp.rage-0.8+0.005) * (float)DRIVERS_NUMBER) / 0.2);

  /* Sets driver's name without duplications */
  strcpy(cp.driver, drivers[drv_ind]);
  while (!strcmp(cp.driver,"***"))
    strcpy(cp.driver, drivers[++drv_ind%DRIVERS_NUMBER]);
  strcpy(drivers[drv_ind],"***");

  cars[car_number] = cp;

  /* ------------ */
  /*  Task Calls  */
  /* ------------ */

  /* sensor task creation */
  hard_task_default_model(sensor_m);

  hard_task_def_arg(sensor_m, (void *)car_number);
  hard_task_def_wcet(sensor_m, SENSOR_WCET);
  hard_task_def_mit(sensor_m, SENSOR_PERIOD);
  hard_task_def_usemath(sensor_m);
  sensor_pid = task_create("camera", sensor, &sensor_m, NULL);
  if(sensor_pid == -1)
    sys_end();

  /* control task creation */
  hard_task_default_model(control_m);
  hard_task_def_arg(control_m, (void *)car_number);
  hard_task_def_wcet(control_m, CONTROL_WCET);
  hard_task_def_mit(control_m, CONTROL_PERIOD);
  hard_task_def_usemath(control_m);
  control_pid = task_create("controller", control, &control_m, NULL);
  if(control_pid == -1)
    sys_end();

  cars[car_number].sensor_pid = sensor_pid;
  cars[car_number].control_pid = control_pid;
  car_number++; // increases cars number

  task_activate(sensor_pid);
  task_activate(control_pid);
}

/* -------------------------------------------------------*/

/* ------------------- */
/*  Soft car creation  */
/* ------------------- */

void soft_car_create() {
  car_params cp;
  SOFT_TASK_MODEL sensor_m, control_m;
  PID sensor_pid, control_pid;
  TIME seed;          /* used to init the random seed */
  int drv_ind;

  if (car_number >= MAX_CAR_NUMBER)     return;

  /* Randomize!!!! */
  seed = sys_gettime(NULL);
  srand(seed);

  /* CAB creation */
  cp.road_status_cab = cab_create("road_cab", ROAD_MSG_DIM, ROAD_MSG_READER);
  cp.car_status_cab = cab_create("car_cab", CAR_MSG_DIM, CAR_MSG_READER);

  /* Car parameters initialization */
  cp.max_speed = rand_01();
  cp.min_acc = cp.max_speed;
  cp.max_acc = cp.max_speed;
  cp.rage = cp.max_speed;
  cp.color = 8+car_number;
  cp.number = car_number;
  drv_ind = DRIVERS_NUMBER-1 - (int)(((cp.rage-0.8+0.005) * (float)DRIVERS_NUMBER) / 0.2);

  /* Sets driver's name without duplications */
  strcpy(cp.driver, drivers[drv_ind]);
  while (!strcmp(cp.driver,"***"))
    strcpy(cp.driver, drivers[++drv_ind%DRIVERS_NUMBER]);
  strcpy(drivers[drv_ind],"***");
  cars[car_number] = cp;

  /* ------------ */
  /*  Task Calls  */
  /* ------------ */

  /* sensor task creation */
  soft_task_default_model(sensor_m);

  soft_task_def_arg(sensor_m, (void *)car_number);
  soft_task_def_met(sensor_m, SENSOR_WCET);
  soft_task_def_period(sensor_m, SENSOR_PERIOD);
  soft_task_def_usemath(sensor_m);
  sensor_pid = task_create("camera", sensor, &sensor_m, NULL);
  if(sensor_pid == -1)
    sys_end();

  /* control task creation */
  soft_task_default_model(control_m);
  soft_task_def_arg(control_m, (void *)car_number);
  soft_task_def_met(control_m, CONTROL_WCET);
  soft_task_def_period(control_m, CONTROL_PERIOD);
  soft_task_def_usemath(control_m);
  control_pid = task_create("controller", control, &control_m, NULL);
  if(control_pid == -1)
    sys_end();

  cars[car_number].sensor_pid = sensor_pid;
  cars[car_number].control_pid = control_pid;
  car_number++; // increases cars number

  task_activate(sensor_pid);
  task_activate(control_pid);
}

/* -------------------------------------------------------*/

/* ------------------- */
/*  The main function  */
/* ------------------- */

int main(int argc, char **argv) {
  TIME seed;          /* used to init the random seed */

  if (MAX_CAB < 30) {
    cprintf("The application needs at least 30 CABS.\n"
            "Please set the MAX_CAB #define to 30 or more...\n"
            "...(see include/modules/cabs.h)\n");
    sys_end();
  }

  /* Set the exception handler */
  set_exchandler_grx();

  /* Set the closing function */
  sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);

  /* Keyboard handling */
  keyb_handler();
  kern_printf("KEYBOARD initialized...\n");

  /* Graphics mutex */
  sem_init(&grx_mutex, 0, 1);

  /* Graphics init */
  grx_init();
  grx_open(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BIT_COLORS);

  /* Track drawing */
  sem_wait(&grx_mutex);
  grx_setcolor(46,0,0,0);
  grx_setcolor(43,255,255,255);
  grx_setcolor(26,255,74,33);
  grx_setcolor(27,255,255,255);
  grx_setcolor(1,8,8,8);
  grx_setcolor(3,255,0,0);
  grx_setcolor(28,255,50,0);
  grx_setcolor(29,255,90,0);
  grx_setcolor(30,255,130,0);
  grx_setcolor(31,255,170,0);
  grx_setcolor(32,255,210,0);
  grx_setcolor(33,48,48,48);

  grx_putimage(TRACK_X1, TRACK_Y1, TRACK_X2, TRACK_Y2, track_img);
  sem_post(&grx_mutex);

  /* Command and info display */
  cmd_display();

  /* JetCTRL Initialization */
  init_jetcontrol();

  /* Randomize!!!! */
  seed = sys_gettime(NULL);
  srand(seed);

  car_number = 0;

  /* ------------ */
  /*  Task Calls  */
  /* ------------ */

  hard_car_create();

  return 0;
}

/* -------------------------------------------------------*/

void read_track(int i)
{
  DOS_FILE *f;  // DOS file descriptor
  int file_length;
  int index;
  char filename[100];
  int err;      // Error code

  /* Init track_list */
  track_init();

  /* Set the selected track from track_list */
  if (i < TRACK_NUMBER)
    index = i;
  else
    index = 0;
  cprintf("Selected index = %d\n", index);

  strcpy(filename, strcat("tracks/", track_list[index].name));
  cprintf("File to open = %s\n", filename);
  track_list[index].selected = 1;

  /* open the DOS file for reading      (you can specify only "r" or "w") */
  f = DOS_fopen(filename,"r");

  /* check for open errors */
  if (!f) {
    err = DOS_error();  // error!!

    /* note that if you call DOS_error() here, it return 0!!! */
    cprintf("Error %d opening %s...\n", err, filename);
    file_length = 0;
    byebye();
    return;
  }

  /* read track file */
  file_length = DOS_fread(&track_img, 1, TRACK_WIDTH*TRACK_HEIGHT, f);

  /* check for errors */
  err = DOS_error();

  cprintf("Read %d bytes from %s...\n", file_length, filename);

  if (err) {
    cprintf("Error %d reading %s...\n", err, filename);
    file_length = 0;
    /* there is not return because I want to close the file! */
  }

  /* Close the file */
  cprintf("Closing file %s...\n", filename);
  DOS_fclose(f);
  cprintf("File %s closed successfully!\n", filename);

}

/* -------------------------------------------------------*/

void read_sprites()
{
  /* DOS file descriptor */
  DOS_FILE *f;
  int myfilebuf_length,i;
  char filename[100];

        /* Error code */
  int err;
  i=0;

  for ( i=0; i < 16; i++) {
    sprintf(filename,"cars/car%d.raw",i);
    f = DOS_fopen(filename,"r");

    // check for open errors
    if (!f) {
      // error!!
      err = DOS_error();

      // note that if you call DOS_error() here, it return 0!!!
      cprintf("Error %d opening %s...\n", err, filename);
      myfilebuf_length = 0;
      return;
    }

    // read from file
    myfilebuf_length = DOS_fread(&sprite[i][0],1,CAR_WIDTH*CAR_HEIGHT,f);

    // check for errors
    err = DOS_error();

    cprintf("Read %d bytes from %s...\n", myfilebuf_length, filename);

    if (err) {
      cprintf("Error %d reading %s...\n", err, filename);
      myfilebuf_length = 0;
      // there is not return because I want to close the file!
    }

    // Close the file
    DOS_fclose(f);
  }

  f = DOS_fopen("cars/boom.raw","r");
  if (!f) {
    err = DOS_error();

    cprintf("Error %d opening boom.raw...\n", err);
    myfilebuf_length = 0;
    return;
  }

  myfilebuf_length = DOS_fread(&sprite[16][0],1,CAR_WIDTH*CAR_HEIGHT,f);

  err = DOS_error();

  cprintf("Read %d bytes from boom.raw...\n", myfilebuf_length);

  if (err) {
    cprintf("Error %d reading boom.raw...\n", err);
    myfilebuf_length = 0;
  }

  DOS_fclose(f);

  f = DOS_fopen("cars/fumo.raw","r");
  if (!f) {
    err = DOS_error();

    cprintf("Error %d opening fumo.raw...\n", err);
    myfilebuf_length = 0;
    return;
  }

  myfilebuf_length = DOS_fread(&sprite[17][0],1,CAR_WIDTH*CAR_HEIGHT,f);

  err = DOS_error();

  cprintf("Read %d bytes from fumo.raw...\n", myfilebuf_length);

  if (err) {
    cprintf("Error %d reading fumo.raw...\n", err);
    myfilebuf_length = 0;
  }

  DOS_fclose(f);

}