Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1659 giacomo 1
/*************************************************************
2
 *                                                           *
3
 *           Stabilizzazione di un pendolo inverso           *
4
 *                                                           *
5
 *                                                           *
6
 *Written by: Gabriele Bolognini - Scuola S.Anna -- Pisa     *
7
 *                                                           *
8
 *************************************************************/
9
 
10
#include "demo.h"
11
#include "kernel/func.h"
12
 
13
#include "kernel/kern.h"
14
#include <stdlib.h>
15
 
16
#include "drivers/keyb.h"
17
#include "drivers/glib.h"
18
#include "ll/i386/x-dos.h"
19
#include "modules/cabs.h"
20
#include "modules/hartport.h"
21
#include "string.h"
22
 
23
#include <math.h>
24
#include "pclab.h"
25
#include "modules/pi.h"
26
#include "modules/nop.h"
27
 
28
#include "cost.h"   /* Contiene le costanti del task DISEGNA  */
29
 
30
#define NSAMPLES  500
31
#define NTASKS  10
32
 
33
 
34
TIME timetab[NSAMPLES][NTASKS];
35
static int index[MAX_PROC];
36
char dest[10000];
37
 
38
 
39
 
40
mutex_t mutex;
41
void app_mutex_init(mutex_t *);
42
 
43
#define LUNGH 35
44
#define THETAMAX 45
45
#define VDANG 2.4
46
#define NMAX 500
47
 
48
/*----- ACHTUNG!!!: param. iniz. del guadagno, deadline, ecc.----------- */
49
#define INIPARAMS {{ 2.3 , 300 , 0.5 , 15 },1.,1.0,20.,20., 0.1,  1 , 1}
50
#define MODPARAMS {{ 1. , 300 , 0.9 , 34 },0.74 ,1.,20.,20., 0.2,  1 , 1}
51
struct Parametri{
52
  float GUAD[4];
53
  float COST;
54
  float NOISE;
55
  float WCUT;
56
  float WCUT1;
57
  float OFFSVAL;
58
  int DEADLINE;
59
  int SCAN;
60
} prm=INIPARAMS, prmbase=INIPARAMS, prmmod=MODPARAMS;
61
#define DEADSECX (prm.DEADLINE * 0.001 * prm.SCAN)/*mit task CONTROLLO in sec*/
62
#define DEADSECTH (prm.DEADLINE * 0.001 ) /* mit task CONTROLLO in sec*/
63
 
64
struct  Data_cab1 {
65
	float   rif;
66
	int     imp;
67
};
68
 
69
struct  Data_cab2 {
70
	float   x;
71
	float   y;
72
};
73
 
74
CAB     cab1, cab2;
75
 
76
float vmax, vmin, vmaxth, vminth;
77
 
78
 
79
float vin=0, vout, yout, thout;
80
 
81
 
82
//int sysid=0, startid=0; /* usate nell'identificazione del sistema */
83
 
84
 
85
 
86
void    graphframe(void);
87
void    my_end(void);
88
void    scritt(void);
89
void    initial(void);
90
 
91
 
92
 
93
void scenario_jetcontrol(void);
94
void init_jetcontrol(void);
95
TASK jetslide_task(void *);
96
TASK jetctrl_task(void *);
97
TASK jetdummy_task(void *);
98
 
99
 
100
void framegrabber_close(void *);
101
TASK camera_task(void *);
102
TASK tracking_task(void *);
103
 
104
 
105
extern int white, black, red, gray, green, blue;
106
 
107
 
108
 
109
int da_motor(float v)
110
{
111
  da_conv( 2.5 , 2 );
112
  da_conv( v + 2.5 , 1 );
113
  return(0);
114
 
115
}
116
float v2x(float v)
117
{ /* trasforma i  volt in x */
118
  float x;
119
 
120
  x= LUNGH * (v-(vmax+vmin)/2 ) / (vmax-vmin);
121
  return x;
122
}
123
 
124
float v2theta(float v)
125
{ /* trasforma i  volt in angoli */
126
  float theta;
127
 
128
  theta=2.0*(THETAMAX/FCA) * (v-(vmaxth+vminth)/2 ) / (vmaxth-vminth);
129
  return theta;
130
}
131
 
132
 
133
float bass1(float u)
134
{
135
 
136
  float y;
137
  static float oldy=0;
138
  y=(oldy + prm.WCUT * DEADSECX *u)/(1+ prm.WCUT * DEADSECX);
139
  oldy=y;
140
  return y;
141
}
142
float bass2(float u)
143
{
144
 
145
  float y;
146
  static float oldy=0;
147
  y=(oldy + prm.WCUT1 * DEADSECX *u)/(1+ prm.WCUT1 * DEADSECX);
148
  oldy=y;
149
  return y;
150
}
151
float bass3(float u)
152
{
153
 
154
  float y;
155
  static float oldy=0;
156
  y=(oldy + prm.WCUT * DEADSECTH *u)/(1+ prm.WCUT * DEADSECTH);
157
  oldy=y;
158
  return y;
159
}
160
float bass4(float u)
161
{
162
 
163
  float y;
164
  static float oldy=0;
165
  y=(oldy + prm.WCUT1 * DEADSECTH *u)/(1+ prm.WCUT1 * DEADSECTH);
166
  oldy=y;
167
  return y;
168
}
169
 
170
float  dx(float u)
171
{
172
  static float oldu=0;
173
  float y;
174
  y=(u-oldu)/DEADSECX;
175
  oldu=u;
176
  return y;
177
}
178
float  dth(float u)
179
{
180
  static float oldu=0;
181
  float y;
182
  y=(u-oldu)/DEADSECTH;
183
  oldu=u;
184
  return y;
185
}
186
 
187
 
188
TASK    eval(void)
189
{
190
  int x,y;
191
  char str[100];
192
 
193
        mutex_lock(&mutex);
194
 
195
        grx_rect(10,325,XTI - 20,479,blue);
196
	x = 20;
197
	y = 350;
198
	grx_text("PARAMETRI DI CONTROLLO ( R , T ) ", x+60,  y-18 , red, 0);
199
	grx_text("Guad=",  x, y,green, 0);y+=18;
200
	grx_text("Cost=",  x, y,green, 0); y+=18;
201
 
202
	grx_text("WCUT=",  x, y,green, 0); y+=18;
203
	grx_text("WCUT1=",  x, y,green, 0); y+=18;
204
	grx_text("NOISE=",  x, y,green, 0); y+=18;
205
	grx_text("DEADL=",  x, y,green, 0);y+=18;
206
	grx_text("OFFS=",  x, y,green, 0);y+=18;
207
 
208
	{  /* legenda */
209
	  int x=280, y=350;
210
	  grx_text("Guad  1,2  3,4  5,6  7,8",  x, y,green, 0);y+=18;
211
	  grx_text("Cost  [ , ]",  x, y,green, 0); y+=18;
212
	  grx_text("WCUT  o , p", x , y,green, 0); y+=18;
213
	  grx_text("WCUT1 k , l",  x, y,green, 0); y+=18;
214
	  grx_text("NOISE n , m",  x, y,green, 0); y+=18;
215
	  grx_text("DEADL - , +",  x, y,green, 0);y+=18;
216
	  grx_text("OFFS  / , *",  x, y,green, 0);y+=18;
217
	}
218
	{  /* frame dei valori rilevati dai sensori */
219
	  int x=15, y=YTI+10;
220
	  grx_rect(10,YTI,XTI - 20,YTI+70,red);
221
	  grx_text("V_com =    ",  x, y,green, 0);y+=14;
222
	  grx_text("V+offs =   ",  x, y,green, 0);y+=14;
223
	  grx_text("V_sens_x = ",  x, y,green, 0);y+=14;
224
	  grx_text("SCAN = ",  x, y,green, 0);y+=14;
225
 
226
	}
227
 
228
	mutex_unlock(&mutex);
229
  while(1){
230
	x = 70;
231
	y = 350;
232
 
233
	mutex_lock(&mutex);
234
 
235
	grx_text("                         ",  x, y, 12, 0);
236
	sprintf(str, "%6.2f  %6.2f  %6.2f  %6.2f ", prm.GUAD[0],prm.GUAD[1],prm.GUAD[2],prm.GUAD[3]);
237
	grx_text(str, x, y, blue, 0);y+=18;
238
 
239
	grx_text("                ",  x, y, 12, 0);
240
	sprintf(str, "%6.2f ", prm.COST);
241
	grx_text(str, x, y, blue, 0); y += 18;
242
 
243
	grx_text("               ",  x, y, 12, 0);
244
	sprintf(str, "%6.2f  Hz", prm.WCUT);
245
	grx_text(str, x, y, blue, 0); y += 18;
246
 
247
	grx_text("              ",  x, y, 12, 0);
248
	sprintf(str, "%6.2f Hz ", prm.WCUT1);
249
	grx_text(str, x, y, blue, 0); y += 18;
250
 
251
	grx_text("              ",  x, y, 12, 0);
252
	sprintf(str, "%6.2f Volt ", prm.NOISE);
253
	grx_text(str, x, y, blue, 0); y += 18;
254
 
255
	grx_text("               ",  x, y, 12, 0);
256
	sprintf(str, "%d ticks ", prm.DEADLINE);
257
	grx_text(str, x, y, blue, 0); y += 18;
258
 
259
	grx_text("               ",  x, y, 12, 0);
260
	sprintf(str, "%6.2f Volt ", prm.OFFSVAL);
261
	grx_text(str, x, y, blue, 0); y += 18;
262
 
263
	mutex_unlock(&mutex);
264
 
265
 
266
 
267
	/* scrivo vin , vout (vin+offs) , SCAN e PARAMETRIBASE*/
268
	{ int x=95, y= YTI + 10;
269
 
270
	  mutex_lock(&mutex);
271
 
272
	  grx_text("              ", x, y,  0,  0);
273
	  sprintf(str, "%7.4f ", vin);
274
	  grx_text(str, x, y, blue, 0);y += 14;
275
 
276
	  grx_text("               ", x, y,  0,  0);
277
	  sprintf(str, "%7.4f",  vout);
278
	  grx_text(str, x, y, blue, 0);y += 14;
279
 
280
	  grx_text("         ", x, y,  0,  0);
281
	  sprintf(str, "%7.3f", yout);
282
	  grx_text(str, x,  y , blue, 0); y += 14;
283
 
284
	  grx_text("         ", x, y,  0,  0);
285
	  sprintf(str, "%3.2d  c , v", prm.SCAN);
286
	  grx_text(str, x,  y , blue, 0); y += 14;
287
 
288
	  mutex_unlock(&mutex);
289
	}
290
 
291
 
292
 
293
    task_endcycle();
294
  }
295
}
296
/*------------------------------------------------------------------------
297
  Il task CARRELLO controlla la posizione del carrello
298
 
299
	y = posizione angolare dell'asta (non usata)
300
	x = posizione lineare del carrello
301
 
302
-----------------------------------------------------------------------*/
303
TASK    carr2(void)
304
{
305
  float   thist, y[2], yp[2],x3ist=0;
306
  while(1) {
307
 
308
	if(contat%prm.SCAN==0) yout=ad_conv(11);
309
 
310
	/* converto i valori dei sensori in posizioni */
311
	/* calcolo le velocita' lineari e angolari opportunamente filtrate */
312
	if(contat%prm.SCAN == 0) {
313
	  x3ist=v2x(yout);
314
	  y[0]=bass1(x3ist);
315
	  yp[0] = bass2(dx(y[0]));
316
	}
317
 
318
 
319
    task_endcycle();
320
  }
321
}
322
TASK    carrello(void)
323
{
324
  float   thist, y[2], yp[2],x3ist=0;          /* valori attuali   */
325
  PORT    pa, pb;                          /* communication ports  */
326
  struct  Data_cab1   data;
327
  char *pun;
328
  int i;
329
  float offset;
330
 
331
  long int k=0, contat=0;
332
  long int stime =0 ;
333
  /* Si creano 2 per mandare al task DISEGNA le var.di stato x1 e x3 */
334
 
335
  pa = port_create("porta1",sizeof(float),1,STICK,WRITE);
336
  pb = port_create("porta2",sizeof(float),1,STICK,WRITE);
337
 
338
  while (1) {
339
 
340
    /* prendo i dati sul riferimento in volt*/
341
	pun = cab_getmes(cab1);
342
	memcpy(&data, pun, sizeof(struct Data_cab1));
343
	cab_unget(cab1, pun);
344
 
345
    /* prendo i dati sulle posizioni dal trasduttore ADC  */
346
//	if(contat%prm.SCAN==0) yout=ad_conv(11);
347
	thout=ad_conv(10);
348
 
349
	/* taglio brusco dei livelli di rumore sopra il NOISE */
350
	{
351
#define AVR 10
352
	  static float oyout[AVR], othout[AVR];
353
	  float avy, avth;
354
	  static int index=0, flag=1;
355
 
356
	  if(flag==1) {
357
	    for(i=0; i<AVR;++i) { oyout[i]=yout; othout[i] = thout;}
358
	    flag=0;
359
	  }
360
	  avy=avth=0;
361
	  for(i=0;i<AVR;++i) { avy += oyout[i]; avth += othout[i]; }
362
	  avy /= AVR; avth /= AVR ;
363
 
364
	  if(fabs(yout-avy)>=prm.NOISE) { yout=avy;}
365
	  if(fabs(thout-avth)>=prm.NOISE) { thout=avth;}
366
 
367
	  oyout[index]=yout;  othout[index]=thout;
368
	  index = (index+1) % AVR ;
369
	}
370
 
371
	/* converto i valori dei sensori in posizioni */
372
	/* calcolo le velocita' lineari e angolari opportunamente filtrate */
373
//	if(contat%prm.SCAN == 0) {
374
//	  x3ist=v2x(yout);
375
//	  y[0]=bass1(x3ist);
376
//	  yp[0] = bass2(dx(y[0]));
377
//	}
378
	thist=v2theta(thout);
379
	y[1]=bass3(thist);
380
	yp[1] = bass4(dth(y[1]));
381
 
382
	/* PARAMETRI del controllore :1 */
383
	  vin = prm.COST * (prm.GUAD[0] * (x3ist-v2x(data.rif))+ prm.GUAD[1] * thist + prm.GUAD[2] * yp[0] + prm.GUAD[3] * yp[1]) ;
384
 
385
 
386
	/* metto un offset che mi elimina l'attrito */
387
	if(  vin >= 0) offset=prm.OFFSVAL;
388
	else offset=-prm.OFFSVAL;
389
 
390
	vout=vin+offset;
391
 
392
	/* !!!! se si e' scelta la procedura di identificazione */
393
	/*	if(sysid != 0){
394
	  if(startid == 0) {vin=0.0; vout=0.0;}
395
	  else if(startid == 1 ) {
396
	    if(stime==0 ) stime = sys_gettime(NULL);
397
	    if((sys_gettime(NULL)-stime)/1000 <= 400 )  {vin= -1.7; vout=-2.4;}
398
	    else  {vin= 0.0; vout=0.0;}
399
	  }
400
	}
401
	*/
402
	if(vout >= VDANG ) vout = VDANG ;
403
	if(vout <= -VDANG ) vout = -VDANG ;
404
	/* la applico   */
405
	da_motor(vout);
406
 
407
 
408
	/* mando i dati sulle posizioni al task DISEGNA */
409
	port_send(pa, &y[1], NON_BLOCK);
410
	port_send(pb, &y[0], NON_BLOCK);
411
 
412
 
413
	contat++;
414
	task_endcycle();
415
  }
416
  /* Eliminazione porte di comunicazione */
417
  port_delete(pa);
418
  port_delete(pb);
419
 
420
}   /* FINE DEL TASK CARRELLO */
421
 
422
 
423
 
424
 
425
/*--------------------------------------------------------------*/
426
/* Il task DISEGNA riceve dal task CONTROLLO i valori delle */
427
/* variabili x1 e x3 e disegna il pendolo corrispondente    */
428
/*--------------------------------------------------------------*/
429
 
430
TASK    diseg(void)
431
{
432
WORD xp, yp, oxp, oyp;   /* coordinate carrello  */
433
WORD xa, ya, oxa, oya;   /* coordinate asta  */
434
float   x1, x3;         /* variabili di stato   */
435
char    str[100];
436
PORT    pra, prb, prc;       /* communication ports  */
437
int delta;          /* dimensione finestre  */
438
 
439
 
440
	/* Connessione alle porte create dal task CONTROLLO */
441
	pra = port_connect("porta1",sizeof(float),STICK,READ);
442
	prb = port_connect("porta2",sizeof(float),STICK,READ);
443
 
444
 
445
	delta = 120;
446
 
447
	/* Inizializzazione variabili carrello e asta    */
448
	xp = oxp = 100;
449
	xa = oxa = 100;
450
	yp = oyp = 105 + delta - H;
451
	ya = oya = 105 + delta - H;
452
 
453
 
454
	graphframe();
455
	while (1) {
456
 
457
 
458
	/* disegna il pendolo */
459
 
460
		port_receive(pra, &x1, BLOCK);
461
		port_receive(prb, &x3, BLOCK);
462
 
463
 
464
 
465
		/* scrivo posizione del carrello e angolo dell'asta */
466
		mutex_lock(&mutex);
467
 
468
		grx_text("       ", XTI+45, YTI+28,  0,  0);
469
		sprintf(str, "%5.2f", x3);
470
		grx_text(str, XTI+45, YTI+28, blue, 0);
471
 
472
		grx_text("     ", XTI+50, YTI+42,  0,  0);
473
		sprintf(str, "%4.3f", x1 * FCA );
474
		grx_text(str, XTI+50, YTI+42, blue, 0);
475
 
476
		mutex_unlock(&mutex);
477
 
478
 
479
		/* calcolo ascissa del punto P del carrello */
480
		xp = XMED + x3 * SGX;
481
 
482
 
483
		/* calcolo ascissa e ordinata dell'estremo dell'asta */
484
		xa = xp + LA * sin((double)(x1 * SGA));
485
		ya = yp - LA * cos((double)(x1 * SGA));
486
 
487
		mutex_lock(&mutex);
488
 
489
		/* cancella e disegna il carrello */
490
		grx_box(oxp-LC,oyp,oxp+LC,oyp+H,0);
491
		grx_box(xp-LC,yp,xp+LC,yp+H,blue);
492
 
493
		/* cancella e disegna la ruota sinistra del carrello */
494
		grx_circle(oxp-LC+8,oyp+H+4,RAGGIO+2,0);
495
		grx_circle(xp-LC+8,yp+H+4,RAGGIO+2,green);
496
 
497
		/* cancella e disegna la ruota destra del carrello */
498
		grx_circle(oxp+LC-8,oyp+H+4,RAGGIO+2,0);
499
		grx_circle(xp+LC-8,yp+H+4,RAGGIO+2,green);
500
 
501
		/* cancella e disegna l'asta */
502
		grx_line(oxp,oyp,oxa,oya,0);
503
		grx_line(xp,yp,xa,ya,red);
504
 
505
		/* cancella e disegna la pallina sull'asta */
506
		grx_circle(oxa,oya,RAGGIO,0);
507
		grx_circle(xa,ya,RAGGIO,blue);
508
 
509
		mutex_unlock(&mutex);
510
 
511
		/* aggiornamento variabili */
512
		oxp = xp;
513
		oyp = yp;
514
		oxa = xa;
515
		oya = ya;
516
 
517
 
518
 
519
 
520
		task_endcycle();
521
 
522
	}
523
 
524
	port_disconnect(pra);
525
	port_disconnect(prb);
526
	port_disconnect(prc);
527
 
528
 
529
} /* FINE TASK DISEGNA */
530
 
531
 
532
TASK    query(void)
533
{
534
char c, str[100];
535
int     count = 0;
536
char *pun;
537
struct Data_cab1    data={0,0};
538
float xrif=0, orif=1;
539
  while(1)
540
    {
541
 
542
       do {
543
	  c = keyb_getch(BLOCK);
544
 
545
 
546
	  switch (c) {
547
	    case 'd':   xrif += 1; break;
548
	    case 's':   xrif -= 1; break;
549
	    case 'w':    xrif=7; break;
550
	    case 'q':    xrif=-7; break;
551
	    case '0':   xrif =0 ; break;
552
 
553
//	    case 'z':   startid = 1; break;
554
 
555
	    case '1':   prm.GUAD[0] -= 0.1 ; break;
556
	    case '2':   prm.GUAD[0] += 0.1; break;
557
	    case '3':   prm.GUAD[1] -= 10 ; break;
558
	    case '4':   prm.GUAD[1] += 10 ; break;
559
	    case '5':   prm.GUAD[2] -= 0.1; break;
560
	    case '6':   prm.GUAD[2] += 0.1; break;
561
	    case '7':   prm.GUAD[3] -= 0.5; break;
562
	    case '8':   prm.GUAD[3] += 0.5; break;
563
 
564
	    case 'o':   prm.WCUT -= 5. ; break;
565
	    case 'p':   prm.WCUT += 5. ; break;
566
	    case 'k':   prm.WCUT1 -= 5. ; break;
567
	    case 'l':   prm.WCUT1 += 5. ; break;
568
	    case 'n':   prm.NOISE -= 0.1; break;
569
	    case 'm':   prm.NOISE += 0.1; break;
570
 
571
	    case '/':   prm.OFFSVAL -= 0.1; break;
572
	    case '*':   prm.OFFSVAL += 0.1; break;
573
 
574
	    case '[':   prm.COST -= 0.05; break;
575
	    case ']':   prm.COST += 0.05; break;
576
 
577
	    case 'c':   prm.SCAN -= 1; break;
578
	    case 'v':   prm.SCAN += 1; break;
579
 
580
 
581
 
582
	    case 'R':   prm = prmbase; break;
583
	    case 'T':   prm = prmmod; break;
584
 
585
 
586
 
587
	    default:    break;
588
	  }
589
 
590
 
591
	  data.rif=(vmax+vmin)/2. + xrif * (vmax-vmin)/LUNGH;
592
 
593
 
594
 
595
	  /* controllo che il riferim non vada fuori scala massima */
596
	  if (data.rif >  vmax) data.rif =  vmax;
597
	  if (data.rif <  vmin) data.rif =  vmin;
598
 
599
	  /* controllo l'impulso */
600
	  if (count>0) count--;
601
	  if (data.imp != 0 && count==0) data.imp = 0;
602
 
603
	  /* scrivo sul cab1 i riferim ( rif + imp) per il carrello */
604
	  pun = cab_reserve(cab1);
605
	  memcpy(pun, &data, sizeof(struct Data_cab1));
606
	  cab_putmes(cab1, pun);
607
 
608
 
609
 
610
	  /* si visualizza il riferimento del carrello. */
611
 
612
	  if (xrif != orif) {
613
	    orif = xrif;
614
	    mutex_lock(&mutex);
615
	    grx_text("       ", XTI+45, YTI+14, 0, 0);
616
	    sprintf(str, "%2.1f cm.", xrif);
617
	    grx_text(str, XTI+55, YTI+14, green, 0);
618
	    mutex_unlock(&mutex);
619
	  }
620
 
621
	} while (c != 27);
622
 
623
	sys_end();
624
 
625
	task_endcycle();
626
    }
627
}
628
 
629
int printglob;
630
TASK timejet(void *arg)
631
{
632
  TIME table[JET_TABLE_DIM];
633
  int k, nistan;
634
  PID i;
635
 
636
  int printed;
637
 
638
  for (;;) {
639
    for (i=2, printed=0; i<MAX_PROC && printed<JET_NTASK; i++) {
640
      if ( (nistan=jet_gettable(i,(TIME *) &table, -1)) != -1) {
641
	for(k=0;k<nistan;k++){
642
	  if(index[i] >= NSAMPLES) break;
643
	  if( i>NTASKS ) sys_end();
644
	  timetab[ index[i] ][i] = table[k];
645
	  index[i]++;
646
	}
647
        printed++;
648
      }
649
    }
650
    printglob=printed;
651
    task_endcycle();
652
  }
653
}
654
 
655
 
656
/******************   *   MAIN   *    ******************************/
657
 
658
 
659
 
660
void    main(int argc, char **argv)
661
{
662
 
663
int     modenum;
664
 
665
 
666
HARD_TASK_MODEL    m;
667
SOFT_TASK_MODEL    md, mt;
668
PID         pc,pd, pe, pq, pt;
669
 
670
 
671
 
672
white = rgb16(255,255,255);
673
black = rgb16(0,0,0);
674
red   = rgb16(255,0,0);
675
gray  = rgb16(128,128,128);
676
green = rgb16(0,255,0);
677
blue  = rgb16(0,0,255);
678
 
679
	sys_atrunlevel((void *) my_end,NULL, RUNLEVEL_BEFORE_EXIT);
680
	sys_atrunlevel((void *) scritt,NULL, RUNLEVEL_AFTER_EXIT);
681
 
682
 
683
 
684
	modenum = grx_getmode(800, 600, 16);
685
	if (modenum == -1) {
686
	cprintf("Errore in grx_mode");
687
	sys_end();
688
	exit(2);
689
	}
690
	if (grx_setmode(modenum) == -1) {
691
	cprintf("Non posso andare in modo grafico");
692
	sys_end();
693
	exit(3);
694
	}
695
 
696
 
697
	initial();
698
 
699
	app_mutex_init(&mutex);
700
 
701
	scenario_jetcontrol();
702
 
703
 
704
	init_framegrabber();
705
 
706
 
707
 
708
	/* Creazione dei CAB   */
709
	cab1 = cab_create("cab1", sizeof(struct Data_cab1), 3);
710
	cab2 = cab_create("cab2", sizeof(struct Data_cab2), 3);
711
	/* Creazione dei task */
712
 
713
	hard_task_default_model(m);
714
	hard_task_def_mit(m, PERIOD_CARRELLO  );
715
	hard_task_def_wcet(m,WCET_CARRELLO );
716
	hard_task_def_group(m,2);
717
	hard_task_def_ctrl_jet(m);
718
	hard_task_def_usemath(m);
719
 
720
	soft_task_default_model(md);
721
	soft_task_def_level(md,1);
722
	soft_task_def_period(md,PERIOD_DESIGN);
723
	soft_task_def_met(md, WCET_DESIGN);
724
	soft_task_def_group(md, 2);
725
	soft_task_def_ctrl_jet(md);
726
	soft_task_def_usemath(md);
727
 
728
 
729
	soft_task_default_model(mt);
730
	soft_task_def_level(mt,1);
731
	soft_task_def_period(mt,PERIOD_TIMEJET);
732
	soft_task_def_met(mt, WCET_TIMEJET);
733
	soft_task_def_group(mt, 2);
734
	soft_task_def_ctrl_jet(mt);
735
	soft_task_def_usemath(mt);
736
 
737
	init_jetcontrol();
738
 
739
	pc = task_create("carrello",(TASK) carrello, &m, NULL);
740
	if (pc == -1) {
741
		sys_end();
742
		exit(4);
743
	}
744
 
745
 
746
	pe = task_create("eval",(TASK) eval,&md, NULL);
747
	if (pe == -1) {
748
		sys_end();
749
		exit(4);
750
	}
751
 
752
 
753
	pd = task_create("diseg",(TASK) diseg,&md,NULL);
754
	if (pd == -1) {
755
		sys_end();
756
		exit(4);
757
	}
758
 
759
	pq = task_create("query",(TASK) query,&md,NULL);
760
	if (pq == -1) {
761
		sys_end();
762
		exit(4);
763
	}
764
	pt = task_create("timejet",(TASK) timejet,&md,NULL);
765
	if (pt == -1) {
766
		sys_end();
767
		exit(4);
768
	}
769
 
770
 
771
 
772
		/* Attivo i task per controllare e disegnare il carrello */
773
	task_activate(pt);
774
	task_activate(pc);
775
	task_activate(pd);
776
	task_activate(pe);
777
	task_activate(pq);
778
 
779
 
780
 
781
	group_activate(1);
782
 
783
 
784
 
785
}
786
 
787
/****************************************************************/
788
/* initial                                                     */
789
/****************************************************************/
790
 
791
void    initial(void)
792
{
793
int x, y;
794
 
795
 
796
	grx_rect(1,   1, 639, 243, blue);  /* dialog box pendolo  */
797
	da_motor(0.);
798
 
799
	x = 10;
800
	y = YTI;
801
	grx_text("INIZIALIZZ.",  x, y, blue, 0); y += 42;
802
	grx_text("Carr a sx e premi un tasto", x, y, green, 0);y += 28;
803
	keyb_getchar();
804
	vmin=ad_conv(11);
805
 
806
	grx_text("Carr a dx e premi un tasto",x,y,green,0); y += 28;
807
	keyb_getchar();
808
	vmax=ad_conv(11);
809
 
810
	grx_text("Asta a sx e premi un tasto", x, y, green, 0);y += 28;
811
	keyb_getchar();
812
	vminth=ad_conv(10);
813
 
814
	grx_text("Asta a dx e premi un tasto", x, y, green, 0);y += 28;
815
	keyb_getchar();
816
	vmaxth=ad_conv(10);
817
 
818
	/*	grx_text("Premi 'y' per l' identificazione ", x, y, green, 0);y += 28;
819
	if(keyb_getchar() == 'y' ) sysid=1;
820
	else sysid=0 ;
821
 
822
	if(sysid==1) {
823
	grx_text("MODALITA' IDENTIFICAZIONE!!!!", x, y, green, 0);y += 14;
824
	grx_text("Premi un tasto per iniziare", x, y, green, 0);y += 14;
825
	keyb_getchar();
826
	}
827
	*/
828
	grx_clear(0);
829
 
830
 
831
}
832
 
833
/****************************************************************/
834
/* graphframe                                                    */
835
/****************************************************************/
836
void    graphframe(void)
837
{
838
int x, y;
839
 
840
        mutex_lock(&mutex);
841
	grx_rect(XTI-15,245,639,479,blue);  /* finestra di stato    */
842
	grx_rect(1,   1, 450 /*639*/, 243, blue);  /* dialog box pendolo  */
843
 
844
 
845
	x = XTI;
846
	y = YTI;
847
	grx_text("PENDOLO",         x, y, red, 0); y += 14;
848
	grx_text("Rif.:",           x, y, green, 0); y += 14;
849
 
850
	grx_text("X =",             x,    y,  green,  0); y += 14;
851
	grx_text("Ang =",           x,    y,  green,  0); y += 14;
852
 
853
 
854
	grx_text("s sx, d dx",      x,    y,  green,  0); y += 14;
855
	grx_text("q: xrif-> -7 ",   x,    y,  green,  0); y += 14;
856
	grx_text("p: xrif-> +7 ",   x,    y,  green,  0); y += 14;
857
	grx_text("0: xrif->  0 ",   x,    y,  green,  0); y += 14;
858
 
859
	grx_text("S imp-,d imp+",   x,    y,  green,  0); y += 14;
860
	mutex_unlock(&mutex);
861
}
862
 
863
/*--------------------------------------------------------------*/
864
/*      This function is called at system termination       */
865
/*--------------------------------------------------------------*/
866
void     my_end(void)
867
{
868
  da_motor(0.0);
869
  grx_close();
870
  sys_end();
871
}
872
/*--------------------------------------------------------------*/
873
/*     This function writes the data in the file DATA           */
874
/*--------------------------------------------------------------*/
875
 
876
 
877
void    scritt(void)
878
{
879
  DOS_FILE *file;
880
  long int i , k;
881
  char  str[500];
882
 
883
 
884
  for(i=2;i<11;i++) {
885
    sprintf(str,"%s ",proc_table[i].name);
886
    strcat(dest,str);
887
  }
888
  strcat(dest,"\n");
889
 
890
 
891
  for(k=0;k<500;k++){
892
    for(i=2;i<11;i++) {
893
      sprintf(str,"%ld ",timetab[k][i]);
894
      strcat(dest,str);
895
    }
896
    strcat(dest,"\n");
897
    }
898
  file = DOS_fopen("data","w");
899
  DOS_fwrite(dest,1,strlen(dest),file);
900
  DOS_fclose(file);
901
 
902
}
903
 
904
 
905