Project

General

Profile

Statistics
| Revision:

root / trunk / code / behaviors / hunter_prey / ref / refBot.c @ 1850

History | View | Annotate | Download (4.12 KB)

1
/* Referee Bot for Hunter-Prey
2
 * Ben Wasserman
3
 * Robotics Club Colony Project 2010
4
 */
5

    
6
#include <dragonfly_lib.h>
7
#include <wl_basic.h>
8
#include "hunter_prey.h"
9

    
10
int main(void){
11
        
12
        dragonfly_init(ALL_ON);
13
        xbee_init();
14
        wl_basic_init_default();
15
        wl_set_channel(15);
16
        rtc_init(SIXTEENTH_SECOND, NULL);
17
        
18
        int currentPrey, length = 0, index, lastTime = 0, lastAck = 0, preyTime[20], timeOfLastTag[20];
19
        unsigned char *packet, lastTags[5], tags[20], acks[20], timesAsPrey[20];
20
        float iAcks, iTags, ratio;
21
        
22
        for(currentPrey=19; currentPrey>0; currentPrey--){
23
                preyTime[currentPrey] = 0;
24
                tags[currentPrey] = 0;
25
                acks[currentPrey] = 0;
26
                timesAsPrey[currentPrey] = 0;
27
                timeOfLastTag[currentPrey] = 0;
28
        }
29
        lastTags[0] = 0;
30
        lastTags[1] = 0;
31
        lastTags[2] = 0;
32
        lastTags[3] = 0;
33
        lastTags[4] = 0;
34
        
35
        delay_ms(1000);
36
        
37
        while(1){
38
                length = 0;
39
                if(rtc_get()%2){
40
                        orb1_set_color(GREEN);
41
                }else{
42
                        orb1_set_color(BLUE);
43
                }
44
                delay_ms(10);
45
                packet = wl_basic_do_default(&length);
46
                switch(length){
47
                        case 0:
48
                                delay_ms(10);
49
                                orb2_set_color(BLUE);
50
                                break;
51
                        case 1:
52
                                usb_puts("\r\nGot packet of size 1: ");
53
                                usb_puti(packet[0]);
54
                                orb2_set_color(ORANGE);
55
                                break;
56
                        case 2:
57
                                usb_puts("\r\nGot packet of size 2: ");
58
                                usb_puti(packet[0]);
59
                                usb_putc('\t');
60
                                usb_puti(packet[0]);
61
                                orb2_set_color(ORANGE);
62
                                break;
63
                        case 3:
64
                                usb_puts("\r\nGot packet of size 3: Action: ");
65
                                switch(packet[0]){
66
                                        case HUNTER_PREY_ACTION_TAG:
67
                                                usb_puts("TAG");
68
                                                lastTags[0] = lastTags[1];
69
                                                lastTags[1] = lastTags[2];
70
                                                lastTags[2] = lastTags[3];
71
                                                lastTags[3] = lastTags[4];
72
                                                lastTags[4] = packet[1];
73
                                                if(packet[1] != currentPrey && (rtc_get() - timeOfLastTag[packet[1]]) > 16 && (rtc_get() - lastAck) > 48){
74
                                                        tags[packet[1]]++;
75
                                                }else{
76
                                                        usb_puts(" CHEAT");
77
                                                }
78
                                                timeOfLastTag[packet[1]] = rtc_get();
79
                                                orb2_set_color(PURPLE);
80
                                                break;
81
                                        case HUNTER_PREY_ACTION_ACK:
82
                                                usb_puts("ACK");
83
                                                if(lastTags[0]==packet[1] || lastTags[1]==packet[1] || lastTags[2]==packet[1] || lastTags[3]==packet[1] || lastTags[4]==packet[1]){
84
                                                        acks[packet[1]]++;
85
                                                        timesAsPrey[packet[2]]++;
86
                                                        currentPrey = packet[1];
87
                                                        lastAck = rtc_get();
88
                                                        usb_puts(" New Prey: ");
89
                                                        usb_puti(currentPrey);
90
                                                        orb2_set_color(GREEN);
91
                                                }else{
92
                                                        usb_puts(" Spam!");
93
                                                        orb2_set_color(ORANGE);
94
                                                }
95
                                                break;
96
                                        default:
97
                                                usb_puts("Unknown: ");
98
                                                usb_puti(packet[0]);
99
                                                orb2_set_color(ORANGE);
100
                                                break;
101
                                }
102
                                usb_puts(" Tagging Bot: ");
103
                                usb_puti(packet[1]);
104
                                usb_puts(" Sending Bot: ");
105
                                usb_puti(packet[2]);
106
                                if(packet[0] == HUNTER_PREY_ACTION_TAG && packet[1] != packet[2]){
107
                                        usb_puts(" Tag error: mismatched IDs");
108
                                }
109
                                break;
110
                        default:
111
                                usb_puts("\nOversized packet. Contents: ");
112
                                for(index = 0; index < length; index++){
113
                                        usb_puti(packet[index]);
114
                                        usb_putc(' ');
115
                                        orb2_set_color(ORANGE);
116
                                }
117
                }
118
                if(rtc_get() - 4 > lastTime){
119
                        preyTime[currentPrey]++;
120
                        lastTime = rtc_get();
121
                        if(timesAsPrey[currentPrey] == 0){
122
                                timesAsPrey[currentPrey] = 1;
123
                        }
124
                }
125
                if(button1_read()){
126
                        usb_puts("\r\n!!Game Stats!! Game time: ");
127
                        usb_puti(rtc_get() / 16);
128
                        usb_puts("\r\nBot\tTags\tAcks\tATR\tP Time\t#aP\tTp#\r\n");
129
                        for(index = 1; index < 20; index++){
130
                                if(tags[index]){
131
                                        usb_puti(index);
132
                                        if(index == currentPrey){
133
                                                usb_putc('*');
134
                                        }
135
                                        usb_putc('\t');
136
                                        usb_puti(tags[index]);
137
                                        usb_putc('\t');
138
                                        usb_puti(acks[index]);
139
                                        usb_putc('\t');
140
                                        iAcks = acks[index];
141
                                        iTags = tags[index];
142
                                        iAcks *= 1000;
143
                                        iTags *= 1000;
144
                                        ratio = iAcks / iTags;
145
                                        usb_puti(ratio*1000);
146
                                        usb_putc('\t');
147
                                        usb_puti(preyTime[index]/4);
148
                                        usb_putc('\t');
149
                                        usb_puti(timesAsPrey[index]);
150
                                        usb_putc('\t');
151
                                        if(timesAsPrey[index]){
152
                                                usb_puti((preyTime[index] / 4) / timesAsPrey[index]);
153
                                        }else{
154
                                                usb_puts("NaN");
155
                                        }
156
                                        usb_puts("\r\n");
157
                                }
158
                        }
159
                }
160
        }
161
        
162
        while(1);
163
        
164
        return 0;
165
}