Rev 1454 | Rev 1456 | 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 | ------------ |
||
1455 | giacomo | 21 | CVS : $Id: orbit.c,v 1.7 2004-05-23 12:58:46 giacomo Exp $ |
1085 | pj | 22 | |
23 | File: $File$ |
||
1455 | giacomo | 24 | Revision: $Revision: 1.7 $ |
25 | Last update: $Date: 2004-05-23 12:58:46 $ |
||
1085 | pj | 26 | ------------ |
27 | */ |
||
28 | |||
29 | /* |
||
30 | * Copyright (C) 2000 Giorgio Buttazzo and Paolo Gai |
||
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 | /* SIMULAZIONE DI MASSE CHE ORBITANO */ |
||
50 | /****************************************************************/ |
||
51 | |||
52 | #include <kernel/kern.h> |
||
53 | #include <semaphore.h> |
||
54 | #include <stdlib.h> |
||
55 | #include <math.h> |
||
1453 | giacomo | 56 | #include <string.h> |
1085 | pj | 57 | |
1453 | giacomo | 58 | #include <drivers/shark_fb26.h> |
59 | #include <drivers/shark_keyb26.h> |
||
60 | |||
1454 | giacomo | 61 | #define MAX 10 /* numero massimo pianeti */ |
1085 | pj | 62 | #define XMAX 639 /* valore massimo coordinata X */ |
63 | #define YMAX 479 /* valore massimo coordinata Y */ |
||
64 | |||
1453 | giacomo | 65 | extern int vga16color[16]; |
66 | |||
1085 | pj | 67 | struct coord { |
68 | double x; |
||
69 | double y; |
||
70 | }; |
||
71 | |||
72 | int flen; // file length |
||
73 | |||
1453 | giacomo | 74 | float mass[MAX]; /* vettore masse pianeti */ |
1085 | pj | 75 | struct coord pos[MAX]; /* vettore posizioni attuali */ |
76 | struct coord vel[MAX]; /* vettore velocita' iniziali */ |
||
77 | |||
78 | int XGS, YGS; /* Coordinate centro spazio */ |
||
79 | int RP, RT; /* raggio pianeta, raggio Terra */ |
||
80 | int np; /* numero attuale di pianeti */ |
||
1453 | giacomo | 81 | float G; /* Gravitazione Universale */ |
82 | float tick; /* tick di sistema */ |
||
83 | float delta; /* incremento temporale */ |
||
84 | float scala; /* fattore grafico di scala */ |
||
1085 | pj | 85 | |
1453 | giacomo | 86 | char fbuf[1000] ="\ |
87 | ----------------------------------------------------\n\ |
||
1454 | giacomo | 88 | period: 20000 wcet: 1000\n\ |
1453 | giacomo | 89 | delta: 1. scala: 150.\n\ |
90 | G: 6.6e-15\n\ |
||
91 | r_pianeta: 4 r_Terra: 8\n\ |
||
92 | X_centro: 320 Y_centro: 240\n\ |
||
93 | ------------------- pianeti ------------------------\n\ |
||
94 | massa pos.x pos.y vel.x vel.y\n\ |
||
1455 | giacomo | 95 | 0: \t6.0e21 \t0. \t0. \t0. \t0. \n\ |
96 | 1: \t1.0e21 \t10000. \t8000. \t-60. \t0. \n\ |
||
97 | 2: \t1.0e8 \t5000. \t0. \t0. \t80. \n\ |
||
98 | 3: \t5.0e18 \t10000. \t8000. \t-50. \t0. \n\ |
||
99 | 4: \t1.0e9 \t10000. \t8000. \t-40. \t20. \n\ |
||
100 | 5: \t1.0e15 \t1000. \t5000. \t-80. \t0. \n\ |
||
101 | 6: \t1.0e5 \t1000. \t5000. \t-80. \t0. \n\ |
||
102 | 7: \t1.0e17 \t1000. \t5000. \t-80. \t0. \n\ |
||
103 | 8: \t1.0e5 \t1000. \t5000. \t-80. \t0. \n\ |
||
104 | 9: \t1.0e5 \t1000. \t5000. \t-80. \t0. \n\ |
||
105 | 10: \t1.0e5 \t1000. \t5000. \t-80. \t0. \n\ |
||
1453 | giacomo | 106 | ----------------------------------------------------\n"; |
107 | |||
1085 | pj | 108 | // ------------------------------------------------------- |
109 | // NOTA: %f o %lf significa double e %nf significa float |
||
110 | // ------------------------------------------------------- |
||
111 | |||
112 | PID pid; |
||
113 | int period; /* task period */ |
||
114 | int wcet; /* task wcet */ |
||
115 | sem_t mutex; /* semaforo di mutua esclusione */ |
||
116 | |||
117 | |||
118 | void get_par(void); |
||
119 | |||
120 | /*--------------------------------------------------------------*/ |
||
121 | |||
122 | int inside(int x, int y) |
||
123 | { |
||
124 | return ((x > RP) && (x < XMAX-RP) && |
||
125 | (y > RP) && (y < YMAX-RP)); |
||
126 | } |
||
127 | |||
128 | /*--------------------------------------------------------------*/ |
||
129 | |||
130 | TASK massa(void *xxx) |
||
131 | { |
||
132 | int i = (int)xxx; /* parametro del task */ |
||
133 | int gx, gy; /* coordinate grafiche pallina */ |
||
134 | int ox, oy; /* vecchia posizione pallina */ |
||
135 | int j; |
||
136 | int r, col; /* raggio e colore pianeta */ |
||
137 | double dt; /* incremento temporale */ |
||
138 | double dist=0.0, dx, dy; /* distanze pianeta-pianeta */ |
||
139 | double dist0=0.0; /* distanze pianeta-Terra */ |
||
140 | double x, y; /* posizione del pianeta */ |
||
141 | double vx, vy; /* velocita' del pianeta */ |
||
142 | double ax, ay; /* accelerazione del pianeta */ |
||
143 | double k; /* variabile ausiliaria */ |
||
144 | double arg; /* variabile di appoggio */ |
||
145 | |||
146 | x = pos[i].x; y = pos[i].y; |
||
147 | vx = vel[i].x; vy = vel[i].y; |
||
148 | ox = XGS + x / scala; |
||
149 | oy = YGS + y / scala; |
||
150 | dt = delta; |
||
151 | |||
152 | do { |
||
153 | x = pos[i].x; |
||
154 | y = pos[i].y; |
||
155 | ax = ay = 0.0; |
||
156 | for (j=0; j<np; j++) { |
||
157 | if (j != i) { |
||
158 | dx = pos[j].x - x; |
||
159 | dy = pos[j].y - y; |
||
160 | arg = dx*dx + dy*dy; |
||
161 | dist = sqrt(arg); |
||
162 | if (dist < RP*scala) dist = RP*scala; |
||
163 | k = G * mass[j] / (dist*dist*dist); |
||
164 | ax += k * dx; |
||
165 | ay += k * dy; |
||
166 | } |
||
167 | if (j == 0) dist0 = dist - (RP+RT)*scala; |
||
168 | } |
||
169 | x += vx*dt + 0.5*ax*dt*dt; |
||
170 | y += vy*dt + 0.5*ay*dt*dt; |
||
171 | vx += ax * dt; |
||
172 | vy += ay * dt; |
||
173 | |||
174 | gx = XGS + x / scala; |
||
175 | gy = YGS + y / scala; |
||
176 | |||
177 | r = RP; |
||
178 | col = i + 1; |
||
179 | |||
180 | sem_wait(&mutex); |
||
1453 | giacomo | 181 | grx_disc(ox,oy,r,vga16color[0]); |
182 | grx_disc(gx,gy,r,vga16color[col]); |
||
1085 | pj | 183 | sem_post(&mutex); |
184 | |||
185 | pos[i].x = x; pos[i].y = y; |
||
186 | ox = gx; oy = gy; |
||
187 | |||
188 | task_endcycle(); |
||
189 | |||
190 | } while ((dist0 > 0) && inside(gx,gy)); |
||
191 | |||
192 | sem_wait(&mutex); |
||
1453 | giacomo | 193 | grx_disc(ox,oy,r,vga16color[0]); |
194 | grx_disc(XGS,YGS,RT,vga16color[12]); |
||
195 | grx_circle(XGS,YGS,RT,vga16color[14]); |
||
1085 | pj | 196 | sem_post(&mutex); |
197 | |||
198 | return NULL; |
||
199 | } |
||
200 | |||
201 | /*--------------------------------------------------------------*/ |
||
202 | /* MAIN */ |
||
203 | /*--------------------------------------------------------------*/ |
||
204 | |||
1111 | pj | 205 | int main() |
1085 | pj | 206 | { |
207 | HARD_TASK_MODEL m; |
||
208 | char c; /* carattere letto da tastiera */ |
||
209 | |||
210 | sem_init(&mutex,0,1); |
||
211 | |||
212 | get_par(); |
||
213 | |||
1453 | giacomo | 214 | grx_disc(XGS,YGS,RT,vga16color[12]); |
215 | grx_circle(XGS,YGS,RT,vga16color[14]); |
||
1085 | pj | 216 | |
217 | np = 0; |
||
218 | |||
219 | do { |
||
220 | if (np < MAX-1) { |
||
221 | np++; |
||
222 | hard_task_default_model(m); |
||
223 | hard_task_def_arg (m, (void *)np); |
||
224 | hard_task_def_wcet (m, wcet); |
||
225 | hard_task_def_mit (m, period); |
||
226 | hard_task_def_usemath (m); |
||
227 | pid = task_create("massa", massa, &m, NULL); |
||
228 | if (pid == NIL) { |
||
1159 | pj | 229 | sys_shutdown_message("Could not create task"); |
230 | sys_end(); |
||
1085 | pj | 231 | } |
232 | task_activate(pid); |
||
233 | } |
||
234 | c = keyb_getch(BLOCK); |
||
235 | |||
236 | } while (c != ESC); |
||
237 | |||
238 | sys_end(); |
||
239 | |||
240 | return 0; |
||
241 | } |
||
242 | |||
243 | /*------------------------------------------------------*/ |
||
244 | /* file reading */ |
||
245 | /*------------------------------------------------------*/ |
||
246 | |||
247 | void read_file(void) |
||
248 | { |
||
249 | int err; |
||
250 | DOS_FILE *fp; |
||
251 | |||
252 | fp = DOS_fopen("orbit.dat","r"); |
||
253 | |||
254 | if (!fp) { |
||
255 | err = DOS_error(); |
||
256 | cprintf("Error %d opening myfile.txt...\n", err); |
||
257 | flen = 0; |
||
258 | return; |
||
259 | } |
||
260 | |||
261 | flen = DOS_fread(&fbuf, 1, 1000, fp); |
||
262 | cprintf("Read %d bytes from orbit.dat\n", flen); |
||
263 | |||
264 | DOS_fclose(fp); |
||
265 | } |
||
266 | |||
267 | /*------------------------------------------------------*/ |
||
268 | /* get data from buffer */ |
||
269 | /*------------------------------------------------------*/ |
||
270 | |||
271 | void get_par(void) |
||
272 | { |
||
273 | int x = 0; |
||
274 | int i; |
||
1454 | giacomo | 275 | float vx, vy, px, py; |
276 | char temp[100]; |
||
277 | |||
1453 | giacomo | 278 | flen = strlen(fbuf); |
1085 | pj | 279 | |
280 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
281 | x++; |
||
282 | sscanf(&fbuf[x], "%d", &period); |
||
283 | |||
284 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
285 | x++; |
||
286 | sscanf(&fbuf[x], "%d", &wcet); |
||
287 | |||
288 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
289 | x++; |
||
290 | sscanf(&fbuf[x], "%f", &delta); |
||
291 | |||
292 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
293 | x++; |
||
294 | sscanf(&fbuf[x], "%f", &scala); |
||
295 | |||
296 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
297 | x++; |
||
298 | sscanf(&fbuf[x], "%f", &G); |
||
299 | |||
300 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
301 | x++; |
||
302 | sscanf(&fbuf[x], "%d", &RP); |
||
303 | |||
304 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
305 | x++; |
||
306 | sscanf(&fbuf[x], "%d", &RT); |
||
307 | |||
308 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
309 | x++; |
||
310 | sscanf(&fbuf[x], "%d", &XGS); |
||
311 | |||
312 | while ((fbuf[x] != ':') && (x < flen)) x++; |
||
313 | x++; |
||
314 | sscanf(&fbuf[x], "%d", &YGS); |
||
315 | |||
316 | for (i=0; i<MAX; i++) { |
||
1455 | giacomo | 317 | |
318 | mass[i] = 0.; |
||
319 | px = 0.; |
||
320 | py = 0.; |
||
321 | vx = 0.; |
||
322 | vy = 0.; |
||
323 | |||
1454 | giacomo | 324 | while ((fbuf[x] != '\t') && (x < flen)) x++; |
325 | x++; |
||
1085 | pj | 326 | sscanf(&fbuf[x], "%f", &mass[i]); |
327 | |||
328 | while ((fbuf[x] != '\t') && (x < flen)) x++; |
||
329 | x++; |
||
1454 | giacomo | 330 | sscanf(&fbuf[x], "%f", &px); |
1085 | pj | 331 | |
332 | while ((fbuf[x] != '\t') && (x < flen)) x++; |
||
333 | x++; |
||
1454 | giacomo | 334 | sscanf(&fbuf[x], "%f", &py); |
1085 | pj | 335 | |
336 | while ((fbuf[x] != '\t') && (x < flen)) x++; |
||
337 | x++; |
||
338 | sscanf(&fbuf[x], "%f", &vx); |
||
339 | |||
340 | while ((fbuf[x] != '\t') && (x < flen)) x++; |
||
341 | x++; |
||
342 | sscanf(&fbuf[x], "%f", &vy); |
||
1454 | giacomo | 343 | |
344 | sprintf(temp,"[M = %f, Px = %f, Py = %f, Vx = %f, Vy = %f]",mass[i],px,py,vx,vy); |
||
345 | grx_text(temp, 10, 20+10*i, vga16color[15],0); |
||
346 | |||
347 | pos[i].x = px; pos[i].y = py; |
||
348 | |||
1085 | pj | 349 | vel[i].x = vx; vel[i].y = vy; |
1454 | giacomo | 350 | |
1085 | pj | 351 | } |
352 | } |
||
353 | |||
354 | /*--------------------------------------------------------------*/ |
||
355 |