Subversion Repositories shark

Rev

Rev 1407 | Rev 1410 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1331 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *
6
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
7
 *
8
 * http://www.sssup.it
9
 * http://retis.sssup.it
10
 * http://shark.sssup.it
11
 */
12
 
13
#include "chimera.h"
1396 giacomo 14
#include "tracer.h"
1408 giacomo 15
#include "unistd.h"
1331 giacomo 16
 
17
//#define DEBUG_SEND            /* Print Sent Values */
18
#define SERIAL_ON               /* Send Data using Serial Port */
19
 
20
/* Servo Tasks Constants */
21
#ifdef DUBUG_SEND
1359 giacomo 22
#define SEND_TASK_WCET 20000
1331 giacomo 23
#else
1359 giacomo 24
#define SEND_TASK_WCET 20000
1331 giacomo 25
#endif
1391 giacomo 26
#define SEND_TASK_MIT  30000
1331 giacomo 27
 
28
#define LEG_A 100.0
29
#define LEG_B  66.0
30
#define LEG_C  26.0
31
#define LEG_D  38.0
32
#define LEG_CD_2IPO 92.087      /* 2 * sqrt(LEG_C^2 + LEG_D^2) */
1338 giacomo 33
#define LEG_CD_ANG  0.600       /* arctg(LEG_C/LEG_D) in radianti */
1331 giacomo 34
 
35
const float c0 = LEG_C * LEG_C;
36
const float c1 = LEG_B * LEG_B;
37
const float c2 = LEG_B * LEG_B - LEG_A * LEG_A;
38
const float todeg = 180.0 / PI;
1336 giacomo 39
const float torad = PI / 180.0;
1331 giacomo 40
 
41
HEXAPOD_STATE   status;
42
 
1408 giacomo 43
void *start_tracer = NULL;
44
void *end_tracer = NULL;
45
void *trace_pointer = NULL;
1407 giacomo 46
 
1332 giacomo 47
extern struct leg_calibration calibration_table[];
48
 
1331 giacomo 49
void print_status(int n){
50
}
51
 
1336 giacomo 52
int leg_to_ang(float px, float py, float pz, int *alfa, int *beta, int *gamma)
1331 giacomo 53
{
54
        float px2 = px * px;
55
        float py2 = py * py;
56
        float pz2 = pz * pz;
57
 
58
        float pxz2 = px2 + pz2;
59
 
1338 giacomo 60
        float alfa1,beta1,alfa2,beta2,gamma1,gamma2;
1331 giacomo 61
        float m,dsqrt;
62
 
63
        float delta_xz = pxz2 - c0;
64
        float s,k,k2,y1,delta_xy;
65
 
66
        if (delta_xz < 0.0) return -1;
67
 
68
        if (pz >= LEG_C) {
1338 giacomo 69
                gamma2 =  acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2);
70
                gamma1 = gamma2 * todeg;
1331 giacomo 71
        } else {
1338 giacomo 72
                gamma2 = -acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2);
73
                gamma1 = gamma2 * todeg;
1331 giacomo 74
        }
75
 
1338 giacomo 76
        m = pxz2 - LEG_CD_2IPO * (pz * sin(gamma2+LEG_CD_ANG) + px * cos(gamma2+LEG_CD_ANG) - LEG_CD_2IPO / 4.0);
1331 giacomo 77
 
78
        s = m + py2;
79
        k = c2 + s;
80
        k2 = k * k;
81
 
82
        delta_xy = py2 * k2 - s * (k2 - 4.0 * m * c1);
83
 
84
        if (delta_xy >= 0.0) {
85
                dsqrt = sqrt(delta_xy);
86
                y1 = (py * k + dsqrt) / (2.0 * s);
87
                beta1 = asin(y1/LEG_B) * todeg;
88
                alfa1 = asin((y1 - py)/LEG_A) * todeg + beta1;
89
                y1 = (py * k - dsqrt) / (2.0 * s);
90
                beta2 = asin(y1/LEG_B) * todeg;
91
                alfa2 = asin((y1 - py)/LEG_A) * todeg + beta2;
92
 
93
                if ((alfa1 >= 0.0 && alfa1 <= 180.0) && (beta1 >= -90.0 && beta1 <= 90.0)) {
1336 giacomo 94
                        *alfa = (int)(alfa1 * 3600.0);
95
                        *beta = (int)(beta1 * 3600.0);
96
                        *gamma = (int)(gamma1 * 3600.0);
1331 giacomo 97
                        return 0;
98
                } else if ((alfa2 >= 0.0 && alfa2 <= 180.0) && (beta2 >= -90.0 && beta2 <= 90.0)) {
1336 giacomo 99
                        *alfa = (int)(alfa2 * 3600.0);
100
                        *beta = (int)(beta2 * 3600.0);
101
                        *gamma = (int)(gamma1 * 3600.0);
1331 giacomo 102
                        return 0;
103
                } else {
1336 giacomo 104
                        return -1;
1331 giacomo 105
                }
106
        } else
1336 giacomo 107
                return -1;
1331 giacomo 108
 
1336 giacomo 109
        return -1;
110
 
1331 giacomo 111
}
112
 
1336 giacomo 113
int ang_to_leg(int alfa, int beta, int gamma, float *px, float *py, float *pz) {
114
 
115
  float alfa1 = (float)(alfa)/3600.0 * torad;
116
  float beta1 = (float)(beta)/3600.0 * torad;
117
  float sin_gamma = sin((float)(gamma)/3600.0 * torad);
118
  float cos_gamma = cos((float)(gamma)/3600.0 * torad);
119
  float m;
120
 
121
  m =  LEG_B * cos(beta1) + LEG_A * cos(alfa1 - beta1);
122
  *py = LEG_B * sin(beta1) - LEG_A * sin(alfa1 - beta1);
123
 
124
  *pz = (LEG_D + m) * sin_gamma + LEG_C * cos_gamma;
125
  *px = (LEG_D + m) * cos_gamma - LEG_C * sin_gamma;
126
 
127
  return 0;
128
 
129
}
130
 
1333 giacomo 131
void update_event_action(void) {
132
 
133
  struct timespec t;
134
  struct action_event *e;
135
  int i;
136
 
137
  kern_gettime(&t);
138
 
139
  while ((e = get_first_old_event(&t)) != NULL) {
140
 
141
        if (e->type == EVT_SET_MASK_LEG_ANGLE) {
142
 
143
                for (i=0;i<6;i++)
1359 giacomo 144
                        if ((e->mask >> i) & 1) {
1333 giacomo 145
 
1336 giacomo 146
                        status.ang[i].a = e->ang.a;
147
                        status.ang[i].b = e->ang.b;
148
                        status.ang[i].c = e->ang.c;
149
 
150
                        status.cfg[i].pwm = e->pwm;
151
 
1396 giacomo 152
#ifdef  DEBUG_SEND
1334 giacomo 153
                        printf_xy(3,2,WHITE,"%8d: Update leg %2d angle",(int)kern_gettime(NULL),i);
1396 giacomo 154
#endif
1333 giacomo 155
 
156
 
157
                        }
158
 
159
                e->status = EVT_STATUS_DONE;
160
 
161
        }
162
 
163
  }
164
 
165
}
166
 
1407 giacomo 167
int trace_init(int buffer_size)
168
{
169
 
170
        start_tracer = trace_pointer = (void *)malloc(buffer_size);
171
        end_tracer = start_tracer + buffer_size;
172
        memset(trace_pointer,0,buffer_size);
173
 
174
        if (trace_pointer == NULL) return -1;
175
 
176
        return 0;
177
}
178
 
1408 giacomo 179
/* Init the network stack */
180
int init_network(char *local_ip)
181
{
182
 
183
        struct net_model m = net_base;
184
 
185
        net_setudpip(m, local_ip, "255.255.255.255");
186
 
187
        if (net_init(&m) != 1) {
188
                cprintf("Network: Init Error.\n");
189
                return -1;
190
        }
191
 
192
        return 0;
193
 
194
}
195
 
1407 giacomo 196
int trace_consumption(int sensor, int value)
197
{
198
 
199
        struct timespec t;
200
        SYS_FLAGS f;
201
 
202
        if (trace_pointer == NULL) return -1;
203
 
204
        if (trace_pointer  >= (end_tracer-16)) return -1;
205
 
206
        f = kern_fsave();
207
 
208
        sys_gettime(&t);
209
 
210
        *(unsigned int *)trace_pointer = (sensor & 0xFF) | 0xAABBFF00;
211
        *(unsigned int *)(trace_pointer + 4) = value;
212
        *(unsigned int *)(trace_pointer + 8) = t.tv_sec;
213
        *(unsigned int *)(trace_pointer + 12) = t.tv_nsec;     
214
 
215
        trace_pointer += 16;
216
 
217
        kern_frestore(f);
218
 
219
        return 0;
220
 
221
}
222
 
1408 giacomo 223
int trace_send() {
1407 giacomo 224
 
225
        static char pkg_buffer[1100];
226
        int actual = 0;
227
 
1408 giacomo 228
        UDP_ADDR target, local;
229
        char local_ip[20], target_ip[20];
230
        int socket;
231
        IP_ADDR bindlist[5];
232
 
233
        strcpy(local_ip,  "192.168.1.10");
234
        strcpy(target_ip, "192.168.1.1");
235
 
236
        if (init_network(local_ip)) sys_end();
237
 
238
        /* local IP string to addr */
239
        ip_str2addr(local_ip,&(local.s_addr));
240
        /* set the source port */
241
        local.s_port = 20000;
242
 
243
        /* target IP string to addr */
244
        ip_str2addr(target_ip,&(bindlist[0]));
245
        memset(&(bindlist[1]), 0, sizeof(IP_ADDR));
246
        /* bind */
247
        socket = udp_bind(&local, NULL);
248
 
249
        /* target IP string to addr */
250
        ip_str2addr(target_ip,&(target.s_addr));
251
        /* target port */
252
        target.s_port = 20000;
253
 
1407 giacomo 254
        trace_pointer = start_tracer;
1408 giacomo 255
        actual = 0;
1407 giacomo 256
 
1408 giacomo 257
        while(trace_pointer < end_tracer) {
1407 giacomo 258
 
1408 giacomo 259
                if (((*(int *)(trace_pointer) >> 8) & 0xAABBFF) == 0xAABBFF) {
1407 giacomo 260
 
1408 giacomo 261
                        memcpy(&(pkg_buffer[actual]),trace_pointer,16);
1407 giacomo 262
 
1408 giacomo 263
                        actual += 16;
264
 
265
                        if (actual > 1000) {
266
                                pkg_buffer[actual] = 0;
267
                                udp_sendto(socket, pkg_buffer, strlen(pkg_buffer), &target);
268
                                usleep(10000);
269
                                actual = 0;
270
                        }
271
 
1407 giacomo 272
                }
273
 
1408 giacomo 274
                trace_pointer += 16;
275
 
1407 giacomo 276
        }
277
 
1408 giacomo 278
        return 0;
279
 
1407 giacomo 280
}
281
 
1331 giacomo 282
TASK servo_send()
283
{
284
        HEXAPOD_STATE   old_status;
285
        register char   changes, new_pos, new_pwm, new_power;
1359 giacomo 286
        int             res,n;
1390 mauro 287
        struct timespec t;
1391 giacomo 288
        int actual_leg = 0;
1331 giacomo 289
 
290
        for (n=0; n<6;n++) {
1333 giacomo 291
                old_status.ang[n].a = 0;
292
                old_status.ang[n].b = 0;
293
                old_status.ang[n].c = 0;
294
                old_status.cfg[n].pwm = 0;
1331 giacomo 295
        }
296
        old_status.power = 0;
297
 
298
        while (1) {
299
                changes = 0;
1333 giacomo 300
 
301
                update_event_action();
1331 giacomo 302
 
303
                for (n=0; n<6; n++){
304
                        new_pos = 0;
305
                        new_pwm = 0;
306
                        new_power = 0;
307
 
1333 giacomo 308
                        if ((status.ang[n].a != old_status.ang[n].a) ||
309
                                (status.ang[n].b != old_status.ang[n].b) ||
310
                                (status.ang[n].c != old_status.ang[n].c)) {
311
 
312
                                        old_status.ang[n].a = status.ang[n].a;
313
                                        old_status.ang[n].b = status.ang[n].b;
314
                                        old_status.ang[n].c = status.ang[n].c;
315
                                        new_pos++;
316
                        }
317
 
318
                        if (status.cfg[n].pwm != old_status.cfg[n].pwm) {
319
                                old_status.cfg[n].pwm = status.cfg[n].pwm;
1331 giacomo 320
                                new_pwm++;
321
                        }
322
                        if (status.power != old_status.power) {
323
                                old_status.power = status.power;
324
                                new_power++;
325
                        }
326
 
327
                        if (new_pos) {
328
#ifdef  SERIAL_ON
1391 giacomo 329
                                task_nopreempt();
1359 giacomo 330
                                res = servo_set_angle_sec(com(n*3  ), pin(n*3  ), adjust(status.ang[n].a,n,0));
331
                                if (res != 0) cprintf("Error send data\n");
332
                                res = servo_set_angle_sec(com(n*3+1), pin(n*3+1), adjust(status.ang[n].b,n,1));
333
                                if (res != 0) cprintf("Error send data\n");
334
                                res = servo_set_angle_sec(com(n*3+2), pin(n*3+2), adjust(status.ang[n].c,n,2));
335
                                if (res != 0) cprintf("Error send data\n");
1391 giacomo 336
                                task_preempt();
1331 giacomo 337
#endif
1359 giacomo 338
 
1331 giacomo 339
                        }
340
 
341
                        if (new_pwm) {
342
#ifdef  SERIAL_ON
1391 giacomo 343
                                task_nopreempt();
1359 giacomo 344
                                (old_status.cfg[n].pwm & 1) ? servo_turn_on(com(n*3  ), pin(n*3  )) : servo_turn_off(com(n*3  ), pin(n*3  ));
345
                                (old_status.cfg[n].pwm & 2) ? servo_turn_on(com(n*3+1), pin(n*3+1)) : servo_turn_off(com(n*3+1), pin(n*3+1));
346
                                (old_status.cfg[n].pwm & 4) ? servo_turn_on(com(n*3+2), pin(n*3+2)) : servo_turn_off(com(n*3+2), pin(n*3+2));
1391 giacomo 347
                                task_preempt();
1331 giacomo 348
#endif
349
                        }
350
                        if (new_power) {
351
#ifdef  SERIAL_ON
1391 giacomo 352
                                task_nopreempt();
1331 giacomo 353
                                if (old_status.power) {
1359 giacomo 354
                                        servo_set_RC5_switch(COM2, 1);
1331 giacomo 355
                                } else {
1359 giacomo 356
                                        servo_set_RC5_switch(COM2, 0);
1331 giacomo 357
                                }
1391 giacomo 358
                                task_preempt();
1331 giacomo 359
#endif
360
                        }
361
 
362
                        if (new_pos || new_pwm || new_power) {
363
                                changes++;
364
                        }
365
                }
366
 
1391 giacomo 367
                if (!new_pos) {
1331 giacomo 368
 
1397 giacomo 369
 
370
#ifdef  SERIAL_ON
1391 giacomo 371
                        task_nopreempt();
372
                        status.cfg[actual_leg].adc_in = servo_get_analog(COM1, actual_leg);
1407 giacomo 373
                        trace_consumption(actual_leg,status.cfg[actual_leg].adc_in);   
1391 giacomo 374
                        task_preempt();
1397 giacomo 375
#endif
1391 giacomo 376
 
377
                        sys_gettime(&t);
378
                        printf_xy(1,20,WHITE,"(%d) (%d) (%d) (%d) (%d) (%d)                ",
379
                                status.cfg[0].adc_in,
380
                                status.cfg[1].adc_in,
381
                                status.cfg[2].adc_in,
382
                                status.cfg[3].adc_in,
383
                                status.cfg[4].adc_in,
384
                                status.cfg[5].adc_in);
385
                        actual_leg = (actual_leg+1)%6;
386
 
387
                }
388
 
1331 giacomo 389
                task_endcycle();
390
        }
391
 
392
        return 0;
393
}
394
 
395
int init_serial()
396
{
397
        int err;
1359 giacomo 398
        err = servo_open(COM1, COM_SPEED);
399
        if (!err)
400
                err = servo_open(COM2, COM_SPEED);
1331 giacomo 401
 
402
        return err;
403
}
404
 
405
void end_serial()
406
{
1359 giacomo 407
        servo_close(COM1);
408
        servo_close(COM2);
1331 giacomo 409
}
410
 
411
void init_send_task()
412
{
1359 giacomo 413
        HARD_TASK_MODEL ms;
1331 giacomo 414
        PID pid;
415
 
1359 giacomo 416
        hard_task_default_model(ms);
417
        hard_task_def_ctrl_jet(ms);
418
        hard_task_def_wcet(ms, SEND_TASK_WCET);
419
        hard_task_def_mit(ms, SEND_TASK_MIT);
420
        hard_task_def_usemath(ms);
1391 giacomo 421
        pid = task_create("Servo_Task", servo_send, &ms, NULL);
1331 giacomo 422
        if (pid == NIL) {
423
                perror("Could not create task <Send_Task>");
424
                sys_end();
425
        } else
426
                task_activate(pid);
427
 
428
}
429
 
430
void init_send()
431
{
432
        int i;
433
 
434
        if (init_serial()) {
435
                perror("Could not initialize serial port.");
436
                sys_end();
437
        }
438
 
439
        for (i=0; i<6;i++) {
1333 giacomo 440
                status.ang[i].a =   0;
441
                status.ang[i].b =   0;
442
                status.ang[i].c =   0;
443
                status.cfg[i].pwm = 0;
1331 giacomo 444
        }
445
        status.power = 0;
446
 
447
        init_send_task();
1336 giacomo 448
 
1331 giacomo 449
}
450
 
451
void end_send()
452
{
453
        end_serial();
454
}