summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/cylinder_cb.c
blob: e29b09e2f595ae936598b8c3763860e91a5f6d1d (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* cylinder_cb.c - cylinder FSM callbacks. */
/*  {{{
 *
 * Copyright (C) 2009 Nicolas Haller
 *
 * 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 "cylinder_cb.h"
#include "asserv.h"
#include "cylinder.h"
#include "filterbridge.h"
#include "elevator.h"
#include "top.h"

/* locales variables */
/* is there a puck on pos 2 or 3 */
uint8_t puck_on_cylinder = 0;
/* is the cylinder are in of_offset mode */
uint8_t of_offset_enabled = 0;

/*
 * IDLE =start=>
 *  => WAIT_FOR_JACK_IN
 *   we wait the jack before moving anything
 */
fsm_branch_t
cylinder__IDLE__start (void)
{
    return cylinder_next (IDLE, start);
}

/*
 * WAIT_FOR_JACK_IN =jack_inserted_into_bot=>
 *  => RESET_POS
 *   we init the cylinder position
 */
fsm_branch_t
cylinder__WAIT_FOR_JACK_IN__jack_inserted_into_bot (void)
{
    asserv_arm_zero_position();
    return cylinder_next (WAIT_FOR_JACK_IN, jack_inserted_into_bot);
}

/*
 * RESET_POS =move_done=>
 *  => INIT_POS
 *   move the cylinder to open it
 */
fsm_branch_t
cylinder__RESET_POS__move_done (void)
{
    asserv_move_arm(CYLINDER_OFFSET,
		    ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (RESET_POS, move_done);
}

/*
 * INIT_POS =move_done=>
 *  => WAIT_A_PUCK
 *   the cylinder is ready to get pucks
 */
fsm_branch_t
cylinder__INIT_POS__move_done (void)
{
    return cylinder_next (INIT_POS, move_done);
}

/*
 * WAIT_A_PUCK =new_puck=>
 *  => WAIT_BRIDGE_READY
 *   look if the bridge is ready before move
 */
fsm_branch_t
cylinder__WAIT_A_PUCK__new_puck (void)
{
    return cylinder_next (WAIT_A_PUCK, new_puck);
}

/*
 * WAIT_A_PUCK =close_order=>
 *  => TURN_PLUS_1_CLOSE
 *   we close cylinder as requested
 */
fsm_branch_t
cylinder__WAIT_A_PUCK__close_order (void)
{
    asserv_move_arm(1*60*ASSERV_ARM_STEP_BY_DEGREE,
		    ASSERV_ARM_SPEED_DEFAULT);
    /*TODO check new puck */
    return cylinder_next (WAIT_A_PUCK, close_order);
}

/*
 * WAIT_A_PUCK =flush_order=>
 *  => WAIT_BRIDGE_READY_FLUSH
 *   flush all pucks to the bridge
 */
fsm_branch_t
cylinder__WAIT_A_PUCK__flush_order (void)
{
    return cylinder_next (WAIT_A_PUCK, flush_order);
}

/*
 * WAIT_A_PUCK =distrib_fucked=>
 *  => WAIT_BRIDGE_READY_DISTRIB
 *   we verify bridge
 */
fsm_branch_t
cylinder__WAIT_A_PUCK__distrib_fucked (void)
{
    return cylinder_next (WAIT_A_PUCK, distrib_fucked);
}

/*
 * WAIT_BRIDGE_READY =bridge_ready=>
 *  => TURN_PLUS_1_AND_OFO
 *   open the cylinder with the puck or not.
 */
fsm_branch_t
cylinder__WAIT_BRIDGE_READY__bridge_ready (void)
{
    of_offset_enabled = 1;
    asserv_move_arm((1+CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
		    ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (WAIT_BRIDGE_READY, bridge_ready);
}

/*
 * TURN_PLUS_1_AND_OFO =move_done=>
 * bot_not_full => TURN_PLUS_1_AND_MINUS_OFO
 *   open the cylinder to wait a new puck
 * bot_full => WAIT_BOT_NOT_FULL
 *   bot full, waiting for pucks teleportation
 */
fsm_branch_t
cylinder__TURN_PLUS_1_AND_OFO__move_done (void)
{
    /* we verify if we drop a puck to the bridge */
    if(puck_on_cylinder)
      {
	if(cylinder_nb_puck) --cylinder_nb_puck;
	++fb_nb_puck;
      }
    /* We probe the OF to see if we have a new puck */
    puck_on_cylinder = asserv_arm_of_status();
    if(puck_on_cylinder)
      {
	++top_total_puck_taken;
	++top_puck_inside_bot;
	++cylinder_nb_puck;
      }
    if(top_puck_inside_bot < 4)
      {
	of_offset_enabled = 0;
	asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
	return cylinder_next_branch (TURN_PLUS_1_AND_OFO, move_done, bot_not_full);
      }
    return cylinder_next_branch (TURN_PLUS_1_AND_OFO, move_done, bot_full);
}

/*
 * TURN_PLUS_1_AND_MINUS_OFO =move_done=>
 *  => WAIT_A_PUCK
 *   ready for other pucks
 */
fsm_branch_t
cylinder__TURN_PLUS_1_AND_MINUS_OFO__move_done (void)
{
    return cylinder_next (TURN_PLUS_1_AND_MINUS_OFO, move_done);
}

/*
 * WAIT_BOT_NOT_FULL =bot_not_full=>
 *  => WAIT_CLEAR_ORDER
 *   the bot is not full, we go testing the other close condition
 */
fsm_branch_t
cylinder__WAIT_BOT_NOT_FULL__bot_not_full (void)
{
    cylinder_distributor_fucked = 0;
    return cylinder_next (WAIT_BOT_NOT_FULL, bot_not_full);
}

/*
 * WAIT_BOT_NOT_FULL =flush_order=>
 *  => TURN_PLUS_1_FLUSH
 *   flush order received, go open the cylinder
 */
fsm_branch_t
cylinder__WAIT_BOT_NOT_FULL__flush_order (void)
{
    cylinder_distributor_fucked = 0;
    if(of_offset_enabled)
      {
	asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
	of_offset_enabled = 0;
      }
    else
	asserv_move_arm(1*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (WAIT_BOT_NOT_FULL, flush_order);
}

/*
 * WAIT_CLEAR_ORDER =no_close_order=>
 *  => TURN_PLUS_1_OPEN
 *   no close order, we reopen cylinder to get other pucks
 */
fsm_branch_t
cylinder__WAIT_CLEAR_ORDER__no_close_order (void)
{
    if(of_offset_enabled)
      {
	asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
	of_offset_enabled = 0;
      }
    else
	asserv_move_arm(1*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (WAIT_CLEAR_ORDER, no_close_order);
}

/*
 * WAIT_CLEAR_ORDER =flush_order=>
 *  => TURN_PLUS_1_FLUSH
 *   flush order received, go open the cylinder
 */
fsm_branch_t
cylinder__WAIT_CLEAR_ORDER__flush_order (void)
{
    if(of_offset_enabled)
      {
	asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
	of_offset_enabled = 0;
      }
    else
	asserv_move_arm(1*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (WAIT_CLEAR_ORDER, flush_order);
}

/*
 * TURN_PLUS_1_OPEN =move_done=>
 *  => WAIT_A_PUCK
 *   cylinder ready to get other pucks
 */
fsm_branch_t
cylinder__TURN_PLUS_1_OPEN__move_done (void)
{
    return cylinder_next (TURN_PLUS_1_OPEN, move_done);
}

/*
 * TURN_PLUS_1_FLUSH =move_done=>
 *  => WAIT_BRIDGE_READY_FLUSH
 *   we wait the bridge before moving
 */
fsm_branch_t
cylinder__TURN_PLUS_1_FLUSH__move_done (void)
{
    return cylinder_next (TURN_PLUS_1_FLUSH, move_done);
}

/*
 * WAIT_BRIDGE_READY_FLUSH =bridge_ready=>
 *  => TURN_PLUS_3_FLUSH
 *   bridge is ready, flush gordon
 */
fsm_branch_t
cylinder__WAIT_BRIDGE_READY_FLUSH__bridge_ready (void)
{
    asserv_move_arm(3*60*ASSERV_ARM_STEP_BY_DEGREE,
		    ASSERV_ARM_SPEED_DEFAULT);
    return cylinder_next (WAIT_BRIDGE_READY_FLUSH, bridge_ready);
}

/*
 * TURN_PLUS_3_FLUSH =move_done=>
 *  => WAIT_BOT_NOT_FULL
 *   cylinder flushed, we test the 2 close conditions before reopen cylinder
 */
fsm_branch_t
cylinder__TURN_PLUS_3_FLUSH__move_done (void)
{
    cylinder_flush_order = 0;
    if(puck_on_cylinder)
      {
	if(cylinder_nb_puck) --cylinder_nb_puck;
	++fb_nb_puck;
	puck_on_cylinder = 0;
      }
    return cylinder_next (TURN_PLUS_3_FLUSH, move_done);
}

/*
 * TURN_PLUS_1_CLOSE =move_done=>
 *  => WAIT_BOT_NOT_FULL
 *   close order executed, test the 2 close conditions before reopen cylinder
 */
fsm_branch_t
cylinder__TURN_PLUS_1_CLOSE__move_done (void)
{
    if(puck_on_cylinder)
      {
	if(cylinder_nb_puck) --cylinder_nb_puck;
	++fb_nb_puck;
	puck_on_cylinder = 0;
      }
    return cylinder_next (TURN_PLUS_1_CLOSE, move_done);
}

/*
 * WAIT_BRIDGE_READY_DISTRIB =bridge_ready=>
 *  => TURN_PLUS_1_AND_OFO_DISTRIB
 *   we turn to check the of
 */
fsm_branch_t
cylinder__WAIT_BRIDGE_READY_DISTRIB__bridge_ready (void)
{
    asserv_move_arm((1+CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
		    ASSERV_ARM_SPEED_DEFAULT);
    of_offset_enabled = 1;
    return cylinder_next (WAIT_BRIDGE_READY_DISTRIB, bridge_ready);
}

/*
 * TURN_PLUS_1_AND_OFO_DISTRIB =move_done=>
 *  => PROBE_OF
 *   We probe the puck
 */
fsm_branch_t
cylinder__TURN_PLUS_1_AND_OFO_DISTRIB__move_done (void)
{
    if(puck_on_cylinder)
      {
	if(cylinder_nb_puck) --cylinder_nb_puck;
	++fb_nb_puck;
	puck_on_cylinder = 0;
      }
    return cylinder_next (TURN_PLUS_1_AND_OFO_DISTRIB, move_done);
}

/*
 * TURN_PLUS_1_AND_MINUS_OFO_DISTRIB =move_done=>
 *  => WAIT_A_PUCK
 *   bridge empty, return to normal mode
 */
fsm_branch_t
cylinder__TURN_PLUS_1_AND_MINUS_OFO_DISTRIB__move_done (void)
{
    return cylinder_next (TURN_PLUS_1_AND_MINUS_OFO_DISTRIB, move_done);
}

/*
 * TURN_PLUS_1_AND_MINUS_OFO_DISTRIB_LOOP =move_done=>
 *  => WAIT_BRIDGE_READY_DISTRIB
 *   ready to get another puck
 */
fsm_branch_t
cylinder__TURN_PLUS_1_AND_MINUS_OFO_DISTRIB_LOOP__move_done (void)
{
    return cylinder_next (TURN_PLUS_1_AND_MINUS_OFO_DISTRIB_LOOP, move_done);
}

/*
 * PROBE_OF =of_puck=>
 * bot_full => WAIT_BOT_NOT_FULL
 *   return to normal mode with a bot full of puck
 * bot_not_full => TURN_PLUS_1_AND_MINUS_OFO_DISTRIB_LOOP
 *   we get a puck and we are hungry yet, go eat an another puck
 */
fsm_branch_t
cylinder__PROBE_OF__of_puck (void)
{
    /* we have a new puck on cylinder */
    puck_on_cylinder =1;
    ++cylinder_nb_puck;
    ++top_total_puck_taken;
    ++top_puck_inside_bot;
    if(top_puck_inside_bot < 4)
      {
	asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
			ASSERV_ARM_SPEED_DEFAULT);
	of_offset_enabled = 0;
	return cylinder_next_branch (PROBE_OF, of_puck, bot_not_full);
      }
    return cylinder_next_branch (PROBE_OF, of_puck, bot_full);
}

/*
 * PROBE_OF =of_no_puck=>
 *  => TURN_PLUS_1_AND_MINUS_OFO_DISTRIB
 *   Distributor empty, go elsewhere
 */
fsm_branch_t
cylinder__PROBE_OF__of_no_puck (void)
{
    cylinder_distributor_empty = 1;
    asserv_move_arm((1-CYLINDER_OF_OFFSET)*60*ASSERV_ARM_STEP_BY_DEGREE,
		    ASSERV_ARM_SPEED_DEFAULT);
    of_offset_enabled = 0;
    return cylinder_next (PROBE_OF, of_no_puck);
}