-
Notifications
You must be signed in to change notification settings - Fork 0
/
Firmware.cs
268 lines (208 loc) · 8.32 KB
/
Firmware.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Hardware;
using CommandTypes;
using System.Diagnostics;
namespace Firmware
{
public class FirmwareController
{
const string FIRMWARE_VERSION = "1.0";
const float MAX_STEPPER_ACCEL = 4.0f;
const float MAX_STEPPER_VELOCITY = 40.0f;
const long TIMEOUT_THRESHOLD = 100000000L;
const byte HEADER_SIZE = 4;
const byte ACK_BYTE = 0xA5;
const byte NAK_BYTE = 0xFF;
bool fDone = false;
bool fInitialized = false;
PrinterControl printer;
float plateZ = -1;
long accelCounter = 0;
float accelCurVelocity = 1.0f; // mm/s
public FirmwareController(PrinterControl printer)
{
this.printer = printer;
}
String DoCommand(byte cmd, byte[] cmdParams, byte offset)
{
string retString = "";
switch (cmd)
{
case FWCommands.CMD_VERSION:
retString = FIRMWARE_VERSION;
break;
case FWCommands.CMD_RETRACT_PLATE:
RetractPlate();
retString = "SUCCESS";
break;
case FWCommands.CMD_SET_Z:
SetZ(GetFloatParam(cmdParams, offset));
retString = "SUCCESS";
break;
case FWCommands.CMD_REMOVE_MODEL:
printer.RemoveModelFromPrinter();
retString = "SUCCESS";
break;
case FWCommands.CMD_MOVE_GALVOS:
printer.MoveGalvos(GetFloatParam(cmdParams, offset), GetFloatParam(cmdParams, offset + 4));
retString = "SUCCESS";
break;
case FWCommands.CMD_SET_LASER:
if (cmdParams[offset] == 1) printer.SetLaser(true);
else printer.SetLaser(false);
retString = "SUCCESS";
break;
default:
retString = "ERROR";
break;
}
return retString;
}
void Process()
{
byte[] value = new byte[HEADER_SIZE];
byte[] cmdParams = new byte[64];
int bytesRead = 0;
string retString = "";
int timeout;
while (!fDone)
{
do // Wait for command byte
{
bytesRead = printer.ReadSerialFromHost(value, HEADER_SIZE); // Read command, length, and checksum
} while (!fDone && bytesRead == 0);
// Console.WriteLine("Process: {0} {1}", value[0], value[1]);
if (!fDone && bytesRead == HEADER_SIZE) // Successfully read command byte, length byte, and checksum bytes
{
// Write header back to host
printer.WriteSerialToHost(value, HEADER_SIZE);
byte[] ACKNAK = new byte[2];
ACKNAK[0] = 0;
do // Wait for ACK/NACK byte
{
bytesRead = printer.ReadSerialFromHost(ACKNAK, 1); // Read ACK/NAK
} while (!fDone && bytesRead == 0);
if (ACKNAK[0] == ACK_BYTE)
{
timeout = 0;
if (value[1] > 0)
do { timeout++; } while ((bytesRead = printer.ReadSerialFromHost(cmdParams, value[1])) == 0 && timeout < TIMEOUT_THRESHOLD); // Read in the parameters
if (timeout == TIMEOUT_THRESHOLD)
{
retString = "TIMEOUT";
}
else
{
// Validate checksum
short checkSum = (byte)(value[0] + value[1]);
short targetCheckSum = (short)(((short)value[3] << 8) + value[2]);
for (int i = 0; i < value[1]; i++)
checkSum += cmdParams[i];
if (checkSum == targetCheckSum) // Valid checksum, proceed
{
if (value[0] != FWCommands.CMD_BUFFER)
retString = DoCommand(value[0], cmdParams, 0);
else // Process command buffer
{
while (cmdParams[0] != FWCommands.CMD_INVALID)
{
byte Ndx = 0;
byte bufferLen = value[1];
byte cmd = cmdParams[Ndx++];
byte len = cmdParams[Ndx++];
Ndx += len;
retString = DoCommand(cmd, cmdParams, 2);
// Delete used command
for (byte i = 0; i < bufferLen - (len + 2); i++)
cmdParams[i] = cmdParams[len + 2 + i];
for (byte i = (byte)(bufferLen - (len + 2)); i < bufferLen; i++)
cmdParams[i] = FWCommands.CMD_INVALID;
}
}
}
else retString = "CHECKSUM";
} // If no TIMEOUT
printer.WriteSerialToHost(retString.Select(c => (byte)c).ToArray(), retString.Length);
value[0] = 0;
printer.WriteSerialToHost(value, 1);
} // if ACK'd
}
}
}
public void Start()
{
fInitialized = true;
Process(); // this is a blocking call
}
public void Stop()
{
fDone = true;
}
public void WaitForInit()
{
while (!fInitialized)
Thread.Sleep(100);
}
void ResetAccelStepper()
{
accelCounter = 0;
accelCurVelocity = 1.0f;
}
void WaitForNextStep()
{
long microWait;
microWait = (long)(1000000L / (accelCurVelocity * PrinterControl.STEPS_PER_MM));
printer.WaitMicroseconds(microWait);
accelCounter += microWait;
if (accelCounter > 1000000L) // Increase velocity every second
{
accelCurVelocity += MAX_STEPPER_ACCEL;
if (accelCurVelocity > MAX_STEPPER_VELOCITY) accelCurVelocity = MAX_STEPPER_VELOCITY;
accelCounter = 0;
}
}
void RetractPlate()
{
ResetAccelStepper();
while (!printer.LimitSwitchPressed())
{
if (!printer.StepStepper(PrinterControl.StepperDir.STEP_UP)) break;
WaitForNextStep();
}
plateZ = printer.GetPrinterHeight();
}
float GetFloatParam(byte[] floatArr, int offset)
{
float retValue = System.BitConverter.ToSingle(floatArr, offset);
return retValue;
}
// Set the Z height of the platform - Zero is at the bottom, against the resin tray
void SetZ(float Z)
{
if (plateZ < 0)
{
RetractPlate();
}
float curZ = plateZ;
PrinterControl.StepperDir dir = PrinterControl.StepperDir.STEP_UP;
float diffZ = Z - curZ;
ResetAccelStepper();
if (diffZ < 0)
dir = PrinterControl.StepperDir.STEP_DOWN;
diffZ = Math.Abs(diffZ);
int numSteps = (int) (diffZ * PrinterControl.STEPS_PER_MM);
for (int i = 0; i < numSteps; i++)
{
printer.StepStepper(dir);
WaitForNextStep();
if (printer.LimitSwitchPressed()) break;
}
plateZ = Z;
}
}
}