root / trunk / code / behaviors / formation_control / push_pull / push_pull.c @ 1540
History | View | Annotate | Download (1.93 KB)
1 |
#include <stdint.h> |
---|---|
2 |
#include <dragonfly_lib.h> |
3 |
#include <wl_basic.h> |
4 |
#include <stdlib.h> |
5 |
|
6 |
|
7 |
int main (void) { |
8 |
|
9 |
/* Store current BOM readings and use them as a weighting factor */
|
10 |
uint8_t intensity[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
11 |
|
12 |
/* Arrays for storing the weighted x ("Rightness") and y ("Forwardness")
|
13 |
* components. Calculated by multiplying the intensity by the x and y
|
14 |
* component respectively (x and y components are stored in the tables
|
15 |
* above). */
|
16 |
int weighted_x_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
17 |
int weighted_y_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
18 |
|
19 |
/* Accumulators to sum up the net x ("Rightness") and y ("Forwardness")
|
20 |
* components for the entire robot. */
|
21 |
long net_x_comp = 0; |
22 |
long net_y_comp = 0; |
23 |
|
24 |
/* Variables used to normalize the net component values */
|
25 |
int total_intensity = 0; |
26 |
int normalized_net_x_comp = 0; |
27 |
int normalized_net_y_comp = 0; |
28 |
|
29 |
int i = 0; |
30 |
|
31 |
dragonfly_init(ALL_ON); |
32 |
xbee_init(); |
33 |
encoders_init(); |
34 |
|
35 |
orbs_set_color(BLUE, GREEN); |
36 |
delay_ms(1000);
|
37 |
orbs_set_color(GREEN, BLUE); |
38 |
delay_ms(1000);
|
39 |
orbs_set_color(RED, RED); |
40 |
|
41 |
while (1) { |
42 |
|
43 |
/* Make sure to clear our accumulators */
|
44 |
net_x_comp = 0;
|
45 |
net_y_comp = 0;
|
46 |
total_intensity = 0;
|
47 |
|
48 |
bom_print_usb(NULL);
|
49 |
|
50 |
bom_refresh(BOM_ALL); |
51 |
for (i = 0; i < 16; i++) { |
52 |
/* BOM intensity is actually measured as more intense = closer to 0 */
|
53 |
intensity[i] = 255 - bom_get(i);
|
54 |
weighted_x_comp[i] = intensity[i] * x_comp[i]; |
55 |
weighted_y_comp[i] = intensity[i] * y_comp[i]; |
56 |
net_x_comp += weighted_x_comp[i]; |
57 |
net_y_comp += weighted_y_comp[i]; |
58 |
total_intensity += intensity[i]; |
59 |
} |
60 |
|
61 |
if (total_intensity > 0) { |
62 |
normalized_net_x_comp = net_x_comp / total_intensity; |
63 |
normalized_net_y_comp = net_y_comp / total_intensity; |
64 |
} |
65 |
|
66 |
usb_puts("x: ");
|
67 |
usb_puti(normalized_net_x_comp); |
68 |
usb_puts("\ty: ");
|
69 |
usb_puti(normalized_net_y_comp); |
70 |
usb_puts("\n");
|
71 |
|
72 |
delay_ms(50);
|
73 |
|
74 |
} |
75 |
|
76 |
while(1); |
77 |
|
78 |
} |