|
楼主 |
发表于 2014-5-10 09:55:02
|
显示全部楼层
// our point structure to make things nice., o) ~5 p( { t2 y$ w$ l6 W
struct LongPoint {; @$ a' ~9 `/ @0 C( O5 d, i9 U
long x;
2 Z' |- _) Q9 C& d: K# Q: B! r( S long y;' X0 i3 A4 p$ v+ g6 a) h5 x/ h
long z;# D& H2 f9 y1 X/ |6 D
long u;
" C: E" m% t0 J3 t};" | d# m4 V( @! V4 x
- D$ P; ` D2 x/ V& K5 F# g2 Estruct FloatPoint {
- k! h2 c; E8 g$ c N float x;, ?9 x8 |1 {; T0 x6 Q+ i" [
float y;2 u# _5 `" k. L! e* M
float z;
! \' M- z1 \6 V0 `3 Q float u;
* J. D: t1 B4 J+ B2 F2 x$ c};
/ F: N" c. h0 Z2 T/ f1 m7 P* u7 W% \5 P0 x) x+ H* x$ q7 n, n
FloatPoint current_units;
2 d, H6 a9 E# E0 I$ L: HFloatPoint target_units;
9 p7 B/ T! |) \* l! w9 d2 |FloatPoint delta_units;! K) k2 a0 S2 q) A/ S
, c# z! C, X3 u/ i* F+ aFloatPoint current_steps;
! o6 {/ W7 T6 V6 R6 h4 FFloatPoint target_steps;
* g$ Z1 H0 O3 q* n* k$ k. c' ]FloatPoint delta_steps;7 c+ J5 Y% `2 [% d7 ]1 }/ b, n
* I% a/ n- m6 i: @8 qboolean abs_mode = false; //0 = 增量位置模式; 1 = 绝对位置模式3 z: \& j- _) G: W4 a1 B
: X- E0 W9 z# X9 q
//default to inches for units
# y' ]9 N- n% x# c- a" ifloat x_units = X_STEPS_PER_INCH;
* t5 U* g% S$ e1 Q% Ffloat y_units = Y_STEPS_PER_INCH;
; R$ D. t2 k: o8 d: i4 sfloat z_units = Z_STEPS_PER_INCH;1 a$ W9 y# ~" g3 U9 ^# h
float u_units = U_STEPS_PER_INCH;+ H5 p* p4 T* |$ V. {
float curve_section = CURVE_SECTION_INCHES;2 U9 K0 F" O3 T3 j2 L
& o* c: I1 v8 U5 `8 @( s$ l
//our direction vars
7 N2 n1 |5 _% g: c" S. Xbyte x_direction = 1;8 b3 ?, E- M0 c5 L& Z. D3 C
byte y_direction = 1;
4 Y9 h& m: M+ h, o. \byte z_direction = 1;
* @% p( X- P- `; U# s8 q2 Ibyte u_direction = 1;" |' `5 e1 p- \1 d3 I# v7 h
, u5 P4 X9 i% A8 y) Z1 B
//初始化字符串处理
1 Q( k4 J: e) E1 w% Zvoid init_process_string()* U, r$ |5 r: W- A
{7 R) [& g; e1 B% _
//init our command! N$ w6 M7 X' Y4 B! {
for (byte i=0; i<COMMAND_SIZE; i++) e* Q4 @; ~ [! b. v4 @
word_old[i] = 0;
0 a( y: A. [; o serial_count = 0;: o& o7 c' R/ E2 K7 m
}
$ P$ I) n+ \" r. L: b& d% U! E' S5 X: T! C
//our feedrate variables.
! L# u6 Z4 p# G3 `3 |+ Hfloat feedrate = 0.0;# F2 a, j3 j0 z
long feedrate_micros = 0;% T* G0 J9 Y# b( Y) l
" N# [9 O7 z+ Z3 F//读取并执行命令
3 U* g# T) t6 `. u. h0 c" xvoid process_string(char instruction[], int size)- Y- E0 H d9 X/ Y( f/ j7 u
{& r" {* |1 \% z+ L
//the character / means delete block... used for comments and stuff.. \$ \) c/ U( B |
if (instruction[0] == '/')
: Y& n% m# G' ~! C {0 q" U1 M( X, `! z
// SerialUSB.print("ok");! ~" u6 c4 k; F. S
// SerialUSB.print(byte(78));
5 B2 A4 v) z) N7 m return;/ F3 Z9 c8 N0 H" x4 c
}3 x/ X/ Q. Q/ t9 c2 q6 c+ L
//init baby!
9 F1 n3 R1 r+ E7 D2 _ FloatPoint fp;
; @3 e% }- V' l1 r' ~( Q fp.x = 0.0;
% j* k1 c0 d; S- A fp.y = 0.0;
0 v+ U: F5 j8 `& H; y fp.z = 0.0;4 ^3 ~1 S1 l1 [8 p
fp.u = 0.0;5 K" Q' v. n) X# w
. T4 U& f+ a0 i; ^5 N
byte code = 0;
+ x% Q, ~2 H* P4 w5 Q' i: e* `- i7 k( E3 m0 n
//显示在处理的命令4 X+ n$ w- j0 p4 o
#if (LCD_TYPE == 1604 )9 f' `) I$ T7 R4 R- X
// lcd.setCursor(0, 4);- W4 g/ Q& A6 s/ ]* ]8 I7 I& j8 ?: F
// lcd.print(word_old);; x! H5 j- W$ @! R7 t
#endif
6 ^9 u+ C8 }8 H3 }; Y" H SerialUSB.println();. r( c# |- A# y" F$ @& K( {
SerialUSB.print(instruction);% c. l2 _% R. a) y( ^) S( y
SerialUSB.print("\t");( y t9 r/ Z9 O5 N) d
* z% k# U/ H1 | //what line are we at?
5 F% s6 L( }/ @/ I2 S( x' U // long line = -1;" ^4 c' h4 l7 N- v3 p, j; z$ g
// if (has_command('N', instruction, size))! m P0 B- w9 ?1 _
// line = (long)search_string('N', instruction, size);
$ N' ~2 E4 G" J/ M: }9 b
- h: ]6 [& p, }# u /*! r& ?5 y7 k, {5 \3 W
Serial.print("line: ");
4 K0 Z0 J! H# @ M7 r8 X: I9 o1 R) e Serial.println(line);2 l6 [; @9 r: H* v
Serial.println(instruction);
) R1 n" f9 ^% g# v */4 f0 }; X0 k4 u7 J5 V/ k
//判断是否读取了个 G代码?
; y' g+ O$ t, r, t if (
( ?0 K, P, a; ~- m" E7 t1 x) c7 K has_command('G', instruction, size) ||
: T0 `2 y: g: i; }8 I/ ~5 q has_command('X', instruction, size) ||7 g, _ `# ?+ N+ V9 d. E
has_command('Y', instruction, size) ||6 h$ U6 B# J9 ]' S
has_command('Z', instruction, size) ||
7 @! x; j% A7 z has_command('U', instruction, size)) P; |3 V' f; J) F, t
)
% \( c% K: ?& d* ]/ x8 S {5 Y& h/ P0 U) e) S; }, p
//which one?! s) n7 T( Y6 P' c+ U4 Z- P- u5 L
code = (int)search_string('G', instruction, size);# F# {1 L3 z; W/ x) A
// Get co-ordinates if required by the code type given
8 O4 `# V L/ E- N K2 R switch (code)
" b4 z' f L* C8 i; L {
" `3 z2 I7 l8 u9 m6 L; Z9 v case 0:
* F$ ~7 p* K/ Q1 w$ S1 C case 1:
) Z% F& v( e& Q/ l2 E1 I% p$ K1 @ case 2:6 H; w9 j; ]+ E) Z& F
case 3:
+ O4 a/ _4 g& G/ x) g% a if(abs_mode)
i" n/ |' F# R& M" ^5 W { J. W1 k1 O+ ?) E7 W, q+ w
//we do it like this to save time. makes curves better.
/ F1 W- x+ t; O3 k- Z //eg. if only x and y are specified, we dont have to waste time looking up z.
6 f" \& x$ R* I3 l& F9 O2 b" h- j/ w if (has_command('X', instruction, size))
/ y' b1 E. Q# }) E! i* W# I( F fp.x = search_string('X', instruction, size);; N. H6 s8 K' \: O4 z) t" S9 o
else
- x# a0 y2 Y0 u: V0 t) v( \ fp.x = current_units.x;, m& H: @) c; _
. P4 M& c1 I8 I4 } if (has_command('Y', instruction, size))
! S- w M- ~) K) _5 J1 F2 k s* K1 V fp.y = search_string('Y', instruction, size);5 u3 c# [; E! c) }2 G$ j7 b
else
. v, p; O5 H0 j" l fp.y = current_units.y;
; ]) r4 j( Z8 b. I8 T
9 J9 F, k/ H' @1 W; F if (has_command('Z', instruction, size))/ y% k) o" [" m
fp.z = search_string('Z', instruction, size);
' M& } z6 f, \ E9 w, `: k h. Q c else
- _% M! T8 _+ c- s, z$ \ fp.z = current_units.z;" I9 [, o: \/ o7 l
/ Q0 l; J. R2 O; Z; [+ U' B
if (has_command('U', instruction, size))9 }, R* @' \& c
fp.u = search_string('U', instruction, size);# b |. M& Z5 l* T4 d s! A
else
' |0 X6 O1 A3 R) k8 f7 w fp.u = current_units.u;
[9 l( ~* w" f# A' K( C }
* n C( ]( ?* [ else0 T! D6 s5 t1 O5 T$ b
{& f) g9 v* u4 O8 p4 g& c6 Q
fp.x = search_string('X', instruction, size) + current_units.x;; j7 D, Q% u, n
fp.y = search_string('Y', instruction, size) + current_units.y;
) x; V& p" f( l7 l% @1 l fp.z = search_string('Z', instruction, size) + current_units.z;
! }6 q0 |! U# F6 h fp.u = search_string('U', instruction, size) + current_units.u;
6 k7 }7 N2 U' l% h( h, U& T }
4 ^. u7 a! r' @0 S3 { break;' T( K: M0 b; w. y* S& G1 {9 f, V
}! A2 d3 v: Y( C3 G5 L+ r
//do something!# ~; x) X0 [: K$ u0 h- s
switch (code)
% l6 Q# K( G' r2 D* U( H% E {+ M8 i+ O" X q
//Rapid Positioning
3 O& J; ^* A: u! k3 s7 J9 X5 g# c //Linear Interpolation
! H8 v* D/ n: d. v& _( @ //these are basically the same thing.
. ^5 ~# v0 I( E; q% ^/ q: Z+ s0 A case 0:2 g+ _8 P- M& c: l% [' E
case 1:- P- n/ M: E' t6 x
//set our target.8 U+ I% A( T' ~$ {/ Z
set_target(fp.x, fp.y, fp.z, fp.u);
, b( \. c4 ~0 B, \ //set_targeta( fp.a);) o! Q% r+ D, A7 c' i
//do we have a set speed?
' G; w) v, k* x. u; W if (has_command('G', instruction, size))
% b2 j# i. t. I6 j* Q% P {
& X: d# c4 t! T! |9 ~+ U, ?5 o5 ] //adjust if we have a specific feedrate.! d3 M0 Q1 j" J; |
if (code == 1)
3 K9 k1 ~1 x- t* z( F {
. w$ L& [1 k! l- Q2 i1 F* @" W //how fast do we move?. F! @. X% s; m
feedrate = search_string('F', instruction, size);
+ t7 X" \, ]3 O" Z if (feedrate > 0)
* [2 k, g" }8 C: W feedrate_micros = calculate_feedrate_delay(feedrate);
" k! S$ R, L1 o6 Y7 a% A4 } //nope, no feedrate
9 ?8 i, H) t+ R# v' x" r# _+ t else
% Y T, R; K" z7 a) _- I* W feedrate_micros = getMaxSpeed();
% ~" {) D& B6 ~' i" q }
8 `7 B# N4 e. G4 D" i //use our max for normal moves.1 [; v# {" S* e
else
/ b! R8 |* s; [* p' N5 _ feedrate_micros = getMaxSpeed();' c( _8 J1 b3 m0 \% E3 L
}
$ Q. b0 C* @- I5 w8 g. } //nope, just coordinates!
+ U+ W# c9 q8 M! V! K7 {. b) i else; x5 w% E+ Z% F0 R9 Y5 k5 z: ?) P
{3 R* y+ o( v) S+ Y r0 J8 B- u1 [
//do we have a feedrate yet?/ T, T q0 o( N8 W* t, b+ {% Q; y" w
if (feedrate > 0)
. |( S# Z7 ] W; D feedrate_micros = calculate_feedrate_delay(feedrate);2 Y d, n. v6 o* P# C. p/ }$ T
//nope, no feedrate
9 t) ^ l/ E, i7 N$ y else% ?: o( w7 o- n8 {
feedrate_micros = getMaxSpeed();
7 K2 O* t3 n$ |) Y4 i }) B' R3 K: K5 M$ t; ^( h
) {6 u H G3 U$ t, q
//finally move.
5 P- {/ H0 z# h& X# h' }; r dda_move(feedrate_micros);
; {3 w8 F* f& m" P% L if(stop_flag) return;
+ _) @/ y0 C: C. N3 B3 \; M break;, P, i0 B( I9 F& N$ W
- c7 K7 I. |. j, {2 v! ^ //Clockwise arc
6 g0 j$ [5 v/ r: } case 2:
u* }/ L6 d: |+ F! }2 [# l //Counterclockwise arc6 m6 W' V2 j5 d! Z) l" c8 Q
case 3:
, L+ P0 Z8 U9 P FloatPoint cent;$ \, l+ v" i' ]
// Centre coordinates are always relative9 T+ a, s6 Y0 c. a" p
cent.x = search_string('I', instruction, size) + current_units.x;7 l# V; T5 o/ N8 E
cent.y = search_string('J', instruction, size) + current_units.y;" B! v5 w7 b* E# m' {. M
float angleA, angleB, angle, radius, length, aX, aY, bX, bY;( r3 ~# V+ s* }, M* o
3 y9 \6 h! Y! n. u2 r
aX = (current_units.x - cent.x);
: H' ?% a. U, G8 F9 ?+ I aY = (current_units.y - cent.y);, K- i* A8 E. m* m5 K" C% m5 g
bX = (fp.x - cent.x);+ Q1 _, K/ ^7 k( r6 b5 j$ f
bY = (fp.y - cent.y);
/ J o0 E1 J# N
- N% e7 Y5 P+ E5 [2 Q; i if (code == 2) { // Clockwise1 P! x# b3 A& N7 c4 a3 p( v
angleA = atan2(bY, bX);% P# R7 L. J) ^1 P* b
angleB = atan2(aY, aX);5 P! ?3 A% O5 V: L
} ' r1 {' N. e5 Z" d" O
else { // Counterclockwise
* Z8 q$ C$ O. D' K: { angleA = atan2(aY, aX);
* Z& J# B K6 T3 u, `, R: i; K angleB = atan2(bY, bX);
6 g/ R$ R% a0 j1 v }; ^' n& [2 b# q* A2 s7 {
// Make sure angleB is always greater than angleA
( B0 a( q% v3 k B8 f: B. o // and if not add 2PI so that it is (this also takes
4 H" ?( }6 O7 x/ L: _ // care of the special case of angleA == angleB,# Y' b% C! V# K/ Y9 i9 t- O
// ie we want a complete circle)
; W2 N1 h, S( N$ a if (angleB <= angleA) angleB += 2 * M_PI;! J, t- ^, L2 F% X) L: u
angle = angleB - angleA;' H* m5 P- e/ S p7 f2 h0 l
& [/ o& X; Y; |; G% \
radius = sqrt(aX * aX + aY * aY);& o# X U2 h* P1 Z* {. a
length = radius * angle;. S3 |6 n7 ~2 Y4 |# L
int steps, s, step;, I$ R# _. l; g1 M; w- h9 ]4 s0 C& N
steps = (int) ceil(length / curve_section);
- K" C* U1 k y
2 z+ q! @! e# J6 ^7 y2 D' P* ] FloatPoint newPoint;! D% L- u% J) |! Q2 Z$ L# _$ z
for (s = 1; s <= steps; s++) {, l1 h2 G) f9 S
step = (code == 3) ? s : steps - s; // Work backwards for CW
) p( F1 c! O" B+ K8 g newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
?: c& k5 L& x! {% U5 H newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
; T9 _$ }% C; y0 F- J9 W set_target(newPoint.x, newPoint.y, fp.z, fp.u);7 R& D# t& Q/ k$ g7 B
" @8 T# r' ?3 O' b. o/ \8 Y3 w // Need to calculate rate for each section of curve
( a Q, `- X' ~! t# ? if (feedrate > 0). D- s& ^9 M% e) U' i Y0 J8 M/ m
feedrate_micros = calculate_feedrate_delay(feedrate);! y( O! F' Y, G/ c
else5 y" `" G/ r6 o, j( f0 J- t
feedrate_micros = getMaxSpeed(); N, s0 r6 [* `, o6 O0 B4 S' z- r
/ }2 i' _5 P9 e, X* W3 c: f
// Make step
N0 r: W7 N! u: G- W0 E dda_move(feedrate_micros);
/ O/ x2 Q" [$ @: {2 }/ h if(stop_flag) return;8 V/ s# P2 e: I) j# W
}
! Q8 v/ {) N) r7 Z: N- Z4 T$ X( Q; s' _9 Q2 b
break;# g3 X3 d$ F1 |4 O1 y2 d9 X
& D' H% d w$ G2 h/ M% W% \6 p
//Dwell4 f$ \' m1 r+ z v) o! W
case 4:; V6 w2 [4 ~. J; Z
delay((int)search_string('P', instruction, size));
+ K. V* e( e; ?0 F" G6 F4 S& ~ break;
4 {* o2 A- u! v4 X. R% F
3 T7 ]2 i! j+ p0 E //Inches for Units
1 ^; f* G8 q+ g0 i4 \) m case 20:
# e; h5 y* m4 H x_units = X_STEPS_PER_INCH;5 ?; `: i0 D* ~
y_units = Y_STEPS_PER_INCH;7 v& v' l; X' z7 ~
z_units = Z_STEPS_PER_INCH;
2 g3 ^- L% K. _6 y u_units = U_STEPS_PER_INCH;
8 V8 ^* x+ G4 |" T4 {% ] curve_section = CURVE_SECTION_INCHES;
* F& r8 C* M8 b6 P calculate_deltas();
" c+ R6 ]" U) w- x; l break;! i0 `: F8 ]. b) C, @3 A
4 f7 c5 O% J3 N7 v; i
//mm for Units, R6 o# L* t6 ?
case 21:6 s$ x7 F z7 l
x_units = X_STEPS_PER_MM;
" W5 ~, J* B( W4 g X+ g3 M y_units = Y_STEPS_PER_MM;
7 ] s8 ^: W% q1 {- s4 A. F$ G/ L z_units = Z_STEPS_PER_MM;
4 B- Q# j3 h7 n u_units = U_STEPS_PER_MM;8 S9 j! |) ~* w2 [- }) u. ] w
curve_section = CURVE_SECTION_MM;5 N" C9 B" Q0 K9 r+ H* }. @% ~
calculate_deltas();. y4 U1 }9 T! ^, e5 ] D
break;* _6 E" f3 h" U2 o% r& Z5 |9 h
- J9 Z" n, _$ r7 N
//go home.
! e1 F5 o: P8 u+ b$ p* G case 28:/ l" c( L' @: Q+ r8 @
set_target(0.0, 0.0, 0.0, 0.0);5 O! A0 o( A4 J: O# ^
dda_move(getMaxSpeed());
" O% Q+ L* G1 g5 y2 {6 G2 g if(stop_flag) return;
, Y! t0 D$ c* u {1 y% A break;: D: l+ T q9 \8 g. i+ o a
$ m0 `. {' u4 s //go home via an intermediate point.+ s" U- F6 I3 L. P4 @1 X
case 30:
+ h6 d4 D+ X, {( z9 L6 P2 J fp.x = search_string('X', instruction, size);& f. f! `: q1 t0 `# k
fp.y = search_string('Y', instruction, size);
. S. A8 t/ ?% o fp.z = search_string('Z', instruction, size);4 G: L1 l8 x9 Q, Z1 d
fp.u = search_string('U', instruction, size);( e0 A# j$ a- [( t+ _
//set our target.
$ {2 J2 J/ u+ h/ `4 f1 t3 z if(abs_mode)8 M/ b1 Y- m) p! |
{8 z5 h$ U) d: [ N6 t6 H
if (!has_command('X', instruction, size))/ \- N% u$ }9 F* n5 R3 C
fp.x = current_units.x;' t& ]3 }8 X) O0 }$ Q2 q
if (!has_command('Y', instruction, size))% F' t7 e) ]" Y2 V: q/ n& _
fp.y = current_units.y;
3 M. X3 y4 Q5 o( V8 N if (!has_command('Z', instruction, size))& T" G- o4 q' I
fp.z = current_units.z;9 W8 \" k& j( S1 h, Y. g
if (!has_command('U', instruction, size))6 h& ]0 t# h! I2 p! ^, y# ~* W
fp.u = current_units.u;: b1 ?% q2 Q4 I( S: I" x7 F/ p' ~, h4 T( S5 U
set_target(fp.x, fp.y, fp.z, fp.u);0 N- Y o6 ]6 l$ }6 G
9 f9 l! X, [0 n6 s( P }8 W& n Z: ? i- {5 K) f
else
0 r, E: F2 p; d5 u; E; B set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );- y; M, R9 a' I% ], q6 G
; e9 B! K: K* e+ V' x& K
//go there.' @' j0 @- Z# E% J' @ w% C
dda_move(getMaxSpeed());
! b% n3 L. m, s) e$ f if(stop_flag) return;# C( s% D- O( @, k9 q# f
% j. o# X! [' H9 H; W //go home.
3 L; v6 n& Y; o+ {# X2 Y$ { set_target(0.0, 0.0, 0.0, 0.0 );0 x' S _6 a- b$ g/ Y: y( {3 F& o
& H" V8 w" {. e, T! l5 Y dda_move(getMaxSpeed());6 l$ s7 o! y4 J n7 b. m' q
if(stop_flag) return;
) Q* c! J3 M+ C; u% ^9 u1 H5 T break;
2 O) m& h# M6 f2 U- Y v5 f( J2 F3 `" Y+ t, }/ K
//Absolute Positioning
' `" U& n( |5 c1 _: y case 90:
/ {/ w( q$ Q9 a8 I$ h2 x abs_mode = true;
9 i p0 X# f* p: G1 P+ b5 e, p break;
, o' r/ u7 Y* f" t; A0 M! ?% W( H, Z0 L
//Incremental Positioning
. x. k( _5 v" N case 91:
8 m) m7 d3 v' [8 X4 j/ L abs_mode = false;
5 F4 P! v9 t( ?2 V$ {; ~ break;% O7 o7 ]4 K+ D$ z) W4 L+ n
. b: m' p1 s( {" c: a7 ] //Set as home* P; Z/ A: Q+ O; Y0 H8 a3 D- a& b
case 92:
, `. R8 N; |% T4 Y
- h& m0 x4 o! Q set_position(0.0, 0.0, 0.0, 0.0 );& i6 ~! ?* ]7 Z: a/ F
* y% E- `. r T7 r& B( T break;
( n: n$ R6 B( c& @8 S1 k
) B* i& X s9 y* I& {6 C$ J- ? /*
7 `6 I' k4 j7 n( {7 u! N$ M G //Inverse Time Feed Mode/ B3 u8 V8 }9 \- k' Y; d
case 93:6 X# j1 U8 k& N3 o: q
' K! b G0 r9 K* S) ]5 \4 `
break; //TODO: add this
3 n4 Q* Z) r2 B4 @; o5 d/ W# Y% J {
7 W8 T2 B: S* p //Feed per Minute Mode
' K! X# n- B3 m/ ? case 94:
7 u3 x# A2 P1 k2 ]
2 G! y% A! M, b0 q9 o% C break; //TODO: add this2 a' ]4 v/ \3 J& C- \
*/% I1 t1 j" A4 }. _: E% d O
, W! F1 \! ]( v3 l( [# y" a
default:
; Y/ j9 \) _0 z# D SerialUSB.print("huh? G");7 G8 }6 M. p; q7 K& T7 w; K4 @3 _) R
SerialUSB.println(code,DEC);* n$ L: `3 f, Z9 z& V Y
}5 J: }7 q/ N! }2 C6 f
}4 l9 X7 @( e/ x# t
4 Y- f2 `5 `% d& L //find us an m code.
7 [% Y4 {+ f. u1 `" J& |8 r& G8 ? if (has_command('M', instruction, size))
$ F/ [6 @2 b. n: E {
$ s8 v6 X/ T7 X/ G4 b3 w code = search_string('M', instruction, size);
# q" k7 B3 `5 ` switch (code): [, I- T; ?" z5 t7 s2 ~* p! x3 O) [
{9 D& R. ]8 |4 b" m- M
//TODO: this is a bug because search_string returns 0. gotta fix that.. E7 A8 j% U, f3 i; {- |
case 0:# i. `, H3 \1 H2 s; ~
true;
9 w) r3 A! C( x v% u& ^# D break;: y( x- Z# ?; }0 t
( Y! [7 k2 x) J s# f2 j1 A: B
case 100:
% o: ~) K5 p5 i# Y" |! \* { break;7 @2 }4 _( F7 N* B1 \
& Z" p0 a6 m# t
// fire camera relay- [8 L9 A% O; h' a' G/ _
case 101:
, Z ? ?" a) {4 H; \+ F camera_shutter1();5 W! @3 l* ~# R% e
break;2 l! ~/ m& s, |; R0 n
& S) d' I: G& F
// fire camera relay2
; @# i3 P4 Z/ e- s. X9 Q case 102:
$ r' P' K7 h4 y, h- E3 {2 A camera_shutter2();
; r# }: k& `5 N/ z9 F+ ]- } break;
" \4 m! Q- t7 G/ k/*
+ X7 H; \3 o/ Z3 C+ {# O" O // turn aux 1 relay on
# x: W" }+ q4 H" F7 ?! q# L3 i case 103:$ G7 K( g8 T! W0 S9 \* _, K7 n1 u% J$ @
camera_aux1_on();) G" q# A" H1 R( Y S D1 D
break;% D9 d! B! ]1 v) j$ u
8 J) ]3 V' _9 \+ W7 I // turn aux 1 relay off
, f# Q+ B' y5 l; Y3 r2 Q* | case 104:
1 ^; v6 o* ]/ N9 ~ camera_aux1_off();
% `2 c" Z' N6 w0 U1 n* ^" e break;" N3 j6 I7 Q$ u3 ]3 \- C0 i5 z6 f
! x$ a9 G3 h1 L! r
// turn aux 2 relay on
6 U) k4 u# `( D9 H% t7 _* N. u case 105:4 f* X) O2 p# ^4 }2 Z
camera_aux2_on();
: `$ c1 P) i/ C' l s3 u7 j1 U2 C' N break;
/ d4 }' P+ ]5 Q' J3 M. |, |' t0 b( Z
// turn aux 2 relay off
& I+ ^* z' F/ W/ _* f4 G- r case 106:
# {& ]9 F3 ~4 E/ F+ w" W camera_aux2_off();
4 a3 B, a# [! f, V break;' }+ `9 d) Q3 q B) g! q
! x$ J' n2 v& F3 n: N! x
// turn aux 3 relay on. P6 [2 ?+ Y6 F. {# u1 _6 Y
case 107:
0 x7 V D7 ^! @2 p camera_aux3_on();
/ K2 g' M+ x, K. k* [ break;
2 N! J% S) ?6 L' w
+ e" t* k2 A: T" X* C" D // turn aux 3 relay off
$ W" H7 X3 H" @# x* j case 108:
: P1 {2 O; B" U0 p camera_aux3_off();$ w% T6 S' m4 `
break; O5 E! R( C- \# R1 T/ X
" ~1 h1 \5 u7 @! j" c) b // turn aux 4 relay on
* Z7 u% U. W1 k4 C# T case 109:
+ J3 L! l6 t4 A. v. I0 e camera_aux4_on();4 f0 g8 @" }- r$ x' i+ _
break;4 P1 D O4 D; X' e! [2 n
; H$ _; }1 i8 L$ d/ o. L1 O // turn aux 4 relay off$ ~1 V% f% H' s7 J
case 110:8 D2 G5 ]' d9 N8 @5 R$ T1 s
camera_aux4_off();
; ]0 t$ n5 t* o8 _5 z3 B4 W! k break;
8 P8 {2 G, J0 u5 F9 K6 w( ~*/! r% R x3 S) I) ~( T
default:) Y# A' H; i$ d# W" K, [
+ _$ C, @# q* M SerialUSB.print("Huh? M");
9 b+ H5 z1 C- V SerialUSB.println(code);' i {# ?0 ?8 Z4 ^' f
}
" Q# E4 j$ c. S. j8 f- E }0 f8 Z I; \7 U4 _7 n2 u& J" F
$ a6 P+ a* R' g( c$ d% Z //tell our host we're done.
3 R7 P' a% E& T9 t+ \. B% P SerialUSB.print(byte(78));
) E1 m$ R5 h; u' @
4 o# K. \! E& U1 E# I2 C. r: l}
# D6 \# M& q3 [' [2 i* o9 }/ A$ Y6 w+ \: N
//look for the number that appears after the char key and return it/ l o9 M$ ~. h9 p+ v
double search_string(char key, char instruction[], int string_size)! _+ B" T/ q3 T0 i* k/ Y
{% c2 }$ D! W& a+ n. u: p& f' C: m
char temp[10] = " ";9 x8 a4 N8 }* I3 T7 F2 C1 x
for (byte i=0; i<string_size; i++)- A1 s: l# l# A' e- U+ Y
{
9 ?2 a$ B# P, U* h# @* j: `5 z if (instruction[i] == key)
/ ]7 d9 b' |& ^5 O {1 o8 q" V; H( u# o1 m4 b# K% P3 O5 f
i++; 1 O* n+ h S& t) Z/ }- [
int k = 0;0 T H' ~* F7 U/ y9 r
while (i < string_size && k < 10)
. v0 a: U: L) k e- G* k. x {
" r0 Y0 [) ~! |$ _' C if (instruction[i] == 0 || instruction[i] == ' ')
' e4 Z/ L# k- ^% h) v2 J( ` break;
9 F) L: E# O. H; f }3 ?# C) Y- N# N w; t
temp[k] = instruction[i];0 \, ~$ b8 {9 I; i
i++;, d2 G5 I6 Z9 h1 a
k++;( n p3 W; F8 T7 P. @% r
}
% J. S0 G! J& d# X/ i2 a return strtod(temp, NULL);5 Z" B y" h9 P3 F1 \! P( h, W1 g
}+ s) Y) c3 Z4 d E; O
}2 d- ~" _0 x' t" D
1 K8 \3 {7 h, \4 ? b return 0;& @" X/ E, S3 Q
}' @$ X! B( n- I' A( h/ z' K% x( g
6 Y J2 v( s! K3 b//look for the command if it exists.
1 Y$ ~" K' |0 E6 ?bool has_command(char key, char instruction[], int string_size)' }5 q6 a7 A& _
{( N9 N! u; G: y% ~6 ~! ?' g
for (byte i=0; i<string_size; i++). l& m3 G/ M( I+ C0 d V
{
6 t5 C7 b/ H9 R if (instruction[i] == key){
: e: a* N1 b4 `$ V# d: P$ }& I1 ^% X8 X
return true;2 w! X9 i7 Y/ a1 M# j
}4 m2 f9 x; X! P, E
}
% R, x9 i8 a5 D, w" y' C7 c- ~; ?5 ?( f% q' i2 T
return false;) [* k" P: { y' P+ t. p0 l
}. T" E! a8 C; T. ^5 T8 C
2 O. ~. [6 M* R# J" H
5 W' ~ z6 B+ s _4 F: G
# ]6 g6 S1 d# Z4 y% | |
|