Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.18 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
    /*usb_puts("\n\rBOM reading: ");
39
        usb_puti(charging_loc);
40
    usb_puts("\n\r");*/
41
        if (charging_loc != old_reading)
42
        {
43
                RECHARGE_DEBUG_PRINT("BOM reading: ");
44
                RECHARGE_DEBUG_PUTI(charging_loc);
45
                RECHARGE_DEBUG_PRINT("\n");
46
                old_reading = charging_loc;
47
        }
48

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

    
62
                move(v, w);
63
        }
64

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

    
73
int seek_station_with_homing_sensor(void)
74
{
75
        int widthcount = recharge_i2c_get_homing_reading();
76
        int direction = homing_direction(widthcount);
77
    /*usb_puts("\n\rHoming Direction: ");
78
    usb_puti(direction);
79
    usb_puts("\n\r");*/
80

    
81
        switch(direction)
82
        {
83
                case HOMING_NONE:
84
                        return SEEK_WITH_BOM;
85
                        break;
86
                case HOMING_FORWARD:
87
                        move(SEEKING_HOMING_FORWARD_VELOCITY, 0);
88
                        break;
89
                case HOMING_LEFT:
90
                        move(SEEKING_HOMING_VELOCITY, 3);
91
                        break;
92
                case HOMING_RIGHT:
93
                        move(SEEKING_HOMING_VELOCITY, -3);
94
                        break;
95
        }
96

    
97
        return SEEK_WITH_HOMING;
98
}
99