root / trunk / code / behaviors / formation_control / push_pull / main.c @ 1508
History | View | Annotate | Download (3.22 KB)
1 |
#include <dragonfly_lib.h> |
---|---|
2 |
#include <wl_basic.h> |
3 |
#include <stdint.h> |
4 |
|
5 |
/* Struct for storing vector components */
|
6 |
typedef struct { |
7 |
uint8_t x; |
8 |
uint8_t y; |
9 |
} Vector; |
10 |
|
11 |
|
12 |
/* Function Prototypes */
|
13 |
static const Vector get_bom_vector(); |
14 |
|
15 |
|
16 |
/*******************************
|
17 |
* BOM Vector Component Tables *
|
18 |
*******************************/
|
19 |
|
20 |
/*
|
21 |
* The x component of each BOM detector (indexed from 0 to 15)
|
22 |
* was calculated using the following formula:
|
23 |
*
|
24 |
* x_comp[i] = round(100 * cos ( 2 * pi / 16 * i) )
|
25 |
*
|
26 |
* If the BOM detectors were superimposed onto a 2 dimensional Cartesian space,
|
27 |
* this effectively calculates the x component of the emitter vector where
|
28 |
* emitter 0 corresponds to an angle of 0 radians, 4 -> pi/2, 8 -> pi, ect.
|
29 |
*/
|
30 |
static const signed int x_comp[16] = { |
31 |
100,
|
32 |
92,
|
33 |
71,
|
34 |
38,
|
35 |
0,
|
36 |
-38,
|
37 |
-71,
|
38 |
-92,
|
39 |
-100,
|
40 |
-92,
|
41 |
-71,
|
42 |
-38,
|
43 |
0,
|
44 |
38,
|
45 |
71,
|
46 |
92
|
47 |
}; |
48 |
|
49 |
|
50 |
/*
|
51 |
* The y component of each BOM detector (indexed from 0 to 15)
|
52 |
* was calculated using the following formula:
|
53 |
*
|
54 |
* y_comp[i] = round(100 * sin ( 2 * pi / 16 * i) )
|
55 |
*
|
56 |
* If the BOM detectors were superimposed onto a 2 dimensional Cartesian space,
|
57 |
* this effectively calculates the y component of the emitter vector where
|
58 |
* emitter 0 corresponds to an angle of 0 radians, 4 -> pi/2, 8 -> pi, ect.
|
59 |
*/
|
60 |
static signed int y_comp[16] = { |
61 |
0,
|
62 |
38,
|
63 |
71,
|
64 |
92,
|
65 |
100,
|
66 |
92,
|
67 |
71,
|
68 |
38,
|
69 |
0,
|
70 |
-38,
|
71 |
-71,
|
72 |
-92,
|
73 |
-100,
|
74 |
-92,
|
75 |
-71,
|
76 |
-38
|
77 |
}; |
78 |
|
79 |
|
80 |
|
81 |
int main (void) { |
82 |
|
83 |
/* Store current BOM readings and use them as a weighting factor */
|
84 |
int intensity[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
85 |
|
86 |
/* Arrays for storing the weighted x ("Rightness") and y ("Forwardness")
|
87 |
* components. Calculated by multiplying the intensity by the x and y
|
88 |
* component respectively (x and y components are stored in the tables
|
89 |
* above). */
|
90 |
int weighted_x_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
91 |
int weighted_y_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
92 |
|
93 |
/* Accumulators to sum up the net x ("Rightness") and y ("Forwardness")
|
94 |
* components for the entire robot. */
|
95 |
long net_x_comp = 0; |
96 |
long net_y_comp = 0; |
97 |
|
98 |
/* Variables used to normalize the net component values */
|
99 |
int total_intensity = 0; |
100 |
int normalized_net_x_comp = 0; |
101 |
int normalized_net_y_comp = 0; |
102 |
|
103 |
int i = 0; |
104 |
|
105 |
dragonfly_init(ALL_ON); |
106 |
xbee_init(); |
107 |
encoders_init(); |
108 |
|
109 |
orbs_set_color(BLUE, GREEN); |
110 |
delay_ms(1000);
|
111 |
orbs_set_color(GREEN, BLUE); |
112 |
delay_ms(1000);
|
113 |
orbs_set_color(RED, RED); |
114 |
|
115 |
while (1) { |
116 |
|
117 |
/* Make sure to clear our accumulators */
|
118 |
net_x_comp = 0;
|
119 |
net_y_comp = 0;
|
120 |
total_intensity = 0;
|
121 |
|
122 |
bom_refresh(BOM_ALL); |
123 |
for (i = 0; i < 16; i++) { |
124 |
/* BOM intensity is actually measured as more intense = closer to 0 */
|
125 |
intensity[i] = 255 - bom_get(i);
|
126 |
weighted_x_comp[i] = intensity[i] * x_comp[i]; |
127 |
weighted_y_comp[i] = intensity[i] * y_comp[i]; |
128 |
net_x_comp += weighted_x_comp[i]; |
129 |
net_y_comp += weighted_y_comp[i]; |
130 |
total_intensity += intensity[i]; |
131 |
} |
132 |
|
133 |
if (total_intensity > 0) { |
134 |
normalized_net_x_comp = net_x_comp / total_intensity; |
135 |
normalized_net_y_comp = net_y_comp / total_intensity; |
136 |
} |
137 |
|
138 |
usb_puts("x: ");
|
139 |
usb_puti(normalized_net_x_comp); |
140 |
usb_puts("\ty: ");
|
141 |
usb_puti(normalized_net_y_comp); |
142 |
usb_puts("\n");
|
143 |
|
144 |
delay_ms(50);
|
145 |
|
146 |
} |
147 |
|
148 |
while(1); |
149 |
|
150 |
} |
151 |
|
152 |
|
153 |
static Vector get_bom_vector() {
|
154 |
|
155 |
} |