root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / seeking.c @ 645
History | View | Annotate | Download (2 KB)
1 |
#include "seeking.h" |
---|---|
2 |
|
3 |
#include <move.h> |
4 |
#include <serial.h> |
5 |
|
6 |
#include "recharge_i2c.h" |
7 |
#include "recharge_defs.h" |
8 |
#include "homing.h" |
9 |
|
10 |
#include <wl_token_ring.h> |
11 |
|
12 |
#define SEEK_WITH_BOM 1 |
13 |
#define SEEK_WITH_HOMING 2 |
14 |
|
15 |
#define SEEKING_BOM_VELOCITY -170 |
16 |
#define SEEKING_HOMING_VELOCITY -170 |
17 |
#define SEEKING_HOMING_FORWARD_VELOCITY -180 |
18 |
|
19 |
//function prototypes
|
20 |
int seek_station_with_bom(int station); |
21 |
int seek_station_with_homing_sensor(void); |
22 |
|
23 |
int seeking_state = SEEK_WITH_BOM;
|
24 |
|
25 |
void seek_station(int station) |
26 |
{ |
27 |
if (seeking_state == SEEK_WITH_BOM)
|
28 |
seeking_state = seek_station_with_bom(station); |
29 |
else
|
30 |
seeking_state = seek_station_with_homing_sensor(); |
31 |
} |
32 |
|
33 |
int old_reading = 0; |
34 |
|
35 |
int seek_station_with_bom(int station) |
36 |
{ |
37 |
int charging_loc = wl_token_get_my_sensor_reading(station);
|
38 |
if (charging_loc != old_reading)
|
39 |
{ |
40 |
RECHARGE_DEBUG_PRINT("BOM reading: ");
|
41 |
RECHARGE_DEBUG_PUTI(charging_loc); |
42 |
RECHARGE_DEBUG_PRINT("\n");
|
43 |
old_reading = charging_loc; |
44 |
} |
45 |
|
46 |
//move to charging station if charging_loc != -1
|
47 |
//at this point, wl_csloc is set to the max bom reading
|
48 |
if(charging_loc != -1) |
49 |
{ |
50 |
int desiredbom = 12; // for moving backwards |
51 |
int err = desiredbom - charging_loc;
|
52 |
if (err < -8) |
53 |
err += 16;
|
54 |
else if (err > 8) |
55 |
err -= 16;
|
56 |
int w = -err * 4; // was *2 |
57 |
int v = SEEKING_BOM_VELOCITY;
|
58 |
|
59 |
move(v, w); |
60 |
} |
61 |
|
62 |
//check if we can use homing sensor
|
63 |
int widthcount = recharge_i2c_get_homing_reading();
|
64 |
|
65 |
if(widthcount>= 4) |
66 |
return SEEK_WITH_HOMING;
|
67 |
return SEEK_WITH_BOM;
|
68 |
} |
69 |
|
70 |
int seek_station_with_homing_sensor(void) |
71 |
{ |
72 |
int widthcount = recharge_i2c_get_homing_reading();
|
73 |
int direction = homing_direction(widthcount);
|
74 |
|
75 |
switch(direction)
|
76 |
{ |
77 |
case HOMING_NONE:
|
78 |
return SEEK_WITH_BOM;
|
79 |
break;
|
80 |
case HOMING_FORWARD:
|
81 |
move(SEEKING_HOMING_FORWARD_VELOCITY, 0);
|
82 |
break;
|
83 |
case HOMING_LEFT:
|
84 |
move(SEEKING_HOMING_VELOCITY, 3);
|
85 |
break;
|
86 |
case HOMING_RIGHT:
|
87 |
move(SEEKING_HOMING_VELOCITY, -3);
|
88 |
break;
|
89 |
} |
90 |
|
91 |
return SEEK_WITH_HOMING;
|
92 |
} |
93 |
|