Subversion Repositories shark

Rev

Rev 1332 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1331 giacomo 1
 
2
/*
3
 * Project: S.Ha.R.K.
4
 *
5
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
6
 *
7
 * Authors     : Paolo Gai <pj@hartik.sssup.it>
8
 * (see authors.txt for full list of hartik's authors)
9
 *
10
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
11
 *
12
 * http://www.sssup.it
13
 * http://retis.sssup.it
14
 * http://shark.sssup.it
15
 */
16
 
17
/*
18
 * Copyright (C) 2000 Paolo Gai
19
 *
20
 * This program is free software; you can redistribute it and/or modify
21
 * it under the terms of the GNU General Public License as published by
22
 * the Free Software Foundation; either version 2 of the License, or
23
 * (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
 *
34
 */
35
 
36
#include "chimera.h"
37
 
38
//#define DEBUG_SEND            /* Print Sent Values */
39
#define SERIAL_ON               /* Send Data using Serial Port */
40
 
41
/* Servo Tasks Constants */
42
#ifdef DUBUG_SEND
43
#define SEND_TASK_WCET  6000
44
#else
45
#define SEND_TASK_WCET 10000
46
#endif
47
#define SEND_TASK_MIT  20000
48
 
49
#ifdef DUBUG_SEND
50
#define GET_TASK_WCET   4000
51
#else
52
#define GET_TASK_WCET   5000
53
#endif
54
#define GET_TASK_MIT   20000
55
 
56
#define LEG_A 100.0
57
#define LEG_B  66.0
58
#define LEG_C  26.0
59
#define LEG_D  38.0
60
#define LEG_CD_2IPO 92.087      /* 2 * sqrt(LEG_C^2 + LEG_D^2) */
61
#define LEG_CD_ANG  34.380      /* arctg(LEG_C/LEG_D) in gradi */
62
 
63
const float c0 = LEG_C * LEG_C;
64
const float c1 = LEG_B * LEG_B;
65
const float c2 = LEG_B * LEG_B - LEG_A * LEG_A;
66
const float todeg = 180.0 / PI;
67
 
68
sem_t           mx_status, mx_servo;
69
HEXAPOD_STATE   status;
70
ANGLES_STATE    angles[6];
71
 
72
void print_status(int n){
73
        int i;
74
 
75
        if (n>=6) {
76
                for (i=0; i<6;i++)
77
                        cprintf("Leg %1d: ( %3d | %3d | %3d ) (%d)\n", i, status.leg[i].x, status.leg[i].y, status.leg[i].z, status.leg[i].pwm );
78
                cprintf("Power: %d", status.power);
79
                cprintf("\n");
80
        } else {
81
                cprintf("Leg %1d: ( %3d | %3d | %3d ) (%1d-%1d)\n", n, status.leg[n].x,status.leg[n].y,status.leg[n].z, status.leg[n].pwm, status.power);
82
        }
83
}
84
 
85
int set_leg_position(int n, float px, float py, float pz)
86
{
87
        float px2 = px * px;
88
        float py2 = py * py;
89
        float pz2 = pz * pz;
90
 
91
        float pxz2 = px2 + pz2;
92
 
93
        float alfa1,beta1,alfa2,beta2,gamma1;
94
        float m,dsqrt;
95
 
96
        float delta_xz = pxz2 - c0;
97
        float s,k,k2,y1,delta_xy;
98
 
99
        if (delta_xz < 0.0) return -1;
100
 
101
        if (pz >= LEG_C) {
102
                gamma1 =  acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2) * todeg;
103
        } else {
104
                gamma1 = -acos((pz * LEG_C + px * sqrt(delta_xz)) / pxz2) * todeg;
105
        }
106
 
107
        if (gamma1 < -90.0 || gamma1 > 90.0) return -1;
108
 
109
        m = pxz2 - LEG_CD_2IPO * (sin(gamma1+LEG_CD_ANG)+cos(gamma1+LEG_CD_ANG));
110
 
111
        s = m + py2;
112
        k = c2 + s;
113
        k2 = k * k;
114
 
115
        delta_xy = py2 * k2 - s * (k2 - 4.0 * m * c1);
116
 
117
        if (delta_xy >= 0.0) {
118
                dsqrt = sqrt(delta_xy);
119
                y1 = (py * k + dsqrt) / (2.0 * s);
120
                beta1 = asin(y1/LEG_B) * todeg;
121
                alfa1 = asin((y1 - py)/LEG_A) * todeg + beta1;
122
                y1 = (py * k - dsqrt) / (2.0 * s);
123
                beta2 = asin(y1/LEG_B) * todeg;
124
                alfa2 = asin((y1 - py)/LEG_A) * todeg + beta2;
125
 
126
                if ((alfa1 >= 0.0 && alfa1 <= 180.0) && (beta1 >= -90.0 && beta1 <= 90.0)) {
127
                        angles[n].a = (int)(alfa1 * 3600.0);
128
                        angles[n].b = (int)(beta1 * 3600.0);
129
                        angles[n].c = (int)(gamma1 * 3600.0);
130
#ifdef  DEBUG_SEND
131
                        cprintf("Leg %d: ( %3d | %3d | %3d )\n", n, angles[n].a, angles[n].b, angles[n].c);
132
#endif
133
                        return 0;
134
                } else if ((alfa2 >= 0.0 && alfa2 <= 180.0) && (beta2 >= -90.0 && beta2 <= 90.0)) {
135
                        angles[n].a = (int)(alfa2 * 3600.0);
136
                        angles[n].b = (int)(beta2 * 3600.0);
137
                        angles[n].c = (int)(gamma1 * 3600.0);
138
#ifdef  DEBUG_SEND
139
                        cprintf("Leg %d: ( %3d | %3d | %3d )\n", n, angles[n].a, angles[n].b, angles[n].c);
140
#endif
141
                        return 0;
142
                } else {
143
#ifdef  DEBUG_SEND
144
                        cprintf("No possible !\n");
145
#endif
146
                        return 1;
147
                }
148
        } else
149
                return 1;
150
 
151
        return 0;
152
}
153
 
154
TASK servo_send()
155
{
156
        HEXAPOD_STATE   old_status;
157
        register char   changes, new_pos, new_pwm, new_power;
158
        int             n;
159
 
160
        for (n=0; n<6;n++) {
161
                old_status.leg[n].x = 150;
162
                old_status.leg[n].y =   0;
163
                old_status.leg[n].z =   0;
164
                old_status.leg[n].pwm = 0;
165
        }
166
        old_status.power = 0;
167
 
168
        while (1) {
169
                changes = 0;
170
 
171
                for (n=0; n<6; n++){
172
                        new_pos = 0;
173
                        new_pwm = 0;
174
                        new_power = 0;
175
 
176
                        //sem_wait(&mx_status);
177
                        if ((status.leg[n].x != old_status.leg[n].x) || (status.leg[n].y != old_status.leg[n].y) || (status.leg[n].z != old_status.leg[n].z)) {
178
                                if (set_leg_position(n, status.leg[n].x, status.leg[n].y, status.leg[n].z)==0) {
179
                                        old_status.leg[n].x = status.leg[n].x;
180
                                        old_status.leg[n].y = status.leg[n].y;
181
                                        old_status.leg[n].z = status.leg[n].z;
182
                                        new_pos++;
183
                                } else {
184
#ifdef  DEBUG_SEND
185
                                        cprintf("Position not reachable.\n");
186
#endif
187
                                        status.leg[n].x = old_status.leg[n].x;
188
                                        status.leg[n].y = old_status.leg[n].y;
189
                                        status.leg[n].z = old_status.leg[n].z;
190
                                }
191
                        }
192
                        if (status.leg[n].pwm != old_status.leg[n].pwm) {
193
                                old_status.leg[n].pwm = status.leg[n].pwm;
194
                                new_pwm++;
195
                        }
196
                        if (status.power != old_status.power) {
197
                                old_status.power = status.power;
198
                                new_power++;
199
                        }
200
                        //sem_post(&mx_status);
201
 
202
                        if (new_pos) {
203
#ifdef  SERIAL_ON
204
                                sem_wait(&mx_servo);
205
                                servo_set_angle_sec(COM_PORT, n*3+2, angles[n].a);
206
                                servo_set_angle_sec(COM_PORT, n*3+1, angles[n].b);
207
                                servo_set_angle_sec(COM_PORT, n*3  , angles[n].c);
208
                                sem_post(&mx_servo);
209
#endif
210
                        }
211
 
212
                        if (new_pwm) {
213
#ifdef  SERIAL_ON
214
                                sem_wait(&mx_servo);
215
                                (old_status.leg[n].pwm & 1) ? servo_turn_on(COM_PORT, n*3+2) : servo_turn_off(COM_PORT, n*3+2);
216
                                (old_status.leg[n].pwm & 2) ? servo_turn_on(COM_PORT, n*3+1) : servo_turn_off(COM_PORT, n*3+1);
217
                                (old_status.leg[n].pwm & 4) ? servo_turn_on(COM_PORT, n*3  ) : servo_turn_off(COM_PORT, n*3  );
218
                                sem_post(&mx_servo);
219
#endif
220
                        }
221
                        if (new_power) {
222
#ifdef  SERIAL_ON
223
                                if (old_status.power) {
224
                                        sem_wait(&mx_servo);
225
                                        servo_set_RC5_switch(COM_PORT, 1);
226
                                        sem_post(&mx_servo);
227
                                } else {
228
                                        sem_wait(&mx_servo);
229
                                        servo_set_RC5_switch(COM_PORT, 0);
230
                                        sem_post(&mx_servo);
231
                                }
232
#endif
233
                        }
234
 
235
                        if (new_pos || new_pwm || new_power) {
236
                                changes++;
237
#ifdef  DEBUG_SEND
238
                                print_status(n);
239
#endif
240
                        }
241
                }
242
                task_endcycle();
243
        }
244
 
245
        return 0;
246
}
247
 
248
TASK servo_get()
249
{
250
        int     i = 0;
251
 
252
        while (1) {
253
#ifdef  SERIAL_ON
254
                //sem_wait(&mx_status);
255
                sem_wait(&mx_servo);
256
                status.leg[i  ].adc_in = servo_get_analog(COM_PORT, i  );
257
                status.leg[i+1].adc_in = servo_get_analog(COM_PORT, i+1);
258
                sem_post(&mx_servo);
259
#ifdef  DEBUG_SEND
260
                cprintf("Leg %1d-%1d: (%4d) (%4d)\n", i, i+1, status.leg[i].adc_in, status.leg[i+1].adc_in);
261
#endif
262
                i = (i+2)%6;
263
                //sem_post(&mx_status);
264
#endif
265
                task_endcycle();
266
        }
267
 
268
        return 0;
269
}
270
 
271
int init_serial()
272
{
273
        int err;
274
        err = servo_open(COM_PORT, COM_SPEED);
275
 
276
        return err;
277
}
278
 
279
void end_serial()
280
{
281
        servo_close(COM_PORT);
282
}
283
 
284
void init_send_task()
285
{
286
        HARD_TASK_MODEL ms;
287
        PID pid;
288
 
289
        hard_task_default_model(ms);
290
        hard_task_def_ctrl_jet(ms);
291
        hard_task_def_wcet(ms, SEND_TASK_WCET);
292
        hard_task_def_mit(ms, SEND_TASK_MIT);
293
        hard_task_def_usemath(ms);
294
        pid = task_create("Send_Task", servo_send, &ms, NULL);
295
        if (pid == NIL) {
296
                perror("Could not create task <Send_Task>");
297
                sys_end();
298
        } else
299
                task_activate(pid);
300
 
301
        hard_task_default_model(ms);
302
        hard_task_def_ctrl_jet(ms);
303
        hard_task_def_wcet(ms, GET_TASK_WCET);
304
        hard_task_def_mit(ms, GET_TASK_MIT);
305
        hard_task_def_usemath(ms);
306
        pid = task_create("Get_Task", servo_get, &ms, NULL);
307
        if (pid == NIL) {
308
                perror("Could not create task <Get_Task>");
309
                sys_end();
310
        } else
311
                task_activate(pid);
312
}
313
 
314
void init_send()
315
{
316
        int i;
317
 
318
        if (init_serial()) {
319
                perror("Could not initialize serial port.");
320
                sys_end();
321
        }
322
 
323
        //sem_init(&mx_status,0,1);
324
        sem_init(&mx_servo,0,1);
325
 
326
        for (i=0; i<6;i++) {
327
                status.leg[i].x = 150;
328
                status.leg[i].y =   0;
329
                status.leg[i].z =   0;
330
                status.leg[i].pwm = 0;
331
        }
332
        status.power = 0;
333
 
334
        init_send_task();
335
}
336
 
337
void end_send()
338
{
339
        end_serial();
340
}