Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.18 KB)

1
#include "seeking.h"
2

    
3
#include <homing.h>
4
#include <move.h>
5

    
6
#include <wl_token_ring.h>
7

    
8
#define SEEK_WITH_BOM 1
9
#define SEEK_WITH_HOMING 2
10

    
11
int state = SEEK_WITH_BOM;
12

    
13
void seek_station(int station)
14
{
15
        if (state == SEEK_WITH_BOM)
16
                state = seek_station_with_bom(station);
17
        else
18
                state = seek_station_with_homing_sensor();
19
}
20

    
21
int seek_station_with_bom(int station)
22
{
23
        int charging_loc = wl_token_get_my_sensor_reading(station);
24

    
25
        //move to charging station if charging_loc != -1
26
        //at this point, wl_csloc is set to the max bom reading
27
        if(charging_loc != -1)
28
        {
29
                int desiredbom = 12; // for moving backwards
30
                int err = desiredbom - charging_loc;
31
                if (err < -8)
32
                        err += 16;
33
                else if (err > 8)
34
                        err -= 16;
35
                int w = -err * 2;
36
                int v = SEEKING_BOM_VELOCITY;
37
                //if the error in the BOM is 'large', slow v down
38
                /*if( abs_int(err) > 2)
39
                  v = SEEKING_BOM_VELOCITY_TURN;
40
                */
41
                move_avoid(v, w, 5);
42
        }
43

    
44
        //TODO: redesign i2c to make this cleaner and not use globals
45
        if(i2c_homing_sensor_data_dirty == I2C_DATA_DIRTY)
46
                return SEEK_WITH_BOM;
47
        
48
        //data is fresh, use it
49
        int widthcount = i2c_homing_sensor_data;
50
        i2c_homing_sensor_data_dirty = I2C_DATA_DIRTY;
51
        
52
        if(widthcount>= 4)
53
                return SEEK_WITH_HOMING;
54
        return SEEK_WITH_BOM;
55
}
56

    
57
int seek_station_with_homing_sensor(void)
58
{
59
        //if you have no new data, just continue on our current path
60
        if(i2c_homing_sensor_data_dirty == I2C_DATA_DIRTY)
61
                return SEEK_WITH_HOMING;
62
        
63
        //TODO: create function in homing.h to return LEFT, FORWARD, RIGHT, or NONE
64
        int widthcount = i2c_homing_sensor_data;
65
        i2c_homing_sensor_data_dirty = I2C_DATA_DIRTY;
66

    
67
        if (widthcount < LEFT_LOW) //can't use beacon, switch to BOM
68
                return SEEK_WITH_BOM;
69
        else if(widthcount >= CENTER_LOW)
70
                move(SEEKING_HOMING_VELOCITY, 0);
71
        else
72
        {
73
                if(widthcount >= LEFT_LOW && widthcount <= LEFT_HIGH)
74
                        move(SEEKING_HOMING_VELOCITY, (widthcount) / 3);
75
                else if (widthcount >= RIGHT_LOW && widthcount <= RIGHT_HIGH)
76
                        move(SEEKING_HOMING_VELOCITY, -(widthcount) / 3);
77
                else
78
                {
79
                        WL_DEBUG_PRINT("Unexpected homing sensor value of ");
80
                        WL_DEBUG_PUTI(widthcount);
81
                        WL_DEBUG_PRINT(".\n");
82
                }
83
        }
84
        
85
        return SEEK_WITH_HOMING;
86
}
87