$ N8 `$ Q# p# d. U% h //go home./ z8 c3 [2 o, P9 Q
set_target(0.0, 0.0, 0.0, 0.0 ); . m6 u }: P' W5 | ( ? V4 m H) f# k) W# E6 I dda_move(getMaxSpeed());) R/ b Z) N" B. @1 `" O. q+ i" \7 b
if(stop_flag) return; 1 ?2 V0 D$ p' T T- h1 U break;$ Y5 S$ w. X5 z8 Z8 o% a! q
' g2 V& n. z$ ]/ d# P. k' u //Absolute Positioning2 O* q# S: D/ K) y3 M
case 90: # l' I4 A" V8 W8 S! _5 x* ~2 \ abs_mode = true; ?: m" J% |; K4 k, X
break; - ^& y& I5 _0 Q" V : Q+ b$ i$ V% [$ C# q //Incremental Positioning, a7 }1 H) M% d
case 91: $ j& o+ d( I2 w2 n5 Y9 o5 g abs_mode = false;7 B- I) B9 l" L4 N7 H; e# y0 R, T
break; 7 v0 E/ c+ Z! s8 k2 h) o. o) C + V% k4 F2 b$ x- t% h+ `6 t! D( A //Set as home n. S4 X6 i' s5 q5 W: _6 ~) {
case 92:& A* m, B4 R! Y6 x) o' Q
$ l( S3 E9 J; `8 t1 n* z2 {
set_position(0.0, 0.0, 0.0, 0.0 ); . @$ l1 V; }2 q- y6 b % B+ b: N! l7 i. B4 P, ]9 z
break; , f3 d3 p' V4 S* H: B " [3 W, g. J. Y x; K4 j2 X /* " `# R) Y6 G i; f. }1 H: J //Inverse Time Feed Mode 0 I7 @. |/ Y2 a, l0 s' ]0 q% E$ g case 93: : @3 c' l/ F* A 5 d0 w6 [3 T; L5 g! N+ ?: { break; //TODO: add this " g4 M! P' w- b4 I + U( N4 t# h$ p5 }. U' s; b1 B* I
//Feed per Minute Mode/ s$ i9 K. g0 k) U
case 94:; u* m, L; o: D% t7 @) X
6 d) j9 Z/ f& S$ G1 Z
break; //TODO: add this ! J. Q, u+ Q0 ?7 K- L */* M* t( v9 F' K8 f
5 H x+ n8 I6 |0 n6 O5 Y default: ! P6 E9 m( O- a5 |; i; G) }# u SerialUSB.print("huh? G");. t$ j& n7 X Y6 M
SerialUSB.println(code,DEC);' b8 L* h, {$ O
}' _) Z. A/ ^; X' [3 o3 u) G
} ; \: s" B4 q, b7 T6 h) W6 g: V& W2 p/ g2 m# Q
//find us an m code. $ a0 M4 c* d! U: P/ Y if (has_command('M', instruction, size)) : \3 N6 e- f) X+ U- v& F8 Z7 o { 2 `" ~4 h. r3 x7 M! g* t! k code = search_string('M', instruction, size); 6 W0 w7 ^7 O- X A* j switch (code) " v3 c5 W1 |8 x/ R3 l$ c; O& b' r& N { 8 T1 L% o" P4 C' b$ u! V //TODO: this is a bug because search_string returns 0. gotta fix that.; `4 X# X8 A* H: m
case 0: 9 Y- n# [0 r: q, ?' _# O true;0 t" w& a1 c, h* E, f* J6 f
break; ' L% ?* e( @" V % H& D& h+ A5 n case 100: $ Z% o$ h l1 i- e& }& r- V" f% ` break;! f" m4 p) @. a7 c
# P' X0 m$ N" T // fire camera relay ; X+ h0 {5 y( N& l case 101:* a( \. [. a6 p
camera_shutter1(); - w9 y2 G5 [( E break; & Z$ W Y, m1 ^3 p' M ' H; ~8 i, E5 X& U" J // fire camera relay2 B0 h+ z7 u; ], t5 k+ e- ~7 O; N case 102: ! y: ~ ]0 U3 C' s) U8 p camera_shutter2(); ; w! V/ f- [- A; Y! A break;% \0 D5 E# u4 k; \7 ]
/* # C) f6 O, ?) j- q( v // turn aux 1 relay on: E) k4 D+ ]; K: ^3 d b
case 103: , y" I8 t$ \9 {/ X. }6 i camera_aux1_on(); " s9 O) Z4 C" }4 B& V break;! h4 ^3 m" q* ]! K1 q) l
) Z- d& a+ k! Q // turn aux 1 relay off / Y/ K9 y$ ?: L$ o" i case 104:: ?) I, g. ~+ s) h
camera_aux1_off(); 9 d0 v" p0 h% R8 F; B$ I* x+ P+ G break;1 @/ t D$ p- _0 t
) P( Q7 w' D- o0 Q
// turn aux 2 relay on7 W* H9 Y, m0 t, }, T! n6 T8 k! f- i
case 105:9 m% I0 _; I% o
camera_aux2_on();* Z5 u) A8 T8 f6 M7 T
break;* f9 P h. \ f
2 G' k& u5 h V+ t/ m! l$ _. f( R
// turn aux 2 relay off - w- D9 M+ _0 g6 b' `( Z0 P+ G case 106:( P h5 a/ a- W( H5 ?
camera_aux2_off(); 6 X* b$ b8 `# A! A0 d+ Z; ~ break;: R# \; w; h- m' I% i1 a
7 F" V6 i( w( s( B7 a7 _
// turn aux 3 relay on m, h" G4 ?- s# A7 E. o
case 107: 1 Q( i8 f% |, z7 a camera_aux3_on(); 7 h6 M, h' ~3 Z* i& z6 t6 V& ` break; : x$ r x" I6 ?& ^ / d* i$ ?8 V" h$ O. h; _' J5 i& o // turn aux 3 relay off; S# ]0 s- H& d
case 108:9 M# ]' l6 q4 L3 F) z
camera_aux3_off();! ]; c: R: b0 N
break;3 l d3 R4 f) y$ S3 d/ L
& D! {& X6 w: y2 \" `- a
// turn aux 4 relay on $ N3 j& g9 ^. M* F( J' ^6 r case 109: ( Y' n% x5 G) g$ j- V; N' E1 ? camera_aux4_on(); 0 D2 l; L0 J# ?. r: W+ B break; ! e2 b! ?; I9 @4 e' v) v+ f8 l' m9 A3 _/ o4 _* @$ a
// turn aux 4 relay off ! }, \( a6 g% X6 s$ [4 U( w case 110: 3 w. D- ?( u y% C- P5 E4 A camera_aux4_off(); . k: g! D# g0 z break;2 h3 Y- [: u: G' L' H1 h B! E* m
*/0 H, O1 o0 ]/ M. X) t' l. z0 z
default:7 \$ U4 [1 x6 U
7 M' S& f7 g( C& [: F8 X8 V l8 ? J
SerialUSB.print("Huh? M"); ' s' @/ r7 l" p- k SerialUSB.println(code); * t6 G1 ]7 O% k3 B* x } ) s5 S) z5 ^8 j) [4 U. l- r. @, A) @ } 0 e% h1 X) U. r& u- x! o* |3 T9 u/ z ; m. c8 c3 h6 C! q: F1 p //tell our host we're done. ! I) G. ]+ l Q% f) u8 B; f SerialUSB.print(byte(78));4 t( ?; M# ]6 X' ~: G
' s0 Y4 O0 h4 P} ) u$ r/ _0 q! b- u- w7 m , @& h7 V5 E3 }" b z" g( H6 N- z//look for the number that appears after the char key and return it 3 u; o% L) a* h# \9 a8 m9 |$ ]; Bdouble search_string(char key, char instruction[], int string_size) 7 C9 Q/ Z( P1 H# z4 l& | c{ G/ E2 ]& p* k H' O# G char temp[10] = " ";. Y' D* Z" Y @7 e, v1 u6 z
for (byte i=0; i<string_size; i++) 4 \7 _8 Y$ a, D `2 P( c2 q6 {1 d {2 a% W9 \7 U8 N5 V5 A" f
if (instruction[i] == key)6 t4 S2 N5 c( G, j) l2 f
{% \1 d U/ P5 n% r1 ^
i++; # z' m) W) Q9 j; V ?
int k = 0; 3 X7 @' R+ P' f while (i < string_size && k < 10) 5 H: j/ B3 s( a9 R- g; i9 l { 3 r, L, R/ v& V if (instruction[i] == 0 || instruction[i] == ' ')$ W9 N" D9 I$ S! C: L
break;+ c" x: z. e+ H L2 J
1 v. ^* \, H$ i1 }: ?6 Q temp[k] = instruction[i];9 U6 L& j# W" Y7 ]+ D* Y; @
i++;. `) t. b' @- o5 c
k++;& ^, @/ v0 P7 E' n6 R% k5 y
} ! p" E9 [& Y( q0 w @7 B return strtod(temp, NULL);9 F9 K; M' Z H7 |
}6 D: U! F& |% r0 I* X4 Y- r
}2 A) l% W2 J0 z; {
* J$ r) G, H. V
return 0; 9 c5 O) k9 J3 R% `" [9 ], j* L}( f+ r$ \1 z. k! \* R
- \. ]- f1 O) C* q) C' A
//look for the command if it exists. , m/ f$ E8 {: T' G ~bool has_command(char key, char instruction[], int string_size)4 b; P9 ?7 f' G8 U6 s q
{ & G' Z" H; E2 ]+ m for (byte i=0; i<string_size; i++)6 M$ W7 m4 r& I( M( Z
{+ Z% w. E0 s$ p: H: c, |* E1 K
if (instruction[i] == key){ # d9 k0 R- `1 c: I% [3 l- E7 P 8 p+ e/ ]& i8 F" @; V1 h, j return true;7 e- l/ d9 S+ r% o- y) K( I
} 1 E, E W5 g0 O1 ?% s) u } ; T: }4 l% I/ Z. z8 E2 u - N: f) ]6 n& w return false;; x" \/ ]5 [! P) q
}8 _' P' W6 J4 m8 i% C9 F5 b) q
. ]; |3 @# r5 ~: R: J4 b5 g% L5 p5 F% w, s* W3 d! K3 I
2 s, E \: A/ P+ T; n, J. n2 R 作者: xmdesign 時(shí)間: 2014-5-10 09:55
- r& D5 Z' O. B8 X
//init our variables p# I% C" S# c4 Z+ S& D4 V: K& }6 n
long max_delta; 2 F, \7 n4 e# e' U5 C$ r, glong x_counter; 6 Y/ P3 a' p( h5 F% C$ d$ Slong y_counter;- i# x$ I# g/ U8 `" t2 x! g& O, @6 y
long z_counter;1 n* a/ s, J, N5 X3 p
long u_counter;8 h* }" L# C7 e% @
long x_pos = 0; // x position in terms of absoloute motor stepps2 }" U8 n7 |+ j( ?
long y_pos = 0; // y position in terms of absoloute motor stepps ; |# [0 e. q5 H c# clong z_pos = 0; // z position in terms of absoloute motor stepps1 B. M$ V6 o: s; f( \5 F
long u_pos = 0; // U position in terms of absoloute motor stepps ' @4 ?; }8 e V ( _, X$ K2 b3 P# S+ |% C2 H, y' Gbool x_can_step;( w0 Y6 J/ s! p$ n
bool y_can_step;* I; `4 r: S p- t" C0 t( |' z
bool z_can_step;3 \1 |3 w2 I3 S/ i% d5 e8 }* `2 m& k/ m
bool u_can_step; 6 t2 D+ Q$ u) f3 `( y/ Kint milli_delay;2 P! ^/ \# l1 X7 x. x/ H
7 d2 _* T; e0 Z( e; {
void init_steppers() u6 d( [- ^9 N+ }3 |{+ _9 E4 d: P$ \2 s+ M
//turn them off to start. " `8 D- n' E0 x7 O disable_steppers();. e! t* d( \3 l" c- }$ f8 c% e4 b5 O
/ @: N6 \% {9 S2 V //init our points. * R, b4 ?7 q$ F b current_units.x = 0.0; * t, O6 `4 U4 \/ O( w current_units.y = 0.0;( u |# ]6 c. D: W( T6 x# V
current_units.z = 0.0;! P% i0 N/ u* S1 C% W
current_units.u = 0.0;8 |9 R, |# e9 V. U* e d2 y! D
target_units.x = 0.0;1 d4 j" {1 N! q
target_units.y = 0.0; : }1 f6 \+ u$ g8 q( f( y3 J target_units.z = 0.0;* N i# ]. h, T1 n; q* q
target_units.u = 0.0; $ L0 O. j) J0 g ' ~( Q/ N: ^- G/ B* u& M
- q9 m2 q9 `' o1 N pinMode(X_STEP_PIN, OUTPUT); + z' |: F# T& q- ? pinMode(X_DIR_PIN, OUTPUT);5 w. x& r: U; u, |7 y0 a& u
pinMode(X_ENABLE_PIN, OUTPUT);$ Y. K2 Z% N6 E1 @9 a" h
pinMode(X_MIN_PIN, INPUT);8 J- c2 O1 ?! f D& Z
pinMode(X_MAX_PIN, INPUT); ( W/ e2 \5 L0 ~& l1 @ y7 @& E8 W! ]5 t- N2 E0 v. J5 [
pinMode(Y_STEP_PIN, OUTPUT);6 b; |# O' X; E* m9 n* X
pinMode(Y_DIR_PIN, OUTPUT); A. e# J& _9 a& J/ X3 ~+ i pinMode(Y_ENABLE_PIN, OUTPUT);1 C% ]' x) B. l1 ] r5 C/ e8 B
pinMode(Y_MIN_PIN, INPUT);$ R- {7 \* o7 U6 K- ~2 B) N
pinMode(Y_MAX_PIN, INPUT);. _' \! g$ X$ C. `1 @9 q6 w! t
( h0 N, S3 j1 B- `$ V
pinMode(Z_STEP_PIN, OUTPUT);5 g. G; G- w# Q2 G8 d
pinMode(Z_DIR_PIN, OUTPUT); 8 |+ J; T3 ]. v, j pinMode(Z_ENABLE_PIN, OUTPUT);2 F) o8 t" u& M; y8 U! T
pinMode(Z_MIN_PIN, INPUT);& ?/ c* W* y& p$ ]5 b- Z; B
pinMode(Z_MAX_PIN, INPUT);+ V8 L* ~3 _$ e7 j- ~
9 |& F! f$ a: ~
pinMode(U_STEP_PIN, OUTPUT); 6 w7 P8 ?: m" p6 k" ^$ Z pinMode(U_DIR_PIN, OUTPUT);' t; o8 A4 C' Y5 |! \
pinMode(U_ENABLE_PIN, OUTPUT); ! V* g6 \( X. f1 w pinMode(U_MIN_PIN, INPUT);9 d+ x* a+ P& Q+ Q/ W9 G
pinMode(U_MAX_PIN, INPUT);$ X% L t. i6 w/ b, o
//figure our stuff. : Z% P$ `- U5 A& P6 g- L7 { calculate_deltas();9 g" H! F3 d" _ R# _" F
}- H! `7 {5 ^6 n. @
) k7 L/ M& Y7 {0 w4 A
void dda_move(long micro_delay) ' M: T4 F/ L/ h9 b{ / b6 m6 y2 d$ Y& S! a //enable our steppers : H5 L& X6 w( w2 t& b! U$ z7 E digitalWrite(X_ENABLE_PIN, HIGH); / P z0 Q \1 k8 ?( x6 ~% S digitalWrite(Y_ENABLE_PIN, HIGH); : Y$ O$ i% d; B8 H digitalWrite(Z_ENABLE_PIN, HIGH); : l+ v6 Z. L+ z4 D' d# C digitalWrite(U_ENABLE_PIN, HIGH); V% i2 _8 M, B" O, g0 M- S //figure out our deltas; {4 Q" r! f1 V; k( t& M3 T
max_delta = max(delta_steps.x, delta_steps.y); : l7 F3 ?- ^1 f1 Q M* ] max_delta = max(delta_steps.z, max_delta); ) @! {0 R( l# n6 |% Y max_delta = max(delta_steps.u, max_delta); ! A: Y) k, k( D& T( }7 R( }6 E //init stuff.$ k" j6 S6 P" u) T6 F2 [
long x_counter = -max_delta/2; 3 V; q( v' q/ K8 i v1 g long y_counter = -max_delta/2;& Z" h3 D1 M& ]
long z_counter = -max_delta/2;7 ]' {6 z. Q% X$ O7 V0 x5 {
long u_counter = -max_delta/2;/ r4 f% ~2 G9 ?$ _3 O, z* n( T
% J9 @9 A$ O6 Y3 y: n, I- S //our step flags 2 l8 K- N7 W9 W* @4 b. ^( m bool x_can_step = 0;% W6 } [+ Q, O6 D: y' E
bool y_can_step = 0;# j( s: ?' k, Q
bool z_can_step = 0; - c1 U/ ~/ W' \6 B- b% I | bool u_can_step = 0; 1 C$ _$ q6 w; @+ _" O+ z/ D# F m3 I4 T t" G
if (micro_delay >= 16383) * A! ~) O+ b5 v7 M milli_delay = micro_delay / 1000;4 T) s) ?2 a3 [
else3 S6 o8 D. ~1 M
milli_delay = 0; " s) p5 V# \4 \: [8 K; [ 7 Q/ |0 J7 o8 \6 W/ @8 H) H% Q( ^% s6 Y1 n# X
//do our DDA line! % F$ ^3 D3 L6 Q Y, H/ w* |, Z/ m0 v; K! ?* |- u$ E. M! m3 R
do % b3 [* a9 O& l0 r. K/ Q7 @ { * P- \1 c. T0 v* e* f if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break; 4 P3 E: { ~6 D8 K3 z9 ] U x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction); & q% f1 Z9 @7 J+ r0 G y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction); ! \' p$ _+ L/ ?7 ^/ h h; R z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);+ b! A& P) E f2 y
u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);4 K! r1 L3 `1 u4 D) ?4 A
if (x_can_step) G' _8 H" Y( N, a8 V" c1 U { / p" b4 |% U0 ]0 g x_counter += delta_steps.x; $ l- L. S3 e5 O$ b$ j" T; X' D2 Z3 ]* @9 U1 T' R; m7 E
if (x_counter > 0)6 @, H1 f" n: m- M+ r c+ a
{ 8 o: n4 e4 M. B. z! H5 u0 N) w do_step(X_STEP_PIN); ( Y, C# ^: u: x x_counter -= max_delta;5 A3 B8 T* ^" M; y* a5 D( o
if (x_direction)) D. V t* A! o0 s9 N/ B; }( j
{ current_steps.x++; x_pos++; }' A9 e9 D+ U& P1 A* g
- T6 ^/ T0 D: j$ | } 6 E; G* x8 ?8 ~8 n6 y }7 U, P" B/ M7 o/ A! x
if (y_can_step) 1 a! a* L% @3 \" |: g. m {' D; |: x$ [- a. y
y_counter += delta_steps.y; 2 |; b1 b4 k! [' E M6 H4 A ?3 D9 L * p+ ]& U7 T5 l: c& e3 |/ U; x$ K5 y if (y_counter > 0) + P2 M7 R2 g9 R! d7 r {. d. a- W0 [4 P, u
do_step(Y_STEP_PIN); : o4 Y& q- p$ Z* c* r; g y_counter -= max_delta; - ^+ ` E* s7 p" ~5 S) I% A+ ^- k: T, a3 o5 o8 A4 D
if (y_direction)+ B4 w" E* ]9 i* S, t- J8 {
{ current_steps.y++; y_pos++; }. N" ?, W5 q( v! u, w
! C* v: t& L3 l& Q' t* c else . |( [( F) e4 S! B- p) D5 | { current_steps.y--; y_pos--; } 1 l# `% U7 J N/ }% Y ( q! O" [0 }0 ~ } 3 m# p$ ^* v9 m } $ V) L! R5 t9 ~. P5 W+ S8 d# U3 v/ V h% E2 h5 W) D- D. ?6 o- `* I) M
if (z_can_step) , x9 ^ T: l/ r { - M+ T9 ~1 s# s z_counter += delta_steps.z;6 g p( m$ c# f) ]
- z6 H v" m) V8 `5 z" @ if (z_counter > 0) $ Z' R; Z, q5 q- \; p { 6 W% P0 e( V4 k1 ^ do_step(Z_STEP_PIN); * U/ K$ r" U& \$ `3 b! Z z_counter -= max_delta; \* ^* z% o! o- I
' x+ w& v, R( Y/ M
if (z_direction)7 V, ?/ b1 l$ l8 t h; n" y
{ current_steps.z++; z_pos++; }9 {0 s/ @# n1 I* T" C
] m0 |: B' ?& F4 i$ m5 o else 2 G3 n& U6 L9 g$ D0 G { current_steps.z--; z_pos--; } , n& a: N; @2 l0 I- ` P & U( H% C% L h% N
}0 P. m1 w+ F: y6 K/ @
} B4 q; z: C6 [7 A7 X
- J* f6 J( S# g8 b& w8 B( B if (u_can_step)/ k- l( C& I" ?1 c
{# l( c# V2 _& i9 A5 B4 k
u_counter += delta_steps.u;1 {* E0 d& q3 t' c
; _, f0 H3 \. L. `4 c3 r" ^! V- {7 T if (u_counter > 0)3 s! Z" Q1 \0 U$ @/ H8 w4 a# b
{ : N0 Y* z7 [' x2 q6 v8 y- o$ }3 q do_step(U_STEP_PIN);6 k- X7 k! s6 L) [8 Y
u_counter -= max_delta; ( x% R) A: W) @6 @. f$ J $ }4 ~. v x+ \# w; V% q: b4 B9 X3 I if (u_direction)6 c2 J9 W0 J( J1 X8 g: l" [. E* V
{ current_steps.u++; u_pos++; }' w% L9 t0 _/ b
8 q) i! r7 e3 ?4 t# q% s, u6 l. J
else. Q8 O2 [. Q! e# F) }
{ current_steps.u--; u_pos--; } ) }, K m4 p+ X" S" f3 ^ ; D& l* n8 }! e0 s( t9 ~1 Q) U5 C: e
}/ @4 p* m/ M7 h! D
}( {/ }4 o0 }2 G% L: B
//wait for next step. , v# g* i" R6 p6 n! \4 U" S# P3 Q if (milli_delay > 0){3 M% G; q1 g3 `" i: U
//if (digitalRead(BUTTON_SP_EN)) SPEEN(); + t6 Z- J! p1 ~2 _3 q$ k. y delay(milli_delay); $ K: l+ `" o: H( u5 _' h, t } $ d) s: s2 S( M6 z& g6 F else{! i$ N2 `3 p1 |& U) [ a
//if (digitalRead(BUTTON_SP_EN)) SPEEN();4 \7 t8 E$ }* y' v1 ~: Y
if(micro_delay>0)delayMicroseconds(micro_delay);: X- N0 Y* }+ Z t @6 c
}6 t/ J- y2 \( R3 m' |) C0 g4 {
//if(x_can_step%40 || y_can_step%40 || z_can_step%40);+ |* e5 s- [ z. R# ]; Q: e
} + ?1 n) I# _+ k& u! T: | while (x_can_step || y_can_step || z_can_step || u_can_step); l3 d9 i- O1 ?, H3 u A
6 `9 K3 S# d, h! G# ?4 N
# d2 m0 v0 B% \& I; Z8 I
//set our points to be the same' N: U" O% c9 S# N
current_units.x = (float) x_pos / X_STEPS_PER_INCH;! |0 @' T) c4 [" `; V
current_units.y = (float) y_pos / Y_STEPS_PER_INCH; 2 R- e- R3 e, k j- l; `" K- P" q current_units.z = (float) z_pos / Z_STEPS_PER_INCH; + J- V3 L( L/ O8 K current_units.u = (float) u_pos / U_STEPS_PER_INCH;! \) p' B2 h# M! E" l4 |" n6 d; w" W) k
. P* v! ~. n% H, ~' { set_position(current_units.x, current_units.y, current_units.z, current_units.u ); Y# B- `2 }+ S) L# M 4 N. G" c8 ~1 U3 F' N& T
long x_pos = 0; // x position in terms of absoloute motor stepps/ _8 x& M6 V! ?! t. j/ c
long y_pos = 0; // y position in terms of absoloute motor stepps! W/ \5 H- z9 S9 Z
long z_pos = 0; // z position in terms of absoloute motor stepps 1 x9 P. ]/ J; ?7 u long u_pos = 0; // u position in terms of absoloute motor stepps+ ?- f- V1 A( b
calculate_deltas();. G" ~; I/ _1 r& K8 o- S+ E$ |
( b# r' p0 K, j) m) @}# }$ s. f6 `" b. B' H! t
+ o& c8 `, W$ C7 c9 N" J! `3 kbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)& T. A6 [% v/ X' M# i: ?
{( l& I1 i( l1 P' l9 P
//stop us if we're on target: _" b$ g+ T) i' K' `
if (target == current)2 G+ J% ^8 I9 L* R2 A( B% m) u
return false;8 T% |9 P$ {( E W8 J" J8 |: c
//stop us if we're at home and still going x: H q5 g: `0 j
else if (read_switch(min_pin) && !direction) ' W$ @4 O8 _: h7 p$ T return false;* `& z: K* w* h1 ], c
//stop us if we're at max and still going3 w- G. @ f. i1 X: e+ y
else if (read_switch(max_pin) && direction)& Q- U0 ~' J" W2 s0 e0 H: c, q
return false; ; T3 f5 S$ z* g) D% d9 q$ N+ {* c9 M1 L, L! ~: R5 l
//default to being able to step 7 R* h: C: a1 Q* L* e return true;) d, J4 V3 o( c: c0 M; ?/ Z/ M4 V4 N. z
}" D3 @! f- e7 ]8 c/ e" Q& X
2 R. m+ t" j2 w" L5 O- K o
void do_step(byte step_pin)! j9 J+ S* i: S ~8 B
{ / C2 Y1 g+ a6 M J$ X digitalWrite(step_pin, HIGH); * e( A/ Y4 u" R/ D+ \ //delayMicroseconds(1);8 r5 B h6 r, ]$ L0 l/ C
digitalWrite(step_pin, LOW); 4 b# j4 z" J0 }9 D} & `1 }4 c. @- v8 Q' k$ _' I0 C" h* N& n8 C! T2 _7 i
bool read_switch(byte pin) T* I8 o9 Z! R* z% E{ 8 \- E. h! c3 v' F. R$ F0 {7 n //dual read as crude debounce% ]' M2 U, e; O g+ L- Z) b+ [* a
6 V- @) E+ w4 _- _
if ( SENSORS_INVERTING )* S0 Q, f$ t) X" j
return !digitalRead(pin) && !digitalRead(pin); W* n& `/ H U& V0 L2 o8 B
else2 J6 y! s$ ?% o. v, M! o z
return digitalRead(pin) && digitalRead(pin); . O! r, n3 Q" K, @. g+ e} 4 g. z6 k; c6 @- B' R$ L$ }0 S3 I. r- Y9 ~; i, Q
long to_steps(float steps_per_unit, float units)- J6 U% c9 R: [/ F [
{ ) I! k. g) L0 l: l. ?& L return steps_per_unit * units; ' t& Q7 e; j. o5 @, h7 _}8 j! n; ]* Y/ {- f Q5 s
6 p$ O6 u2 z' g8 ] ! s, ^$ Y+ X* [* S* ` + b: ^9 }7 q" w/ e- o3 y //calculate delay between steps in microseconds. this is sort of tricky, but not too bad. 5 q# J" `1 m7 B" _ //the formula has been condensed to save space. here it is in english: 3 U6 X6 j) N6 Z% F7 y1 ] // distance / feedrate * 60000000.0 = move duration in microseconds / _4 \- p3 u+ q w" I6 m R+ {/ U // move duration / master_steps = time between steps for master axis. r0 J4 V8 d0 l9 ?
return ((distance * 6000000.0) / feedrate) / master_steps; 9 k% V- J6 [, }* E) ^+ L" p1 ~4 v. @% ~ j. K5 }+ Y
}7 [2 K% |# Z" m
^9 Q5 H9 O8 a" Z' ]9 P' y' hlong getMaxSpeed() 4 }8 u7 o: `. X S{7 x* |. w, G7 z' R
if (delta_steps.z > 0 || delta_steps.u > 0 )) {, @2 b$ B: {
return calculate_feedrate_delay(FAST_Z_FEEDRATE);- R) b5 D' H8 ^! w7 R( M
else: x" ]6 \0 v- ~6 c; C5 o. T
return calculate_feedrate_delay(FAST_XY_FEEDRATE);4 y/ g* K8 q% T+ Y
}: i; X4 c y: ?+ f! q0 \' f
]' }5 G' g5 C# r" c* G" r. s
void disable_steppers()% a( t/ o; F! U7 J7 h+ Q4 U
{, ]) ]0 t+ X2 Z! {& ]9 q, o( y
//enable our steppers 9 V: L9 a; m; P$ e: s digitalWrite(X_ENABLE_PIN, LOW); 4 s Y" G4 t3 z; J/ } digitalWrite(Y_ENABLE_PIN, LOW);# x( V& ~6 n+ d) l
digitalWrite(Z_ENABLE_PIN, LOW);( _3 J( {) A' _/ C1 Y, e, L( \
digitalWrite(U_ENABLE_PIN, LOW);) o& d' P& l5 p
}, a/ ?3 h, g( A7 z
# X# x/ t: e% p, u# P : r4 a+ ]5 ~0 g% u+ S//繪制LCD & F+ l: r3 l% g( Z! ?! `+ u//unsigned int DRAWCount=0; ) ~. e7 n& e) w, Z5 X) B7 |# w' Evoid LCD_DRAW() ! a% t1 k2 x0 c7 F{ 7 c0 S* D: k, y" P lcd.clear(); $ D3 R. m" ]" G% L2 B
lcd.setCursor(0, 0); " N, \) b6 [" H1 H2 z3 i9 J lcd.print(" X="); & q+ N0 Q6 c' N: n lcd.print(current_units.x);1 T9 x4 b3 ` o
lcd.setCursor(0, 1);/ [: P2 X( k5 c6 M0 O0 m% V r
lcd.print(" Y=");: f. x0 U- f$ \3 Q; `
lcd.print(current_units.y);* y0 i- W/ I/ \: k) Y0 c8 [
lcd.setCursor(0, 2); $ L; j! Y* v& _1 x; N2 x lcd.print("Z=");, o, O& \) t' T8 q
lcd.print(current_units.z);5 o3 H* v9 o+ Y7 @
lcd.setCursor(0, 3); 3 [/ W4 \7 Z; B lcd.print("U=");, T4 ^9 m3 |5 L ~+ ]! `* t
lcd.print(current_units.u);: b: Q% n# }# R
} 8 i7 A2 _) N5 y5 R% A. c4 F // else if (DRAWCount>=30)DRAWCount=0; t) V; e6 c8 Z# s( J: N0 t
// else DRAWCount++;: M. i% ?8 u) v4 O
void SPEEN() 9 w9 z7 \" o5 @4 ]! ?# ]5 z { & [5 P/ s5 [: z2 \. ?( ~ delayMicroseconds(analogRead(SPEEN_CTL)+1);" B& C2 N0 Q( H4 [9 @) T
}6 x( `8 T, M& F R) r3 n
, q7 q! L; n/ c& K& c. Q; D