-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain.cs
416 lines (380 loc) · 17.1 KB
/
frmMain.cs
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using CameraControl;
using System.Runtime.InteropServices;
namespace PhotonController
{
public partial class frmMain : Form, IObserver
{
private CameraController _controller = null;
private ActionSource _actionSource = new ActionSource();
private List<ActionListener> _actionListenerList = new List<ActionListener>();
private IObserver formAs = null;
int receiverPort = 4023;
int takeEvery = 3; //th picture
int skipFirst = 6; //frames
double MaxLiftHeight = 45;
IPEndPoint ipEndPoint;
UdpClient receiver;
string gcodeCmd = "";
int framenumber = 0;
double progressBefore = -9999;
double zposbefore = -9999;
double prevzoffset = 0;
bool timelapse = false;
frmLog logForm = new frmLog();
int progressCount = 0;
bool isPrinting = false;
bool FrameSnapped = false;
bool FirstSnap = false;
bool snapComplete = false;
bool snapped = false;
public frmMain(ref CameraController controller)
{
InitializeComponent();
_controller = controller;
_actionListenerList.Add((ActionListener)_controller);
_actionListenerList.ForEach(actionListener => _actionSource.AddActionListener(ref actionListener));
}
public void Update(Observable from, CameraEvent e)
{
CameraEvent.Type eventType = e.GetEventType();
switch (eventType)
{
case CameraEvent.Type.DOWNLOAD_COMPLETE:
SetText(logForm.txtResponse.Text+"Image Saved to Disk\r\n");
snapComplete = true;
break;
case CameraEvent.Type.SHUT_DOWN:
SetText(logForm.txtResponse.Text+"Camera is disconnected\r\n");
break;
default:
break;
}
}
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (logForm.txtResponse.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
logForm.txtResponse.Text = text;
logForm.txtResponse.SelectionStart = logForm.txtResponse.Text.Length;
logForm.txtResponse.ScrollToCaret();
logForm.txtResponse.Refresh();
}
}
delegate void SetZheightCallback(string text);
private void SetZheight(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (lblZheight.InvokeRequired)
{
SetZheightCallback d = new SetZheightCallback(SetZheight);
this.Invoke(d, new object[] { text });
}
else
{
lblZheight.Text = text;
lblZheight.Refresh();
}
}
delegate void SetProgressCallback(int value);
private void SetProgress(int value)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.PrintProgress.InvokeRequired)
{
SetProgressCallback d = new SetProgressCallback(SetProgress);
this.Invoke(d, new object[] { value });
}
else
{
this.PrintProgress.Value = value;
}
}
delegate void SetLblCallback(string text);
private void SetLbl(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.PrintProgress.InvokeRequired)
{
SetLblCallback d = new SetLblCallback(SetLbl);
this.Invoke(d, new object[] { text });
}
else
{
this.lblPercentDone.Text = text;
}
}
private void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)ar.AsyncState;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receivedBytes = c.EndReceive(ar, ref receivedIpEndPoint);
// Convert data to ASCII and print in console
string receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
//SetText(logForm.txtResponse.Text + receivedText);
//logForm.txtResponse.SelectionStart = logForm.txtResponse.Text.Length;
if (gcodeCmd == "M27") //progress report
{
string[] lines = receivedText.Split('\n');
lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray();
double[] progress = new double[2];
if ((lines[0].Substring(0, 5).ToUpper() != "ERROR") && (lines[0].Substring(0, 2).ToUpper() != "OK"))
{
isPrinting = true;
lines = lines[0].Split(new[] { "byte" }, StringSplitOptions.None);
lines = lines[1].Split('/');
progress[0] = double.Parse(lines[0].Trim());
progress[1] = double.Parse(lines[1].Trim());
//SetText(logForm.txtResponse.Text + progressCount.ToString()+"\r\n");
if (progress[0] > progressBefore && progressCount>=1)
{
progressCount = 0;
int PrintProgress = (int)Math.Round((progress[0] * 100) / progress[1]);
SetProgress(PrintProgress);
SetLbl("Percent done : " + PrintProgress.ToString());
progressBefore = progress[0];
SetText(logForm.txtResponse.Text + "Frame = "+framenumber.ToString() + ", ");
if (timelapse)
{
FrameSnapped = false;
if (framenumber >= skipFirst)
{
if ((framenumber- skipFirst) % takeEvery == 0)
{
gcodeCmd = "M25";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
receivedBytes = receiver.Receive(ref ipEndPoint);
receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
gcodeCmd = "M114";
//receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
//receivedBytes = receiver.Receive(ref ipEndPoint);
//receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
FrameSnapped = true;
}
//else
//{
// if(PrintProgress>=100) //It's not the nth frame but the print finished. take the last frame
// {
// gcodeCmd = "M25";
// receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
// receivedBytes = receiver.Receive(ref ipEndPoint);
// receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
// gcodeCmd = "M114";
// }
//}
}
}
framenumber++;
}
else if (progress[0] > progressBefore && progressCount <1)
{
progressCount++;
}
//txtResponse.SelectionStart = txtResponse.Text.Length;
//txtResponse.ScrollToCaret();
//txtResponse.Refresh();
}
else if(lines[0].Substring(0, 5).ToUpper() == "ERROR")
{
if(isPrinting) //means it was printing and now it finsihed
{
SetProgress(100);
SetLbl("Percent done : 100" );
if(timelapse)
{
if(!FrameSnapped) //The lasr frame was not snapped coz it was not the nth frame
{
gcodeCmd = "M114";
//receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
//receivedBytes = receiver.Receive(ref ipEndPoint);
//receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
FrameSnapped = true;
}
timelapse = false; //stop time lapse
}
}
isPrinting = false;
}
}
else if (gcodeCmd == "M114") //Get current Z value untill it settles
{
if (receivedText.IndexOf("X:0.000000") != -1)
{
string[] lines = receivedText.Split(new[] { "Z:" }, StringSplitOptions.None);
lines = lines[1].Split(new[] { "E:" }, StringSplitOptions.None);
double zpos = double.Parse(lines[0]);
SetZheight("Z Height = " + Math.Round(zpos, 3).ToString());
if (zposbefore != zpos)
zposbefore = zpos;
else
{
double Zoffset = MaxLiftHeight - zpos;
gcodeCmd = "G91, G0 Z" + Zoffset.ToString() + " F300";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
receivedBytes = receiver.Receive(ref ipEndPoint);
receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
prevzoffset = Zoffset;
zposbefore = 0;
snapped = false;
snapComplete = false;
gcodeCmd = "M4000";
}
}
}
else if (gcodeCmd == "M4000") //Get current Z value untill it settles
{
if (receivedText.IndexOf("X:0.000") != -1)
{
string[] lines = receivedText.Split(new[] { "Z:" }, StringSplitOptions.None);
lines = lines[1].Split(new[] { "F:" }, StringSplitOptions.None);
double zpos = double.Parse(lines[0]);
SetZheight("Z Height = "+Math.Round(zpos, 3).ToString());
if (zposbefore != zpos)
zposbefore = zpos;
else
{
if (!snapped)
{
if (!FirstSnap)
{
SetText(logForm.txtResponse.Text + "Time Lapse Starts\r\n");
MessageBox.Show("Setup the Camera for time Lapse and click OK.");
FirstSnap = true;
}
snapComplete = false;
_actionSource.FireEvent(ActionEvent.Command.TAKE_PICTURE, IntPtr.Zero);
SetText(logForm.txtResponse.Text + "Take Picture\r\n");
snapped = true;
}
if (snapComplete)
{
gcodeCmd = "G91, G0 Z" + (-1 * prevzoffset).ToString() + " F300"; ;
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
receivedBytes = receiver.Receive(ref ipEndPoint);
receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
if (isPrinting) //if the printing it going on then resume print
{
gcodeCmd = "M24";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
receivedBytes = receiver.Receive(ref ipEndPoint);
receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
}
zposbefore = 0;
snapComplete = false;
snapped = false;
gcodeCmd = "M27";
}
}
}
}
// Restart listening for udp data packages
c.BeginReceive(DataReceived, ar.AsyncState);
}
private void btnConnect_Click(object sender, EventArgs e)
{
receiver = new UdpClient(receiverPort);
//receiver.BeginReceive(DataReceived, receiver);
ipEndPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
receiver.Connect(ipEndPoint);
gcodeCmd = "M114";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
Byte[] receivedBytes = receiver.Receive(ref ipEndPoint);
string receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
if (receivedText.IndexOf("X:0.000000") != -1)
lblStatus.Text = "Connected";
else
lblStatus.Text = "Not Connected";
gcodeCmd = "M27";
receiver.BeginReceive(DataReceived, receiver);
pollTimer.Enabled = true;
}
public void SendGcode()
{
//using (UdpClient sender1 = new UdpClient(ipEndPoint))
gcodeCmd = logForm.txtCmd.Text;
receiver.Send(Encoding.ASCII.GetBytes(logForm.txtCmd.Text), logForm.txtCmd.Text.Length);
}
private void pollTimer_Tick(object sender, EventArgs e)
{
if(gcodeCmd=="M27" || gcodeCmd=="M114" || gcodeCmd=="M4000")
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
}
private void getPrintPercent()
{
gcodeCmd = "M27";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
}
private void btnPause_Click(object sender, EventArgs e)
{
gcodeCmd = "M25";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
//btnPause.Enabled = false;
//btnStart.Enabled = true;
//btnStop.Enabled = true;
}
private void btnStart_Click(object sender, EventArgs e)
{
gcodeCmd = "M24";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
//btnPause.Enabled = true;
//btnStart.Enabled = false;
//btnStop.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
gcodeCmd = "M29";
receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length);
//btnPause.Enabled = false;
//btnStart.Enabled = true;
//btnStop.Enabled = false;
}
private void btnStartTimeLapse_Click(object sender, EventArgs e)
{
gcodeCmd = "M27";
takeEvery = (int)numTakeEvery.Value; //th picture
skipFirst = (int)numSkipFirst.Value; //frames
MaxLiftHeight = (double)numMaxLiftHeight.Value;
PrintProgress.Value = 0;
timelapse = true;
FirstSnap = false;
}
private void btnStopTimeLapse_Click(object sender, EventArgs e)
{
gcodeCmd = "M27";
timelapse = false;
}
private void button1_Click(object sender, EventArgs e)
{
logForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
_actionSource.FireEvent(ActionEvent.Command.TAKE_PICTURE, IntPtr.Zero);
}
}
}