summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/robospierre/logistic.c
blob: d3f0293f287becfe95a5ad5df15f00491445b195 (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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* logistic.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 "logistic.h"

#include "clamp.h"
#include "defs.h"

#include "contact.h"
#include "io.h"

#include "debug.host.h"

/** Handle elements stored inside the robot. */

/** Global context. */
struct logistic_t logistic_global;
#define ctx logistic_global

inline void
logistic_debug_dump (void)
{
#ifdef HOST
    uint8_t i;
    static const char *names[][CLAMP_SLOT_NB] = {
	  { "f1", "f2", "f3", "b1", "b2", "b3", "s1" },
	  { "F1", "F2", "F3", "B1", "B2", "B3", "S1" }
    };
    static const char *names_dir[] = { "--", "<-", "->" };
    DPRINTF ("%s", names_dir[ctx.collect_direction]);
    for (i = 0; i < CLAMP_SLOT_NB; i++)
      {
	DPRINTF (" %s", ctx.slots[i]
		 ? names[ELEMENT_IS_HEAD (ctx.slots[i]) ? 1 : 0][i]
		 : "__");
      }
    if (ctx.moving_from != CLAMP_SLOT_NB)
      {
	DPRINTF ("    %s => %s", names[0][ctx.moving_from], names[0][ctx.moving_to]);
      }
    DPRINTF ("\n");
    DPRINTF ("construct_possible: %u\n", ctx.construct_possible);
    DPRINTF ("prepare: %u\n", ctx.prepare);
    DPRINTF ("ready: %u\n", ctx.ready);
    DPRINTF ("need_prepare: %u\n", ctx.need_prepare);
#endif
}

/** Return 1 if location corresponds to element. */
inline uint8_t
logistic_case_test (uint8_t loc, uint8_t e)
{
    if (e == LOG_a && ctx.slots[loc])
	return 1;
    if (e == LOG__)
	return 1;
    if ((e == LOG_e || e == LOG_D) && !ctx.slots[loc])
	return 1;
    if ((e == LOG_P || e == LOG_p) && ctx.slots[loc] &&
	!ELEMENT_IS_HEAD (ctx.slots[loc]))
	return 1;
    if ((e == LOG_H || e == LOG_h) && ELEMENT_IS_HEAD (ctx.slots[loc]))
	return 1;
    if (e == LOG_t && ctx.slots[loc] == ELEMENT_TOWER)
	return 1;
    return 0;
}

static uint8_t
logistic_case (uint8_t e1, uint8_t e2, uint8_t e3, uint8_t e4, uint8_t e5,
	       uint8_t e6, uint8_t e7, uint8_t new_dir, uint8_t ready,
	       uint8_t check_symetric)
{
    /* Define direction bay and opposed bay. */
    uint8_t dir_bay, opp_bay;
    if (ctx.collect_direction == DIRECTION_FORWARD)
      {
	dir_bay = CLAMP_SLOT_FRONT_BOTTOM;
	opp_bay = CLAMP_SLOT_BACK_BOTTOM;
      }
    else
      {
	dir_bay = CLAMP_SLOT_BACK_BOTTOM;
	opp_bay = CLAMP_SLOT_FRONT_BOTTOM;
      }
    /*if (check_symetric)
      {
	uint8_t tmp = dir_bay;
	dir_bay = opp_bay;
	opp_bay = tmp;
      }*/

    /* Emplacement of elements. */
    uint8_t
	e1_loc = dir_bay + 2,
	e3_loc = dir_bay + 1,
	e6_loc = dir_bay,
	e4_loc = CLAMP_SLOT_SIDE,
	e2_loc = opp_bay + 2,
	e5_loc = opp_bay + 1,
	e7_loc = opp_bay;

    /* Check elements are here. */
    if (!(logistic_case_test (e1_loc, e1) &&
	  logistic_case_test (e2_loc, e2) &&
	  logistic_case_test (e3_loc, e3) &&
	  logistic_case_test (e4_loc, e4) &&
	  logistic_case_test (e5_loc, e5) &&
	  logistic_case_test (e6_loc, e6) &&
	  logistic_case_test (e7_loc, e7)))
      {
	/* Check mirror. */
	/*if (!check_symetric)
	    logistic_case (e1, e2, e3, e4, e5, e6, e7, new_dir, ready, 1);*/
	return 0;
      }

    /* Find source/destination if we have to make a move. */
    /* Find source. */
    uint8_t src = CLAMP_SLOT_NB, dst = CLAMP_SLOT_NB;
    if (e1 == LOG_P ||e1 == LOG_H)
	src = e1_loc;
    else if (e3 == LOG_P ||e3 == LOG_H)
	src = e3_loc;
    else if (e6 == LOG_P ||e6 == LOG_H)
	src = e6_loc;
    else if (e4 == LOG_P ||e4 == LOG_H)
	src = e4_loc;
    else if (e2 == LOG_P ||e2 == LOG_H)
	src = e2_loc;
    else if (e5 == LOG_P ||e5 == LOG_H)
	src = e5_loc;
    else if (e7 == LOG_P ||e7 == LOG_H)
	src = e7_loc;

    /* Find destination. */
    if (e1 == LOG_D)
	dst = e1_loc;
    else if (e3 == LOG_D)
	dst = e3_loc;
    else if (e6 == LOG_D)
	dst = e6_loc;
    else if (e4 == LOG_D)
	dst = e4_loc;
    else if (e2 == LOG_D)
	dst = e2_loc;
    else if (e5 == LOG_D)
	dst = e5_loc;
    else if (e7 == LOG_D)
	dst = e7_loc;
    
    /* We are making a move. */
    if (src != CLAMP_SLOT_NB && dst != CLAMP_SLOT_NB)
      {
	if (ctx.slots[src] && !ctx.slots[dst])
	{
	    ctx.moving_from = src;
	    ctx.moving_to = dst;
	    ctx.ready = 0;
	}
      }

    /* Set collect direction. */
    /* LEFT means we keep the same side, RIGHT means we put the opposed side. */
    if (new_dir == LOG_DIR_RIGHT)
      {
	if (ctx.collect_direction == DIRECTION_FORWARD)
	    ctx.collect_direction = DIRECTION_BACKWARD;
	else
	    ctx.collect_direction = DIRECTION_FORWARD;
      }

    /* Don't touch clamp's idle position if broken. */
    if (ctx.prepare != 3)
      {
	/* Set clamp position idle. */
	if (ctx.collect_direction == DIRECTION_FORWARD)
	    ctx.clamp_pos_idle = CLAMP_SLOT_FRONT_MIDDLE;
	else
	    ctx.clamp_pos_idle = CLAMP_SLOT_BACK_MIDDLE;
      }
    /* Set ready */
    ctx.ready = ready;
    return 1;
}

static void
logistic_update_construct_possible ()
{
    uint8_t pawn = 0, head = 0, i;
    /* Check for tower. */
    if (ctx.slots[CLAMP_SLOT_FRONT_BOTTOM] ==  ELEMENT_TOWER ||
	ctx.slots[CLAMP_SLOT_BACK_BOTTOM] == ELEMENT_TOWER)
      {
	ctx.construct_possible = 1;
	return;
      }

    for (i = CLAMP_SLOT_FRONT_BOTTOM; i < CLAMP_SLOT_NB; i++)
      {
	    if (ELEMENT_IS_HEAD (ctx.slots[i]))
		head++;
	    else if (ctx.slots[i])
		pawn++;
	    if (head >= 1 && pawn >= 2)
	    {
	      ctx.construct_possible = 1;
	      return;
	    }
      }
    if (pawn || head)
	ctx.construct_possible = 2;
    else
	ctx.construct_possible = 0;
}

static void
logistic_update_need_prepare ()
{
    uint8_t i, head = 0, pawn = 0;
    for (i = CLAMP_SLOT_FRONT_BOTTOM; i < CLAMP_SLOT_NB; i++)
      {
	    if (ELEMENT_IS_HEAD (ctx.slots[i]))
		head++;
	    else if (ctx.slots[i])
		pawn++;
      }
    if ((head == 1 && pawn == 3) ||
	head == 2 || pawn >= 4)
	ctx.need_prepare = 1;
    else
	ctx.need_prepare = 0;

    /* Define direction bay and opposed bay. */
    uint8_t dir_bay, opp_bay;
    if (ctx.collect_direction == DIRECTION_FORWARD)
      {
	dir_bay = CLAMP_SLOT_FRONT_BOTTOM;
	opp_bay = CLAMP_SLOT_BACK_BOTTOM;
      }
    else
      {
	dir_bay = CLAMP_SLOT_BACK_BOTTOM;
	opp_bay = CLAMP_SLOT_FRONT_BOTTOM;
      }
    /* If a head appear at the back (?) */
    if (ELEMENT_IS_HEAD (ctx.slots[opp_bay]))
	ctx.need_prepare = 1;

    /* We founded a tower ! */
    if (ctx.slots[CLAMP_SLOT_FRONT_BOTTOM] == ELEMENT_TOWER ||
	ctx.slots[CLAMP_SLOT_FRONT_BOTTOM] == ELEMENT_TOWER)
	ctx.need_prepare = 1;
}

static void
logisitic_make_broken ()
{
    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   a,      _, RIGHT, 1);

    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   _,      a, LEFT, 1);
}

static void
logistic_make_tower ()
{
    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   _,      t, LEFT, 1);

    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   t,      _, RIGHT, 1);

    LOGISTIC_CASE (D,      _,
		   e,  _,  _,
		   H,      _, RIGHT, 0);

    LOGISTIC_CASE (_,      h,
		   _,  _,  p,
		   _,      p, LEFT, 1);

    LOGISTIC_CASE (P,      h,
		   e,  _,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (_,      h,
		   e,  _,  e,
		   P,      D, LEFT, 0);

    LOGISTIC_CASE (_,      h,
		   _,  P,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (P,      h,
		   e,  _,  D,
		   _,      p, LEFT, 0);

    LOGISTIC_CASE (_,      h,
		   e,  _,  D,
		   P,      p, LEFT, 0);

    LOGISTIC_CASE (_,      h,
		   _,  P,  D,
		   _,      p, LEFT, 0);
}

static void
logistic_make_unload ()
{
    /* Making a small tower. Move head when having pawn. */
    LOGISTIC_CASE (D,      _,
		   e,  p,  _,
		   H,      _, RIGHT, 0);

    LOGISTIC_CASE (D,      p,
		   e,  e,  e,
		   H,      _, RIGHT, 0);

    LOGISTIC_CASE (D,      _,
		   e,  e,  e,
		   H,      p, RIGHT, 0);

    /* Making a small tower. Move pawn under head. */
    LOGISTIC_CASE (_,      h,
		   _,  P,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (P,      h,
		   e,  e,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (_,      h,
		   e,  e,  e,
		   P,      D, LEFT, 0);

    /* Making a small tower. Finally move head on pawn. */
    LOGISTIC_CASE (_,      H,
		   _,  _,  D,
		   _,      p, LEFT, 0);

    LOGISTIC_CASE (_,      _,
		   _,  _,  h,
		   _,      p, LEFT, 1);

    /* Having any element. */
    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   _,      a, LEFT, 1);

    LOGISTIC_CASE (_,      _,
		   _,  _,  _,
		   a,      _, RIGHT, 1);

    LOGISTIC_CASE (_,      a,
		   _,  _,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (a,      _,
		   e,  _,  _,
		   D,      _, RIGHT, 0);

    LOGISTIC_CASE (_,      _,
		   _,  P,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (_,      _,
		   e,  P,  _,
		   D,      _, RIGHT, 0);
}

static void
logisitic_make_switches ()
{
    LOGISTIC_CASE (_,      P,
		   _,  D,  e,
		   _,      _, LEFT, 0);

    LOGISTIC_CASE (P,      _,
		   e,  D,  _,
		   _,      _, LEFT, 0);

    LOGISTIC_CASE (_,      _,
		   e,  D,  _,
		   P,      _, LEFT, 0);

    LOGISTIC_CASE (_,      _,
		   _,  D,  e,
		   _,      P, LEFT, 0);

    LOGISTIC_CASE (_,      D,
		   e,  p,  e,
		   P,      _, LEFT, 0);

    LOGISTIC_CASE (_,      a,
		   e,  p,  e,
		   P,      D, LEFT, 0);
    
    LOGISTIC_CASE (D,      _,
		   e,  _,  _,
		   H,      _, RIGHT, 0);

    LOGISTIC_CASE (h,      P,
		   e,  _,  e,
		   D,      _, RIGHT, 0);

    LOGISTIC_CASE (h,      e,
		   e,  _,  e,
		   D,      P, RIGHT, 0);

    LOGISTIC_CASE (P,      h,
		   e,  _,  e,
		   _,      D, LEFT, 0);

    LOGISTIC_CASE (e,      h,
		   e,  _,  e,
		   P,      D, LEFT, 0);
}

void
logistic_decision (void)
{
    /* Reset. */
    ctx.moving_from = CLAMP_SLOT_NB;
    ctx.moving_to = CLAMP_SLOT_NB;
    ctx.construct_possible = 1;
    ctx.ready = 0;
    ctx.need_prepare = 0;

    /* Update context. */
    logistic_update_construct_possible ();
    /* Update if a something is possible. */
    logistic_update_need_prepare ();

    /* Broken clamp. */
    if (ctx.construct_possible && ctx.prepare == 3)
      {
	DPRINTF ("\nlogisitic_make_broken\n");
	logisitic_make_broken ();
      }
    /* Prepare tower. */
    else if (ctx.construct_possible == 1 && ctx.prepare != 0)
      {

	DPRINTF ("\nlogisitic_make_tower\n");
	logistic_make_tower ();
      }
    /* Need to unload. */
    else if (ctx.construct_possible == 2 && ctx.prepare == 2)
      {
	DPRINTF ("\nlogisitic_make_unload\n");
	logistic_make_unload ();
      }
    /* Internal switches. */
    else
      {
	DPRINTF ("\nlogisitic_make_switches\n");
	logisitic_make_switches ();
      }

    logistic_debug_dump ();
    return;
}

void
logistic_init (void)
{
    uint8_t i;
    for (i = 0; i < CLAMP_SLOT_NB; i++)
	ctx.slots[i] = 0;
    ctx.moving_from = ctx.moving_to = CLAMP_SLOT_NB;
    ctx.collect_direction = DIRECTION_FORWARD;
    ctx.clamp_pos_idle = ctx.collect_direction == DIRECTION_FORWARD
	? CLAMP_SLOT_FRONT_MIDDLE : CLAMP_SLOT_BACK_MIDDLE;
    ctx.construct_possible = 0;
    ctx.prepare = 1;
    ctx.ready = 0;
    ctx.need_prepare = 0;
}

void
logistic_update (void)
{
    uint8_t side_now = !IO_GET (CONTACT_SIDE);
    /* Filter side contact. */
    if (side_now)
      {
	ctx.side_filter = 0;
	ctx.side_state = 1;
      }
    else if (ctx.side_filter++ == 2 * 250)
      {
	ctx.side_state = 0;
	ctx.side_filter = 0;
      }
    /* Side slot element can be lost. */
    if (ctx.moving_from != CLAMP_SLOT_SIDE && !ctx.side_state)
	ctx.slots[CLAMP_SLOT_SIDE] = 0;
}

void
logistic_element_new (uint8_t pos, uint8_t element_type)
{
    assert (pos < CLAMP_SLOT_NB);
    assert (!ctx.slots[pos]);
    ctx.slots[pos] = element_type;
    logistic_decision ();
}

void
logistic_element_change (uint8_t pos, uint8_t element_type)
{
    assert (pos < CLAMP_SLOT_NB);
    ctx.slots[pos] = element_type;
    logistic_decision ();
}

void
logistic_element_move_done (void)
{
    assert (!ctx.slots[ctx.moving_to] || ctx.moving_to == ctx.moving_from);
    ctx.slots[ctx.moving_to] = ctx.slots[ctx.moving_from];
    ctx.slots[ctx.moving_from] = 0;
    ctx.moving_from = ctx.moving_to = CLAMP_SLOT_NB;
    logistic_decision ();
}

void
logistic_drop (uint8_t direction)
{
    uint8_t bay = direction == DIRECTION_FORWARD
	? CLAMP_SLOT_BACK_BOTTOM : CLAMP_SLOT_FRONT_BOTTOM;
    uint8_t i;
    for (i = bay; i < bay + 3; i++)
	ctx.slots[i] = 0;
    logistic_decision ();
}

uint8_t
logistic_drop_element_type (uint8_t direction)
{
    uint8_t bay = direction == DIRECTION_FORWARD
	? CLAMP_SLOT_BACK_BOTTOM : CLAMP_SLOT_FRONT_BOTTOM;
    uint8_t nb = 0;
    uint8_t element_type = ELEMENT_NONE;
    uint8_t i;
    for (i = bay; i < bay + 3; i++)
      {
	if (ctx.slots[i])
	  {
	    nb++;
	    element_type = ctx.slots[i];
	  }
      }
    if (ELEMENT_IS_HEAD (element_type))
	return ELEMENT_TOWER;
    else if (nb > 1)
	return ELEMENT_TOWER;
    else
	return ELEMENT_PAWN;
}

void
logistic_dump (uint8_t direction, uint8_t drop_top)
{
    /* Drop. */
    uint8_t i;
    uint8_t bay = direction == DIRECTION_FORWARD
	? CLAMP_SLOT_FRONT_BOTTOM : CLAMP_SLOT_BACK_BOTTOM;
    for (i = bay; i < bay + 2 + drop_top; i++)
	ctx.slots[i] = 0;
}

static uint8_t
logistic_slot_clear (uint8_t slot)
{
    if (CLAMP_IS_SLOT_IN_FRONT_BAY (slot))
      {
	if (ctx.slots[CLAMP_SLOT_FRONT_MIDDLE])
	    return 0;
	uint8_t middle_type = ctx.slots[CLAMP_SLOT_FRONT_BOTTOM];
	if (ELEMENT_IS_HEAD (middle_type) || middle_type == ELEMENT_TOWER)
	    return 0;
      }
    else if (CLAMP_IS_SLOT_IN_BACK_BAY (slot))
      {
	if (ctx.slots[CLAMP_SLOT_BACK_MIDDLE])
	    return 0;
	uint8_t middle_type = ctx.slots[CLAMP_SLOT_BACK_BOTTOM];
	if (ELEMENT_IS_HEAD (middle_type) || middle_type == ELEMENT_TOWER)
	    return 0;
      }
    return 1;
}

uint8_t
logistic_path_clear (uint8_t slot1, uint8_t slot2)
{
    return logistic_slot_clear (slot1) && logistic_slot_clear (slot2);
}