Rev 1656 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1655 | giacomo | 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 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
10 | * |
||
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 | #include <ll/i386/defs.h> |
||
20 | |||
21 | #include <drivers/pclab.h> |
||
22 | |||
23 | #include <math.h> |
||
24 | #include <stdlib.h> |
||
25 | #include <kernel/log.h> |
||
26 | #include <GL/osmesa.h> |
||
27 | #include <GL/glut.h> |
||
28 | |||
29 | #include <modules/hartport.h> |
||
30 | #include <kernel/kern.h> |
||
31 | #include <kernel/func.h> |
||
32 | #include <ll/i386/x-dos.h> |
||
33 | |||
1661 | giacomo | 34 | #include <drivers/shark_keyb26.h> |
35 | #include <drivers/shark_fb26.h> |
||
36 | |||
1655 | giacomo | 37 | #include "const.h" |
38 | |||
39 | #define WIDTH 640 |
||
40 | #define HEIGHT 480 |
||
41 | #define BYTES_PP 2 |
||
42 | |||
43 | unsigned long int PERIOD_CARRELLO = 10000; |
||
44 | unsigned long int PERIOD_DISEGNA = 80000; |
||
45 | struct Parametri prm=PARAM; |
||
46 | |||
47 | unsigned long int WCET_CARRELLO, WCET_DISEGNA; |
||
48 | |||
49 | TASK carrello(void *); |
||
50 | |||
51 | PID carrello_PID, disegna_PID; |
||
52 | |||
53 | unsigned char *buffers = NULL; |
||
54 | unsigned char *vbuf = NULL; |
||
55 | OSMesaContext ctx; |
||
56 | |||
57 | static GLfloat view_rotx = 170.0, view_roty = -200.0, view_rotz = 0.0; |
||
58 | static GLfloat angle; |
||
59 | |||
60 | GLUquadricObj *quadratic; |
||
61 | |||
62 | static GLfloat SUP_W = 110.0; |
||
63 | static GLfloat SUP_P = 30.0; |
||
64 | |||
65 | extern void da_motor(float v); |
||
1661 | giacomo | 66 | extern void *video_memory; |
1655 | giacomo | 67 | |
68 | #ifndef M_PI |
||
69 | #define M_PI 3.14159265 |
||
70 | #endif |
||
71 | |||
1661 | giacomo | 72 | void program_key_end(KEY_EVT *k) |
1655 | giacomo | 73 | { |
1661 | giacomo | 74 | |
1655 | giacomo | 75 | da_motor(0.0); |
1661 | giacomo | 76 | |
1655 | giacomo | 77 | OSMesaDestroyContext(ctx); |
78 | free(buffers); |
||
79 | |||
80 | sys_end(); |
||
81 | |||
82 | } |
||
83 | |||
84 | static void draw_box(GLfloat p1x, GLfloat p1y ,GLfloat p1z, GLfloat p2x, GLfloat p2y, GLfloat p2z) { |
||
85 | |||
86 | glBegin(GL_QUADS); |
||
87 | |||
88 | // Front Face |
||
89 | glVertex3f(p1x, p1y, p1z); |
||
90 | glVertex3f(p2x, p1y, p1z); |
||
91 | glVertex3f(p2x, p2y, p1z); |
||
92 | glVertex3f(p1x, p2y, p1z); |
||
93 | |||
94 | // Back Face |
||
95 | glVertex3f(p1x, p1y, p2z); |
||
96 | glVertex3f(p1x, p2y, p2z); |
||
97 | glVertex3f(p2x, p2y, p2z); |
||
98 | glVertex3f(p2x, p1y, p2z); |
||
99 | |||
100 | // Top Face |
||
101 | glVertex3f(p1x, p2y, p2z); |
||
102 | glVertex3f(p1x, p2y, p1z); |
||
103 | glVertex3f(p2x, p2y, p1z); |
||
104 | glVertex3f(p2x, p2y, p2z); |
||
105 | |||
106 | // Bottom Face |
||
107 | glVertex3f(p1x, p1y, p2z); |
||
108 | glVertex3f(p2x, p1y, p2z); |
||
109 | glVertex3f(p2x, p1y, p1z); |
||
110 | glVertex3f(p1x, p1y, p1z); |
||
111 | |||
112 | // Right face |
||
113 | glVertex3f(p2x, p1y, p2z); |
||
114 | glVertex3f(p2x, p2y, p2z); |
||
115 | glVertex3f(p2x, p2y, p1z); |
||
116 | glVertex3f(p2x, p1y, p1z); |
||
117 | |||
118 | // Left Face |
||
119 | glVertex3f(p1x, p1y, p2z); |
||
120 | glVertex3f(p1x, p1y, p1z); |
||
121 | glVertex3f(p1x, p2y, p1z); |
||
122 | glVertex3f(p1x, p2y, p2z); |
||
123 | |||
124 | glEnd(); |
||
125 | |||
126 | } |
||
127 | |||
128 | static void draw_carrello(GLfloat x_scene, GLfloat angle_scene) { |
||
129 | |||
130 | static GLfloat gl_white[3] = {1.0f, 1.0f, 1.0f}; |
||
131 | static GLfloat gl_dark_gray[3] = {0.3f, 0.3f, 0.3f}; |
||
132 | static GLfloat gl_gray[3] = {0.7f, 0.7f, 0.7f}; |
||
133 | |||
134 | glPushMatrix(); |
||
135 | |||
136 | glColor3fv(gl_dark_gray); |
||
137 | |||
138 | glShadeModel(GL_FLAT); |
||
139 | |||
140 | glTranslatef(x_scene, 0.0f, 0.0f); |
||
141 | |||
142 | draw_box(-6.0f, -6.0f, 9.0f, 6.0f, -2.0f, -9.0f); |
||
143 | draw_box(-6.0f, -2.0f, 9.0f, 6.0f, -1.0f, 4.0f); |
||
144 | |||
145 | glColor3fv(gl_white); |
||
146 | |||
147 | draw_box(-8.0f, -2.0f, -3.0f, 8.0f, -1.0f, -10.0f); |
||
148 | |||
149 | glColor3fv(gl_dark_gray); |
||
150 | |||
151 | draw_box(-5.0f, 1.0f, 8.0f, 5.0f, 9.0f, 6.0f); |
||
152 | draw_box(-5.0f,1.0f,-6.0f,5.0f,9.0f,-8.0f); |
||
153 | draw_box(-5.0f,-1.0f,8.0f,5.0f,1.0f,-8.0f); |
||
154 | |||
155 | glShadeModel(GL_SMOOTH); |
||
156 | |||
157 | glColor3fv(gl_white); |
||
158 | |||
159 | glTranslatef(0.0f, 4.5f, -6.0f); |
||
160 | gluCylinder(quadratic, 2.2f, 2.2f, 12.0f, 16, 16); |
||
161 | |||
162 | glColor3fv(gl_gray); |
||
163 | |||
164 | glTranslatef(0.0f, 0.0f, 6.0f); |
||
165 | glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); |
||
166 | glRotatef(angle_scene, 0.0f, 1.0f, 0.0f); |
||
167 | gluCylinder(quadratic, 1.3f, 1.3f, 160.0f, 16, 16); |
||
168 | glRotatef(-angle_scene, 0.0f, 1.0f, 0.0f); |
||
169 | glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); |
||
170 | |||
171 | glTranslatef(-4.0f, -2.5f, -6.0f); |
||
172 | gluCylinder(quadratic, 0.5f, 0.5f, 12.0f, 16, 16); |
||
173 | |||
174 | glTranslatef(8.0f, 0.0f, 0.0f); |
||
175 | gluCylinder(quadratic, 0.5f, 0.5f, 12.0f, 16, 16); |
||
176 | |||
177 | glPopMatrix(); |
||
178 | |||
179 | } |
||
180 | |||
181 | static void draw_support() { |
||
182 | |||
183 | static GLfloat gl_dark_gray[3] = {0.3f, 0.3f, 0.3f}; |
||
184 | static GLfloat gl_gray[3] = {0.7f, 0.7f, 0.7f}; |
||
185 | |||
186 | glPushMatrix(); |
||
187 | |||
188 | glColor3fv(gl_gray); |
||
189 | |||
190 | glShadeModel(GL_FLAT); |
||
191 | |||
192 | draw_box(-SUP_W/2, -0.1f, SUP_P/2, SUP_W/2, 0.1f, -SUP_P/2); |
||
193 | |||
194 | glColor3fv(gl_dark_gray); |
||
195 | |||
196 | draw_box(-SUP_W/2-3.0f, -0.1f, SUP_P/2, -SUP_W/2, 14.0f, -SUP_P/2-0.2f); |
||
197 | draw_box(SUP_W/2, -0.1f, SUP_P/2, SUP_W/2+3.0f, 14.0f, -SUP_P/2-0.2f); |
||
198 | |||
199 | glShadeModel(GL_SMOOTH); |
||
200 | |||
201 | glColor3fv(gl_gray); |
||
202 | |||
203 | glTranslatef(-SUP_W/2, 6.0f, 0.0f); |
||
204 | glRotatef(90.0f, 0.0f, 1.0f, 0.0f); |
||
205 | gluCylinder(quadratic, 1.5f, 1.5f, SUP_W, 16, 16); |
||
206 | |||
207 | glPopMatrix(); |
||
208 | |||
209 | } |
||
210 | |||
211 | static void draw(GLfloat prm_x, GLfloat prm_th) |
||
212 | { |
||
213 | |||
214 | prm_x = -prm_x / LUNGH * (SUP_W-20.0f); |
||
215 | prm_th = -prm_th * FCA; |
||
216 | |||
217 | glPushMatrix(); |
||
218 | |||
219 | glRotatef(view_rotx, 1.0f, 0.0f, 0.0f); |
||
220 | glRotatef(view_roty, 0.0f, 1.0f, 0.0f); |
||
221 | glRotatef(view_rotz, 0.0f, 0.0f, 1.0f); |
||
222 | |||
223 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
||
224 | |||
225 | glRotatef(angle, 0.0f, 1.0f, 0.0f); |
||
226 | |||
227 | draw_support(); |
||
228 | glTranslatef(0.0f, 10.0f, -4.0f); |
||
229 | draw_carrello(prm_x,prm_th); |
||
230 | |||
231 | glPopMatrix(); |
||
232 | |||
233 | glFinish(); |
||
234 | |||
235 | } |
||
236 | |||
237 | static void init_gl() |
||
238 | { |
||
239 | |||
240 | static GLfloat pos0[4] = {40.0f, -70.0f, 180.0f, 1.0f}; |
||
241 | |||
242 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f); |
||
243 | |||
244 | glLightfv(GL_LIGHT0, GL_POSITION, pos0); |
||
245 | glEnable(GL_LIGHTING); |
||
246 | glEnable(GL_LIGHT0); |
||
247 | glEnable(GL_DEPTH_TEST); |
||
248 | glEnable(GL_CULL_FACE); |
||
249 | |||
250 | glViewport(0, 0, (GLint) WIDTH, (GLint) HEIGHT); |
||
251 | glMatrixMode(GL_PROJECTION); |
||
252 | glLoadIdentity(); |
||
253 | gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.1f,250.0f); |
||
254 | glMatrixMode(GL_MODELVIEW); |
||
255 | glLoadIdentity(); |
||
256 | glTranslatef(0.0f, 30.0f, -120.0f); |
||
257 | |||
258 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); |
||
259 | |||
260 | quadratic=gluNewQuadric(); |
||
261 | |||
262 | glEnable(GL_AUTO_NORMAL); |
||
263 | glEnable(GL_COLOR_MATERIAL); |
||
264 | glEnable(GL_NORMALIZE); |
||
265 | glDepthFunc(GL_LEQUAL); |
||
266 | |||
267 | } |
||
268 | |||
269 | TASK disegna(void) |
||
270 | { |
||
271 | |||
272 | PORT pr_x, pr_th; |
||
273 | float prm_x, prm_th; |
||
274 | |||
275 | char text[100]; |
||
276 | TIME disegna_TIME, carrello_TIME; |
||
277 | |||
278 | pr_x = port_connect("porta1",sizeof(float),STICK,READ); |
||
279 | pr_th = port_connect("porta2",sizeof(float),STICK,READ); |
||
280 | |||
281 | //vbuf = malloc(WIDTH * HEIGHT * 2); //Debug line |
||
282 | |||
283 | while(1) { |
||
284 | |||
285 | port_receive(pr_x, &prm_x, BLOCK); |
||
286 | port_receive(pr_th, &prm_th, BLOCK); |
||
287 | |||
288 | angle += 0.2; |
||
289 | |||
290 | draw(prm_x,prm_th); |
||
291 | |||
292 | jet_gettable(carrello_PID, &carrello_TIME, 1); |
||
293 | jet_gettable(disegna_PID, &disegna_TIME, 1); |
||
294 | |||
295 | sprintf(text,"Hard Task Control PER:%6d us EX:%6d us",(int)PERIOD_CARRELLO,(int)carrello_TIME); |
||
296 | grx_text(text,10,5,rgb16(0,0,255),rgb16(255,255,255)); |
||
297 | sprintf(text,"Hard Task Draw PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME); |
||
298 | grx_text(text,10,15,rgb16(0,0,255),rgb16(255,255,255)); |
||
299 | |||
300 | memcpy(buffers,vbuf,WIDTH*HEIGHT*BYTES_PP); |
||
301 | |||
302 | task_endcycle(); |
||
303 | |||
304 | } |
||
305 | |||
306 | port_disconnect(pr_x); |
||
307 | port_disconnect(pr_th); |
||
308 | |||
309 | sys_end(); |
||
310 | |||
311 | } |
||
312 | |||
313 | void waitenter() { |
||
314 | |||
315 | KEY_EVT k; |
||
316 | char esc = FALSE; |
||
317 | |||
318 | while(!esc) { |
||
319 | keyb_getcode(&k,BLOCK); |
||
320 | if (k.ascii == 13) esc = TRUE; |
||
321 | } |
||
322 | |||
323 | } |
||
324 | |||
325 | |||
326 | void init_motor(void) |
||
327 | { |
||
1661 | giacomo | 328 | char txt[100]; |
329 | |||
1655 | giacomo | 330 | da_motor(0.0); |
331 | |||
1661 | giacomo | 332 | sprintf(txt,"Calibrazione pendolo inverso...\n"); |
333 | grx_text(txt,10,10,rgb16(255,255,255),0); |
||
1655 | giacomo | 334 | |
1661 | giacomo | 335 | sprintf(txt,"Carr a sx e premi enter\n"); |
336 | grx_text(txt,10,20,rgb16(255,255,255),0); |
||
1655 | giacomo | 337 | waitenter(); |
338 | vmin=ad_conv(11); |
||
339 | |||
1661 | giacomo | 340 | sprintf(txt,"Carr a dx e premi enter\n"); |
341 | grx_text(txt,10,30,rgb16(255,255,255),0); |
||
1655 | giacomo | 342 | waitenter(); |
343 | vmax=ad_conv(11); |
||
344 | |||
1661 | giacomo | 345 | sprintf(txt,"Asta a sx e premi enter\n"); |
346 | grx_text(txt,10,40,rgb16(255,255,255),0); |
||
1655 | giacomo | 347 | waitenter(); |
348 | vminth=ad_conv(10); |
||
349 | |||
1661 | giacomo | 350 | sprintf(txt,"Asta a dx e premi enter\n"); |
351 | grx_text(txt,10,50,rgb16(255,255,255),0); |
||
1655 | giacomo | 352 | waitenter(); |
353 | vmaxth=ad_conv(10); |
||
354 | |||
1661 | giacomo | 355 | sprintf(txt,"Vxmax:%f Vxmin:%f Vthmax:%f Vthmin:%f\n",vmax,vmin,vmaxth,vminth); |
356 | grx_text(txt,10,60,rgb16(255,255,255),0); |
||
1655 | giacomo | 357 | waitenter(); |
358 | |||
359 | } |
||
360 | |||
361 | int main (int argc, char *argv[]) |
||
362 | { |
||
363 | HARD_TASK_MODEL ht_carrello, ht_disegna; |
||
1661 | giacomo | 364 | |
365 | vbuf = video_memory; |
||
366 | |||
1655 | giacomo | 367 | WCET_CARRELLO =((long int) PERIOD_CARRELLO * (0.05)); |
368 | WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.875)); |
||
369 | |||
370 | hard_task_default_model(ht_carrello); |
||
371 | hard_task_def_wcet(ht_carrello,WCET_CARRELLO); |
||
372 | hard_task_def_mit(ht_carrello,PERIOD_CARRELLO); |
||
373 | hard_task_def_usemath(ht_carrello); |
||
374 | hard_task_def_group(ht_carrello,1); |
||
375 | hard_task_def_ctrl_jet(ht_carrello); |
||
376 | |||
377 | carrello_PID = task_create("carrello", carrello, &ht_carrello, NULL); |
||
378 | if (carrello_PID == -1) { |
||
379 | sys_end(); |
||
380 | } |
||
381 | |||
382 | hard_task_default_model(ht_disegna); |
||
383 | hard_task_def_mit(ht_disegna,PERIOD_DISEGNA); |
||
384 | hard_task_def_wcet(ht_disegna,WCET_DISEGNA); |
||
385 | hard_task_def_group(ht_disegna,1); |
||
386 | hard_task_def_ctrl_jet(ht_disegna); |
||
387 | hard_task_def_usemath(ht_disegna); |
||
388 | hard_task_def_stack(ht_disegna,60000); |
||
389 | |||
390 | disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL); |
||
391 | if (disegna_PID == -1) { |
||
392 | sys_end(); |
||
393 | } |
||
394 | |||
395 | { |
||
396 | KEY_EVT k; |
||
397 | k.flag = ALTL_BIT; |
||
398 | k.scan = KEY_C; |
||
399 | k.ascii = 'c'; |
||
1661 | giacomo | 400 | k.status = KEY_PRESSED; |
401 | keyb_hook(k,program_key_end,FALSE); |
||
1655 | giacomo | 402 | } |
403 | |||
404 | init_motor(); |
||
405 | |||
406 | ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL ); |
||
407 | |||
408 | buffers = malloc(WIDTH*HEIGHT*BYTES_PP); |
||
409 | |||
410 | OSMesaMakeCurrent(ctx, buffers, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT); |
||
411 | |||
412 | init_gl(); |
||
413 | |||
414 | group_activate(1); |
||
415 | |||
416 | return 0; |
||
417 | |||
418 | } |