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