Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.82 KB)

1
#include <move.h>
2
#include <battery.h>
3
#include <dio.h>
4
#include <lights.h>
5

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

    
13
// Function prototypes
14
void recharge_check_low_battery(void);
15
void recharge_check_charging_complete(void);
16
void recharge_seek(void);
17
void recharge_depart(void);
18

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

    
24
/**
25
 * Initializes recharging. Before this function may be called,
26
 * wl_init must be called.
27
 **/
28
void recharge_init(void)
29
{
30
        wl_recharge_register();
31
        //TODO: uncomment to use homing sensor
32
        //recharge_i2c_init();
33
}
34

    
35
/**
36
 * Uninitialize recharging. Recharging and wireless must be 
37
 * initialized before this function may be used.
38
 **/
39
void recharge_uninit(void)
40
{
41
        wl_recharge_unregister();
42
}
43

    
44
/**
45
 * This function must be called frequently to perform recharging.
46
 * 
47
 * wl_do must be called often in conjunction with this function.
48
 * 
49
 * If recharge_do returns zero, then the robot is not charging, 
50
 * and the user has control of the robot. If this function returns
51
 * nonzero, then the robot is attempting to charge, and the user
52
 * should do nothing other than continously call this function
53
 * and wl_do.
54
 *
55
 * recharge_init must be called before this function
56
 * may be used.
57
 *
58
 * @return nonzero if the robot is recharging, zero otherwise
59
 **/
60
int recharge_do(void)
61
{
62
        switch (wl_recharge_get_state())
63
        {
64
                case NOT_RECHARGING:
65
                        recharge_check_low_battery();
66
                        break;
67
                case POLLING:
68
                case REQUESTING:
69
                        //do nothing, wait for succesful request
70
                        break;
71
                case SEEKING:
72
                        recharge_seek();
73
                        break;
74
                case DOCKED:
75
                        recharge_check_charging_complete();
76
                        break;
77
                case DEPARTING:
78
                        recharge_depart();
79
                        break;
80
                default:
81
                        WL_DEBUG_PRINT("Unexpected state.\n");
82
                        break;
83
        }
84

    
85
        // show colors if we are debugging
86
        #ifdef RECHARGE_DEBUG
87
        if (old_state != wl_recharge_get_state())
88
        {
89
                old_state = wl_recharge_get_state();
90
                switch (old_state)
91
                {
92
                        case NOT_RECHARGING:
93
                                RECHARGE_DEBUG_PRINT("Not recharging.\n");
94
                                orb_set_color(GREEN);
95
                                break;
96
                        case POLLING:
97
                                RECHARGE_DEBUG_PRINT("Polling available stations.\n");
98
                                orb_set_color(CYAN);
99
                                break;
100
                        case REQUESTING:
101
                                RECHARGE_DEBUG_PRINT("Requesting a station.\n");
102
                                orb_set_color(RED);
103
                                break;
104
                        case SEEKING:
105
                                RECHARGE_DEBUG_PRINT("Seeking.\n");
106
                                orb_set_color(YELLOW);
107
                                break;
108
                        case DOCKED:
109
                                RECHARGE_DEBUG_PRINT("Docked.\n");
110
                                orb_set_color(BLUE);
111
                                break;
112
                        case DEPARTING:
113
                                RECHARGE_DEBUG_PRINT("Departing.\n");
114
                                orb_set_color(PURPLE);
115
                                break;
116
                        default:
117
                                WL_DEBUG_PRINT("Unexpected state.\n");
118
                                break;
119
                }
120
        }
121
        #endif
122
        
123
        return wl_recharge_get_state() != NOT_RECHARGING;
124
}
125

    
126
/**
127
 * Called to check if the battery is low. If the battery
128
 * is low and the robot needs to charge, the state
129
 * is changed to POLLING.
130
 **/
131
void recharge_check_low_battery()
132
{
133
        //TODO: use battery_low instead of button press
134
        //if (battery_low())
135
        if (button2_click())
136
        {
137
                wl_recharge_begin();
138
                move(0, 0);
139
        }
140
}
141

    
142
/**
143
 * Called when the robot is seeking the charging station.
144
 * Controls the robot's movement, and use of the BOM
145
 * and the homing sensor. Also checks if the robot
146
 * is currently charging, and changes its state
147
 * accordingly.
148
 **/
149
void recharge_seek()
150
{
151
        seek_station(wl_recharge_get_station());
152

    
153
        //TODO: check if battery is charging
154
        //if (battery is charging)
155
        if (button2_click())
156
                wl_recharge_dock();
157
}
158

    
159
/**
160
 * Called to check if charging is complete.
161
 * If charging is complete, the state is
162
 * changed to DEPARTING.
163
 **/
164
void recharge_check_charging_complete()
165
{
166
        //TODO: implement
167
        //if (battery is charged)
168
        if (button2_click())
169
                wl_recharge_depart();
170
}
171

    
172
/**
173
 * Called when the robot is departing from the 
174
 * charging station.
175
 **/
176
void recharge_depart()
177
{
178
        if (depart_station())
179
                wl_recharge_stop();
180
}
181