Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.65 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
                //if the error in the BOM is 'large', slow v down
65
                /*if( abs_int(err) > 2)
66
                  v = SEEKING_BOM_VELOCITY_TURN;
67
                */
68
                //TODO: uncomment this line
69
                move(v, w);
70
        }
71

    
72
        //data is fresh, use it
73
        //TODO: uncomment to use homing sensor
74
        int widthcount = recharge_i2c_get_homing_reading();
75
        
76
        if(widthcount>= 4)
77
                return SEEK_WITH_HOMING;
78
        return SEEK_WITH_BOM;
79
}
80

    
81
int seek_station_with_homing_sensor(void)
82
{
83
        //TODO: create function in homing.h to return LEFT, FORWARD, RIGHT, or NONE
84
        int widthcount = recharge_i2c_get_homing_reading();
85
        RECHARGE_DEBUG_PRINT("Homing sensor: ");
86
        RECHARGE_DEBUG_PUTI(widthcount);
87
        RECHARGE_DEBUG_PRINT("\n");
88

    
89
        if (widthcount < LEFT_LOW) //can't use beacon, switch to BOM
90
                return SEEK_WITH_BOM;
91
        else if(widthcount >= CENTER_LOW)
92
                move(SEEKING_HOMING_VELOCITY, 0);
93
        else
94
        {
95
                if(widthcount >= LEFT_LOW && widthcount <= LEFT_HIGH)
96
                        move(SEEKING_HOMING_VELOCITY, (widthcount) / 3);
97
                else if (widthcount >= RIGHT_LOW && widthcount <= RIGHT_HIGH)
98
                        move(SEEKING_HOMING_VELOCITY, -(widthcount) / 3);
99
                else
100
                {
101
                        RECHARGE_DEBUG_PRINT("Unexpected homing sensor value of ");
102
                        RECHARGE_DEBUG_PUTI(widthcount);
103
                        RECHARGE_DEBUG_PRINT(".\n");
104
                }
105
        }
106
        
107
        return SEEK_WITH_HOMING;
108
}
109