+ i- ]3 L# r' [- Q9 q9 O' O//our direction vars % ]3 `3 C2 M( kbyte x_direction = 1; * n% T; u, j! ]0 Kbyte y_direction = 1; + E1 j$ a3 O0 P( Ubyte z_direction = 1;' i8 ]3 \6 Z( H$ D5 }# g1 F+ `" p
byte u_direction = 1;3 T8 O) T @& `( s/ v+ g
3 D9 t7 E, H* ~+ C//初始化字符串處理 3 i0 _# H* @* H8 ?! _6 K& [8 Xvoid init_process_string()) s& I7 u1 O8 ?' ]: \
{6 c# }: R7 F+ T" h* f& S, \
//init our command8 w( S1 _6 ^$ W! y+ o* X
for (byte i=0; i<COMMAND_SIZE; i++) , |0 T2 k6 h! F word_old[i] = 0; , o% P7 P, z9 @0 F) m- ]! p serial_count = 0; ' o! a0 X+ y! S3 a5 p7 u6 J b} : O% `2 S% H8 B9 I$ t" \; y1 _* p / U# }2 L! p5 [5 P; L3 ^3 O//our feedrate variables. 4 n j, D! w3 ?7 ]float feedrate = 0.0;) z B% L6 A" _" J) _9 ]& V6 E( U
long feedrate_micros = 0; 6 V6 |5 r6 q9 p4 w3 l5 E & x! \, X# \" G+ k! r+ W* s3 I/ _//讀取并執(zhí)行命令% V6 @- [( ?) Q& z
void process_string(char instruction[], int size) ; ~$ c0 |4 H% J+ y# r# b{3 Q* P% P n8 r1 h1 @( \
//the character / means delete block... used for comments and stuff.. {; `! K& ?5 F( z% D: @
if (instruction[0] == '/') " O. ?/ X3 x/ e( i {/ z# N8 k5 s& e* {4 H* L; \
// SerialUSB.print("ok");7 n [" g4 s4 m$ K- h# ^
// SerialUSB.print(byte(78)); ! `+ ?0 u& v6 V return;. M% e( e4 F- m0 Y
}8 u. ^3 I" i8 U' U
//init baby!" T0 M0 N1 _6 t6 U
FloatPoint fp;. F, ~4 j/ e. G8 w O7 m
fp.x = 0.0;/ e0 N3 h* u% F' D% E0 \8 S5 O
fp.y = 0.0; $ H5 T7 w; |/ b! U4 i. X fp.z = 0.0;/ ^* j. A4 o1 m
fp.u = 0.0;& C& z# ?/ M7 W5 T/ f
; X( d5 B L4 D' W) ~$ P
byte code = 0; 6 g0 X& x% l. _ L+ w- [6 X% u6 @ D
//顯示在處理的命令 " G: L+ L- i2 C" b6 ]#if (LCD_TYPE == 1604 )! r5 ?" @. c# i# E6 F% N6 F! R
// lcd.setCursor(0, 4); / q% M. b$ J4 `( x0 [2 { e0 ^ // lcd.print(word_old); 6 u& F: f1 Q# \! c8 \5 m7 Z#endif' f9 z. f$ H' |. b2 Y! C
SerialUSB.println();+ {* m0 Z0 P( E; _) c- Q5 W9 d$ ]$ [
SerialUSB.print(instruction);4 A6 }* ?+ |( ?0 I" [& x8 W
SerialUSB.print("\t");9 V0 K# `( G, ]. U
5 ^. w3 ~" x7 Q: i! O! k
//what line are we at? / z5 F( s) w/ M- X- d* Q // long line = -1; 7 m+ h/ D' ?8 }2 O8 A" k/ g* }9 H // if (has_command('N', instruction, size)) [: l3 O6 ]( t+ k, T // line = (long)search_string('N', instruction, size); # w( r" q, U1 q9 S4 m9 I" K3 p% ^2 V
/* : Q& h; q& i9 T9 ?$ q. T' T" L1 I Serial.print("line: "); 6 J' r/ w6 S1 T/ } w$ { Serial.println(line);- y; M4 X$ z- Q
Serial.println(instruction); " f, |2 P8 d2 S+ a6 Z */# c" R5 t4 d# s
//判斷是否讀取了個 G代碼?3 R) L: v( a W6 q0 H
if (4 u6 E# x' T* x' N
has_command('G', instruction, size) ||& c1 s6 b2 V/ F- H, C" G
has_command('X', instruction, size) || 6 x1 y( u+ X3 a% e4 x: F4 x* U* S has_command('Y', instruction, size) ||, U& t5 t$ A3 X* w1 u2 b' C- [( u
has_command('Z', instruction, size) ||9 u0 i3 W$ ^- t
has_command('U', instruction, size)% s2 W$ b; U# z7 w, H
) ' A6 W2 c; n4 U { 9 m/ O' m2 c$ R1 T //which one? 0 F b9 S2 }8 j code = (int)search_string('G', instruction, size); 3 P6 q d i( ~5 Y* k // Get co-ordinates if required by the code type given 3 X, i* s4 u# a/ ~- o: x4 G switch (code) ' n \( V" l. D5 @3 B {* E: q9 f; Y0 [% ?+ A2 N' y% u7 ]
case 0: - E# m! K; E ]- [( S case 1:4 Q- J7 v' e. ]6 D
case 2: 5 K2 t) ?) x" ]) d& K2 ]" T n5 H" B* S case 3:6 K9 Q. C% c- ~/ H% h/ q
if(abs_mode)! O5 M$ V5 z( w$ z! H4 }" G
{8 {/ L/ G8 X( \* z
//we do it like this to save time. makes curves better. ' x! R; u6 x+ v% |( t x* p5 W //eg. if only x and y are specified, we dont have to waste time looking up z. }8 t0 j' N/ n! m. l: k6 Q0 j
if (has_command('X', instruction, size))8 F# Z4 Z; ~( O6 a3 d# c. y
fp.x = search_string('X', instruction, size); % z6 p: P: ?$ W: W* y: v4 b else - R6 t9 Z! _9 z5 r% Y0 w, m fp.x = current_units.x;7 \2 c6 h0 ~$ `. @: o" s
' E: X+ g+ s1 U8 y0 W$ A) H
if (has_command('Y', instruction, size))) f: Z# m$ Q2 m+ {; x5 F. X
fp.y = search_string('Y', instruction, size);9 P3 u! p. o" K
else , K. \/ S) u' h1 `7 `2 F fp.y = current_units.y; ( y( B/ o) Z8 _$ J: x/ z- N7 C4 O0 x5 d% y5 n$ t4 m$ O
if (has_command('Z', instruction, size)) c; d2 A9 u3 {' `& m fp.z = search_string('Z', instruction, size);7 A* K. @7 m$ r7 c* S, T2 p( \
else . {: J- a: F1 g2 j$ O fp.z = current_units.z; & N+ d7 K4 h' q( z2 M: n X! D0 ~; R L* J: I' ^5 W) L
if (has_command('U', instruction, size)) $ J3 } F; x5 s+ e" I fp.u = search_string('U', instruction, size);; z0 r, v6 F2 s, x. k
else $ k( Q$ E$ E! d7 h f ^% A+ G fp.u = current_units.u; # \/ q7 ~/ r2 C! V* f0 q) Z! I } ' R" E( ^$ f0 d/ h2 C else0 y0 L) G+ {# v/ Z% g C4 N
{ 2 z! @9 z* q8 v" z% I; t6 S fp.x = search_string('X', instruction, size) + current_units.x; 7 `7 E) X! g2 _; X5 G" J" i- K, F fp.y = search_string('Y', instruction, size) + current_units.y;/ w. \- K; G0 U2 H9 c+ F0 ]0 d
fp.z = search_string('Z', instruction, size) + current_units.z;2 ~# D+ M4 X+ c, Q$ A3 ]' ?( {1 D' |
fp.u = search_string('U', instruction, size) + current_units.u; & ^. B- U$ ]% H, @ }& C- J+ s! y) T" r
break; 0 C! h& a0 X1 f* S- [ } 6 I& j7 ]$ H! {3 |( A //do something! 3 j7 ?% u4 V1 J X$ ^ switch (code) & M2 A- k# M: ^) h' J5 F1 N {% Q) R. E" _0 i+ u. P0 t/ [& h
//Rapid Positioning ) q) y/ J/ O. V/ h //Linear Interpolation# k& [9 o! @ w9 p; d% A
//these are basically the same thing. ! }9 L9 ]9 X; e$ k case 0:) `+ G! A6 n1 ^; a: y4 \5 t8 B) P
case 1:# d' v5 U5 S( M* t" [ r! q
//set our target.8 ` p7 b7 t) Q2 q9 Q# T* k- q' b8 j9 {
set_target(fp.x, fp.y, fp.z, fp.u); ?, |1 m3 L/ T4 ~# v* a
//set_targeta( fp.a);' ?5 q# U+ Z& [- `) H3 V
//do we have a set speed? 3 a4 j( _9 o% I$ z" a if (has_command('G', instruction, size)) ' ]8 V& b, b; n' M; g {9 U: a" t8 O" j @* F8 \( p
//adjust if we have a specific feedrate.. b1 H0 e, A) c6 T3 [7 S/ F$ w
if (code == 1)$ k* m9 x3 T* z: S3 Y0 \6 S9 s4 W
{. Y- X& q' W' `3 ^+ i" L- z
//how fast do we move? 9 ?- H& f$ M0 K. c; L6 L feedrate = search_string('F', instruction, size); 5 ^8 V3 h: y5 L' ~, F9 v if (feedrate > 0)* Q5 K9 B" k% w/ `, N
feedrate_micros = calculate_feedrate_delay(feedrate); 2 y5 _" g9 b @' q h //nope, no feedrate * q1 K2 O9 i! g' {1 q1 s0 \* p2 j9 p else ( B0 L1 f% i* F feedrate_micros = getMaxSpeed(); : R" @4 {: W% D' J' |9 J# J }) Q6 K: n7 z# n+ Z3 `
//use our max for normal moves. ( E# [" R8 A! [; d9 D/ u3 u else' m0 j3 @' |: k! U' r r3 c
feedrate_micros = getMaxSpeed(); + N8 z0 ^; X) E: m; C. J; E }: s1 h& m8 @8 M3 Y8 d6 w$ f- p& \/ B% p
//nope, just coordinates!: G: A0 P, l8 u7 b) y
else % N5 E) ?( c- l {$ [/ ?; {+ `. Q& A) G/ }% t' Z. e8 k
//do we have a feedrate yet? 1 e# H+ V. o) |* A K1 S if (feedrate > 0)4 d3 r- h& k1 X& f% ]
feedrate_micros = calculate_feedrate_delay(feedrate); / U% X# Z: _9 l8 ? //nope, no feedrate ) B3 l2 [% O4 E) ^, |- _8 | else $ ~" q, ?; b& O" Y2 v" S% h feedrate_micros = getMaxSpeed(); ' [9 h! L# r* N F; H/ s& Y+ j }/ F, `4 y9 Y% ^5 v
8 L. w# c4 i0 p e s7 `9 e8 {
//finally move. 7 E$ R. {, \# T" F- Z* P dda_move(feedrate_micros); * O9 e1 U7 w2 D' T. g3 l" [ if(stop_flag) return;) G% W; Z( A; ^5 ]8 m
break;4 i& a. z3 b+ V/ W
6 t6 n4 u7 h6 S x# V
//Clockwise arc& a1 m% I8 ~- J8 o2 e7 g
case 2:0 ?# {, |/ x9 s& }6 y2 D% k- M4 h
//Counterclockwise arc % L7 s, R2 q( Z4 D; B9 |8 ^ case 3:$ b! q* n. }" Z3 v: H
FloatPoint cent;5 _9 x! T. ]( [8 x/ @
// Centre coordinates are always relative9 u! f% f% P' l& {, H6 l
cent.x = search_string('I', instruction, size) + current_units.x;9 |4 g5 I* O+ \+ V
cent.y = search_string('J', instruction, size) + current_units.y;! l4 i2 @+ Q# K% Q' a
float angleA, angleB, angle, radius, length, aX, aY, bX, bY; / w. r& c, L" Y8 ?, F( d ! \7 J& b" X% i7 G aX = (current_units.x - cent.x); % A8 Z. z4 ]4 | w t. d aY = (current_units.y - cent.y); J/ M, R8 j& Q( R# R2 u7 G* d bX = (fp.x - cent.x);) H" v* n- ^9 f% ?# ]) u' v# N
bY = (fp.y - cent.y);& V0 A3 p4 I- C Q
* n* E$ W3 g5 w q6 [
if (code == 2) { // Clockwise 4 [% Y l h( v& @ angleA = atan2(bY, bX); . t( t- @3 ?( [7 H: }" F angleB = atan2(aY, aX); 6 y4 o- @ ~+ h7 y! Z } ) [+ |* W% N7 o7 \6 O
else { // Counterclockwise F! I9 L/ S# b
angleA = atan2(aY, aX);6 g% A9 U) K8 q6 }5 U7 V* o2 b
angleB = atan2(bY, bX); / Z' V. B* P1 b3 E } : m+ u5 B2 w! m a6 ~ // Make sure angleB is always greater than angleA x! d* i( d3 f) N // and if not add 2PI so that it is (this also takes ; l0 _6 J! Q2 X // care of the special case of angleA == angleB, , t& r A' t3 c$ f // ie we want a complete circle)2 F* _* Z& \8 `0 ]0 i) q
if (angleB <= angleA) angleB += 2 * M_PI; ?- m. A3 M& H0 }; ?& E B
angle = angleB - angleA; , W! e* h0 z' _, @, ]; g `) u. _* ]8 n& b1 T- d H! C
radius = sqrt(aX * aX + aY * aY); " t8 P* [' N$ W% F6 B K4 Q length = radius * angle; + Y9 a/ i( ~; D6 v1 K int steps, s, step;5 {' k; j7 \1 w1 x( F& m
steps = (int) ceil(length / curve_section); 9 z5 Z4 U6 l! L" P# o; p: t3 O! k
FloatPoint newPoint;5 y. H4 s, q& d. H3 t; g
for (s = 1; s <= steps; s++) { . M0 t! h v4 L3 r' { step = (code == 3) ? s : steps - s; // Work backwards for CW ; u! K5 F% B, _ newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps)); % H* S1 m# \3 d c4 ?. R. W* H newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));+ k. K8 K9 I' x
set_target(newPoint.x, newPoint.y, fp.z, fp.u); / U, H; a) Z$ e3 U0 [1 a" f& M3 c% o' T1 M6 i
// Need to calculate rate for each section of curve6 o% s+ f8 q. g: V
if (feedrate > 0) 6 ~: R8 ~ ]! [- E feedrate_micros = calculate_feedrate_delay(feedrate); / z& [( h2 T5 r/ f: }( N. X( v else: V' I/ Y5 F' r2 j- \& ^7 ^
feedrate_micros = getMaxSpeed(); 2 @, n- S6 l0 a: Q5 @# W" J5 n- X. h O4 i& N& t; x // Make step' Q z. E l+ L! O
dda_move(feedrate_micros);* l) Y6 B6 ~2 {8 I2 v
if(stop_flag) return; 1 C1 r$ W# b, s# Q6 T }+ P- s6 Y' C7 I
, D4 ~% W) Z; [" h* m break;0 H* a5 T1 ]4 X$ {. d
2 w% Z# @2 ?0 Q0 s
//Dwell " p% K8 p [$ |0 A6 V& U case 4: 2 S, y: ?- D4 g) s6 i# p" K delay((int)search_string('P', instruction, size));9 R, Z# Z8 b; G% N- c; k
break; 9 @& f; H8 x4 a% ` 9 D0 W% v) |$ l$ f. A+ V //Inches for Units3 J2 f0 a( I8 o
case 20:% g% ] V# O( D- n$ C; I4 f% F) T
x_units = X_STEPS_PER_INCH; 8 v! _3 G6 a2 n y_units = Y_STEPS_PER_INCH; ! ? S8 C( i5 t! }2 Y z_units = Z_STEPS_PER_INCH;: t' t/ B5 I/ v8 ^+ s0 u1 ~
u_units = U_STEPS_PER_INCH;0 C' U# A9 z6 h1 o1 f2 A* R
curve_section = CURVE_SECTION_INCHES;) z4 X2 U i0 [% K9 R
calculate_deltas();5 z1 _) H0 W. B# N7 P
break;, t% D0 ?6 Q' J% J% ?# H
0 b2 j+ k- e) t/ y+ y
//mm for Units5 b8 ^+ v; Q. L4 r: W* F7 U) \2 F
case 21:5 V- I* c6 w5 ^6 _" L9 R! a0 B
x_units = X_STEPS_PER_MM;3 W. }( ^0 N8 \* _# u0 Q: U
y_units = Y_STEPS_PER_MM;- C7 j# Y! h1 @& x
z_units = Z_STEPS_PER_MM;1 ~2 x0 o* R% m5 ~" j; U* ~ C: W+ O5 o
u_units = U_STEPS_PER_MM;; a9 y8 N, `0 I- ^9 T4 f8 a
curve_section = CURVE_SECTION_MM; ( p; ~/ V* r' L5 n6 i( L calculate_deltas();& }) C4 `" k M# d. f
break; # {8 e3 c* {: t4 i' X: g & @2 ]' `5 g* V, p //go home. 4 e, B* T3 A+ q' ^7 ]0 O7 G0 n" w case 28:8 x, w; R( p: B8 j v% [4 {* ~
set_target(0.0, 0.0, 0.0, 0.0); 6 G0 x% Y8 D: b0 K9 N, a dda_move(getMaxSpeed());( k, [$ v& e1 _
if(stop_flag) return;: [% @" [ n k9 k* u x7 g
break;7 Q; @" c D& u( s3 m
$ b K* I; ]2 l0 v' T
//go home via an intermediate point.. c! p- ^" c! q( K
case 30: F- N- o: |. h0 @' r. S9 Q
fp.x = search_string('X', instruction, size);7 M; w3 n3 R/ D, F; m. `
fp.y = search_string('Y', instruction, size); 3 `6 ` Y* y- Z- I+ N fp.z = search_string('Z', instruction, size);- T6 ^; @6 Y4 L3 ], M) ^) n
fp.u = search_string('U', instruction, size); 4 E$ F5 c; x+ ^; P //set our target.# x& v7 N+ W* ^5 y C2 i) z8 f9 k5 }
if(abs_mode)+ m, V3 M! Z& P/ v7 D/ u* x
{- w g: e, z2 `2 G- U" H; B
if (!has_command('X', instruction, size))2 j: N+ X' M2 q1 v X
fp.x = current_units.x;# K0 ^$ i# ~8 ?) T; D7 v' v7 s
if (!has_command('Y', instruction, size)) " c. N$ L! |2 x* K' h% K, ^ fp.y = current_units.y; ! F5 K3 C) a! \+ s if (!has_command('Z', instruction, size)) 1 N3 D8 J7 W; V& Y5 h h fp.z = current_units.z; ' E- ]$ u' C1 U8 ? if (!has_command('U', instruction, size)), c! `* ?: f" k+ E: x
fp.u = current_units.u; 3 h/ e1 F z- l% C. f" R6 k set_target(fp.x, fp.y, fp.z, fp.u); 3 j6 m0 ]: p, K 6 M- S( a3 m' h* M0 w6 S) M% e
}( K2 s/ n! v/ A5 r* p7 p
else 3 M6 O" o( l8 r, f& c' K set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u ); 2 ?( K. W$ O, A. B9 v. t % J0 U: e% A- m //go there. ! T ?7 h; y" M; X6 o O" ]" `7 U dda_move(getMaxSpeed());" `! W( ~3 V" T5 [( O5 g: f f
if(stop_flag) return;* M4 |& `- M! O0 a( w
3 z* u, T% v* t. b3 y# i% v
//go home.. Z, H3 n8 v1 d6 @0 y; C
set_target(0.0, 0.0, 0.0, 0.0 ); y8 l# y. d9 T7 z 8 K' h* o$ j' s$ Z- u
dda_move(getMaxSpeed()); : p+ s8 {& }2 e8 H: Z, { if(stop_flag) return; ?9 U# f8 s/ Q& p+ l+ \ break;: y( U, ]* _) D4 q! J( u& E8 A
, b! ]4 i2 b0 S- n
//Absolute Positioning1 {0 N- _+ ]0 P2 q i7 V: o
case 90:! P$ |' N# Y8 w- \. _. q; r
abs_mode = true;- \- k/ }" f# E
break; ) }0 s/ j, P' X: i1 A/ t0 J" D& ^1 ]. V
//Incremental Positioning : N; a6 j0 V6 Y5 s! J5 `& B! V# b case 91:, {2 l5 j6 o7 [! f
abs_mode = false;8 r/ }& H" L% m( ~" n$ B
break; + m" g9 _& G1 c% k- t 5 W- p5 Y; J9 C2 d/ F; Z1 Y2 C //Set as home9 o2 Y5 t8 Y# {
case 92:9 b/ [- U9 f' y# Z: u
9 O4 _8 a2 K, O, G7 `' b
set_position(0.0, 0.0, 0.0, 0.0 );2 g( u# b+ Y3 b4 ?5 S' m) I7 E6 L4 v
2 l; t. P+ h U) Y0 b2 T( }( ~+ }4 L* n+ Z
break;1 D+ P: u) K6 ~9 N1 y
( x5 _ a9 J, Y& a2 y! w
/*3 v. d: Q: S: L
//Inverse Time Feed Mode - x; P2 T: m! }" h0 y0 L case 93: . v5 f9 a, C3 f" h + ~- j) ]" o& C
break; //TODO: add this , F' r* T' Q# U; T$ t7 D : p9 _. Q, Y; q* ^ //Feed per Minute Mode/ |4 d. N9 m9 v" N
case 94:$ `; ^ E3 w" k( M$ ]
+ S' o7 ]% ~- L' k$ }+ v; D2 e break; //TODO: add this2 U, M0 X+ a: Q& W1 ?. G4 M
*/& W2 e- P) }5 T
: |% V, m5 ]) ] r! h5 V! d
default: * H5 y1 R0 M( N2 P: t: J9 i! r$ X SerialUSB.print("huh? G"); 6 [! d1 k- [6 M' P- a. S" V SerialUSB.println(code,DEC);$ s) g1 `3 @# J. k' k2 s
}1 G$ J6 a' l9 W3 t
} ; Z) }- P4 @. l9 a6 E0 v' i . p: I3 c( c& H9 e9 s% {' p( y //find us an m code. 5 L& C e* y9 x! B7 T: R if (has_command('M', instruction, size)) 4 s! ?% T# o5 ~2 k q5 h4 W. ^. C {. E8 i/ _4 K: H# {
code = search_string('M', instruction, size);1 c3 b% T# ^5 e0 e* g1 I# J
switch (code)" i6 Y: C4 n% B7 m: N! P* y R5 I
{; T8 R+ ?. @) S+ V% E+ v/ A5 R
//TODO: this is a bug because search_string returns 0. gotta fix that.2 g1 T2 {: ~8 G% i; d8 X% |" `/ p
case 0:. Z* G+ b1 w- R G
true;9 {1 M4 h d0 e$ J8 T
break;6 g0 _% o& F: C( U
0 q0 y) G' E' Y6 r
case 100: 7 U" G: k4 @, a break;& K+ i) p$ I# \3 t. V5 H' M
8 U$ w& Z& L) J1 Q8 ]2 W
// fire camera relay9 U& S8 P( D' ~. S8 N- c+ X
case 101: - j6 a/ D6 E1 @3 n7 _ camera_shutter1(); " Q/ V: c% a7 K. M break;- ~9 D& f$ t& o4 Z& r$ {' l# g
. |' J9 k2 @, x
// fire camera relay20 x- U5 G6 r7 k- S8 g
case 102: + V* b1 v0 X% C5 u camera_shutter2(); % g+ g5 o; j, D break;" B% E! c3 V9 C! A9 c( g
/* 6 T }( C4 `% U' Y! K/ l" p // turn aux 1 relay on - [7 N' T& m7 O6 U/ g" V case 103:0 B B9 T1 M3 t; V T3 ~
camera_aux1_on(); 9 }) F3 s3 L; o Z; f break;& `2 g3 l* R8 _5 w$ L7 m
4 Z6 Z5 K9 B) k
// turn aux 1 relay off1 |/ R' G7 f4 O, ^
case 104: : z8 ?% W; |) @6 v' f: @ camera_aux1_off(); A7 z4 {3 A! f7 u1 H! I
break;( P- J$ w7 U7 ^: C: {
+ D! }; P# M3 p$ q( X4 |
// turn aux 2 relay on + `) f2 N! G. W1 U6 G8 z! y case 105:& i" C! p3 ]0 ^; M0 n- s
camera_aux2_on(); & K9 X6 P5 m5 [4 V: y4 a break; # x- K/ \; t+ }+ K3 E/ {$ N/ V( ?1 t& f$ l4 X! f5 F
// turn aux 2 relay off " I$ o4 H) ?! p# } case 106:- ]4 ~6 _( J. B. v. y
camera_aux2_off(); + k! k. t' s2 R4 G break;$ ^7 t! s, t- k( N( v8 ^* ?# [
. Z3 f b0 n& o1 {; } // turn aux 3 relay on+ y" F% R+ z( N; q, {3 @
case 107: " s) k3 J1 ^* ?8 K* L _: ^ camera_aux3_on(); & b. o8 H) @2 Q1 i5 o7 G break; B2 S; `0 _7 {0 I
- M! K0 O6 d4 n! q- h# Q, m9 _ // turn aux 3 relay off : r: D: e ^$ D2 q case 108:$ U" z1 y( e- y
camera_aux3_off();. F {8 i1 \$ f
break;# ` S& m. U! c
6 I* S+ m+ m; u3 q6 V0 \0 h // turn aux 4 relay on ( T3 |2 g- e/ b case 109:- X& Q7 Z' G! v9 G; }+ |7 S
camera_aux4_on(); 0 f5 U5 c0 X4 J9 {' T break;7 ?' g- v' b7 i; s: f9 \
7 X L$ a/ y# W9 p% H* |6 x
// turn aux 4 relay off! U& W3 ~) w2 _% x- g
case 110:% R, ?+ u6 E& |3 v$ {9 i$ L
camera_aux4_off(); * U1 j- b( B. g/ y* l$ s break; 1 I8 \* B) a% y% u' @*/. A4 Z' | j; x6 s7 I$ N
default: ( T1 F7 |7 O. m! ?' |) ^4 b: l; n0 N1 \6 w6 q
SerialUSB.print("Huh? M");" H) R( S# e" F) U
SerialUSB.println(code);, w/ S3 ~. J a) f' ~
}" O j0 A9 H+ u6 ~8 G- X" P
}* h9 h8 N/ V3 [) C8 M
+ k1 \( N$ M6 w1 l, \
//tell our host we're done. 7 q; H, F+ m; c- d8 E7 W0 n, f, e SerialUSB.print(byte(78));( |8 I1 M; o4 n4 Z" k1 K' W/ s7 L$ Y
7 p' s! g( J; i, a9 ^4 z) d}/ C0 R: K( E. u7 [0 z' r
9 g! M0 Z2 x1 j! Z1 W//look for the number that appears after the char key and return it2 ]; H! t3 E/ O3 n$ j O# R( H3 y3 c% z
double search_string(char key, char instruction[], int string_size) 0 s" y; u ^ N9 O{ 7 [' o' ^2 ^9 _$ l char temp[10] = " "; 5 R; c1 V) x) o e O for (byte i=0; i<string_size; i++) 6 D' k5 U& j# N) e! F {; W- h0 d1 \8 _6 q3 d) x2 b7 B* O
if (instruction[i] == key) * C* j, Y/ W. N7 q% E { ' m0 v/ }8 ^# t6 l i++; c6 i& ]- p$ W* O int k = 0;5 m1 I" b) h+ ?7 ~7 d5 D
while (i < string_size && k < 10) . C9 G) L8 P7 e! c7 h* p { i. i; M4 H+ d if (instruction[i] == 0 || instruction[i] == ' ')* p+ a, L9 _$ g* g
break;6 A* a7 I' m% Y
L3 X2 q# q4 {+ j. W
temp[k] = instruction[i]; % S4 H' T8 S' U- b1 b7 J3 u i++; + d, q: c! s7 O5 p) _ k++;* R S! k! Y; Q' r: Z+ y& {
} 8 g3 p- y+ _& t, z* _ ]4 F return strtod(temp, NULL);5 {6 {! I3 |* g7 J
} # X% A" o1 k/ e7 I6 a6 o' v }6 k y% _- K4 _6 G! f
0 S/ f0 m: U- Z0 T5 b; T% B# x6 H: a. D' X return 0;- W! E6 B! f& v' s: Y# ]! V
}- s% D7 p. b9 a& ?3 ~" `
, F9 J! ^. x8 X/ q7 n$ A
//look for the command if it exists.- z) \( k' v0 R; `4 d
bool has_command(char key, char instruction[], int string_size) L/ ^: j2 ?3 c& O' Y! d: b' r
{& a5 f2 F/ _/ y" u, Q, f) F3 \
for (byte i=0; i<string_size; i++)* }5 k X7 m; p- g4 x: E1 t
{ 1 A* ^5 S# c: N* O& r. [ if (instruction[i] == key){ , c3 K3 U( c, N; K; ~. J- v' d+ V$ N' x3 g: a5 l
return true;5 Q# u+ \: R" P3 l. x& b
} 0 c' Y( q. _: ~4 _- Y }: F" H, E& p: v( z/ o5 Z2 e
, r5 ~6 ^/ ~; q5 x0 V' R return false;: l9 f. ?* \1 X
} 8 K; H8 z, v4 Z: g) S3 P$ x9 F( y& C6 C
: A: N; A2 O% [+ x # [& s E N( A' S作者: xmdesign 時間: 2014-5-10 09:55
, {- b9 l0 ^1 e: {8 P
//init our variables 3 a- \, I0 I5 J9 c: \) llong max_delta; # ? t) ?. J+ W- t+ A. jlong x_counter;: [. i0 @$ H7 ~; a& c; J/ n
long y_counter;4 z% M) z5 C# {% e
long z_counter; 9 f% t$ R: N: [% ] Hlong u_counter; 0 V- Q% _8 M! b4 P' L$ ulong x_pos = 0; // x position in terms of absoloute motor stepps / p, ]$ Q; b/ U, E! |& D# ?long y_pos = 0; // y position in terms of absoloute motor stepps7 ?$ T, I: p i& k' h6 q8 D% K
long z_pos = 0; // z position in terms of absoloute motor stepps ( C7 B1 N: J) p% P3 {& K1 Plong u_pos = 0; // U position in terms of absoloute motor stepps* ] R+ L! M. N. z
' t' P# z/ c8 b6 O; O1 C
bool x_can_step;, R" B0 _2 x! e% S
bool y_can_step;$ z m8 C+ f: d
bool z_can_step; 7 o: ]/ i9 U9 h/ ^7 ~, @' hbool u_can_step;; c" P; k2 j( E7 U/ A
int milli_delay;' }0 s5 @2 M4 P
# H$ G5 h8 }) T7 Z# O7 Wvoid init_steppers() 5 @7 P( S2 v: _6 H7 K# w{ 7 M) P: }; H0 m0 H9 V7 H //turn them off to start.- ?. F- @5 | Y+ F: l' F
disable_steppers();4 n! n( _1 c$ o
8 S: p+ ^* l& W, F: U' } //init our points. % {) _: R1 l7 n* V current_units.x = 0.0; z- g/ `7 c9 [1 `% Z, i current_units.y = 0.0; - p! B) p4 k* _8 _: k1 `1 p current_units.z = 0.0; , c+ ?+ d; F6 l0 D& }- Y current_units.u = 0.0;% k" s6 f. v4 Y
target_units.x = 0.0;: M6 \4 ^' r8 X( O1 w" C$ m
target_units.y = 0.0;' X7 s a! y9 m
target_units.z = 0.0; 5 w6 ` f. A4 N2 m: Q w- y target_units.u = 0.0; ( v( R5 g; E, B* y' P & o. c: I4 i# g8 ] D' e( X" _( i; a
4 h- ^6 l4 u8 J5 K0 y' |, A& k( U pinMode(X_STEP_PIN, OUTPUT); % g+ n* L: y5 ]* A. z! D pinMode(X_DIR_PIN, OUTPUT); ( ?9 h! Q( A$ I7 ^6 @; w pinMode(X_ENABLE_PIN, OUTPUT);" u" r7 }+ s) Z/ N" u
pinMode(X_MIN_PIN, INPUT); ; c: L; M& N6 L pinMode(X_MAX_PIN, INPUT);, N* b7 X% A" v/ m
) h' N: y0 m0 X$ z1 x$ d* W7 B ? pinMode(Y_STEP_PIN, OUTPUT);0 j9 y- f# {1 g9 B. o
pinMode(Y_DIR_PIN, OUTPUT);) C( q. ]5 E- M4 \0 o0 I
pinMode(Y_ENABLE_PIN, OUTPUT);- ?( @. _1 v: _3 z
pinMode(Y_MIN_PIN, INPUT); 7 d; }7 T- H1 x% H* j; l pinMode(Y_MAX_PIN, INPUT); 4 G9 l% ?! _7 s/ P + Z7 x/ @1 u2 h' t* j pinMode(Z_STEP_PIN, OUTPUT); 6 h! w+ S/ l) u2 ?& _) |6 v pinMode(Z_DIR_PIN, OUTPUT);' @ G* f- n' I) d5 b% ]$ r
pinMode(Z_ENABLE_PIN, OUTPUT); ; L' ?& B: L$ V { pinMode(Z_MIN_PIN, INPUT); F, j- x+ p, Q4 S6 e pinMode(Z_MAX_PIN, INPUT);5 Z9 K$ J! Z* r- d
' m1 N2 t0 A( Z6 p$ }; ~7 V! i j pinMode(U_STEP_PIN, OUTPUT);$ g) v2 f& h5 u0 {0 F( n
pinMode(U_DIR_PIN, OUTPUT); . D; ^8 X; L* z pinMode(U_ENABLE_PIN, OUTPUT); 2 ^/ V$ t9 l/ K+ G$ ?$ ? pinMode(U_MIN_PIN, INPUT); ? |0 l* w, b6 H' |3 Q5 }
pinMode(U_MAX_PIN, INPUT);) O: J% T& g7 x8 n; a" x
//figure our stuff. * H$ U5 p2 u$ N3 C3 { calculate_deltas();; B4 m# M' S8 b+ l
} . m8 H0 z n, `; q+ O% ]7 L* f# F% p# l
void dda_move(long micro_delay) : N& d: O& [" f{2 h# H r& {( m, l0 ~. c
//enable our steppers . I$ s0 s& f2 F digitalWrite(X_ENABLE_PIN, HIGH);7 @) @* ]% E: W; X$ g, T
digitalWrite(Y_ENABLE_PIN, HIGH); 8 |" l K! r" t" @ digitalWrite(Z_ENABLE_PIN, HIGH);5 x! H# w" [- j. q% i. T8 Y Y" W
digitalWrite(U_ENABLE_PIN, HIGH); ) B$ N; M- w# |4 k1 O/ B //figure out our deltas 2 U/ l' x. J; E4 `3 I max_delta = max(delta_steps.x, delta_steps.y);5 G% O t" _- x9 N( _2 c7 S" \$ o
max_delta = max(delta_steps.z, max_delta); 1 W4 h1 @0 A- T9 \9 f) K' Z max_delta = max(delta_steps.u, max_delta);+ U/ a. }0 d9 X, w5 Z; M
//init stuff. 0 K1 j6 o( K% M( h9 P1 b long x_counter = -max_delta/2; 5 Z5 r, j- \3 g `8 U* Z2 {$ r long y_counter = -max_delta/2;% T* M t4 n1 Z4 |9 c( _% t' ~( Q
long z_counter = -max_delta/2; 9 b; i+ g2 z- J. r8 ^0 t0 O2 \* e2 ?6 E long u_counter = -max_delta/2; ! R: J5 N7 C& |+ N* T) W & F' D, _2 d% n! Z; I) x3 h //our step flags ! R/ c/ ]) a) T1 S bool x_can_step = 0;0 E. a: E. n+ g3 i6 v( q) [6 o
bool y_can_step = 0; 0 R( r; @; L# {1 N bool z_can_step = 0;! S* b5 L1 r' n8 O+ N1 o4 Z
bool u_can_step = 0;" U# S7 ?" f- @/ G5 @/ U
, e7 \: s T3 Y8 K% ` if (micro_delay >= 16383) ( j7 @% ?! i* O4 w+ X milli_delay = micro_delay / 1000;- \; h0 p& a! Z
else3 R+ w2 f5 O; G1 Z
milli_delay = 0;: N; I, ^% z Z- m! S
+ v' t6 P3 c5 ~ u. ]: G* Q5 V1 g+ C0 \* r8 ?: q+ T
//do our DDA line! A1 w4 }" P& l; g( l8 B/ i# I Z7 _8 B( d
do 4 L1 B6 m5 |2 S7 a0 e- } { % W1 D% g" I1 i2 h1 S if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break; 6 r; l5 R. G& K, ?" M x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);) ]6 U* } s8 b1 h9 Q
y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);* q( B, k% \" m1 |& F9 a( |
z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);- f3 Y" l' o' P& i* R; x2 O* w+ Z/ x
u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);( y7 P* H4 r' m, k3 K
if (x_can_step), ]) w; [$ {, r Y) d7 M
{6 T+ Z5 w \- O3 N( h
x_counter += delta_steps.x;; _. ?+ L) V* B- o- C! U# a! [3 L
" x9 N& D% S2 @( x, M1 C1 C if (x_counter > 0)/ s7 L+ }% Y8 @4 N: ~# G4 R
{# U: t3 |# D- p: h
do_step(X_STEP_PIN);1 e; b& h$ F+ `1 D
x_counter -= max_delta; ) N0 {$ U; j+ Y' B2 ^8 x if (x_direction)8 T7 q+ S7 G0 g2 \" P
{ current_steps.x++; x_pos++; } + n% C! `: ^$ A2 Q # z( G4 N; N# X8 @' k7 \/ L5 M1 ?& ^: y else ; v2 }% R9 k' S2 y* Z) s* a7 A { current_steps.x--; x_pos--; }$ N* T0 ?5 k l q
$ N% M; z" w. z8 s3 b }" p; t! C7 P6 P
}* z8 K0 p/ C. Q2 f- C
if (y_can_step)* i7 o3 O2 n# b( F$ k# m
{ " q2 ]0 f& E4 g2 J y_counter += delta_steps.y;: M+ s+ n0 ]5 n) e4 t
Z- u6 x. ]% e' K1 C/ T if (y_counter > 0) : e$ A( i8 e; o, {5 x { 5 S- g9 D5 m1 R; `1 j do_step(Y_STEP_PIN); 3 l) t; y9 t% |6 r y_counter -= max_delta;6 ^3 D8 f/ t+ p
# w# P" t# g: i4 o% i9 n' W if (y_direction)' `! \& }; x0 i$ q4 Y: D
{ current_steps.y++; y_pos++; } 7 x( w+ q/ R5 g5 Z3 W0 L $ B; T' c. A' N8 b6 X else ( J e h3 V1 S: }+ a8 T { current_steps.y--; y_pos--; } 5 z4 W6 G6 Z$ i* _4 y$ z % Q0 b4 m N6 ?+ M! R' H9 A } o0 b& u. h' o4 w2 h
}2 W( H- ?! x" X* E" m# u" i
4 T1 ? K. V4 ~& x5 ]
if (z_can_step) $ u$ P; X+ b* z5 y ^ { 7 M5 i7 |6 l* W/ n+ j5 v z_counter += delta_steps.z;4 y9 C$ j; U% s
, b; r8 h J+ g3 V1 k5 I! \
if (z_counter > 0); b+ s b9 p. i* x' V5 J
{2 Z7 H a; [6 F- A7 o
do_step(Z_STEP_PIN);, _2 g% p3 \2 j1 w7 i* H
z_counter -= max_delta; - V6 y; A# e1 C4 ]1 {9 _: Z Z. } 0 Y/ S8 y c0 [4 O- r if (z_direction)9 z3 z7 q- r6 |) `" F
{ current_steps.z++; z_pos++; }) k, r% |+ e$ } V
2 I7 l3 M. B) a0 z8 B- q else 5 k5 X# @" m0 ~" c2 | { current_steps.z--; z_pos--; } 6 V+ ^' a3 {" F4 n/ y 2 n7 f" H W% K! X( y# m
}( r6 P6 ~9 W, V$ i
} $ j; O2 d- j' R# n% h& ~: E+ y) b o( t( G! p' U9 H. L( y: v% w if (u_can_step)( D! u% x' G# F, g- R6 R/ ~
{" y8 n% @. V$ e* }% |- Q) ^
u_counter += delta_steps.u; 0 ^- w( ?% b* M/ B8 h; x) f; y$ d+ G0 n! z8 t
if (u_counter > 0)# @ @ m( E/ ~) A8 D
{ 3 [( i! @2 [- j# a( E/ n do_step(U_STEP_PIN); ( D5 |3 ^' J Y3 [/ t7 l! p u_counter -= max_delta;4 q X8 b8 g* l" P+ l# [0 _' R r6 Q
( K9 @ {# `7 B if (u_direction) % x6 m! c1 w- K/ P. H; x { current_steps.u++; u_pos++; }$ Y) i- s* s9 |
3 x: n$ a* ?9 H) _9 B else # }' a r, U) h) X { current_steps.u--; u_pos--; } 7 k) C8 C. y4 i/ g( f & Y2 f' ]( p1 S
} 4 b: D+ O! r, z; ~% u$ y/ _' D9 M& h; K } $ ^. o, O/ F+ U2 o" @ //wait for next step.& r: U) T; o7 L5 Q- D+ ]0 m6 S
if (milli_delay > 0){$ \& _8 H e/ ]/ @/ ]6 R
//if (digitalRead(BUTTON_SP_EN)) SPEEN();% g- `3 ]6 n$ o) M K) o) A5 f
delay(milli_delay);0 `/ [6 M+ v! G4 X# S: }
} 0 B4 F5 M3 ~4 w: g d( D else{5 }- F' i: k+ L9 D P& c! m
//if (digitalRead(BUTTON_SP_EN)) SPEEN(); % y' m. e8 g9 q- a T if(micro_delay>0)delayMicroseconds(micro_delay); h2 M7 @; m# K/ M* f }& f+ l' a( I: E _
//if(x_can_step%40 || y_can_step%40 || z_can_step%40);3 F% M A% ^7 m; M7 _
} 8 G& X5 N) X j while (x_can_step || y_can_step || z_can_step || u_can_step);' @5 a) i8 Q- a) R( ?# H/ H
V$ _1 \7 p( y% t U7 g; z/ B6 Y: ^ //set our points to be the same - P; Z1 @: q& d( s, A current_units.x = (float) x_pos / X_STEPS_PER_INCH;. s5 p3 \0 L) N7 o! C) z
current_units.y = (float) y_pos / Y_STEPS_PER_INCH;6 r# N( N: n- O! ]: Y) L' C
current_units.z = (float) z_pos / Z_STEPS_PER_INCH; ' p2 H" E* a' Y# b current_units.u = (float) u_pos / U_STEPS_PER_INCH;9 E4 X( d! Z, n# a! l D
* [3 Y& ~; y* A: x( U9 l8 R
set_position(current_units.x, current_units.y, current_units.z, current_units.u ); ) \6 D5 r" u% d( {6 N ! D6 C" |. y) f+ Y @3 q1 P long x_pos = 0; // x position in terms of absoloute motor stepps" z7 ~0 m+ M) S& E2 o4 b+ t3 H
long y_pos = 0; // y position in terms of absoloute motor stepps7 \- ~4 a9 T3 P; ^' k; y6 T
long z_pos = 0; // z position in terms of absoloute motor stepps & S! h5 Q. T' Q0 q6 w0 k long u_pos = 0; // u position in terms of absoloute motor stepps# U1 X2 M: D# I0 {# v6 h
calculate_deltas(); $ y, J& k M) {. H 0 _8 C% l; W$ N* G3 ^5 d7 g' w}: `* {8 f G B% u. O
C/ q0 p' f3 w; }% {( b* v
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction) " \8 U: t8 h8 m; ~{( o; V7 z0 F7 h i g* |
//stop us if we're on target- `7 k3 y3 m( t/ |$ w7 ~; f9 y
if (target == current) $ [; `% W# b! { return false;' f7 n, c7 _- \4 s
//stop us if we're at home and still going : k5 S2 J3 p& ?1 ^5 J( @, R
else if (read_switch(min_pin) && !direction), r* Z% c! f& ~1 i8 L9 \& V
return false;9 q# J$ e O' n0 i! K" N p
//stop us if we're at max and still going% C; s( ~, j" W9 N1 K; {2 P% o
else if (read_switch(max_pin) && direction) , [. Y9 u* N; q( k9 x" }; f, R C return false;# y8 e& P4 v6 k
6 m5 \2 G6 G* D0 b
//default to being able to step e$ S5 g! F/ R N6 r; K return true; ( S: Y {5 u5 ^1 K} $ Q# U5 t: N2 m& L/ q: x % l1 |8 z2 ]. d/ [& f) ^+ gvoid do_step(byte step_pin) ' ]" z# q' W3 l3 Y" J$ f; {{! Z; X" A5 A, @; R$ k9 C
digitalWrite(step_pin, HIGH);9 A5 j3 k) c) y8 G
//delayMicroseconds(1);% y8 w6 d: D7 \+ m- o' m" [) d" y
digitalWrite(step_pin, LOW);9 `% T# T" T( e/ H+ \, t2 D
} . R0 n7 k- S* z. R# y 0 S0 D/ [* ~0 J+ @8 \# ~% ^bool read_switch(byte pin)9 f6 i" I3 I' G
{, `2 c8 k; |, C: h* A2 _4 y
//dual read as crude debounce / x3 P4 f5 O/ d * ]. `' \$ b9 @7 Z if ( SENSORS_INVERTING )& P; a1 f( @6 }& L. l- t
return !digitalRead(pin) && !digitalRead(pin);& g- x0 X8 q4 ]# ]( r0 i
else + t2 e: h' I6 [! G& u' _, s return digitalRead(pin) && digitalRead(pin);9 C1 S, }4 Z: q: L2 ^
} ! l# u6 r8 O6 Z% K) p( k! m " ^7 x Z, Z; p8 u, p8 n" Hlong to_steps(float steps_per_unit, float units) , `1 V* Q- t6 U' O7 G' \. G{; B, f6 {5 d6 Q/ L. {; K* I
return steps_per_unit * units;6 I7 `2 m* ]: M. l7 m! C1 E0 H# c
} - {2 f0 z+ W2 k& G6 k: s4 ^- Q; a6 B3 _. {
void set_target(float x, float y, float z, float u)% G& i7 }$ W9 K/ A9 a& t( y1 x
{ 0 O2 K3 _* n( {5 U3 N+ l target_units.x = x; ) y7 j7 g+ B$ c9 @ target_units.y = y; * y: L' D' \1 c$ p i- R target_units.z = z;9 ~( {! [3 H5 S0 a' V' V
target_units.u = u;/ E# r! `5 s' G
calculate_deltas(); , R" d5 H. X+ Y/ x}( ^+ D: _0 T! J
5 t3 c A& s# \; X3 h5 ^2 _
void set_position(float x, float y, float z, float u)' l( X$ P2 ~" q [
{+ y$ t0 `. g- O5 Y
current_units.x = x; $ H a3 y/ `9 [' l8 y0 T- i current_units.y = y; 2 a6 k6 C, Q: W& u+ l( C: ] current_units.z = z; * e) k7 U9 L" E current_units.u = u;8 R9 D6 Q. { e4 R% L3 I
calculate_deltas(); 4 c& ?7 |% B9 l}/ a4 p% c" F% V# D! ?' z" K
4 G, A. t& i# b) k W+ {7 T! e* ~6 H# y& s: M" n+ N1 j
//what is our direction+ S9 S7 I" p5 K3 ?& O3 f% B* C
x_direction = (target_units.x >= current_units.x);7 m# q( B( |! Y: J5 J6 Z
y_direction = (target_units.y >= current_units.y); 7 o0 T( ? T4 Z! i9 c2 o z_direction = (target_units.z >= current_units.z); , q9 O4 H! K# z- ]) o: @% P5 i" M u_direction = (target_units.u >= current_units.u); . i+ y) i- P1 c+ y1 n, [# d4 I! C J, i3 g0 }
//set our direction pins as well + }+ ]" L& v9 {5 v# G) B% J' r digitalWrite(X_DIR_PIN,x_direction); / N7 O3 b+ j6 S/ [3 K digitalWrite(Y_DIR_PIN,y_direction); , X+ g& N& p, a3 m) v- h0 F6 Q Z digitalWrite(Z_DIR_PIN,z_direction);1 x: m+ y) k7 ^5 J
digitalWrite(U_DIR_PIN,u_direction); , V9 `% H9 c) M3 F7 Q % j& A2 y& }# H1 d% H //繪制LCD / `. ~0 x* l$ g4 X2 i LCD_DRAW(); h D3 ?/ I6 T' P, Q8 F" ~; h, ^
} - e$ D/ }2 X# y: q" h7 ~1 Q& ]0 I- B9 Q" D( ?( f9 G: K3 h
& U: R0 v; A* t* f. l$ S1 glong calculate_feedrate_delay(float feedrate) ' c, o5 _6 }7 k+ j' N( h{% s4 g2 o5 f9 W: F3 Z$ u. A
//how long is our line length? 0 M8 g& |3 d) k( e/ n6 m8 c$ O float distance = sqrt(delta_units.x*delta_units.x + delta_units.y*delta_units.y + delta_units.z*delta_units.z + delta_units.u*delta_units.u); 8 ~# c" e2 {& M long master_steps = 0;: x; C+ E" }$ t5 h
- e( j' \6 a0 s% e9 v
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y; . w; A6 F. y5 [) b* s. Gmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;( W. n& C$ I2 B0 Y) k8 Z* z5 j. N7 O
master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;- {1 C. |9 ]1 b- Z; \6 o2 a5 [3 T Z
; p. u( H; _7 }2 }& R( s! Q
$ b4 ^6 H; j. [4 Y! R( ]& M+ | # B$ X3 X8 l! n, K' l . n% ~. z" T2 T1 u. |6 i //calculate delay between steps in microseconds. this is sort of tricky, but not too bad. 0 x( m W9 i. P! a6 B //the formula has been condensed to save space. here it is in english:% D, z2 o. W. q% \
// distance / feedrate * 60000000.0 = move duration in microseconds 9 r7 D% [5 I1 z! I) {0 D5 ` // move duration / master_steps = time between steps for master axis. 4 W7 t' Z( L& S0 w- ^7 R% w8 w return ((distance * 6000000.0) / feedrate) / master_steps; . P! ]% F0 ^$ |0 f, K6 ` % w5 K6 Z! G: d1 l9 q} ~; b* _" w( }. {0 g3 \ : L* K2 \7 B6 W8 Xlong getMaxSpeed() + @8 M' Q3 X' G. ]{5 j5 D& I0 H& Q, M& P, C9 d$ K
if (delta_steps.z > 0 || delta_steps.u > 0 ) , }4 J0 l" h1 i1 p1 |$ g return calculate_feedrate_delay(FAST_Z_FEEDRATE); 2 H# c) j: k% p else7 \. l5 \# B9 f( @5 o' N+ `
return calculate_feedrate_delay(FAST_XY_FEEDRATE);3 p/ w: r$ R6 x; I3 C7 e+ J& F
} 2 j/ u) K! g2 P5 `- g9 q( |& D! _ W& O3 f& W i
void disable_steppers() ' _/ p3 A9 g/ e0 L- g+ D{ 7 u8 V4 n" [" ^! J2 G w //enable our steppers ; a; c9 B6 [* R+ e digitalWrite(X_ENABLE_PIN, LOW); ! T% D# T* W; J, N) @0 I8 s digitalWrite(Y_ENABLE_PIN, LOW);5 V; G/ s% @( L" T+ s
digitalWrite(Z_ENABLE_PIN, LOW); 8 V4 y6 {" X* F; a( G digitalWrite(U_ENABLE_PIN, LOW);1 o; \ |5 u+ \/ c
} & p$ S& v7 W- u2 a$ g+ w" Z& A4 x & ~3 K8 Y% o9 \$ D8 n! s/ {2 v, S" [0 u* B8 ^
//繪制LCD1 h* _( k, H: e& G: V3 M
//unsigned int DRAWCount=0; . m: M. M4 S+ h0 d1 d9 H' Q" E+ |void LCD_DRAW() 7 t, {$ M) Z) v6 _/ o3 a{% u* P4 u, U/ I
lcd.clear(); & ~! h% @9 l2 ~$ v
lcd.setCursor(0, 0);+ a* z" D; H5 o
lcd.print(" X="); % o2 M2 k5 }7 U8 \3 l6 V lcd.print(current_units.x);& y- l! ]/ ` A
lcd.setCursor(0, 1); 4 C' p) m4 K7 m9 E N2 k: O lcd.print(" Y="); 1 _1 {) W. v4 t& \+ G lcd.print(current_units.y);0 M* g- I) _4 D4 `4 |# C4 I7 e9 F6 E
lcd.setCursor(0, 2); : Q* G) g0 P! t; t: L lcd.print("Z=");7 ?- e4 m$ x' v" T! y
lcd.print(current_units.z);- z* ?. R7 ]* H! F. P
lcd.setCursor(0, 3);2 }& n' G$ b3 ~* [: V2 F, L% l
lcd.print("U="); 5 `3 A8 j+ i/ m3 [: p$ V lcd.print(current_units.u);. k( t/ {: A6 m/ _* i- o7 x5 K+ x
} - p; ]5 Y0 B- u% m9 S0 { // else if (DRAWCount>=30)DRAWCount=0; 9 t9 J- e$ y7 ?- Y+ r// else DRAWCount++; ! ^0 U. z$ m: R0 c3 H: Z" f void SPEEN()" l1 I2 z! X6 T) v- D
{% i$ {2 p/ A" Q) O W+ P3 X, ~' ~
delayMicroseconds(analogRead(SPEEN_CTL)+1);( G8 G8 T9 g W
}) B" q: D9 [2 b& w$ i: k8 F ^
( i% Y. G# E* |- l* B3 u' \6 }1 i2 H3 o, R3 e* P' C
//delayMicroseconds(analogRead(SPEEN_CTL)+1); ( W; n( s. I. X! e3 Z% I //if (digitalRead(BUTTON_SP_EN)) SPEEN();& O6 o# z6 b! v