root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / recharge.c @ 100
History | View | Annotate | Download (2.89 KB)
1 |
#include <move.h> |
---|---|
2 |
#include <battery.h> |
3 |
#include <dio.h> |
4 |
|
5 |
#include <wl_defs.h> |
6 |
#include "wl_recharge_group.h" |
7 |
#include "seeking.h" |
8 |
#include "departing.h" |
9 |
|
10 |
// Function prototypes
|
11 |
void recharge_check_low_battery(void); |
12 |
void recharge_check_charging_complete(void); |
13 |
void recharge_seek(void); |
14 |
void recharge_depart(void); |
15 |
|
16 |
/**
|
17 |
* Initializes recharging. Before this function may be called,
|
18 |
* wl_init must be called.
|
19 |
**/
|
20 |
void recharge_init(void) |
21 |
{ |
22 |
wl_recharge_register(); |
23 |
} |
24 |
|
25 |
/**
|
26 |
* Uninitialize recharging. Recharging and wireless must be
|
27 |
* initialized before this function may be used.
|
28 |
**/
|
29 |
void recharge_uninit(void) |
30 |
{ |
31 |
wl_recharge_unregister(); |
32 |
} |
33 |
|
34 |
/**
|
35 |
* This function must be called frequently to perform recharging.
|
36 |
*
|
37 |
* wl_do must be called often in conjunction with this function.
|
38 |
*
|
39 |
* If recharge_do returns zero, then the robot is not charging,
|
40 |
* and the user has control of the robot. If this function returns
|
41 |
* nonzero, then the robot is attempting to charge, and the user
|
42 |
* should do nothing other than continously call this function
|
43 |
* and wl_do.
|
44 |
*
|
45 |
* recharge_init must be called before this function
|
46 |
* may be used.
|
47 |
*
|
48 |
* @return nonzero if the robot is recharging, zero otherwise
|
49 |
**/
|
50 |
int recharge_do(void) |
51 |
{ |
52 |
switch (wl_recharge_get_state())
|
53 |
{ |
54 |
case NOT_RECHARGING:
|
55 |
recharge_check_low_battery(); |
56 |
break;
|
57 |
case POLLING:
|
58 |
case REQUESTING:
|
59 |
//do nothing, wait for succesful request
|
60 |
break;
|
61 |
case SEEKING:
|
62 |
recharge_seek(); |
63 |
break;
|
64 |
case DOCKED:
|
65 |
recharge_check_charging_complete(); |
66 |
break;
|
67 |
case DEPARTING:
|
68 |
recharge_depart(); |
69 |
break;
|
70 |
default:
|
71 |
WL_DEBUG_PRINT("Unexpected state.\n");
|
72 |
break;
|
73 |
} |
74 |
|
75 |
// show colors if we are debugging
|
76 |
#ifdef RECHARGE_DEBUG
|
77 |
switch (wl_recharge_get_state())
|
78 |
{ |
79 |
|
80 |
} |
81 |
#endif
|
82 |
|
83 |
return wl_recharge_get_state() != NOT_RECHARGING;
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* Called to check if the battery is low. If the battery
|
88 |
* is low and the robot needs to charge, the state
|
89 |
* is changed to POLLING.
|
90 |
**/
|
91 |
void recharge_check_low_battery()
|
92 |
{ |
93 |
//TODO: use battery_low instead of button press
|
94 |
//if (battery_low())
|
95 |
if (button2_click())
|
96 |
{ |
97 |
wl_recharge_begin(); |
98 |
move(0, 0); |
99 |
} |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Called when the robot is seeking the charging station.
|
104 |
* Controls the robot's movement, and use of the BOM
|
105 |
* and the homing sensor. Also checks if the robot
|
106 |
* is currently charging, and changes its state
|
107 |
* accordingly.
|
108 |
**/
|
109 |
void recharge_seek()
|
110 |
{ |
111 |
seek_station(wl_recharge_get_station()); |
112 |
|
113 |
//TODO: check if battery is charging
|
114 |
//if (battery is charging)
|
115 |
if (button2_click())
|
116 |
wl_recharge_dock(); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Called to check if charging is complete.
|
121 |
* If charging is complete, the state is
|
122 |
* changed to DEPARTING.
|
123 |
**/
|
124 |
void recharge_check_charging_complete()
|
125 |
{ |
126 |
//TODO: implement
|
127 |
//if (battery is charged)
|
128 |
if (button2_click())
|
129 |
wl_recharge_depart(); |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Called when the robot is departing from the
|
134 |
* charging station.
|
135 |
**/
|
136 |
void recharge_depart()
|
137 |
{ |
138 |
if (depart_station())
|
139 |
wl_recharge_stop(); |
140 |
} |
141 |
|