Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.91 KB)

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

    
5
#include <wl_defs.h>
6
#include "wl_recharge_group.h"
7
#include "seeking.h"
8
#include "departing.h"
9

    
10
// Function prototypes
11
void recharge_check_low_battery(void);
12
void recharge_check_charging_complete(void);
13
void recharge_seek(void);
14
void recharge_depart(void);
15

    
16
/**
17
 * Initializes recharging. Before this function may be called,
18
 * wl_init must be called.
19
 **/
20
void recharge_init(void)
21
{
22
        wl_recharge_register();
23
        recharge_i2c_init();
24
}
25

    
26
/**
27
 * Uninitialize recharging. Recharging and wireless must be 
28
 * initialized before this function may be used.
29
 **/
30
void recharge_uninit(void)
31
{
32
        wl_recharge_unregister();
33
}
34

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

    
76
        // show colors if we are debugging
77
        #ifdef RECHARGE_DEBUG
78
        switch (wl_recharge_get_state())
79
        {
80
                
81
        }
82
        #endif
83
        
84
        return wl_recharge_get_state() != NOT_RECHARGING;
85
}
86

    
87
/**
88
 * Called to check if the battery is low. If the battery
89
 * is low and the robot needs to charge, the state
90
 * is changed to POLLING.
91
 **/
92
void recharge_check_low_battery()
93
{
94
        //TODO: use battery_low instead of button press
95
        //if (battery_low())
96
        if (button2_click())
97
        {
98
                wl_recharge_begin();
99
                move(0, 0);
100
        }
101
}
102

    
103
/**
104
 * Called when the robot is seeking the charging station.
105
 * Controls the robot's movement, and use of the BOM
106
 * and the homing sensor. Also checks if the robot
107
 * is currently charging, and changes its state
108
 * accordingly.
109
 **/
110
void recharge_seek()
111
{
112
        seek_station(wl_recharge_get_station());
113

    
114
        //TODO: check if battery is charging
115
        //if (battery is charging)
116
        if (button2_click())
117
                wl_recharge_dock();
118
}
119

    
120
/**
121
 * Called to check if charging is complete.
122
 * If charging is complete, the state is
123
 * changed to DEPARTING.
124
 **/
125
void recharge_check_charging_complete()
126
{
127
        //TODO: implement
128
        //if (battery is charged)
129
        if (button2_click())
130
                wl_recharge_depart();
131
}
132

    
133
/**
134
 * Called when the robot is departing from the 
135
 * charging station.
136
 **/
137
void recharge_depart()
138
{
139
        if (depart_station())
140
                wl_recharge_stop();
141
}
142