Subversion Repositories shark

Rev

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
 ------------
1482 giacomo 21
 CVS :        $Id: fly.c,v 1.9 2004-06-15 14:10:23 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1482 giacomo 24
 Revision:    $Revision: 1.9 $
25
 Last update: $Date: 2004-06-15 14:10:23 $
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>
1482 giacomo 53
#include <modules/sem.h>
1085 pj 54
#include <stdlib.h>
55
#include <math.h>
56
 
1382 giacomo 57
#include <drivers/shark_keyb26.h>
1377 giacomo 58
#include <drivers/shark_fb26.h>
59
 
1085 pj 60
#define YMENU    10             /* menu level                   */
61
#define XMIN     50
62
#define XMAX     600
63
#define YMIN     100
64
#define YMAX     450
65
#define VEL      5              /* linear velocity (def. = 5)   */
66
#define ANG      30             /* angolo massimo sterzata (30) */
67
#define D        3              /* raggio mosca                 */
1377 giacomo 68
#undef ESC
1085 pj 69
#define ESC      27             /* ASCII code of ESCAPE key     */
70
#define MAX_P    35             /* max number of flies          */
71
#define FLYGROUP 1
72
 
73
double  tick = 1.0;             /* system tick = 1 ms           */
74
int     fly_period = 40000;     /* task period                  */
75
int     fly_wcet = 1000;        /* task wcet                    */
76
PID     pid;
77
 
1482 giacomo 78
sem_t   grx_mutex;
79
 
1085 pj 80
/*--------------------------------------------------------------*/
81
 
82
void    draw_fly(int x, int y, int c)
83
{
84
        grx_disc(x, y, D, c);
85
}
86
 
87
/******************************************************************/
88
 
89
TASK    fly(void *arg)
90
{
91
int     x, y;
92
int     ox, oy;
93
int     dx, dy, da;
1386 giacomo 94
int     teta, col,red;
1085 pj 95
int     outx, outy;
96
double  r;
97
int     i = (int)arg;
98
 
99
        x = ox = (XMIN+XMAX)/2;
100
        y = oy = (YMIN+YMAX)/2;
101
        teta = 0;
1398 giacomo 102
        red = 100+10*i;
103
        if (red > 255) red = 255;
1386 giacomo 104
        col = rgb16(red,0,50); /* colore fly           */
1085 pj 105
 
106
        while (1) {
107
 
108
                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
109
                teta += da;
110
 
111
                if (teta > 360) teta -= 360;
112
                if (teta < 0) teta += 360;
113
                r = (double)teta * PI / 180.;
114
 
115
                dx = (float)(VEL * cos(r));
116
                dy = (float)(VEL * sin(r));
117
                x += dx;
118
                y += dy;
119
 
120
                outx = (x >= XMAX) || (x <= XMIN);
121
                outy = (y >= YMAX) || (y <= YMIN);
122
 
123
                if (outx || outy) {
124
                        x = x - dx;
125
                        y = y - dy;
126
                        if (outx) teta = 180 - teta;
127
                        if (outy) teta = -teta;
128
                        if (teta > 360) teta -= 360;
129
                        if (teta < 0) teta += 360;
130
                        r = (double)teta * PI / 180.;
131
 
132
                        dx = (float)(VEL * cos(r));
133
                        dy = (float)(VEL * sin(r));
134
 
135
                        x += dx;
136
                        y += dy;
137
                }
138
 
1482 giacomo 139
                sem_wait(&grx_mutex);
140
                  draw_fly(ox, oy, 0);
141
                  draw_fly(x, y, col);
142
                  ox = x; oy = y;
143
                sem_post(&grx_mutex);
1085 pj 144
 
145
                task_endcycle();
146
        }
147
}
148
 
149
/****************************** MAIN ******************************/
150
 
151
int main(int argc, char **argv)
152
{
153
    HARD_TASK_MODEL m;
154
 
155
    char c;             /* character from keyboard      */
156
    int  i = 0;         /* number of tasks created      */
157
    TIME seme;          /* used to init the random seed */
158
 
1482 giacomo 159
    /* Init the mutex */
160
    sem_init(&grx_mutex,0,1);
161
 
1085 pj 162
    /* The scenario */
1386 giacomo 163
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, rgb16(255,255,255));
164
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, rgb16(255,255,255), 0);
165
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, rgb16(255,255,255), 0);
166
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, rgb16(255,255,255), 0);
1085 pj 167
 
168
    /* The program waits a space to create a fly */
169
    c = keyb_getch(BLOCK);
170
 
171
    /* randomize!!!! */
172
    seme = sys_gettime(NULL);
173
    srand(seme);
174
 
175
    do {
176
        if ((c == ' ') && (i < MAX_P)) {
177
            hard_task_default_model(m);
178
            hard_task_def_ctrl_jet (m);
179
            hard_task_def_arg      (m, (void *)i);
180
            hard_task_def_wcet     (m, fly_wcet);
181
            hard_task_def_mit      (m, fly_period);
182
            hard_task_def_group    (m, FLYGROUP);
183
            hard_task_def_usemath  (m);
184
            pid = task_create("fly", fly, &m, NULL);
185
            if (pid == NIL) {
1377 giacomo 186
              sys_shutdown_message("Could not create task <fly>");
1382 giacomo 187
              sys_end();
1377 giacomo 188
              return 0;
1085 pj 189
            }
190
            task_activate(pid);
191
            i++;
192
        }
193
        c = keyb_getch(BLOCK);
194
 
195
    } while (c != ESC);
196
 
1382 giacomo 197
    sys_end();
1085 pj 198
 
199
    return 0;
1377 giacomo 200
 
1085 pj 201
}
202
 
203
/*--------------------------------------------------------------*/