Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1357 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
 ------------
21
 CVS :        $Id: fly2.c,v 1.1 2004-03-30 06:46:08 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1 $
25
 Last update: $Date: 2004-03-30 06:46:08 $
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 (with death of flies)         */
50
/*--------------------------------------------------------------*/
51
 
52
#include <kernel/kern.h>
53
#include <drivers/glib.h>
54
#include <drivers/keyb.h>
55
#include <semaphore.h>
56
#include <stdlib.h>
57
#include <math.h>
58
 
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                 */
67
#define ESC      27             /* ASCII code of ESCAPE key     */
68
#define MAX_P    35             /* max number of flies          */
69
#define FLYGROUP 1
70
 
71
double  tick = 1.0;             /* system tick = 1 ms           */
72
int     fly_period = 40000;     /* task period                  */
73
int     fly_wcet = 1000;        /* task wcet                    */
74
PID     pid;
75
sem_t   mutex;
76
 
77
 
78
sem_t   mutex_i;        /* mutex to protect i */
79
int  i = 0;             /* number of tasks created      */
80
 
81
 
82
/*--------------------------------------------------------------*/
83
 
84
void    draw_fly(int x, int y, int c)
85
{
86
        sem_wait(&mutex);
87
        grx_disc(x, y, D, c);
88
        sem_post(&mutex);
89
}
90
 
91
void    draw_i(void)
92
{
93
        char s[50];
94
 
95
        sem_wait(&mutex_i);
96
        sprintf(s,"i=%d  ",i);
97
        sem_post(&mutex_i);
98
 
99
        sem_wait(&mutex);
100
        grx_text(s, XMIN+200, YMENU+30, 12, 0);
101
        sem_post(&mutex);
102
}
103
 
104
int get_i(void)
105
{
106
    int j;
107
 
108
    sem_wait(&mutex_i);
109
    j = i;
110
    sem_post(&mutex_i);
111
 
112
    return j;
113
}
114
 
115
void i_minus_1(void)
116
{
117
    sem_wait(&mutex_i);
118
    i--;
119
    sem_post(&mutex_i);
120
}
121
 
122
void i_plus_1(void)
123
{
124
    sem_wait(&mutex_i);
125
    i++;
126
    sem_post(&mutex_i);
127
}
128
 
129
 
130
 
131
/******************************************************************/
132
 
133
TASK    fly(void *arg)
134
{
135
int     x, y;
136
int     ox, oy;
137
int     dx, dy, da;
138
int     teta, col;
139
int     outx, outy;
140
double  r;
141
int     local_i = (int)arg;
142
int     number_of_iterations;
143
 
144
        x = ox = (XMIN+XMAX)/2;
145
        y = oy = (YMIN+YMAX)/2;
146
        teta = 0;
147
        col = 2 + local_i;                    /* colore fly           */
148
        number_of_iterations = 200;
149
 
150
        while (number_of_iterations) {
151
 
152
                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
153
                teta += da;
154
 
155
                if (teta > 360) teta -= 360;
156
                if (teta < 0) teta += 360;
157
                r = (double)teta * PI / 180.;
158
 
159
                dx = (float)(VEL * cos(r));
160
                dy = (float)(VEL * sin(r));
161
                x += dx;
162
                y += dy;
163
 
164
                outx = (x >= XMAX) || (x <= XMIN);
165
                outy = (y >= YMAX) || (y <= YMIN);
166
 
167
                if (outx || outy) {
168
                        x = x - dx;
169
                        y = y - dy;
170
                        if (outx) teta = 180 - teta;
171
                        if (outy) teta = -teta;
172
                        if (teta > 360) teta -= 360;
173
                        if (teta < 0) teta += 360;
174
                        r = (double)teta * PI / 180.;
175
 
176
                        dx = (float)(VEL * cos(r));
177
                        dy = (float)(VEL * sin(r));
178
 
179
                        x += dx;
180
                        y += dy;
181
                }
182
 
183
                draw_fly(ox, oy, 0);
184
                draw_fly(x, y, col);
185
                ox = x; oy = y;
186
 
187
                number_of_iterations--;
188
 
189
                task_endcycle();
190
        }
191
 
192
        draw_fly(x, y, 0);
193
 
194
        i_minus_1();
195
 
196
        draw_i();
197
        return 0;
198
}
199
 
200
/****************************************************************/
201
 
202
/* This function is called when the system exits */
203
void byebye(void *arg)
204
{
205
  grx_close();
206
  cprintf("Bye Bye!\n");
207
}
208
 
209
/****************************** MAIN ******************************/
210
 
211
int main(int argc, char **argv)
212
{
213
    HARD_TASK_MODEL m;
214
 
215
    char c;             /* character from keyboard      */
216
    TIME seme;          /* used to init the random seed */
217
    int local_i;        /* local copy of i */
218
 
219
    /* Set the closing function */
220
    sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);
221
 
222
    sem_init(&mutex,0,1);
223
    sem_init(&mutex_i,0,1);
224
 
225
    /* graphic card Initialization */
226
    if (grx_init() < 1) {
227
       sys_abort(1);
228
    }
229
 
230
    if (grx_open(640, 480, 8) < 0) {
231
        cprintf("GRX Err\n");
232
        sys_abort(1);
233
    }
234
 
235
    /* The scenario */
236
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, 14);
237
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, 13, 0);
238
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, 12, 0);
239
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, 12, 0);
240
    draw_i();
241
 
242
 
243
    /* The program waits a space to create a fly */
244
    c = keyb_getch(BLOCK);
245
 
246
    /* randomize!!!! */
247
    seme = sys_gettime(NULL);
248
    srand(seme);
249
 
250
    do {
251
        local_i = get_i();
252
        if ((c == ' ') && (local_i < MAX_P)) {
253
            hard_task_default_model(m);
254
            hard_task_def_ctrl_jet (m);
255
            hard_task_def_arg      (m, (void *)local_i);
256
            hard_task_def_wcet     (m, fly_wcet);
257
            hard_task_def_mit      (m, fly_period);
258
            hard_task_def_group    (m, FLYGROUP);
259
            hard_task_def_usemath  (m);
260
            pid = task_create("fly", fly, &m, NULL);
261
            if (pid == NIL) {
262
              grx_close();
263
              perror("Could not create task <fly>");
264
              sys_abort(1);
265
            }
266
            task_activate(pid);
267
            i_plus_1();
268
            draw_i();
269
        }
270
        c = keyb_getch(BLOCK);
271
 
272
    } while (c != ESC);
273
 
274
    sys_end();
275
 
276
    return 0;
277
}
278
 
279
/*--------------------------------------------------------------*/