summaryrefslogtreecommitdiff
path: root/n/asserv/src/goto.c
blob: 07ab840937587ca5dbf3b17b1c67dc06adc11d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* goto.c */
/* asserv - Position & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

/** Goto mode:
 * 0: linear move.
 * 1: angular move.
 * 2: position move.
 * 4: counter move.
 * 10: we fuck the wall.
 */
uint8_t goto_mode;
/** Distance for mode 0, ui16. */
uint16_t goto_d;
/** Sign of movement (0: forward, 1 backward). */
uint8_t goto_sign;
/** Counter aim. */
int16_t goto_counter_left, goto_counter_right;
/** Destination/start position, f24.8. */
int32_t goto_x, goto_y;
/** Destination angle, f0.8. */
int8_t goto_a;
/** Travel speed for fixed speed movements, i8. */
int8_t goto_s;
/** Destination epsillon. */
int32_t goto_eps = 20L << 8;
/** Debug values. */
int32_t goto_dx, goto_dy, goto_dl, goto_da;
/** Movement finished. */
uint8_t goto_finish;

/* +AutoDec */
/* -AutoDec */

/** Linear control mode. */
void
goto_linear_mode (void)
{
    uint16_t d;
    int32_t dx, dy;
    /* Compute distance from the start point.
     * WARNING: this could overflow as dsp_hypot accept a maximum value of
     * +/- 65535. */
    dx = goto_x - postrack_x;
    dy = goto_y - postrack_y;
    d = dsp_hypot (dx, dy);
    /* Change speed. */
    if (d > goto_d)
      {
	speed_left = 0;
	speed_right = 0;
	speed_left_aim = 0;
	speed_right_aim = 0;
	if (!goto_finish)
	    goto_finish = 1;
      }
    else
      {
	/* Convert back to f24.8. */
	int32_t com = goto_d - d;
	com <<= 8;
	speed_distance (goto_sign ? -com : com, 0);
      }
}

/** Setup linear mode. */
static inline void
goto_linear (int16_t d)
{
    motor_mode = 2;
    goto_mode = 0;
    goto_sign = d >> 15;
    goto_d = d;
    if (goto_sign) goto_d = -goto_d;
    goto_x = postrack_x;
    goto_y = postrack_y;
    goto_finish = 0;
}

/** Angular control mode. */
void
goto_angular_mode (void)
{
    int32_t angle_diff;
    /* Compute angle diff. */
    angle_diff = v8_to_v32 (0, goto_a, 0, 0) - postrack_a;
    angle_diff <<= 8;
    angle_diff >>= 8;
    /* Small angles. */
    if (0x10000L > angle_diff && angle_diff > -0x10000L)
      {
	speed_left = 0;
	speed_right = 0;
	speed_left_aim = 0;
	speed_right_aim = 0;
	if (!goto_finish)
	    goto_finish = 1;
      }
    else
      {
	/* Compute arc. */
	goto_da = dsp_mul_f824 (angle_diff, postrack_footing_2pi);
	/* Set speed. */
	speed_distance (0, goto_da);
      }
}

/** Setup angular mode. */
static inline void
goto_angular (int8_t a)
{
    motor_mode = 2;
    goto_mode = 1;
    goto_a = a;
    goto_finish = 0;
}

/** Position control mode. */
static inline void
goto_position_mode (void)
{
    int32_t c, s, arc;
    int32_t dla, daa;
    goto_dx = goto_x - postrack_x;	/* f24.8 */
    goto_dy = goto_y - postrack_y;
    if (goto_dx < goto_eps && goto_dx > -goto_eps
	&& goto_dy < goto_eps && goto_dy > -goto_eps)
      {
	speed_left = 0;
	speed_right = 0;
	speed_left_aim = 0;
	speed_right_aim = 0;
	if (!goto_finish)
	    goto_finish = 1;
      }
    else
      {
	/* Project in the robot base. */
	c = dsp_cos_f824 (postrack_a);
	s = dsp_sin_f824 (postrack_a);
	goto_dl = dsp_mul_f824 (goto_dx, c) + dsp_mul_f824 (goto_dy, s);
	goto_da = dsp_mul_f824 (goto_dy, c) - dsp_mul_f824 (goto_dx, s);
	if (goto_dl > 0)
	    dla = goto_dl;
	else
	    dla = -goto_dl;
	if (goto_da > 0)
	    daa = goto_da;
	else
	    daa = -goto_da;
	/* If very big angle (> 83 �), rotate. */
	if (daa > dla * 8)
	  {
	    /* Compute arc. */
	    arc = postrack_footing_2pi / 4;
	    speed_distance (0, goto_da > 0 ? arc : -arc);
	  }
	/* If big angle (> 1.7�), rotate. */
	else if (daa * 32 > dla)
	  {
	    /* Compute arc. This is a rough aproximation. */
	    arc = goto_da / (goto_dl >> 8) * (postrack_footing / 2);
	    speed_distance (0, arc);
	  }
	/* Is small angle (< 0.44�), strait ahead. */
	else if (daa * 128 < dla)
	  {
	    speed_distance (goto_dl, 0);
	  }
	/* Else, curve. */
	else
	  {
	    speed_distance (goto_dl, 0);
	    if ((goto_da >= 0 && goto_dl >= 0)
		|| (goto_da < 0 && goto_dl < 0))
	      {
		speed_left_aim--;
		speed_right_aim++;
	      }
	    else
	      {
		speed_left_aim++;
		speed_right_aim--;
	      }
	  }
      }
}

/** Setup position mode. */
static inline void
goto_position (int32_t x, int32_t y)
{
    motor_mode = 2;
    goto_mode = 2;
    goto_x = x;
    goto_y = y;
    goto_finish = 0;
}

/** Position control mode, `exact' method. */
static inline void
goto_position_exact_mode (void)
{
    int32_t c, s;
    /* Project in the robot base. */
    goto_dx = goto_x - postrack_x;	/* f24.8 */
    goto_dy = goto_y - postrack_y;
    if (goto_dx < goto_eps && goto_dx > -goto_eps
	&& goto_dy < goto_eps && goto_dy > -goto_eps)
      {
	speed_left_aim = 0;
	speed_right_aim = 0;
	if (!goto_finish)
	    goto_finish = 1;
      }
    else
      {
	c = dsp_cos_f824 (postrack_a);
	s = dsp_sin_f824 (postrack_a);
	goto_dl = dsp_mul_f824 (goto_dx, c) + dsp_mul_f824 (goto_dy, s);
	goto_da = dsp_mul_f824 (goto_dy, c) - dsp_mul_f824 (goto_dx, s);
	/* Convert da into a arc. This is a rough aproximation. */
	goto_da = goto_da * (postrack_footing / 2) / (goto_dl >> 8);
	speed_distance (goto_dl, goto_da);
      }
}

/** Counter control mode. */
void
goto_counter_mode (void)
{
    int16_t dl, dr;
    int32_t dl248, dr248;
    dl = goto_counter_left - counter_left;
    dr = goto_counter_right - counter_right;
    if (dl < 3 && dl > -3
	&& dr < 3 && dr > -3)
      {
	if (!goto_finish)
	    goto_finish = 1;
      }
    dl248 = dl;
    dr248 = dr;
    speed_distance_lr (dl248 << 8, dr248 << 8);
}

/** Setup counter mode. */
static inline void
goto_counter (int16_t l, int16_t r)
{
    motor_mode = 2;
    goto_mode = 4;
    goto_counter_left = speed_left_counter + l;
    goto_counter_right = speed_right_counter + r;
    goto_finish = 0;
}

/** We fuck the wall mode. */
void
goto_ftw_mode (void)
{
    /* Change speed. */
    if (PINA & _BV (0))
      {
	speed_left_aim = goto_s;
      }
    else
      {
	speed_left = 0;
	speed_left_aim = 0;
      }
    if (PINA & _BV (7))
      {
	speed_right_aim = goto_s;
      }
    else
      {
	speed_right = 0;
	speed_right_aim = 0;
      }
    if (!(PINA & (_BV (0) | _BV (7))))
	if (!goto_finish)
	    goto_finish = 1;
}

/** Setup ftw mode. */
static inline void
goto_ftw (int8_t s)
{
    motor_mode = 2;
    goto_mode = 10;
    goto_s = s;
    goto_finish = 0;
}

/** Update the speed according to the desired destination. */
void
goto_update (void)
{
    switch (goto_mode)
      {
      case 0:
	goto_linear_mode ();
	break;
      case 1:
	goto_angular_mode ();
	break;
      case 2:
	//goto_position_mode ();
	break;
      case 4:
	goto_counter_mode ();
	break;
      case 10:
	goto_ftw_mode ();
	break;
      }
}