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