|

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