Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / seeking.c @ 138

History | View | Annotate | Download (2.37 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

    
9
#include <wl_token_ring.h>
10

    
11
#define SEEK_WITH_BOM 1
12
#define SEEK_WITH_HOMING 2
13

    
14
#define SEEKING_BOM_VELOCITY -170
15
#define SEEKING_HOMING_VELOCITY -160
16

    
17
// values for homing sensor readings
18
#define LEFT_LOW 6
19
#define LEFT_HIGH 7
20
#define CENTER_LOW 18
21
#define CENTER_HIGH 19
22
#define RIGHT_LOW 12
23
#define RIGHT_HIGH 13
24

    
25
//function prototypes
26
int seek_station_with_bom(int station);
27
int seek_station_with_homing_sensor(void);
28

    
29
int seeking_state = SEEK_WITH_BOM;
30

    
31
void seek_station(int station)
32
{
33
        if (seeking_state == SEEK_WITH_BOM)
34
                seeking_state = seek_station_with_bom(station);
35
        else
36
                seeking_state = seek_station_with_homing_sensor();
37
}
38

    
39
int old_reading = 0;
40

    
41
int seek_station_with_bom(int station)
42
{
43
        int charging_loc = wl_token_get_my_sensor_reading(station);
44
        if (charging_loc != old_reading)
45
        {
46
                RECHARGE_DEBUG_PRINT("BOM reading: ");
47
                RECHARGE_DEBUG_PUTI(charging_loc);
48
                RECHARGE_DEBUG_PRINT("\n");
49
                old_reading = charging_loc;
50
        }
51

    
52
        //move to charging station if charging_loc != -1
53
        //at this point, wl_csloc is set to the max bom reading
54
        if(charging_loc != -1)
55
        {
56
                int desiredbom = 12; // for moving backwards
57
                int err = desiredbom - charging_loc;
58
                if (err < -8)
59
                        err += 16;
60
                else if (err > 8)
61
                        err -= 16;
62
                int w = -err * 2;
63
                int v = SEEKING_BOM_VELOCITY;
64

    
65
                move(v, w);
66
        }
67

    
68
        //check if we can use homing sensor
69
        int widthcount = recharge_i2c_get_homing_reading();
70
        
71
        if(widthcount>= 4)
72
                return SEEK_WITH_HOMING;
73
        return SEEK_WITH_BOM;
74
}
75

    
76
int seek_station_with_homing_sensor(void)
77
{
78
        //TODO: create function in homing.h to return LEFT, FORWARD, RIGHT, or NONE
79
        int widthcount = recharge_i2c_get_homing_reading();
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