Statistics
| Revision:

root / trunk / code / behaviors / hunter_prey / james / main.c @ 1848

History | View | Annotate | Download (3.34 KB)

1
/*HUNTER_PREY by jamesCarroll 10/26/10*/
2
#include <dragonfly_lib.h>
3
#include <wl_basic.h>
4
#include "hunter_prey.h"
5

    
6
#define WL_CHANNEL 15 
7

    
8
/* Variables used to receive packets */
9
unsigned char* packet_data;
10
int data_length;
11

    
12
/*  Data buffer used to send packets */
13
char send_buffer[3];
14

    
15
int main(void)
16
{
17

    
18
    /* Initialize dragonfly board */
19
    dragonfly_init(ALL_ON);
20
    /* Initialize the basic wireless library */
21
    wl_basic_init_default();
22
    /* Set the XBee channel to your assigned channel */
23
    wl_set_channel(WL_CHANNEL);
24

    
25
    /* ****** CODE HERE ******* */
26
        int yes= 1;
27
        int no = 3;
28
        int speed = 200;
29
        int isHunter = yes;
30
        int t1 = 0;
31
        int t2 = 0;
32
        int timechecked = 0;
33
        int beginning = 1;
34
        send_buffer[2] = get_robotid();
35
        if(wheel()<100)
36
                isHunter=yes;
37
        else
38
                isHunter=no;
39

    
40
        while(1)
41
        {
42
                if(isHunter == yes)
43
                {
44
                        bom_off();
45
                        orb_set_color(RED);
46
                        while(1)
47
                        {
48
                                int front = 0;
49
                                front = range_read_distance(IR2);
50
                                bom_refresh(BOM_ALL);
51
                                int bomNum = 0;
52
                                bomNum = bom_get_max();
53
                                if(hunter_prey_tagged(bomNum, front))
54
                                {
55
                                        send_buffer[0] = HUNTER_PREY_ACTION_TAG;
56
                                        send_buffer[1] = get_robotid();
57
                                        wl_basic_send_global_packet(42, send_buffer, 3);
58
                                }
59
                                packet_data = wl_basic_do_default(&data_length);
60
                                if(packet_data != 0)
61
                                {
62
                                        if(data_length == 3 && packet_data[0] == HUNTER_PREY_ACTION_ACK)
63
                                        {
64
                                                if(packet_data[1] == get_robotid())
65
                                                {
66
                                                        orb_set_color(GREEN);
67
                                                        isHunter = no;
68
                                                        motor_l_set(1, 200);
69
                                                        motor_r_set(0,200);delay_ms(1000);break;
70
                                                }
71
                                                else{
72
                                                        orb_set_color(BLUE);motor_r_set(0,0);motor_l_set(0,0);delay_ms(3000);break;
73
                                                }
74
                                        }
75
                                }
76
                                bom_refresh(BOM_ALL);
77
                                bomNum = bom_get_max();
78
                                if(bomNum == 4)
79
                                {        
80
                                        motor_l_set(1, speed);
81
                                        motor_r_set(1, speed);
82
                                }
83
                                else
84
                                {
85
                                        if(bomNum == -1){}
86
                                        else if((bomNum >= 12) || (bomNum < 4))
87
                                        {
88
                                                motor_l_set(FORWARD, speed);
89
                                                motor_r_set(BACKWARD, speed);
90
                                        }
91
                                        else
92
                                        {
93
                                                motor_l_set(BACKWARD, speed);
94
                                                motor_r_set(FORWARD, speed);
95
                                        }
96
                                }
97
                        }
98
                }
99
                else
100
                {
101
                        orb_set_color(GREEN);
102
                        bom_on();
103
                        while(1)
104
                        {
105
                                int front = range_read_distance(IR2);
106
                                packet_data = wl_basic_do_default(&data_length);
107
                                if(packet_data != 0)
108
                                {        
109
                                        if(data_length == 3 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
110
                                        {
111
                                                send_buffer[0] = HUNTER_PREY_ACTION_ACK;
112
                                                send_buffer[1] = packet_data[1];
113
                                                wl_basic_send_global_packet(42, send_buffer, 3);
114
                                                isHunter=yes;
115
                                                orb_set_color(BLUE);
116
                                                motor_r_set(0,0);motor_l_set(0,0);
117
                                                bom_off();
118
                                                delay_ms(3000);
119
                                                break;
120
                                        }
121
                                }
122
                                int side1 = range_read_distance(IR1);
123
                                int side2 = range_read_distance(IR3);
124
                                t2 = rtc_get();
125
                                if(front < 200 && front > 50 && !(timechecked == 1))
126
                                {
127
                                        orb2_set_color(RED);
128
                                        timechecked = 1;
129
                                        t1 = rtc_get();
130
                                        motor_l_set(1, speed);
131
                                        motor_r_set(0, 0);
132
                                }
133
                                else if(side1 < 300 && side1 > 0 && timechecked != 1)
134
                                {
135
                                        motor_r_set(0,0);
136
                                }
137
                                else if(side2 < 300 && side2 > 0 && timechecked != 1)
138
                                {
139
                                        motor_l_set(0,0);
140
                                }
141
                                else
142
                                {
143
                                        if(t2 - t1 > 2 || beginning == 1)
144
                                        {
145
                                                orb2_set_color(BLUE);
146
                                                timechecked = 0;
147
                                                motor_l_set(1, speed);
148
                                                motor_r_set(1, speed);
149
                                                beginning = 0;
150
                                        }
151
                                }
152
                        }
153
                }
154
        }
155

    
156

    
157
    /* ****** END HERE ******* */
158

    
159
    while(1);
160

    
161
    return 0;
162

    
163
}
164