|
7#

樓主 |
發表于 2014-5-10 09:55:02
|
只看該作者
// our point structure to make things nice.
7 t9 p, |+ X, d$ A( Tstruct LongPoint {/ u# w! l& ], F. r+ Y \' X
long x;$ w. ~/ X! L/ `$ {1 `0 g
long y;- W6 X; e6 z; r2 I* {" N
long z;, ~7 z; t; O! |! `+ m$ H
long u;
+ `2 ?8 E/ I! C @};
& C: w- t/ f+ g% a9 C4 |. j# a) B" ` i- h
struct FloatPoint {
( f- Z. P* `% ` float x;
# l1 U w! g9 x% V% x8 w. | float y;3 E# U. p) E* h! c2 N$ Q; X
float z;7 h, q/ s9 H4 s; n% V
float u;
( T, s2 t! W6 X- H6 [};+ }9 ]% X# v' C( V2 }1 B- c9 M
6 c" J1 u9 T/ M IFloatPoint current_units;5 X5 d2 `& J1 |# q# F+ j/ Z
FloatPoint target_units;) F# Q# u$ X _
FloatPoint delta_units;% o* I- C1 S5 H. Y1 f0 p
1 Z8 I% `1 ?3 \! P0 PFloatPoint current_steps;
* R, Q0 F$ O% ]$ z$ K# UFloatPoint target_steps;
& w8 p# a% S! GFloatPoint delta_steps;
7 b" h5 \, T* s/ g v! g: s4 ~; f3 g$ @0 [& N& J
boolean abs_mode = false; //0 = 增量位置模式; 1 = 絕對位置模式. l% A* L' X* |4 k5 n& K+ W/ K
8 J% `" A. ^1 U' Q2 e; Q
//default to inches for units
/ n1 t5 p+ I, s. h5 o) q, |! u8 N; Ffloat x_units = X_STEPS_PER_INCH;
& S5 [; B- y# B0 T) ^0 W- U- Sfloat y_units = Y_STEPS_PER_INCH;# Y+ {& H i' x# R* \% w
float z_units = Z_STEPS_PER_INCH;
4 T! [/ t3 ^& n; F! C+ Z8 L) d# o) Afloat u_units = U_STEPS_PER_INCH;
& a! r% L2 I1 k2 l7 w% `. efloat curve_section = CURVE_SECTION_INCHES;; w Y$ O$ P0 l( Q& h) }/ f m
7 x+ G& ]3 D7 I8 @' h
//our direction vars: k- p# _" P" g& `
byte x_direction = 1;4 L, z6 b3 W& L0 h( C2 k- t I
byte y_direction = 1;# g9 y$ A2 K* ^" `5 V/ j9 d
byte z_direction = 1;4 f: p" a9 o4 k+ a
byte u_direction = 1;" y/ K) j3 p7 R4 l' J' I
+ r0 T* p0 z+ {/ \# i; J* A$ P
//初始化字符串處理1 w4 k' m+ ]3 c# s b( Y
void init_process_string()
9 U7 { Y, c' \8 f% c: h4 m5 J8 w2 }{
9 d6 w' G+ r9 d( s5 O* C7 [ //init our command
1 W/ ]3 a4 h( S; U5 w for (byte i=0; i<COMMAND_SIZE; i++)2 B; X5 t$ d( K& P4 K8 H r
word_old[i] = 0;1 U0 R' b" C( p! I* v
serial_count = 0;8 ?2 m$ @; O! [( b
} q# U( s* P4 q! D7 Y
* @0 u% M6 T0 _3 U8 O* x//our feedrate variables.# T/ ^+ `1 m: [( i/ B" F J
float feedrate = 0.0;; \3 q7 A5 `3 `; T9 l( ?9 l
long feedrate_micros = 0;. l& ] P* Q* Z# ^! W( Y
; w4 _% P: Q4 W3 X, H1 \7 C
//讀取并執行命令
, q+ Z7 M( S9 r( _0 C* A! Lvoid process_string(char instruction[], int size), g' B9 F1 K: A4 l
{
; I: \! I+ C+ g //the character / means delete block... used for comments and stuff.3 Q9 N6 Y0 E+ w3 P7 H& {' [
if (instruction[0] == '/')
+ f! U& `/ [4 o1 Y6 O3 x {* ]2 t. \9 F5 i) E7 L) O' o+ S- `
// SerialUSB.print("ok");
( n0 d% c3 I W9 d. p // SerialUSB.print(byte(78));
) [. Y1 T; [6 R5 I( ?* Z6 _ return;3 Z2 K8 r1 G5 Q0 Z( o! x
}
+ ^ S4 ~6 s: @$ T b" Z6 k //init baby!
0 i) A( e- P' G5 c# Y! k FloatPoint fp;0 D- d1 c) d$ Z7 W3 U3 |
fp.x = 0.0;/ l s7 p; m6 x1 k& L
fp.y = 0.0;
" E3 U$ F) J2 |& W fp.z = 0.0;
8 L4 _+ L, M& d- X fp.u = 0.0;
B7 k2 \4 P0 M% l, p" m' C/ _- Y- X+ \
byte code = 0;% O, R* e- Y. u/ v4 Z6 ~
8 R1 g3 o" b C; D2 g
//顯示在處理的命令
* S+ R- B+ N4 L) Y. e#if (LCD_TYPE == 1604 )
& Q& Q4 Y; ], J // lcd.setCursor(0, 4);6 E! I e% F, H2 x$ w
// lcd.print(word_old);4 t! y. J+ N1 \1 o
#endif
8 y4 v: e9 e0 w3 S* i( v SerialUSB.println();
+ `2 S7 c/ B+ l$ y' A SerialUSB.print(instruction);
/ K. W) d2 ~7 K SerialUSB.print("\t");
/ z0 o/ J/ w, x6 P% x4 u4 A$ \
3 ^3 q, M/ ~ }+ k/ ] //what line are we at?! g2 h, A' f) U5 o6 L1 y
// long line = -1;
+ y1 ~0 ^, Q5 o" p4 z7 L // if (has_command('N', instruction, size))# G5 ^! }! h) J- ^2 m" @
// line = (long)search_string('N', instruction, size);
$ U+ r# Q9 o- E; w1 F% a( {! t) g7 R; y
/*6 L: e( I, H0 |3 } ]) C2 U2 U% B. V
Serial.print("line: ");, f6 [% C6 `. j% b. r1 C
Serial.println(line);
! h6 q: I; |3 J0 m0 ?, Z ]+ W Serial.println(instruction);
. o# _( W' D% g. u6 K3 @ */
& z0 ^7 m' p( T0 l2 o8 ^ //判斷是否讀取了個 G代碼?# e- p4 Q4 `/ Z% G1 X* \6 x' D
if ( _1 ?$ n% N, {5 [) o' u- Q' T
has_command('G', instruction, size) ||
! ~5 [- g+ H% M1 T+ @2 y, i0 | has_command('X', instruction, size) ||3 K; i) X2 c, [4 F, }0 Y2 Q7 R
has_command('Y', instruction, size) ||
# [+ r- Z- d* i2 B7 ^8 P, g% N has_command('Z', instruction, size) ||
/ t2 [+ B6 X/ E# h; v has_command('U', instruction, size)
5 O2 @+ a3 [7 P )- Q' e5 a, ~* ]7 D4 g: q& O
{% z4 j, M7 M; W3 R
//which one?- |* U8 E7 r/ E$ @0 Y( \7 a/ q6 b s
code = (int)search_string('G', instruction, size);
& q. v" A X/ R& k) z7 d // Get co-ordinates if required by the code type given8 k( s( [5 `0 v% w# `- C9 r" }
switch (code)3 V$ B/ g& C, }/ h1 V
{/ O7 {5 y/ d+ r5 ] H K
case 0:/ @8 v S# u1 M4 t" r+ Q
case 1:& d0 @, G$ }: O {& N* S6 t8 b% b' X
case 2:" X' O9 s" A _6 u0 Q( h$ L
case 3:9 Y4 J7 r0 y6 T" F2 i2 _4 P- G9 x a
if(abs_mode). n; F2 N$ j/ K2 c! L+ y
{
- ~5 r# E% {* a //we do it like this to save time. makes curves better.
; o! `2 J' T: n, V4 T- c9 ? //eg. if only x and y are specified, we dont have to waste time looking up z.
, L* Y4 _. {* K+ Z if (has_command('X', instruction, size))
' S8 d$ _5 c* `" i- B" w6 u fp.x = search_string('X', instruction, size);
0 @% v- K: o/ C/ n5 n else
1 l1 ] J) b4 {$ C) u fp.x = current_units.x;0 z8 h3 J4 }: G4 c* [5 `. w
2 G7 |( S) N k/ y: j; a if (has_command('Y', instruction, size))) g( ^' S0 U- n5 V' a' V
fp.y = search_string('Y', instruction, size);
* A, S( o0 q0 r! S7 a else
7 r5 H. A- u' n1 D fp.y = current_units.y;
, k; P! v2 Z2 E6 o& q8 ^. |5 o, k- B$ I5 l9 \
if (has_command('Z', instruction, size))9 w; `6 V7 J, t+ w
fp.z = search_string('Z', instruction, size);
, N4 R9 F! T5 s else) a+ G+ \8 c" X
fp.z = current_units.z;
" e. ?' x1 b9 R7 w1 s( c % F8 ^8 z. c t7 [4 |7 A
if (has_command('U', instruction, size))
5 C1 @7 b% [- u) z1 O; k1 w fp.u = search_string('U', instruction, size);
( @1 E$ K; k$ K. |" f else& b/ r7 U" b$ p3 m3 b2 c
fp.u = current_units.u;
4 L# k" \' U: ~2 H }) g% m* t9 g& D W
else
( P7 R+ \9 s+ o9 d/ A \, m {
% Q b. y! l( q4 r0 n! A fp.x = search_string('X', instruction, size) + current_units.x;9 [; n* N' \$ x4 j* B% l) A
fp.y = search_string('Y', instruction, size) + current_units.y;! G* w. F$ R' M7 q" E
fp.z = search_string('Z', instruction, size) + current_units.z;
: j) d! z7 O, N" q# L fp.u = search_string('U', instruction, size) + current_units.u;
/ w; P1 m9 o7 B* ^" V; G }
3 A) y; {2 J8 M break;2 ]; c# x; E' n3 n% v6 C+ a9 F
}
5 A! l. z; I" o //do something!( e; w9 \4 U4 R; s2 w
switch (code)3 q4 z/ {. m+ |; c: I! F
{
2 _5 N+ c0 a5 Q5 W/ J1 w3 S //Rapid Positioning
- K: G, D9 m- i0 p* n //Linear Interpolation' E% p& p; x8 `( v2 I& `
//these are basically the same thing.! S4 _2 {7 y; I6 Z8 v3 y/ I. n
case 0:
5 W: r! h% C4 t' W$ @: E4 n& A" _! d4 Y case 1:
: K, @4 y/ m! o8 S: \# q //set our target.
2 g" C8 _+ F J0 w5 P* _- z set_target(fp.x, fp.y, fp.z, fp.u);5 x1 e! b; R2 I8 |6 E3 [3 A
//set_targeta( fp.a);
4 x! V m( E5 A7 C5 Q5 c- B //do we have a set speed?
, A9 Y* I* ~. T0 H g' R: m: ~9 T if (has_command('G', instruction, size))3 L+ F0 O- y3 Y( f' [8 s
{
+ I$ B9 ~9 P! Z) w' M( y/ p$ g //adjust if we have a specific feedrate.9 p! y3 N' L5 _# u. A
if (code == 1)
. W+ d+ q3 z5 S6 d. S" q9 `/ H {' l8 q- l: x6 R- e5 _
//how fast do we move?
8 W; J$ U i' n ?( p2 O. v; M feedrate = search_string('F', instruction, size);
* B! W# X! D y, ]& h if (feedrate > 0)
; e; h3 U- a& c# Q8 x' j7 q- | feedrate_micros = calculate_feedrate_delay(feedrate);, M4 [. `" {- h: G
//nope, no feedrate
# p9 F" `. x6 h3 K4 L4 K8 u& j( o else/ Q( c9 A) w" ]+ Y* W
feedrate_micros = getMaxSpeed();6 L/ D7 k4 @+ y: z
}; M3 F2 V7 @2 t" u) x N
//use our max for normal moves.3 E* A/ K. y' a) P$ o
else
& ~2 A6 e% h" y+ O, M& a# g feedrate_micros = getMaxSpeed();
9 _& x6 k3 q% O$ n% O K' z3 ` }9 f( _$ }5 x5 Y |: c, y7 k0 k
//nope, just coordinates!6 e. g' y5 l+ _8 L
else; v6 `* m5 c [" Y/ c0 C
{
^7 n( s7 M$ W( T4 v //do we have a feedrate yet?! o* x5 \" \. C, C _$ o% M8 q3 p
if (feedrate > 0)0 p* I9 i7 B9 F8 ^
feedrate_micros = calculate_feedrate_delay(feedrate);. s8 }) V8 b& E. Z
//nope, no feedrate$ Y& U2 n/ J: b' K
else
" N; c3 R" i5 l: t1 f/ Z* ~ feedrate_micros = getMaxSpeed();
$ f$ C; ~+ {. f }7 z. H e9 \4 F2 D
, G( m4 U7 i, F0 `% A' X2 q
//finally move.9 R* S6 ?# Y0 `2 S# j, _: b
dda_move(feedrate_micros);) k7 q7 D2 |7 J+ U0 ~. G1 [
if(stop_flag) return;6 B4 b5 t4 k: x. J) w# u8 V! F
break;
; f) t5 X- {6 A* D8 O6 R
1 I6 s! c7 v2 j' v- Z. u4 O A" S //Clockwise arc
4 O9 M9 e' y) e2 V! t7 C case 2:
4 a5 Y; ]- k0 w3 b- F4 W //Counterclockwise arc1 t/ l3 `$ _% T
case 3:
+ q) t7 O* A+ h4 u' g8 d FloatPoint cent;: N, S( ?0 m7 x0 E3 a8 s" m
// Centre coordinates are always relative
$ W, Q8 o" i7 y8 @) L1 W% s cent.x = search_string('I', instruction, size) + current_units.x;6 s4 D- Q5 K0 y5 \# }2 n2 m
cent.y = search_string('J', instruction, size) + current_units.y;- i3 P; S( ~9 c% d
float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
, D7 A- ]& p/ N% _ y! y0 e- T. N) m& e" G! n3 D
aX = (current_units.x - cent.x);2 f8 p% }2 B* T4 B& M9 l- s
aY = (current_units.y - cent.y);& h0 Q3 Q. N, v& ]+ g( R
bX = (fp.x - cent.x);
! h* w% t# r/ E% x& r bY = (fp.y - cent.y);
/ F D% X% c2 P& d; }4 j
$ y8 X) v8 e% J6 W9 z9 r c if (code == 2) { // Clockwise1 r# I( V# ^5 I' a
angleA = atan2(bY, bX);
5 h, K% l3 V! K, r0 C+ a+ d angleB = atan2(aY, aX);8 Z7 X# J" q# v: J
}
3 }8 O9 x1 Q3 H1 o! s/ j3 }! R else { // Counterclockwise! J2 l- X+ D; l* W8 m
angleA = atan2(aY, aX);
6 G5 M$ p( v& n0 m angleB = atan2(bY, bX);
$ e$ W6 @" m5 A3 D3 V: z }& r& \5 q2 w/ U. F- z, C9 m+ {
// Make sure angleB is always greater than angleA
- V* \1 v7 u& r) B // and if not add 2PI so that it is (this also takes
; H6 x* H6 H) C9 f // care of the special case of angleA == angleB,, n2 h4 f" ^( T5 a" p
// ie we want a complete circle)
/ c: z c" s- [! q9 a8 n* X' N" P if (angleB <= angleA) angleB += 2 * M_PI;* Y; \) d! O* A6 i' ^0 w! n
angle = angleB - angleA;0 s& R) [. a+ W g/ k
9 ^, d3 J7 j' K e, d8 T3 {: w6 x radius = sqrt(aX * aX + aY * aY);
. k6 ?5 s: R3 m$ @7 c& l4 Z length = radius * angle;7 W1 k( @$ E! D' g( y+ H
int steps, s, step;
9 d1 i( m+ j6 r3 v% D. A steps = (int) ceil(length / curve_section);
% g& s% y& H8 d( o# m5 |# Y) M0 U' }
4 a" a0 R. ~% ]' f* e/ [ FloatPoint newPoint;
$ m, n- M, F( c# Z0 y Z for (s = 1; s <= steps; s++) {7 \* O: `; i- k
step = (code == 3) ? s : steps - s; // Work backwards for CW- V1 r5 J( X: e! A
newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));$ M# n$ _/ b/ F+ J# T7 m& |% h, N
newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps)); ]5 r; S7 I9 C8 Y+ q. h# y
set_target(newPoint.x, newPoint.y, fp.z, fp.u);
) U9 Q( ?( O* r8 Z1 G0 J9 Z
' w. S" o* y; o: s // Need to calculate rate for each section of curve9 h5 M9 Z2 a) G- Y
if (feedrate > 0)
- \8 |% S3 n1 n& j* r3 J [ feedrate_micros = calculate_feedrate_delay(feedrate);
; {. u) c2 d# _: U else
) o# H" d4 |. n" L5 l- x# ?7 q feedrate_micros = getMaxSpeed();
# T) v0 |& y8 `7 b" b1 t- ?9 |4 j( s; R7 c+ S" Z
// Make step* `7 H6 W+ N3 X( j3 |& i' W' a
dda_move(feedrate_micros);+ F, k! {. I! g% K- ]2 _4 M
if(stop_flag) return;
) I$ L$ X" L- N }
- n- Q% f0 s @# T6 g1 G5 A9 V# n" {5 k# F2 D* K
break;
9 w) y1 |$ p% g" c- M/ x5 D) S" V8 D; V& l5 U k
//Dwell
5 ~9 o$ c& X' g. V" O case 4:
; o! B3 D; u" Q2 z# h. Q delay((int)search_string('P', instruction, size));/ [- v' h- _% l/ H! t0 ]
break;% K" y9 O2 G0 K( S. K8 @) d' v
" ~: r5 E d: }4 @+ C9 z- x
//Inches for Units0 ~' g) c1 n- A& y4 ^
case 20:+ f9 u+ r9 ~1 v6 x+ F# ?. F+ Q
x_units = X_STEPS_PER_INCH;9 a9 J, ]7 o2 U. o+ L" d
y_units = Y_STEPS_PER_INCH;1 _/ }' z3 r/ T" `% `9 }" T
z_units = Z_STEPS_PER_INCH;
E* Z6 q* N$ K9 d, O u_units = U_STEPS_PER_INCH;2 i' R7 E. \1 S4 x% {! F
curve_section = CURVE_SECTION_INCHES;- m* R$ d5 v/ _5 ?+ ~0 |9 {0 p
calculate_deltas();3 @) G% W' z6 j1 p
break;
, Q) g+ F0 |; e1 ~; m+ I4 u; L/ U) ]- [( K: |3 C4 i
//mm for Units
. @( J1 c( ?! B" V% @; T2 O2 k; ~ case 21:
& i/ u- o% w( t x_units = X_STEPS_PER_MM;
3 V5 Z+ h6 p/ r, G7 ]+ E- n y_units = Y_STEPS_PER_MM;7 c1 U) y! `( `% ^
z_units = Z_STEPS_PER_MM;1 G' L% F9 T6 l2 Q
u_units = U_STEPS_PER_MM;
4 ]( B) ~3 }0 {3 _ curve_section = CURVE_SECTION_MM;
- a, r( A* W" _$ x" O calculate_deltas();
; h$ ~5 o5 b) w1 y( E: p: N" h break;
2 l/ y2 ]" `9 j. \1 \
! a2 h' T+ B& b1 K% t' q* s( F //go home.2 |: e* G$ p0 \4 ?$ O4 s7 C
case 28:
! w& L# X& {/ l set_target(0.0, 0.0, 0.0, 0.0);
. v& O$ X) ^6 n' F. A3 @$ f% N dda_move(getMaxSpeed());$ \9 o8 H) g- j( K: g
if(stop_flag) return;$ ]1 o* U4 h$ [( y
break;
! U4 B7 A* N* Y6 {8 k2 L9 L7 N4 b3 Q$ _) `/ n# e
//go home via an intermediate point.
- u8 P4 i- \* \$ ~& x( v1 R case 30:3 ?( n) U/ x6 e) p, \- \
fp.x = search_string('X', instruction, size);9 y- f3 i9 I$ B) ]/ d) i
fp.y = search_string('Y', instruction, size);( _7 o6 {7 Q" A# I8 q# B
fp.z = search_string('Z', instruction, size);
% U1 `1 { r& c9 B fp.u = search_string('U', instruction, size);
" Y) X9 D5 f5 p4 S [3 y. u4 B //set our target.
' J- @" W+ x* i8 e: f if(abs_mode)+ i% ]; D5 v- Q
{
' k' v: U" O- X! k% k: z if (!has_command('X', instruction, size))& e1 h$ @; o6 d, s l$ `
fp.x = current_units.x;
- M9 M+ S+ p; B2 j6 f/ D# S if (!has_command('Y', instruction, size))6 e+ b0 S. R4 j* W( z9 P: l+ T
fp.y = current_units.y;, [+ Y) g# Q1 S% `6 O( e$ K7 C
if (!has_command('Z', instruction, size))
7 H& ?/ i6 v9 s- W" d fp.z = current_units.z;# f" K, `6 |' S; k H! c& G' o! C
if (!has_command('U', instruction, size))& K& Z' |' t6 Y# J. [" H0 b
fp.u = current_units.u;5 d2 p5 S0 w D+ t7 R4 W; f
set_target(fp.x, fp.y, fp.z, fp.u);' o' Z5 B/ p+ r+ X* _ m
$ c" _5 p7 V0 N5 D' Z: ?0 z ^) \
}
3 t5 f9 E+ `3 v) D/ s: N! x else
0 S4 u1 D! r8 G& O# E6 k set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
& a) Q& {& r; ?' L7 h " h5 k+ f; R1 h, f' X! j+ x0 c/ \
//go there.) R$ Q9 b* b7 Q8 k+ A. X- Q
dda_move(getMaxSpeed());
% @+ E7 t8 W2 F' q if(stop_flag) return;
6 ~) ]6 b3 j7 w/ y% ^0 q; l# v4 T' Q T" b
//go home.# x- w; \- [( l5 U. S
set_target(0.0, 0.0, 0.0, 0.0 );- k9 W9 j) A9 ?# O- {7 H' W
. g8 l$ b! }3 a- R: O
dda_move(getMaxSpeed());1 ?* n+ Z. {8 o9 T/ {2 A; E6 s
if(stop_flag) return;- }, @/ y1 {# M" ?; ~ U- Y0 ^
break;
- G. b% j" v4 }2 M' X! T( S
( g7 ~" N8 l2 `7 B //Absolute Positioning9 E, ^" W; B/ D2 t& {# P0 p" t
case 90:
" l' \$ s @9 V6 |: B, T1 P- { abs_mode = true;+ O8 L4 O# t8 i5 [6 J
break;4 N" o" z4 L, N, e2 T
5 K* h1 E4 `+ z$ S/ m" X
//Incremental Positioning
( M" X `0 {/ G& e/ q case 91:
0 g5 K+ i7 _+ K) e9 F" T abs_mode = false;9 H3 W, w$ q" ]4 D5 A
break;* n4 w, N8 |, V6 x2 f
& l: K8 p8 a! u5 N, C) r" Y! p+ Q
//Set as home1 M( y9 r/ |. B9 q
case 92:
; R$ U2 M2 |; c8 g4 P - A# k g* v5 i( _! [& V. q" [
set_position(0.0, 0.0, 0.0, 0.0 );1 R0 w/ s# [; u Q- v$ ?" x- ^
g- y, k& |! s. U. D( s1 p+ M S break;
, } E3 @0 J- x$ ~( q$ r0 I X# ?" U4 {1 r2 p
/* K$ P- l% w& x; h$ d
//Inverse Time Feed Mode% r) V9 [2 ^; G! t4 ], Z$ @, p/ I
case 93:
8 ?. x( p9 ]- |# c + Y7 _; N$ }; Z8 j3 \
break; //TODO: add this4 ?9 g% p- `2 S: G
) V5 S2 p& u2 {% k
//Feed per Minute Mode. U- E+ N! N, l
case 94:
5 T" P l3 r; S$ d9 B
% y3 b( w: _$ `7 O break; //TODO: add this+ x! H$ J1 a Y5 H) M. g
*/2 ^3 B6 `) k8 E- {' ?( \& X" U
4 a1 [, i& d; o% t% `+ r, g2 l
default:
+ T) D0 g" |2 {- c: P% F) { SerialUSB.print("huh? G");0 _. k# u1 l* b! K' t; d
SerialUSB.println(code,DEC);
% k9 C5 y/ H$ _" n9 V( a7 o2 w; s }7 ^& Q0 I0 N; X) H( J! E# C
}
4 z$ x' o3 B0 H* v* `) F0 h2 l; T/ A4 q. G1 D% `' T1 R
//find us an m code.
& G' O& K4 D8 v; n* D8 O if (has_command('M', instruction, size))) q5 a% T. f) L% P; W
{
; M5 `; F$ X+ ^8 \$ J; k3 j+ @& C code = search_string('M', instruction, size);6 T+ S$ m3 x2 `. f* D
switch (code)' i* u8 v) p2 V5 N* k3 t, I* K
{
& ` p( `& T( X, ]+ { //TODO: this is a bug because search_string returns 0. gotta fix that.6 [" P# z7 j$ s* J R1 N M0 X- j1 a' y
case 0:
; ]9 q: W I2 D4 H2 l. s" U true;7 m) O6 a5 h5 H* K
break;
: M4 P+ k9 o( ~7 R z. N
' G1 }% H* R$ [1 v* z, G case 100:
, A& e6 g" _+ k. i9 M break;) ?+ o2 e* Y) w6 S/ h
& q* m4 |5 r6 r( U5 U/ ? // fire camera relay- W t4 L: Z8 p
case 101:
7 p! A8 u; k& {' e2 I camera_shutter1();
( ~8 E- B- U/ r break;
. v( w+ p7 f! g7 q0 g9 M+ |0 P4 i! V/ q+ V0 U1 Y% M2 t
// fire camera relay29 ]8 v8 b; k7 v; u
case 102:. }8 b' ]$ O. D4 Y, z/ M
camera_shutter2();6 n9 \" p1 S0 L A
break;
; Z# r% w' E E& v; g% B8 d/*
. N1 `# k1 E1 j1 a* _ // turn aux 1 relay on J {$ y: y+ ~, m+ h+ b
case 103:
0 b3 _$ G& e( _ camera_aux1_on();
: x/ o* X/ J+ A1 _$ p; y( O i: u break;+ G: T+ q: s7 `4 v4 ]& R
* }9 z6 [3 D- ^. {
// turn aux 1 relay off; q3 S0 F2 h, v% `2 e
case 104:
+ X3 f# f& y) S camera_aux1_off();5 U' V0 X) i" {0 u# X
break;
G, D/ M! @7 H$ q3 x( x& Y- p J( x' ]5 ~8 e
// turn aux 2 relay on
* z& ^8 B) e7 S* L2 v case 105:, R/ g# J9 Q, J4 O" F9 c# @
camera_aux2_on();
! B. I6 g* o" M break;
* w) g( g0 w8 Y; y0 s% h2 _
. Z& ?+ J" O8 W& R, p ^ // turn aux 2 relay off
: I2 T1 P" X- g8 d& W case 106:
4 w* E* `( M8 @1 m+ l camera_aux2_off();
/ f) V- V6 O; Z' x break;' i0 N o; w# i% u: s7 O
7 Y& i4 t5 U8 Y9 |4 [4 Z7 U' u7 G
// turn aux 3 relay on& E; w) j2 A8 p8 D) I6 H
case 107:- \1 I" K6 t( b: e, x
camera_aux3_on();' Z' ^, I5 Q+ n
break;" Y9 F0 d" Y- _4 |
9 f# S7 x7 F3 {% J- ?
// turn aux 3 relay off
. l7 h2 I# d! H2 R5 l7 O case 108:
% E, {) s9 N, ]8 E8 F, { camera_aux3_off();
' t% J' W3 L2 J% M9 J. R$ P break;1 g/ ]5 m$ Y2 u# t. K4 O, U
4 Y. W. \! t' `- L
// turn aux 4 relay on
7 N; T* ` H4 Z" f. D2 O& Q case 109:5 [! J; u0 S' d! l# \
camera_aux4_on();
. i( ^3 n! Z- _6 e break;
8 Y4 d0 d* ?. R6 n/ d2 W0 Q6 E' G7 h' S5 [8 f; l' y; W9 ?8 N
// turn aux 4 relay off
3 C1 t$ j! h9 t0 M case 110:8 A7 Y0 D, F* ^
camera_aux4_off();. n. z8 T( y4 N' D+ S! z( K" C$ @# T r
break;
_2 g, i) u+ C*/
# J+ _. c/ ]9 s) @ default:
+ G& Y2 l- Q' e {
' V( ~3 w3 `& j SerialUSB.print("Huh? M");
; ^/ Y A% T6 [: c" I0 c" S SerialUSB.println(code);8 ~" d6 F0 [; g5 R9 ^6 T
}
3 i! U7 Z% G* Z: i7 X2 M2 o9 i+ @ }5 s" u& _9 n3 o
+ U: }% s1 U! V {( i3 ~ //tell our host we're done.3 \1 x. G) X$ j2 ?4 x6 o
SerialUSB.print(byte(78));' i! [7 [4 T$ V* X
. K5 U5 F3 Z# ?/ `4 o; G/ |}
" |' U. T7 Z' J8 h( v
i/ v( U9 s9 e" x( y//look for the number that appears after the char key and return it1 R0 O- N. Q+ q: B" \$ C7 @, U
double search_string(char key, char instruction[], int string_size)7 E3 ~, d6 t7 [7 f" J) o6 N8 p8 \% j
{
1 R$ g9 w$ v6 V' l$ b char temp[10] = " ";' A& _0 r. T8 k) m3 m
for (byte i=0; i<string_size; i++)- v) x- Z4 G( |3 V
{. n3 L! x6 I' z2 T& ?
if (instruction[i] == key) N, C6 i3 x+ q% H; `' x
{
& T. P) \! h6 j' g i++; + W, h( X. C6 W! \7 t
int k = 0;" s# W0 i% u1 I: ?* @% F/ p7 I, W6 `
while (i < string_size && k < 10)
_# L2 g1 i7 a {
: ~; \1 H5 E" Q if (instruction[i] == 0 || instruction[i] == ' ')
, e1 N; G/ D. z* y' `' {+ o break;1 |2 d# v2 i# M/ V* r
7 ^0 o% }/ I d
temp[k] = instruction[i];: e6 e# Z% t. x7 _
i++;
9 s6 U# F/ L! J k++;
1 [) S. [ q8 a7 g0 O }
5 M2 }5 u- S; s% a0 V return strtod(temp, NULL);9 E! }7 g7 p5 ]5 s6 P
}* W, E; N+ k9 M
}+ i$ ^* n4 \1 B5 Q& H% v/ ^. Z6 t( U
4 u" O0 w& u6 h" t* w' L4 a return 0;
1 Y& l: [* N$ k/ X7 A9 o}( z# x) z. r' I5 O) D r
% F, M5 c+ ^$ J9 J//look for the command if it exists.% t! @/ E6 R0 ]6 P# I, C2 d. t
bool has_command(char key, char instruction[], int string_size)
. y* v. s2 q& ^- w. q7 z{% h/ J* R) k* b) A% a
for (byte i=0; i<string_size; i++)
& f. l0 e- ]/ @8 n. \ {3 b8 l! v; F* k: t
if (instruction[i] == key){
7 Q- T, ?; v: x( `6 h1 u1 M' e+ s5 K: f5 ?9 }
return true;
6 [) p- q- ^2 m8 B }
& h: P. F' I$ k* G7 y }
* q& `3 R1 D' q/ U& ~' u. _: j2 x+ K& F& M" X2 s, S
return false;$ `: n5 U1 c/ N( Z
}
3 D! T* b$ J" r2 Q* V }- e6 v L' N
, b. c% V1 a$ i3 Z; b5 \
6 C9 [* W% t0 s( K% Z8 X/ U) M |
|