root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / seeking.c @ 100
History | View | Annotate | Download (2.53 KB)
1 |
#include "seeking.h" |
---|---|
2 |
|
3 |
#include <move.h> |
4 |
|
5 |
#include <wl_token_ring.h> |
6 |
|
7 |
#define SEEK_WITH_BOM 1 |
8 |
#define SEEK_WITH_HOMING 2 |
9 |
|
10 |
#define SEEKING_BOM_VELOCITY -160 |
11 |
#define SEEKING_HOMING_VELOCITY -160 |
12 |
|
13 |
// values for homing sensor readings
|
14 |
#define LEFT_LOW 6 |
15 |
#define LEFT_HIGH 7 |
16 |
#define CENTER_LOW 18 |
17 |
#define CENTER_HIGH 19 |
18 |
#define RIGHT_LOW 12 |
19 |
#define RIGHT_HIGH 13 |
20 |
|
21 |
//function prototypes
|
22 |
int seek_station_with_bom(int station); |
23 |
int seek_station_with_homing_sensor(void); |
24 |
|
25 |
int state = SEEK_WITH_BOM;
|
26 |
|
27 |
void seek_station(int station) |
28 |
{ |
29 |
if (state == SEEK_WITH_BOM)
|
30 |
state = seek_station_with_bom(station); |
31 |
else
|
32 |
state = seek_station_with_homing_sensor(); |
33 |
} |
34 |
|
35 |
int seek_station_with_bom(int station) |
36 |
{ |
37 |
int charging_loc = wl_token_get_my_sensor_reading(station);
|
38 |
|
39 |
//move to charging station if charging_loc != -1
|
40 |
//at this point, wl_csloc is set to the max bom reading
|
41 |
if(charging_loc != -1) |
42 |
{ |
43 |
int desiredbom = 12; // for moving backwards |
44 |
int err = desiredbom - charging_loc;
|
45 |
if (err < -8) |
46 |
err += 16;
|
47 |
else if (err > 8) |
48 |
err -= 16;
|
49 |
int w = -err * 2; |
50 |
int v = SEEKING_BOM_VELOCITY;
|
51 |
//if the error in the BOM is 'large', slow v down
|
52 |
/*if( abs_int(err) > 2)
|
53 |
v = SEEKING_BOM_VELOCITY_TURN;
|
54 |
*/
|
55 |
move_avoid(v, w, 5);
|
56 |
} |
57 |
|
58 |
//TODO: redesign i2c to make this cleaner and not use globals
|
59 |
if(i2c_homing_sensor_data_dirty == I2C_DATA_DIRTY)
|
60 |
return SEEK_WITH_BOM;
|
61 |
|
62 |
//data is fresh, use it
|
63 |
int widthcount = i2c_homing_sensor_data;
|
64 |
i2c_homing_sensor_data_dirty = I2C_DATA_DIRTY; |
65 |
|
66 |
if(widthcount>= 4) |
67 |
return SEEK_WITH_HOMING;
|
68 |
return SEEK_WITH_BOM;
|
69 |
} |
70 |
|
71 |
int seek_station_with_homing_sensor(void) |
72 |
{ |
73 |
//if you have no new data, just continue on our current path
|
74 |
if(i2c_homing_sensor_data_dirty == I2C_DATA_DIRTY)
|
75 |
return SEEK_WITH_HOMING;
|
76 |
|
77 |
//TODO: create function in homing.h to return LEFT, FORWARD, RIGHT, or NONE
|
78 |
int widthcount = i2c_homing_sensor_data;
|
79 |
i2c_homing_sensor_data_dirty = I2C_DATA_DIRTY; |
80 |
|
81 |
if (widthcount < LEFT_LOW) //can't use beacon, switch to BOM |
82 |
return SEEK_WITH_BOM;
|
83 |
else if(widthcount >= CENTER_LOW) |
84 |
move(SEEKING_HOMING_VELOCITY, 0);
|
85 |
else
|
86 |
{ |
87 |
if(widthcount >= LEFT_LOW && widthcount <= LEFT_HIGH)
|
88 |
move(SEEKING_HOMING_VELOCITY, (widthcount) / 3);
|
89 |
else if (widthcount >= RIGHT_LOW && widthcount <= RIGHT_HIGH) |
90 |
move(SEEKING_HOMING_VELOCITY, -(widthcount) / 3);
|
91 |
else
|
92 |
{ |
93 |
RECHARGE_DEBUG_PRINT("Unexpected homing sensor value of ");
|
94 |
RECHARGE_DEBUG_PUTI(widthcount); |
95 |
RECHARGE_DEBUG_PRINT(".\n");
|
96 |
} |
97 |
} |
98 |
|
99 |
return SEEK_WITH_HOMING;
|
100 |
} |
101 |
|