summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/robospierre/logistic.c
blob: 534f1c469c78c94ce9f46b39aa08d848583f4f0a (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
/* 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 "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");
#endif
}

/** Examine current state and take a decision. */
static void
logistic_decision (void)
{
    uint8_t i;
    /* If currently moving, do not take decision. */
    if (ctx.moving_from != CLAMP_SLOT_NB)
	return;
    /* Determine collect_direction. */
    uint8_t front_head = 0, back_head = 0,
	    front_element = 0, back_element = 0;
    uint8_t collect_direction;
    for (i = CLAMP_SLOT_FRONT_BOTTOM; i <= CLAMP_SLOT_FRONT_TOP; i++)
      {
	if (ctx.slots[i])
	  {
	    front_element++;
	    if (ELEMENT_IS_HEAD (ctx.slots[i]))
		front_head++;
	  }
      }
    for (i = CLAMP_SLOT_BACK_BOTTOM; i <= CLAMP_SLOT_BACK_TOP; i++)
      {
	if (ctx.slots[i])
	  {
	    back_element++;
	    if (ELEMENT_IS_HEAD (ctx.slots[i]))
		back_head++;
	  }
      }
    if (front_head < back_head)
	collect_direction = DIRECTION_FORWARD;
    else if (front_head > back_head)
	collect_direction = DIRECTION_BACKWARD;
    else if (front_head)
      {
	if (front_element < back_element)
	    collect_direction = DIRECTION_FORWARD;
	else if (front_element > back_element)
	    collect_direction = DIRECTION_BACKWARD;
	else
	    collect_direction = ctx.collect_direction;
      }
    else
	collect_direction = ctx.collect_direction;
    ctx.collect_direction = collect_direction;
    /* Now use this direction. */
    uint8_t collect_bay, storage_bay;
    uint8_t collect_bay_head, storage_bay_head;
    if (collect_direction == DIRECTION_FORWARD)
      {
	collect_bay = CLAMP_SLOT_FRONT_BOTTOM;
	storage_bay = CLAMP_SLOT_BACK_BOTTOM;
	collect_bay_head = front_head;
	storage_bay_head = back_head;
	ctx.clamp_pos_idle = CLAMP_SLOT_FRONT_MIDDLE;
      }
    else
      {
	collect_bay = CLAMP_SLOT_BACK_BOTTOM;
	storage_bay = CLAMP_SLOT_FRONT_BOTTOM;
	collect_bay_head = back_head;
	storage_bay_head = front_head;
	ctx.clamp_pos_idle = CLAMP_SLOT_BACK_MIDDLE;
      }
    /* Find a destination for an element move. */
    uint8_t moving_to = CLAMP_SLOT_NB;
    uint8_t moving_from = CLAMP_SLOT_NB;
    if (!ctx.slots[collect_bay + 1])
      {
	/* Movements in collect bay possible. */
	if (ELEMENT_IS_HEAD (ctx.slots[collect_bay + 0]))
	  {
	    moving_to = collect_bay + 2;
	    moving_from = collect_bay + 0;
	  }
      }
    if (moving_to == CLAMP_SLOT_NB && !ctx.slots[storage_bay + 1])
      {
	/* No movement yet and movements in storage bay possible. */
	if (ELEMENT_IS_HEAD (ctx.slots[storage_bay + 0]))
	  {
	    moving_to = storage_bay + 2;
	    moving_from = storage_bay + 0;
	  }
	else if (storage_bay_head)
	  {
	    if (!ctx.slots[storage_bay + 0])
		moving_to = storage_bay + 0;
	    else if (!ctx.slots[storage_bay + 1])
		moving_to = storage_bay + 1;
	  }
      }
    if (moving_to == CLAMP_SLOT_NB && !ctx.slots[CLAMP_SLOT_SIDE])
      {
	/* No movement yet, store in side slot. */
	moving_to = CLAMP_SLOT_SIDE;
      }
    /* Find a source if available. */
    if (moving_to != CLAMP_SLOT_NB && moving_from == CLAMP_SLOT_NB)
      {
	if (ctx.slots[collect_bay + 0])
	    moving_from = collect_bay + 0;
	else if (ctx.slots[CLAMP_SLOT_SIDE])
	    moving_from = CLAMP_SLOT_SIDE;
      }
    /* Ask for movement. */
    if (moving_from != CLAMP_SLOT_NB)
      {
	ctx.moving_from = moving_from;
	ctx.moving_to = moving_to;
      }
    logistic_debug_dump ();
}

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;
}

void
logistic_update (void)
{
}

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

void
logistic_element_move_done (void)
{
    assert (!ctx.slots[ctx.moving_to]);
    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 ();
}