summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/getsamples_cb.c
blob: 8679288719c8c42140e8e69ca80a9981e145f359 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* getsamples_cb.c - getsamples FSM callbacks. */
/*  {{{
 *
 * 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 "getsamples_cb.h"
#include "getsamples.h"
#include "asserv.h"
#include "trap.h"
#include "move.h"

#include "giboulee.h"	/* BOT_ */
#include "playground.h"	/* PG_* */
#include "main.h"	/* main_post_event_for_top_fsm */
#include "chrono.h"

#include "modules/proto/proto.h"
#include "modules/utils/utils.h"

#include "io.h"


uint16_t originate_position_;

/**
 * The distance to go backward from the distributor.
 */
#define GET_SAMPLES_MOVE_BACKWARD_DISTANCE 7

/**
 * Arm time out.
 */
#define GET_SAMPLES_ARM_TIMEOUT (5 * 225)

/**
 * Move back timeout.
 */
#define GET_SAMPLES_MOVE_AWAY_TIMEOUT (225)

/**
 * 'Private' get samples data used internaly by the FSM.
 */
extern struct getsamples_data_t getsamples_data_;

/**
 * Configure the classifier (using the trap and the internal bit field) for
 * the first bit set to 1.
 * After the configuring the classifier, the bit will be reset to 0 to use the
 * next one when calling this function again.
 */
void
getsamples_configure_classifier (void);

/* Configure the classifier (using the trap and the internal bit field) for the first bit set to 1. */
void
getsamples_configure_classifier (void)
{
    uint8_t trap_num;
    /* Go through all the bits of the sample bit field */
    for (trap_num = out_left_box; trap_num < trap_count; trap_num++)
      {
	/* Is the bit set? */
	if (bit_is_set (getsamples_data_.sample_bitfield, trap_num))
	  {
	    /* Configure the classifier */
	    trap_setup_path_to_box (trap_num);
	    /* Reset this bit */
	    getsamples_data_.sample_bitfield &= ~_BV (trap_num);
	    /* Stop here */
	    return;
	  }
      }
}

/* Count the number of samples to take. */
uint8_t
getsamples_count_samples (void)
{
    uint8_t i, bit, count = 0;
    for (i = 0, bit = 1; i < 8; i++, bit <<= 1)
	if (getsamples_data_.sample_bitfield & bit)
	    count++;
    return count;
}

/*
 * MOVE_BACKWARD_FROM_DISTRIBUTOR =bot_move_succeed=>
 *  => TAKE_SAMPLES
 *   start taking some samples
 *   setup a timeout
 */
fsm_branch_t
getsamples__MOVE_BACKWARD_FROM_DISTRIBUTOR__bot_move_succeed (void)
{
    /* start taking some samples */
    getsamples_data_.arm_noted_position = asserv_get_arm_position () +
	BOT_ARM_NOTED_POSITION;
    asserv_arm_set_position_reached (getsamples_data_.arm_noted_position);
    asserv_move_arm (getsamples_count_samples () * BOT_ARM_THIRD_ROUND,
		     BOT_ARM_SPEED);
    /* Post an event for the top FSM to be waked up later */
    main_getsamples_wait_cycle = GET_SAMPLES_ARM_TIMEOUT;
    proto_send1b ('T', 0);
    return getsamples_next (MOVE_BACKWARD_FROM_DISTRIBUTOR, bot_move_succeed);
}

/*
 * FACE_DISTRIBUTOR =bot_move_succeed=>
 *  => OPEN_INPUT_HOLE
 *   move the arm to open the input hole
 */
fsm_branch_t
getsamples__FACE_DISTRIBUTOR__bot_move_succeed (void)
{
    /* Move the arm to open the input hole to be able to take some samples */
    asserv_move_arm (-BOT_ARM_MIN_TO_OPEN, BOT_ARM_SPEED);
    proto_send0 ('O');
    return getsamples_next (FACE_DISTRIBUTOR, bot_move_succeed);
}

/*
 * OPEN_INPUT_HOLE =arm_move_succeed=>
 *  => APPROACH_DISTRIBUTOR
 *   start approaching the distributor now
 */
fsm_branch_t
getsamples__OPEN_INPUT_HOLE__arm_move_succeed (void)
{
    /* Approach the distributor */
    asserv_go_to_distributor ();
    proto_send0 ('A');
    return getsamples_next (OPEN_INPUT_HOLE, arm_move_succeed);
}

/*
 * CLOSE_INPUT_HOLE =wait_finished=>
 *  => IDLE
 *   timed out, give up
 *   tell the top FSM we have finished
 */
fsm_branch_t
getsamples__CLOSE_INPUT_HOLE__wait_finished (void)
{
    /* Give up, tell the top FSM we have finished */
    main_post_event_for_top_fsm = TOP_EVENT_get_samples_fsm_finished;
    proto_send0 ('I');
    return getsamples_next (CLOSE_INPUT_HOLE, wait_finished);
}

/*
 * CLOSE_INPUT_HOLE =arm_move_succeed=>
 *  => IDLE
 *   tell the top FSM we have finished
 */
fsm_branch_t
getsamples__CLOSE_INPUT_HOLE__arm_move_succeed (void)
{
    /* Tell the top FSM we have finished */
    main_post_event_for_top_fsm = TOP_EVENT_get_samples_fsm_finished;
    return getsamples_next (CLOSE_INPUT_HOLE, arm_move_succeed);
}

/*
 * CLOSE_INPUT_HOLE =arm_pass_noted_position=>
 *  => CLOSE_INPUT_HOLE
 *   prepare the classification of the taken sample
 */
fsm_branch_t
getsamples__CLOSE_INPUT_HOLE__arm_pass_noted_position (void)
{
    /* Prepare the classification of the taken sample */
    getsamples_configure_classifier ();
    return getsamples_next (CLOSE_INPUT_HOLE, arm_pass_noted_position);
}

/*
 * WAIT_AND_TRY_AGAIN =wait_finished=>
 *  => MOVE_AWAY_FROM_DISTRIBUTOR
 *   compute remaining distance (protection agains 0 & 300)
 *   try to move away again
 */
fsm_branch_t
getsamples__WAIT_AND_TRY_AGAIN__wait_finished (void)
{
    /* Get position */
    asserv_position_t position;
    asserv_get_position (&position);

    uint16_t remaining_distance;

    if (getsamples_data_.direction == 0)
      {
	/* Horizontal */
	remaining_distance = UTILS_ABS (position.x - originate_position_);
      }
    else
      {
	/* Vertical */
	remaining_distance = UTILS_ABS (position.y - originate_position_);
      }
    /* Compute real remaining */
    remaining_distance = PG_DISTANCE_DISTRIBUTOR - remaining_distance;
    /* Bound */
    UTILS_BOUND (remaining_distance, 1, PG_DISTANCE_DISTRIBUTOR);
    /* Move away */
    asserv_move_linearly (-remaining_distance);
    return getsamples_next (WAIT_AND_TRY_AGAIN, wait_finished);
}

/*
 * TAKE_SAMPLES =wait_finished=>
 *  => MOVE_AWAY_FROM_DISTRIBUTOR
 *   store current position
 *   timed out, give up
 *   go backward
 */
fsm_branch_t
getsamples__TAKE_SAMPLES__wait_finished (void)
{
    /* Get position */
    asserv_position_t position;
    asserv_get_position (&position);
    if (getsamples_data_.direction == 0)
      {
	/* Horizontal */
	originate_position_ = position.x;
      }
    else
      {
	/* Vertical */
	originate_position_ = position.y;
      }
    /* Go backward */
    asserv_move_linearly (-PG_DISTANCE_DISTRIBUTOR);
    proto_send1b ('M', 1);
    return getsamples_next (TAKE_SAMPLES, wait_finished);
}

/*
 * TAKE_SAMPLES =arm_pass_noted_position=>
 * no_more => MOVE_AWAY_FROM_DISTRIBUTOR
 *   store current position
 *   go backward
 * more => TAKE_SAMPLES
 *   prepare the classification of the taken sample
 *   take a new one
 *   reset the timeout
 */
fsm_branch_t
getsamples__TAKE_SAMPLES__arm_pass_noted_position (void)
{
    /* Prepare classification */
    getsamples_configure_classifier ();
    /* More samples? */
    if (getsamples_data_.sample_bitfield && !(chrono_remaining_time () > 83000))
      {
	/* Compute notifier */
	getsamples_data_.arm_noted_position += BOT_ARM_THIRD_ROUND;
	asserv_arm_set_position_reached (getsamples_data_.arm_noted_position);
	/* Post an event for the top FSM to be waked up later */
	main_getsamples_wait_cycle = GET_SAMPLES_ARM_TIMEOUT;
	/* Continue to take sample */
	proto_send1b ('T', 1);
	return getsamples_next_branch (TAKE_SAMPLES, arm_pass_noted_position, more);
      }
    else
      {
	/* Get position */
	asserv_position_t position;
	asserv_get_position (&position);
	if (getsamples_data_.direction == 0)
	  {
	    /* Horizontal */
	    originate_position_ = position.x;
	  }
	else
	  {
	    /* Vertical */
	    originate_position_ = position.y;
	  }
	/* Go backward */
	asserv_move_linearly (-PG_DISTANCE_DISTRIBUTOR);
	proto_send1b ('M', 0);
	return getsamples_next_branch (TAKE_SAMPLES, arm_pass_noted_position, no_more);
      }
}

/*
 * IDLE =start=>
 *  => FACE_DISTRIBUTOR
 *   do a goto angle to make the bot facing the distributor
 */
fsm_branch_t
getsamples__IDLE__start (void)
{
    /* Face the distributor */
    asserv_goto_angle (getsamples_data_.approach_angle);
    proto_send0 ('F');
    return getsamples_next (IDLE, start);
}

/*
 * MOVE_AWAY_FROM_DISTRIBUTOR =bot_move_failed=>
 *  => WAIT_AND_TRY_AGAIN
 *   ask to be wake up in a certain time
 */
fsm_branch_t
getsamples__MOVE_AWAY_FROM_DISTRIBUTOR__bot_move_failed (void)
{
    /* Post an event for the top FSM to be waked up later */
    main_getsamples_wait_cycle = GET_SAMPLES_MOVE_AWAY_TIMEOUT;
    return getsamples_next (MOVE_AWAY_FROM_DISTRIBUTOR, bot_move_failed);
}

/*
 * MOVE_AWAY_FROM_DISTRIBUTOR =bot_move_succeed=>
 *  => CLOSE_INPUT_HOLE
 *   close input hole
 *   setup a close timeout
 */
fsm_branch_t
getsamples__MOVE_AWAY_FROM_DISTRIBUTOR__bot_move_succeed (void)
{
    /* Move the arm to close the input hole */
    asserv_move_arm (BOT_ARM_MIN_TO_OPEN + BOT_ARM_THIRD_ROUND, BOT_ARM_SPEED);
    /* Post an event for the top FSM to be waked up later */
    main_getsamples_wait_cycle = GET_SAMPLES_ARM_TIMEOUT;
    proto_send0 ('C');
    return getsamples_next (MOVE_AWAY_FROM_DISTRIBUTOR, bot_move_succeed);
}

/*
 * MOVE_AWAY_FROM_DISTRIBUTOR =arm_pass_noted_position=>
 *  => MOVE_AWAY_FROM_DISTRIBUTOR
 *   prepare the classification of the taken sample
 */
fsm_branch_t
getsamples__MOVE_AWAY_FROM_DISTRIBUTOR__arm_pass_noted_position (void)
{
    /* Prepare the classification of the taken sample */
    getsamples_configure_classifier ();
    return getsamples_next (MOVE_AWAY_FROM_DISTRIBUTOR, arm_pass_noted_position);
}

/*
 * APPROACH_DISTRIBUTOR =bot_move_failed=>
 *  => APPROACH_DISTRIBUTOR
 *   try to move again
 */
fsm_branch_t
getsamples__APPROACH_DISTRIBUTOR__bot_move_failed (void)
{
    /* Approach the distributor */
    asserv_go_to_distributor ();
    return getsamples_next (APPROACH_DISTRIBUTOR, bot_move_failed);
}

/*
 * APPROACH_DISTRIBUTOR =bot_move_succeed=>
 *  => MOVE_BACKWARD_FROM_DISTRIBUTOR
 *   move a little bit backward from the distributor
 */
fsm_branch_t
getsamples__APPROACH_DISTRIBUTOR__bot_move_succeed (void)
{
    /* Move a little bit backward from the distributor */
    asserv_move_linearly (-GET_SAMPLES_MOVE_BACKWARD_DISTANCE);
    proto_send0 ('m');
    return getsamples_next (APPROACH_DISTRIBUTOR, bot_move_succeed);
}