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