Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / recharge.c @ 189

History | View | Annotate | Download (3.88 KB)

1
#include <move.h>
2
#include <battery.h>
3
#include <dio.h>
4
#include <lights.h>
5
#include <motor.h>
6
#include <serial.h>
7

    
8
#include <wl_defs.h>
9
#include "wl_recharge_group.h"
10
#include "seeking.h"
11
#include "departing.h"
12
#include "recharge_i2c.h"
13
#include "recharge_defs.h"
14

    
15
// 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

    
21
//Global variables
22
#ifdef RECHARGE_DEBUG
23
int old_state = -1;
24
#endif
25

    
26
/**
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
        recharge_i2c_init();
34
}
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

    
86
        // show colors if we are debugging
87
        #ifdef RECHARGE_DEBUG
88
        if (old_state != wl_recharge_get_state())
89
        {
90
                old_state = wl_recharge_get_state();
91
                switch (old_state)
92
                {
93
                        case NOT_RECHARGING:
94
                                RECHARGE_DEBUG_PRINT("Not recharging.\n");
95
                                //orb_set_color(GREEN);
96
                                break;
97
                        case POLLING:
98
                                RECHARGE_DEBUG_PRINT("Polling available stations.\n");
99
                                //orb_set_color(CYAN);
100
                                break;
101
                        case REQUESTING:
102
                                RECHARGE_DEBUG_PRINT("Requesting a station.\n");
103
                                //orb_set_color(RED);
104
                                break;
105
                        case SEEKING:
106
                                RECHARGE_DEBUG_PRINT("Seeking.\n");
107
                                //orb_set_color(YELLOW);
108
                                break;
109
                        case DOCKED:
110
                                RECHARGE_DEBUG_PRINT("Docked.\n");
111
                                //orb_set_color(BLUE);
112
                                break;
113
                        case DEPARTING:
114
                                RECHARGE_DEBUG_PRINT("Departing.\n");
115
                                //orb_set_color(PURPLE);
116
                                break;
117
                        default:
118
                                WL_DEBUG_PRINT("Unexpected state.\n");
119
                                break;
120
                }
121
        }
122
        #endif
123
        
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
        //TODO: use battery_low instead of button press
135
        //if (battery_low())
136
        if (button2_click())
137
        {
138
                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
        seek_station(wl_recharge_get_station());
153

    
154
        //TODO: check if battery is charging
155
        //if (battery is charging)
156
        if (recharge_i2c_is_battery_charging())
157
        {
158
                wl_recharge_dock();
159
                move(0, 0);
160
        }
161
}
162

    
163
/**
164
 * Called to check if charging is complete.
165
 * If charging is complete, the state is
166
 * changed to DEPARTING.
167
 **/
168
void recharge_check_charging_complete()
169
{
170
        //TODO: implement
171
        //if (battery is charged)
172
        if (button2_click())
173
        {
174
                wl_recharge_depart();
175
        }
176
}
177

    
178
/**
179
 * Called when the robot is departing from the 
180
 * charging station.
181
 **/
182
void recharge_depart()
183
{
184
        if (depart_station())
185
                wl_recharge_stop();
186
}
187