-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
208 lines (159 loc) · 6.13 KB
/
Program.cs
File metadata and controls
208 lines (159 loc) · 6.13 KB
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
#define USE_OPTIMAL_COMMS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Threading;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using Hardware;
using System.Runtime.InteropServices;
using Firmware;
using GCoder;
using CommandHandler;
using CommandTypes;
using SimTests;
using System.Diagnostics;
namespace PrinterSimulator
{
class PrintSim
{
const int CMD_SEND_THRESHOLD = 47;
static void PrintFile(string fileName, Command command, PrinterControl simCtl)
{
string Cmd;
float X, Y, Z, E;
bool fLaserOn = false;
command.SendSingleCommand((byte)FWCommands.CMD_RETRACT_PLATE); // Retract plate
Stopwatch swTimer = new Stopwatch();
swTimer.Start();
command.SendCommandParam1((byte)FWCommands.CMD_REMOVE_MODEL, 0); // Set Z axis
command.SendCommandParam1((byte)FWCommands.CMD_SET_Z, 0); // Set Z axis
command.StartCommandBuffer();
GCode coder = new GCode(fileName);
Cmd = ""; X = 0; Y = 0; Z = 0; E = 0;
while (coder.GetNextLine(ref Cmd, ref X, ref Y, ref Z, ref E)) {
#if USE_OPTIMAL_COMMS
if (Z != 0)
{
command.Add((byte)FWCommands.CMD_SET_Z);
command.Add(4);
command.Add(Z);
}
if (E != 0) // Turn on laser
{
if (!fLaserOn)
{
command.Add((byte)FWCommands.CMD_SET_LASER);
command.Add(1);
command.Add(1);
fLaserOn = true;
}
}
else // Turn OFF laser
{
if (fLaserOn)
{
command.Add((byte)FWCommands.CMD_SET_LASER);
command.Add(1);
command.Add(0);
fLaserOn = false;
}
}
if (X != 0 || Y != 0)
{
command.Add((byte)FWCommands.CMD_MOVE_GALVOS);
command.Add(8);
command.Add(X * 0.025f);
command.Add(Y * 0.025f);
}
if (command.Count() > CMD_SEND_THRESHOLD)
{
command.SendCommandBuffer();
command.StartCommandBuffer();
}
#else
if (Z != 0)
command.SendCommandParam1((byte)FWCommands.CMD_SET_Z, Z); // Set Z axis
if (E != 0) { // Set laser
if (!fLaserOn)
{
command.SendCommandParam1((byte)FWCommands.CMD_SET_LASER, 1);
fLaserOn = true;
}
}
else // Turn OFF laser
{
if (fLaserOn)
{
command.SendCommandParam1((byte)FWCommands.CMD_SET_LASER, 0);
fLaserOn = false;
}
}
if (X != 0 || Y != 0)
command.SendCommandParam2((byte)FWCommands.CMD_MOVE_GALVOS, X * 0.025f, Y * 0.025f);
#endif
Cmd = ""; X = 0; Y = 0; Z = 0; E = 0;
}
command.SendSingleCommand((byte)FWCommands.CMD_RETRACT_PLATE); // Retract plate
swTimer.Stop();
long elapsedMS = swTimer.ElapsedMilliseconds;
Console.WriteLine("Total Print Time: {0}", elapsedMS / 1000.0);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
[STAThread]
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main()
{
IntPtr ptr = GetConsoleWindow();
//MoveWindow(ptr, -1000, 0, 1000, 400, true); // Move console window out from under printer windows
// Start the printer - DO NOT CHANGE THESE LINES
PrinterThread printer = new PrinterThread();
Thread oThread = new Thread(new ThreadStart(printer.Run));
oThread.Start();
printer.WaitForInit();
// Start the firmware thread - DO NOT CHANGE THESE LINES
FirmwareController firmware = new FirmwareController(printer.GetPrinterSim());
oThread = new Thread(new ThreadStart(firmware.Start));
oThread.Start();
firmware.WaitForInit();
Command command = new Command(printer.GetPrinterSim());
Tests testMenu = new Tests(command, printer.GetPrinterSim());
SetForegroundWindow(ptr);
bool fDone = false;
while (!fDone)
{
Console.Clear();
Console.WriteLine("3D Printer Simulation - Control Menu\n");
Console.WriteLine("P - Print");
Console.WriteLine("T - Test");
Console.WriteLine("Q - Quit");
char ch = Char.ToUpper(Console.ReadKey().KeyChar);
switch (ch)
{
case 'P': // Print
PrintFile("..\\SampleSTLs\\F-35_Corrected.gcode", command, printer.GetPrinterSim());
break;
case 'T': // Test menu
testMenu.TestMenu();
break;
case 'Q' : // Quite
printer.Stop();
firmware.Stop();
fDone = true;
break;
}
}
}
}
}