FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Netduino-gcode/StepperBasic/CncDevice.cs at master · yazici/Netduino-gcode · GitHub
yazici
Netduino-gcode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Netduino-gcode
/
StepperBasic
/
CncDevice.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
588 lines (470 loc) · 18.1 KB
Breadcrumbs
Netduino-gcode
/
StepperBasic
/
CncDevice.cs
Copy path
File metadata and controls
588 lines (470 loc) · 18.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
using
System
;
using
Microsoft
.
SPOT
;
using
Microsoft
.
SPOT
.
Hardware
;
using
SecretLabs
.
NETMF
.
Hardware
;
using
SecretLabs
.
NETMF
.
Hardware
.
Netduino
;
using
gcodeparser
;
using
NetduinoDevice
;
namespace
StepperBasic
{
public
class
CncDevice
:
Device
{
private
const
int
MaximumXYFeedRate
=
670
;
// Safe at default driver current setting
private
const
int
MaximumZFeedRate
=
230
;
// Safe at default driver current setting
// __ Port setup ______________________________________________________
static
OutputPort
MicroStep1Port
=
new
OutputPort
(
Pins
.
GPIO_PIN_D0
,
false
)
;
static
OutputPort
MicroStep2Port
=
new
OutputPort
(
Pins
.
GPIO_PIN_D1
,
false
)
;
static
OutputPort
XDirectionPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D10
,
false
)
;
static
OutputPort
YDirectionPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D9
,
false
)
;
static
OutputPort
ZDirectionPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D8
,
false
)
;
static
OutputPort
XStepPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D6
,
false
)
;
static
OutputPort
YStepPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D5
,
false
)
;
static
OutputPort
ZStepPort
=
new
OutputPort
(
Pins
.
GPIO_PIN_D4
,
false
)
;
// __ Absolute position _______________________________________________
private
int
AbsoluteXSteps
=
0
;
private
int
AbsoluteYSteps
=
0
;
private
int
AbsoluteZSteps
=
0
;
private
uint
XYStepInterval
=
1500
;
private
uint
ZStepInterval
=
1500
;
private
float
FeedRate
=
125
;
// mm per minute
private
float
StepsPerMmX
=
160.2564f
*
2
;
private
float
StepsPerMmY
=
160.3849f
*
2
;
private
float
StepsPerMmZ
=
160.97924f
*
2
;
//private int XBacklashCompensationSteps = 0;
//private int YBacklashCompensationSteps = 0;
//private int ZBacklashCompensationSteps = 0;
private
const
bool
PositiveAxisDirection
=
true
;
private
const
bool
NegativeAxisDirection
=
!
PositiveAxisDirection
;
// __ Commands ________________________________________________________
/// <summary>
/// Sets the feed rate (cutting head advance speed).
/// </summary>
/// <param name="feedRate">Feed rate in milimetes per minute.</param>
/// <remarks>At 19V and max current, the vexta steppers can handle 670mmperminute in with the current screws.
/// </remarks>
public
override
void
SetFeedRate
(
float
mmPerMinute
)
{
if
(
mmPerMinute
>
MaximumXYFeedRate
)
mmPerMinute
=
MaximumXYFeedRate
;
FeedRate
=
mmPerMinute
;
XYStepInterval
=
GetIntervalForFeedRate
(
mmPerMinute
)
;
if
(
mmPerMinute
>
MaximumZFeedRate
)
mmPerMinute
=
MaximumZFeedRate
;
ZStepInterval
=
GetIntervalForFeedRate
(
mmPerMinute
)
;
}
private
uint
GetIntervalForFeedRate
(
float
mmPerMinute
)
{
const
uint
microsecondsPerMinute
=
60000000
;
float
stepsPerMinute
=
mmPerMinute
*
StepsPerMmX
;
return
(
uint
)
(
microsecondsPerMinute
/
stepsPerMinute
)
;
}
public
void
SetZero
(
)
{
AbsoluteXSteps
=
0
;
AbsoluteYSteps
=
0
;
AbsoluteZSteps
=
0
;
}
public
string
GetLocation
(
)
{
return
"LOCATION"
+
" X: "
+
mCurrentX
+
" Y: "
+
mCurrentY
+
" Z: "
+
mCurrentZ
+
"
\r
\n
"
;
}
public
string
GetStatus
(
)
{
string
s
=
"STAT"
+
" X:"
+
AbsoluteXSteps
+
" Y:"
+
AbsoluteYSteps
+
" Z:"
+
AbsoluteZSteps
+
" DirX:"
+
(
XDirectionPort
.
Read
(
)
?
'1'
:
'0'
)
+
" DirY:"
+
(
YDirectionPort
.
Read
(
)
?
'1'
:
'0'
)
+
" MS1:"
+
(
MicroStep1Port
.
Read
(
)
?
'1'
:
'0'
)
+
" MS2:"
+
(
MicroStep2Port
.
Read
(
)
?
'1'
:
'0'
)
+
" FeedRate: "
+
XYStepInterval
+
"ms/step"
+
"
\r
\n
"
;
return
s
;
}
public
string
GetCalibration
(
)
{
return
"CALIBRATION"
+
" X:"
+
StepsPerMmX
+
" Y:"
+
StepsPerMmY
+
" Z:"
+
StepsPerMmZ
;
}
public
int
GetAbsoluteSteps
(
int
steps
,
OutputPort
directionPort
)
{
bool
ms1
=
MicroStep1Port
.
Read
(
)
;
bool
ms2
=
MicroStep2Port
.
Read
(
)
;
bool
dir
=
directionPort
.
Read
(
)
;
int
multiplier
=
1
;
if
(
ms1
&&
ms2
)
multiplier
=
1
;
if
(
!
ms1
&&
ms2
)
multiplier
=
2
;
if
(
ms1
&&
!
ms2
)
multiplier
=
4
;
if
(
!
ms1
&
!
ms2
)
multiplier
=
8
;
if
(
dir
==
NegativeAxisDirection
)
multiplier
=
-
multiplier
;
return
steps
*
multiplier
;
}
public
void
SetDirection
(
bool
xDir
,
bool
yDir
,
bool
zDir
)
{
XDirectionPort
.
Write
(
xDir
)
;
YDirectionPort
.
Write
(
yDir
)
;
ZDirectionPort
.
Write
(
!
zDir
)
;
}
public
void
SetMicroStepping
(
bool
ms1
,
bool
ms2
)
{
MicroStep1Port
.
Write
(
ms1
)
;
MicroStep2Port
.
Write
(
ms2
)
;
NetduinoDevice
.
Delay
.
Microseconds
(
1000
)
;
}
private
void
MoveSingleAxis
(
OutputPort
port
,
OutputPort
directionPort
,
int
steps
,
uint
delay
)
{
SetMovementDirection
(
ref
steps
,
port
,
directionPort
)
;
MoveSingleAxis
(
port
,
steps
,
delay
)
;
}
private
void
MoveSingleAxis
(
OutputPort
port
,
int
steps
,
uint
delay
)
{
if
(
port
==
ZStepPort
&&
delay
==
0
)
delay
=
ZStepInterval
;
if
(
delay
==
0
)
delay
=
XYStepInterval
;
for
(
int
i
=
0
;
i
<
steps
;
++
i
)
{
port
.
Write
(
true
)
;
NetduinoDevice
.
Delay
.
Microseconds
(
delay
)
;
port
.
Write
(
false
)
;
NetduinoDevice
.
Delay
.
Microseconds
(
delay
)
;
}
}
private
bool
XLastState
=
false
;
private
bool
YLastState
=
false
;
private
bool
ZLastState
=
false
;
/*
public void MoveAxes(int xSteps, int ySteps)
{
int max = (xSteps > ySteps) ? xSteps : ySteps;
uint time = (uint)max * FeedRate;
MoveAxes(xSteps, ySteps, time);
}
public void MoveAxes(int xSteps, int ySteps, uint timeInUs)
{
if (xSteps == 0 && ySteps == 0) return;
// Single axis move
if (xSteps == 0)
{
MoveSingleAxis(YStepPort, ySteps, (uint)FeedRate);
return;
}
if (ySteps == 0)
{
MoveSingleAxis(XStepPort, xSteps, (uint)FeedRate);
return;
}
// Coordinated move
uint xstep = timeInUs / (uint)xSteps / 2;
uint ystep = timeInUs / (uint)ySteps / 2;
// Proc2
// This method waits the time needed for the next closest state change. It's
// pretty accurate and independent of the numbers.
uint uxstep = (uint)xstep;
uint uystep = (uint)ystep;
uint xtime = uxstep;
uint ytime = uystep;
uint ellapsed = 0;
while (ellapsed < timeInUs)
{
if (xtime > ytime)
{
NetduinoDevice.Delay.Microseconds(ytime);
ellapsed += ytime;
xtime -= ytime;
YLastState = !YLastState;
YStepPort.Write(YLastState);
ytime = uystep;
}
else
{
NetduinoDevice.Delay.Microseconds(xtime);
ellapsed += xtime;
ytime -= xtime;
XLastState = !XLastState;
XStepPort.Write(XLastState);
xtime = uxstep;
}
}
AbsoluteXSteps += GetAbsoluteSteps(xSteps, XDirectionPort);
AbsoluteYSteps += GetAbsoluteSteps(ySteps, YDirectionPort);
}
*/
/*
public void MoveAxes3(int xSteps, int ySteps, int zSteps)
{
// 3D line Bresenham
int dx = xSteps;
int dy = ySteps;
int dz = zSteps;
int x_inc = (dx < 0) ? -1 : 1;
int y_inc = (dy < 0) ? -1 : 1;
int z_inc = (dz < 0) ? -1 : 1;
int Adx = Abs(dx);
int Ady = Abs(dy);
int Adz = Abs(dz);
int dx2 = Adx * 2;
int dy2 = Ady * 2;
int dz2 = Adz * 2;
if ((Adx >= Ady) && (Adx >= Adz))
{
int err_1 = dy2 - Adx;
int err_2 = dz2 - Adx;
for (int c = 0; c < Adx; ++c)
{
if (err_1 > 0)
{
YLastState = !YLastState;
YStepPort.Write(YLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_1 -= dx2;
}
if (err_2 > 0)
{
ZLastState = !ZLastState;
ZStepPort.Write(ZLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_2 -= dx2;
}
err_1 += dy2;
err_2 += dz2;
XLastState = !XLastState;
XStepPort.Write(XLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
}
}
if ((Ady > Adx) && (Ady >= Adz))
{
int err_1 = dx2 - Ady;
int err_2 = dz2 - Ady;
for (int c = 0; c < Ady; ++c)
{
if (err_1 > 0)
{
XLastState = !XLastState;
XStepPort.Write(XLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_1 -= dy2;
}
if (err_2 > 0)
{
ZLastState = !ZLastState;
ZStepPort.Write(ZLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_2 -= dy2;
}
err_1 += dx2;
err_2 += dz2;
YLastState = !YLastState;
YStepPort.Write(YLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
}
}
if ((Adz > Adx) && (Adz > Ady))
{
int err_1 = dy2 - Adz;
int err_2 = dx2 - Adz;
for (int c = 0; c < Adz; ++c)
{
if (err_1 > 0)
{
YLastState = !YLastState;
YStepPort.Write(YLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_1 -= dz2;
}
if (err_2 > 0)
{
XLastState = !XLastState;
XStepPort.Write(XLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
err_2 -= dz2;
}
err_1 += dy2;
err_2 += dx2;
ZLastState = !ZLastState;
ZStepPort.Write(ZLastState);
NetduinoDevice.Delay.Microseconds(StepInterval);
}
}
}
*/
public
void
MoveAxes4
(
int
xSteps
,
int
ySteps
,
int
zSteps
)
{
// 3D line Bresenham
int
dx
=
xSteps
;
int
dy
=
ySteps
;
int
dz
=
zSteps
;
int
x_inc
=
(
dx
<
0
)
?
-
1
:
1
;
int
y_inc
=
(
dy
<
0
)
?
-
1
:
1
;
int
z_inc
=
(
dz
<
0
)
?
-
1
:
1
;
int
Adx
=
Abs
(
dx
)
;
int
Ady
=
Abs
(
dy
)
;
int
Adz
=
Abs
(
dz
)
;
int
dx2
=
Adx
*
2
;
int
dy2
=
Ady
*
2
;
int
dz2
=
Adz
*
2
;
if
(
(
Adx
>=
Ady
)
&&
(
Adx
>=
Adz
)
)
{
int
err_1
=
dy2
-
Adx
;
int
err_2
=
dz2
-
Adx
;
for
(
int
c
=
0
;
c
<
Adx
;
++
c
)
{
if
(
err_1
>
0
)
{
YLastState
=
!
YLastState
;
YStepPort
.
Write
(
YLastState
)
;
Delay
.
Axis
(
Axis
.
Y
,
XYStepInterval
)
;
err_1
-=
dx2
;
}
if
(
err_2
>
0
)
{
ZLastState
=
!
ZLastState
;
ZStepPort
.
Write
(
ZLastState
)
;
Delay
.
Axis
(
Axis
.
Z
,
ZStepInterval
)
;
err_2
-=
dx2
;
}
err_1
+=
dy2
;
err_2
+=
dz2
;
XLastState
=
!
XLastState
;
XStepPort
.
Write
(
XLastState
)
;
Delay
.
Axis
(
Axis
.
X
,
XYStepInterval
)
;
}
}
if
(
(
Ady
>
Adx
)
&&
(
Ady
>=
Adz
)
)
{
int
err_1
=
dx2
-
Ady
;
int
err_2
=
dz2
-
Ady
;
for
(
int
c
=
0
;
c
<
Ady
;
++
c
)
{
if
(
err_1
>
0
)
{
XLastState
=
!
XLastState
;
XStepPort
.
Write
(
XLastState
)
;
Delay
.
Axis
(
Axis
.
X
,
XYStepInterval
)
;
err_1
-=
dy2
;
}
if
(
err_2
>
0
)
{
ZLastState
=
!
ZLastState
;
ZStepPort
.
Write
(
ZLastState
)
;
Delay
.
Axis
(
Axis
.
Z
,
ZStepInterval
)
;
err_2
-=
dy2
;
}
err_1
+=
dx2
;
err_2
+=
dz2
;
YLastState
=
!
YLastState
;
YStepPort
.
Write
(
YLastState
)
;
Delay
.
Axis
(
Axis
.
Y
,
XYStepInterval
)
;
}
}
if
(
(
Adz
>
Adx
)
&&
(
Adz
>
Ady
)
)
{
int
err_1
=
dy2
-
Adz
;
int
err_2
=
dx2
-
Adz
;
for
(
int
c
=
0
;
c
<
Adz
;
++
c
)
{
if
(
err_1
>
0
)
{
YLastState
=
!
YLastState
;
YStepPort
.
Write
(
YLastState
)
;
Delay
.
Axis
(
Axis
.
Y
,
XYStepInterval
)
;
err_1
-=
dz2
;
}
if
(
err_2
>
0
)
{
XLastState
=
!
XLastState
;
XStepPort
.
Write
(
XLastState
)
;
Delay
.
Axis
(
Axis
.
X
,
XYStepInterval
)
;
err_2
-=
dz2
;
}
err_1
+=
dy2
;
err_2
+=
dx2
;
ZLastState
=
!
ZLastState
;
ZStepPort
.
Write
(
ZLastState
)
;
Delay
.
Axis
(
Axis
.
Z
,
ZStepInterval
)
;
}
}
}
private
int
Abs
(
int
val
)
{
return
(
val
<
0
)
?
-
val
:
val
;
}
// __ Device implementation ___________________________________________
public
override
void
MoveRawX
(
int
steps
)
{
MoveSingleAxis
(
XStepPort
,
XDirectionPort
,
steps
,
(
uint
)
XYStepInterval
)
;
}
public
override
void
MoveRawY
(
int
steps
)
{
MoveSingleAxis
(
YStepPort
,
YDirectionPort
,
steps
,
(
uint
)
XYStepInterval
)
;
}
public
override
void
MoveRawZ
(
int
steps
)
{
MoveSingleAxis
(
ZStepPort
,
ZDirectionPort
,
steps
,
(
uint
)
XYStepInterval
)
;
}
public
override
void
Calibrate
(
float
x
,
float
y
,
float
z
)
{
if
(
x
>
float
.
MinValue
)
StepsPerMmX
=
x
;
if
(
y
>
float
.
MinValue
)
StepsPerMmY
=
y
;
if
(
z
>
float
.
MinValue
)
StepsPerMmZ
=
z
;
Print
(
GetCalibration
(
)
)
;
}
public
override
void
SetPosition
(
float
x
,
float
y
,
float
z
)
{
mCurrentX
=
x
;
mCurrentY
=
y
;
mCurrentZ
=
z
;
}
public
override
void
MoveAbsoluteLinear
(
float
x
,
float
y
,
float
z
)
{
float
xRelative
=
x
-
mCurrentX
;
float
yRelative
=
y
-
mCurrentY
;
float
zRelative
=
z
-
mCurrentZ
;
int
xRelativeSteps
=
(
int
)
(
xRelative
*
StepsPerMmX
)
;
int
yRelativeSteps
=
(
int
)
(
yRelative
*
StepsPerMmY
)
;
int
zRelativeSteps
=
(
int
)
(
zRelative
*
StepsPerMmZ
)
;
SetMovementDirection
(
ref
xRelativeSteps
,
XStepPort
,
XDirectionPort
)
;
SetMovementDirection
(
ref
yRelativeSteps
,
YStepPort
,
YDirectionPort
)
;
SetMovementDirection
(
ref
zRelativeSteps
,
ZStepPort
,
ZDirectionPort
)
;
MoveAxes4
(
xRelativeSteps
,
yRelativeSteps
,
zRelativeSteps
)
;
mCurrentX
=
x
;
mCurrentY
=
y
;
mCurrentZ
=
z
;
}
private
void
SetMovementDirection
(
ref
int
steps
,
OutputPort
stepPort
,
OutputPort
directionPort
)
{
if
(
steps
==
0
)
return
;
//bool previousDirection = directionPort.Read();
if
(
steps
<
0
)
{
steps
=
-
steps
;
SetMovementDirection
(
directionPort
,
NegativeAxisDirection
)
;
}
else
{
SetMovementDirection
(
directionPort
,
PositiveAxisDirection
)
;
}
// NetduinoDevice.Delay.Microseconds(1000);
// Backlash compensation
//bool newDirection = directionPort.Read();
//if (previousDirection != newDirection)
//{
// MoveSingleAxis(stepPort, XBacklashCompensationSteps, 0);
//}
}
private
void
SetMovementDirection
(
OutputPort
port
,
bool
direction
)
{
if
(
port
==
ZDirectionPort
)
direction
=
!
direction
;
if
(
port
==
XDirectionPort
)
direction
=
!
direction
;
port
.
Write
(
direction
)
;
}
public
override
void
Print
(
string
s
)
{
Debug
.
Print
(
s
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL