summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/move_cb.c
blob: a92135eaba6c6b6c71530435d3c5ce3e02035536 (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
/* move_cb.c */
/*  {{{
 *
 * Copyright (C) 2008 Nélio Laranjeiro
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * 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.
 *
 * }}} */
#include "common.h"
#include "fsm.h"
#include "move_cb.h"

#include "path.h"
#include "asserv.h"
#include "playground.h"
#include "move.h"

#include "main.h"      /* main_post_event_for_top_fsm */
#include "modules/math/fixed/fixed.h"	/* fixed_* */

/**
 * The real radius of the obstacle.
 */
#define MOVE_REAL_OBSTACLE_RADIUS 150

/**
 * The sharp distance between the bot and the obstacle.
 */
#define MOVE_SHARP_DISTANCE 300

/**
 * The distance between the axis of the bot and the front sharp.
 */
#define MOVE_AXIS_FRONT_SHARP 150

/**
 * The standard distance of the obstacle.
 */
#define MOVE_OBSTACLE_DISTANCE \
    (MOVE_REAL_OBSTACLE_RADIUS + MOVE_SHARP_DISTANCE + MOVE_AXIS_FRONT_SHARP)

/**
 * The radius of the obstacle for the path module.
 * It corresponds to the real radius of the obstacle plus the distance you
 * want to add to avoid it.
 */
#define MOVE_OBSTACLE_RADIUS (MOVE_REAL_OBSTACLE_RADIUS + 250)

/**
 * The generic validity time (in term of number of cyles).
 */
#define MOVE_OBSTACLE_VALIDITY (5 * 225)

/**
 * Cycles count to ignore sharp event in the main loop.
 */
#define MOVE_MAIN_IGNORE_SHARP_EVENT (3 * 225)

/**
 * Easier function to get the next intermediate positon from the path module.
 * @param dst new destination position computed by the path module
 */
void
move_get_next_position (move_position_t *dst)
{
    /* Get the current position */
    asserv_position_t current_pos;
    asserv_get_position (&current_pos);
    /* Give the current position of the bot to the path module */
    path_endpoints (current_pos.x, current_pos.y,
		    move_data.final.x, move_data.final.y);
    /* Update the path module */
    path_update ();

    /* Retrieve next path coordinate */
    if (!path_get_next (&dst->x, &dst->y))
      {
	/* If it failed, try original destination */
	dst->x = move_data.final.x;
	dst->y = move_data.final.y;
      }
    main_sharp_ignore_event = MOVE_MAIN_IGNORE_SHARP_EVENT;
}

/**
 * Compute the obstacle position assuming it is right in front of us.
 * @param cur current position
 * @param obstacle the obstacle position computed
 */
void
move_compute_obstacle_position (asserv_position_t cur,
				move_position_t *obstacle)
{
    /* Convert the angle */
    uint32_t angle = cur.a;
    angle = angle << 8;
    /* X */
    obstacle->x = cur.x + fixed_mul_f824 (fixed_cos_f824 (angle),
					  MOVE_OBSTACLE_DISTANCE);
    /* Y */
    obstacle->y = cur.y + fixed_mul_f824 (fixed_sin_f824 (angle),
					  MOVE_OBSTACLE_DISTANCE);
}
/**
 * Unique function to compute the obstacle position from here.
 */
void
move_obstacle_here (void)
{
    /* Get the current position of the bot */
    asserv_position_t current;
    asserv_get_position (&current);
    /* Compute the obstacle position */
    move_compute_obstacle_position (current, &move_data.obstacle);
    /* Give it to the path module */
    path_obstacle (0, move_data.obstacle.x, move_data.obstacle.y,
		   MOVE_OBSTACLE_RADIUS, MOVE_OBSTACLE_VALIDITY);
}

/**
 * Unique function after moving backward to have unique code.
 */
void
move_after_moving_backward (void)
{
    /* Give the current position of the bot to the path module */
    move_get_next_position (&move_data.intermediate);
    /* Go to the next position */
    asserv_goto (move_data.intermediate.x, move_data.intermediate.y);
}

/*
 * IDLE =start=>
 *  => MOVING_TO_FINAL_POSITION
 *   ask the asserv to go to the final position
 */
fsm_branch_t
move__IDLE__start (void)
{
    /* Go to the destination position */
    asserv_goto (move_data.final.x, move_data.final.y);
    return move_next (IDLE, start);
}

/*
 * MOVING_TO_INTERMEDIATE_POSITION =bot_move_succeed=>
 * final_position => IDLE
 *   post an event for the top FSM to tell we have finished
 * position_intermediary => MOVING_TO_INTERMEDIATE_POSITION
 *   go to the next intermediate position computed by the path module
 */
fsm_branch_t
move__MOVING_TO_INTERMEDIATE_POSITION__bot_move_succeed (void)
{
    if ((move_data.final.x == move_data.intermediate.x) &&
	(move_data.final.y == move_data.intermediate.y))
      {
	/* Post an event for the top FSM to tell we have finished */
	main_post_event_for_top_fsm = TOP_EVENT_move_fsm_finished;
	return move_next_branch (MOVING_TO_INTERMEDIATE_POSITION, bot_move_succeed, final_position);
      }
    else
      {
	/* Get next position */
	move_get_next_position (&move_data.intermediate);
	/* Go to the next intermediate position */
	asserv_goto (move_data.intermediate.x, move_data.intermediate.y);
	return move_next_branch (MOVING_TO_INTERMEDIATE_POSITION, bot_move_succeed, position_intermediary);
      }
}

/*
 * MOVING_TO_INTERMEDIATE_POSITION =bot_move_obstacle=>
 *  => MOVING_TO_INTERMEDIATE_POSITION
 *   compute the obstacle position
 *   give the obstacle position to the path module
 *   go to the next intermediate position computed by the path module
 */
fsm_branch_t
move__MOVING_TO_INTERMEDIATE_POSITION__bot_move_obstacle (void)
{
    /* Compute obstacle position */
    move_obstacle_here ();
    /* Get next position */
    move_get_next_position (&move_data.intermediate);
    /* Go to the next intermediate position */
    asserv_goto (move_data.intermediate.x, move_data.intermediate.y);
    return move_next (MOVING_TO_INTERMEDIATE_POSITION, bot_move_obstacle);
}

/*
 * MOVING_TO_INTERMEDIATE_POSITION =bot_move_failed=>
 *  => MOVING_BACKWARD
 *   store the current position of the obstacle
 *   move backward to turn freely
 */
fsm_branch_t
move__MOVING_TO_INTERMEDIATE_POSITION__bot_move_failed (void)
{
    /* Compute obstacle position */
    move_obstacle_here ();
    /* Go backward */
    asserv_move_linearly (-PG_MOVE_DISTANCE); 
    return move_next (MOVING_TO_INTERMEDIATE_POSITION, bot_move_failed);
}

/*
 * MOVING_BACKWARD =bot_move_failed=>
 *  => MOVING_TO_INTERMEDIATE_POSITION
 *   do the same as when we succeed
 *   give the obstacle position to the path module
 *   give the current position of the bot to the path module
 *   get the intermediate position from path module
 */
fsm_branch_t
move__MOVING_BACKWARD__bot_move_failed (void)
{
    /* Call generic function */
    move_after_moving_backward ();
    return move_next (MOVING_BACKWARD, bot_move_failed);
}

/*
 * MOVING_BACKWARD =bot_move_succeed=>
 *  => MOVING_TO_INTERMEDIATE_POSITION
 *   give the obstacle position to the path module
 *   give the current position of the bot to the path module
 *   get the intermediate position from path module
 */
fsm_branch_t
move__MOVING_BACKWARD__bot_move_succeed (void)
{
    /* Call generic function */
    move_after_moving_backward ();
    return move_next (MOVING_BACKWARD, bot_move_succeed);
}

/*
 * MOVING_TO_FINAL_POSITION =bot_move_failed=>
 *  => MOVING_BACKWARD
 *   store the current position of the obstacle
 *   move backward to turn freely
 */
fsm_branch_t
move__MOVING_TO_FINAL_POSITION__bot_move_failed (void)
{
    /* Compute obstacle position */
    move_obstacle_here ();
    /* Move backward to turn freely */
    asserv_move_linearly (-PG_MOVE_DISTANCE);
    return move_next (MOVING_TO_FINAL_POSITION, bot_move_failed);
}

/*
 * MOVING_TO_FINAL_POSITION =bot_move_succeed=>
 *  => IDLE
 *   post an event for the top FSM to tell we have finished
 */
fsm_branch_t
move__MOVING_TO_FINAL_POSITION__bot_move_succeed (void)
{
    /* Post an event for the top FSM to tell we have finished */
    main_post_event_for_top_fsm = TOP_EVENT_move_fsm_finished;
    return move_next (MOVING_TO_FINAL_POSITION, bot_move_succeed);
}

/*
 * MOVING_TO_FINAL_POSITION =bot_move_obstacle=>
 *  => MOVING_TO_INTERMEDIATE_POSITION
 *   compute the obstacle position
 *   give the obstacle position to the path module
 *   give the current position of the bot to the path module
 *   get the intermediate position from path module
 */
fsm_branch_t
move__MOVING_TO_FINAL_POSITION__bot_move_obstacle (void)
{
    /* Compute obstacle position */
    move_obstacle_here ();
    /* Get next position */
    move_get_next_position (&move_data.intermediate);
    /* Go to the next intermediate position */
    asserv_goto (move_data.intermediate.x, move_data.intermediate.y);
    return move_next (MOVING_TO_FINAL_POSITION, bot_move_obstacle);
}