summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/robospierre/clamp.c
blob: 1ee020e989852309397b0ff71f411116bbdf5729 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* clamp.c */
/* robospierre - Eurobot 2011 AI. {{{
 *
 * Copyright (C) 2011 Nicolas Schodet
 *
 * 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 "clamp.h"

#include "mimot.h"
#include "pwm.h"
#include "contact.h"
#include "bot.h"
#include "element.h"

#define FSM_NAME AI
#include "fsm.h"
#include "fsm_queue.h"

#include "logistic.h"

/*
 * There is two FSM in this file.
 *
 * The clamp FSM handles high level clamp behaviour, new elements, drop, and
 * gives orders to the clamp move FSM.
 *
 * The clamp move FSM only handle moving the clamp without load or moving an
 * element from a slot to another one.
 */

FSM_INIT

FSM_STATES (
	    /* Initial state, to be complete with full initialisation. */
	    CLAMP_START,
	    /* Returning to idle position. */
	    CLAMP_GOING_IDLE,
	    /* Waiting external events, clamp at middle level. */
	    CLAMP_IDLE,
	    /* Taking an element at bottom slots. */
	    CLAMP_TAKING_DOOR_CLOSING,
	    /* Moving elements around. */
	    CLAMP_MOVING_ELEMENT,

	    /* Waiting movement order. */
	    CLAMP_MOVE_IDLE,
	    /* Moving to a final or intermediary position. */
	    CLAMP_MOVE_ROUTING,
	    /* Moving to source slot. */
	    CLAMP_MOVE_SRC_ROUTING,
	    /* Closing the clamp once arrived at source. */
	    CLAMP_MOVE_SRC_CLAMP_CLOSING,
	    /* Opening door once clamp closed. */
	    CLAMP_MOVE_SRC_DOOR_OPENDING,
	    /* Moving to destination slot. */
	    CLAMP_MOVE_DST_ROUTING,
	    /* Closing door once arrived at destination. */
	    CLAMP_MOVE_DST_DOOR_CLOSING,
	    /* Opening the clamp once door closed. */
	    CLAMP_MOVE_DST_CLAMP_OPENING)

FSM_EVENTS (
	    /* Here for the moment, to be moved later. */
	    start,
	    /* New element inside bottom slot. */
	    clamp_new_element,
	    /* Order to move the clamp. */
	    clamp_move,
	    /* Clamp movement success. */
	    clamp_move_success,
	    /* Elevation and elevation motor success. */
	    clamp_elevation_rotation_success,
	    /* Elevation motor failure. */
	    clamp_elevation_failure,
	    /* Rotation motor failure. */
	    clamp_rotation_failure)

FSM_START_WITH (CLAMP_START)
FSM_START_WITH (CLAMP_MOVE_IDLE)

/** Clamp context. */
struct clamp_t
{
    /** Current position. */
    uint8_t pos_current;
    /** Requested position. */
    uint8_t pos_request;
    /** Element moving destination. */
    uint8_t moving_to;
    /** Position of a new element. */
    uint8_t pos_new;
    /** New element kind. */
    uint8_t new_element;
};

/** Global context. */
struct clamp_t clamp_global;
#define ctx clamp_global

/** Clamp positions. */
static const uint16_t clamp_pos[][2] = {
      { BOT_CLAMP_SLOT_FRONT_BOTTOM_ELEVATION_STEP,
	BOT_CLAMP_BAY_FRONT_ROTATION_STEP },
      { BOT_CLAMP_SLOT_FRONT_MIDDLE_ELEVATION_STEP,
	BOT_CLAMP_BAY_FRONT_ROTATION_STEP },
      { BOT_CLAMP_SLOT_FRONT_TOP_ELEVATION_STEP,
	BOT_CLAMP_BAY_FRONT_ROTATION_STEP },
      { BOT_CLAMP_SLOT_BACK_BOTTOM_ELEVATION_STEP,
	BOT_CLAMP_BAY_BACK_ROTATION_STEP },
      { BOT_CLAMP_SLOT_BACK_MIDDLE_ELEVATION_STEP,
	BOT_CLAMP_BAY_BACK_ROTATION_STEP },
      { BOT_CLAMP_SLOT_BACK_TOP_ELEVATION_STEP,
	BOT_CLAMP_BAY_BACK_ROTATION_STEP },
      { BOT_CLAMP_SLOT_SIDE_ELEVATION_STEP,
	BOT_CLAMP_BAY_SIDE_ROTATION_STEP },
      { BOT_CLAMP_BAY_FRONT_LEAVE_ELEVATION_STEP,
	BOT_CLAMP_BAY_FRONT_ROTATION_STEP },
      { BOT_CLAMP_BAY_BACK_LEAVE_ELEVATION_STEP,
	BOT_CLAMP_BAY_BACK_ROTATION_STEP },
      { BOT_CLAMP_BAY_SIDE_ENTER_LEAVE_ELEVATION_STEP,
	BOT_CLAMP_BAY_SIDE_ROTATION_STEP },
};

/** Slot doors. */
static const uint8_t clamp_slot_door[] = {
    BOT_PWM_DOOR_FRONT_BOTTOM,
    0xff,
    BOT_PWM_DOOR_FRONT_TOP,
    BOT_PWM_DOOR_BACK_BOTTOM,
    0xff,
    BOT_PWM_DOOR_BACK_TOP,
    0xff
};

void
clamp_move (uint8_t pos)
{
    if (pos != ctx.pos_current)
      {
	ctx.pos_request = pos;
	ctx.moving_to = CLAMP_POS_NB;
	FSM_HANDLE (AI, clamp_move);
      }
    else
	fsm_queue_post_event (FSM_EVENT (AI, clamp_move_success));
}

void
clamp_move_element (uint8_t from, uint8_t to)
{
    assert (from != to);
    ctx.pos_request = from;
    ctx.moving_to = to;
    FSM_HANDLE (AI, clamp_move);
}

void
clamp_new_element (uint8_t pos, uint8_t element)
{
    assert (pos == CLAMP_SLOT_FRONT_BOTTOM || pos == CLAMP_SLOT_BACK_BOTTOM);
    ctx.pos_new = pos;
    ctx.new_element = element;
    FSM_HANDLE (AI, clamp_new_element);
}

uint8_t
clamp_handle_event (void)
{
    if (FSM_CAN_HANDLE (AI, clamp_new_element))
      {
	/* XXX: temporary hack. */
	uint8_t element = contact_get_color () ? ELEMENT_PAWN : ELEMENT_KING;
	if (!IO_GET (CONTACT_FRONT_BOTTOM)
	    && !logistic_global.slots[CLAMP_SLOT_FRONT_BOTTOM])
	  {
	    clamp_new_element (CLAMP_SLOT_FRONT_BOTTOM, element);
	    return 1;
	  }
	if (!IO_GET (CONTACT_BACK_BOTTOM)
	    && !logistic_global.slots[CLAMP_SLOT_BACK_BOTTOM])
	  {
	    clamp_new_element (CLAMP_SLOT_BACK_BOTTOM, element);
	    return 1;
	  }
      }
    return 0;
}

/** Find next position and start motors. */
static void
clamp_route (void)
{
    uint8_t pos_new;
    uint8_t pos_current = ctx.pos_current;
    uint8_t pos_request = ctx.pos_request;
    /* Compute new position. */
    if (CLAMP_IS_SLOT_IN_FRONT_BAY (pos_current))
      {
	if (!CLAMP_IS_SLOT_IN_FRONT_BAY (pos_request))
	    pos_new = CLAMP_BAY_FRONT_LEAVE;
	else
	    pos_new = pos_request;
      }
    else if (CLAMP_IS_SLOT_IN_BACK_BAY (pos_current))
      {
	if (!CLAMP_IS_SLOT_IN_BACK_BAY (pos_request))
	    pos_new = CLAMP_BAY_BACK_LEAVE;
	else
	    pos_new = pos_request;
      }
    else if (pos_current == CLAMP_SLOT_SIDE)
      {
	pos_new = CLAMP_BAY_SIDE_ENTER_LEAVE;
      }
    else if (pos_current == CLAMP_BAY_FRONT_LEAVE)
      {
	if (pos_request == CLAMP_SLOT_SIDE)
	    pos_new = CLAMP_BAY_SIDE_ENTER_LEAVE;
	else
	    pos_new = CLAMP_SLOT_BACK_MIDDLE;
      }
    else if (pos_current == CLAMP_BAY_BACK_LEAVE)
      {
	if (pos_request == CLAMP_SLOT_SIDE)
	    pos_new = CLAMP_BAY_SIDE_ENTER_LEAVE;
	else
	    pos_new = CLAMP_SLOT_FRONT_MIDDLE;
      }
    else
      {
	assert (pos_current == CLAMP_BAY_SIDE_ENTER_LEAVE);
	if (pos_request == CLAMP_SLOT_SIDE)
	    pos_new = pos_request;
	else if (CLAMP_IS_SLOT_IN_FRONT_BAY (pos_request))
	    pos_new = CLAMP_SLOT_FRONT_MIDDLE;
	else
	    pos_new = CLAMP_SLOT_BACK_MIDDLE;
      }
    /* Run motors. */
    mimot_move_motor0_absolute (clamp_pos[pos_new][0],
				BOT_CLAMP_ELEVATION_SPEED);
    mimot_move_motor1_absolute (clamp_pos[pos_new][1],
				BOT_CLAMP_ROTATION_SPEED);
    /* Remember new position. */
    ctx.pos_current = pos_new;
}

/* CLAMP FSM */

FSM_TRANS (CLAMP_START, start, CLAMP_GOING_IDLE)
{
    clamp_move (CLAMP_SLOT_FRONT_MIDDLE);
    return FSM_NEXT (CLAMP_START, start);
}

FSM_TRANS (CLAMP_GOING_IDLE, clamp_move_success, CLAMP_IDLE)
{
    return FSM_NEXT (CLAMP_GOING_IDLE, clamp_move_success);
}

FSM_TRANS (CLAMP_IDLE, clamp_new_element, CLAMP_TAKING_DOOR_CLOSING)
{
    pwm_set_timed (clamp_slot_door[ctx.pos_new], BOT_PWM_DOOR_CLOSE);
    return FSM_NEXT (CLAMP_IDLE, clamp_new_element);
}

FSM_TRANS_TIMEOUT (CLAMP_TAKING_DOOR_CLOSING, BOT_PWM_DOOR_CLOSE_TIME,
		   move_element, CLAMP_MOVING_ELEMENT,
		   move_to_idle, CLAMP_GOING_IDLE,
		   done, CLAMP_IDLE)
{
    logistic_element_new (ctx.pos_new, ctx.new_element);
    if (logistic_global.moving_from != CLAMP_SLOT_NB)
      {
	clamp_move_element (logistic_global.moving_from,
			    logistic_global.moving_to);
	return FSM_NEXT_TIMEOUT (CLAMP_TAKING_DOOR_CLOSING, move_element);
      }
    else if (logistic_global.clamp_pos_idle != ctx.pos_current)
      {
	clamp_move (logistic_global.clamp_pos_idle);
	return FSM_NEXT_TIMEOUT (CLAMP_TAKING_DOOR_CLOSING, move_to_idle);
      }
    else
	return FSM_NEXT_TIMEOUT (CLAMP_TAKING_DOOR_CLOSING, done);
}

FSM_TRANS (CLAMP_MOVING_ELEMENT, clamp_move_success,
	   move_element, CLAMP_MOVING_ELEMENT,
	   move_to_idle, CLAMP_GOING_IDLE,
	   done, CLAMP_IDLE)
{
    logistic_element_move_done ();
    if (logistic_global.moving_from != CLAMP_SLOT_NB)
      {
	clamp_move_element (logistic_global.moving_from,
			    logistic_global.moving_to);
	return FSM_NEXT (CLAMP_MOVING_ELEMENT, clamp_move_success,
			 move_element);
      }
    else if (logistic_global.clamp_pos_idle != ctx.pos_current)
      {
	clamp_move (logistic_global.clamp_pos_idle);
	return FSM_NEXT (CLAMP_MOVING_ELEMENT, clamp_move_success,
			 move_to_idle);
      }
    else
	return FSM_NEXT (CLAMP_MOVING_ELEMENT, clamp_move_success,
			 done);
}

/* CLAMP_MOVE FSM */

FSM_TRANS (CLAMP_MOVE_IDLE, clamp_move,
	   move, CLAMP_MOVE_ROUTING,
	   move_element, CLAMP_MOVE_SRC_ROUTING,
	   move_element_here, CLAMP_MOVE_SRC_CLAMP_CLOSING)
{
    if (ctx.moving_to == CLAMP_POS_NB)
      {
	clamp_route ();
	return FSM_NEXT (CLAMP_MOVE_IDLE, clamp_move, move);
      }
    else
      {
	if (ctx.pos_current != ctx.pos_request)
	  {
	    clamp_route ();
	    return FSM_NEXT (CLAMP_MOVE_IDLE, clamp_move, move_element);
	  }
	else
	  {
	    pwm_set_timed (BOT_PWM_CLAMP, BOT_PWM_CLAMP_CLOSE);
	    return FSM_NEXT (CLAMP_MOVE_IDLE, clamp_move, move_element_here);
	  }
      }
}

FSM_TRANS (CLAMP_MOVE_ROUTING, clamp_elevation_rotation_success,
	   done, CLAMP_MOVE_IDLE,
	   next, CLAMP_MOVE_ROUTING)
{
    if (ctx.pos_current == ctx.pos_request)
      {
	fsm_queue_post_event (FSM_EVENT (AI, clamp_move_success));
	return FSM_NEXT (CLAMP_MOVE_ROUTING, clamp_elevation_rotation_success,
			 done);
      }
    else
      {
	clamp_route ();
	return FSM_NEXT (CLAMP_MOVE_ROUTING, clamp_elevation_rotation_success,
			 next);
      }
}

FSM_TRANS (CLAMP_MOVE_SRC_ROUTING, clamp_elevation_rotation_success,
	   done, CLAMP_MOVE_SRC_CLAMP_CLOSING,
	   next, CLAMP_MOVE_SRC_ROUTING)
{
    if (ctx.pos_current == ctx.pos_request)
      {
	pwm_set_timed (BOT_PWM_CLAMP, BOT_PWM_CLAMP_CLOSE);
	return FSM_NEXT (CLAMP_MOVE_SRC_ROUTING,
			 clamp_elevation_rotation_success, done);
      }
    else
      {
	clamp_route ();
	return FSM_NEXT (CLAMP_MOVE_SRC_ROUTING,
			 clamp_elevation_rotation_success, next);
      }
}

FSM_TRANS_TIMEOUT (CLAMP_MOVE_SRC_CLAMP_CLOSING, BOT_PWM_CLAMP_CLOSE_TIME,
		   open_door, CLAMP_MOVE_SRC_DOOR_OPENDING,
		   move, CLAMP_MOVE_DST_ROUTING)
{
    if (clamp_slot_door[ctx.pos_current] != 0xff)
      {
	pwm_set_timed (clamp_slot_door[ctx.pos_current], BOT_PWM_DOOR_OPEN);
	return FSM_NEXT_TIMEOUT (CLAMP_MOVE_SRC_CLAMP_CLOSING, open_door);
      }
    else
      {
	ctx.pos_request = ctx.moving_to;
	clamp_route ();
	return FSM_NEXT_TIMEOUT (CLAMP_MOVE_SRC_CLAMP_CLOSING, move);
      }
}

FSM_TRANS_TIMEOUT (CLAMP_MOVE_SRC_DOOR_OPENDING, BOT_PWM_DOOR_OPEN_TIME,
		   CLAMP_MOVE_DST_ROUTING)
{
    ctx.pos_request = ctx.moving_to;
    clamp_route ();
    return FSM_NEXT_TIMEOUT (CLAMP_MOVE_SRC_DOOR_OPENDING);
}

FSM_TRANS (CLAMP_MOVE_DST_ROUTING, clamp_elevation_rotation_success,
	   done_close_door, CLAMP_MOVE_DST_DOOR_CLOSING,
	   done_open_clamp, CLAMP_MOVE_DST_CLAMP_OPENING,
	   next, CLAMP_MOVE_DST_ROUTING)
{
    if (ctx.pos_current == ctx.pos_request)
      {
	if (clamp_slot_door[ctx.pos_current] != 0xff)
	  {
	    pwm_set_timed (clamp_slot_door[ctx.pos_current],
			   BOT_PWM_DOOR_CLOSE);
	    return FSM_NEXT (CLAMP_MOVE_DST_ROUTING,
			     clamp_elevation_rotation_success,
			     done_close_door);
	  }
	else
	  {
	    pwm_set_timed (BOT_PWM_CLAMP, BOT_PWM_CLAMP_OPEN);
	    return FSM_NEXT (CLAMP_MOVE_DST_ROUTING,
			     clamp_elevation_rotation_success,
			     done_open_clamp);
	  }
      }
    else
      {
	clamp_route ();
	return FSM_NEXT (CLAMP_MOVE_DST_ROUTING,
			 clamp_elevation_rotation_success, next);
      }
}

FSM_TRANS_TIMEOUT (CLAMP_MOVE_DST_DOOR_CLOSING, BOT_PWM_DOOR_CLOSE_TIME,
		   CLAMP_MOVE_DST_CLAMP_OPENING)
{
    pwm_set_timed (BOT_PWM_CLAMP, BOT_PWM_CLAMP_OPEN);
    return FSM_NEXT_TIMEOUT (CLAMP_MOVE_DST_DOOR_CLOSING);
}

FSM_TRANS_TIMEOUT (CLAMP_MOVE_DST_CLAMP_OPENING, BOT_PWM_CLAMP_OPEN_TIME,
		   CLAMP_MOVE_IDLE)
{
    fsm_queue_post_event (FSM_EVENT (AI, clamp_move_success));
    return FSM_NEXT_TIMEOUT (CLAMP_MOVE_DST_CLAMP_OPENING);
}