summaryrefslogtreecommitdiff
path: root/n/asserv/src/goto.c
blob: 64d1eacc51aaf5e85369af51bed518ade2561b9e (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
/* 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.
 * 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;
/** Destination 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 = 200L << 8;
/** Debug values. */
int32_t goto_dx, goto_dy, goto_dl, goto_da;
/** Movement finished. */
uint8_t goto_finish;

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

/** Linear control mode. */
static inline void
goto_linear_mode (void)
{
    uint16_t d;
    uint32_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_aim = 0;
	speed_right_aim = 0;
	goto_finish = 1;
      }
    else
      {
	/* Convert back to f24.8. */
	int32_t com = goto_d - d;
	com <<= 8;
	speed_distance (goto_sign ? -com : com, 0);
      }
}

/** Angular control mode. */
static inline 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)
      {
	goto_finish = 1;
	speed_left = 0;
	speed_right = 0;
	speed_left_aim = 0;
	speed_right_aim = 0;
      }
    else
      {
	/* Compute arc. */
	goto_da = dsp_mul_f824 (angle_diff, postrack_footing_2pi);
	/* Set speed. */
	speed_distance (0, goto_da);
      }
}

/** Position control mode. */
static inline void
goto_position_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;
	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 / 2, goto_da * 2);
      }
}

/** 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;
	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);
      }
}

/** We fuck the wall mode. */
static inline 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))))
	goto_finish = 1;
}

/** Update the speed according to the desired destination. */
static 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 10:
	goto_ftw_mode ();
	break;
      }
}