Project

General

Profile

Statistics
| Revision:

root / branches / 16299_s10 / code / behaviors / formation_control / hive / hive.c @ 1812

History | View | Annotate | Download (4.85 KB)

1
#include <dragonfly_lib.h>
2
#include <wl_basic.h>
3
#include <encoders.h>
4
#include "hive.h"
5

    
6
int timeout = 0;
7
int sending = 0;
8
int desired_max_bom;
9
int bom_max_counter;
10

    
11

    
12
//Last used 12,13,7(BOM)
13
void set_desired_max_bom(int desired_angle)
14
{
15
    if (desired_angle >= 348 || desired_angle < 11) desired_max_bom = 0;
16
    if (desired_angle >= 11 && desired_angle < 33) desired_max_bom = 1;
17
    if (desired_angle >= 33 && desired_angle < 56) desired_max_bom = 2;
18
    if (desired_angle >= 56 && desired_angle < 78) desired_max_bom = 3;
19
    if (desired_angle >= 78 && desired_angle < 101) desired_max_bom = 4;
20
    if (desired_angle >= 101 && desired_angle < 123) desired_max_bom = 5;
21
    if (desired_angle >= 123 && desired_angle < 145) desired_max_bom = 6;
22
    if (desired_angle >= 145 && desired_angle < 167) desired_max_bom = 7;
23
    if (desired_angle >= 167 && desired_angle < 190) desired_max_bom = 8;
24
    if (desired_angle >= 190 && desired_angle < 212) desired_max_bom = 9;
25
    if (desired_angle >= 212 && desired_angle < 235) desired_max_bom = 10;
26
    if (desired_angle >= 235 && desired_angle < 257) desired_max_bom = 11;
27
    if (desired_angle >= 257 && desired_angle < 280) desired_max_bom = 12;
28
    if (desired_angle >= 280 && desired_angle < 302) desired_max_bom = 13;
29
    if (desired_angle >= 302 && desired_angle < 325) desired_max_bom = 14;
30
    if (desired_angle >= 325 && desired_angle < 348) desired_max_bom = 15;
31
}
32

    
33
void forward(int speed){                        // set the motors to this forward speed.
34
        motor_l_set(FORWARD,speed);
35
        motor_r_set(FORWARD,speed);
36
}
37
void left(int speed){                                // turn left at this speed.
38
        motor_l_set(FORWARD,speed);
39
        motor_r_set(BACKWARD,speed);
40
}
41
void right(int speed){
42
        motor_l_set(BACKWARD,speed);
43
        motor_r_set(FORWARD,speed);
44
}
45
void stop(void){                                        // could be set to motors_off(), or just use this as an alternative.
46
        motor_l_set(BACKWARD,0);                        // stop() is better - motors_off() creates a slight delay to turn them back on.
47
        motor_r_set(FORWARD,0);
48
}
49
void setforward(int spd1, int spd2){
50
        motor_l_set(FORWARD,spd1);
51
        motor_r_set(FORWARD,spd2);
52
}
53
void backward(int speed){
54
        motor_l_set(BACKWARD, speed);
55
        motor_r_set(BACKWARD, speed);
56
}
57

    
58

    
59
void switch_sending(void)
60
{
61
    if(sending)
62
    {
63
        sending = 0;
64
        bom_off();
65
    }
66
    else
67
    {
68
        sending = 1;
69
        bom_on();
70
    }
71
}
72

    
73

    
74

    
75
int get_distance(void){                                // takes an averaged reading of the front rangefinder
76
    int temp,distance,kk=5;                        // kk sets this to 5 readings.
77
    distance =0;
78
    for (int i=0; i<kk; i++){
79
        temp = range_read_distance(IR2);
80
        if (temp == -1)
81
        {
82
            //temp=0;
83
            i--;
84
        }
85
        else
86
            distance+= temp;
87
        delay_ms(3);
88
    }
89
    if (kk>0)
90
        return (int)(distance/kk);
91
    else 
92
        return 0;
93
}
94

    
95

    
96

    
97
/*
98
  Orients the robot so that it is facing the beacon (or the broadcasting BOM).
99
        
100
*/
101
void correctTurn(void)
102
{
103
    orb1_set_color(BLUE);                        // BLUE and PURPLE
104
    left(220);
105
    while(1)
106
    {
107
        int bomNum = 0;                                // bomNum is the current maximum reading
108
        bom_refresh(BOM_ALL);
109
        bomNum = bom_get_max();
110
        usb_puti(bomNum);
111
        if(bomNum == 4)                                // when it's turned the right way, stop
112
        {
113
            timeout = 0;
114
            //motor_l_set(1, 200);
115
            //motor_r_set(1, 200);
116
            break;                                // exits the while() loop to stop the method
117
        }
118
        else                                        // facing the wrong way
119
        {
120
            if(bomNum == -1)
121
            {
122
                timeout++;
123
                                
124
                if(timeout > 5000)        // if it's been looking too long, move a little bit as it turns
125
                {
126
                    motor_r_set(FORWARD, 210);
127
                    motor_l_set(BACKWARD, 190);
128
                }
129
            }
130
            else if((bomNum >= 12) || (bomNum < 4))
131
            {
132
                motor_l_set(FORWARD, 200);
133
                motor_r_set(BACKWARD, 200);
134
                timeout = 0;
135
            }
136
            else
137
            {
138
                motor_l_set(BACKWARD, 200);
139
                motor_r_set(FORWARD, 200);
140
                timeout = 0;
141
            }
142
        }
143
    }
144
    return;
145
}
146

    
147

    
148

    
149
/* 
150
   drives forward a hardcoded distance. May not be useful.
151
*/
152
void go_straight(void){
153
    forward(200);
154
    encoder_rst_dx(LEFT);
155
    encoder_rst_dx(RIGHT);
156
    delay_ms(100); 
157
    int x_left = encoder_get_x(LEFT), x_right = encoder_get_x(RIGHT);
158
    int count = 0;
159
    int d;
160
    while (count<25){                                                //count = 25 when bot6; count <12
161
        x_left = encoder_get_x(LEFT);
162
        x_right = encoder_get_x(RIGHT);
163
        d = x_right-x_left;
164
        if (d>13 || d<-13){
165
            if(d<50 && d>-50){
166
                d = round(1.0*d/4);
167
                setforward(200+d, 200-d);
168
            }
169
        }
170
        delay_ms(32);
171
        count++;
172
    }
173
}
174

    
175

    
176
/* 
177
   BLINK the given number times
178
*/
179
void blink(int num) {
180
    for(int i = 0; i<num; i++)
181
    {
182
        orb_set_color(ORB_OFF);
183
        delay_ms(350);
184
        orb_set_color(RED);
185
        delay_ms(200);
186
    }
187
    orb_set_color(ORB_OFF);
188
}
189

    
190

    
191
int main(void)
192
{
193
    /* Initialize dragonfly board */
194
    dragonfly_init(ALL_ON);
195
    /* Initialize the basic wireless library */
196
    wl_basic_init_default();
197
    /* Set the XBee channel to your assigned channel */        /* Set the XBee channel to your assigned channel */
198
    wl_set_channel(24);
199

    
200
        orb_set_color(BLUE);
201
        
202
    encoder_rst_dx(LEFT);
203
    encoder_rst_dx(RIGHT);
204

    
205
        forward(200);
206
        delay_ms(3000);
207

    
208

    
209
    orb_set_color(RED);
210
    while(1); /* END HERE */
211

    
212
    //return 0;
213
}