Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1332 giacomo 1
 
2
/*
3
 * Project: S.Ha.R.K.
4
 *
5
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
6
 *
7
 *
8
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
9
 *
10
 * http://www.sssup.it
11
 * http://retis.sssup.it
12
 * http://shark.sssup.it
13
 */
14
 
15
/*
16
 * Copyright (C) 2000 Paolo Gai
17
 *
18
 * This program is free software; you can redistribute it and/or modify
19
 * it under the terms of the GNU General Public License as published by
20
 * the Free Software Foundation; either version 2 of the License, or
21
 * (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31
 *
32
 */
33
 
34
#include "chimera.h"
35
#include <ll/i386/64bit.h>
36
 
37
volatile int calibrate_status = 0;
38
volatile int calibrate_exec_step = 0;
39
 
40
extern HEXAPOD_STATE status;
41
extern unsigned char active_leg;
42
 
43
extern sem_t mx_servo;
44
 
45
struct leg_calibration {
1333 giacomo 46
        int side;
1332 giacomo 47
        int pos[6];
48
        int delta_90[3];
49
        int zero[3];
50
};
51
 
52
struct leg_calibration calibration_table[] = {
1335 giacomo 53
        {1,{0,90,-45,45,-90,0},{432001,403201,432001},{-201600,28800,241201}},
54
        {1,{0,90,-45,45,-45,45},{417601,388801,424741},{-216000,-43200,82770}},
55
        {1,{0,90,-45,45,0,90},{414001,424801,410401},{-162000,39600,-122400}},
1333 giacomo 56
        {-1,{0,90,-45,45,-90,0},{413949,453599,431999},{216000,50401,-215599}},
57
        {-1,{0,90,-45,45,-45,45},{421199,421199,443799},{165600,-8999,30600}},
58
        {-1,{0,90,-45,45,0,90},{0,0,0},{0,0,0}},
1332 giacomo 59
};
60
 
61
int adjust(int angle_sec, int leg, int num) {
62
 
63
        int temp;
64
 
65
        smul32div32to32(angle_sec,calibration_table[leg].delta_90[num],324000,temp);
66
 
1333 giacomo 67
        return calibration_table[leg].side * temp + calibration_table[leg].zero[num];
1332 giacomo 68
 
69
}
70
 
71
TASK calibrate(void *arg) {
72
 
73
        int i, num = 0, angsec = 0, angsec_temp[6];
74
        static int test_angle[20],set,turn_on,servo_count;
75
        char servo_name[10];
76
 
77
        for (i=0;i<20;i++) test_angle[i] = 0;
78
 
79
        sem_wait(&mx_servo);
80
        servo_turn_off(COM_PORT, active_leg*3+2);
81
        servo_turn_off(COM_PORT, active_leg*3+1);
82
        servo_turn_off(COM_PORT, active_leg*3);
83
 
84
        servo_count = 0;
85
        servo_turn_on(COM_PORT, active_leg*3+2);
86
        turn_on = 0;
87
        sem_post(&mx_servo);
88
 
89
        while (calibrate_status == 1) {
90
 
91
                sem_wait(&mx_servo);
92
 
93
                if (calibrate_exec_step == 100000) {
94
 
95
                        angsec_temp[servo_count] = test_angle[3*active_leg+num];                                                                                                    
96
                        printf_xy(0,10+servo_count,WHITE,"%08d",test_angle[3*active_leg+num]);
97
                        servo_turn_off(COM_PORT,3*active_leg+num);
98
                        servo_count++;
99
 
100
                        if (servo_count == 6) {
101
 
102
                                for (i=0;i<3;i++) {
103
                                        calibration_table[active_leg].delta_90[i] = abs(angsec_temp[2*i+1] - angsec_temp[2*i] + 1);
1333 giacomo 104
                                        calibration_table[active_leg].zero[i] = calibration_table[active_leg].side * abs(calibration_table[active_leg].pos[2*i] * calibration_table[active_leg].delta_90[i] / 90) + angsec_temp[2*i];
1332 giacomo 105
 
1333 giacomo 106
                                        printf_xy(22*i,22,WHITE,"D%d %7d Z%d %7d",
107
                                                        i,calibration_table[active_leg].delta_90[i],
108
                                                        i,calibration_table[active_leg].zero[i]);
1332 giacomo 109
 
110
                                }
111
 
112
                                calibrate_status = 0;
113
                                calibrate_exec_step = 0;
114
 
115
                                sem_post(&mx_servo);
116
 
117
                                task_kill(exec_shadow);
118
                                task_testcancel();
119
 
120
                        }
121
 
122
                        turn_on = 1;
123
 
124
                        calibrate_exec_step = 0;
125
 
126
                }
127
 
128
                switch (servo_count) {
129
                        case 0:
130
                        case 1:
131
                                sprintf(servo_name,"ALFA ");
132
                                num = 2;
133
                                break;
134
                        case 2:
135
                        case 3:
136
                                sprintf(servo_name,"BETA ");
137
                                num = 1;
138
                                break;
139
                        case 4:
140
                        case 5:
141
                                sprintf(servo_name,"GAMMA");
142
                                num = 0;
143
                                break;
144
                }
145
 
146
                if (turn_on == 1) {
147
                        servo_turn_on(COM_PORT,3*active_leg+num);
148
                        turn_on = 0;
149
                }      
150
 
151
                set = calibration_table[active_leg].pos[servo_count];
152
                printf_xy(10,10+servo_count,WHITE,"Set servo %s to position %d",servo_name,set);
153
 
154
                servo_turn_on(COM_PORT, active_leg*3+num);
155
 
156
                if (calibrate_exec_step != 0) {
157
 
158
                        test_angle[3*active_leg+num] += calibrate_exec_step;
159
                        printf_xy(10,20,WHITE,"Set %08d to servo %03d",test_angle[3*active_leg+num],3*active_leg+num);
160
                        servo_set_angle_sec(COM_PORT, 3*active_leg+num, test_angle[3*active_leg+num]);
161
 
162
                        calibrate_exec_step = 0;
163
 
164
                }
165
 
166
                angsec = servo_get_angle_sec(COM_PORT, 3*active_leg+num);
167
 
168
                sem_post(&mx_servo);
169
 
170
                printf_xy(10,21,WHITE,"Angle Seconds = %08d",angsec);
171
 
172
                task_endcycle();
173
 
174
        }
175
 
176
        return 0;
177
 
178
}
179
 
180
void calibrate_init() {
181
 
182
        SOFT_TASK_MODEL st;
183
        PID st_pid;
184
 
185
        if (calibrate_status == 0) {
186
                calibrate_status = 1;
187
 
188
                soft_task_default_model(st);
189
                soft_task_def_period(st,300000);
190
                soft_task_def_met(st,30000);
191
                soft_task_def_usemath(st);
192
                soft_task_def_ctrl_jet(st);
193
 
194
                st_pid = task_create("Calibration task",calibrate,&st,NULL);
195
                if (st_pid == NIL) {
196
                        cprintf("Error creating calibration task\n");
197
                        sys_end();
198
                }
199
 
200
                task_activate(st_pid);
201
 
202
        } else {
203
                return;
204
        }
205
 
206
}
207
 
208
void calibrate_step(int step) {
209
 
210
        if (calibrate_status != 0 && calibrate_exec_step == 0) {
211
 
212
                calibrate_exec_step = step;
213
 
214
        }
215
 
216
}