forked from anidev/612-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision_alt.cpp
346 lines (317 loc) · 11.7 KB
/
vision_alt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* vision_alt.cpp
*
* Copyright (c) 2011, 2012 Chantilly Robotics <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* An alternative implementation of image processing
*/
#include "vision_alt.h"
#include "ranges.h"
#include "update.h"
#include "ports.h"
#include "visionalg.h"
#include "612.h"
#include "vision/vision_processing.h"
#include <nivision.h>
#include <Vision/Threshold.h>
#include <Vision/AxisCamera.h>
#include <Vision/HSLImage.h>
#include <Vision/ColorImage.h>
#include <Vision/BinaryImage.h>
#include <Vision2009/VisionAPI.h>
#include <Timer.h>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
enum VISION_ALGORITHM {
TRIGONOMETRIC,
REGRESSION
};
//note: only REGRESSION will work if the target detection doesn't work well :/
const VISION_ALGORITHM ALGORITHM = REGRESSION;
//note: if VISION_ALGORITHM_ADHOC is enabled then REGRESSION will be selected
//regardless of what you put here.
/* Remember that y increases as you go DOWN the image!! */
const double robot_height = 31.0/12;
#ifdef VISION_ALT_HEURISTIC
//heights in feet:
//TODO: add in the height to the center of the particle!
target bottom_basket( 28.0/12 - robot_height );
target midleft_basket( 61.0/12 - robot_height );
target midright_basket( 61.0/12 - robot_height );
target top_basket( 98.0/12 - robot_height );
#elif defined VISION_ALT_ADHOC
const unsigned numtargets = 4;
target target_arr[numtargets];
#endif
void output_debug_info_target(const char * n, const target& t) {
if (t.valid()) {
std::printf("Target %s found:\n"
"\tdistance = %g\n"
"\tx_offset = %i\n"
"\theight = %g\n",
n, t.distance(), t.x_offset(), t.height());
//
}
else {
std::printf("Target %s not found.\n", n);
}
}
void output_debug_info() {
/*
#ifdef VISION_ALT_HEURISTIC
output_debug_info_target("bottom_basket", bottom_basket);
output_debug_info_target("midleft_basket", midleft_basket);
output_debug_info_target("midright_basket", midright_basket);
output_debug_info_target("top_basket", top_basket);
#elif defined VISION_ALT_ADHOC
for (unsigned i = 0; i < numtargets; i++) {
output_debug_info_target("generic", target_arr[i]);
}
#endif
*/
}
#ifdef VISION_ALT_HEURISTIC
void target::id_two_targets(report_vector * reports) {
//guess that it's bottom basket and one side.
//TODO: Probably should be a little more intelligent about this too
if (reports->front().center_mass_y > reports->back().center_mass_y) {
//front() is bottom basket
bottom_basket.update_data_with_report(reports->front());
if (reports->front().center_mass_x < reports->back().center_mass_x) {
//back() is mid-right basket
midright_basket.update_data_with_report(reports->back());
}
else {
//back() is mid-left basket
midleft_basket.update_data_with_report(reports->back());
}
}
else {
//back() is bottom basket
bottom_basket.update_data_with_report(reports->back());
if (reports->front().center_mass_x < reports->back().center_mass_x) {
//front() is mid-left basket
midleft_basket.update_data_with_report(reports->front());
}
else {
//front() is mid-right basket
midright_basket.update_data_with_report(reports->back());
}
}
}
void target::id_four_targets(report_vector * reports) {
//all baskets on screen.
//pick lowest for bottom, highest for top, furthest left for midleft,
//and the last on is midright.
report_vector::iterator chosen;
double cmpval = 0.0; //further up than everything
//find bottom
for (report_vector::iterator i = reports->begin(); i < reports->end(); ++i) {
if (i->center_mass_y > cmpval) {
chosen = i;
}
}
bottom_basket.update_data_with_report(*chosen);
reports->erase(chosen);
//find top
cmpval = (double)RESOLUTION().Y(); //further down than everything
for (report_vector::iterator i = reports->begin(); i < reports->end(); ++i) {
if (i->center_mass_y < cmpval) {
chosen = i;
}
}
top_basket.update_data_with_report(*chosen);
reports->erase(chosen);
//find left
cmpval = (double)RESOLUTION().X();
for (report_vector::iterator i = reports->begin(); i < reports->end(); ++i) {
if (i->center_mass_x < cmpval) {
chosen = i;
}
}
midleft_basket.update_data_with_report(*chosen);
reports->erase(chosen);
//should only be one element left in vector
//right
midright_basket.update_data_with_report(*(reports->begin()));
}
void target::id_three_targets(report_vector * reports) {
//three on screen. Check if a line through two of the baskets is approx
//parallel to an axis. If it's the x axis, they're the mid-level
//baskets. If it's the y axis, they're the top/bottom baskets
int x0_1, x1_2, x0_2;
x0_1 = std::abs(reports->at(0).center_mass_x - reports->at(1).center_mass_x);
x1_2 = std::abs(reports->at(1).center_mass_x - reports->at(2).center_mass_x);
x0_2 = std::abs(reports->at(0).center_mass_x - reports->at(2).center_mass_x);
int y0_1, y1_2, y0_2;
y0_1 = std::abs(reports->at(0).center_mass_y - reports->at(1).center_mass_y);
y1_2 = std::abs(reports->at(1).center_mass_y - reports->at(2).center_mass_y);
y0_2 = std::abs(reports->at(0).center_mass_y - reports->at(2).center_mass_y);
int xmin = std::min(std::min(x0_1, x0_2), x1_2);
int ymin = std::min(std::min(y0_1, y0_2), x1_2);
if (xmin < ymin) {
//vertically aligned, therefore two of them are top and bottom
if (xmin == x0_1) {
three_targets_alignx(reports->at(0), reports->at(1), reports->at(2));
}
else if (xmin == x0_2) {
three_targets_alignx(reports->at(0), reports->at(2), reports->at(1));
}
else if (xmin == x1_2) {
three_targets_alignx(reports->at(1), reports->at(2), reports->at(0));
}
}
else {
//horizontally aligned, therefore two of them are left and right
if (ymin == y0_1) {
three_targets_aligny(reports->at(0), reports->at(1), reports->at(2));
}
else if (ymin == y0_2) {
three_targets_aligny(reports->at(0), reports->at(2), reports->at(1));
}
else if (ymin == y1_2) {
three_targets_aligny(reports->at(1), reports->at(2), reports->at(0));
}
}
}
void target::three_targets_alignx(const ParticleAnalysisReport& aligna,
const ParticleAnalysisReport& alignb,
const ParticleAnalysisReport& unalign)
{
//aligna and alignb are vertically aligned
if (aligna.center_mass_y > alignb.center_mass_y) {
//aligna is bottom, and alignb is top
bottom_basket.update_data_with_report(aligna);
top_basket.update_data_with_report(alignb);
}
else {
//alignb is bottom, and aligna is top
bottom_basket.update_data_with_report(alignb);
top_basket.update_data_with_report(aligna);
}
if (unalign.center_mass_x < (aligna.center_mass_x + alignb.center_mass_x) / 2) {
//unalign is midleft
midleft_basket.update_data_with_report(unalign);
}
else {
//unalign is midright
midright_basket.update_data_with_report(unalign);
}
}
void target::three_targets_aligny(const ParticleAnalysisReport& aligna,
const ParticleAnalysisReport& alignb,
const ParticleAnalysisReport& unalign)
{
//aligna and alignb are horizontally aligned
if (aligna.center_mass_x < alignb.center_mass_x) {
//aligna is midleft, and alignb is midright
midleft_basket.update_data_with_report(aligna);
midright_basket.update_data_with_report(alignb);
}
else {
//alignb is midleft, and aligna is midright
midleft_basket.update_data_with_report(alignb);
midright_basket.update_data_with_report(aligna);
}
if (unalign.center_mass_y < (aligna.center_mass_y + alignb.center_mass_y) / 2) {
//unalign is top
top_basket.update_data_with_report(unalign);
}
else {
//unalign is bottom
bottom_basket.update_data_with_report(unalign);
}
}
#endif
void target::id_and_process(report_vector * reports) {
#ifdef VISION_ALT_HEURISTIC
//set all valid flags to false for now. Call update_data_with_report()
//to make it valid later.
bottom_basket.m_valid = false;
midleft_basket.m_valid = false;
midright_basket.m_valid = false;
top_basket.m_valid = false;
if (reports->size() == 0) {
//no particles
}
else if (reports->size() == 1) {
//guess top basket so we'll shoot at something known
//TODO: Probably should be a little more intelligent about this
//check based on position in image? esp y pos cause that doesn't change
top_basket.update_data_with_report(reports->front());
}
else if (reports->size() == 2) {
id_two_targets(reports);
}
else if (reports->size() == 3) {
id_three_targets(reports);
}
else if (reports->size() == 4) {
id_four_targets(reports);
}
else {
//shouldn't happen
perror_612("Too Many Particles. Aborting.");
}
#else
//give each target a particle :)
if (numtargets < reports->size()) {
//should NEVER be true
for (unsigned i = 0; i < numtargets; i++) {
target_arr[i].update_data_with_report(reports->at(i));
}
}
else {
for (unsigned i = 0; i < reports->size(); i++) {
target_arr[i].update_data_with_report(reports->at(i));
}
for (unsigned i = reports->size(); i < numtargets; i++) {
target_arr[i].m_valid = false;
}
}
#endif
}
/*
* Distance - given COM_Y of the backboard
*
* COM_Y ranges 0 (top) to RESOLUTION().Y() (bottom)
* H is the height (feet) between camera and center of backboard
*
* theta = angle_offset(RESOLUTION().Y()/2 - COM_Y, RESOLUTION().Y(), FOV());
* DX = H/std::tan(theta);
*/
double get_distance_TRIG(const ParticleAnalysisReport& r, double height) {
double theta = angle_offset(RESOLUTION().Y()/2 - r.center_mass_y, RESOLUTION().Y(), FOV().Y());
return (height/std::tan(theta));
}
void target::update_data_with_report(const ParticleAnalysisReport & r) {
m_valid = true;
#ifdef VISION_ALT_HEURISTIC
if (ALGORITHM == TRIGONOMETRIC) {
m_distance = get_distance_TRIG(r, m_height);
}
else if (ALGORITHM == REGRESSION) {
m_distance = vision_processing::get_distance_from_report(r);
}
#elif defined VISION_ALT_ADHOC
m_distance = vision_processing::get_distance_from_report(r);
m_height = vision_processing::get_height_offset_from_report(r, m_distance);
#endif
m_x_offset = r.center_mass_x - (RESOLUTION().X()/2);
m_fresh = true;
}