root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / recharge.c @ 210
History | View | Annotate | Download (3.83 KB)
1 | 100 | bcoltin | #include <move.h> |
---|---|---|---|
2 | #include <battery.h> |
||
3 | #include <dio.h> |
||
4 | 116 | bcoltin | #include <lights.h> |
5 | 126 | bcoltin | #include <motor.h> |
6 | #include <serial.h> |
||
7 | 100 | bcoltin | |
8 | 93 | jykong | #include <wl_defs.h> |
9 | #include "wl_recharge_group.h" |
||
10 | 100 | bcoltin | #include "seeking.h" |
11 | #include "departing.h" |
||
12 | 116 | bcoltin | #include "recharge_i2c.h" |
13 | #include "recharge_defs.h" |
||
14 | 93 | jykong | |
15 | 100 | bcoltin | // Function prototypes
|
16 | void recharge_check_low_battery(void); |
||
17 | void recharge_check_charging_complete(void); |
||
18 | void recharge_seek(void); |
||
19 | void recharge_depart(void); |
||
20 | 93 | jykong | |
21 | 116 | bcoltin | //Global variables
|
22 | #ifdef RECHARGE_DEBUG
|
||
23 | int old_state = -1; |
||
24 | #endif
|
||
25 | |||
26 | 93 | jykong | /**
|
27 | * Initializes recharging. Before this function may be called,
|
||
28 | * wl_init must be called.
|
||
29 | **/
|
||
30 | void recharge_init(void) |
||
31 | { |
||
32 | wl_recharge_register(); |
||
33 | 126 | bcoltin | recharge_i2c_init(); |
34 | 93 | jykong | } |
35 | |||
36 | /**
|
||
37 | * Uninitialize recharging. Recharging and wireless must be
|
||
38 | * initialized before this function may be used.
|
||
39 | **/
|
||
40 | void recharge_uninit(void) |
||
41 | { |
||
42 | wl_recharge_unregister(); |
||
43 | } |
||
44 | |||
45 | /**
|
||
46 | * This function must be called frequently to perform recharging.
|
||
47 | *
|
||
48 | * wl_do must be called often in conjunction with this function.
|
||
49 | *
|
||
50 | * If recharge_do returns zero, then the robot is not charging,
|
||
51 | * and the user has control of the robot. If this function returns
|
||
52 | * nonzero, then the robot is attempting to charge, and the user
|
||
53 | * should do nothing other than continously call this function
|
||
54 | * and wl_do.
|
||
55 | *
|
||
56 | * recharge_init must be called before this function
|
||
57 | * may be used.
|
||
58 | *
|
||
59 | * @return nonzero if the robot is recharging, zero otherwise
|
||
60 | **/
|
||
61 | int recharge_do(void) |
||
62 | { |
||
63 | switch (wl_recharge_get_state())
|
||
64 | { |
||
65 | case NOT_RECHARGING:
|
||
66 | recharge_check_low_battery(); |
||
67 | break;
|
||
68 | case POLLING:
|
||
69 | case REQUESTING:
|
||
70 | //do nothing, wait for succesful request
|
||
71 | break;
|
||
72 | case SEEKING:
|
||
73 | recharge_seek(); |
||
74 | break;
|
||
75 | case DOCKED:
|
||
76 | recharge_check_charging_complete(); |
||
77 | break;
|
||
78 | case DEPARTING:
|
||
79 | recharge_depart(); |
||
80 | break;
|
||
81 | default:
|
||
82 | WL_DEBUG_PRINT("Unexpected state.\n");
|
||
83 | break;
|
||
84 | } |
||
85 | 100 | bcoltin | |
86 | // show colors if we are debugging
|
||
87 | #ifdef RECHARGE_DEBUG
|
||
88 | 116 | bcoltin | if (old_state != wl_recharge_get_state())
|
89 | 100 | bcoltin | { |
90 | 116 | bcoltin | old_state = wl_recharge_get_state(); |
91 | 120 | bcoltin | switch (old_state)
|
92 | 116 | bcoltin | { |
93 | case NOT_RECHARGING:
|
||
94 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Not recharging.\n");
|
95 | 137 | bcoltin | //orb_set_color(GREEN);
|
96 | 116 | bcoltin | break;
|
97 | case POLLING:
|
||
98 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Polling available stations.\n");
|
99 | 137 | bcoltin | //orb_set_color(CYAN);
|
100 | 120 | bcoltin | break;
|
101 | 116 | bcoltin | case REQUESTING:
|
102 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Requesting a station.\n");
|
103 | 137 | bcoltin | //orb_set_color(RED);
|
104 | 116 | bcoltin | break;
|
105 | case SEEKING:
|
||
106 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Seeking.\n");
|
107 | 137 | bcoltin | //orb_set_color(YELLOW);
|
108 | 116 | bcoltin | break;
|
109 | case DOCKED:
|
||
110 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Docked.\n");
|
111 | 137 | bcoltin | //orb_set_color(BLUE);
|
112 | 116 | bcoltin | break;
|
113 | case DEPARTING:
|
||
114 | 120 | bcoltin | RECHARGE_DEBUG_PRINT("Departing.\n");
|
115 | 137 | bcoltin | //orb_set_color(PURPLE);
|
116 | 116 | bcoltin | break;
|
117 | default:
|
||
118 | WL_DEBUG_PRINT("Unexpected state.\n");
|
||
119 | break;
|
||
120 | } |
||
121 | 100 | bcoltin | } |
122 | #endif
|
||
123 | 93 | jykong | |
124 | return wl_recharge_get_state() != NOT_RECHARGING;
|
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * Called to check if the battery is low. If the battery
|
||
129 | * is low and the robot needs to charge, the state
|
||
130 | * is changed to POLLING.
|
||
131 | **/
|
||
132 | void recharge_check_low_battery()
|
||
133 | { |
||
134 | 195 | cmar | //if (battery_low() || button2_click())
|
135 | 100 | bcoltin | if (button2_click())
|
136 | 93 | jykong | { |
137 | 210 | bcoltin | recharge_i2c_set_battery_full(0);
|
138 | 93 | jykong | wl_recharge_begin(); |
139 | move(0, 0); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * Called when the robot is seeking the charging station.
|
||
145 | * Controls the robot's movement, and use of the BOM
|
||
146 | * and the homing sensor. Also checks if the robot
|
||
147 | * is currently charging, and changes its state
|
||
148 | * accordingly.
|
||
149 | **/
|
||
150 | void recharge_seek()
|
||
151 | { |
||
152 | 100 | bcoltin | seek_station(wl_recharge_get_station()); |
153 | 195 | cmar | |
154 | if (recharge_i2c_is_battery_charging() || button2_click())
|
||
155 | 137 | bcoltin | { |
156 | 93 | jykong | wl_recharge_dock(); |
157 | 137 | bcoltin | move(0, 0); |
158 | } |
||
159 | 93 | jykong | } |
160 | |||
161 | /**
|
||
162 | * Called to check if charging is complete.
|
||
163 | * If charging is complete, the state is
|
||
164 | * changed to DEPARTING.
|
||
165 | **/
|
||
166 | void recharge_check_charging_complete()
|
||
167 | { |
||
168 | 210 | bcoltin | if (recharge_i2c_is_battery_full() || button2_click())
|
169 | 126 | bcoltin | { |
170 | 93 | jykong | wl_recharge_depart(); |
171 | 126 | bcoltin | } |
172 | 93 | jykong | } |
173 | |||
174 | /**
|
||
175 | * Called when the robot is departing from the
|
||
176 | * charging station.
|
||
177 | **/
|
||
178 | void recharge_depart()
|
||
179 | { |
||
180 | if (depart_station())
|
||
181 | wl_recharge_stop(); |
||
182 | } |