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