国产精品乱码一区-性开放网站-少妇又紧又爽视频-西西大胆午夜人体视频-国产极品一区-欧美成人tv-四虎av在线-国产无遮挡无码视频免费软件-中文字幕亚洲乱码熟女一区二区-日产精品一区二区三区在线观看-亚洲国产亚综合在线区-五月婷婷综合色-亚洲日本视频在线观看-97精品人人妻人人-久久久久久一区二区三区四区别墅-www.免费av-波多野结衣绝顶大高潮-日本在线a一区视频高清视频-强美女免费网站在线视频-亚洲永久免费

 找回密碼
 注冊會員

QQ登錄

只需一步,快速開始

搜索
查看: 24665|回復(fù): 14

運動控制入門篇GCode_Interpreter

  [復(fù)制鏈接]
1#
發(fā)表于 2014-5-10 09:39:53 | 只看該作者 |倒序瀏覽 |閱讀模式
本帖最后由 xmdesign 于 2014-5-10 09:42 編輯
7 X) x- @9 ]9 p; f6 o6 ~7 Y8 w% m
首先聲明( D$ y* @1 q8 K; [% c" ^. H
我不是專家,業(yè)余搗鼓2 B+ _  N; G; N+ ]1 d" Y+ O9 F
源代碼首先來自網(wǎng)絡(luò)
$ c: P+ a! O' Y開發(fā)平臺Arduino及Maple
1 w2 a0 y2 K2 D% W5 h% b' X4 Fhttp://www.arduino.cc/5 d& j9 y7 n* C7 w- b3 M
http://leaflabs.com/devices/
9 l( ~8 G# I5 d  Y7 Y國內(nèi)活躍社區(qū)7 T  H# g& {/ z% C- x$ z/ T3 T
http://www.geek-workshop.com/forum.php ( I5 A) X7 C) s, y* w, b7 O5 o
4 W+ A9 n% G2 Z4 W
竟然缺少積累,首先尋找最大的開放式開源平臺,這樣可以積累全球范圍的創(chuàng)客和DIY者的腦力活動經(jīng)驗
6 X8 {0 q, z7 J9 F+ C, T& H來入門了解熟悉思路架構(gòu)等) v: @& L, H* C/ C+ j- J& P- l
$ R5 Z. s. a* C" P
我只搗鼓了下8位機Arduino 移植到32位機Maple 方面一點點事情,( l( k" l: a# t! r: p+ J
許多功能還木完成,不過作為低檔次得應(yīng)用可能還有可能
4 F) l2 i0 ?* B# I# h4 E7 q2 R3 u$ R9 Y: G% s& a$ N) o
我自己也是個半桶水,再樂意玩的起碼自學(xué)能力還是要有點吧( o- m0 s+ m4 |' ?

! k3 n/ W8 R9 _# c9 N: Z: e拒絕 所有的求
+ M' t6 a; g3 y7 U求人不如求自己 不然木玩8 T, D7 a5 w0 m7 y! x
9 q. [$ i) w" ~3 @4 G1 \& V
高手繞道 謝謝!& V% a% H; i9 a

6 n' k8 }& O) |& q- V# C
回復(fù)

使用道具 舉報

2#
 樓主| 發(fā)表于 2014-5-10 09:48:37 | 只看該作者
本帖最后由 xmdesign 于 2014-5-10 09:52 編輯
0 X3 l0 p+ d4 E$ N% N1 V
7 r0 v1 @% }& l4 R, l; Q  qGCode_Interpreter是比較容易懂的,木那些寄存器等蝦米開始不容易懂的東東6 i& Q: d4 D, S; k" T
貼代碼先
% j7 j+ L0 h5 l; ]$ ?直接Maple的,某寶許多超便宜哈
2 z3 ~( a" ~; J暈,我怎么上不了RAR?) Q. g2 X! N9 c8 S8 v. ?
想玩的留下 e妹吧6 k  ]1 e" {/ T1 r% |7 \" J5 J0 E' P
第一個妹麻煩傳第二個妹哈3 O/ K  @" N$ B9 n
我平常雜事多, K. z1 i/ J( i
誰放網(wǎng)盤上再,麻煩開放下
3#
 樓主| 發(fā)表于 2014-5-10 09:52:53 | 只看該作者
// Arduino G-code Interpreter for Rep Rap
  E. H/ q. N1 V// v1.0 by Mike Ellery - initial software (mellery@gmail.com)$ s; m( s; ~' ?4 x& |, V0 ]
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)% p2 b: y! L: P2 y3 O0 r# Y
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)4 Z6 }* a. L$ ]# b/ b) u
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
8 y- x- H0 }) j5 [
9 Q1 A" p1 K5 O* w5 Z& E// Arduino G-code Interpreter for Macro / Micro photography
- ]  q+ T! X, P* D+ Y% x// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)5 O- v( b+ F6 Y2 {
//modified by YaoHan China 2010.2.15
* ~/ N- V. O- _6 z6 L, _//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
: [' r* H( g! w$ X0 b. W- `#include <stdlib.h>" ~0 ?4 v3 m* p% m3 |! F
#include <LiquidCrystal.h>3 h; g7 z- ~3 x

1 M' m- ~- W5 `8 N8 o- L" j//啟動(高電平有效)/暫停(低電平有效)  默認(rèn)高電平% ]# k; I" A. P  V8 @# D7 Q
#define BUTTON_CTL 35
/ }" p& E# n' Z" a/ E8 ]! u, s7 L5 E' U) j: `! O( P+ w
//點動模式鍵(高電平有效)  默認(rèn)低電平- ]) U8 W; w, n3 z* a. B! y
#define BUTTON_SS 36% V/ H  Y# q1 k3 n- ~) D
0 `1 q% W2 d: K0 }
//點動鍵) R. [' O, L; T1 t$ O1 h
#define BUTTON_MAN 15+ c- i# x) J9 I: d/ X% @8 q
3 m$ V0 Q9 R, E- f; a& q
//電位器速度控制+ z. G: }' M' K
#define SPEEN_CTL 16
6 q5 T2 M+ d6 `2 j# x& _6 H# y' m8 a8 l( p/ O1 O; u
//手動調(diào)速使能
+ f' u% [* l6 e! p#define BUTTON_SP_EN 17
  V, k* ]* X) T% m7 W' d7 X# a& g- `& _* m8 m+ C9 K
//LCD 類型 1604或16022 Z: F$ e4 _) H) z7 o$ O: g
#define LCD_TYPE 1604" h( d- G4 o" B+ Z* e5 i( {
//LCD 引腳定義
8 E$ o- A9 ?1 Q7 m# W' C#define LCD_RS 23
, G6 Z) Y+ Y, W. h0 g) g#define LCD_EN 24
; e, R+ U4 w- T9 h/ k+ s#define LCD_D4 25
7 w. j, E1 I* Z( f#define LCD_D5 26& j1 \* ?! o6 z- X* U: ~
#define LCD_D6 27
: {  B) }4 l" ^) x. e' [#define LCD_D7 28# A- H& y/ {0 @, x" c: Y/ n

" V5 R& u9 v$ z. k! F& x9 l//命令字符串
: X- J0 T3 y+ u+ B( N#define COMMAND_SIZE 1283 j( r/ d* o$ e: c9 T0 S; e
char word_old[COMMAND_SIZE];) m* D' _* z- f: H$ w) @: B
byte serial_count=0;
+ U& k8 A, y& {% n2 p+ G, Xint no_data = 0;
  N; @3 h( B$ T0 ]//LCD 引腳鏈接 配置
" m% S( q# B$ E* F" W( w8 SLiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
7 n  e0 J# l/ a7 C//停止控制,高電平有效
" \' k* E! H1 \( a. o- r#define BUTTON_STOP 303 ?9 o; P: V" G* y
int stop_flag=0;' S' X9 y3 Q, l  K% h. Z# h, R

1 Z9 M1 U. B" `3 c0 b: y) F//暫停
5 L( Y3 K  w8 pvoid pause(){6 m! J6 e9 V+ j  W
  while(!digitalRead(BUTTON_CTL));, Q  L/ R5 j5 j- g0 Y
}' a; _' ?8 M& w* o* b' c

3 k1 q7 v/ Z; j6 t/ Q) uvoid stopper(){
1 x# B0 X6 B& c0 u1 W0 y& |, D  delayMicroseconds(10);" r8 F% n9 D1 o# _0 t
  stop_flag = digitalRead(BUTTON_STOP);8 s7 U: J+ R1 N( C. Q$ X
}6 v# V; N3 ^8 g9 x% Z* i2 j3 g; @( y

+ Y2 _6 I% ~/ K8 ?1 |. M* {6 T- |void setup()/ [6 T; D2 {- }0 T2 Z$ Q
{3 j0 A* I& T5 ~- B/ K5 U7 s+ G- U
  //show start
8 d6 T8 V2 k* {# D/ D3 I) I4 S  SerialUSB.println("start");
7 s8 u/ M$ S; s# T: L" p: a) H, ?/ J8 q; X
  //啟動lcd9 u3 g. H2 k! e+ ^$ v; w( O
    lcd.begin(16,4);
7 b& d) s) A  C: W    lcd.setCursor(0, 0);
- v" E4 A( n# s) o' T    lcd.print("hello, world!");+ _+ D+ K$ `6 @. S- N( x$ b

# j5 O1 [8 C6 p' ]) h  //初始化控制引腳及引腳功能( H$ Y0 o4 N. |# h' U3 _
  pinMode(BUTTON_CTL,INPUT_PULLUP);
- [8 f& f& X! f$ c* S- \! a  pinMode(BUTTON_SS,INPUT_PULLDOWN);2 n5 J; c$ j$ U- h9 ?' X9 T3 ]
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);! ?6 ~' X6 [7 a! O- ]
  pinMode(BUTTON_MAN,INPUT_ANALOG);. }8 @- u# {& r1 J3 l: j# ?

- _. X$ X! Z. M; n3 L" U# q  //控制引腳的中斷1 l; U; G, |- b% p: j8 I# A
  attachInterrupt(BUTTON_CTL,pause,FALLING);//暫停的中斷6 O/ G1 l! _( Z6 I; ^0 [2 E- F- P
  attachInterrupt(BUTTON_STOP,stopper,CHANGE);
* E0 G. _0 ?4 b" j* C/ g3 l6 x. n# ^/ L0 g8 p7 `; o
  //other init
( R. E% G& p6 h+ ~  init_process_string();
/ J9 O4 I; w( O8 V4 E' ]4 D  init_steppers();/ x$ s( r$ C& e% j) v
  init_camera();! f) Y( S. o! }+ _# k
, i! l6 ]& ?6 m* U4 [1 w0 h
}1 L4 G; N& {2 ~1 E! }7 P/ E
: [: r" _3 a2 f) p1 V9 M
void loop()
" a, }: b& p- ?% U" x{
4 U& f1 b% O9 U4 R& x' L6 `  char c;8 y/ D6 v5 c* p

1 ], A9 k: {7 X, }( y# f0 P( K) R  //讀取輸入的字符
& N3 F0 X+ m, k) q  if ((SerialUSB.available() > 0) && (!stop_flag))
" P6 N1 A: Z8 R2 o  {
( ?" H: h& B& [    c = SerialUSB.read();& v# v1 W7 h: F% d( t
    no_data = 0;: F9 {& ~, n; {7 h
$ _1 u/ [& b3 P( y2 k! }
    //換行符代表一個命令的結(jié)束* [+ N: s% P# C
    if (c != '\n')
7 E  j6 X: J( n3 e7 y* ?; E    {
- A% U% D1 l. {+ G' o9 ]+ `& f" O      word_old[serial_count] = c;
& }% K. O% u; P( r3 v      serial_count++;
" v1 v! k- H3 ^2 O. d! X& J3 R$ a5 X. b6 T
    }
6 g; n! p, _8 M! J  }! m) Q8 L6 b- t" a& Q
  //標(biāo)記沒有數(shù)據(jù)輸入7 z0 l/ f- ?- J9 _" a
  else
6 I  U* U8 b/ U7 P. {. W) T  {+ e" d! L! V  u" j/ j
    no_data++;; M. J! }* t- {$ u  K
    delayMicroseconds(100);
. M6 _7 s% k! Q3 r0 T: r3 z- X  }# p0 K) M$ k$ k6 X

! B3 C+ G) ~/ [+ ~4 q  //if theres a pause or we got a real command, do it
) v( Z3 w  _! w$ m& X5 d  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))) J" V: u$ [+ s% [( y
  {$ i8 P: |8 k; w

6 |9 h0 K! }% ^    //處理命令
+ d/ ?- F. X  P& r. J. S    process_string(word_old, serial_count);7 h9 f. c* T2 ]' m# Q7 U. |3 k6 P  J' T

# W5 G6 {# u1 L5 F& {0 x    //清除命令
; h5 |) K1 z8 R1 T6 n: I    init_process_string();
( ~% U3 B# c9 W8 X  }3 ]) y" r/ A. a" }
( ~+ b) Z: `$ E/ }4 A, u
  //如果沒有數(shù)據(jù)關(guān)閉電機
! l4 x% n6 Y5 f6 u9 ^- L" l  if (no_data > 1000)
2 x4 y; U0 A1 A: F# b    disable_steppers();( P+ R8 D8 R! ^) z$ v3 \
% U3 g" i" p' h! S
  //如果ss鍵按下進(jìn)入點動模式
! P0 q6 U& }. y  //if(digitalRead(BUTTON_SS)) ss();1 P" f0 l! t( t4 Q: c9 q% E  M7 T
  run_a();
1 F7 {, w" P: O" Y" Q& a( `1 C}) K8 A; h- I* S: j

" o* X9 V/ Q5 ^4 }4 Q! ]//點動模式0 B9 d1 E& O; Q  D+ [& x
void ss(){
3 B0 L1 o4 L8 q2 }; @1 k% y* y- R3 l- G
  delay(1);
& S* J3 F5 Y/ @' b6 z  if(!digitalRead(BUTTON_SS))return;
" z9 `  o' ?& P" Y  if(!digitalRead(BUTTON_SS))return;. J! U# q: J- u8 v4 x
  //init_process_string();
( F" ?( z! u3 w0 S  //init_steppers();
% M* \) P) W( a  //init_camera();% D; P8 D2 |0 c+ m1 s( f6 Y  i
8 \* |7 i0 w) o7 l& q. P
// SerialUSB.println("Step By Step Mode");: ?) U1 `. D) z3 d! c. ]( G
//#if (LCD_TYPE == 1604 )
3 V6 K% E5 ^" a' M$ P9 l$ t' e4 h  //當(dāng)LCD為1604時顯示要處理的命令
" j/ ], x2 d3 g& `7 a) ~+ z   lcd.setCursor(0, 0);0 z) F6 ~2 d: S% ~
   lcd.print("Step By Step Mode");
* r$ e' [6 L3 p! D0 O//#endif1 S/ Y- Z0 @; L7 U5 x
  process_string("G1 F5000",8);# B" U+ I; u  O
  process_string("G91",8);
' W- H8 q) R; w  //init_process_string();' C! N4 E2 \2 v. S6 [8 b
- Z$ `1 R, {8 [0 a; X7 }9 b
  while(digitalRead(BUTTON_SS)){
. A/ Y- Y* I0 h) Q/ B2 k) Y    int i=0;
+ w- O% C6 {3 E: x: \0 L# X    i=analogRead(BUTTON_MAN)>>9;
/ y* j) c* R8 N, [( X3 t8 B    //if (i==0){break;}' y( H3 O+ m+ Z6 C7 j
   //SerialUSB.println(i);
" I# X0 o5 D$ t. i! J   //delay(1000);   4 j$ d9 w4 }" T- A9 Z- Q+ O+ p

/ Y& i3 y5 `* |1 z& k9 x7 f   if(i==2){4 S' {( h. q$ _& d
      set_target(1000.0, 0.0, 0.0, 0.0);
% T7 @( M1 Y; Q( m3 }      dda_move(getMaxSpeed());: b  v5 J& F# y  I+ ?" z3 O
      if(stop_flag) return;5 ]6 y" ?* [  ~7 G- k8 Z
      //process_string("X0.01",5);
) W3 f; R. y* G  ~* X, T3 J      //init_process_string();. U1 i0 x- l$ M0 q' |
    } 7 ~$ x' R  I- ?: @- Y
    else 8 M5 r; B, g8 \6 s' w0 ~, P! T0 P
    if(i==5){
' I5 v1 e/ r3 I* b/ i; U, ?- X      set_target(-1000.0, 0.0, 0.0, 0.0);
0 T+ k. g# ?- V0 p4 f( b  o      dda_move(getMaxSpeed());
9 H; _5 L% R  g2 ?9 H      if(stop_flag) return;
" a4 x% D! k' N2 e" M5 s      //process_string("X-0.01",6);
: ^7 q/ Q; ?; S      //init_process_string();) F" m( y  K) P
    }
1 b# {/ u6 j- |     2 A$ @, T1 R# K( S) i- Y0 z+ Y1 `

: K0 c) j8 E7 ~4 V  }
% B  {: i" ]% l; m% d& l) Q3 y
# p, f* ]: P5 Y# _  //init_steppers();6 j: ?2 n+ n8 O  ~" ~$ X. h
  //init_camera();% F, d$ g7 K0 A7 e
   // SerialUSB.println("Return To Normal Mode");
; X9 L. w8 S8 s7 a% p     process_string("G1",8);
/ `3 N. x9 o7 Q  // process_string("G91",8);+ L6 O, d$ M7 g& P
    init_process_string();
8 X8 q* p+ U4 i+ c* {& q//#if (LCD_TYPE == 1604 )
& Y: m& M) j( v$ p% ?3 I  //當(dāng)LCD為1604時顯示要處理的命令
8 M5 d4 Z7 L  J( X- M1 H2 f% W  // lcd.setCursor(0, 4);
. g4 [9 i( u1 Y5 t  // lcd.print("Return To Normal Mode");" z" f, ^( G" G
//#endif
; N. c* Q- L+ J- O  B  g5 z8 D! T/ E# a}
( u2 h0 }3 S6 a2 S# x9 W/ P& M' G3 _7 w: v( D- S
void run_a()4 z# ]  I# v9 ]
{
. ]/ K7 r, u! O% @1 o3 j  //delay(1);
1 N9 Q- X) o- f9 h  //if(digitalRead(14)== HIGH)return;1 ^% q! e) }! F# p/ }
  //if(digitalRead(14)== HIGH)return;0 C  F% g( f5 p- `% M; y+ C6 Y" Y
  //process_string("G1 F2000",8);
" w# q% k% d& G, q% j+ {/ ~ //process_string("G92",8);- p" R  V3 X4 G' I
process_string("G90",8);
% N, w2 `9 e* d+ q/ Z" c' L process_string("X120 F10",5);7 m- |/ o( ~( \# h* `
process_string("G4P2000",8);
  I' T3 T! V, w! r$ U2 O9 t2 Z process_string("X10 F10",5);
& R- f" `" g% b: T9 ]6 P4 M2 h// process_string("M101",8);
' ~8 N) L0 ~- b# f/ ~// process_string("X-50",5);
, m+ L+ Q; w% ] dda_move(getMaxSpeed());5 m. U. _0 c7 I2 \* K
init_process_string();
' w& h* N+ k$ @- x* K8 u! u: Z: Y
% N  i1 G/ d2 G1 g  t}; w, S8 E9 _$ J/ }) p. |
4#
 樓主| 發(fā)表于 2014-5-10 09:53:25 | 只看該作者

  ~& K' |) e4 i" |2 s  \0 R+ P0 o( t: \
// 定義機器參數(shù)2 c! k1 i4 y, U& n) ]7 r! `4 c( \
#define X_STEPS_PER_INCH 400
6 v& w6 X: L, g9 t0 U; s* J0 z#define X_STEPS_PER_MM   16.0, c3 L3 r: C* _
#define X_MOTOR_STEPS    200
1 L7 d. e: C9 v) B$ i) a% N7 I% S3 V5 `5 N- O) I$ b
#define Y_STEPS_PER_INCH 400.00 v; s( o8 g2 a0 G6 o9 f, S; J
#define Y_STEPS_PER_MM   16.0+ @* w. r& _8 v5 y# ~* {4 J) ?
#define Y_MOTOR_STEPS    200# d5 M9 T( F: E

% ~+ A6 p  ]6 n# a2 l: u2 I; {1 k#define Z_STEPS_PER_INCH 400.0
+ d) H9 b# J/ E8 x2 ^4 q. t2 m& B#define Z_STEPS_PER_MM   16.0
, Q7 y9 `/ ~( i: }2 [#define Z_MOTOR_STEPS    2005 |! K/ l: }3 `5 G5 L) i
6 S3 J2 J" l6 U# }: D! ~1 D1 D) a  o
#define U_STEPS_PER_INCH 400.0
1 Q% ^, U3 D/ q2 D6 E/ W5 p& C#define U_STEPS_PER_MM   16.06 G3 S0 \3 y& E( r, d  R9 v6 o
#define U_MOTOR_STEPS    2004 e$ @. W5 K6 M9 [0 p7 l1 l
6 |: [( B; }4 Z
//最大進(jìn)給率
0 \$ x" [  |( [6 r7 G4 Z#define FAST_XY_FEEDRATE 1500.0
) x- p: n; m2 ~+ z#define FAST_Z_FEEDRATE  1500.0
( Y/ L8 F# J* z( k" g* n#define FAST_U_FEEDRATE  1500.0
8 l2 b: N; R0 W0 o, X// Units in curve section8 p( w* ^4 U$ P0 r+ \# r
#define CURVE_SECTION_INCHES 0.019685
9 V: p6 U! y/ `1 C! @#define CURVE_SECTION_MM 0.5$ q% R+ p5 c* I, t4 q: \) l
# l% u! S5 X, Z. w4 H. h, d8 W
% ^4 }7 N1 f$ `4 r( M+ f1 \9 d/ m
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
. d7 |8 o& G& y* @- \2 J7 L8 F// RepRap opto endstops are *not* inverting.
! g+ W$ N; ]5 ~# C! r0 f#define SENSORS_INVERTING 1  {+ s. q( U* j; d9 w- [

$ t+ m7 L5 s& o+ o; e; |! {$ G/****************************************************************************************1 c" s& z3 |/ C7 q9 t8 y
* digital i/o pin assignment
) N; y. n9 l" `: w *
- e6 f% ^: i. l0 l * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5$ k5 q6 L5 K* Q0 W. z' U
****************************************************************************************/
0 X$ z' g9 t* A0 U" P
; c& W  ]/ y- a9 Y, w! h//camera shutter and control pins4 C& `" \% p% T
#define CAM_SHUTTER_PIN1 299 p" v" ]" N* |% m+ Z3 |' R
#define CAM_SHUTTER_PIN2 30/ E  h3 \0 C8 G' W0 `6 |9 P
//#define CAM_AUX_PIN1 31 // analog 0) E* c* k1 x: P( D/ x3 D( ]2 |
//#define CAM_AUX_PIN2 32 // analog 1
6 k$ _3 [$ Z& B+ f# B7 c% }//#define CAM_AUX_PIN3 33 // analog 2  {$ P+ e% m& u, D5 M8 Q
//#define CAM_AUX_PIN4 34 // analog 3& q! u3 N; m  B6 v# Y8 [. p1 l
4 v9 A: Q0 z" M# v. N% e( e
// stepper driver pins
* K9 Q! Z. |: L2 ^; ^# D5 Z( L#define X_STEP_PIN 7
6 o$ w/ V5 E* b9 B$ t0 x#define X_DIR_PIN 86 I9 d$ M3 l/ h; m, C$ |; ^: _
#define X_ENABLE_PIN 193 p- A1 }, z1 p$ O. x# p

) z4 R/ }0 _8 |+ Q#define Y_STEP_PIN 9
+ E+ d! U$ ]# R" K#define Y_DIR_PIN 10
  i# U/ f# ~( p! U#define Y_ENABLE_PIN 19% g# `1 ]6 q- a$ b' x
* \% z) G; q8 R- t
#define Z_STEP_PIN 110 `) z; \) L& }+ R3 p* I
#define Z_DIR_PIN 123 z8 ]; x# w3 h4 `# Y/ a/ Z" V, X* t
#define Z_ENABLE_PIN 19
  [  O* A- ?3 `" J1 z8 s" B9 n0 [  L
#define U_STEP_PIN 13* ?- u+ J  P8 P0 j- B! b. ?
#define U_DIR_PIN 14
& z" C6 O% M9 |7 U/ \3 Q#define U_ENABLE_PIN 19& H3 D. @' T6 o/ Q3 }' B; s2 p

0 x& g. o3 h1 r// limits not used right now. K7 `6 _. A  B; b- m+ {
#define X_MIN_PIN 14
& W' r5 {2 {# [$ z, `#define X_MAX_PIN 146 E9 P; o7 y* i) Q; Z/ R
#define Y_MIN_PIN 14
4 c' g; S6 i; t#define Y_MAX_PIN 14+ B* f0 I, y4 j3 d" z4 a; C, n
#define Z_MIN_PIN 144 c4 h* ~2 s- o% ?) h7 B5 S1 n
#define Z_MAX_PIN 142 g/ a) d; v, O: y; t4 K" p3 g
#define U_MIN_PIN 14
" L9 ?' O% g) ?& f7 e#define U_MAX_PIN 148 `% r! o  d8 X! e
5#
 樓主| 發(fā)表于 2014-5-10 09:54:26 | 只看該作者
void init_camera()
, q! R# c; \  u& \- W4 N{4 w( `" ~$ ?) v' M0 ]' ?) ^* b% b
  pinMode(CAM_SHUTTER_PIN1, OUTPUT);" H/ L# v- p3 C
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
. T$ L8 e% r7 Y" p2 \  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
5 j/ u6 e, v. q7 s* W( E // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
) G( v3 @# Z& l% `9 G# g! V4 V8 h' Q // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2. k. I3 a+ [/ U6 m4 e. p& g8 Q  |
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
; `+ P% Z% F9 x; r5 I}# r0 x# `) Q& g
5 Z( {3 ]* \$ D6 J; S
void camera_shutter1()
( ?% K9 t" G4 C9 R- |4 d{1 W6 j7 J: p+ T' }1 R
  // fire the camera shutter via relay...1/4 sec hold time+ r( d( _% _. k/ r) s, V  O% l
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
7 c- `, P! W9 y* X8 {  delay(250);
( M+ C  \; v0 X! ^9 `8 Q1 h  digitalWrite(CAM_SHUTTER_PIN1, LOW);1 B! ~* W5 e: U# Z
8 G2 _( M& c9 g
}
, |& v- z! M$ C4 D4 E0 P
4 ]* Q% ^7 Y: e( ?5 wvoid camera_shutter2()8 v5 P$ ~& O/ C6 m% T
{8 `# g4 Q+ N# w8 V9 W
  // fire the camera shutter via relay...1/4 sec hold time
" D' p. t& P% |- n  digitalWrite(CAM_SHUTTER_PIN2, HIGH);1 u5 M; `1 U0 B: |0 e( K
  delay(250);0 U. \: J, e& }8 S) t
  digitalWrite(CAM_SHUTTER_PIN2, LOW);
! r: g: Z, S% h# r6 v5 x: G3 V; h$ ?; h% u# T( o# f% H* e
}. @% m8 R, ?. U/ M
/*
5 ^. x7 N' R# C- w$ f2 `void camera_aux1_on()
: H- T" V+ }1 W+ h  A{
  T  @8 O: f- \4 _& k; K  // turn aux relay 1 on5 z; S2 d' r% X' I$ N
  digitalWrite(CAM_AUX_PIN1, HIGH);2 X, A7 L4 ~4 d: b* [
}3 [7 X" W# G( E$ B7 f8 P

5 n" A9 I+ ^% Q% L& Z) gvoid camera_aux1_off()
( k" B- R. k5 |{
, {' s5 K, u  M; k: K  // turn aux relay 1 off7 R( M6 I  t# ^
  digitalWrite(CAM_AUX_PIN1, LOW);) h' X' I6 U% k' E4 P8 c  {) i
}
0 c7 @) `3 [* T2 P- g9 N4 |5 m7 \8 @( [- Y. d, e( O* D
void camera_aux2_on()
2 |* R/ T0 L& n5 |0 j* [' P8 X{2 |8 @1 X' S) ~
  // turn aux relay 2 on4 Z3 Y( g  e' s( O) V/ s9 b/ g
  digitalWrite(CAM_AUX_PIN2, HIGH);3 J/ W$ C7 B7 e: N
}" D$ B3 [( {1 @" X" _; m) l1 ]

) ?6 R3 ]. }% lvoid camera_aux2_off()  ^+ P2 G& G, J) l4 m
{8 y7 v0 \9 X' [9 U' ^$ r+ }6 K- a
  // turn aux relay 2 off; x# O: L( [0 E$ r! w2 `3 ?3 L5 U
  digitalWrite(CAM_AUX_PIN2, LOW);
* u7 z2 Q$ j7 m! x8 B8 _}! c$ u/ u7 {6 g# m' O. X! I$ h9 X
( U& D. q& t  |! W1 ^. g% L
void camera_aux3_on()
5 B/ f( k; X4 G3 Y* i8 {{
3 ~+ g- [1 H* ]( _, {: S3 `+ }* E1 ^  // turn aux relay 3 on
  _7 G6 M8 n/ ^, u. Z# e+ G  digitalWrite(CAM_AUX_PIN3, HIGH);
  T0 Z( ~% d* w}9 }& w* _, T* B' ?8 I; L" l

& K0 h- b7 b) s6 O0 pvoid camera_aux3_off()
; ~9 ~* F+ C# `9 L! a2 b{
. p; K; R% J: _( J! N  // turn aux relay 3 off6 H9 ^  N  w* m* `1 ^
  digitalWrite(CAM_AUX_PIN3, LOW);
7 A! Q$ ^9 B6 e}
4 h( Q& m; K) \8 b% M
9 g# N: z. z0 {3 _$ c1 @void camera_aux4_on()
: Y) P; B( f. z! }2 {" ~' V{/ f; G: t  L. {2 z- U
  // turn aux relay 4 on
' I6 u: W$ m: ^  digitalWrite(CAM_AUX_PIN4, HIGH);8 O) g. ^9 G9 d1 S
}0 J$ }. _. d8 H1 C

# g. K+ f' j! Kvoid camera_aux4_off()
' p& D; Q( B5 g, X{
% U" j, O; h! ]# N% N. ~2 O  // turn aux relay 4 off* n% Y6 F, w+ d$ a  j' i
  digitalWrite(CAM_AUX_PIN4, LOW);
% y: Q5 v- G/ P7 P' Q  b2 a}
6#
發(fā)表于 2014-5-10 09:54:28 | 只看該作者
樓主推薦的網(wǎng)址不錯9 S. r( K9 m9 n3 A! y
請問樓主是玩什么的,用樂高玩具嗎?
7#
 樓主| 發(fā)表于 2014-5-10 09:55:02 | 只看該作者
// our point structure to make things nice.
" C6 `6 h+ p, ^' d: s3 ?struct LongPoint {
3 _6 y0 Y# l" X0 Q  long x;* p' u2 h& n2 T3 _; s8 ~- `. U
  long y;
5 W$ w/ K9 H( v+ U  long z;
; M4 L4 ?3 O' x6 o/ W+ y  long u;
* r0 l' q% ^. Y4 @9 Y2 b6 d/ t, E9 Y};& ]7 ^0 x2 t, i8 f! Q; n
; W' _2 x3 _. j+ k- H; J# u2 `
struct FloatPoint {
0 U7 t" q6 ^2 m: ]$ T& z  float x;  E/ ?; f1 Y! l! H
  float y;) a4 `5 [5 x9 s% Q, F  p
  float z;
3 X4 d9 E1 C' x7 e; L  float u;
: D6 S! f: {0 q- w& {$ P8 y6 e) j- b. k3 \};
% j( T; N  F  D6 N6 G) \) m) z, i! o# k- x7 ~
FloatPoint current_units;
  d( e( x/ ]: y6 x7 M9 a" a# lFloatPoint target_units;
9 A! J  f( ?5 W/ y  GFloatPoint delta_units;! }6 y% @/ X. I

( T) F# A* t  L, LFloatPoint current_steps;
) ^. l7 M* y9 H$ p6 E% I, ^FloatPoint target_steps;! p7 I1 a! _: @* a/ U5 W
FloatPoint delta_steps;& j# P, j& x* \1 m  a- Z6 }

5 o0 g& S* \  Wboolean abs_mode = false;   //0 = 增量位置模式; 1 = 絕對位置模式
) r" q4 ?* D& n( h2 o0 h' X( w, k0 h/ M8 }
//default to inches for units8 o9 H% U8 k+ k, a
float x_units = X_STEPS_PER_INCH;$ _5 A' O4 f1 x/ U
float y_units = Y_STEPS_PER_INCH;- s) d- j# M0 ^9 X
float z_units = Z_STEPS_PER_INCH;
  f: I! Q4 l+ G, Jfloat u_units = U_STEPS_PER_INCH;
: [. F4 y4 j7 Q  i# X) ofloat curve_section = CURVE_SECTION_INCHES;
0 Y: h9 F" L; s8 s* J, f. l; G9 O# y2 p# {7 b  c3 F& X
//our direction vars
# b) Y5 M. S  r6 Y8 x- cbyte x_direction = 1;
# S' Y, H! z+ _0 U, e/ j& d9 ]; Hbyte y_direction = 1;
& ?) Y6 [% u" a7 ~* \" wbyte z_direction = 1;; }3 m# e: j6 p. p1 |
byte u_direction = 1;
1 u4 ?( K, u+ q
7 k# A# }# T0 g//初始化字符串處理
. e) N" K4 o$ _$ Lvoid init_process_string()
3 J& H7 d8 o2 i/ t{8 Q% H$ m% `7 A" L0 C3 t: I' b
  //init our command
8 E" B& U, h+ I& D  for (byte i=0; i<COMMAND_SIZE; i++)
  y8 ~: Z' k# X/ f3 M& N    word_old[i] = 0;; _8 U9 l9 q9 m2 Z
  serial_count = 0;5 ?  v2 X: p7 p# a2 J( ~8 r
}
5 V7 Q- u3 u. g. K/ X# d$ q* i; e) j. `2 q9 U) {2 k
//our feedrate variables.
5 u% E7 p7 |' K) g* Sfloat feedrate = 0.0;
6 C& K2 O$ c. z  j3 @( zlong feedrate_micros = 0;# A3 ~% C$ p4 ^

& V: W7 X' y5 G  `$ N3 m! p//讀取并執(zhí)行命令
9 D  L4 F; D% E3 E( O: t+ zvoid process_string(char instruction[], int size)5 \2 Q7 H' X2 u: P0 j3 |$ n% d3 i
{1 d6 s# _# s# V) R
  //the character / means delete block... used for comments and stuff.
" w, U7 b- P; [0 P' [: b  if (instruction[0] == '/')
: Y3 Y. X* p& t2 T* T' F  {
: @2 P( s, L( K- M6 [    // SerialUSB.print("ok");0 j" l# S) y9 ]' N. M( u* U
    // SerialUSB.print(byte(78));
6 o) P( [0 j! X, G    return;
% m* b+ G7 b) \" \# N' `  B  }. W$ {; O2 t( A4 Q1 |6 g' v
  //init baby!
8 F4 h4 a# W- q# S5 b4 J4 v& k  FloatPoint fp;
( s- J0 z# P+ `  fp.x = 0.0;
5 \) y& n$ m" z$ J  fp.y = 0.0;
8 N( q4 Q/ [5 H* a; @  fp.z = 0.0;" k! L/ d- W. m
  fp.u = 0.0;
3 a- X+ e' x! x8 I6 f0 A) [* A2 k! R! O7 L8 F& F! \
  byte code = 0;
" s% T! G9 J% g/ I
) N( O4 ?! x- S4 y# D  //顯示在處理的命令
& r7 G. W3 W6 p+ T#if (LCD_TYPE == 1604 )
8 u5 k$ i0 ]- V& ?8 {% k; _2 X8 h, {/ f  // lcd.setCursor(0, 4);
1 j: ?4 q% g# X- {2 p: a5 h2 J. o% n  // lcd.print(word_old);7 g! |/ M/ f- w2 \+ _7 l: T
#endif; O- G& d2 H$ O3 k! x& H
  SerialUSB.println();6 Y/ ]3 @7 a- K- Z- g2 E- W$ T- r
  SerialUSB.print(instruction);7 a: i% \# M( L# o, \" U0 d' g
  SerialUSB.print("\t");- d( n  N- f# u/ u$ H

# I; K/ n- o/ Y. @# p8 ~  //what line are we at?1 l1 U5 S; K7 P$ B" w
  //        long line = -1;
( e8 _; r; q: B- s3 y  //        if (has_command('N', instruction, size)). V9 i" P: ?: M- ?* C+ b
  //                line = (long)search_string('N', instruction, size);. g; J/ ?9 s, {2 }

: G7 k# ]' G- P. G9 L  ~- p  /*1 L2 J6 h; o) v( B2 q7 Y
        Serial.print("line: ");
" U( R( C3 f; F# E           Serial.println(line);9 K# E5 V) ]& T
           Serial.println(instruction);- y# I% G% q! O5 }4 g+ o0 C# ]
   */
& B  c5 Z7 ?' `9 w3 D9 l  //判斷是否讀取了個 G代碼?- p# @% H/ T' b1 L
  if (
6 u  l9 }8 A1 y# o( h    has_command('G', instruction, size) ||
! c/ A# L, L6 q: |    has_command('X', instruction, size) ||
$ R/ T! C& ~- n, Y3 e6 }    has_command('Y', instruction, size) ||
. p. O. A6 j7 R; E' y    has_command('Z', instruction, size) ||7 {& I& t# s. r( Y: }3 f
    has_command('U', instruction, size)0 P$ u$ T( q' N+ y* i1 M2 R
    )! ]6 c0 Y* f' g; @, ~9 V3 L
  {
) D  `" _5 T& G    //which one?
  _9 g0 T* w; p" I4 x  t  z0 S8 G9 V1 p    code = (int)search_string('G', instruction, size);% t& X5 q, i; ]0 p$ g
    // Get co-ordinates if required by the code type given
2 S/ R6 E" _8 @& x4 J6 \0 v5 r    switch (code)2 z9 C+ H4 p# d8 w
    {& m1 W4 f9 ~$ w8 ?9 a
    case 0:
4 l6 h; K6 N/ s9 |7 i8 R6 ]    case 1:
4 U) j* ^( k( p  H3 `. ~    case 2:9 C+ p$ Z2 _  p
    case 3:8 m! v$ p7 P0 j# H3 {8 ]: ~
      if(abs_mode)
( U$ a4 M- c) N! K1 c) W1 Y      {; h; L, ^% P$ s5 V) L
        //we do it like this to save time. makes curves better., _2 A% j' V9 V8 G7 p" b
        //eg. if only x and y are specified, we dont have to waste time looking up z.' r& f7 d% \/ W
        if (has_command('X', instruction, size))
( T  M" V/ H/ Z3 V" k0 o* T          fp.x = search_string('X', instruction, size);3 `+ q: F2 N0 T% U4 p- ^9 y. K, ^
        else5 ]/ B& s+ z. T6 q" z* Y
          fp.x = current_units.x;
1 E" p1 }0 I& R. ?/ K
8 I8 a1 C5 n) m3 }  b        if (has_command('Y', instruction, size))
6 q$ ]5 N' {7 f& p- i' v          fp.y = search_string('Y', instruction, size);
6 @5 K' T* a5 Z* D, `        else/ R4 y# y2 _+ D
          fp.y = current_units.y;( F; g4 }" F% i6 i( x
0 r% k. K& X* X7 f
        if (has_command('Z', instruction, size))( _/ G2 m6 b3 H# `" _! }& x
          fp.z = search_string('Z', instruction, size);& k& U( n% ~3 N4 b5 f
        else* T9 o& N2 ?2 R7 A+ Y; y" j
          fp.z = current_units.z;
5 U* s% S1 l3 O% j( \          * c' m) D/ a8 K! i
        if (has_command('U', instruction, size))% J" i0 k7 |- T2 x* a3 M1 R
          fp.u = search_string('U', instruction, size);
/ [  U6 R# E1 M9 A% a, U' w        else
6 q9 D: }& p2 M2 w, D          fp.u = current_units.u;$ Y0 z( F/ n4 P
      }2 N9 i2 W4 o. T7 q7 i
      else
6 a- l# a" r  U9 m+ l! l      {
" i; O8 e; i# B# ^' H  K" _        fp.x = search_string('X', instruction, size) + current_units.x;
2 T9 P' `0 W5 Z) B6 a* X& l- ?8 z7 c        fp.y = search_string('Y', instruction, size) + current_units.y;/ w) X. `) f9 Q1 j: p% d* ~& w( r( \
        fp.z = search_string('Z', instruction, size) + current_units.z;1 C5 s* f; P9 p, Y' A/ K
        fp.u = search_string('U', instruction, size) + current_units.u;
& x! t5 ?0 b& r      }
# g3 j+ c- b2 X( T; g- M. i- `      break;
  T3 h3 ?9 I( Y1 O' o4 x    }- ?5 S# b2 i8 v1 ]+ z3 [
    //do something!
1 R5 m/ I0 l8 l    switch (code)
. U& a1 a# Y3 l8 r& ^    {
4 X6 J4 ^1 `/ g      //Rapid Positioning
# |; }) U9 ?+ P6 E2 U3 x3 j" b      //Linear Interpolation5 X3 W8 u/ x7 Z0 F/ q) I; N* ?0 b( U
      //these are basically the same thing.
9 M' H+ Y6 C' e% N, L4 A0 A    case 0:
( S' M6 V+ f* C0 d" Y    case 1:
& w( U; h8 z) g1 L      //set our target.. p# ]0 }, ^0 F" ~& W- R4 J8 M
      set_target(fp.x, fp.y, fp.z, fp.u);
- c3 g; @+ G9 ]/ h' n0 F, k) X      //set_targeta( fp.a);4 _9 W$ F0 p* N( G; }
      //do we have a set speed?9 ?+ k, v' }8 A3 N! |4 e, `3 n
      if (has_command('G', instruction, size))
3 u* E# U  N4 S5 R) s      {
: A0 X- J) ]2 _+ ~* a        //adjust if we have a specific feedrate.  B( g+ ^( e& s4 q: j- l9 ]
        if (code == 1)
% s4 b& O# Z# Q0 _) M$ n& n5 z        {
) e' C3 ~! Y; f- e          //how fast do we move?+ n) S  G( O8 h/ n$ q: y
          feedrate = search_string('F', instruction, size);
8 b/ J, e. ^( g5 l# G2 `          if (feedrate > 0)
6 ]8 N2 d' C. n            feedrate_micros = calculate_feedrate_delay(feedrate);- ]/ t0 C+ S; w( y8 U
          //nope, no feedrate
- f! n% G& _& h- G& ]          else
# I  [" O/ z" e$ z            feedrate_micros = getMaxSpeed();. i8 X* M: y1 ]
        }9 n8 x5 h) a1 U$ Q/ `
        //use our max for normal moves.( }! F  W4 L; s8 l- I% r
        else/ Q3 ^$ g& G5 d. }+ u
          feedrate_micros = getMaxSpeed();
5 f: u, G5 k4 z2 ?      }( j5 V$ I) y) _2 g/ V
      //nope, just coordinates!+ x6 V0 x% \* U/ T# k
      else( o( o! j* F0 O! U" T; x
      {
# d  `2 v- a- C6 e3 ]4 c        //do we have a feedrate yet?; d, l* K% w! `7 U
        if (feedrate > 0)* w' S4 G' ~" {- _
          feedrate_micros = calculate_feedrate_delay(feedrate);9 ^$ y- ^% r* b9 H2 U
        //nope, no feedrate. ]1 H6 y# y7 U
        else/ g! M+ k( f; R8 ]
          feedrate_micros = getMaxSpeed();6 K' t6 w2 c; H. I6 I& C7 i/ I
      }& `! s* t# g+ ]* ]4 c9 S4 x

6 t3 N. k1 m3 N, P' C+ o      //finally move.
3 m% y# t8 ^7 T      dda_move(feedrate_micros);
1 w. K* k0 X, v. \, }      if(stop_flag) return;/ w! t7 V2 G+ _! v9 f
      break;: Y3 J1 h/ b! B

1 w# `/ G1 W$ V' c3 N1 |5 P, q      //Clockwise arc
" O2 \% p" ^1 e3 s* S  _    case 2:% z) [# @( m: [
      //Counterclockwise arc
4 f! |8 H( h) d/ T    case 3:" C* I) l: S& k6 \
      FloatPoint cent;" w) E: G3 l% F: ~
      // Centre coordinates are always relative  S% w5 G+ ?7 z5 O- B4 |6 k0 r
      cent.x = search_string('I', instruction, size) + current_units.x;
5 |4 e4 e, E% o( z- ?. s# E      cent.y = search_string('J', instruction, size) + current_units.y;+ ?4 X2 p+ I# c8 p% C! g
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;4 s  Y" u" {9 C6 C

7 c% a# i9 K& w4 `      aX = (current_units.x - cent.x);
* A! U! y. H& ^$ ]: h5 R2 s: h3 h7 B      aY = (current_units.y - cent.y);, X: ~3 Z1 _) a% w5 p- T! O* x1 g. r
      bX = (fp.x - cent.x);
% ]7 i. @2 J" a7 v" V: \+ N      bY = (fp.y - cent.y);- \" R/ x* r3 ^8 C( L
& u0 h; E1 c' W9 r6 O1 Q; L
      if (code == 2) { // Clockwise( _( o# E! m9 _" b
        angleA = atan2(bY, bX);
1 D0 c4 a9 m* a1 P! l: R1 a+ \        angleB = atan2(aY, aX);
* S; W- |5 A% }& l8 q      } % e: C" N% W$ x/ w( Z4 c
      else { // Counterclockwise6 S. R! Y2 x9 \9 n
        angleA = atan2(aY, aX);
2 V( _3 j6 ]* U- k4 s: e; Z        angleB = atan2(bY, bX);
- z3 K8 a% c' C; A2 `      }% }/ w& g1 ^. ^, B4 B
      // Make sure angleB is always greater than angleA
9 M2 N6 W1 |  p" }- k# t: Q4 m' z      // and if not add 2PI so that it is (this also takes
- V: k, }" ~5 O' Z- W9 L8 x      // care of the special case of angleA == angleB,' u% |3 G8 x( H9 M) g& @: ^
      // ie we want a complete circle)2 l+ y4 B; P9 \6 _# D5 R
      if (angleB <= angleA) angleB += 2 * M_PI;) I! I! R5 ^3 q6 q0 Z* I
      angle = angleB - angleA;/ X/ A6 U1 i5 k1 b! j
) R8 J% Y- |, F9 D
      radius = sqrt(aX * aX + aY * aY);7 c4 u. `8 Q4 G. [+ W  E9 X
      length = radius * angle;
$ o. J3 `) s2 d- t, P      int steps, s, step;5 Z% z# N; e* F5 L0 L1 T% E
      steps = (int) ceil(length / curve_section);+ k7 A' f. M. Z9 ]

4 A$ {/ M) X4 G6 U' w7 X/ E      FloatPoint newPoint;
" g$ b2 p/ {- \' b+ c      for (s = 1; s <= steps; s++) {
" c, n$ [3 }/ s, X        step = (code == 3) ? s : steps - s; // Work backwards for CW
; t# ]! |7 _3 J: A; C3 w        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));2 G9 S& ^4 |7 b2 R- b
        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));, I% `5 {* h+ o3 Y) z( d0 G
        set_target(newPoint.x, newPoint.y, fp.z, fp.u);; G1 F4 Y4 H! j0 f" f

* h" P( x4 ^$ w+ I( L        // Need to calculate rate for each section of curve
2 v- {& ?# Y  x. I& Z, ~& l6 g1 `        if (feedrate > 0)9 J* e: ]; f3 t2 ]
          feedrate_micros = calculate_feedrate_delay(feedrate);# l, m/ b# N; p7 Z5 Y$ C2 u
        else
5 k2 K  o8 \( W7 Y          feedrate_micros = getMaxSpeed();9 u) [$ K1 q0 |& B7 {5 H2 y

2 E' I# @& P6 U3 `9 Z        // Make step3 J* b0 z) T. q3 \
        dda_move(feedrate_micros);1 R$ K4 ^; v8 w( f/ n4 W$ z% y
        if(stop_flag) return;
2 B% |. l1 J  J4 F7 U      }3 S0 @" x8 o( D# g. B
! S7 j* S2 K4 t* x# g+ N$ p2 c' ]: c
      break;
6 [$ w8 G: s8 o2 c* d0 N9 @
7 s2 _4 Z" O# o/ [; @. v/ C      //Dwell: M; B% S* [  Y& D) ]8 ]7 B
    case 4:
- J1 u& f1 R4 L) K) k0 F      delay((int)search_string('P', instruction, size));$ U# w/ p, ?( A4 t1 g9 A
      break;: F) X. J& O( V& V- @

; c; {9 m+ N2 q9 w3 L) _" m      //Inches for Units" Y; p* [  j7 Q$ f4 t% `  I' E& g
    case 20:: t: B8 c2 Y1 {5 ~
      x_units = X_STEPS_PER_INCH;6 Y) X4 ]- c+ G, c( y2 Y
      y_units = Y_STEPS_PER_INCH;  ~9 q; v- }2 v( Z0 N
      z_units = Z_STEPS_PER_INCH;
9 }8 P0 h% v0 ]. h2 r* Z0 v      u_units = U_STEPS_PER_INCH;
- I8 w$ D/ h7 f* i' d: L      curve_section = CURVE_SECTION_INCHES;
% A7 O) }' h: w8 C: t# o3 h      calculate_deltas();
, ~% v1 `4 p* K0 \3 _* w% ^      break;
0 s6 S  t6 t$ ~# E7 M9 A- q, W
" ?2 r' f6 S& L6 {8 t2 a( Y      //mm for Units
+ t0 p0 ~3 H1 r7 {  E    case 21:
5 t* V) _5 O4 k7 O1 L( ~, G: B- y      x_units = X_STEPS_PER_MM;$ a9 ~* z/ x4 j# b- S% y! p$ b
      y_units = Y_STEPS_PER_MM;
. o: c( M& V6 ~( ~4 _, `7 V6 k      z_units = Z_STEPS_PER_MM;. r5 h% \4 I. m2 q( P* {
      u_units = U_STEPS_PER_MM;3 I/ u0 O4 U' S5 g3 }0 C
      curve_section = CURVE_SECTION_MM;! p3 w4 h7 i/ C# [" d
      calculate_deltas();
- R. c" Y/ l: b* x      break;9 _2 X7 ]' Q% T( r! a) n7 U
( N5 x$ F0 y0 x; O9 R% F
      //go home.( h5 Y2 A  e  _( r& A, X7 R
    case 28:  ]/ |; ]! P3 D: {& `, r
      set_target(0.0, 0.0, 0.0, 0.0);  {: J5 q/ O2 `* D! q  b7 J5 n
      dda_move(getMaxSpeed());
/ B' V! P/ }2 n# [      if(stop_flag) return;
* x5 ]! H8 g4 g; R# R4 \# O      break;1 ?# Y5 @8 Q0 d5 c+ H. C
( b. K3 @0 d5 ?# S* m2 j& C+ q
      //go home via an intermediate point.. Q$ h! [+ K# c+ G
    case 30:0 @5 S5 j; n) }
      fp.x = search_string('X', instruction, size);
1 Y0 M  G. z* Q& |      fp.y = search_string('Y', instruction, size);
. v( i* m. n( b1 w. L  l      fp.z = search_string('Z', instruction, size);
# S$ h! a% ]8 {! c( o; [+ x      fp.u = search_string('U', instruction, size);, ?) s6 i. B6 M# i
      //set our target.# x+ g$ \0 f- }& H
      if(abs_mode)" i6 D& ^# H9 e6 j
      {
9 ?; b: K" f# F8 K, j# m        if (!has_command('X', instruction, size))5 J0 d& B$ s( j8 Y, c
          fp.x = current_units.x;; t+ @2 }* k1 b% q2 F) L  E9 x
        if (!has_command('Y', instruction, size))9 @/ p. e7 L8 U, i5 q* H5 J
          fp.y = current_units.y;
3 y" i6 Z9 q9 u, V6 I  m3 @        if (!has_command('Z', instruction, size))6 Z/ n. E4 V7 N; {9 S! i2 a- W
          fp.z = current_units.z;
( w4 I$ v8 W7 ?% s6 s5 \/ `. g        if (!has_command('U', instruction, size))3 G* o7 U/ a$ I1 `
          fp.u = current_units.u;% N( R, x- k8 u
        set_target(fp.x, fp.y, fp.z, fp.u);6 g4 W4 A; X/ ~3 B
        ) X9 Q. K! K8 P5 T$ H
      }
3 u: |8 |: H4 q4 K4 b5 d      else
  c" g& L" f/ i9 C- g( y! ]3 S        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );* `- l3 t2 }9 m4 e. H
      
- _6 ~: l+ S" e, [- }9 m      //go there.% E2 |7 S# l9 L6 _$ c: Z" ]* N
      dda_move(getMaxSpeed());
4 y( O0 g( D  c- y3 t, c      if(stop_flag) return;  H" c/ L0 Q" M; X

  R7 H' l" d$ o5 J- O5 g      //go home.
4 G0 f9 X; \6 q4 ^9 F' y7 G      set_target(0.0, 0.0, 0.0, 0.0 );5 e4 j) H; z% ~5 h
     
* u9 d0 W+ e  J$ `& u  |      dda_move(getMaxSpeed());, R+ t6 c7 B, c2 p  y7 k, i8 ~! R: m
      if(stop_flag) return;
- z3 v7 o0 m5 F& b6 D3 h      break;- C8 P" K$ L% @- Y% ?- r
' c# |9 d, e+ B6 ^3 ?0 J  P6 W3 j- W
      //Absolute Positioning
, [" z& s5 z/ ?  I# A2 }    case 90:" K* y0 p7 e9 L  L; H5 g7 e
      abs_mode = true;
4 E2 @1 i) |- K  [8 u; G$ j      break;$ _' B7 r3 b& e/ T( b4 L& a8 q
; ?* x$ w" t( X1 S; t! Q
      //Incremental Positioning
) r* d/ k0 {7 ~) n, F9 f" h  L    case 91:/ @1 P2 x/ O8 j# W
      abs_mode = false;' S: V) y. [0 E* A8 q+ H/ x
      break;! ]$ B# Z2 r4 r$ T6 \8 t( f7 O

: f$ k3 b  G4 t& E      //Set as home
1 |$ j3 W' v1 S! `, B    case 92:
) M" B6 r6 t6 ^+ w5 e     
7 U, h6 o$ n1 [( h5 ]      set_position(0.0, 0.0, 0.0, 0.0 );& x9 w, `6 t1 H. B
      / R+ P: x4 V+ \' {9 J
      break;
' u: f: ^, u7 d5 K2 M# B: e  h' B% `/ r0 v9 I! M
      /*& T2 ]! `' |9 E1 Z; F- F  e( S
                        //Inverse Time Feed Mode
! q! a! O# m) [  V                               case 93:0 _$ m0 e' E/ x$ R# N  T
       . s% ^' S* R9 K0 R" T8 d
                               break;  //TODO: add this
- h4 T, W. o4 t4 h- w       / r7 w2 x2 j$ t* [7 A: D# B, r) h
                               //Feed per Minute Mode
0 [( h4 J# x% w# b7 Y2 d                               case 94:
1 l4 k) A- Z  z; V1 D       * ^- Q2 }1 e+ Q. `) j. z2 b4 D
                               break;  //TODO: add this
: O2 |6 g& Z# A, o' Y# w3 u       */- q. i# v$ a/ s  C
( h6 F7 B" k3 Y+ g& K; E
    default:- X$ a& v( `1 d/ \) e6 l
      SerialUSB.print("huh? G");
9 \# n7 x/ c/ Q9 J: }" r      SerialUSB.println(code,DEC);4 y- V6 H5 O( l
    }
% L! y/ b/ s* H/ a) u7 p0 Q; `  }0 p9 m6 h2 I0 E/ e5 C+ Y2 p

. @* u  [( A# a' p+ p( e  o  //find us an m code.
0 a6 D; x& f# E! e- b. P. B  if (has_command('M', instruction, size))- t4 R, D* H, g: h* ?5 k+ b1 ~0 z# S
  {8 D- ~) d7 @$ ?! z/ ~
    code = search_string('M', instruction, size);
0 `3 k4 {" b  |0 u    switch (code)
- `$ m  E: X8 i. T% h) X! O    {
$ g  d4 q# E  I2 r      //TODO: this is a bug because search_string returns 0.  gotta fix that.. D) |/ A9 y4 l9 k5 H$ z# k: M
    case 0:
7 N& Z# ~5 e8 u& [      true;% p- Z+ }8 o: o
      break;
3 g% m9 K3 F1 D: @/ w. {5 |# Q' h3 o$ v  ^5 l+ G* o& c- d
    case 100:5 ^+ Q* H6 q( m# N6 T8 `
      break;1 [. B8 f2 h  l* s/ H6 v  z
/ C  v: I! F8 ?* Y8 ?/ ?
      // fire camera relay/ [/ a7 t1 ]7 V5 r" v. k+ G1 O
    case 101:
# r0 i/ E6 J  C2 ]( r      camera_shutter1();/ V5 d/ Y) g% }9 A
      break;
$ }2 G: O/ S$ u% z7 }8 }( }, q- i/ P
      // fire camera relay29 g  B1 _. z% U8 K6 A
    case 102:
/ u# t* L- h' \* l+ ^" G( W  C      camera_shutter2();
+ r7 \% j: w% g7 k      break;$ g: L; r1 v) _0 ~3 n. m
/*
1 j2 |$ ]) E3 ?8 X6 ~  _9 H      // turn aux 1 relay on
$ N9 U& v6 u7 a! \    case 103:' i0 t* H- o; x- T4 k- y! {
      camera_aux1_on();- ]+ P8 H: P3 {9 z7 N
      break;
' l8 O7 K: b1 A0 `! e: z6 n6 L! Z" q9 W8 ]  D6 u2 ^
      // turn aux 1 relay off
, f/ @  g) q2 K8 @8 V# g    case 104:
$ W- x& K7 y5 r" H' J7 H2 V+ @      camera_aux1_off();
8 i$ j+ J$ L$ ~5 u      break;& _( K7 X+ I# M8 @
" r( c4 n7 a) _2 d% C2 d& b2 K3 e
      // turn aux 2 relay on3 L. e9 @8 t/ K3 o) Z
    case 105:
9 Q' m& u+ W7 G: S% a% ]' H      camera_aux2_on();
1 f9 t4 [& o: M+ y% I* F: l; O      break;
% P. m$ }4 D2 J9 P1 \; t8 K  c6 q) g9 d
      // turn aux 2 relay off
6 ?( Z4 g* D# w$ Z    case 106:% D6 L" f5 p: [
      camera_aux2_off();
3 j' o6 ^% w5 Y& P      break;
& k( |% N. L8 q4 X  J8 h5 Z, P+ X/ p
      // turn aux 3 relay on
; E5 R: i9 c- r" U* B8 @( ?+ V    case 107:. M8 B* K0 u) B! \/ L: ~
      camera_aux3_on();+ e  T  f: {- C+ N0 {/ K- d  ]& w! N
      break;0 F4 A( P6 B; V+ e/ @7 L
; H0 `/ O9 ^' }0 r; r+ c7 K7 K$ y
      // turn aux 3 relay off% \1 M/ a4 G' r7 R5 ~2 C
    case 108:
; [5 C. O; e0 v, o  ]% R/ k5 o) [3 O/ \      camera_aux3_off();3 D$ z! i3 A+ g& P" o- _) U
      break;# Z2 L0 |2 B/ `% e0 l- t# g

; G( p7 B) d* e( g      // turn aux 4 relay on
- N( z: B4 U9 M7 {2 H6 H5 c    case 109:, g1 R* |+ w+ C' f) E/ T
      camera_aux4_on();
- t% |! z7 X$ i2 l      break;
! ^+ C9 S, H9 o) i% a: L9 a4 n# E6 h" Y; V' s& z# N
      // turn aux 4 relay off2 o7 C' y! c0 F* B+ j
    case 110:
# T0 N9 G/ k1 {) ?  P! F3 U3 S      camera_aux4_off();
8 ?& H" X1 i" I7 v6 {      break;
/ S) r( H5 B% ~*/: _; W" u# a7 u3 H( r& y: P
    default:6 B7 l' h3 S& h/ w/ S+ n

6 a% g; F4 E$ t  f      SerialUSB.print("Huh? M");
5 T5 @4 V+ e- p# q* \      SerialUSB.println(code);, }0 _' H) r! O  ~" D4 W# \5 g
    }# s4 w6 W/ Y/ G
  }
2 X  Y- C* Z) k* i' v5 x7 q3 p9 r+ A3 V! j2 s$ m1 K; i
  //tell our host we're done.
9 p" c# }" [( c% s  SerialUSB.print(byte(78));, R/ d& n2 p6 t, m" \5 U1 a

1 G2 B) p+ k# J6 V: h) t}3 L3 K( a5 I5 X5 S$ K
. x+ Z0 o0 R+ Y' i, l/ d  v+ L
//look for the number that appears after the char key and return it1 O0 [6 t. S" |; P8 j. z
double search_string(char key, char instruction[], int string_size)$ ]" Y# w+ @; P, v& h5 k+ b. @3 a
{5 P5 H& A# R" r. }
  char temp[10] = "         ";
# N+ ?7 h8 Y# N  for (byte i=0; i<string_size; i++)  \* H$ @' a: M. x% X# t
  {
" ]3 \9 l( a0 W9 D" \    if (instruction[i] == key)1 u0 a& V2 e, s8 n) |# ^( I# Y7 f
    {, K& {: K( m& s! b3 K# v; g
      i++;      9 Y1 O- n; z  |, X+ R. Q/ i
      int k = 0;
. \5 f' A5 P7 L+ O2 X  x9 J      while (i < string_size && k < 10): V1 Y+ x* a9 S1 F# V4 M; w8 S
      {
: g  q! e; P! A6 j5 |  K9 Q        if (instruction[i] == 0 || instruction[i] == ' '); K8 F4 A1 N7 ^: K/ S- f" {5 m. O
          break;- z2 |( _" P' B  V$ W$ `. |5 e

) R) _4 Z  G+ y% M        temp[k] = instruction[i];6 h  v8 Q; s4 X% |% z' L& @
        i++;) L! S# G2 j; [1 q4 u1 i9 ~
        k++;" N( m) [! Z1 i3 b' G, H2 \1 A) x
      }. }& E8 e8 P3 p/ }% |
      return strtod(temp, NULL);
, Q. J! \$ M# ^  K! G$ M% F    }. b2 E0 a3 b$ D; [+ m. W% q8 p
  }
6 b( @3 @3 A9 g+ s6 {- G+ Q! x" `6 _+ F* M( \# G' p" F, s
  return 0;
+ l6 h) `( i$ h: J8 v- k}
) A# z7 Z8 P- j+ F) A  `# b7 p, [
6 C+ y# |; p* f8 R//look for the command if it exists.0 }' x0 t2 E* w4 d1 R% t5 F! A
bool has_command(char key, char instruction[], int string_size)
9 c" c0 a3 s% _{
0 l, k- }; `0 J1 g- J  for (byte i=0; i<string_size; i++)- C, y1 J+ _! `9 n6 d4 V5 N3 i
  {
9 J6 D2 M8 ?! r    if (instruction[i] == key){: ^& C5 J: [/ J; L' g6 e( \5 S! ?
$ u9 X6 u9 g1 e5 s4 t' ^( D
      return true;
8 O, q$ B1 j: I    }: Z: x) D* `' G. a5 q9 t0 C
  }
& F2 n+ T  _% ^; n8 Q6 L. E1 A- A; e
  return false;+ J7 f& B% m, j7 X
}
& v- r- X  ?4 e
8 |1 k' \# q) J' n7 U4 S  T
$ }: B  l% r: A! Q# b  ?' v, g$ H8 d! _4 L0 l" U
8#
 樓主| 發(fā)表于 2014-5-10 09:55:37 | 只看該作者

  U- Y& A/ ~: E4 f//init our variables
- a2 I) C7 K# ~/ blong max_delta;
% W7 u& Q, s; F3 R  |4 c) clong x_counter;
' S5 Z. Z. `- l+ u/ b- Tlong y_counter;) w& L0 `6 N7 U$ Z7 ^( Y
long z_counter;  C/ [; q( H# X& `2 h1 H
long u_counter;
: q# A* S, w; W+ Q' ?( p3 f; Vlong x_pos = 0; // x position in terms of absoloute motor stepps; P* f) |1 x: p2 g, R
long y_pos = 0; // y position in terms of absoloute motor stepps$ ?3 n' I# `- E9 m# h/ q2 c
long z_pos = 0; // z position in terms of absoloute motor stepps
6 L! y4 n2 ]/ I- Along u_pos = 0; // U position in terms of absoloute motor stepps
& C' a) N) k9 \- f/ w+ `6 N& H8 R0 M: {; S
bool x_can_step;
4 @, a. i; ~. e' G* i2 {$ f: Vbool y_can_step;
* c8 x1 \/ \  U) s2 M) I- j: b* d& @bool z_can_step;
3 L6 [6 c; m* N9 V! a4 z3 _" \bool u_can_step;- R* z; r' H- K$ v
int milli_delay;
/ e9 j+ y$ `$ Y0 t
, h  |6 Y0 B' [- c) Zvoid init_steppers()" K6 }: g6 J" H& b- o8 U1 E4 f
{
8 X9 [4 Q  K9 m% T8 B9 P5 C  //turn them off to start.' O9 F9 i. A) G# _
  disable_steppers();. n* i2 T& g4 o
$ v$ }/ O3 J4 k- [* I( |4 T# |1 c, q
  //init our points.4 b% M  ?, F) O( ?% O4 F
  current_units.x = 0.0;2 l9 D5 G8 z1 ~( a* a$ I
  current_units.y = 0.0;
. d% Z7 l! |' a6 u  current_units.z = 0.0;1 o5 o6 h5 t$ z
  current_units.u = 0.0;% {9 ]+ |  F. h) L0 ~& @2 Y+ `
  target_units.x = 0.0;
3 H, j) P9 }: s  target_units.y = 0.0;
, e" Z9 l5 P# M& M5 A+ i4 k' L  target_units.z = 0.0;
) q( @" e! n- O4 r  target_units.u = 0.0;7 L0 T% L0 z8 z: l& f1 {
  
# C( g; M' f* Z1 `* T: d- F8 p3 \: x, _# G/ `
  pinMode(X_STEP_PIN, OUTPUT);
' {3 i5 s5 t) O! C2 {' L) A  pinMode(X_DIR_PIN, OUTPUT);3 V" T0 I5 q0 K
  pinMode(X_ENABLE_PIN, OUTPUT);. _. V1 R4 X. i* d" K2 L7 W
  pinMode(X_MIN_PIN, INPUT);
/ t/ o& `- B- b) t  pinMode(X_MAX_PIN, INPUT);! q' S* l5 i' d' X) F

. _2 \9 z" ]$ M3 J  pinMode(Y_STEP_PIN, OUTPUT);
2 W0 |: t% X8 U$ L  pinMode(Y_DIR_PIN, OUTPUT);
: h$ ?+ _% E( O$ v2 k' c  pinMode(Y_ENABLE_PIN, OUTPUT);
, y: J2 Q* S2 _  pinMode(Y_MIN_PIN, INPUT);
1 P+ i) i% N2 ?3 l  pinMode(Y_MAX_PIN, INPUT);- z" z! N8 `7 C% E% e9 a- \2 `; y9 Z
' c7 R; n. f6 b' E, G
  pinMode(Z_STEP_PIN, OUTPUT);
) n: Q, I" U# A7 q+ l; q6 t+ H  pinMode(Z_DIR_PIN, OUTPUT);
) Z* n2 f! M3 @# K+ |: f/ j  pinMode(Z_ENABLE_PIN, OUTPUT);2 F: T* M" G1 M
  pinMode(Z_MIN_PIN, INPUT);
& [$ k+ J6 i8 T! e& P' ?  pinMode(Z_MAX_PIN, INPUT);
! M- Z- G0 P/ {6 R5 v5 Y  b5 V2 I2 H7 X# o
  pinMode(U_STEP_PIN, OUTPUT);. }8 {- K# ^% ?$ N, w% R" z
  pinMode(U_DIR_PIN, OUTPUT);
, a; z. x1 [& }" s! v  pinMode(U_ENABLE_PIN, OUTPUT);+ w+ t/ ^0 P# @# v% r# \# X/ ~1 M% z  y5 m
  pinMode(U_MIN_PIN, INPUT);- }& Y$ C" L* ]0 O' n, J9 }
  pinMode(U_MAX_PIN, INPUT);
+ d8 t) A. g7 q  N$ y  //figure our stuff.# S# [: r+ c9 A; P
  calculate_deltas();8 w- h3 `+ g0 [! b6 l
}
" }& [6 Y( J, f' t8 L! q# W! B
' {* k5 S3 f3 D! |void dda_move(long micro_delay)
  A2 t) `/ x/ M/ ^" ~{8 `" N  B7 V8 I! g3 `
  //enable our steppers0 s8 P; Z0 \/ ?( ~" M0 h& O4 s+ i4 a
  digitalWrite(X_ENABLE_PIN, HIGH);3 P' Q2 ^. F5 w8 E, b  n' o
  digitalWrite(Y_ENABLE_PIN, HIGH);
8 S: }6 F. Q' C) T) @- ]& Z  digitalWrite(Z_ENABLE_PIN, HIGH);
- A$ Q9 `- Y' p3 K  digitalWrite(U_ENABLE_PIN, HIGH);  G6 a, \) F  ]; z& _5 d9 ^/ R# L  p
  //figure out our deltas2 v; ]6 F2 M. X4 Q1 A
  max_delta = max(delta_steps.x, delta_steps.y);
; B; ~- [! R& \: o# T6 L  max_delta = max(delta_steps.z, max_delta);
2 {9 x) \+ h- [! c  N  max_delta = max(delta_steps.u, max_delta);) W, e- j# g' k0 V# D
  //init stuff.
! d* `3 V  r" d$ R8 Y1 t* e* U# b  long x_counter = -max_delta/2;
+ v4 |, V8 {) K. Y% Q  D  long y_counter = -max_delta/2;
7 r9 [) K7 X; |3 F  long z_counter = -max_delta/2;
% C8 A4 Q; h# Q1 c  long u_counter = -max_delta/2;" l$ [) U+ N* H
2 \3 c  c2 l; W) I0 l9 i: b# q
  //our step flags
3 w+ C# v) `2 k7 o  bool x_can_step = 0;) U1 m6 y3 q# u' j1 ^
  bool y_can_step = 0;
$ B  G1 z; O# F/ C  bool z_can_step = 0;
, i8 E! t$ {9 ^+ }7 d% S! S, Q$ Y  bool u_can_step = 0;' b+ m4 f$ U! |( s' l2 {$ ~

' \3 c$ [  \8 c3 W8 m  if (micro_delay >= 16383)
+ E. b5 A# `3 R: J# T    milli_delay = micro_delay / 1000;
5 v! @% [+ I7 y- L* J( z' n  else$ ~! T$ P. l  r: z$ }- `8 A' h) M
    milli_delay = 0;
& V( H; K' W! z6 t# @1 }3 X' \' c% C5 t7 [0 H5 Y

. J; V/ s& `, B! h+ i. T+ e  //do our DDA line!7 _# e$ W, V  A3 G
8 ?* |- J6 d3 o% d0 m
  do! _" p; [) ~8 M
  {
# Z  K8 m, J2 n, _    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;3 U  x7 d0 i- t3 T( Y0 R/ ^
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
% |' L. y+ n5 o    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);' m( @- o/ ?& ]( [# I
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);6 n) N. b0 h# U( ]1 ^
    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);% F; s5 B0 T- V7 P2 s+ `
    if (x_can_step)
& B. N+ ^% D5 j. q: p    {
1 \( ?. B3 d3 M! @      x_counter += delta_steps.x;
# I; j$ j- E1 |6 P5 [$ F/ c
' w" D2 c4 h+ M: O* o      if (x_counter > 0)
9 E) c8 {! s) ~, G      {% q# o, W$ f2 d, u( j- B: V
        do_step(X_STEP_PIN);% B/ s+ G/ r6 ~, j, T. l
        x_counter -= max_delta;$ c2 s8 {% m6 d# d
        if (x_direction)
! m; q! C8 f+ N( Q         { current_steps.x++; x_pos++; }1 @0 ?, `# Q0 `6 h& @. K
         
  i( g9 @2 T. Z% S9 I. [        else- {  `4 @- U7 T" V' f0 r% H
          { current_steps.x--; x_pos--; }
' [( S) p  o: `) ]& h0 r, W         6 O5 [. R% J3 W; z0 K1 ^/ v1 C
      }0 E3 {$ x3 v/ k& E' Z/ E: @4 e
    }2 t% c9 j+ Q% {, [! z1 u
    if (y_can_step)4 }& v" i) @% G1 S
    {; D5 y" l; ]. ~$ x
      y_counter += delta_steps.y;
4 f, N# Y. o$ m  p/ Y: a8 i
# F/ |' @" W( K- ~, B1 ^. e" i, j      if (y_counter > 0)
8 Y; [; X$ O% C  z      {0 X* M: }4 T$ k3 ^* |- D( Q
        do_step(Y_STEP_PIN);; k( t7 Q" X5 n2 \+ i
        y_counter -= max_delta;
( [$ |+ Y5 {% a) y3 v# A! \% q. _/ z# G* v" K4 i- q) @; K
        if (y_direction)
$ e! }( ]8 |9 {8 k0 e' g+ A' F        { current_steps.y++; y_pos++; }
; S% d+ d% L; H, a7 @       2 G% {$ b7 f4 I+ ]# j2 p: K4 H
        else
  d( r* L4 W, Z        { current_steps.y--; y_pos--; }1 i7 j0 v. d4 }% d+ M
        
+ Z7 I$ e- ~4 ]      }
4 z$ \$ i' |! K: z1 r! }8 D    }
, H* i+ G% `- {! T! I
9 G. t( y, h! w# K0 ?, v  [    if (z_can_step): Z' F1 f- L/ e. l1 R' [
    {) h" ]: W" M: Y3 l' ^, ]
      z_counter += delta_steps.z;" i4 Q$ T  A; {" n. y8 I

8 P  k2 r' T" P# R      if (z_counter > 0)
5 w: d8 Y( A/ {8 W3 Z6 |' L+ l      {7 G) ?4 b* V/ C% e
        do_step(Z_STEP_PIN);
! B& r5 K0 ~6 y" a+ _% K0 u+ I8 u        z_counter -= max_delta;
6 N/ \+ e7 H2 u. s% }! k
! v' q1 u' F* k9 e        if (z_direction)$ `4 ]1 E8 X8 v3 B' O7 r" K
        { current_steps.z++; z_pos++; }
- p, ]$ O' Q  z( u9 d$ ^+ s        ; R, Z* ]2 G: Q
        else- B! e$ h! \  d) [! C
        { current_steps.z--; z_pos--; }# }( R- \: c2 H* l, n
        
3 s) S. L  b; E/ u* f5 c' s3 W* G      }
% W* T! Q' l) z    }
- [' i" X. }* K   
% j& A! a! G% @( |    if (u_can_step)/ [7 O( e9 k# l- A! w! W
    {8 t( F( |/ @% ^+ p: ]& k
      u_counter += delta_steps.u;" ?, m( q2 C9 d: `
- s. p, a0 X* G8 R9 a) ^; w2 G# L
      if (u_counter > 0)3 o2 s! s/ k- G2 Y$ f0 a' r$ r7 A
      {
8 V: M+ _1 ?1 ]- z        do_step(U_STEP_PIN);/ a7 {& q: Z/ D8 y7 b- q# T
        u_counter -= max_delta;
& t9 v' E# J/ {# \
5 N  l: J0 n* ~8 z" r1 }5 U$ |        if (u_direction)7 ?! j3 B9 _! j
        { current_steps.u++; u_pos++; }
1 Q5 \' \/ V) k/ i- X' J         
+ M/ b2 g/ g; {' a' T  W% O        else
2 T/ v% y& h& W) f$ }          { current_steps.u--; u_pos--; }
$ f$ j1 e0 z: N; `, a3 ~! p         
8 x7 x; U. ^, ?5 s) ]3 M# R      }
( B( B: s" s# @    }
' i7 b6 L- ~( k% s6 z! b    //wait for next step.
% @; _# ~1 c# P4 T4 {    if (milli_delay > 0){, g. a! F$ n$ y5 U* ]
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();) g2 Q/ S8 W( @
      delay(milli_delay);
7 U3 l$ @% ?$ v  B# n, `    }               
, ?) E' m( A( I# |    else{6 R+ ~+ m: C4 E8 `. s' S2 }9 b
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
0 F0 d! @2 f$ `% A- c6 J% q      if(micro_delay>0)delayMicroseconds(micro_delay);& b  x4 ^1 {" p- R" o
    }
' s/ Z; Z+ J% h6 P    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
9 n$ {! V  Z: F, }9 c  }
# s/ E; }+ M+ d6 o" s1 H3 k  while (x_can_step || y_can_step || z_can_step || u_can_step);  L5 f( ]7 }, l
6 r* h2 {7 n! z
2 j, ?, }# A; W7 q' R! x9 K% x
  //set our points to be the same7 _) }' B% h( ^  e2 V3 h) p0 H9 R
  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
7 b) M, _* j: T. h8 X7 {  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;' b0 B& }1 A- Q& g$ y+ M4 t
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
# _' j2 w, d! G4 U( Q( f# k4 X2 @" k$ \  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
& ]8 v3 @" r2 x. y: q& t  , b7 {: J& m( ]  J
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );7 u7 o* j3 o4 L
; m/ z( e7 a9 O, Y6 g7 @
  long x_pos = 0; // x position in terms of absoloute motor stepps
$ R+ H3 P, h6 Y8 F/ b, n" a  long y_pos = 0; // y position in terms of absoloute motor stepps
9 q+ c+ y# q4 B( ]  long z_pos = 0; // z position in terms of absoloute motor stepps
% ]' `, q+ }, Y/ v) ]  long u_pos = 0; // u position in terms of absoloute motor stepps( b- v0 ~" O' E
  calculate_deltas();
% S9 ~+ J' [9 _& b( k/ m  # i# E: h( R4 A0 U/ e
}
% N5 [6 y1 L5 e6 w+ j$ v- t6 ~+ w9 V2 g9 [
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)
  f/ [. @6 p5 m$ H) k{" L8 C# y: p4 `* X4 k% B: d5 \0 Z
  //stop us if we're on target  _! A( L' |6 W4 u
  if (target == current)* L5 U) [( E4 o- ?' J
    return false;
1 G' w0 _  c) P1 y$ [" W! D  //stop us if we're at home and still going % d! ]; e7 q" F$ v. n3 t
  else if (read_switch(min_pin) && !direction)/ N) z  G3 A. Z. S; {- T
    return false;5 A! ^# F* @& L- h
  //stop us if we're at max and still going4 M* G. s5 `" h# \, U+ u
  else if (read_switch(max_pin) && direction)( z1 x% ~/ j$ @6 f+ m8 r. I
    return false;
0 d& y6 k% T1 l; O, D9 ^# g5 k3 F& p& V+ ?9 _& g$ P
  //default to being able to step
3 @" m) ]! |: {! q% v4 t2 `  return true;
8 {* i+ _% @2 ?" d) g4 Y; J5 b: {/ H' i}. Y+ B2 \$ \( P% `$ g5 a

' v  \8 f3 O1 S) ~) T9 X4 B! T8 \( ?; qvoid do_step(byte step_pin)
2 i+ n5 H% T% X; @) M3 l1 N1 ?+ u! u{
" T4 w0 p) v. N, ^- h, s5 [  digitalWrite(step_pin, HIGH);6 ]8 x* E. ]8 I2 J+ q0 ^) X7 {
  //delayMicroseconds(1);
0 T6 n4 O% a. H" D9 F% o  digitalWrite(step_pin, LOW);5 ?2 ^  M& c- u
}& \/ w4 Q5 ?; w8 R: D
7 G" T# \. d% i* r0 ~! W
bool read_switch(byte pin)+ O; G( {0 l0 Z" L8 o, y& R
{
4 s$ w  ~  S8 |  //dual read as crude debounce( M% I/ Q  S- E0 p/ }, ~  M% V2 L

) ]3 }* R& p, j; B  if ( SENSORS_INVERTING )
$ r, R' @0 Q4 D    return !digitalRead(pin) && !digitalRead(pin);
9 E& q: \, `0 k: _& |  else
' T1 ~3 `9 b  `# }    return digitalRead(pin) && digitalRead(pin);7 ?8 V6 c9 i' q* I- d8 {. [6 X
}! k2 G8 d4 F  k! S* j# ]

( t: M6 h! y! R& ulong to_steps(float steps_per_unit, float units)
' P& C, a4 L9 s# U& q4 K' U{: T* ?; {* k! D0 K
  return steps_per_unit * units;% Z" g& b% V) V7 Q3 d" |
}
  y" P$ }- V9 d9 G0 W8 s/ C. Z
2 j: ]# |2 {* evoid set_target(float x, float y, float z, float u)
( X0 |" P9 N: s$ a) f# G" `{. _( u8 N( |* h2 X0 F/ F
  target_units.x = x;: c0 S; R3 N' e+ s* |
  target_units.y = y;, e; _5 i3 [$ r3 G$ N6 E6 u
  target_units.z = z;$ |+ M+ N# U7 I# W" l
  target_units.u = u;; U$ \; p/ `; K" D7 z+ ~
  calculate_deltas();( W; l& `7 v7 a9 T9 E% j( Z
}
; ?# k: R! c6 B7 ^$ b  C' i2 e0 _- O8 o
void set_position(float x, float y, float z, float u)
6 x! J4 G! a. e{- f4 b  t4 Y+ K# K
  current_units.x = x;! o) U9 K& g' C( r% `- U$ Y
  current_units.y = y;7 A6 p! Y3 i. R
  current_units.z = z;% A5 y, Z. K7 T
  current_units.u = u;4 L* T/ S6 r. ?$ b
  calculate_deltas();
, c0 W1 C5 b$ a}
! h3 i. k' ?: D: c* w
& `2 d4 N! N" O( ^void calculate_deltas()9 U% W) E3 f$ O! n0 v& Y9 P
{
- e& w2 t0 ]6 j, d  //figure our deltas.2 w4 H, ~2 V6 t8 n
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
& {+ ^# Q- h2 I  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);
7 ^4 Z, K) T8 ~9 P  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
! n1 V/ I/ h) s9 y3 p+ G  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);3 v1 `5 E, L, K% u
: \% y; f: x- @7 Z2 k
  //set our steps current, target, and delta
+ d0 X# O; k* K8 B  current_steps.x = to_steps(x_units, current_units.x);5 A" p( b8 t& V+ Q1 Y+ B
  current_steps.y = to_steps(y_units, current_units.y);
/ W( b# J- U& D4 [* }* a  current_steps.z = to_steps(z_units, current_units.z);! p; m1 p3 \# i! U& q
  current_steps.u = to_steps(u_units, current_units.u);" @, r1 \( Z; s1 p0 W& Y
' u$ E/ T' R3 D) y! |1 E" L. p
  target_steps.x = to_steps(x_units, target_units.x);
) h& B' y6 ~- z  target_steps.y = to_steps(y_units, target_units.y);
* p( S1 [0 r" U( I  target_steps.z = to_steps(z_units, target_units.z);
6 h) z1 |0 e4 j  target_steps.u = to_steps(u_units, target_units.u);
9 T; s8 G0 Y$ r- j0 n& P6 q4 H% j! _; g6 q  \
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
" c$ Y. ~, U- ^  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
9 U8 D9 `! v9 C' g  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);6 d- e7 X3 G' x0 l' ~+ h2 t
  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
4 O! A& ^8 k3 U) J/ W' U5 O  I
7 d" @" ~" N" b" {! a; \7 Y
1 W3 O9 g( t# Z0 @& K0 P' y  |4 h; i4 p5 g
  //what is our direction+ \* T6 B* ^7 \  i
  x_direction = (target_units.x >= current_units.x);+ E( [) h: ?9 u: t+ A
  y_direction = (target_units.y >= current_units.y);4 d8 N4 k8 k) d
  z_direction = (target_units.z >= current_units.z);
- C5 l2 e/ }7 P( w  u_direction = (target_units.u >= current_units.u);
7 A( w' T9 Z, i- u" l: J4 r' J3 [
1 C5 I1 y0 p( F* M- ]' K& N1 t/ M  //set our direction pins as well* _  N2 G( m9 V; B  X, u, B& N1 Y
  digitalWrite(X_DIR_PIN,x_direction);
0 x1 h+ P9 k; {8 \  digitalWrite(Y_DIR_PIN,y_direction);
1 i* K% ]! v1 z/ S# I  digitalWrite(Z_DIR_PIN,z_direction);
  a% U) g! ?1 ]6 C3 H/ H! w  digitalWrite(U_DIR_PIN,u_direction); ! l% O2 e" |/ w- j5 G6 y
# K) ~+ D" k8 x  Q2 @  I
  //繪制LCD
! ?+ I+ k/ K& L. \. Q( R% O  LCD_DRAW();
' z  O  q0 T1 o4 ^8 D
* ?, v# G" F1 a8 f3 F}
* `+ U1 O5 M$ `* Z) V& b
* z! p& y. [" O0 o& c
5 P8 m$ d1 v/ ^+ ilong calculate_feedrate_delay(float feedrate)
& c% j. X3 \: V& x. i{. p0 d! E$ p+ s9 p7 q; i
  //how long is our line length?) M5 }0 d6 c/ i$ z7 j# u
  float distance = sqrt(delta_units.x*delta_units.x + delta_units.y*delta_units.y + delta_units.z*delta_units.z + delta_units.u*delta_units.u);
+ k1 e8 Z& I) Z0 V; d) R  long master_steps = 0;
) o" T" i: F) k5 G1 f5 F* ]0 {" j$ y; N- k' T0 C
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;" D2 ~# W  D! i" t  |
master_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
8 z/ ^$ X5 B2 j7 p6 j8 r* {master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
& j7 q: _  p  N0 E: u" ]  i* I  k: U3 ?7 [6 r2 }
2 G8 v/ d4 U0 Z" d2 t
' s+ f% V& B$ G1 G7 X

* u$ f% P/ s! W8 ]5 [  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.
5 d! t% D; X6 B" @  //the formula has been condensed to save space.  here it is in english:
  d3 V+ Y4 b* Y1 [3 h  // distance / feedrate * 60000000.0 = move duration in microseconds
1 Y/ N4 }0 _7 _3 _- R0 G  // move duration / master_steps = time between steps for master axis.* n/ s& J( _7 i, y) }7 ]' A
return ((distance * 6000000.0) / feedrate) / master_steps;& }  K, J$ D4 M2 G, ?5 |
/ M, t( U0 r9 f. O) T: Z  R
}
6 E! x3 I) X7 q3 k! {. y
7 z- U! w; ]1 ^" n7 |long getMaxSpeed()6 ~5 L# M( X0 u# L
{
; E. ^5 m& P1 }4 y, y! d- x% k% D if (delta_steps.z > 0 || delta_steps.u > 0 )
* h0 E, X1 \4 L2 n& f: W/ v    return calculate_feedrate_delay(FAST_Z_FEEDRATE);
8 Y: O, H5 z1 u5 }' R  else6 V2 x' d. T5 |, R7 x7 ~
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);
: |8 h; ?/ B9 O  o: S5 b}
1 d' v" D5 R& n  r
) p4 {& O# M, `& _; tvoid disable_steppers()
* b7 N2 Z. J- R0 W{
6 z( H: O( j- H; D3 S! M$ C6 ]; U9 [  //enable our steppers
# h- W( M( [: ]- M( p0 G, R2 @  digitalWrite(X_ENABLE_PIN, LOW);! w/ Y- H( u$ K, j/ c2 q
  digitalWrite(Y_ENABLE_PIN, LOW);" ~8 A3 j1 c! v6 w  R
  digitalWrite(Z_ENABLE_PIN, LOW);* Q% G8 k7 y( z0 c* o
  digitalWrite(U_ENABLE_PIN, LOW);; N+ @  e5 Y' a
}
  [, ^8 P* W7 R! `# L. L' I) P' J
; B1 t6 |& S3 u$ p- s; I6 b
# y5 Y( ?9 P; Y9 I! F: }//繪制LCD
/ Y1 j: @5 v( X$ x8 @1 h/ t6 p//unsigned int DRAWCount=0; 4 K7 T& J$ q# Y7 D$ ~6 {  O
void LCD_DRAW()7 }* d, j. e0 v, H4 L4 _
{
# k4 K, ]2 L6 C( p* ~: R    lcd.clear(); - h$ a: R3 ]1 t$ E0 ~
    lcd.setCursor(0, 0);2 a) d2 g, L0 ~* t$ r
    lcd.print("    X=");) i: y# o" r! G* Q: Q
    lcd.print(current_units.x);
+ `* V  d: d( f: l( N3 z, e% M: c    lcd.setCursor(0, 1);
' E- l+ `9 x  g, m" `3 m, H    lcd.print("    Y=");
  U& r" l2 m+ o8 h# o" {! e  d    lcd.print(current_units.y);
( l* `2 Z5 }; a8 ~4 r* Y6 C, w) S    lcd.setCursor(0, 2);. s7 J' f$ j. m6 F$ b
    lcd.print("Z=");
' p  x, O/ Y8 @: M8 l    lcd.print(current_units.z);( M# \& W+ a( o; |; S  V. [
    lcd.setCursor(0, 3);
6 H4 x) T  x5 |. Z+ ]6 O    lcd.print("U=");- o9 m1 \& Z- }# U# H
    lcd.print(current_units.u);
: E) g; b$ ]' k. ?. J  i5 a  }" u* J5 g' C3 X3 W" D
// else if (DRAWCount>=30)DRAWCount=0;8 W% p- p' v4 \9 \' V
//  else DRAWCount++;
: ~1 Z) y. U! d9 T3 w void SPEEN()
- J2 g, T4 ]  ] {
. F% B$ D0 [6 K: h4 t  m, n' l" T2 W( h   delayMicroseconds(analogRead(SPEEN_CTL)+1);
+ ]8 R: l7 |) y$ `4 n }
4 n. V! ]5 y2 B0 Q
2 z! x  K, A+ M2 F3 E$ E& {- L
, K1 w; K: N0 m //delayMicroseconds(analogRead(SPEEN_CTL)+1);7 B* Q( P4 ~# W0 \
//if (digitalRead(BUTTON_SP_EN)) SPEEN();
5 w% }) {, L3 V9 x: z: W; p" {* l9 l# f. t( w& j
& V/ ^* f  F( Q$ u8 V" A
9#
 樓主| 發(fā)表于 2014-5-10 09:58:04 | 只看該作者
基本原代碼的大概是這樣,我是斷斷續(xù)續(xù)搗鼓的,玩到最后版的,后面我會找出來先測試下過,再貼上:)
10#
發(fā)表于 2014-5-10 10:48:17 | 只看該作者
請樓主發(fā)一份給我,感謝!178354773@qq.com
您需要登錄后才可以回帖 登錄 | 注冊會員

本版積分規(guī)則

Archiver|手機版|小黑屋|機械社區(qū) ( 京ICP備10217105號-1,京ICP證050210號,浙公網(wǎng)安備33038202004372號 )

GMT+8, 2025-9-25 17:09 , Processed in 0.112993 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回復(fù) 返回頂部 返回列表