Subversion Repositories shark

Rev

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