root / branches / autonomous_recharging / code / projects / autonomous_recharging / charging_station / charging.c @ 120
History | View | Annotate | Download (7.8 KB)
1 |
#include "charging.h" |
---|---|
2 |
#include "charging_defs.h" |
3 |
#include "bays.h" |
4 |
|
5 |
#include <stdlib.h> |
6 |
#include <serial.h> |
7 |
|
8 |
#include <wl_token_ring.h> |
9 |
#include "wl_charging_station.h" |
10 |
|
11 |
//set this to the number of bays
|
12 |
#define NUM_BAYS 2 |
13 |
|
14 |
//Function prototypes
|
15 |
void charging_bom_on(void); |
16 |
void charging_bom_off(void); |
17 |
int charging_bom_read(void); |
18 |
|
19 |
//Structures
|
20 |
typedef struct |
21 |
{ |
22 |
int state;
|
23 |
int bay;
|
24 |
int verifyCount;
|
25 |
} ChargingRobot; |
26 |
|
27 |
ChargingRobot** robots; |
28 |
int robotsSize = 0; |
29 |
int numRobots = 0; |
30 |
int iteratorPos = 0; |
31 |
|
32 |
int bays[NUM_BAYS];
|
33 |
|
34 |
int is_seeking = 0; |
35 |
|
36 |
/**
|
37 |
* Call this function to initialize charging.
|
38 |
* wl_token_ring_register must be called before
|
39 |
* this function.
|
40 |
**/
|
41 |
void charging_init(void) |
42 |
{ |
43 |
robots = malloc(robotsSize * sizeof(ChargingRobot*));
|
44 |
if (!robots)
|
45 |
{ |
46 |
STATION_DEBUG_PRINT("Out of memory.\n");
|
47 |
return;
|
48 |
} |
49 |
|
50 |
int i;
|
51 |
for (i = 0; i < robotsSize; i++) |
52 |
robots[i] = NULL;
|
53 |
|
54 |
for (i = 0; i < NUM_BAYS; i++) |
55 |
bays[i] = -1;
|
56 |
|
57 |
bays_init(); |
58 |
|
59 |
// call our own special function to set the BOM
|
60 |
wl_token_ring_set_bom_functions(charging_bom_on, |
61 |
charging_bom_off, charging_bom_read); |
62 |
wl_station_register(); |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Initializes the iterator through the robots
|
67 |
* which are using the charging station.
|
68 |
**/
|
69 |
void charging_robot_iterator_init(void) |
70 |
{ |
71 |
iteratorPos = 0;
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Returns zero if the iterator has gone through
|
76 |
* all robots that are using the charging station
|
77 |
* since the last call to charging_robot_iterator_init,
|
78 |
* nonzero otherwise.
|
79 |
*
|
80 |
* @return if the iterator has gone through all the robots
|
81 |
**/
|
82 |
int charging_robot_iterator_has_next(void) |
83 |
{ |
84 |
while (iteratorPos < robotsSize)
|
85 |
{ |
86 |
if (robots[iteratorPos] != NULL) |
87 |
return 1; |
88 |
iteratorPos++; |
89 |
} |
90 |
return 0; |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Returns the id of the next robot in the iterator
|
95 |
* of robots that are using the charging station.
|
96 |
*
|
97 |
* @return the id of a robot using the station
|
98 |
**/
|
99 |
int charging_robot_iterator_next(void) |
100 |
{ |
101 |
while (iteratorPos < robotsSize)
|
102 |
{ |
103 |
if (robots[iteratorPos] != NULL) |
104 |
{ |
105 |
iteratorPos++; |
106 |
return iteratorPos - 1; |
107 |
} |
108 |
} |
109 |
|
110 |
return -1; |
111 |
} |
112 |
|
113 |
/**
|
114 |
* Return the verification count for a robot
|
115 |
* using the station. If the count is zero,
|
116 |
* then the robot is probably dead.
|
117 |
*
|
118 |
* @param robot the id of the robot to check
|
119 |
*
|
120 |
* @return the verify count for robot robot
|
121 |
**/
|
122 |
int charging_get_verify_count(int robot) |
123 |
{ |
124 |
if (robot >= robotsSize || robot < 0) |
125 |
{ |
126 |
STATION_DEBUG_PRINT("Index out of range.\n");
|
127 |
return -1; |
128 |
} |
129 |
if (robots[robot] == NULL) |
130 |
{ |
131 |
STATION_DEBUG_PRINT("Robot not known to charging station.\n");
|
132 |
return -1; |
133 |
} |
134 |
|
135 |
return robots[robot]->verifyCount;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Sets the verify count for the specified robot.
|
140 |
*
|
141 |
* @param robot the robot to specify the count for
|
142 |
* @param count the new verify count
|
143 |
**/
|
144 |
void charging_set_verify_count(int robot, int count) |
145 |
{ |
146 |
if (robot >= robotsSize || robot < 0) |
147 |
{ |
148 |
STATION_DEBUG_PRINT("Index out of range.\n");
|
149 |
return;
|
150 |
} |
151 |
if (robots[robot] == NULL) |
152 |
{ |
153 |
STATION_DEBUG_PRINT("Robot not known to charging station.\n");
|
154 |
return;
|
155 |
} |
156 |
|
157 |
robots[robot]->verifyCount = count; |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Called when charging has been cancelled for a
|
162 |
* specific robot.
|
163 |
*
|
164 |
* @param robot the robot to cancel charging for
|
165 |
**/
|
166 |
void charging_cancel(int robot) |
167 |
{ |
168 |
if (robot < 0 || robot >= robotsSize || robots[robot] == NULL) |
169 |
{ |
170 |
STATION_DEBUG_PRINT("Attempted to cancel charging for an ");
|
171 |
STATION_DEBUG_PRINT("unknown robot.\n");
|
172 |
return;
|
173 |
} |
174 |
|
175 |
STATION_DEBUG_PRINT("Cancelling charging for robot ");
|
176 |
STATION_DEBUG_PUTI(robot); |
177 |
STATION_DEBUG_PRINT(".\n");
|
178 |
|
179 |
//allow other robots to seek
|
180 |
if (robots[robot]->state == STATE_SEEKING)
|
181 |
{ |
182 |
bay_disable(); |
183 |
is_seeking = 0;
|
184 |
} |
185 |
|
186 |
//free the bay
|
187 |
bays[robots[robot]->bay] = -1;
|
188 |
|
189 |
free(robots[robot]); |
190 |
robots[robot] = NULL;
|
191 |
numRobots--; |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Assigns a bay to a specific robot.
|
196 |
*
|
197 |
* @param robot the robot to assign the bay to
|
198 |
*
|
199 |
* @return the identifier for the assigned bay
|
200 |
**/
|
201 |
int assign_bay(int robot) |
202 |
{ |
203 |
int bay = 0; |
204 |
|
205 |
//TODO: use better algorithm based on distance
|
206 |
for (bay = 0; bay < NUM_BAYS; bay++) |
207 |
if (bays[bay] == -1) |
208 |
break;
|
209 |
|
210 |
if (bay == NUM_BAYS)
|
211 |
{ |
212 |
STATION_DEBUG_PRINT("Attempted to assign a bay when all ");
|
213 |
STATION_DEBUG_PRINT("are full.\n");
|
214 |
} |
215 |
|
216 |
bays[bay] = robot; |
217 |
return bay;
|
218 |
} |
219 |
|
220 |
/**
|
221 |
* Called when a robot begins seeking.
|
222 |
*
|
223 |
* @param robot the robot which has begun seeking
|
224 |
**/
|
225 |
void charging_begin_seeking(int robot) |
226 |
{ |
227 |
STATION_DEBUG_PRINT("Robot ");
|
228 |
STATION_DEBUG_PUTI(robot); |
229 |
STATION_DEBUG_PRINT(" has begun seeking.\n");
|
230 |
|
231 |
if (robot > robotsSize)
|
232 |
{ |
233 |
int nextSize = robot + 1; |
234 |
ChargingRobot** temp = malloc(nextSize * sizeof(ChargingRobot*));
|
235 |
if (temp == NULL) |
236 |
{ |
237 |
STATION_DEBUG_PRINT("Out of memory.\n");
|
238 |
return;
|
239 |
} |
240 |
int i;
|
241 |
for (i = 0; i < robotsSize; i++) |
242 |
temp[i] = robots[i]; |
243 |
for (; i < nextSize; i++)
|
244 |
temp[i] = NULL;
|
245 |
free(robots); |
246 |
robots = temp; |
247 |
robotsSize = nextSize; |
248 |
} |
249 |
|
250 |
if (charging_is_robot_seeking())
|
251 |
{ |
252 |
STATION_DEBUG_PRINT("A robot is already seeking.\n");
|
253 |
return;
|
254 |
} |
255 |
|
256 |
//check if robot has reverted from docking to seeking
|
257 |
if (robots[robot] != NULL) |
258 |
{ |
259 |
switch (robots[robot]->state)
|
260 |
{ |
261 |
case STATE_SEEKING:
|
262 |
//do nothing
|
263 |
STATION_DEBUG_PRINT("Robot is already ");
|
264 |
STATION_DEBUG_PRINT("seeking.\n");
|
265 |
break;
|
266 |
case STATE_DOCKED:
|
267 |
if (is_seeking)
|
268 |
{ |
269 |
//we can only allow one robot to seek
|
270 |
robots[robot]->state = STATE_NOT_CHARGING; |
271 |
bays[robots[robot]->bay] = -1;
|
272 |
robots[robot]->bay = -1;
|
273 |
break;
|
274 |
} |
275 |
robots[robot]->state = STATE_SEEKING; |
276 |
is_seeking = 1;
|
277 |
bay_enable(robots[robot]->bay); |
278 |
break;
|
279 |
default:
|
280 |
STATION_DEBUG_PRINT("Robot is in an ");
|
281 |
STATION_DEBUG_PRINT("unexpected state.\n");
|
282 |
break;
|
283 |
} |
284 |
return;
|
285 |
} |
286 |
|
287 |
if (!charging_is_space_available())
|
288 |
{ |
289 |
STATION_DEBUG_PRINT("No bays are available for charging.\n");
|
290 |
return;
|
291 |
} |
292 |
|
293 |
ChargingRobot* r = malloc(sizeof(ChargingRobot));
|
294 |
if (!r)
|
295 |
{ |
296 |
STATION_DEBUG_PRINT("Out of memory.\n");
|
297 |
return;
|
298 |
} |
299 |
|
300 |
r->state = STATE_SEEKING; |
301 |
r->bay = assign_bay(robot); |
302 |
r->verifyCount = VERIFY_DELAY; |
303 |
robots[robot] = r; |
304 |
numRobots++; |
305 |
is_seeking = 1;
|
306 |
bay_enable(r->bay); |
307 |
} |
308 |
|
309 |
/**
|
310 |
* Called when a robot has docked.
|
311 |
*
|
312 |
* @param robot the robot which has docked
|
313 |
**/
|
314 |
void charging_dock(int robot) |
315 |
{ |
316 |
STATION_DEBUG_PRINT("Robot ");
|
317 |
STATION_DEBUG_PUTI(robot); |
318 |
STATION_DEBUG_PRINT(" has docked with the station.\n");
|
319 |
|
320 |
if (robots[robot] == NULL) |
321 |
{ |
322 |
STATION_DEBUG_PRINT("Should not proceed from not ");
|
323 |
STATION_DEBUG_PRINT("charging to docked.\n");
|
324 |
return;
|
325 |
} |
326 |
|
327 |
if (robots[robot]->state == STATE_DOCKED)
|
328 |
{ |
329 |
STATION_DEBUG_PRINT("Robot is already docked.\n");
|
330 |
return;
|
331 |
} |
332 |
|
333 |
//robot was seeking before
|
334 |
is_seeking = 0;
|
335 |
bay_disable(); |
336 |
robots[robot]->state = STATE_DOCKED; |
337 |
} |
338 |
|
339 |
/**
|
340 |
* Checks if there are any available bays
|
341 |
* for a robot to occupy.
|
342 |
*
|
343 |
* @return nonzero if there are bays available,
|
344 |
* and zero otherwise.
|
345 |
**/
|
346 |
int charging_is_space_available(void) |
347 |
{ |
348 |
return numRobots < NUM_BAYS;
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* Checks if there is a robot which
|
353 |
* is currently seeking the station.
|
354 |
*
|
355 |
* @return nonzero if a robot is seeking the
|
356 |
* station, zero otherwise.
|
357 |
**/
|
358 |
int charging_is_robot_seeking(void) |
359 |
{ |
360 |
return is_seeking;
|
361 |
} |
362 |
|
363 |
/**
|
364 |
* Returns the state of the specified robot.
|
365 |
*
|
366 |
* @param robot the robot to get the state of
|
367 |
*
|
368 |
* @return either STATE_NOT_CHARGING, STATE_SEEKING, or STATE_DOCKED
|
369 |
**/
|
370 |
int charging_get_robot_state(int robot) |
371 |
{ |
372 |
if (robot >= robotsSize || robots[robot] == NULL) |
373 |
return STATE_NOT_CHARGING;
|
374 |
return robots[robot]->state;
|
375 |
} |
376 |
|
377 |
/**
|
378 |
* Called when the BOM needs to be turned on for the
|
379 |
* token ring. If a robot is seeking, only the BOM of
|
380 |
* the bay that is being sought will turn on. If no
|
381 |
* robot is seeking, the BOM for all of the bays will
|
382 |
* turn on.
|
383 |
**/
|
384 |
void charging_bom_on(void) |
385 |
{ |
386 |
lbom_on(); |
387 |
} |
388 |
|
389 |
/**
|
390 |
* Called when the BOM needs to be turned off for the
|
391 |
* token ring. Turns off the BOM for all bays.
|
392 |
**/
|
393 |
void charging_bom_off(void) |
394 |
{ |
395 |
lbom_off(); |
396 |
} |
397 |
|
398 |
/**
|
399 |
* The charging station BOM can only emit, so
|
400 |
* we just return -1.
|
401 |
**/
|
402 |
int charging_bom_read(void) |
403 |
{ |
404 |
return -1; |
405 |
} |
406 |
|