-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagTest.java
233 lines (192 loc) · 7.84 KB
/
TagTest.java
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
package robot.vision;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.lang.Math;
import april.jmat.*;
import april.jmat.geom.*;
import april.jcam.*;
import april.util.*;
import april.tag.*;
import robot.lcm.*;
public class TagTest
{
static final double CODE_FLAT = 0.0;
static final double APRIL_CODE_PIXEL_HEIGHT = 0.5; //244;
static final double APRIL_CODE_PIXEL_WIDTH = 350;
static final double CODE_ANGLE_RANGE = 20;
static final double PIXEL_RANGE = 0.15; //50;
static final double PIXEL_RANGE_WIDTH = 75;
ImageSource is;
TagFamily tf;
TagDetector detector;
// usage: TagTest <imagesource> [tagfamily class]
public static void main(String args[])
{
GetOpt opts = new GetOpt();
opts.addBoolean('h',"help",false,"See this help screen");
opts.addString('u',"url","","Camera url");
opts.addString('t',"tagfamily","april.tag.Tag36h11","Tag family");
if (!opts.parse(args)) {
System.out.println("option error: "+opts.getReason());
}
String url = opts.getString("url");
String tagfamily = opts.getString("tagfamily");
if (opts.getBoolean("help") || url.isEmpty()){
System.out.println("Usage:");
opts.doHelp();
System.exit(1);
}
try {
ImageSource is = ImageSource.make(url);
TagFamily tf = (TagFamily) ReflectUtil.createObject(tagfamily);
TagTest tt = new TagTest(is, tf);
} catch (IOException ex) {
System.out.println("Ex: "+ex);
}
}
public TagTest(ImageSource is, TagFamily tf)
{
this.is = is;
this.tf = tf;
detector = new TagDetector(tf);
ImageSourceFormat ifmt = is.getCurrentFormat();
new RunThread().start();
new RunThread().start();
new RunThread().start();
}
class RunThread extends Thread
{
public void run()
{
is.start();
ImageSourceFormat fmt = is.getCurrentFormat();
detector = new TagDetector(tf);
//These parameters are set based on April TagDetector documentation
//and tweaked with a little experimentation
//The two values below are zero because segDecimate is enabled
//Both should be 0.7 if segDecimate is false
detector.segSigma = 0.0;
detector.sigma = 0.0;
//minMag can go from 0.001 to 0.01 the higher the number the faster
//the image processes
detector.minMag = 0.01;
detector.maxEdgeCost = 0.52359;
detector.magThresh = 12000.0;
detector.thetaThresh = 100.0;
//Lower values are faster
detector.WEIGHT_SCALE = 60;
detector.segDecimate = true;
detector.debug = false;
//TODO: Figure out of these are needed
/*detector.debugSegments = vw.getBuffer("segments");
detector.debugQuads = vw.getBuffer("quads");
detector.debugSamples = vw.getBuffer("samples");
detector.debugLabels = vw.getBuffer("labels");*/
MotorPublisher mp = new MotorPublisher();
MotorSpeed ms = new MotorSpeed();
FrameData frmd;
BufferedImage im;
double angle = 0;
double prev_angle = 0;
int empCounter = 0;
while (true) {
frmd = null;
for (int i = 0; i < 3; i++){
frmd = is.getFrame();
}
if (frmd == null)
continue;
im = ImageConvert.convertToImage(frmd);
//Not sure what this does but this is the number set in the
//test program and it works so I'm going to use it
tf.setErrorRecoveryBits(1);
//TODO: Use this detection array to get location of tags
//Import TagDetection class fields include:
//cxy: (center of tag in pixel in tag coordinates)
//rotation: (How many 90 degree rotations to align with code)
ArrayList<TagDetection> detections = detector.process(im, new double[] {im.getWidth()/2.0, im.getHeight()/2.0});
//If can't find a tag stop the robot before it kills someone
if (detections.isEmpty()){
empCounter++;
if (empCounter > 3){
ms.frontMotor = 0;
ms.backMotor = 0;
ms.rightMotor = 0;
ms.leftMotor = 0;
mp.publish(ms);
mp.publish(ms);
empCounter = 0;
}
}
for (TagDetection d : detections) {
double yPix = d.cxy[1];
double xPix = d.cxy[0];
//5.2 inches to meters
double tagsize_m = 0.132;
//Need to figure out the proper focal length
double f = 485.6;
double M[][] = CameraUtil.homographyToPose(f, f, im.getWidth()/2, im.getHeight()/2, d.homography);
prev_angle = angle;
angle = -Math.asin(M[2][0]) * 100;
System.out.print("Angle: " + angle);
//System.out.print("CenterX: " + d.cxy[0] + " CenterY: " + d.cxy[1]);
if (yPix < (APRIL_CODE_PIXEL_HEIGHT - PIXEL_RANGE)){
ms.rightMotor = -1;
ms.leftMotor = -1;
}
else if (yPix > (APRIL_CODE_PIXEL_HEIGHT + PIXEL_RANGE)){
ms.rightMotor = 1;
ms.leftMotor = 1;
}
else{
ms.rightMotor = 0;
ms.leftMotor = 0;
}
if (xPix < (APRIL_CODE_PIXEL_WIDTH - PIXEL_RANGE_WIDTH)){
ms.frontMotor = -1;
ms.backMotor = -1;
System.out.println(" Driving right!");
}
else if (xPix > (APRIL_CODE_PIXEL_WIDTH + PIXEL_RANGE_WIDTH)){
ms.frontMotor = 1;
ms.backMotor = 1;
System.out.println(" Driving left!");
}
else{
ms.frontMotor = 0;
ms.backMotor = 0;
System.out.println(" In a good range!");
}
if ((angle > 100) || (angle < -100)){
System.out.println("Invalid Angle");
}
else{
if (angle < (CODE_FLAT - CODE_ANGLE_RANGE)){
ms.frontMotor = -0.9;
ms.backMotor = -0.9;
ms.rightMotor = -0.5;
ms.leftMotor = 0.5;
System.out.println(" Angling Right!");
}
else if (angle > (CODE_FLAT + CODE_ANGLE_RANGE)){
ms.frontMotor = 0.9;
ms.backMotor = 0.9;
ms.rightMotor = 0.5;
ms.leftMotor = -0.5;
System.out.println(" Angling Left!");
}
else {
System.out.println(" I'm good!");
}
}
mp.publish(ms);
mp.publish(ms);
}
}
}
}
}