Subversion Repositories shark

Rev

Rev 1391 | 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
 
1332 giacomo 42
extern struct leg_calibration calibration_table[];
43
 
1331 giacomo 44
void print_status(int n){
45
}
46
 
1336 giacomo 47
int leg_to_ang(float px, float py, float pz, int *alfa, int *beta, int *gamma)
1331 giacomo 48
{
49
        float px2 = px * px;
50
        float py2 = py * py;
51
        float pz2 = pz * pz;
52
 
53
        float pxz2 = px2 + pz2;
54
 
1338 giacomo 55
        float alfa1,beta1,alfa2,beta2,gamma1,gamma2;
1331 giacomo 56
        float m,dsqrt;
57
 
58
        float delta_xz = pxz2 - c0;
59
        float s,k,k2,y1,delta_xy;
60
 
61
        if (delta_xz < 0.0) return -1;
62
 
63
        if (pz >= LEG_C) {
1338 giacomo 64
                gamma2 =  acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2);
65
                gamma1 = gamma2 * todeg;
1331 giacomo 66
        } else {
1338 giacomo 67
                gamma2 = -acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2);
68
                gamma1 = gamma2 * todeg;
1331 giacomo 69
        }
70
 
1338 giacomo 71
        m = pxz2 - LEG_CD_2IPO * (pz * sin(gamma2+LEG_CD_ANG) + px * cos(gamma2+LEG_CD_ANG) - LEG_CD_2IPO / 4.0);
1331 giacomo 72
 
73
        s = m + py2;
74
        k = c2 + s;
75
        k2 = k * k;
76
 
77
        delta_xy = py2 * k2 - s * (k2 - 4.0 * m * c1);
78
 
79
        if (delta_xy >= 0.0) {
80
                dsqrt = sqrt(delta_xy);
81
                y1 = (py * k + dsqrt) / (2.0 * s);
82
                beta1 = asin(y1/LEG_B) * todeg;
83
                alfa1 = asin((y1 - py)/LEG_A) * todeg + beta1;
84
                y1 = (py * k - dsqrt) / (2.0 * s);
85
                beta2 = asin(y1/LEG_B) * todeg;
86
                alfa2 = asin((y1 - py)/LEG_A) * todeg + beta2;
87
 
88
                if ((alfa1 >= 0.0 && alfa1 <= 180.0) && (beta1 >= -90.0 && beta1 <= 90.0)) {
1336 giacomo 89
                        *alfa = (int)(alfa1 * 3600.0);
90
                        *beta = (int)(beta1 * 3600.0);
91
                        *gamma = (int)(gamma1 * 3600.0);
1331 giacomo 92
                        return 0;
93
                } else if ((alfa2 >= 0.0 && alfa2 <= 180.0) && (beta2 >= -90.0 && beta2 <= 90.0)) {
1336 giacomo 94
                        *alfa = (int)(alfa2 * 3600.0);
95
                        *beta = (int)(beta2 * 3600.0);
96
                        *gamma = (int)(gamma1 * 3600.0);
1331 giacomo 97
                        return 0;
98
                } else {
1336 giacomo 99
                        return -1;
1331 giacomo 100
                }
101
        } else
1336 giacomo 102
                return -1;
1331 giacomo 103
 
1336 giacomo 104
        return -1;
105
 
1331 giacomo 106
}
107
 
1336 giacomo 108
int ang_to_leg(int alfa, int beta, int gamma, float *px, float *py, float *pz) {
109
 
110
  float alfa1 = (float)(alfa)/3600.0 * torad;
111
  float beta1 = (float)(beta)/3600.0 * torad;
112
  float sin_gamma = sin((float)(gamma)/3600.0 * torad);
113
  float cos_gamma = cos((float)(gamma)/3600.0 * torad);
114
  float m;
115
 
116
  m =  LEG_B * cos(beta1) + LEG_A * cos(alfa1 - beta1);
117
  *py = LEG_B * sin(beta1) - LEG_A * sin(alfa1 - beta1);
118
 
119
  *pz = (LEG_D + m) * sin_gamma + LEG_C * cos_gamma;
120
  *px = (LEG_D + m) * cos_gamma - LEG_C * sin_gamma;
121
 
122
  return 0;
123
 
124
}
125
 
1333 giacomo 126
void update_event_action(void) {
127
 
128
  struct timespec t;
129
  struct action_event *e;
130
  int i;
131
 
132
  kern_gettime(&t);
133
 
134
  while ((e = get_first_old_event(&t)) != NULL) {
135
 
136
        if (e->type == EVT_SET_MASK_LEG_ANGLE) {
137
 
138
                for (i=0;i<6;i++)
1359 giacomo 139
                        if ((e->mask >> i) & 1) {
1333 giacomo 140
 
1336 giacomo 141
                        status.ang[i].a = e->ang.a;
142
                        status.ang[i].b = e->ang.b;
143
                        status.ang[i].c = e->ang.c;
144
 
145
                        status.cfg[i].pwm = e->pwm;
146
 
1396 giacomo 147
#ifdef  DEBUG_SEND
1334 giacomo 148
                        printf_xy(3,2,WHITE,"%8d: Update leg %2d angle",(int)kern_gettime(NULL),i);
1396 giacomo 149
#endif
1333 giacomo 150
 
151
 
152
                        }
153
 
154
                e->status = EVT_STATUS_DONE;
155
 
156
        }
157
 
158
  }
159
 
160
}
161
 
1331 giacomo 162
TASK servo_send()
163
{
164
        HEXAPOD_STATE   old_status;
165
        register char   changes, new_pos, new_pwm, new_power;
1359 giacomo 166
        int             res,n;
1390 mauro 167
        struct timespec t;
1391 giacomo 168
        int actual_leg = 0;
1331 giacomo 169
 
170
        for (n=0; n<6;n++) {
1333 giacomo 171
                old_status.ang[n].a = 0;
172
                old_status.ang[n].b = 0;
173
                old_status.ang[n].c = 0;
174
                old_status.cfg[n].pwm = 0;
1331 giacomo 175
        }
176
        old_status.power = 0;
177
 
178
        while (1) {
179
                changes = 0;
1333 giacomo 180
 
181
                update_event_action();
1331 giacomo 182
 
183
                for (n=0; n<6; n++){
184
                        new_pos = 0;
185
                        new_pwm = 0;
186
                        new_power = 0;
187
 
1333 giacomo 188
                        if ((status.ang[n].a != old_status.ang[n].a) ||
189
                                (status.ang[n].b != old_status.ang[n].b) ||
190
                                (status.ang[n].c != old_status.ang[n].c)) {
191
 
192
                                        old_status.ang[n].a = status.ang[n].a;
193
                                        old_status.ang[n].b = status.ang[n].b;
194
                                        old_status.ang[n].c = status.ang[n].c;
195
                                        new_pos++;
196
                        }
197
 
198
                        if (status.cfg[n].pwm != old_status.cfg[n].pwm) {
199
                                old_status.cfg[n].pwm = status.cfg[n].pwm;
1331 giacomo 200
                                new_pwm++;
201
                        }
202
                        if (status.power != old_status.power) {
203
                                old_status.power = status.power;
204
                                new_power++;
205
                        }
206
 
207
                        if (new_pos) {
208
#ifdef  SERIAL_ON
1391 giacomo 209
                                task_nopreempt();
1359 giacomo 210
                                res = servo_set_angle_sec(com(n*3  ), pin(n*3  ), adjust(status.ang[n].a,n,0));
211
                                if (res != 0) cprintf("Error send data\n");
212
                                res = servo_set_angle_sec(com(n*3+1), pin(n*3+1), adjust(status.ang[n].b,n,1));
213
                                if (res != 0) cprintf("Error send data\n");
214
                                res = servo_set_angle_sec(com(n*3+2), pin(n*3+2), adjust(status.ang[n].c,n,2));
215
                                if (res != 0) cprintf("Error send data\n");
1391 giacomo 216
                                task_preempt();
1331 giacomo 217
#endif
1359 giacomo 218
 
1331 giacomo 219
                        }
220
 
221
                        if (new_pwm) {
222
#ifdef  SERIAL_ON
1391 giacomo 223
                                task_nopreempt();
1359 giacomo 224
                                (old_status.cfg[n].pwm & 1) ? servo_turn_on(com(n*3  ), pin(n*3  )) : servo_turn_off(com(n*3  ), pin(n*3  ));
225
                                (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));
226
                                (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 227
                                task_preempt();
1331 giacomo 228
#endif
229
                        }
230
                        if (new_power) {
231
#ifdef  SERIAL_ON
1391 giacomo 232
                                task_nopreempt();
1331 giacomo 233
                                if (old_status.power) {
1359 giacomo 234
                                        servo_set_RC5_switch(COM2, 1);
1331 giacomo 235
                                } else {
1359 giacomo 236
                                        servo_set_RC5_switch(COM2, 0);
1331 giacomo 237
                                }
1391 giacomo 238
                                task_preempt();
1331 giacomo 239
#endif
240
                        }
241
 
242
                        if (new_pos || new_pwm || new_power) {
243
                                changes++;
244
#ifdef  DEBUG_SEND
245
                                print_status(n);
246
#endif
247
                        }
248
                }
249
 
1391 giacomo 250
                if (!new_pos) {
1331 giacomo 251
 
1391 giacomo 252
                        task_nopreempt();
253
                        status.cfg[actual_leg].adc_in = servo_get_analog(COM1, actual_leg);
1396 giacomo 254
                        TRACER_LOGEVENT(FTrace_EVT_ipoint,actual_leg,status.cfg[actual_leg].adc_in);
1391 giacomo 255
                        task_preempt();
256
 
257
                        sys_gettime(&t);
258
                        printf_xy(1,20,WHITE,"(%d) (%d) (%d) (%d) (%d) (%d)                ",
259
                                status.cfg[0].adc_in,
260
                                status.cfg[1].adc_in,
261
                                status.cfg[2].adc_in,
262
                                status.cfg[3].adc_in,
263
                                status.cfg[4].adc_in,
264
                                status.cfg[5].adc_in);
265
                        actual_leg = (actual_leg+1)%6;
266
 
267
                }
268
 
1331 giacomo 269
                task_endcycle();
270
        }
271
 
272
        return 0;
273
}
274
 
275
int init_serial()
276
{
277
        int err;
1359 giacomo 278
        err = servo_open(COM1, COM_SPEED);
279
        if (!err)
280
                err = servo_open(COM2, COM_SPEED);
1331 giacomo 281
 
282
        return err;
283
}
284
 
285
void end_serial()
286
{
1359 giacomo 287
        servo_close(COM1);
288
        servo_close(COM2);
1331 giacomo 289
}
290
 
291
void init_send_task()
292
{
1359 giacomo 293
        HARD_TASK_MODEL ms;
1331 giacomo 294
        PID pid;
295
 
1359 giacomo 296
        hard_task_default_model(ms);
297
        hard_task_def_ctrl_jet(ms);
298
        hard_task_def_wcet(ms, SEND_TASK_WCET);
299
        hard_task_def_mit(ms, SEND_TASK_MIT);
300
        hard_task_def_usemath(ms);
1391 giacomo 301
        pid = task_create("Servo_Task", servo_send, &ms, NULL);
1331 giacomo 302
        if (pid == NIL) {
303
                perror("Could not create task <Send_Task>");
304
                sys_end();
305
        } else
306
                task_activate(pid);
307
 
308
}
309
 
310
void init_send()
311
{
312
        int i;
313
 
314
        if (init_serial()) {
315
                perror("Could not initialize serial port.");
316
                sys_end();
317
        }
318
 
319
        for (i=0; i<6;i++) {
1333 giacomo 320
                status.ang[i].a =   0;
321
                status.ang[i].b =   0;
322
                status.ang[i].c =   0;
323
                status.cfg[i].pwm = 0;
1331 giacomo 324
        }
325
        status.power = 0;
326
 
327
        init_send_task();
1336 giacomo 328
 
1331 giacomo 329
}
330
 
331
void end_send()
332
{
333
        end_serial();
334
}