Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.82 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
        //if (battery_low() || button2_click())
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
        if (recharge_i2c_is_battery_charging() || button2_click())
154
        {
155
                wl_recharge_dock();
156
                move(0, 0);
157
        }
158
}
159

    
160
/**
161
 * Called to check if charging is complete.
162
 * If charging is complete, the state is
163
 * changed to DEPARTING.
164
 **/
165
void recharge_check_charging_complete()
166
{
167
    //if (recharge_i2c_is_battery_full() || button2_click())
168
        if (button2_click())
169
        {
170
                wl_recharge_depart();
171
        }
172
}
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
}
183