Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / object_manipulation / obj_detect_swarm / driver.c @ 750

History | View | Annotate | Download (3.9 KB)

1
/** driver for bfs swarming
2
        execute bfs behavior
3
*/
4

    
5
#define COLOR_DETECT // comment out if color detect hardware is unavailable
6

    
7
#include <dragonfly_lib.h>
8
#include <wireless.h>
9
#include <wl_token_ring.h>
10
#include <dio.h>
11
#include <analog.h>
12
#include "bfs_fsm.h"
13
#include "smart_run_around_fsm.h"
14

    
15

    
16
//A6
17
#define RED_LED PIN_ADC4
18
//A8
19
#define GREEN_LED PIN_ADC5
20
//A7
21
#define BLUE_LED PIN_ADC6
22

    
23

    
24
// COLOR_NOTHING {255,255,255}
25
#define COLOR_WALL 2 // ratio of blue/green
26
#define COLOR_GREEN 1
27

    
28

    
29
#define SWARM_GROUP   15
30
#define SWARM_ACTION  16
31

    
32

    
33
/* swarm wireless packet handlers */
34
void swarm_receive (char type, int source, unsigned char* packet,
35
                          int length);
36
PacketGroupHandler swarmHandler = {SWARM_GROUP, NULL, NULL, 
37
                                     &swarm_receive, NULL};
38

    
39

    
40
int main(void) {
41
  // enable everything
42
  dragonfly_init(ALL_ON);
43
  orb_enable();
44
  orb_init();
45
  orb_set_color(PURPLE);
46
  wl_init();
47
  wl_set_channel(0xF); // set wireless channel
48
  
49
  // register swarm packet
50
  wl_register_packet_group(&swarmHandler);
51
  
52
  wl_token_ring_register();
53
  wl_token_ring_join(); // join token ring
54
  usb_init();
55
  
56
  usb_puts("start\n\r");
57
  
58
  
59
#ifdef COLOR_DETECT
60
  /* set up color detect */
61
  int rgb[3] = {0,0,0};
62
        
63
        //long int distance_red;
64
        //long int distance_green;
65
        //long int distance_blue;
66
  
67
  int ratio_gb;
68
  
69
        //int blue[3] = {255,245,249};
70
        //int red[3] = {190,122,126};
71
        //int green[3] = {118, 80, 86};
72

    
73
  analog_init(ADC_START);
74
  
75
#else
76

    
77
  run_around_init();
78

    
79
#endif
80

    
81
  int object = 0;
82
        
83

    
84

    
85
  while(1) {
86
    wl_do(); // do wireless
87
    
88
#ifdef COLOR_DETECT
89
    // need color sensor code here
90
    digital_output(BLUE_LED, 1);
91
                
92
                delay_ms(5);
93
                analog_stop_loop();
94
                rgb[2] = analog8(AN9);
95
                analog_start_loop();
96
                
97
                digital_output(BLUE_LED,0);
98
                digital_output(RED_LED, 1);
99
                delay_ms(5);
100
                
101
                analog_stop_loop();
102
                rgb[0] = analog8(AN9);
103
                analog_start_loop();
104
                
105
                digital_output(RED_LED, 0);
106
                digital_output(GREEN_LED,1);
107
                delay_ms(5);
108
                
109
                analog_stop_loop();
110
                rgb[1] = analog8(AN9);
111
                analog_start_loop();
112
                
113
                digital_output(GREEN_LED,0);
114
    
115
    ratio_gb = rgb[1] - rgb[2];
116
    if (ratio_gb < -20)
117
      ratio_gb = 2;
118
    else if (ratio_gb > 20)
119
      ratio_gb = 0;
120
    else
121
      ratio_gb = 1;
122
    
123
    if (rgb[0] > 200 && rgb[1] > 200 && rgb[2] > 200) {
124
      // we think this is open area
125
      move(170,0);
126
    } else if (ratio_gb == COLOR_WALL) {
127
      // we think this is a wall, so turn away
128
      move(170,90);
129
      delay_ms(400);
130
      move(170,0);
131
    } else if (ratio_gb == COLOR_GREEN) {
132
      // we think this is a green object, so swarm
133
      object = 1;
134
      move(0,0);
135
    }
136
    else
137
      move(170,0); // just go forth
138
                
139
                /*distance_red = 1;
140
                distance_green = rgb[1] - rgb[0];
141
                distance_blue = rgb[2] - rgb[0];
142
                
143
                distance_blue = distance_blue * rgb[0]/255;
144
                distance_green = distance_green * rgb[0]/255;*/
145
                
146
    usb_puts("R:");
147
                usb_puti(rgb[0]);
148
                usb_puts("\tG:");
149
                usb_puti(rgb[1]);
150
                usb_puts("\tB:");
151
                usb_puti(rgb[2]);
152
                usb_puts("\t\tratio_gb:");
153
    usb_puti(ratio_gb);
154
    usb_puts("\r\n");
155
    
156
#else
157
    // do smart run around until find object
158
    run_around_FSM();
159
    //move(170,0);
160
#endif  
161
    
162
    /*usb_puts("R:");
163
                usb_puti(distance_red);
164
                usb_puts("\tG:");
165
                usb_puti(distance_green);
166
                usb_puts("\tB:");
167
                usb_puti(distance_blue);
168
                usb_puts("\r\n");*/
169
    
170
    if (object || button2_read()) { // if we find the object 
171
      // send packet to call for swarm on object
172
      wl_send_global_packet(SWARM_GROUP,SWARM_ACTION,
173
        NULL,0,0); 
174
      move(0,0); // stop
175
      break; // break loop
176
    } 
177
  }
178
  
179
  // object manip goes here
180
  while(1) {
181
    wl_do();
182
  }
183

    
184
  return 0;
185
}
186

    
187

    
188
void swarm_receive (char type, int source, unsigned char* packet,
189
                          int length)
190
{
191
  switch (type)
192
  {
193
    case SWARM_ACTION:
194
      // do swarm action
195
      bfs_init(source); // set robot_id to find
196
      while(1) {
197
        bfs_fsm(); // do bfs
198
      }
199
  }
200
}