Project

General

Profile

Statistics
| Revision:

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

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
        //TODO: uncomment to use homing sensor
34
        recharge_i2c_init();
35
}
36

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

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

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

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

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

    
155
        //TODO: check if battery is charging
156
        //if (battery is charging)
157
        if (button2_click())
158
                wl_recharge_dock();
159
}
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
        //TODO: implement
169
        //if (battery is charged)
170
        if (button2_click())
171
        {
172
                motors_off();
173
                wl_recharge_depart();
174
        }
175
}
176

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