|
7#
樓主 |
發(fā)表于 2014-5-10 09:55:02
|
只看該作者
// our point structure to make things nice.
" C6 `6 h+ p, ^' d: s3 ?struct LongPoint {
3 _6 y0 Y# l" X0 Q long x;* p' u2 h& n2 T3 _; s8 ~- `. U
long y;
5 W$ w/ K9 H( v+ U long z;
; M4 L4 ?3 O' x6 o/ W+ y long u;
* r0 l' q% ^. Y4 @9 Y2 b6 d/ t, E9 Y};& ]7 ^0 x2 t, i8 f! Q; n
; W' _2 x3 _. j+ k- H; J# u2 `
struct FloatPoint {
0 U7 t" q6 ^2 m: ]$ T& z float x; E/ ?; f1 Y! l! H
float y;) a4 `5 [5 x9 s% Q, F p
float z;
3 X4 d9 E1 C' x7 e; L float u;
: D6 S! f: {0 q- w& {$ P8 y6 e) j- b. k3 \};
% j( T; N F D6 N6 G) \) m) z, i! o# k- x7 ~
FloatPoint current_units;
d( e( x/ ]: y6 x7 M9 a" a# lFloatPoint target_units;
9 A! J f( ?5 W/ y GFloatPoint delta_units;! }6 y% @/ X. I
( T) F# A* t L, LFloatPoint current_steps;
) ^. l7 M* y9 H$ p6 E% I, ^FloatPoint target_steps;! p7 I1 a! _: @* a/ U5 W
FloatPoint delta_steps;& j# P, j& x* \1 m a- Z6 }
5 o0 g& S* \ Wboolean abs_mode = false; //0 = 增量位置模式; 1 = 絕對位置模式
) r" q4 ?* D& n( h2 o0 h' X( w, k0 h/ M8 }
//default to inches for units8 o9 H% U8 k+ k, a
float x_units = X_STEPS_PER_INCH;$ _5 A' O4 f1 x/ U
float y_units = Y_STEPS_PER_INCH;- s) d- j# M0 ^9 X
float z_units = Z_STEPS_PER_INCH;
f: I! Q4 l+ G, Jfloat u_units = U_STEPS_PER_INCH;
: [. F4 y4 j7 Q i# X) ofloat curve_section = CURVE_SECTION_INCHES;
0 Y: h9 F" L; s8 s* J, f. l; G9 O# y2 p# {7 b c3 F& X
//our direction vars
# b) Y5 M. S r6 Y8 x- cbyte x_direction = 1;
# S' Y, H! z+ _0 U, e/ j& d9 ]; Hbyte y_direction = 1;
& ?) Y6 [% u" a7 ~* \" wbyte z_direction = 1;; }3 m# e: j6 p. p1 |
byte u_direction = 1;
1 u4 ?( K, u+ q
7 k# A# }# T0 g//初始化字符串處理
. e) N" K4 o$ _$ Lvoid init_process_string()
3 J& H7 d8 o2 i/ t{8 Q% H$ m% `7 A" L0 C3 t: I' b
//init our command
8 E" B& U, h+ I& D for (byte i=0; i<COMMAND_SIZE; i++)
y8 ~: Z' k# X/ f3 M& N word_old[i] = 0;; _8 U9 l9 q9 m2 Z
serial_count = 0;5 ? v2 X: p7 p# a2 J( ~8 r
}
5 V7 Q- u3 u. g. K/ X# d$ q* i; e) j. `2 q9 U) {2 k
//our feedrate variables.
5 u% E7 p7 |' K) g* Sfloat feedrate = 0.0;
6 C& K2 O$ c. z j3 @( zlong feedrate_micros = 0;# A3 ~% C$ p4 ^
& V: W7 X' y5 G `$ N3 m! p//讀取并執(zhí)行命令
9 D L4 F; D% E3 E( O: t+ zvoid process_string(char instruction[], int size)5 \2 Q7 H' X2 u: P0 j3 |$ n% d3 i
{1 d6 s# _# s# V) R
//the character / means delete block... used for comments and stuff.
" w, U7 b- P; [0 P' [: b if (instruction[0] == '/')
: Y3 Y. X* p& t2 T* T' F {
: @2 P( s, L( K- M6 [ // SerialUSB.print("ok");0 j" l# S) y9 ]' N. M( u* U
// SerialUSB.print(byte(78));
6 o) P( [0 j! X, G return;
% m* b+ G7 b) \" \# N' ` B }. W$ {; O2 t( A4 Q1 |6 g' v
//init baby!
8 F4 h4 a# W- q# S5 b4 J4 v& k FloatPoint fp;
( s- J0 z# P+ ` fp.x = 0.0;
5 \) y& n$ m" z$ J fp.y = 0.0;
8 N( q4 Q/ [5 H* a; @ fp.z = 0.0;" k! L/ d- W. m
fp.u = 0.0;
3 a- X+ e' x! x8 I6 f0 A) [* A2 k! R! O7 L8 F& F! \
byte code = 0;
" s% T! G9 J% g/ I
) N( O4 ?! x- S4 y# D //顯示在處理的命令
& r7 G. W3 W6 p+ T#if (LCD_TYPE == 1604 )
8 u5 k$ i0 ]- V& ?8 {% k; _2 X8 h, {/ f // lcd.setCursor(0, 4);
1 j: ?4 q% g# X- {2 p: a5 h2 J. o% n // lcd.print(word_old);7 g! |/ M/ f- w2 \+ _7 l: T
#endif; O- G& d2 H$ O3 k! x& H
SerialUSB.println();6 Y/ ]3 @7 a- K- Z- g2 E- W$ T- r
SerialUSB.print(instruction);7 a: i% \# M( L# o, \" U0 d' g
SerialUSB.print("\t");- d( n N- f# u/ u$ H
# I; K/ n- o/ Y. @# p8 ~ //what line are we at?1 l1 U5 S; K7 P$ B" w
// long line = -1;
( e8 _; r; q: B- s3 y // if (has_command('N', instruction, size)). V9 i" P: ?: M- ?* C+ b
// line = (long)search_string('N', instruction, size);. g; J/ ?9 s, {2 }
: G7 k# ]' G- P. G9 L ~- p /*1 L2 J6 h; o) v( B2 q7 Y
Serial.print("line: ");
" U( R( C3 f; F# E Serial.println(line);9 K# E5 V) ]& T
Serial.println(instruction);- y# I% G% q! O5 }4 g+ o0 C# ]
*/
& B c5 Z7 ?' `9 w3 D9 l //判斷是否讀取了個 G代碼?- p# @% H/ T' b1 L
if (
6 u l9 }8 A1 y# o( h has_command('G', instruction, size) ||
! c/ A# L, L6 q: | has_command('X', instruction, size) ||
$ R/ T! C& ~- n, Y3 e6 } has_command('Y', instruction, size) ||
. p. O. A6 j7 R; E' y has_command('Z', instruction, size) ||7 {& I& t# s. r( Y: }3 f
has_command('U', instruction, size)0 P$ u$ T( q' N+ y* i1 M2 R
)! ]6 c0 Y* f' g; @, ~9 V3 L
{
) D `" _5 T& G //which one?
_9 g0 T* w; p" I4 x t z0 S8 G9 V1 p code = (int)search_string('G', instruction, size);% t& X5 q, i; ]0 p$ g
// Get co-ordinates if required by the code type given
2 S/ R6 E" _8 @& x4 J6 \0 v5 r switch (code)2 z9 C+ H4 p# d8 w
{& m1 W4 f9 ~$ w8 ?9 a
case 0:
4 l6 h; K6 N/ s9 |7 i8 R6 ] case 1:
4 U) j* ^( k( p H3 `. ~ case 2:9 C+ p$ Z2 _ p
case 3:8 m! v$ p7 P0 j# H3 {8 ]: ~
if(abs_mode)
( U$ a4 M- c) N! K1 c) W1 Y {; h; L, ^% P$ s5 V) L
//we do it like this to save time. makes curves better., _2 A% j' V9 V8 G7 p" b
//eg. if only x and y are specified, we dont have to waste time looking up z.' r& f7 d% \/ W
if (has_command('X', instruction, size))
( T M" V/ H/ Z3 V" k0 o* T fp.x = search_string('X', instruction, size);3 `+ q: F2 N0 T% U4 p- ^9 y. K, ^
else5 ]/ B& s+ z. T6 q" z* Y
fp.x = current_units.x;
1 E" p1 }0 I& R. ?/ K
8 I8 a1 C5 n) m3 } b if (has_command('Y', instruction, size))
6 q$ ]5 N' {7 f& p- i' v fp.y = search_string('Y', instruction, size);
6 @5 K' T* a5 Z* D, ` else/ R4 y# y2 _+ D
fp.y = current_units.y;( F; g4 }" F% i6 i( x
0 r% k. K& X* X7 f
if (has_command('Z', instruction, size))( _/ G2 m6 b3 H# `" _! }& x
fp.z = search_string('Z', instruction, size);& k& U( n% ~3 N4 b5 f
else* T9 o& N2 ?2 R7 A+ Y; y" j
fp.z = current_units.z;
5 U* s% S1 l3 O% j( \ * c' m) D/ a8 K! i
if (has_command('U', instruction, size))% J" i0 k7 |- T2 x* a3 M1 R
fp.u = search_string('U', instruction, size);
/ [ U6 R# E1 M9 A% a, U' w else
6 q9 D: }& p2 M2 w, D fp.u = current_units.u;$ Y0 z( F/ n4 P
}2 N9 i2 W4 o. T7 q7 i
else
6 a- l# a" r U9 m+ l! l {
" i; O8 e; i# B# ^' H K" _ fp.x = search_string('X', instruction, size) + current_units.x;
2 T9 P' `0 W5 Z) B6 a* X& l- ?8 z7 c fp.y = search_string('Y', instruction, size) + current_units.y;/ w) X. `) f9 Q1 j: p% d* ~& w( r( \
fp.z = search_string('Z', instruction, size) + current_units.z;1 C5 s* f; P9 p, Y' A/ K
fp.u = search_string('U', instruction, size) + current_units.u;
& x! t5 ?0 b& r }
# g3 j+ c- b2 X( T; g- M. i- ` break;
T3 h3 ?9 I( Y1 O' o4 x }- ?5 S# b2 i8 v1 ]+ z3 [
//do something!
1 R5 m/ I0 l8 l switch (code)
. U& a1 a# Y3 l8 r& ^ {
4 X6 J4 ^1 `/ g //Rapid Positioning
# |; }) U9 ?+ P6 E2 U3 x3 j" b //Linear Interpolation5 X3 W8 u/ x7 Z0 F/ q) I; N* ?0 b( U
//these are basically the same thing.
9 M' H+ Y6 C' e% N, L4 A0 A case 0:
( S' M6 V+ f* C0 d" Y case 1:
& w( U; h8 z) g1 L //set our target.. p# ]0 }, ^0 F" ~& W- R4 J8 M
set_target(fp.x, fp.y, fp.z, fp.u);
- c3 g; @+ G9 ]/ h' n0 F, k) X //set_targeta( fp.a);4 _9 W$ F0 p* N( G; }
//do we have a set speed?9 ?+ k, v' }8 A3 N! |4 e, `3 n
if (has_command('G', instruction, size))
3 u* E# U N4 S5 R) s {
: A0 X- J) ]2 _+ ~* a //adjust if we have a specific feedrate. B( g+ ^( e& s4 q: j- l9 ]
if (code == 1)
% s4 b& O# Z# Q0 _) M$ n& n5 z {
) e' C3 ~! Y; f- e //how fast do we move?+ n) S G( O8 h/ n$ q: y
feedrate = search_string('F', instruction, size);
8 b/ J, e. ^( g5 l# G2 ` if (feedrate > 0)
6 ]8 N2 d' C. n feedrate_micros = calculate_feedrate_delay(feedrate);- ]/ t0 C+ S; w( y8 U
//nope, no feedrate
- f! n% G& _& h- G& ] else
# I [" O/ z" e$ z feedrate_micros = getMaxSpeed();. i8 X* M: y1 ]
}9 n8 x5 h) a1 U$ Q/ `
//use our max for normal moves.( }! F W4 L; s8 l- I% r
else/ Q3 ^$ g& G5 d. }+ u
feedrate_micros = getMaxSpeed();
5 f: u, G5 k4 z2 ? }( j5 V$ I) y) _2 g/ V
//nope, just coordinates!+ x6 V0 x% \* U/ T# k
else( o( o! j* F0 O! U" T; x
{
# d `2 v- a- C6 e3 ]4 c //do we have a feedrate yet?; d, l* K% w! `7 U
if (feedrate > 0)* w' S4 G' ~" {- _
feedrate_micros = calculate_feedrate_delay(feedrate);9 ^$ y- ^% r* b9 H2 U
//nope, no feedrate. ]1 H6 y# y7 U
else/ g! M+ k( f; R8 ]
feedrate_micros = getMaxSpeed();6 K' t6 w2 c; H. I6 I& C7 i/ I
}& `! s* t# g+ ]* ]4 c9 S4 x
6 t3 N. k1 m3 N, P' C+ o //finally move.
3 m% y# t8 ^7 T dda_move(feedrate_micros);
1 w. K* k0 X, v. \, } if(stop_flag) return;/ w! t7 V2 G+ _! v9 f
break;: Y3 J1 h/ b! B
1 w# `/ G1 W$ V' c3 N1 |5 P, q //Clockwise arc
" O2 \% p" ^1 e3 s* S _ case 2:% z) [# @( m: [
//Counterclockwise arc
4 f! |8 H( h) d/ T case 3:" C* I) l: S& k6 \
FloatPoint cent;" w) E: G3 l% F: ~
// Centre coordinates are always relative S% w5 G+ ?7 z5 O- B4 |6 k0 r
cent.x = search_string('I', instruction, size) + current_units.x;
5 |4 e4 e, E% o( z- ?. s# E cent.y = search_string('J', instruction, size) + current_units.y;+ ?4 X2 p+ I# c8 p% C! g
float angleA, angleB, angle, radius, length, aX, aY, bX, bY;4 s Y" u" {9 C6 C
7 c% a# i9 K& w4 ` aX = (current_units.x - cent.x);
* A! U! y. H& ^$ ]: h5 R2 s: h3 h7 B aY = (current_units.y - cent.y);, X: ~3 Z1 _) a% w5 p- T! O* x1 g. r
bX = (fp.x - cent.x);
% ]7 i. @2 J" a7 v" V: \+ N bY = (fp.y - cent.y);- \" R/ x* r3 ^8 C( L
& u0 h; E1 c' W9 r6 O1 Q; L
if (code == 2) { // Clockwise( _( o# E! m9 _" b
angleA = atan2(bY, bX);
1 D0 c4 a9 m* a1 P! l: R1 a+ \ angleB = atan2(aY, aX);
* S; W- |5 A% }& l8 q } % e: C" N% W$ x/ w( Z4 c
else { // Counterclockwise6 S. R! Y2 x9 \9 n
angleA = atan2(aY, aX);
2 V( _3 j6 ]* U- k4 s: e; Z angleB = atan2(bY, bX);
- z3 K8 a% c' C; A2 ` }% }/ w& g1 ^. ^, B4 B
// Make sure angleB is always greater than angleA
9 M2 N6 W1 | p" }- k# t: Q4 m' z // and if not add 2PI so that it is (this also takes
- V: k, }" ~5 O' Z- W9 L8 x // care of the special case of angleA == angleB,' u% |3 G8 x( H9 M) g& @: ^
// ie we want a complete circle)2 l+ y4 B; P9 \6 _# D5 R
if (angleB <= angleA) angleB += 2 * M_PI;) I! I! R5 ^3 q6 q0 Z* I
angle = angleB - angleA;/ X/ A6 U1 i5 k1 b! j
) R8 J% Y- |, F9 D
radius = sqrt(aX * aX + aY * aY);7 c4 u. `8 Q4 G. [+ W E9 X
length = radius * angle;
$ o. J3 `) s2 d- t, P int steps, s, step;5 Z% z# N; e* F5 L0 L1 T% E
steps = (int) ceil(length / curve_section);+ k7 A' f. M. Z9 ]
4 A$ {/ M) X4 G6 U' w7 X/ E FloatPoint newPoint;
" g$ b2 p/ {- \' b+ c for (s = 1; s <= steps; s++) {
" c, n$ [3 }/ s, X step = (code == 3) ? s : steps - s; // Work backwards for CW
; t# ]! |7 _3 J: A; C3 w newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));2 G9 S& ^4 |7 b2 R- b
newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));, I% `5 {* h+ o3 Y) z( d0 G
set_target(newPoint.x, newPoint.y, fp.z, fp.u);; G1 F4 Y4 H! j0 f" f
* h" P( x4 ^$ w+ I( L // Need to calculate rate for each section of curve
2 v- {& ?# Y x. I& Z, ~& l6 g1 ` if (feedrate > 0)9 J* e: ]; f3 t2 ]
feedrate_micros = calculate_feedrate_delay(feedrate);# l, m/ b# N; p7 Z5 Y$ C2 u
else
5 k2 K o8 \( W7 Y feedrate_micros = getMaxSpeed();9 u) [$ K1 q0 |& B7 {5 H2 y
2 E' I# @& P6 U3 `9 Z // Make step3 J* b0 z) T. q3 \
dda_move(feedrate_micros);1 R$ K4 ^; v8 w( f/ n4 W$ z% y
if(stop_flag) return;
2 B% |. l1 J J4 F7 U }3 S0 @" x8 o( D# g. B
! S7 j* S2 K4 t* x# g+ N$ p2 c' ]: c
break;
6 [$ w8 G: s8 o2 c* d0 N9 @
7 s2 _4 Z" O# o/ [; @. v/ C //Dwell: M; B% S* [ Y& D) ]8 ]7 B
case 4:
- J1 u& f1 R4 L) K) k0 F delay((int)search_string('P', instruction, size));$ U# w/ p, ?( A4 t1 g9 A
break;: F) X. J& O( V& V- @
; c; {9 m+ N2 q9 w3 L) _" m //Inches for Units" Y; p* [ j7 Q$ f4 t% ` I' E& g
case 20:: t: B8 c2 Y1 {5 ~
x_units = X_STEPS_PER_INCH;6 Y) X4 ]- c+ G, c( y2 Y
y_units = Y_STEPS_PER_INCH; ~9 q; v- }2 v( Z0 N
z_units = Z_STEPS_PER_INCH;
9 }8 P0 h% v0 ]. h2 r* Z0 v u_units = U_STEPS_PER_INCH;
- I8 w$ D/ h7 f* i' d: L curve_section = CURVE_SECTION_INCHES;
% A7 O) }' h: w8 C: t# o3 h calculate_deltas();
, ~% v1 `4 p* K0 \3 _* w% ^ break;
0 s6 S t6 t$ ~# E7 M9 A- q, W
" ?2 r' f6 S& L6 {8 t2 a( Y //mm for Units
+ t0 p0 ~3 H1 r7 { E case 21:
5 t* V) _5 O4 k7 O1 L( ~, G: B- y x_units = X_STEPS_PER_MM;$ a9 ~* z/ x4 j# b- S% y! p$ b
y_units = Y_STEPS_PER_MM;
. o: c( M& V6 ~( ~4 _, `7 V6 k z_units = Z_STEPS_PER_MM;. r5 h% \4 I. m2 q( P* {
u_units = U_STEPS_PER_MM;3 I/ u0 O4 U' S5 g3 }0 C
curve_section = CURVE_SECTION_MM;! p3 w4 h7 i/ C# [" d
calculate_deltas();
- R. c" Y/ l: b* x break;9 _2 X7 ]' Q% T( r! a) n7 U
( N5 x$ F0 y0 x; O9 R% F
//go home.( h5 Y2 A e _( r& A, X7 R
case 28: ]/ |; ]! P3 D: {& `, r
set_target(0.0, 0.0, 0.0, 0.0); {: J5 q/ O2 `* D! q b7 J5 n
dda_move(getMaxSpeed());
/ B' V! P/ }2 n# [ if(stop_flag) return;
* x5 ]! H8 g4 g; R# R4 \# O break;1 ?# Y5 @8 Q0 d5 c+ H. C
( b. K3 @0 d5 ?# S* m2 j& C+ q
//go home via an intermediate point.. Q$ h! [+ K# c+ G
case 30:0 @5 S5 j; n) }
fp.x = search_string('X', instruction, size);
1 Y0 M G. z* Q& | fp.y = search_string('Y', instruction, size);
. v( i* m. n( b1 w. L l fp.z = search_string('Z', instruction, size);
# S$ h! a% ]8 {! c( o; [+ x fp.u = search_string('U', instruction, size);, ?) s6 i. B6 M# i
//set our target.# x+ g$ \0 f- }& H
if(abs_mode)" i6 D& ^# H9 e6 j
{
9 ?; b: K" f# F8 K, j# m if (!has_command('X', instruction, size))5 J0 d& B$ s( j8 Y, c
fp.x = current_units.x;; t+ @2 }* k1 b% q2 F) L E9 x
if (!has_command('Y', instruction, size))9 @/ p. e7 L8 U, i5 q* H5 J
fp.y = current_units.y;
3 y" i6 Z9 q9 u, V6 I m3 @ if (!has_command('Z', instruction, size))6 Z/ n. E4 V7 N; {9 S! i2 a- W
fp.z = current_units.z;
( w4 I$ v8 W7 ?% s6 s5 \/ `. g if (!has_command('U', instruction, size))3 G* o7 U/ a$ I1 `
fp.u = current_units.u;% N( R, x- k8 u
set_target(fp.x, fp.y, fp.z, fp.u);6 g4 W4 A; X/ ~3 B
) X9 Q. K! K8 P5 T$ H
}
3 u: |8 |: H4 q4 K4 b5 d else
c" g& L" f/ i9 C- g( y! ]3 S set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );* `- l3 t2 }9 m4 e. H
- _6 ~: l+ S" e, [- }9 m //go there.% E2 |7 S# l9 L6 _$ c: Z" ]* N
dda_move(getMaxSpeed());
4 y( O0 g( D c- y3 t, c if(stop_flag) return; H" c/ L0 Q" M; X
R7 H' l" d$ o5 J- O5 g //go home.
4 G0 f9 X; \6 q4 ^9 F' y7 G set_target(0.0, 0.0, 0.0, 0.0 );5 e4 j) H; z% ~5 h
* u9 d0 W+ e J$ `& u | dda_move(getMaxSpeed());, R+ t6 c7 B, c2 p y7 k, i8 ~! R: m
if(stop_flag) return;
- z3 v7 o0 m5 F& b6 D3 h break;- C8 P" K$ L% @- Y% ?- r
' c# |9 d, e+ B6 ^3 ?0 J P6 W3 j- W
//Absolute Positioning
, [" z& s5 z/ ? I# A2 } case 90:" K* y0 p7 e9 L L; H5 g7 e
abs_mode = true;
4 E2 @1 i) |- K [8 u; G$ j break;$ _' B7 r3 b& e/ T( b4 L& a8 q
; ?* x$ w" t( X1 S; t! Q
//Incremental Positioning
) r* d/ k0 {7 ~) n, F9 f" h L case 91:/ @1 P2 x/ O8 j# W
abs_mode = false;' S: V) y. [0 E* A8 q+ H/ x
break;! ]$ B# Z2 r4 r$ T6 \8 t( f7 O
: f$ k3 b G4 t& E //Set as home
1 |$ j3 W' v1 S! `, B case 92:
) M" B6 r6 t6 ^+ w5 e
7 U, h6 o$ n1 [( h5 ] set_position(0.0, 0.0, 0.0, 0.0 );& x9 w, `6 t1 H. B
/ R+ P: x4 V+ \' {9 J
break;
' u: f: ^, u7 d5 K2 M# B: e h' B% `/ r0 v9 I! M
/*& T2 ]! `' |9 E1 Z; F- F e( S
//Inverse Time Feed Mode
! q! a! O# m) [ V case 93:0 _$ m0 e' E/ x$ R# N T
. s% ^' S* R9 K0 R" T8 d
break; //TODO: add this
- h4 T, W. o4 t4 h- w / r7 w2 x2 j$ t* [7 A: D# B, r) h
//Feed per Minute Mode
0 [( h4 J# x% w# b7 Y2 d case 94:
1 l4 k) A- Z z; V1 D * ^- Q2 }1 e+ Q. `) j. z2 b4 D
break; //TODO: add this
: O2 |6 g& Z# A, o' Y# w3 u */- q. i# v$ a/ s C
( h6 F7 B" k3 Y+ g& K; E
default:- X$ a& v( `1 d/ \) e6 l
SerialUSB.print("huh? G");
9 \# n7 x/ c/ Q9 J: }" r SerialUSB.println(code,DEC);4 y- V6 H5 O( l
}
% L! y/ b/ s* H/ a) u7 p0 Q; ` }0 p9 m6 h2 I0 E/ e5 C+ Y2 p
. @* u [( A# a' p+ p( e o //find us an m code.
0 a6 D; x& f# E! e- b. P. B if (has_command('M', instruction, size))- t4 R, D* H, g: h* ?5 k+ b1 ~0 z# S
{8 D- ~) d7 @$ ?! z/ ~
code = search_string('M', instruction, size);
0 `3 k4 {" b |0 u switch (code)
- `$ m E: X8 i. T% h) X! O {
$ g d4 q# E I2 r //TODO: this is a bug because search_string returns 0. gotta fix that.. D) |/ A9 y4 l9 k5 H$ z# k: M
case 0:
7 N& Z# ~5 e8 u& [ true;% p- Z+ }8 o: o
break;
3 g% m9 K3 F1 D: @/ w. {5 |# Q' h3 o$ v ^5 l+ G* o& c- d
case 100:5 ^+ Q* H6 q( m# N6 T8 `
break;1 [. B8 f2 h l* s/ H6 v z
/ C v: I! F8 ?* Y8 ?/ ?
// fire camera relay/ [/ a7 t1 ]7 V5 r" v. k+ G1 O
case 101:
# r0 i/ E6 J C2 ]( r camera_shutter1();/ V5 d/ Y) g% }9 A
break;
$ }2 G: O/ S$ u% z7 }8 }( }, q- i/ P
// fire camera relay29 g B1 _. z% U8 K6 A
case 102:
/ u# t* L- h' \* l+ ^" G( W C camera_shutter2();
+ r7 \% j: w% g7 k break;$ g: L; r1 v) _0 ~3 n. m
/*
1 j2 |$ ]) E3 ?8 X6 ~ _9 H // turn aux 1 relay on
$ N9 U& v6 u7 a! \ case 103:' i0 t* H- o; x- T4 k- y! {
camera_aux1_on();- ]+ P8 H: P3 {9 z7 N
break;
' l8 O7 K: b1 A0 `! e: z6 n6 L! Z" q9 W8 ] D6 u2 ^
// turn aux 1 relay off
, f/ @ g) q2 K8 @8 V# g case 104:
$ W- x& K7 y5 r" H' J7 H2 V+ @ camera_aux1_off();
8 i$ j+ J$ L$ ~5 u break;& _( K7 X+ I# M8 @
" r( c4 n7 a) _2 d% C2 d& b2 K3 e
// turn aux 2 relay on3 L. e9 @8 t/ K3 o) Z
case 105:
9 Q' m& u+ W7 G: S% a% ]' H camera_aux2_on();
1 f9 t4 [& o: M+ y% I* F: l; O break;
% P. m$ }4 D2 J9 P1 \; t8 K c6 q) g9 d
// turn aux 2 relay off
6 ?( Z4 g* D# w$ Z case 106:% D6 L" f5 p: [
camera_aux2_off();
3 j' o6 ^% w5 Y& P break;
& k( |% N. L8 q4 X J8 h5 Z, P+ X/ p
// turn aux 3 relay on
; E5 R: i9 c- r" U* B8 @( ?+ V case 107:. M8 B* K0 u) B! \/ L: ~
camera_aux3_on();+ e T f: {- C+ N0 {/ K- d ]& w! N
break;0 F4 A( P6 B; V+ e/ @7 L
; H0 `/ O9 ^' }0 r; r+ c7 K7 K$ y
// turn aux 3 relay off% \1 M/ a4 G' r7 R5 ~2 C
case 108:
; [5 C. O; e0 v, o ]% R/ k5 o) [3 O/ \ camera_aux3_off();3 D$ z! i3 A+ g& P" o- _) U
break;# Z2 L0 |2 B/ `% e0 l- t# g
; G( p7 B) d* e( g // turn aux 4 relay on
- N( z: B4 U9 M7 {2 H6 H5 c case 109:, g1 R* |+ w+ C' f) E/ T
camera_aux4_on();
- t% |! z7 X$ i2 l break;
! ^+ C9 S, H9 o) i% a: L9 a4 n# E6 h" Y; V' s& z# N
// turn aux 4 relay off2 o7 C' y! c0 F* B+ j
case 110:
# T0 N9 G/ k1 {) ? P! F3 U3 S camera_aux4_off();
8 ?& H" X1 i" I7 v6 { break;
/ S) r( H5 B% ~*/: _; W" u# a7 u3 H( r& y: P
default:6 B7 l' h3 S& h/ w/ S+ n
6 a% g; F4 E$ t f SerialUSB.print("Huh? M");
5 T5 @4 V+ e- p# q* \ SerialUSB.println(code);, }0 _' H) r! O ~" D4 W# \5 g
}# s4 w6 W/ Y/ G
}
2 X Y- C* Z) k* i' v5 x7 q3 p9 r+ A3 V! j2 s$ m1 K; i
//tell our host we're done.
9 p" c# }" [( c% s SerialUSB.print(byte(78));, R/ d& n2 p6 t, m" \5 U1 a
1 G2 B) p+ k# J6 V: h) t}3 L3 K( a5 I5 X5 S$ K
. x+ Z0 o0 R+ Y' i, l/ d v+ L
//look for the number that appears after the char key and return it1 O0 [6 t. S" |; P8 j. z
double search_string(char key, char instruction[], int string_size)$ ]" Y# w+ @; P, v& h5 k+ b. @3 a
{5 P5 H& A# R" r. }
char temp[10] = " ";
# N+ ?7 h8 Y# N for (byte i=0; i<string_size; i++) \* H$ @' a: M. x% X# t
{
" ]3 \9 l( a0 W9 D" \ if (instruction[i] == key)1 u0 a& V2 e, s8 n) |# ^( I# Y7 f
{, K& {: K( m& s! b3 K# v; g
i++; 9 Y1 O- n; z |, X+ R. Q/ i
int k = 0;
. \5 f' A5 P7 L+ O2 X x9 J while (i < string_size && k < 10): V1 Y+ x* a9 S1 F# V4 M; w8 S
{
: g q! e; P! A6 j5 | K9 Q if (instruction[i] == 0 || instruction[i] == ' '); K8 F4 A1 N7 ^: K/ S- f" {5 m. O
break;- z2 |( _" P' B V$ W$ `. |5 e
) R) _4 Z G+ y% M temp[k] = instruction[i];6 h v8 Q; s4 X% |% z' L& @
i++;) L! S# G2 j; [1 q4 u1 i9 ~
k++;" N( m) [! Z1 i3 b' G, H2 \1 A) x
}. }& E8 e8 P3 p/ }% |
return strtod(temp, NULL);
, Q. J! \$ M# ^ K! G$ M% F }. b2 E0 a3 b$ D; [+ m. W% q8 p
}
6 b( @3 @3 A9 g+ s6 {- G+ Q! x" `6 _+ F* M( \# G' p" F, s
return 0;
+ l6 h) `( i$ h: J8 v- k}
) A# z7 Z8 P- j+ F) A `# b7 p, [
6 C+ y# |; p* f8 R//look for the command if it exists.0 }' x0 t2 E* w4 d1 R% t5 F! A
bool has_command(char key, char instruction[], int string_size)
9 c" c0 a3 s% _{
0 l, k- }; `0 J1 g- J for (byte i=0; i<string_size; i++)- C, y1 J+ _! `9 n6 d4 V5 N3 i
{
9 J6 D2 M8 ?! r if (instruction[i] == key){: ^& C5 J: [/ J; L' g6 e( \5 S! ?
$ u9 X6 u9 g1 e5 s4 t' ^( D
return true;
8 O, q$ B1 j: I }: Z: x) D* `' G. a5 q9 t0 C
}
& F2 n+ T _% ^; n8 Q6 L. E1 A- A; e
return false;+ J7 f& B% m, j7 X
}
& v- r- X ?4 e
8 |1 k' \# q) J' n7 U4 S T
$ }: B l% r: A! Q# b ?' v, g$ H8 d! _4 L0 l" U
|
|