Rev 1335 | Rev 1359 | 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; |
||
1336 | giacomo | 78 | |
79 | clear(); |
||
1332 | giacomo | 80 | |
81 | sem_wait(&mx_servo); |
||
82 | servo_turn_off(COM_PORT, active_leg*3+2); |
||
83 | servo_turn_off(COM_PORT, active_leg*3+1); |
||
84 | servo_turn_off(COM_PORT, active_leg*3); |
||
85 | |||
86 | servo_count = 0; |
||
87 | servo_turn_on(COM_PORT, active_leg*3+2); |
||
88 | turn_on = 0; |
||
89 | sem_post(&mx_servo); |
||
90 | |||
91 | while (calibrate_status == 1) { |
||
92 | |||
93 | sem_wait(&mx_servo); |
||
94 | |||
95 | if (calibrate_exec_step == 100000) { |
||
96 | |||
97 | angsec_temp[servo_count] = test_angle[3*active_leg+num]; |
||
98 | printf_xy(0,10+servo_count,WHITE,"%08d",test_angle[3*active_leg+num]); |
||
99 | servo_turn_off(COM_PORT,3*active_leg+num); |
||
100 | servo_count++; |
||
101 | |||
102 | if (servo_count == 6) { |
||
103 | |||
104 | for (i=0;i<3;i++) { |
||
105 | calibration_table[active_leg].delta_90[i] = abs(angsec_temp[2*i+1] - angsec_temp[2*i] + 1); |
||
1333 | giacomo | 106 | 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 | 107 | |
1333 | giacomo | 108 | printf_xy(22*i,22,WHITE,"D%d %7d Z%d %7d", |
109 | i,calibration_table[active_leg].delta_90[i], |
||
110 | i,calibration_table[active_leg].zero[i]); |
||
1332 | giacomo | 111 | |
112 | } |
||
113 | |||
114 | calibrate_status = 0; |
||
115 | calibrate_exec_step = 0; |
||
116 | |||
117 | sem_post(&mx_servo); |
||
118 | |||
119 | task_kill(exec_shadow); |
||
120 | task_testcancel(); |
||
121 | |||
122 | } |
||
123 | |||
124 | turn_on = 1; |
||
125 | |||
126 | calibrate_exec_step = 0; |
||
127 | |||
128 | } |
||
129 | |||
130 | switch (servo_count) { |
||
131 | case 0: |
||
132 | case 1: |
||
133 | sprintf(servo_name,"ALFA "); |
||
134 | num = 2; |
||
135 | break; |
||
136 | case 2: |
||
137 | case 3: |
||
138 | sprintf(servo_name,"BETA "); |
||
139 | num = 1; |
||
140 | break; |
||
141 | case 4: |
||
142 | case 5: |
||
143 | sprintf(servo_name,"GAMMA"); |
||
144 | num = 0; |
||
145 | break; |
||
146 | } |
||
147 | |||
148 | if (turn_on == 1) { |
||
149 | servo_turn_on(COM_PORT,3*active_leg+num); |
||
150 | turn_on = 0; |
||
151 | } |
||
152 | |||
153 | set = calibration_table[active_leg].pos[servo_count]; |
||
154 | printf_xy(10,10+servo_count,WHITE,"Set servo %s to position %d",servo_name,set); |
||
155 | |||
156 | servo_turn_on(COM_PORT, active_leg*3+num); |
||
157 | |||
158 | if (calibrate_exec_step != 0) { |
||
159 | |||
160 | test_angle[3*active_leg+num] += calibrate_exec_step; |
||
161 | printf_xy(10,20,WHITE,"Set %08d to servo %03d",test_angle[3*active_leg+num],3*active_leg+num); |
||
162 | servo_set_angle_sec(COM_PORT, 3*active_leg+num, test_angle[3*active_leg+num]); |
||
163 | |||
164 | calibrate_exec_step = 0; |
||
165 | |||
166 | } |
||
167 | |||
168 | angsec = servo_get_angle_sec(COM_PORT, 3*active_leg+num); |
||
169 | |||
170 | sem_post(&mx_servo); |
||
171 | |||
172 | printf_xy(10,21,WHITE,"Angle Seconds = %08d",angsec); |
||
173 | |||
174 | task_endcycle(); |
||
175 | |||
176 | } |
||
177 | |||
178 | return 0; |
||
179 | |||
180 | } |
||
181 | |||
182 | void calibrate_init() { |
||
183 | |||
184 | SOFT_TASK_MODEL st; |
||
185 | PID st_pid; |
||
186 | |||
187 | if (calibrate_status == 0) { |
||
188 | calibrate_status = 1; |
||
189 | |||
190 | soft_task_default_model(st); |
||
191 | soft_task_def_period(st,300000); |
||
192 | soft_task_def_met(st,30000); |
||
193 | soft_task_def_usemath(st); |
||
194 | soft_task_def_ctrl_jet(st); |
||
195 | |||
196 | st_pid = task_create("Calibration task",calibrate,&st,NULL); |
||
197 | if (st_pid == NIL) { |
||
198 | cprintf("Error creating calibration task\n"); |
||
199 | sys_end(); |
||
200 | } |
||
201 | |||
202 | task_activate(st_pid); |
||
203 | |||
204 | } else { |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | } |
||
209 | |||
210 | void calibrate_step(int step) { |
||
211 | |||
212 | if (calibrate_status != 0 && calibrate_exec_step == 0) { |
||
213 | |||
214 | calibrate_exec_step = step; |
||
215 | |||
216 | } |
||
217 | |||
218 | } |