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