Subversion Repositories shark

Rev

Rev 1385 | Rev 1398 | Go to most recent revision | Details | Compare with Previous | 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
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
1386 giacomo 21
 CVS :        $Id: fly.c,v 1.7 2004-04-19 14:11:07 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1386 giacomo 24
 Revision:    $Revision: 1.7 $
25
 Last update: $Date: 2004-04-19 14:11:07 $
1085 pj 26
 ------------
27
**/
28
 
29
/*
30
 * Copyright (C) 2000 Paolo Gai and Giorgio Buttazzo
31
 *
32
 * This program is free software; you can redistribute it and/or modify
33
 * it under the terms of the GNU General Public License as published by
34
 * the Free Software Foundation; either version 2 of the License, or
35
 * (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45
 *
46
 */
47
 
48
/*--------------------------------------------------------------*/
49
/*              SIMULATION OF RANDOM FLIES                      */
50
/*--------------------------------------------------------------*/
51
 
52
#include <kernel/kern.h>
53
#include <stdlib.h>
54
#include <math.h>
55
 
1382 giacomo 56
#include <drivers/shark_keyb26.h>
1377 giacomo 57
#include <drivers/shark_fb26.h>
58
 
1085 pj 59
#define YMENU    10             /* menu level                   */
60
#define XMIN     50
61
#define XMAX     600
62
#define YMIN     100
63
#define YMAX     450
64
#define VEL      5              /* linear velocity (def. = 5)   */
65
#define ANG      30             /* angolo massimo sterzata (30) */
66
#define D        3              /* raggio mosca                 */
1377 giacomo 67
#undef ESC
1085 pj 68
#define ESC      27             /* ASCII code of ESCAPE key     */
69
#define MAX_P    35             /* max number of flies          */
70
#define FLYGROUP 1
71
 
72
double  tick = 1.0;             /* system tick = 1 ms           */
73
int     fly_period = 40000;     /* task period                  */
74
int     fly_wcet = 1000;        /* task wcet                    */
75
PID     pid;
76
 
77
/*--------------------------------------------------------------*/
78
 
79
void    draw_fly(int x, int y, int c)
80
{
81
        grx_disc(x, y, D, c);
82
}
83
 
84
/******************************************************************/
85
 
86
TASK    fly(void *arg)
87
{
88
int     x, y;
89
int     ox, oy;
90
int     dx, dy, da;
1386 giacomo 91
int     teta, col,red;
1085 pj 92
int     outx, outy;
93
double  r;
94
int     i = (int)arg;
95
 
96
        x = ox = (XMIN+XMAX)/2;
97
        y = oy = (YMIN+YMAX)/2;
98
        teta = 0;
1386 giacomo 99
        red = 50+10*i;
100
        col = rgb16(red,0,50); /* colore fly           */
1085 pj 101
 
102
        while (1) {
103
 
104
                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
105
                teta += da;
106
 
107
                if (teta > 360) teta -= 360;
108
                if (teta < 0) teta += 360;
109
                r = (double)teta * PI / 180.;
110
 
111
                dx = (float)(VEL * cos(r));
112
                dy = (float)(VEL * sin(r));
113
                x += dx;
114
                y += dy;
115
 
116
                outx = (x >= XMAX) || (x <= XMIN);
117
                outy = (y >= YMAX) || (y <= YMIN);
118
 
119
                if (outx || outy) {
120
                        x = x - dx;
121
                        y = y - dy;
122
                        if (outx) teta = 180 - teta;
123
                        if (outy) teta = -teta;
124
                        if (teta > 360) teta -= 360;
125
                        if (teta < 0) teta += 360;
126
                        r = (double)teta * PI / 180.;
127
 
128
                        dx = (float)(VEL * cos(r));
129
                        dy = (float)(VEL * sin(r));
130
 
131
                        x += dx;
132
                        y += dy;
133
                }
134
 
135
                draw_fly(ox, oy, 0);
136
                draw_fly(x, y, col);
137
                ox = x; oy = y;
138
 
139
                task_endcycle();
140
        }
141
}
142
 
143
/****************************** MAIN ******************************/
144
 
145
int main(int argc, char **argv)
146
{
147
    HARD_TASK_MODEL m;
148
 
149
    char c;             /* character from keyboard      */
150
    int  i = 0;         /* number of tasks created      */
151
    TIME seme;          /* used to init the random seed */
152
 
153
    /* The scenario */
1386 giacomo 154
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, rgb16(255,255,255));
155
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, rgb16(255,255,255), 0);
156
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, rgb16(255,255,255), 0);
157
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, rgb16(255,255,255), 0);
1085 pj 158
 
159
    /* The program waits a space to create a fly */
160
    c = keyb_getch(BLOCK);
161
 
162
    /* randomize!!!! */
163
    seme = sys_gettime(NULL);
164
    srand(seme);
165
 
166
    do {
167
        if ((c == ' ') && (i < MAX_P)) {
168
            hard_task_default_model(m);
169
            hard_task_def_ctrl_jet (m);
170
            hard_task_def_arg      (m, (void *)i);
171
            hard_task_def_wcet     (m, fly_wcet);
172
            hard_task_def_mit      (m, fly_period);
173
            hard_task_def_group    (m, FLYGROUP);
174
            hard_task_def_usemath  (m);
175
            pid = task_create("fly", fly, &m, NULL);
176
            if (pid == NIL) {
1377 giacomo 177
              sys_shutdown_message("Could not create task <fly>");
1382 giacomo 178
              sys_end();
1377 giacomo 179
              return 0;
1085 pj 180
            }
181
            task_activate(pid);
182
            i++;
183
        }
184
        c = keyb_getch(BLOCK);
185
 
186
    } while (c != ESC);
187
 
1382 giacomo 188
    sys_end();
1085 pj 189
 
190
    return 0;
1377 giacomo 191
 
1085 pj 192
}
193
 
194
/*--------------------------------------------------------------*/