|
楼主 |
发表于 2014-5-10 09:55:02
|
显示全部楼层
// our point structure to make things nice.4 o' D9 v1 p0 u! s! J
struct LongPoint {
9 w9 O2 w0 F& S) ^$ K' L: k7 p long x;* W+ M* h( d: I$ C7 @& L( k
long y;
# c) |& i; |1 [/ m5 g long z;4 E" V2 I" V" T
long u;% \, R" l1 {2 j& }+ Q# S
};
1 a2 a# c, C" F0 z! A" v- g0 K, Q* x
struct FloatPoint {
' T) j' Q- I( {% n: D float x;
9 ~) ~( Q9 q& ]4 T6 Q* d float y;
. p# ]! P$ ]& m4 T float z;5 L I6 `, r7 n/ C P
float u;
) r( d) T' V5 K};9 ~ n- M0 |5 v0 r/ n
- G+ H- O( G5 f3 d2 a
FloatPoint current_units;2 i. w6 P* ^5 Q
FloatPoint target_units;
; Y6 |8 O( ?! [5 F9 zFloatPoint delta_units;
) ~9 `1 d5 _8 U2 C# \! d: {: \4 E O. \) [
FloatPoint current_steps;
- |# v$ z4 K7 Q$ W* _FloatPoint target_steps;
- P% P4 b& m3 S4 [* v/ AFloatPoint delta_steps;
6 m( x$ z) k* O& D- i1 @! G
2 q. j8 F( p" W/ P; Lboolean abs_mode = false; //0 = 增量位置模式; 1 = 绝对位置模式
3 s4 ^4 S* S1 B6 h$ x7 ^: v# r2 I- C; P' @- e
//default to inches for units, B1 O' D2 F+ O2 I8 \* B/ o6 ^1 i% ^
float x_units = X_STEPS_PER_INCH;1 M, F6 Q% O0 x. I& c8 o8 u
float y_units = Y_STEPS_PER_INCH;
( g+ m% W) I- a% \+ {# ~float z_units = Z_STEPS_PER_INCH;
: p5 J* Z/ B; U0 n4 Mfloat u_units = U_STEPS_PER_INCH;% ~) a& K; a& T1 }
float curve_section = CURVE_SECTION_INCHES;8 ?* ]: y' u, Z) z9 k& p
5 R. G' ^0 B" j8 X n//our direction vars
3 ~0 u3 C; Q* lbyte x_direction = 1;6 B! h# D* ?( m. g
byte y_direction = 1;
- ]/ r9 p6 V7 H* v8 R2 ]4 e& B; a! sbyte z_direction = 1;
$ b6 w* s' y" }+ M$ Nbyte u_direction = 1;
, Y; P0 k4 {; N* \
4 X- _2 @" P) B, K- H' }" g//初始化字符串处理+ j3 s; n& I. ~6 V, A
void init_process_string()
1 q( a! Z5 ~, M: g( A{
! X' r, i# H6 U( s9 R' e //init our command, }$ w, [. e e8 d* y0 Q
for (byte i=0; i<COMMAND_SIZE; i++)
" [8 G9 X0 k5 G/ q word_old[i] = 0;
! Y8 c. T1 I1 H2 I/ _- I Y" P serial_count = 0;3 o' S* F n+ C E3 p
}) d. h2 |7 y: P/ h! R. f
: }+ I' I/ L1 a6 O0 }/ M
//our feedrate variables.4 _7 Q9 ^( u9 g9 y4 q; X- \
float feedrate = 0.0;
. v- `$ C _% k2 F8 e$ glong feedrate_micros = 0;
5 Q5 a8 L1 S; P4 T+ [/ V/ I# e# \1 l( X6 P" i) n% D) f* i1 ]! V: X
//读取并执行命令
% ^& t/ f: H/ h; \, q1 rvoid process_string(char instruction[], int size)
# e7 C5 u" U# D3 X6 i' }" o, A2 I, M{3 @# @4 t3 R/ n7 t; U
//the character / means delete block... used for comments and stuff.+ g' Z# Q# f3 [. B& Y" R
if (instruction[0] == '/')/ H: d" b$ v( l0 D+ p* {" ~5 I
{
" ?. w% b0 Y: Z8 i# F8 W2 } // SerialUSB.print("ok");/ f/ y+ f* B8 F! n0 a. L
// SerialUSB.print(byte(78));6 v. i3 I, y4 Y' n5 I7 f
return;
0 u4 g8 m6 x$ P }4 E6 y/ D: x( K" A9 S6 q9 M/ ~
//init baby!
* ? C5 }! o c2 I0 v3 M& t ?. Z. K FloatPoint fp;. ^: j( I( i( C( j# O2 P- V( P
fp.x = 0.0;
5 H+ j$ W) [( { fp.y = 0.0;
' f, v7 m9 ^2 r8 ^' l0 B fp.z = 0.0;
/ w2 K$ _; R' d* p& ~ fp.u = 0.0;
( ~! N( y7 d" B7 f3 o4 k3 I, D. e
byte code = 0;9 D, T2 y! |" T9 v
8 a% M7 { X# b. |
//显示在处理的命令, M/ @* S5 e, K. |9 f# ?
#if (LCD_TYPE == 1604 ) B0 q1 h2 r! i; H; S& `$ L
// lcd.setCursor(0, 4);% B' V# R4 }. D6 {5 W/ T; ~
// lcd.print(word_old);7 p9 n8 M4 i4 Y: H( q) ]' N5 K6 _
#endif3 W5 Y) @; V0 I3 a; i6 L0 @8 N/ g
SerialUSB.println();# ?/ _, v0 f9 m4 s4 H& E
SerialUSB.print(instruction);1 c( d; Z9 p3 t0 I
SerialUSB.print("\t");
3 V' u, N b; v6 [4 T) f
. I/ H8 k0 @5 ]5 y6 ?4 t //what line are we at?2 N( E& Q& T# i# J, v
// long line = -1;$ D- r+ ?, K# f
// if (has_command('N', instruction, size))
! }! W9 {, a! `3 ?. z // line = (long)search_string('N', instruction, size);% L4 S; U8 S" U1 E' W# [0 b
7 B$ B3 N7 x, E( j( V1 X4 o! Z /*
2 ]0 ^- h3 t7 h3 O' C- g Serial.print("line: ");- C2 D/ v4 d/ H' A) S8 r% ~
Serial.println(line);4 p0 U" R3 L: J4 ^# d8 n
Serial.println(instruction);
) Y3 A# r3 L* Z */* p7 l! L* ~( v) I, B) q; c: [
//判断是否读取了个 G代码?
) j7 |$ z, t j0 D. N if (6 _. d( L( O/ w
has_command('G', instruction, size) ||
3 i, w; p. G4 s6 ~( p has_command('X', instruction, size) ||& s0 j8 ]; k$ \
has_command('Y', instruction, size) ||
1 L% h" U4 l0 [1 F* ~( x% X# R has_command('Z', instruction, size) ||
( L9 W. C) M2 x( _0 y' f has_command('U', instruction, size)
" i9 C. j6 H6 |( R )
( K6 D4 h# B0 k+ N1 c {
2 S- M' }6 o: s //which one?& {$ g' ?3 O7 X, C# I+ B2 J
code = (int)search_string('G', instruction, size);
( `7 C* \+ w" O5 l! t0 E // Get co-ordinates if required by the code type given0 A" ^- b4 O2 N' v2 ^
switch (code)) ?/ X' l+ u4 ^! |% W4 ?$ y: K
{
* o: y0 u; }: c8 s case 0:
+ J7 S# ^. B7 C3 O- t' O% [1 F case 1:! B2 p* ^8 N1 Z1 K$ v
case 2:5 ]- I7 x8 X) f- X. l
case 3:9 B! L+ T: y2 V4 j& p
if(abs_mode)
( t6 N+ q: O( L* o0 g. \3 ] {
' J" G- T+ M7 e. r: P+ I2 V) ^ //we do it like this to save time. makes curves better.
; C2 z3 d$ k6 v* Y. U //eg. if only x and y are specified, we dont have to waste time looking up z.$ x2 t3 m9 |# q& n/ `: l5 n, B8 E
if (has_command('X', instruction, size))
) v& h, b- l, Q" w: u1 j4 |* y fp.x = search_string('X', instruction, size);
& d6 w8 }8 x' Z. k. Y, ^ else
+ A3 ]+ Z ?; o- u fp.x = current_units.x; V0 o( x1 F9 m7 T
1 S- I: k# C7 }' y
if (has_command('Y', instruction, size)), O3 n# n# _3 A; n6 t- e0 e
fp.y = search_string('Y', instruction, size);
: G, q/ I# Y* H/ [% e else
: a% R, K' B1 K7 \ fp.y = current_units.y;
5 V3 b( J) j! P2 L, e( G$ L7 _( h$ ?5 k" K4 K" ]. h% P
if (has_command('Z', instruction, size))
! o) E8 Z" s- { z8 ~ fp.z = search_string('Z', instruction, size);
( H; {) T5 m! Z8 c else# R5 q9 t' c7 i7 h( ~# _4 B
fp.z = current_units.z;
! _. N, _& x0 e9 l/ T
! B0 @ J1 G2 m1 K3 ` if (has_command('U', instruction, size))$ [ W* H5 z) i1 z3 a- Q
fp.u = search_string('U', instruction, size);5 @4 Z. ]- o6 @; _& [0 c c
else
1 I4 e6 ^$ \: H fp.u = current_units.u;# ]2 }8 n2 }# r6 ~: v* j
}9 t5 n$ P! \8 l
else; q" K1 H* h. g+ w$ a6 r$ x' W
{+ i; s# K# {4 l
fp.x = search_string('X', instruction, size) + current_units.x;+ o; s' e% ~7 [- x
fp.y = search_string('Y', instruction, size) + current_units.y;6 c$ k. f- X6 ?/ i' E
fp.z = search_string('Z', instruction, size) + current_units.z;/ ]$ G6 a9 ~- c% B- D" g
fp.u = search_string('U', instruction, size) + current_units.u;
: T- s* T/ {" Z8 \; M8 W }
# `9 Z. p; X" m. m# A% Q/ ]5 W break;
( p9 N( _4 G* }" p. s0 o$ N) X }
) F$ k" [% K: Z# U# M3 O //do something!
0 O( @- K# R# P6 J* W' ^" f) F- O switch (code)
4 x% B9 c' {) S+ a) u; X$ L. {# s {
) u! V* W3 ~7 [. \9 C0 B' z7 x, g$ ] //Rapid Positioning
, |5 _9 y' ^; D6 h //Linear Interpolation1 L% q$ U( B' ?3 }2 B: j: r( c3 ]
//these are basically the same thing.
- d1 ]3 F% t4 V6 a3 t- M case 0:6 @: b8 O0 A5 D& A4 n$ Q
case 1:
$ c+ s" M- s# \& U U //set our target.) K* @. Y- M* @! G2 g- N6 t
set_target(fp.x, fp.y, fp.z, fp.u);
) Q$ T( f; q3 `7 ~5 b T //set_targeta( fp.a);1 S4 f0 T) x) x; @; A8 ~) k0 c) t
//do we have a set speed?
- {" ` ^& }0 j- C if (has_command('G', instruction, size))( t/ d! v' l8 T, Q7 O/ A
{
& X, N% H8 T2 v- Q4 ` //adjust if we have a specific feedrate.3 V3 {% x- g6 |( u
if (code == 1)0 m" ]8 c0 S- g# I: Q" h" Y) d
{& \3 ~6 F" {& l* Z" f* z$ C
//how fast do we move?$ k. R7 p/ C8 k0 C; @ u7 Y
feedrate = search_string('F', instruction, size);. K6 j9 _4 ]/ R' n2 j) X
if (feedrate > 0)
" d" n+ m$ D; I, L) |$ P feedrate_micros = calculate_feedrate_delay(feedrate);& u- l$ o* h1 o1 h9 b
//nope, no feedrate
! |3 [% p0 z G. B1 p7 m9 X: w else
* w$ {$ d8 s7 t W feedrate_micros = getMaxSpeed();+ Q5 |8 d4 ^2 q' J( l9 ?! s
}) Q/ h& Q) \( m+ X- t7 u; g( z, Q
//use our max for normal moves.
6 T. K4 n9 i2 f* _8 k+ f4 b: k else
+ }6 q2 l0 t) g feedrate_micros = getMaxSpeed();+ n% G/ z+ I) M# ~5 {5 {
}
+ C9 P; I3 D2 a0 p5 ?3 v6 T //nope, just coordinates!
3 y2 z7 T8 M+ p3 m/ b U& L& l& n* n else& b- I& G+ o. O; a1 x6 H6 D
{, g; {& k+ j$ r! u% s0 J3 I4 k
//do we have a feedrate yet?' b# C, P/ y; O
if (feedrate > 0)! @# H) k3 H4 E9 d' X
feedrate_micros = calculate_feedrate_delay(feedrate);5 m, ~. I4 d. p- R, z
//nope, no feedrate
, |% C- f i- ^ else& J/ a+ E$ \& A5 z9 e5 `
feedrate_micros = getMaxSpeed();) u9 K3 d/ _( n, J2 t8 `# N6 j
}4 T5 j$ y4 W. w( r
9 I4 s* i. r0 x I% E1 s3 T //finally move.+ l- L! R. O) m+ b
dda_move(feedrate_micros);
, [7 T4 h6 ?% o/ k# m+ c. T. K/ c if(stop_flag) return;$ Q' c, _) b4 j
break;' q" U R/ g- t' v% A
) I7 F6 T, W0 j1 J8 |% d //Clockwise arc6 a* @+ V! X4 z
case 2:
/ J' [+ b. U9 n$ j( d, g& l' _- d //Counterclockwise arc
( R0 S; y: {5 L6 y4 { case 3:
3 L; m' I- M: u6 s3 K FloatPoint cent;+ c3 q& ~& z; y( `
// Centre coordinates are always relative
% u' U0 @2 m7 w/ H: h+ r cent.x = search_string('I', instruction, size) + current_units.x;. X9 |" t$ _; ~% I
cent.y = search_string('J', instruction, size) + current_units.y;
5 h( h- R7 X% t% n# } float angleA, angleB, angle, radius, length, aX, aY, bX, bY;% l0 y, n- c# \% v( a) t% ^
" V( U8 v0 Z( u7 [4 W2 ~9 o1 z
aX = (current_units.x - cent.x);/ f* o% L" B& F! ?& E4 {/ S
aY = (current_units.y - cent.y);9 M3 b$ o; z+ o
bX = (fp.x - cent.x);; I' |' Z2 j( i
bY = (fp.y - cent.y);6 K! N2 W/ Q* O% R
7 M6 r2 g( Y8 Z+ r if (code == 2) { // Clockwise$ X% G; G+ @9 H( c) D" H
angleA = atan2(bY, bX);' e5 F9 ^7 {' ]1 @% N7 p
angleB = atan2(aY, aX);* F& m/ \, O Q+ @; @7 K d$ I! c
}
* j5 P! O! G7 Q, L else { // Counterclockwise! o( h# \/ w- m) n' o; R
angleA = atan2(aY, aX);0 t0 j1 Z/ \4 n" G
angleB = atan2(bY, bX);
2 M; s$ \, g- T- e9 \& U+ p* j }
- r8 @" y8 q3 p // Make sure angleB is always greater than angleA
; c' z6 _$ ?% P# K- U7 f2 X# Q // and if not add 2PI so that it is (this also takes; M2 p4 R+ k* n x0 g; `
// care of the special case of angleA == angleB,8 k8 B' P. g0 ^5 R9 x
// ie we want a complete circle)" r8 S% p2 D6 W# D) V
if (angleB <= angleA) angleB += 2 * M_PI;
+ V6 f1 K& Z( w angle = angleB - angleA;
% v6 l# I0 U r6 E& s5 V
2 s. Z: e s4 s }" m9 ]3 D% t radius = sqrt(aX * aX + aY * aY);
6 Q" p( Y0 c7 e$ F length = radius * angle;
% s6 J# m0 d4 w/ _ int steps, s, step;3 ], v% {2 P0 c% v) \1 O, V
steps = (int) ceil(length / curve_section);6 a4 \$ i4 P8 H5 } _# `- y
8 u( Y3 B$ [* v# {1 ~# u9 P5 p
FloatPoint newPoint;
1 `7 Q4 t" g# p1 ~4 q5 ^ for (s = 1; s <= steps; s++) {9 F5 Y+ u( c* N# P9 C; x% d5 `6 h
step = (code == 3) ? s : steps - s; // Work backwards for CW4 S; @; Y& W- C6 b0 v' S. }
newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
. f( C, T$ N7 C8 @( C2 | newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));: z, f5 p' q1 t( M4 T, _/ \
set_target(newPoint.x, newPoint.y, fp.z, fp.u);7 ~: B4 {, Q- P; W, [+ {
# l2 P! M+ i/ x1 _ [& s // Need to calculate rate for each section of curve
. e& O: M" ~; A if (feedrate > 0)
7 O( D" t6 o# X" J feedrate_micros = calculate_feedrate_delay(feedrate);
' q, o- t. n% i T+ Z; d' b' Z else
: m) r) n' h4 G8 E6 y feedrate_micros = getMaxSpeed();
+ @" O8 G2 C' I2 _2 y: H
4 d. E( F+ }7 B0 u. T // Make step
! ^& f1 \0 M0 w0 x dda_move(feedrate_micros);
% S7 m6 J% k* _: T2 [1 _2 I if(stop_flag) return;6 Y1 u" p1 o4 B, t1 s
}% m+ [' @6 ]. L5 M8 Z
7 |, r$ C( W& w& T6 Q: C6 V& `8 u
break;- `7 }! t4 e3 P' Q6 Z& J
5 ^- g- I: k b! z D0 K
//Dwell0 {2 {# u2 w" Y
case 4:* {$ P8 p0 E9 d7 f8 t7 u7 k
delay((int)search_string('P', instruction, size));
1 O' M. P) u# H5 J break;
* \- J2 X7 o9 U. B0 f- S# h
" Z, |- ^: V, B: @* Z //Inches for Units
% N+ T: \* D) m* s) z case 20:
+ S' Z/ ?: _0 M5 S x_units = X_STEPS_PER_INCH;) Q( A2 t5 N5 a/ ?$ E
y_units = Y_STEPS_PER_INCH;; \, o7 w0 `/ `! ^0 ]
z_units = Z_STEPS_PER_INCH;$ r2 L* a. E% \1 Y
u_units = U_STEPS_PER_INCH;
' N/ y0 B1 G5 f# m& e, ] curve_section = CURVE_SECTION_INCHES;/ h$ a9 h3 e3 r6 }0 O
calculate_deltas();
& D" r+ ]% W B1 H9 D break;, ^. A* [! ]. f& X' `
' N! b/ C, H% e5 L) N/ d! R4 U
//mm for Units
, n. ?1 s% e# ^ case 21:. r$ q2 n, u |, Y" X; X6 y+ G
x_units = X_STEPS_PER_MM;
g- i5 S; P# F- Y! E# J y_units = Y_STEPS_PER_MM;, e1 J# N' S. J" n6 A$ l
z_units = Z_STEPS_PER_MM;
; Z2 ^8 F- ~ u/ e! R0 r u_units = U_STEPS_PER_MM;& k ~) q! c# T0 ~. F
curve_section = CURVE_SECTION_MM;
) N. `4 j; f* A1 D calculate_deltas();* y2 A6 i+ z/ H3 h* K
break;1 v! M: n$ g2 J; Z* O% P6 }& D2 d
6 E* b9 f8 \# k8 f g8 m8 }
//go home.
) @- _* e) v4 g1 ?4 q case 28:
. i3 w1 E$ h# S: K4 ^6 L$ F) _& j set_target(0.0, 0.0, 0.0, 0.0);
# d; [' k: ]4 ?8 y6 N dda_move(getMaxSpeed());* X0 o% t5 `0 w% d
if(stop_flag) return;
/ W v$ m0 f' w6 H* h8 V6 F break;$ u7 K& `* I( b3 f$ H
6 V- c7 w! |% |0 F" \
//go home via an intermediate point.
" _- \7 @* L" T! A; `1 s) [ case 30:# H# O$ ?+ N. j4 N7 ^. S5 J" s& f$ p
fp.x = search_string('X', instruction, size);+ n- F& B: ?, O: o" Q! l: K! P y
fp.y = search_string('Y', instruction, size);
: x% \; a; x3 s' f3 [; F/ q- H5 v fp.z = search_string('Z', instruction, size);! {# c, q5 _% V- r* b
fp.u = search_string('U', instruction, size);
: _3 i; n: K. h2 f/ \3 {# _ //set our target.
7 _2 q5 P6 O0 I if(abs_mode)
6 }) |3 W7 v0 ]9 S8 V- x& Z {, K& _- Y0 K* H7 a- q2 L
if (!has_command('X', instruction, size))6 ~% M7 j; Y: e8 g7 w; s6 K, Y
fp.x = current_units.x;
( @: E1 ]6 k" `6 V/ ~ if (!has_command('Y', instruction, size))- Q, j, `( g) Y" X
fp.y = current_units.y;. D1 [' [# C0 w+ s
if (!has_command('Z', instruction, size))
5 x5 z4 h" R! T" u9 o0 I& t; B fp.z = current_units.z;
% I# P# M$ S7 r if (!has_command('U', instruction, size))
7 o, H( K/ a. j3 P! R fp.u = current_units.u;$ X* u# m$ D7 ^, j5 D) D
set_target(fp.x, fp.y, fp.z, fp.u);8 u! m* z) X2 ?# I+ a" e. a6 n
. w; p A" z; M* R% O }! d2 a1 S' n) r' V; b7 L6 G% v! Z; |
else
6 S% v- J, K E set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
# I5 T( M, q) q1 L
7 n/ d) y& R. h ^: T$ _- r( j //go there.6 n' ?8 A- p6 ]4 q
dda_move(getMaxSpeed());. ^6 a" N" w8 j1 R
if(stop_flag) return;% t8 d4 s: t! a0 } n! u8 Z
y; F7 ]! |4 M //go home.
, |( `4 E- V$ v( B" E9 f set_target(0.0, 0.0, 0.0, 0.0 );
# ?: G) `9 C* C9 \9 J9 K k9 A6 k7 N, y
dda_move(getMaxSpeed());! I' X$ `3 m$ g* x" ~# X
if(stop_flag) return;
9 t- w! k5 T# s( K- u9 n break;& D e" [0 M; O/ G( e, D. C
) R) M, T, [! F+ j/ r. R //Absolute Positioning
4 Q& t2 ]" c3 ` U9 v* J+ v2 s9 G case 90:
6 Z6 V b8 c9 m' z, g abs_mode = true;
! _: M) o7 J* R" D0 v/ P( i5 j+ _ break;& I) q; Q& ^/ x6 ^2 P* ^* m# B/ _
1 `, U* e' }* A$ B //Incremental Positioning
& n; R- u# {7 V( B1 h case 91: ]9 l4 R) M/ X- U
abs_mode = false;8 a. \3 a0 y; e' F0 t
break;- U7 y/ o$ V) c# ?
1 ]" U" u$ p2 R$ O. e4 ?" d- {! J0 o, I
//Set as home# v/ r/ a. E0 T& Z! L
case 92:
! [$ K h# @) K. r/ |) b + y2 O* }3 }5 A. u4 i$ L5 q" h
set_position(0.0, 0.0, 0.0, 0.0 );
7 q. w: g$ R: o 2 N3 f3 V; J$ d# x" J
break;0 X7 p% E8 r$ ]; |, ?8 S9 n: R
' Q+ m3 v2 }8 |0 ]9 D /*) D% f, S% E6 P" \! B
//Inverse Time Feed Mode
* x+ k$ p( E$ B1 `- A' _ case 93:
+ r- I3 } I/ v# ]7 V
* b @+ p0 A, t. n, A U4 C break; //TODO: add this
# {- A/ E2 ^- J1 v! G0 z& r' ` # @4 q2 A9 h, U9 J3 K# j- G
//Feed per Minute Mode
3 `1 w& ?& t1 `0 r% A; d5 {$ g9 s case 94:
7 x; c9 l) w0 v1 l0 G- r" J
" T6 w, O( p2 o, K7 T# g break; //TODO: add this
O% a* ~3 i1 D$ }3 D */
0 q7 Z! f& l$ [, X
" F7 c3 \- F" c* d7 ?5 R default:5 b9 @# W J; Z: N# @3 m
SerialUSB.print("huh? G");
: h1 J! @0 G3 x8 S SerialUSB.println(code,DEC);6 c8 n/ a0 n6 J
}5 b4 J9 @* u8 v2 p4 A
}$ ^; } X, _/ K- [7 x4 O8 D
3 C% w5 B0 l/ @; j) i; D0 \ //find us an m code., D- h9 E& O% b, Q8 W/ l
if (has_command('M', instruction, size))
9 [1 C3 A# \! E" N- x* T2 m" O {* z0 w9 q) m+ q7 W; n: K+ \! G( j
code = search_string('M', instruction, size);
4 ]9 |9 `0 z" h. i( j- ?; O4 ~ switch (code)# x( {5 b# L0 P; v4 z R& K
{
5 B2 C& k. |/ G" S& _- V //TODO: this is a bug because search_string returns 0. gotta fix that.
! @+ a2 L/ a, k/ ~# {! X3 A# x case 0:
2 J" J; X) A# e. g9 M true;
& |. h* h6 ?9 f, } break;
& }% q6 m4 d$ [* [ r; F" [7 H& B
, a; [, L( _8 z9 N case 100:& w1 H1 G$ O2 N& k2 D" L* l1 T
break;
; A+ c4 [( D* D2 f* }$ @$ u0 ^( T8 G& P3 A, c
// fire camera relay/ B6 X. V# m$ ~( `+ b: i
case 101:) l6 `7 r5 M* P& [4 a9 u
camera_shutter1();
* u/ S7 {* ]7 u4 Z1 _! S break;' \# v( H! e, c. ]1 j- q! a! }
) g: A5 b1 ?( O8 c! H+ _" l // fire camera relay24 E1 @4 T; `# ]2 _9 @$ N; ^! \
case 102:
' n2 W- o" T7 M8 s camera_shutter2();
. t8 G$ @7 ]: Y2 J9 Y break;* o# a* O& o+ Q9 s) p. i
/*
3 S. S) r, ?/ R: I // turn aux 1 relay on1 ^# D9 [9 d6 K6 f. H3 ?3 _
case 103:1 ?# g1 V5 L+ u1 S; r
camera_aux1_on();
& R$ J' G) V; C" z+ ~ break;; D) y g0 p* k+ z8 L
0 p( J! ^; F1 Q: w; |/ { // turn aux 1 relay off7 o7 Y. {+ W! Z/ q Z
case 104:% x5 b; ~/ a1 C9 G& i
camera_aux1_off();
( [: \* z3 F) ?( Q; k break;
! K2 ~& F. j& O" ?: `/ r3 y. M8 _
4 L6 g- F: \7 P/ x // turn aux 2 relay on
% A2 I8 M+ v+ t8 a case 105:
& O: [" j- J6 \5 I0 I camera_aux2_on();+ x9 i N3 J+ x2 z
break;
2 R+ g5 x8 W: `; @; V, l t6 z; G2 T% a; G- u6 D
// turn aux 2 relay off( S) [# k3 S6 \+ ^1 }* K+ F) @
case 106:3 A/ q1 x8 ^0 Y& S$ v% x
camera_aux2_off();
: Z( A. u! _9 k1 ?& i2 z+ @! w break;( M+ B" p2 A# I0 _1 `& B
9 N# Y# p' B( y/ V' T+ V
// turn aux 3 relay on
* v& v% U. D5 i, k4 i case 107:
5 D9 q' K' g8 a. v. _6 R. H& g( ]- } camera_aux3_on();3 ~ p& L9 S$ v+ ]1 s0 r4 M" _
break;
4 A) r9 p e3 k4 R: Y4 T& }
, O/ ~* \ b$ C- c/ j" L // turn aux 3 relay off4 y2 l3 v. B( P8 a7 N2 q' `. T; O
case 108:8 s2 {, h" q. o$ ^/ ]
camera_aux3_off();! v+ ]2 l _# f- O- x! ^) k/ a5 E
break;0 R6 n% s. o7 D! {8 _1 x
7 z7 \/ V; w( _, U, @8 k
// turn aux 4 relay on& F2 Z- C" Y, Y+ a- L+ \1 R
case 109:
% b1 [) S/ i4 O0 ?; _' ~* A9 p camera_aux4_on();
. v7 G2 z: J3 T# v2 L0 P break;8 i9 u% L+ Q5 R' \6 z
& ]! `% `- ~- a# Y' C2 F // turn aux 4 relay off
. E! _: L1 q% { case 110:. V. Q* Z/ M. Y0 H3 o/ `% ~. {
camera_aux4_off();
9 l- x' g X- B7 `$ J0 C) z" j' w break;
8 t0 _# ~' i* Z% Y*/
- \; k7 B8 v s# B0 o default:
1 ?1 w- [& }% U+ h, r! _
, v6 R4 c L& |' c& ]% ^2 k" A SerialUSB.print("Huh? M");
, X( R/ A% ?' ]6 Z, V SerialUSB.println(code);
# ?. E9 G. ~! Z4 u- R' [$ z }
9 T2 e. y3 z& w% }7 z }$ U+ G- G- d) N! ]" L, W0 d
S/ X/ S% k+ g! N //tell our host we're done./ b. ]/ W& ?7 I0 C8 ` o6 i
SerialUSB.print(byte(78));
/ Q9 O6 F; } v# x. h
' z7 f0 z2 a; d1 j' s" j2 f}. S) Q* [; k7 \# r
2 Q* M* N' |$ m3 w//look for the number that appears after the char key and return it+ s1 E. C2 \- g* F' q* j
double search_string(char key, char instruction[], int string_size)
# K& Q- b+ x" G# N( S{
, N1 j: j5 L. Y) u char temp[10] = " ";
( J( P/ m+ N N) L0 F" _+ ?! u for (byte i=0; i<string_size; i++)+ x1 P6 B5 ?0 O, ~6 i
{! O* M3 T$ y7 P
if (instruction[i] == key)* ?: L' h+ O; C, a4 x
{; e* O( n t7 J: n# _3 }' p
i++;
* @. Q" _( a% {/ R+ v int k = 0;+ y: ~4 Z+ Q# x& ?
while (i < string_size && k < 10)
* b" l! U/ D3 R4 D {
7 g2 O1 P7 m1 U0 w if (instruction[i] == 0 || instruction[i] == ' '); T; _+ J1 L8 L8 P
break;- B, o# e# V* O! O- I8 H. H
P; I. ?% k, E+ Z1 P* [ temp[k] = instruction[i];
- Y" r* N: g' Y1 F" Y5 r i++;& ^- c1 z0 [$ }- x$ Z# Z
k++;
+ G; \, j) Y0 a. ?- E) O }
9 W9 ]) ]0 H3 n return strtod(temp, NULL);' S$ g4 j! F- J
}1 [8 M& A$ o" R* P, G5 j6 @8 p
}
( k1 d" w. Q$ m2 r0 {4 m" |6 E6 o( d S% ~
return 0;! [$ ^/ `2 P1 i: u
}7 D s! ^. b$ f+ `6 ]
, s0 @% K Z/ w& ~9 B//look for the command if it exists.
; e" r0 e) V" S# y0 R0 rbool has_command(char key, char instruction[], int string_size)
/ i: V k& d: e7 ?. Y+ \{' |# ?- V$ u+ }. J& h2 G
for (byte i=0; i<string_size; i++)# w; ]4 Y) D9 j' u5 a7 k
{
$ Z1 s: x8 Q+ k, D* u) R6 { if (instruction[i] == key){4 e# Z: ?( h2 D+ P
/ i8 u2 [7 Z- y+ V4 }
return true;7 {0 ~& y+ E. M n) @
}- D9 h- t' I$ f r/ _6 V2 k3 e o
}
0 w$ h# b \! e' f5 I2 |: h: }1 [- x+ c2 w3 r: g0 I1 c
return false;( D7 [* I5 U" J3 \1 L
}7 m. [9 V3 A4 K4 k9 g5 e
4 `5 P; z/ l! W% `+ ~% e) k
; ]. m% \. ?6 ]* Y9 f6 [
6 d- \( g7 K- _! k, K, s |
|