-
Notifications
You must be signed in to change notification settings - Fork 0
/
Micromirror_record.pde
264 lines (226 loc) · 6.66 KB
/
Micromirror_record.pde
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
import processing.serial.*;
import javax.swing.*;
import static javax.swing.JOptionPane.*;
final int SERIAL_TIMEOUT = 10000;
final float pi = 3.1415926535;
final boolean throwOutRepeats = false;
final char CHAR_SYN = 0x01; // Connection test from control computer
final char CHAR_SYNACK = 0x02; // Response to connection test from microcontroller
final char CHAR_ACK = 0x03; // Confirmation from control computer
final char CHAR_NAK = 0x04; // Invalid response from control computer
final char CHAR_DAT = 0x05; // Data request from control computer
final char CHAR_DRYRUN = 0x06; // Request to take dry-run measurements of microcontroller
enum state{
STATE_DISCONNECTED,
STATE_STOPPED,
STATE_INITIALIZING,
STATE_RUNNING
};
PGraphics win;
state cur_state;
Serial arduinoComms;
PrintWriter logOut;
String logLoc;
long readings = 0;
float[] lastReading = {0,0,0};
final JFileChooser fc = new JFileChooser();
int lastVal = 0;
int numRep = 0;
void setup(){
cur_state = state.STATE_DISCONNECTED;
System.out.println("Available serial ports: ");
printArray(Serial.list());
while(true){
int port = Integer.parseInt(showInputDialog("Choose Serial Port (see output console)"));
int baud = Integer.parseInt(showInputDialog("Baudrate?"));
arduinoComms = new Serial(this, Serial.list()[port], baud);
if(connect(arduinoComms)){
break;
}else{
arduinoComms.stop();
System.out.println("Connection timed out. Re-prompting for connection");
}
}
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if(file.exists()){
if(!showInputDialog("File exists. Overwrite? [Y/N]").equals("Y")){
exit();
}
}
logLoc = file.getAbsolutePath();
logOut = createWriter(logLoc);
}
else {
println("Cancelled.");
}
System.out.println("Activating GUI");
size(200,200);
win = createGraphics(200,200);
cur_state = state.STATE_STOPPED;
}
void draw(){
win.beginDraw();
System.out.println(cur_state);
win.background(102);
win.stroke(255);
win.fill(cur_state==state.STATE_STOPPED ? 255 : 0);
win.rect(25,20,150,50);
win.textSize(20);
win.fill(255,0,0);
win.rect(180,180,10,10);
String s;
if(cur_state == state.STATE_STOPPED){
win.fill(0);
s = " STOPPED ";
}else if(cur_state == state.STATE_INITIALIZING){
win.fill(255);
s = " INIT... ";
}else if(cur_state == state.STATE_RUNNING){
win.fill(255);
s = "RECORDING";
}else{
win.fill(255,0,0);
s = "ERROR...";
}
win.text(s, 30, 55);
if(cur_state==state.STATE_RUNNING){
delay(10);
arduinoFlush(arduinoComms);
float[] dat_p = getData();
double[] dat = getCoordsFromAngles(angleFromVolts(dat_p[0]), angleFromVolts(dat_p[1]), dat_p[2]);
if(!(dat.equals(lastReading)) || !throwOutRepeats){
win.textSize(10);
win.text("X="+dat[0]+"\nY="+dat[1]+"\nZ="+dat[2],0,100);
win.text("Received Vh="+dat_p[0]+",Vv="+dat_p[1]+"Dis="+dat_p[2],0,160);
String st = dat[0]+","+dat[1]+","+dat[2];
readings+=st.length();
win.text(readings+" Bytes Written",0,170);
win.text("Logging output to: ",0,180);
win.textSize(5);
win.text(logLoc,0,190);
logOut.println(st);
}
}else if(cur_state==state.STATE_INITIALIZING){
float[] dat_pi;
double[] dati;
dat_pi = getData();
dati = getCoordsFromAngles(angleFromVolts(dat_pi[0]), angleFromVolts(dat_pi[1]), dat_pi[2]);
win.textSize(10);
win.text("X="+dati[0]+",Y="+dati[1]+"Z="+dati[2],0,100);
win.text("Received Vh="+dat_pi[0]+",Vv="+dat_pi[1]+"Dis="+dat_pi[2],0,110);
win.text("Currently initializing... No output yet.",0,125);
if(abs((int)dat_pi[2] - lastVal)<20 && dat_pi[2] > 2.0){
numRep++;
if(numRep>=20){
win.text("Initialization complete!",0,135);
cur_state = state.STATE_RUNNING;
}
}else{
numRep = 0;
}
lastVal = (int)dat_pi[2];
}
win.endDraw();
image(win, 0, 0);
}
void mouseClicked(){
if((mouseX > 25) & (mouseY > 20) & (mouseX < 175) & (mouseY < 70)){
if(cur_state == state.STATE_RUNNING){
cur_state = state.STATE_STOPPED;
}else{
cur_state = state.STATE_INITIALIZING;
}
}
if((mouseX > 180) & (mouseY > 180) & (mouseX < 190) & (mouseY < 190)){
logOut.flush();
logOut.close();
exit();
}
}
/**
* bool connect(Serial s)
*
* Establishes connection to the microcontroller
* Handshake structure is as follows:
* 1. Control computer sends "syn" signal to microcontroller
* 2. Microcontroller sends "synack" signal to control computer
* 3. Control computer sends "ack" to microcontroller
* This ensures that both components are responding and can communicate data.
*
* PARAMS:
* Serial s -- serial port to connect over
*
* RETURN:
* bool -- Connection state (Successful / timed out)
*/
boolean connect(Serial s){
s.write(CHAR_SYN);
for(int i = 0; i < SERIAL_TIMEOUT; i++){
char c = (char)s.read();
if(c==CHAR_SYNACK){
s.write(CHAR_ACK);
return true;
}else{
delay(1);
}
}
return false;
}
float[] getData(){
arduinoFlush(arduinoComms);
float[] arr = {0,0,0};
for(int i = 0; i < 3; i++){
System.out.println("Waiting for Arduino response");
while(arduinoComms.available()==0){
if(cur_state == state.STATE_RUNNING){
arduinoFlush(arduinoComms);
arduinoComms.write(CHAR_DAT);
//delay(100);
}else if(cur_state == state.STATE_INITIALIZING){
arduinoFlush(arduinoComms);
arduinoComms.write(CHAR_DRYRUN);
//System.out.println(ini);
}else{
float[] f = {-1,-1,-1};
return f;
}
}
delay(10);
System.out.println("Received response. Parsing.");
String s = arduinoComms.readStringUntil('\n');
/*if(s == null){
System.out.println("Erroneous data packet received.");
arr[i] = -1;
}else if(s.length()>0){
arr[i] = Float.parseFloat(s);
}else{
System.out.println("Erroneous data packet received.");
arr[i] = -1;
}*/
try{
arr[i] = Float.parseFloat(s);
}catch(Exception e){
System.out.println("Erroneous data packet received.");
arr[i] = -1;
}
}
return arr;
}
float angleFromVolts(float volts){
System.out.println(volts+"->"+volts * (pi / 180.0));
return volts * (pi / 180.0);
}
double[] getCoordsFromAngles(float yaw, float pitch, float distance){
double x = (distance)*cos(pitch)*sin(yaw);
double y = (distance)*cos(pitch)*cos(yaw);
double z = (distance)*sin(pitch);
double[] arr = {x,y,z};
return arr;
}
void arduinoFlush(Serial ser){
while(ser.available()==1){
ser.read();
}
}