機械社區(qū)

 找回密碼
 注冊會員

QQ登錄

只需一步,快速開始

搜索
查看: 23530|回復: 14
打印 上一主題 下一主題

運動控制入門篇GCode_Interpreter

[復制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2014-5-10 09:39:53 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 xmdesign 于 2014-5-10 09:42 編輯 4 H( r  y/ [& @

9 z& I* z- J" P首先聲明6 ?* D# Y0 Y3 I/ [+ V+ n# j9 M
我不是專家,業(yè)余搗鼓: I8 v6 V& _; y0 d" k
源代碼首先來自網(wǎng)絡(luò)+ L6 T' w/ [4 n* E7 R6 U
開發(fā)平臺Arduino及Maple- t1 z+ @1 d1 r! g. e
http://www.arduino.cc/$ l$ g6 |$ i0 ^, Y2 B# _5 q& y
http://leaflabs.com/devices/
  i5 d8 M5 V% u6 e. y* S國內(nèi)活躍社區(qū)' z9 z1 C$ I6 I5 F6 T
http://www.geek-workshop.com/forum.php
7 t; j# G; k. ]8 L  H- t& \& T
0 l+ R6 q4 w* |  Z9 g竟然缺少積累,首先尋找最大的開放式開源平臺,這樣可以積累全球范圍的創(chuàng)客和DIY者的腦力活動經(jīng)驗. V6 m: e' B4 z2 |' w5 c
來入門了解熟悉思路架構(gòu)等
' r% V! P* c$ @/ ]; \+ f( {
% H# x, \  s% }  T. S0 y2 s1 O我只搗鼓了下8位機Arduino 移植到32位機Maple 方面一點點事情," ?" B' @! w4 x" ~2 W1 V7 y
許多功能還木完成,不過作為低檔次得應用可能還有可能
* z% `" y5 w  C% W* |$ J4 }
( B9 u! K' j7 e  P3 |+ Q4 r我自己也是個半桶水,再樂意玩的起碼自學能力還是要有點吧5 K$ T3 p9 s5 H, C/ t

2 F6 a5 \0 u7 `# [$ b6 Y拒絕 所有的求% b( @. `& v& E' K' ^% P" F) Q" ]
求人不如求自己 不然木玩
% p: j6 @% k) ?, ~  b0 z$ |( R$ d: e1 Q$ d" {4 p5 F6 d  W
高手繞道 謝謝!
/ f1 c; m" ^4 c' [4 g) C( @
) A* j0 B$ s3 M* G- K) h4 T1 l
回復

使用道具 舉報

2#
 樓主| 發(fā)表于 2014-5-10 09:48:37 | 只看該作者
本帖最后由 xmdesign 于 2014-5-10 09:52 編輯
, m  o& j$ w2 Q/ @$ k: `4 {9 e! V
. }# R( a7 O6 P9 z8 h6 }% w# Z' g7 vGCode_Interpreter是比較容易懂的,木那些寄存器等蝦米開始不容易懂的東東
  r* q& `" }$ C& o貼代碼先
" |, @/ @% d7 P5 }* m直接Maple的,某寶許多超便宜哈
* S6 \8 Y' O7 U2 ~暈,我怎么上不了RAR?$ Z7 w4 E& l  @
想玩的留下 e妹吧& B7 U# U- k" Y/ n1 Z0 y
第一個妹麻煩傳第二個妹哈
4 ]( r5 Q' ^% p  G0 Q我平常雜事多( F+ A$ o# ~8 G- g; c7 C% b
誰放網(wǎng)盤上再,麻煩開放下
回復 支持 反對

使用道具 舉報

3#
 樓主| 發(fā)表于 2014-5-10 09:52:53 | 只看該作者
// Arduino G-code Interpreter for Rep Rap2 e/ U, g, x" x# A
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
4 d& e; X8 T* d/ U" @& L// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)8 h. }) L' r1 Y" o) S% x' [& t
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
& _) _8 B2 L3 V+ S// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
) J+ i, D, V- k6 C& s
. t4 h% [3 L( @5 Q* Z// Arduino G-code Interpreter for Macro / Micro photography0 w; t1 T+ k% x6 }, i. {0 S: h
// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)8 x( O8 M. j& U
//modified by YaoHan China 2010.2.15: B; X6 B; U7 h& J7 O. W2 T1 k
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175). ?# E+ s9 P" [3 |
#include <stdlib.h>7 |& h- s: A! k2 s; S; r
#include <LiquidCrystal.h>
* b6 v, x# C+ B) v7 \+ D
3 K: a- e9 c0 ~  ]+ S. M1 Q//啟動(高電平有效)/暫停(低電平有效)  默認高電平5 ?9 s1 J* z. R1 I5 I
#define BUTTON_CTL 35
! l  l9 o9 B$ F9 U$ b9 G; E  A* v+ R& [
//點動模式鍵(高電平有效)  默認低電平! g- m0 l* z* D" b) S
#define BUTTON_SS 361 a: R3 _7 ~# }( X6 F

( J* ^: |0 _, T7 |4 t0 Q$ N//點動鍵
6 Q5 G' h- d3 d* a9 a#define BUTTON_MAN 15  p! I* k3 Q$ P% N4 ^

7 u3 Q; D* l( F//電位器速度控制- \. X/ T5 B1 i$ g: q1 \2 O$ M% ~
#define SPEEN_CTL 163 k4 ^6 d& p. N$ s" l5 P8 C# _
' E) E+ d4 i. e. y
//手動調(diào)速使能
' j5 e+ a8 s0 ^* n#define BUTTON_SP_EN 17  ^0 K) _+ ~/ R/ R1 O( Y
- j. q6 I5 A" e# w! I5 o
//LCD 類型 1604或16025 m5 @0 T; g# p1 X4 t
#define LCD_TYPE 1604
- M, ]; ^0 Y9 U: e: s//LCD 引腳定義
! P6 I. T4 N4 _1 q0 O#define LCD_RS 23
: h9 s) ]/ |; S. r#define LCD_EN 24) t+ a! I- r% B, d
#define LCD_D4 25' V% S' C" ~9 @2 e3 s- H0 ~
#define LCD_D5 26
) V) V1 u) L# t# z- p+ o" \#define LCD_D6 271 T) B) }/ ~1 k5 [: u
#define LCD_D7 28
, o# P: F& n( G: Y8 L0 W3 V1 _2 s# H7 p  B
//命令字符串5 w5 q" M$ `# t
#define COMMAND_SIZE 128# M* K/ e3 ~) v+ n5 \
char word_old[COMMAND_SIZE];
+ q1 S, B6 L* o* p3 E' sbyte serial_count=0;$ s! E; t+ P5 g. }' |8 ~
int no_data = 0;
$ ]0 c7 v0 x5 c0 n1 R4 w/ ?//LCD 引腳鏈接 配置' y  ^" `  `4 z; g( |8 C) Q
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);. W, B8 q4 D8 k6 h
//停止控制,高電平有效$ g; ?/ w% v. s; z2 x# |8 [
#define BUTTON_STOP 30/ e  v  M3 M+ e. E5 t6 f% b
int stop_flag=0;
* j1 i' p7 B  _4 U& N; N  H
1 R' Y* @2 A$ o  J4 q//暫停- C) }+ M! Q- V. I1 t
void pause(){1 J/ z; s- a8 ?
  while(!digitalRead(BUTTON_CTL));4 z. U" n6 }! U2 O5 C& A
}
  L% R# _* Y8 B* a- R! L/ E+ q/ E6 A
1 q# y+ U2 ~9 y2 j8 Uvoid stopper(){
5 k/ l" V8 x0 Y( G1 D8 m  delayMicroseconds(10);
) t8 B( U. d; f0 _  stop_flag = digitalRead(BUTTON_STOP);" S! O$ m4 d6 v
}
5 b- x* s- v$ N: m- ~) T% K
: G! G* H0 D5 C, v/ _' Gvoid setup()
, o0 A( \2 M; Y* i5 `  I: ?{' K$ S$ B5 V6 s
  //show start2 ?" v) a' [2 l5 P* X0 o
  SerialUSB.println("start");. m+ P5 L) F0 |# s" g9 |
6 A+ v0 o+ j$ M4 J- A% R
  //啟動lcd
( a% F0 F; `. B: w/ }9 I    lcd.begin(16,4);
6 O2 q/ j* d* o5 l6 v3 ^0 }/ a    lcd.setCursor(0, 0);$ I% b5 U8 V. h2 D/ {
    lcd.print("hello, world!");; e2 }8 x1 h  c! J/ l* W

: s# |: {9 A8 S4 S5 {$ O4 I  //初始化控制引腳及引腳功能
  M' y: q4 B+ v8 E3 Z1 w& k; c4 [5 @- T5 r/ r  pinMode(BUTTON_CTL,INPUT_PULLUP);2 u5 U8 {6 F1 f) M; g
  pinMode(BUTTON_SS,INPUT_PULLDOWN);+ ~. x. i; w! W' z' J
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
3 w% ?& E9 G, ?$ D" R# l  pinMode(BUTTON_MAN,INPUT_ANALOG);
: \, ^8 y2 r2 x6 V2 I' ~( `# J. \
# t! X- d; b2 l- Q& a  //控制引腳的中斷
* \9 p; G8 k- s: t! m  attachInterrupt(BUTTON_CTL,pause,FALLING);//暫停的中斷
8 O3 r  M; O  ~* c  attachInterrupt(BUTTON_STOP,stopper,CHANGE);" s+ b- D, B& P) ~
+ w1 ?8 m% k$ j4 C( H
  //other init3 m4 ]6 S4 {" f, a: T& |! [( p
  init_process_string();
* p' G* H5 x6 p2 S; H: w$ v  init_steppers();* k2 E3 V) ^" K# o( `
  init_camera();
' |0 I  k: s  x, t& p  p, e  L: ^" L* {; ]/ d3 Q
}
: O. j" W/ h4 S2 Y# E8 f2 f3 R% }7 A5 U
void loop()9 Z5 |; S9 L" P4 f8 @! A9 o
{8 ?. d- g: y/ N4 e
  char c;0 b' x3 [( u1 P7 H9 s

! ^3 ~4 t, o# Z, i  //讀取輸入的字符
' P2 J" f: w6 C# g, \  if ((SerialUSB.available() > 0) && (!stop_flag))) |3 H3 v0 Z4 i' f/ }
  {
; q' n& ?; [) r& h) D    c = SerialUSB.read();
  R& P9 _" X% {7 H' u6 Y( _+ D    no_data = 0;8 |/ K% _; v  U4 Y* i# M

4 j: G* c, g5 {5 i  H* m3 `% l    //換行符代表一個命令的結(jié)束
- ?) ^8 T7 I6 P- t7 c0 f    if (c != '\n')
2 \+ A2 H$ `) a6 w1 R# f& z    {
8 R& V+ \2 N* S2 y$ l      word_old[serial_count] = c;
" U" J4 r- h) I. P2 Y8 \$ B      serial_count++;
, v" S+ x9 A( A7 _% S
* z4 b  w, _5 \- q0 J0 |    }. `3 V7 n$ b" u5 g8 \/ f
  }
1 I. a6 l' m" G2 B  //標記沒有數(shù)據(jù)輸入
4 t! |5 g# t, ]8 V4 x  s3 S8 w  else
% \! b7 C, V3 c- y+ x  {
3 \3 E  e3 }7 c$ ^) Y    no_data++;
. h$ L7 ~; E4 Z' E- K    delayMicroseconds(100);' W2 \" T3 ]8 N( |% m
  }
9 v1 h- q0 T2 Q8 n' G- w# G( k2 y& l0 f6 Q
  //if theres a pause or we got a real command, do it
- l( N" V$ \2 ]5 G" }  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))
; n1 f& d6 j1 t+ f* \) u  {
+ w! G6 }/ S; l0 X& C$ ^: G: \, Y7 R. j. E/ _$ T( u
    //處理命令
) Z: ?+ J  c  z) y7 p    process_string(word_old, serial_count);4 M/ X! l4 M9 ^
: X6 l/ T% G+ {& S9 I
    //清除命令9 Q' d' }7 R2 J1 O  Q# |8 k0 @
    init_process_string();
2 _$ \6 Q' N, K0 c/ J  }
0 _6 K. A: D7 g3 @. y0 k7 d
$ @1 j5 j2 J7 R0 Y, |: k  //如果沒有數(shù)據(jù)關(guān)閉電機# F! l; X5 S8 g$ M; m; B" r2 s+ {
  if (no_data > 1000)
# p, ?$ j0 p2 u5 n. ?8 C+ m9 B/ G/ ~    disable_steppers();! Q4 m. n0 @8 k

! P2 f, G) F6 I% F  //如果ss鍵按下進入點動模式& q7 r6 @4 s4 J5 ?" e" j9 L! L
  //if(digitalRead(BUTTON_SS)) ss();. U- F% z+ v, F8 N( y, c  u$ J
  run_a();
1 {2 J# O+ X+ B& G}7 O/ e  `: M9 G& h' Z

" I3 m' Q7 u. I6 k: C* F- \' d//點動模式7 B5 e" c+ P! Z4 Z
void ss(){
9 H* f/ ~" A8 }$ j" Q5 |
5 R- D8 Z! q& x9 \. ~: b, [, n* j4 p  delay(1);8 u5 j( G8 ?1 q; h0 @9 h% W% x
  if(!digitalRead(BUTTON_SS))return;- o3 x4 L1 i2 x' g3 }" z
  if(!digitalRead(BUTTON_SS))return;' V6 |2 f1 M/ z3 Z' C
  //init_process_string();6 U3 d( `" _. Y0 l1 M5 G* o+ i4 Y
  //init_steppers();
, a/ G7 b  s# G! n+ Z: J/ J7 o  //init_camera();' S9 \3 ^' t# r% B3 l( W, x

, j! F7 |$ ]& n // SerialUSB.println("Step By Step Mode");, u9 K' X# R! B! `, f3 D& E3 I
//#if (LCD_TYPE == 1604 )
' @& K. I( S4 @  //當LCD為1604時顯示要處理的命令8 i) H: u5 }) G, @8 T/ s
   lcd.setCursor(0, 0);: t) r1 r/ U8 P+ h
   lcd.print("Step By Step Mode");: G8 r# j2 j* u7 q
//#endif
' ^1 J+ X: k" a) R, j( v. |; M  process_string("G1 F5000",8);( F6 a: c7 M* p* M; @( \% c
  process_string("G91",8);
4 q# _* J: ^) V. ?/ I  //init_process_string();# u5 ~. s, Q0 T% ?- }4 H8 L4 M; M
3 t$ _$ Y% ~* ^4 a# E
  while(digitalRead(BUTTON_SS)){0 x, W  c& m2 p9 n/ O2 y6 Z
    int i=0;
* L2 @0 w5 F: S! I    i=analogRead(BUTTON_MAN)>>9;, I* s. M$ a1 H. n5 w# {3 a" A+ [$ c; ]
    //if (i==0){break;}, e3 R3 D& q- x7 k
   //SerialUSB.println(i);" k( m# `" r' b) Z) B
   //delay(1000);   
4 Z( |, e0 K3 g4 H& f
% e1 Z7 Y6 H7 n, `   if(i==2){3 V  ~0 E! n9 l' Y3 z# R! J
      set_target(1000.0, 0.0, 0.0, 0.0);. i4 e1 e" x. L0 T
      dda_move(getMaxSpeed());
' n' M  v$ B1 \( c( m# B      if(stop_flag) return;; c' k  w: w, J0 [, f
      //process_string("X0.01",5);# f# ^# t; ?: V$ ^5 v& A- |
      //init_process_string();
7 \- Y$ t; r* t/ }- v8 H( W    }
( s5 `4 J- f3 ~. o( D% E" Q    else
+ P0 A+ J7 s! A9 D! t: e  A7 R    if(i==5){7 X0 {6 Y3 J7 U8 o5 F
      set_target(-1000.0, 0.0, 0.0, 0.0);# ~6 H, O* _2 F6 ]$ Z9 k. l. S
      dda_move(getMaxSpeed());/ w) c: y5 \7 _1 G) B2 h
      if(stop_flag) return;# S* v1 m$ h* R9 f/ q. A
      //process_string("X-0.01",6);
- q# ]2 j; x& h) a  c9 R      //init_process_string();( t0 ?( f. l) C: ~# k3 I: r7 r4 h
    } + o9 @' f" H4 M5 H
     
. v8 _* A/ D' j" @0 F( _' j* P3 b- E0 J! p( M
  }5 B8 ~; s; f* ?+ Y! A
" P. _6 q% I7 P
  //init_steppers();, l) t0 y5 V6 H3 f4 @1 s1 O7 `4 ]
  //init_camera();
) O" q1 I6 S: A   // SerialUSB.println("Return To Normal Mode");9 ~+ M" e& h9 r1 Y
     process_string("G1",8);( D3 o7 s2 E8 L. Y% W3 E* J
  // process_string("G91",8);  r& }$ H) ^$ r1 p: |, v
    init_process_string();
+ p- j& q2 V1 m" L: Q6 @//#if (LCD_TYPE == 1604 )
4 _7 C0 f1 O, T- n  //當LCD為1604時顯示要處理的命令
# N3 \* d+ L1 I" ^9 }  // lcd.setCursor(0, 4);
  J6 O5 I6 l. }8 i/ g  // lcd.print("Return To Normal Mode");
' e; g7 ?; E, d0 `* K  F//#endif
; ?& N6 \1 f* e7 ?}
8 Y4 D2 o/ E6 t1 B, G) d* T' {
% k; \& E1 R3 p  Avoid run_a()
. _8 a8 K1 g5 Z2 |0 a) Y. h" t{) U4 v3 R2 m9 F4 r& _9 B
  //delay(1);
1 Z8 ?, H2 A* `  q6 C  //if(digitalRead(14)== HIGH)return;
7 l# F0 A' j) n9 e: e' j2 ]/ s  //if(digitalRead(14)== HIGH)return;; M0 z4 ]# [. U) G9 _1 V8 q
  //process_string("G1 F2000",8);
; d5 X2 g" O& H) n1 B9 w9 X //process_string("G92",8);
$ ?6 b% ^  M7 g, Z: g( k9 @( W process_string("G90",8);, y3 {& H$ @: [0 p
process_string("X120 F10",5);
# j' j4 F" ]- c9 o" g! P' { process_string("G4P2000",8);# R8 u1 }2 c4 |9 ]+ @' |4 I- S  x
process_string("X10 F10",5);3 x& O" Q. u- [7 p$ t
// process_string("M101",8);* f: N6 s: j3 ~
// process_string("X-50",5);* O* J: Q4 \' L6 B$ y/ W( H
dda_move(getMaxSpeed());% t$ |) V! |/ f2 j; g
init_process_string();$ I1 l" ]3 S/ Q8 a: @8 E0 L& m
- w7 U+ P4 u4 ]# e5 o
}9 v3 E- b6 \( P, E6 H
回復 支持 反對

使用道具 舉報

4#
 樓主| 發(fā)表于 2014-5-10 09:53:25 | 只看該作者
+ j$ T5 g) w* T8 m6 Y
9 k& R) ]1 V& ^! A
// 定義機器參數(shù)
( Y* V9 o3 x% U. t/ l3 }#define X_STEPS_PER_INCH 4008 w0 G9 u" O, ?, p3 b
#define X_STEPS_PER_MM   16.00 G$ |4 L2 H+ Y1 N
#define X_MOTOR_STEPS    200
+ |/ ^& w4 d+ A- P) P
% h. ~+ o9 @, Z3 R  C7 d#define Y_STEPS_PER_INCH 400.0" I" U& {& ~& _$ z! V
#define Y_STEPS_PER_MM   16.0
# c# l# ~' s/ H  ]% G4 t, a! |#define Y_MOTOR_STEPS    200
; C1 q7 j4 v/ ^+ \4 I3 J  F
& x* ?: {/ f: D- l8 g0 J! f! |( ?, N! ]#define Z_STEPS_PER_INCH 400.0
/ l+ s9 c9 s+ `: _+ u2 d3 ^#define Z_STEPS_PER_MM   16.0) g- u: L# v! a; V0 w( B
#define Z_MOTOR_STEPS    200
) D2 h6 W& V+ W% p5 V
4 @& `& c+ a  {$ ?! ^% E& P5 D8 o6 h#define U_STEPS_PER_INCH 400.0+ x* u8 U4 Z2 E- u, m
#define U_STEPS_PER_MM   16.0
' E3 v7 A8 n& t3 ?4 G; K#define U_MOTOR_STEPS    200% n: }8 ~# w6 S
# z6 ^$ f; E! U; n! m
//最大進給率+ M9 a- G( z6 o( z$ v
#define FAST_XY_FEEDRATE 1500.0
5 N& n  p( Y( _) c+ W% M7 X#define FAST_Z_FEEDRATE  1500.0. v8 b3 d% V9 x3 V8 x) L- ?9 w
#define FAST_U_FEEDRATE  1500.0
" Z* Y) N, R0 K7 B0 Y. ]9 h7 v// Units in curve section
  G( [3 L4 [: B$ \#define CURVE_SECTION_INCHES 0.0196854 {8 _  B& [) S+ l3 z( [' }) h2 K; S
#define CURVE_SECTION_MM 0.5
' o: h' V9 h  G$ S6 L/ |$ [9 f  q4 F0 U: M
/ H' M. {0 H5 ]2 k: g2 g4 ]
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)- }) R+ C- I1 u0 e* U! C
// RepRap opto endstops are *not* inverting.
0 G) x/ y$ [2 ~+ V/ B  \1 ~( g#define SENSORS_INVERTING 1
  W& {2 ]: u) M' ^0 b, \" C
1 [& G) f: o1 ]4 G" x/****************************************************************************************
- M% T- {. a8 D/ L * digital i/o pin assignment
7 ~7 N2 t3 |3 z" j" W. h+ z/ f *
3 k% N# ]; B" `5 X9 z * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5
$ C$ x; M4 X0 f6 x ****************************************************************************************/# Z9 k5 s: P$ Q6 p7 L! s

! M! W5 c, Y5 A" R; B$ D//camera shutter and control pins, f+ s, ~: D# w8 O5 f4 o
#define CAM_SHUTTER_PIN1 29  \( F" O" ^' Y+ d% @9 W/ C0 ?( i2 J
#define CAM_SHUTTER_PIN2 30; t3 m/ T8 ]$ [' W0 k! l; P$ i
//#define CAM_AUX_PIN1 31 // analog 0
7 G% J9 |+ M( w5 ?( R  U//#define CAM_AUX_PIN2 32 // analog 18 r! a. f, ^0 }+ `, H2 x' W
//#define CAM_AUX_PIN3 33 // analog 2
: ]1 p3 i9 y/ A! h/ _//#define CAM_AUX_PIN4 34 // analog 3+ D" ~9 S) i0 `# Z8 v' ?

% ]4 s% t# F! v+ P5 J+ v* ^// stepper driver pins/ ]% b: v8 _1 ]
#define X_STEP_PIN 72 h( X4 _  o# e! K5 |
#define X_DIR_PIN 8
& j: k4 j6 k. l#define X_ENABLE_PIN 196 D# f0 X& n# J: R' l& t
* e3 N8 f6 |1 z" V8 q$ Y( N7 {
#define Y_STEP_PIN 9" ~( Q8 W: \, k- _1 p2 o
#define Y_DIR_PIN 10! K1 y$ `7 h1 G5 p& M
#define Y_ENABLE_PIN 19
( p4 @6 V. s9 `# }6 g* c- F& B" w4 w2 T* s% E
#define Z_STEP_PIN 11
6 p3 p$ M# _0 {9 x$ ~) H#define Z_DIR_PIN 12, N1 [% j) H! H, l! I7 I
#define Z_ENABLE_PIN 19
; n2 K" v2 P, r' q! O
$ Q3 t, U* M! `4 ^( M#define U_STEP_PIN 13
4 u. ]& Y; {4 y#define U_DIR_PIN 14
7 f7 P: B# w( h6 f8 j* S5 p#define U_ENABLE_PIN 194 ]! ?! X. F. W4 _: r- g7 t, v
, c% `* K5 f1 M: E" a
// limits not used right now
+ `/ d( V0 }% X$ o#define X_MIN_PIN 14
3 S# X' b- J* R0 M7 d#define X_MAX_PIN 14
* B! j7 |) c' X5 ?3 \* y% e#define Y_MIN_PIN 143 c; d7 Y, k+ m4 |6 |) {8 t) g
#define Y_MAX_PIN 14
) D- o' D8 y5 J1 V#define Z_MIN_PIN 14
4 S7 N- k* r( V5 o& i! t2 q6 |#define Z_MAX_PIN 146 j% e; v/ y% n0 {
#define U_MIN_PIN 142 g6 C, L  N2 U% O
#define U_MAX_PIN 14% P! Y  |$ l; P" y
回復 支持 反對

使用道具 舉報

5#
 樓主| 發(fā)表于 2014-5-10 09:54:26 | 只看該作者
void init_camera()
+ e) B4 ^+ k3 m9 R- L2 M{
: [1 T/ l1 q8 b, m8 w  pinMode(CAM_SHUTTER_PIN1, OUTPUT);
* |! ~8 s9 z0 U1 y8 }  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
7 G0 U9 ^1 D8 D/ \( m  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0" D% C6 j, b$ W* `1 N3 e
// pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1) J+ H0 |7 U7 b1 D, e" I
// pinMode(CAM_AUX_PIN3, OUTPUT); // analog 23 p# w# y7 |6 u+ h
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
2 u" L8 S! o% R7 n}* A0 B* K; r- `( ^; B2 L) Q
+ f1 j& h- I8 u) i
void camera_shutter1()
: q3 C1 H, G9 o8 Q( E{
/ }8 O/ _' b3 x" n2 p: n  // fire the camera shutter via relay...1/4 sec hold time9 A- @5 t, r! F3 K* Q6 s4 V
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);' v8 c9 w3 {: d8 {
  delay(250);
/ q/ N2 ?8 B& b3 x% ~3 {  digitalWrite(CAM_SHUTTER_PIN1, LOW);: }+ v9 j& i- r5 j" L" O% S' Z

' K0 @' l" y6 c}& H. t- ]4 Y! c# g; D& a+ j
9 g5 S; x. M( F5 H: p3 Q6 v) G4 t
void camera_shutter2()
' F7 K( S# P: C{/ T& k9 G9 ?/ q7 a7 n' U
  // fire the camera shutter via relay...1/4 sec hold time
2 ^. q$ b% r# V% j  digitalWrite(CAM_SHUTTER_PIN2, HIGH);
0 v5 y! r: S3 |* Q  delay(250);
. U, J# C/ p7 C) o# y4 g8 o7 p5 U  digitalWrite(CAM_SHUTTER_PIN2, LOW);3 M( Y" z  P9 n

$ f6 Z% P; a, R& D) L}
# S2 ~4 l( G+ r$ ]' r. l/*; Y) d& Y3 i0 K# j8 @
void camera_aux1_on()- a$ c- h3 E+ W6 T' p
{
/ |% h1 p$ Q6 T  // turn aux relay 1 on3 a4 c6 L$ U  [, x
  digitalWrite(CAM_AUX_PIN1, HIGH);
" z; O- |1 f6 f: X}! [. p3 S/ u- H* x' l# n, v

3 d! M6 V( ?, Ivoid camera_aux1_off()9 d3 u# A# D  S1 B
{
, ^& g9 W4 c5 Z, D, v  // turn aux relay 1 off
+ \+ p. }# U6 S  F3 g3 o$ y2 f) O  digitalWrite(CAM_AUX_PIN1, LOW);. r' }1 d5 {1 W9 l: w
}# C4 d/ T. d* j0 G
8 |; D- [+ N! s
void camera_aux2_on()
. M' @# y7 ^( f0 N6 u{
6 r5 n: f/ ~6 K8 C) _  // turn aux relay 2 on
8 D$ N6 |' w) F7 t9 l( V. X, N  digitalWrite(CAM_AUX_PIN2, HIGH);
% j: O1 _# b8 n. H" G$ |}
1 m5 Z% \0 \8 A1 H! v$ @
8 [  {0 k& q1 p3 @void camera_aux2_off()
. J1 a% r& Q( ~" V{) T% l. a& k; D; _  z/ U
  // turn aux relay 2 off
: ]! h0 b6 O; Z7 S4 i! K3 i9 U' o7 f" A  digitalWrite(CAM_AUX_PIN2, LOW);+ X. i$ C. B& k  a3 e3 c6 A& n
}+ K: y2 l  y% b  M* M3 [( @

+ F9 v5 y+ L0 ~2 J* a4 qvoid camera_aux3_on()
5 m) ~7 t2 i4 y, n{5 b9 l& Q. }3 N* w7 S6 p( t7 Q
  // turn aux relay 3 on9 s* m" `) [  z9 K
  digitalWrite(CAM_AUX_PIN3, HIGH);
. l4 L4 C3 P* _# x}# [3 e9 b) X: Y9 p2 S- C1 j

6 m3 J) G. N! H, @) K, Yvoid camera_aux3_off()5 u8 V( k* I0 ?$ ?/ w
{& ^, u9 H: I/ `6 `3 H2 t
  // turn aux relay 3 off" T% g! v. ^1 r: l* b, C5 r
  digitalWrite(CAM_AUX_PIN3, LOW);
$ [2 L' m3 \& O6 q4 e}: {  U6 y  l  T; c+ z/ z* Q4 Q
5 ~5 w+ i, ?& ]1 v) k) ?7 b
void camera_aux4_on()
; x5 a  y! I6 j{
" C/ L5 K+ [: y; g. L  // turn aux relay 4 on8 i4 B/ [4 e; w0 O
  digitalWrite(CAM_AUX_PIN4, HIGH);
6 f0 o8 Z. m# c# C* H}
, L3 U6 h; B6 ]- b% T  k7 A, i" t# b/ T; H5 P
void camera_aux4_off()
! H" R; `, l% T9 G{
5 ~% G3 L( E7 c, C; _( q  // turn aux relay 4 off
6 z8 c6 G; p8 k! ?1 ^( E7 d  digitalWrite(CAM_AUX_PIN4, LOW);
' h1 _' l# E$ p- h$ z& |}
回復 支持 反對

使用道具 舉報

6#
發(fā)表于 2014-5-10 09:54:28 | 只看該作者
樓主推薦的網(wǎng)址不錯: ~; B1 w/ ?6 Q9 V& x
請問樓主是玩什么的,用樂高玩具嗎?
回復 支持 反對

使用道具 舉報

7#
 樓主| 發(fā)表于 2014-5-10 09:55:02 | 只看該作者
// our point structure to make things nice.
  p/ Y; H8 i. W! o! M9 L3 ]9 Tstruct LongPoint {
" d6 Z, E$ @/ ]  p9 x  long x;
( M$ N" T; ~  ~& d% Q  long y;
! [9 W$ k3 @- x; E! j% o  long z;
- |) o2 a' D. i5 c& o  long u;3 ~+ N. [; a1 G  a* x
};
7 l1 M; z' R. n2 w$ E# p; J8 o8 L% W6 \# T4 X. c
struct FloatPoint {
2 |8 r- e5 c# ^# W6 G9 }7 ?+ V  float x;
7 l: g" Y- J  ?4 P0 J  float y;
5 G# }+ X) `# Q; ^: g7 C  float z;
5 p! B0 a) d! r3 x& ?) [/ ]" ~$ P  float u;
& E% L: n  O% d2 x) ?' }};
+ A4 ]" q& m7 {. I6 u
& f5 A* s6 V' T* @! kFloatPoint current_units;, j- l+ _1 g( g  U+ p* B
FloatPoint target_units;0 R6 L4 p1 V2 M, g* w7 T2 n
FloatPoint delta_units;
0 X# m/ ~8 n/ c: M$ S. `) M# X- f1 n0 ^3 f- D2 L# [: _$ K
FloatPoint current_steps;
: W' G2 w: u& ]; kFloatPoint target_steps;1 m) \  o4 }0 B4 L& k
FloatPoint delta_steps;6 Q; O! }  O- t' b

: d5 @, x" P* cboolean abs_mode = false;   //0 = 增量位置模式; 1 = 絕對位置模式
! A* Q6 |/ W! I4 d8 h' M' l" U# @5 X9 @# }* e
//default to inches for units0 L% a7 Y' I# j% ^  v6 r# U! b
float x_units = X_STEPS_PER_INCH;
$ M& X# M) f  }float y_units = Y_STEPS_PER_INCH;2 x' E$ p) c% a  |) A* ]
float z_units = Z_STEPS_PER_INCH;
5 v2 v/ {$ m# ]& g  O2 ]' }, ufloat u_units = U_STEPS_PER_INCH;
- B- s5 i+ k. g8 b! `% y  rfloat curve_section = CURVE_SECTION_INCHES;
3 H) T0 v9 h6 F% Y0 B
# B; v+ T# b& r+ O% z% W//our direction vars
& G: ^/ |$ a0 u% Nbyte x_direction = 1;3 ?! Y( F  }) N$ @
byte y_direction = 1;
6 l* f- a  ^4 p1 L2 ubyte z_direction = 1;
! A2 r6 s6 I0 Tbyte u_direction = 1;# U& Z: Q( g+ U8 _0 x6 c' P7 F0 ^% Z( K
( L5 L! z: B2 D! r; Z
//初始化字符串處理  _. ^/ k2 l# Z
void init_process_string()
. q: C/ _1 ^6 q8 c( Q{8 V# }  `1 f, S% g' W" t& t# X
  //init our command
! d: V4 R; n: `2 v, l  for (byte i=0; i<COMMAND_SIZE; i++). q9 i& U! W, L* k* d' E  H8 l
    word_old[i] = 0;
- }5 D1 m" Y- }( S: _! g0 l$ g! a  serial_count = 0;
; ]% p' @" U/ A8 C1 X2 j, C+ `: q( a}3 n7 i6 _& x: @  l- }6 ~* u) Y' q
$ l8 ~4 M7 V; J1 Q% Y, I+ h
//our feedrate variables.& t  F7 S% Q$ y" D
float feedrate = 0.0;
5 X3 ~! Y1 J% W* A+ [long feedrate_micros = 0;/ z  r0 H' S4 N7 K4 J

, J. i3 |4 P, ?& u, P//讀取并執(zhí)行命令
4 H: U) p3 [! X/ W9 m2 \' ]+ Avoid process_string(char instruction[], int size)
% l' G* _. U. z1 l% N& m) t1 R& j& P{0 f, V" s' }/ [) n% y
  //the character / means delete block... used for comments and stuff.1 A) p1 i5 ]& [+ c
  if (instruction[0] == '/')
- ?) Z6 @. b0 B8 ?* c  {
) ?0 |3 S5 [7 i. b. }- A    // SerialUSB.print("ok");$ |0 a+ m: Y% T; H4 P! Q
    // SerialUSB.print(byte(78));
+ t" ~/ ?+ R" o7 M" T, Q8 z    return;
1 G, E: _- _" a  P9 r! `7 V0 s! k* `  }- ]7 {9 ~) I* g: r
  //init baby!
  M; s' J5 @7 r0 M, r7 u- w5 s  FloatPoint fp;
& m; f9 I1 Z" t% K! e/ K  fp.x = 0.0;! ?) @2 x$ M( v6 ~7 a4 |
  fp.y = 0.0;
- H3 s! R' ^% J5 k- c0 k4 s5 s  fp.z = 0.0;7 ~- u; s: Y% A
  fp.u = 0.0;! r! @+ j+ c' Y. F1 e( L
: J5 h9 s+ C9 z% |& I) I
  byte code = 0;0 j7 n, z: `0 S% u  _5 ^
- Q) w  ~0 E6 q% i" m! T8 x* l: ^
  //顯示在處理的命令/ L* v: w6 \& r1 E  C0 V
#if (LCD_TYPE == 1604 )
) T/ Z; Z' `2 W1 z4 C6 A  // lcd.setCursor(0, 4);" K# z# J0 S- F" ]' W
  // lcd.print(word_old);
) F7 N9 C# Q% _. F2 w0 q#endif' S; ~- b; E0 F7 {
  SerialUSB.println();* p- j  A5 E  k% f- k" u, \* L
  SerialUSB.print(instruction);
8 {* {. B3 U6 }% N, {' K' }. p4 l/ y  SerialUSB.print("\t");
9 a, ?3 u0 D8 q) z# W% Z; H" l# ~, U
  //what line are we at?2 n: V, G/ Q! c6 U# Y
  //        long line = -1;8 q, Q& ^# g  }7 {
  //        if (has_command('N', instruction, size)), I5 x  v3 i- X: [7 z- r
  //                line = (long)search_string('N', instruction, size);5 O' S& |0 R( d  p
4 G4 Z- H& u6 }9 J, L
  /*' H1 i* N' }0 }" E( w6 G
        Serial.print("line: ");  u6 r7 n  E: H2 Y+ h" E
           Serial.println(line);
; ?5 Q9 B9 ~! V: V: {, F           Serial.println(instruction);
$ r4 S; c' k( x# Z   */# D8 ]* L4 J8 b" f
  //判斷是否讀取了個 G代碼?: X. l( W$ G3 Z0 q' |
  if (
  q' h. ?) ]( Z' z+ m- A: b    has_command('G', instruction, size) ||
  s; ?, {7 t9 n4 H! x8 E! C+ a    has_command('X', instruction, size) ||" Q; k7 \1 \: a+ P; |
    has_command('Y', instruction, size) ||
7 A5 h4 c" }, L* d+ p% O  Y    has_command('Z', instruction, size) ||
0 x% r2 n, S  ?, k    has_command('U', instruction, size)
4 u" W. y9 w, z2 {+ m    )6 l9 i0 W; T" Z: K  K' s# x
  {
: q: X8 s4 u- j* O7 e# Q    //which one?5 l/ \8 ^- I: v9 E( M
    code = (int)search_string('G', instruction, size);
7 t4 r2 e4 T' ~  X, g. e    // Get co-ordinates if required by the code type given. h- P) h) A! S* ^- N
    switch (code)
8 i$ j7 j, _4 }  Z" w    {
( }1 o7 }  n3 K! X  ^    case 0:
! u! v- e. m; P    case 1:" Z# E! X$ b& d+ U5 L
    case 2:
( w( g& y& Z3 s    case 3:; k- X: `% [# u: q( O. Q% w
      if(abs_mode)( E; k8 _* o% a: D. g
      {
3 n6 \) p3 c; J' a( \. x, d9 `        //we do it like this to save time. makes curves better.
, M; n8 B# y- l- {# F        //eg. if only x and y are specified, we dont have to waste time looking up z.
. r9 x+ V8 z/ C  `2 U6 N  Z        if (has_command('X', instruction, size)); N, x8 F! \% d$ k
          fp.x = search_string('X', instruction, size);
# l: c+ e) `3 r; {, e% m+ o: t        else# c! i# v) u. ]- b+ ?9 v
          fp.x = current_units.x;
5 _0 M+ }/ P& p1 v% c
$ s  }" m- K# i( _1 X        if (has_command('Y', instruction, size))# a* R, h2 M. ]7 F! p" z# n/ \
          fp.y = search_string('Y', instruction, size);
3 x& Z) b. j$ I        else
/ O( z2 _2 w8 q& |$ g& H: W4 D          fp.y = current_units.y;
, r* i+ d' W/ x. I" G/ T# K2 Y7 ~4 {1 s. \+ k
        if (has_command('Z', instruction, size))
3 k; e! V& t; [2 }0 e; w" n          fp.z = search_string('Z', instruction, size);
* z1 b8 j7 R3 l! ^5 {        else
) R: f, W" K4 |: U0 ^# ?  v1 W* e          fp.z = current_units.z;- _2 d& _& r* V$ _: T
          6 K* W- _- E# W: u8 E
        if (has_command('U', instruction, size)): ?4 \9 w* V% F. X  _4 u
          fp.u = search_string('U', instruction, size);
. }/ g* J" T% G- f( I4 p( A7 ~        else( ~( J. {1 M" \3 W( Y- Q
          fp.u = current_units.u;
. ^2 p8 x& C7 ?: @  Y0 V3 a      }& E5 x. k$ e5 P; ^4 Y. k. n
      else! }( x7 M( o  y" u4 p
      {
) |: [% g7 [0 }        fp.x = search_string('X', instruction, size) + current_units.x;% e) G" H  K+ o& H- q8 V* Y% {
        fp.y = search_string('Y', instruction, size) + current_units.y;. w5 o1 A' }: H. C
        fp.z = search_string('Z', instruction, size) + current_units.z;, W! B9 e4 z; v6 {
        fp.u = search_string('U', instruction, size) + current_units.u;+ Z2 ]6 Z- e0 e7 ]
      }
; v5 ~! f7 f9 h% y# t$ P/ _      break;
, X4 z' x5 L" w7 r    }; L/ k5 ~. N# _! m
    //do something!+ i% q( d3 m' M* g1 b0 [5 d4 ]+ O
    switch (code)% E& o& c& ]. w! u! a9 S0 F% n; ?
    {
5 _+ f: l4 u/ {: Y! p      //Rapid Positioning
, |. R; A: f0 o( V/ t      //Linear Interpolation( |- a) {, a6 z
      //these are basically the same thing.
+ P6 T: X9 D: m( y8 w$ k    case 0:
% ^) ~2 g& ~- e3 a9 G; s+ ?    case 1:# V/ W# A. J8 }4 T+ T/ ]
      //set our target.- r: D% w% L  i
      set_target(fp.x, fp.y, fp.z, fp.u);
/ |# i+ h$ A7 f9 C      //set_targeta( fp.a);
; _- {6 O) O  N0 d- }- e! R2 S* d      //do we have a set speed?
# K2 l; _: i+ b3 C1 @; h      if (has_command('G', instruction, size))  F$ k- v" e# A# e" ^( R
      {
9 ]0 y8 d& p+ q        //adjust if we have a specific feedrate.
! Y: A  \& a/ \' M) E1 h5 U6 r6 j3 y        if (code == 1)6 `$ ]9 `. W9 u# c
        {8 K3 W* ~0 B3 E3 @7 G  H
          //how fast do we move?
5 x) v6 Q3 N1 W8 ]8 T1 t% Q          feedrate = search_string('F', instruction, size);
: }6 w! W6 A% j          if (feedrate > 0)
) h7 r9 o0 ^5 j3 t            feedrate_micros = calculate_feedrate_delay(feedrate);
) Q9 c4 a  o: a# ]! F" S! b          //nope, no feedrate
  d# d6 r4 {. Y% I$ O; t" @          else
: L2 c# H0 [4 k/ T  u* _8 j# E            feedrate_micros = getMaxSpeed();9 o. N6 X+ e5 o/ n! T
        }' X3 t+ p3 N+ Y1 o, M/ `: x8 m
        //use our max for normal moves.- y9 W5 a' m9 y. A. J
        else
$ D0 H4 ]0 j2 A          feedrate_micros = getMaxSpeed();
1 s0 z* c4 q; E, W4 K      }
) i! z1 W. p, Q9 B      //nope, just coordinates!$ u# t5 r/ d/ ?
      else
) s5 Y& Z1 ^$ i* z5 @1 e- ~/ i      {
% Z1 p" n& V5 s1 R+ ~- G        //do we have a feedrate yet?% t! S# }# s# V0 [: c2 v* q
        if (feedrate > 0)
( h4 X8 u0 X8 u% d1 v          feedrate_micros = calculate_feedrate_delay(feedrate);
2 G: K6 o) m; g0 q! d: O; `. l        //nope, no feedrate  V+ X3 d2 h. o! n- s
        else
+ W" O' g5 Z4 O* g          feedrate_micros = getMaxSpeed();
0 H: J0 L# n& b      }, f$ m* J% y% V& b
' P  C! O* W2 }; l: S& [; F0 C. O
      //finally move.- [) d  {5 \. g2 ]/ `1 o. V1 g
      dda_move(feedrate_micros);5 }% M# {) B& q9 |  h
      if(stop_flag) return;
6 q' \" H; N, j9 Y      break;* b; E$ d% ?5 ~1 c8 B7 {
" Y& o" B( G$ L, k
      //Clockwise arc
- K6 ]' u% h7 Q( I9 ]    case 2:8 W* V( i  [" ~% e! `. G5 r
      //Counterclockwise arc
0 I" {  R3 H6 T! Q3 Y2 [    case 3:
/ W1 [4 p" M- ~: r4 x      FloatPoint cent;1 N. h# `/ A8 E/ X% e3 k7 |
      // Centre coordinates are always relative$ ]+ v1 ~8 _1 f4 \2 T" }; m
      cent.x = search_string('I', instruction, size) + current_units.x;
) J( q% ?) |! H& {9 Z      cent.y = search_string('J', instruction, size) + current_units.y;
9 k2 b0 j5 W: H( `3 G$ d) L      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
6 v2 t7 H9 L& b$ \7 z" P& l0 W" _+ @; G& d! [" y0 _4 ?7 g- Z; k
      aX = (current_units.x - cent.x);
1 `. ?: T+ G+ ?0 l      aY = (current_units.y - cent.y);  K9 D% s6 F& g( f! Q
      bX = (fp.x - cent.x);
: h7 s& f: o; i5 _5 C* T      bY = (fp.y - cent.y);
7 b. b+ u$ z8 D' ~7 s
( T0 ^- d6 T! K6 }1 ~      if (code == 2) { // Clockwise
0 c1 W' }! H5 Q9 ]" e        angleA = atan2(bY, bX);5 g# u( O; y- ?; C
        angleB = atan2(aY, aX);% @/ l* a* b$ Y" t3 x
      } - d" N, g4 x- B+ c: U1 \5 j
      else { // Counterclockwise
7 N: A' f& {4 P2 M2 W$ T( w        angleA = atan2(aY, aX);
! O$ u; _. V) ?  Q% }# i        angleB = atan2(bY, bX);
$ N( e3 k6 n- |# p; r      }$ c' g9 l# }8 L! q
      // Make sure angleB is always greater than angleA. c6 w) X% q2 S. ~6 d9 |3 ?5 P
      // and if not add 2PI so that it is (this also takes
2 s) [' J! {, D& K      // care of the special case of angleA == angleB,, r  l" a. R0 k8 O; w
      // ie we want a complete circle)- N3 \. a- P- p; M1 u9 }" q' o' ^  k2 \
      if (angleB <= angleA) angleB += 2 * M_PI;
0 n+ a9 `' L: E, N' T      angle = angleB - angleA;
' d3 [/ b! n+ n. s+ M* t# o2 c" W2 k
      radius = sqrt(aX * aX + aY * aY);. O  I; ^' x$ W  E! I
      length = radius * angle;
8 w5 g3 \# L5 L7 i' a, B9 l      int steps, s, step;
+ x4 Y5 @5 ?: \/ s; X* ?) t      steps = (int) ceil(length / curve_section);0 h9 V& R% G) e; e

7 s. s8 a  b8 ]  d! i      FloatPoint newPoint;* g0 e3 w0 E1 u
      for (s = 1; s <= steps; s++) {+ x/ f6 f, C3 f
        step = (code == 3) ? s : steps - s; // Work backwards for CW- b. `) r, C- }; J5 K
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
, Q. w" P7 G+ g        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
, @/ _1 j7 n% ^' U' s0 M        set_target(newPoint.x, newPoint.y, fp.z, fp.u);! ?) z, n8 s' w! M. }

$ F4 O. i1 @- u" Q- m  C9 h" t* K8 E* I        // Need to calculate rate for each section of curve. _0 S6 {# p# u* b
        if (feedrate > 0)- Q, _  X8 N8 S
          feedrate_micros = calculate_feedrate_delay(feedrate);
* Z6 }+ M, @7 w: W% h9 |6 o        else: T- S0 J( L# F2 h1 C
          feedrate_micros = getMaxSpeed();( }$ R! y6 I) m8 {
" }( Z' r* V7 w/ Z( e  {& b* f
        // Make step
+ b' W& d" \- I        dda_move(feedrate_micros);
6 C  o% w5 b, m, F- U. F        if(stop_flag) return;1 k9 R* `+ Q7 v2 R$ E! {3 E% j+ T
      }
% i1 Q/ {) D6 V5 w' {% k
# {' L6 [7 f7 F  g9 j/ D      break;
; U1 Y& x/ s& E1 A* E
. O" B$ n+ F2 s" U, n1 T      //Dwell4 ], i" i1 i8 K4 s( t. N
    case 4:9 f( q  e( Y& ]% B/ Q4 w
      delay((int)search_string('P', instruction, size));" O- C' n# L) l. O( B
      break;
3 ^3 m+ o, _$ D# y0 F" i0 r) s
  H3 }  q& n) H, k- q      //Inches for Units
8 l% }- D, z( h; [$ f    case 20:; E5 q; s: P0 B4 p
      x_units = X_STEPS_PER_INCH;
( t) q  e; f! `6 W      y_units = Y_STEPS_PER_INCH;. C' \3 |/ V  G# p
      z_units = Z_STEPS_PER_INCH;
. j* {' Q' U0 p8 J7 {3 L      u_units = U_STEPS_PER_INCH;
5 D& f. D3 ^- x1 Y6 u# D/ h      curve_section = CURVE_SECTION_INCHES;, m8 _' ~; b/ K& \; }
      calculate_deltas();
+ Y0 e/ e) G; _  b# g7 L1 d      break;
, y5 ^: y7 [! Y! N" i# l) q. d9 I: [0 P+ G3 l; o3 @% c# n  E
      //mm for Units
9 C# \0 \% O6 i# a+ E    case 21:% |% C5 r6 x$ o( Z0 [
      x_units = X_STEPS_PER_MM;1 n8 d+ M1 F# \# J- O4 J
      y_units = Y_STEPS_PER_MM;
0 h9 A" Q( @; i; h( l+ E0 l      z_units = Z_STEPS_PER_MM;1 a' m7 p* o2 V9 d# E# p
      u_units = U_STEPS_PER_MM;; j9 v( Y4 ]' u( N' R. |% A7 Y
      curve_section = CURVE_SECTION_MM;
0 j5 d' B- `5 M- ^  \' j      calculate_deltas();6 b5 K1 \* Z# q  K5 C# V& B
      break;
+ i# f! N: o  A7 w# L: H/ \) O4 C- F) A
      //go home.$ ?4 Z' R9 ^3 _- ~
    case 28:
8 J4 q: q( J# c  |0 u5 H      set_target(0.0, 0.0, 0.0, 0.0);% {' @3 A+ [- c7 @$ f" _2 W" K! J% b
      dda_move(getMaxSpeed());/ `# f% p3 |. b, y" b# O( c" C
      if(stop_flag) return;4 f* l7 w, F( Y" }/ o8 o4 M
      break;7 \$ T, l6 I: z% l$ y& o
* j! x; l/ K, d* s* e
      //go home via an intermediate point.9 N, `/ |- d/ D+ y3 I0 U9 l3 |
    case 30:' m4 k' c) o! v/ a7 O
      fp.x = search_string('X', instruction, size);9 d+ Y3 a" S  _
      fp.y = search_string('Y', instruction, size);9 p5 R0 @: c8 ?2 O+ s$ l" d9 i
      fp.z = search_string('Z', instruction, size);
. q! G1 m0 l2 w) a* h7 t      fp.u = search_string('U', instruction, size);
7 P9 {. U$ ]  I0 u% I      //set our target.5 T: \- _3 S/ _
      if(abs_mode)* z1 R8 [: E' p0 C" h2 a3 N
      {
& m2 E& B+ Y; u+ `7 [9 U        if (!has_command('X', instruction, size))
% m- M" K9 x2 R. k2 ~6 D          fp.x = current_units.x;6 z( E6 \- c: n- i$ c  P
        if (!has_command('Y', instruction, size))( ?! m4 H( [  H# T  q  |
          fp.y = current_units.y;! b; h  ]: t/ Q' k" {
        if (!has_command('Z', instruction, size)); x' [0 W0 r# L+ X
          fp.z = current_units.z;0 h  ^  R! b4 Q  n# O8 @7 E) ?
        if (!has_command('U', instruction, size))
! u/ z. L5 |, [          fp.u = current_units.u;
4 x3 o! o) y4 F, F6 c+ \: ^        set_target(fp.x, fp.y, fp.z, fp.u);" e8 P. a: H+ b3 F
        
+ n5 ^9 l# C+ O7 |- Q9 S      }- \6 u5 q* a0 a3 w+ N! G4 M& ?9 V+ y
      else* k5 k$ ~9 l% [; D
        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );2 `$ A( S" y7 v' \- d! x; @9 N2 _
      
+ j" Q6 U# w' a7 K3 Z3 L( L      //go there.3 [6 l- V: M+ f: e$ D( r+ ^
      dda_move(getMaxSpeed());
+ b. t% _; @8 b3 H& ]      if(stop_flag) return;
& _1 d6 ^$ S* z3 X. V3 g0 j8 e& e$ a
3 w# T9 @9 k8 P+ x5 \& F      //go home., B8 L6 n9 a! h* }  y
      set_target(0.0, 0.0, 0.0, 0.0 );
: {, c. x2 U9 [3 X5 E% c; w     
4 I/ y: w% T/ B$ T: B. ]      dda_move(getMaxSpeed());
( l0 T" O: ~  L) A      if(stop_flag) return;5 A- `/ x! @/ e5 k' @; O
      break;
9 w3 I. A: d/ T* B: J, H! W$ y9 J" c! F9 j
      //Absolute Positioning: R8 G* R6 s+ u; D  d
    case 90:
6 @9 ?8 I* @4 }      abs_mode = true;
) J+ o9 o+ j1 [7 T. e- H) v      break;# e4 X$ M/ {" Y+ q9 s

8 z. m8 e* o" Y4 X2 F3 M5 S' @      //Incremental Positioning
3 g7 I; U/ T$ t; L  M$ `) v  u    case 91:8 f* l1 M8 X8 f7 I% ^/ X
      abs_mode = false;- T3 g+ u- N8 R) }# N3 I
      break;
% @9 i- ~  _! g0 X6 @. y/ }1 C3 E- G& L% u
      //Set as home
. h7 U' z) E7 j' A' Z    case 92:# R. d: g2 e" {6 a0 m
     9 [4 u. N8 g" Y$ ~/ `* `2 O
      set_position(0.0, 0.0, 0.0, 0.0 );+ E* T; l, t) ^# m' j
      ; }; L  w: H! V+ w1 w
      break;
( D4 d+ s" H( {9 h7 Z2 ~2 s- c
1 `7 `, G! k" a) ~; D. {9 T      /*
; Y1 ~4 w, s$ X                        //Inverse Time Feed Mode. ]3 R& a& W# i* v( U
                               case 93:
3 l! C6 N" A& o* ]       * p  y- ~. Y$ ]* \. x9 E- [, n+ H
                               break;  //TODO: add this0 p1 q3 e& i/ C6 ~. B2 X8 Z
       * C) T' ~# T& N3 y, d
                               //Feed per Minute Mode( q! `0 K! j" C6 `
                               case 94:- A; k8 }9 @( X+ b1 O& Z  e
      
4 t, V0 r5 d* V& n                               break;  //TODO: add this1 S0 U! \- S4 s0 u1 j  }6 e4 g
       */
& {. i! P2 @6 C2 q! M- ^& t
- q. I  C4 n/ v& u. f3 [% G7 D    default:2 P' d3 q4 r  v  j1 |& m/ N
      SerialUSB.print("huh? G");- a( c  n. d' t( f* @* J# @
      SerialUSB.println(code,DEC);9 T' C! X# p! t8 p
    }
& A) |4 p" M4 u+ u9 b4 w7 W5 D  }
  {9 @% q: d7 V6 I* p! i) r; S, h% {& b) m9 |2 s; s) I
  //find us an m code.
- B  E8 a5 \9 m+ T/ @! `  if (has_command('M', instruction, size))& {' k* f9 Z4 @1 A
  {1 o0 }/ |7 D& v& ~$ [4 f# a
    code = search_string('M', instruction, size);
5 x# i0 b7 S6 I3 c    switch (code)
# [, V+ ~8 P2 r: p    {. @5 n  b3 V' D0 }
      //TODO: this is a bug because search_string returns 0.  gotta fix that.
9 P$ k& v1 n- i2 B/ O" @    case 0:
/ m- B1 l" |  |0 q) \5 F; ~      true;
. g' ]* Y; L) c$ m      break;
' p2 E" t' z# T" ~0 I' a6 T. Y  u3 B4 f& N
    case 100:
) z% z1 D) @' @0 \8 y      break;+ {9 G8 Q$ X+ j! C
% H$ w7 A* r7 J3 J" ~9 w: G
      // fire camera relay
, i3 A( G- S& Z4 K    case 101:
1 z. w4 s0 p$ H+ \) j      camera_shutter1();
5 i/ X* j; j5 r      break;! R0 j5 Y/ M% ?5 I
  ?1 s& o2 P% O. v
      // fire camera relay2
, `" x( U5 ^7 Q: o0 Y    case 102:$ t+ d+ S2 j- U" Y7 n* i
      camera_shutter2();
* K4 X! X* ~( ]7 t- k! j      break;9 @" s8 A( H. Z1 H
/*
2 E8 O) A9 [$ D; `0 Y      // turn aux 1 relay on
8 O$ w$ C6 i) z3 t    case 103:
: j+ |0 A$ p; m1 o& t+ ]& G& G      camera_aux1_on();
6 G3 ^% i) P; ]; v% R, i      break;# r- x: {# F, i
1 G; k0 R9 }# _. b$ E
      // turn aux 1 relay off
3 S3 j' z2 @: s& t+ s: ?    case 104:; m7 V' d5 X) r  `
      camera_aux1_off();& C5 b, }7 A6 {
      break;
7 i- v9 H) H4 L
: d7 x* W7 @5 G      // turn aux 2 relay on$ r, |3 z, Y6 K$ _3 _# o
    case 105:
, D3 ^4 q# ~( z8 h$ t* B      camera_aux2_on();: b% v8 H/ E9 ^' L/ h0 @1 J! z8 }
      break;! \1 A! T! O( L7 B

0 j( p7 q# w# f3 b. p1 H      // turn aux 2 relay off' d0 S: B* }- b, K! {
    case 106:5 q: s3 `: T1 F, @, |, a
      camera_aux2_off();- A% `: @8 L0 ]
      break;
, w! C2 S1 L1 L4 w. D2 P  a. m1 H* T3 M
      // turn aux 3 relay on, t. P# _6 _9 j8 `' ^, v# c
    case 107:' w, z; M4 L# `# l6 P0 k5 m
      camera_aux3_on();
& b/ `) v5 P+ P5 E" |6 w      break;" o) z# o8 Y$ T) t" T
" L1 A; o: r$ u1 w, A- I
      // turn aux 3 relay off! P) p+ ^! S$ q$ u
    case 108:$ ~2 r$ U6 }$ D' G. r% o
      camera_aux3_off();
9 F0 P" e( V% ]      break;
) C* u& o: N2 b/ g7 Q7 v
% Q/ l6 L4 D$ B: t; a1 x$ F      // turn aux 4 relay on
" Z; M7 f, c& z4 [' e' X7 P    case 109:
) [/ S0 s" }2 M) l, b4 K8 O      camera_aux4_on();; j5 m" w! q! u, [
      break;/ p: s6 \# p1 E! r+ o9 o' i: a7 ]' p

6 V. v' Q0 Q, }) Z* D      // turn aux 4 relay off
( d$ z9 L3 t" L5 P+ B    case 110:5 K+ @( |6 ^" w. y3 i
      camera_aux4_off();
. S2 x! B$ p+ T( ?      break;
0 R4 x+ r2 u2 c8 t*/8 m$ T2 h0 C+ w9 S
    default:
* L6 A+ p; W2 q4 z# ^7 e) p# w
  w1 p1 i% G. |  f: H+ a* K      SerialUSB.print("Huh? M");% g1 J8 N2 l, H- c
      SerialUSB.println(code);
& ]' y- a# x7 G0 n2 l3 }! f    }7 C4 u  `) Z) B8 d
  }
3 A6 }+ m- g" v( G: m$ s  f3 R8 B$ r! F+ e! Q  V
  //tell our host we're done.$ e8 R4 i8 S3 r, H& d) L
  SerialUSB.print(byte(78));
1 y( ~# O( m* v( J: J7 ^! |& ]* }8 X) Y9 q  n/ B
}8 D+ f0 Y) G: S! \; g# R- p

4 x& G( m  T& k. \//look for the number that appears after the char key and return it. D* }: G6 C3 m! b6 _3 L# M
double search_string(char key, char instruction[], int string_size)
' V% Y0 W5 u( R" }1 Y{# Z0 j! x1 @7 R3 ?' \6 U* q
  char temp[10] = "         ";
* T8 Z' S5 L0 T; q5 I  for (byte i=0; i<string_size; i++)
+ f7 t7 S+ {% X) G6 R  {; S3 K+ H( ?0 t' m
    if (instruction[i] == key)3 r6 J" @; h" D2 `# O
    {
2 Z; v* g: [8 {' a. x9 F      i++;      
4 f( R" u9 n' w- x) H- M      int k = 0;
  O# H, Y' J4 w. s. W/ a* ~; e' m      while (i < string_size && k < 10)
+ |$ C2 _# q/ [2 ]      {
" a. {" B9 p' b% L# v7 x* ^# P6 i        if (instruction[i] == 0 || instruction[i] == ' ')& g' b1 @2 l- g3 w5 J* ]. _
          break;( k* u+ @0 m7 g/ s4 d
: P& }" p" n5 x) C
        temp[k] = instruction[i];3 y0 W. N% z5 J8 M: z
        i++;( a: E# Y1 @- Y4 @* T
        k++;7 n  u4 W& l0 H" w$ g
      }8 i" x8 M) s: J- s4 A4 h% n0 s
      return strtod(temp, NULL);
8 Q" L9 \9 B6 V6 |. W+ b    }% h& e& j# L% j- ~0 v5 b
  }) Q+ C* y- E/ D4 w5 j  ]! u

" `' f$ l+ u4 \; [1 f$ x  return 0;/ a* K5 C+ J' u: X) c- X. j' h
}0 z. S5 m& T% a6 D0 _  D
) `7 F8 N$ G% ~8 c3 h, _$ `) Q
//look for the command if it exists.. t8 V" [' K! r' ~: o
bool has_command(char key, char instruction[], int string_size)
$ ^# g# L9 e( x7 X6 k+ O! @; I{4 O- Q0 q9 {- w7 L; H" o: R  r+ }
  for (byte i=0; i<string_size; i++)
* Q7 J+ C$ K+ Q7 |; w  {1 M5 Y: X& J; J4 V
    if (instruction[i] == key){
6 l! k, ~( r' p! d. R  A" m# j9 e' D$ x2 y% V* W
      return true;
) q- b% N! j- c    }! P, }+ E; `( p2 D) Q
  }1 V; Y2 _, V5 ^0 }9 e
4 j1 [4 M5 `" m( V: k+ i
  return false;8 `" Q) w: B8 k2 W! D
}
" V3 s' ^/ \; r+ D" ?5 e! n  Y- ^. d. E, ]) [7 ^# Y
, M& \/ B6 |0 B$ b) |  |
+ ?4 j% N$ r, e! ?( n
回復 支持 反對

使用道具 舉報

8#
 樓主| 發(fā)表于 2014-5-10 09:55:37 | 只看該作者
0 k: M4 q" n) {, c5 q) ~
//init our variables( A7 c* [0 C5 `( n) Y
long max_delta;
! E: f9 _2 `) y1 Q1 g, Zlong x_counter;% k( b9 p5 X8 P& C5 e
long y_counter;  g: u! e& P9 f( e% e& ^! ?
long z_counter;
/ G! o  ]* L6 q8 P% S; |- J  Llong u_counter;# C/ H$ D5 G' @" R
long x_pos = 0; // x position in terms of absoloute motor stepps
7 U& x) b3 j3 h# q: a5 q; x. jlong y_pos = 0; // y position in terms of absoloute motor stepps% ~* ]* E: [* i
long z_pos = 0; // z position in terms of absoloute motor stepps
" p( C# O! _# _& H% G$ ilong u_pos = 0; // U position in terms of absoloute motor stepps
- m" ~% v# Q& Q
8 E  c' B. q! K* x2 c+ ?& h3 k2 qbool x_can_step;: K- H1 T0 u0 a8 i5 q; N$ b: T
bool y_can_step;
& G. q+ K1 m# R! N) lbool z_can_step;4 D. H" R+ \9 f0 [$ m8 T& L
bool u_can_step;
. S. @0 O! Q5 v7 |. Y' r' Hint milli_delay;: Z- ?+ M) c4 c5 c- N6 @$ `5 F

8 @3 N; B3 S0 l' xvoid init_steppers(). A  }" O3 e% q  W3 l3 k/ D: t
{
& X0 p9 W: ?7 F" U' H  //turn them off to start.
! ?" `. j: ?* b5 s6 m7 }  disable_steppers();3 e5 t: O' W8 t& L' L* Y+ j! |4 D  H

9 c2 w2 J4 a1 z; X1 r4 c) n  //init our points.( h) h; B9 F2 @: D+ `2 _3 w, d7 s$ I' h
  current_units.x = 0.0;4 ~, m( I. P: X# t
  current_units.y = 0.0;
( S' z+ D' }! x( x  current_units.z = 0.0;
* P' @# B7 q1 i) q8 d  current_units.u = 0.0;% L1 }5 C4 J' ~' Z' G; G# Z4 L
  target_units.x = 0.0;
! A  m4 g2 c( f8 |4 w: m  target_units.y = 0.0;
' W- g. x- K0 {1 s6 W: ?8 K  target_units.z = 0.0;
* f& ]7 ~4 f& D" ]. b  target_units.u = 0.0;1 r5 n$ p  `5 j
  8 H* E8 p& D. K- c

* f8 _  p3 \  m* m7 \& U" T  pinMode(X_STEP_PIN, OUTPUT);' G- N7 _# R, ^% M# R
  pinMode(X_DIR_PIN, OUTPUT);0 P3 a/ M5 i' a  Z
  pinMode(X_ENABLE_PIN, OUTPUT);
) \: f5 i0 O! Q6 w. ]  pinMode(X_MIN_PIN, INPUT);
5 ^4 s7 F0 S7 w( z5 H4 Y$ c  y  pinMode(X_MAX_PIN, INPUT);1 t- J* t' e) i. \( `) u7 h
  h* W8 i# U. ]# _; Z
  pinMode(Y_STEP_PIN, OUTPUT);
3 U+ W7 L. F  s3 J# G  pinMode(Y_DIR_PIN, OUTPUT);+ b% x* b2 B- {0 y  d$ ]
  pinMode(Y_ENABLE_PIN, OUTPUT);2 l3 g6 J' t, F( g2 H
  pinMode(Y_MIN_PIN, INPUT);# L/ [4 p, x( X$ q/ \1 h( W
  pinMode(Y_MAX_PIN, INPUT);
) r) s/ x1 K; C4 \) I. |) S4 M4 `, H
  pinMode(Z_STEP_PIN, OUTPUT);1 \& G+ Z7 L- U7 Z* T2 `% B0 o7 ^
  pinMode(Z_DIR_PIN, OUTPUT);
7 W* g- q8 o$ n4 p  pinMode(Z_ENABLE_PIN, OUTPUT);
* u4 }7 i, O8 W3 R$ \% ^: l7 g  pinMode(Z_MIN_PIN, INPUT);
: u" ^% ^8 U# I+ _; p3 R  pinMode(Z_MAX_PIN, INPUT);
0 {. N6 Y. b9 }  e+ f0 ~8 S
, S7 M* Z$ r+ d4 V; _  pinMode(U_STEP_PIN, OUTPUT);# T1 {" ~  D. g
  pinMode(U_DIR_PIN, OUTPUT);
1 }& k9 g; y+ r. a/ z6 C  U- N- u0 d. U  pinMode(U_ENABLE_PIN, OUTPUT);
/ ?" Y& x( t0 H9 A1 i8 }! T7 ~& }  pinMode(U_MIN_PIN, INPUT);
0 S/ i( F6 E- `/ C  pinMode(U_MAX_PIN, INPUT);
3 P8 @' `' A, {* F  //figure our stuff.
3 G: }' {  [2 h1 s% {5 \  calculate_deltas();6 W* N7 b! {/ J! w
}
8 l7 `0 D! b( Z9 u2 r9 h: g: L. _: n; D. f. [
void dda_move(long micro_delay)
  ^3 N1 h% y' G{
7 V4 }* N9 Z2 d0 j) f) P2 d  //enable our steppers; U4 J3 r1 t6 v8 X% K" B; [
  digitalWrite(X_ENABLE_PIN, HIGH);* D4 _0 L6 D9 x7 B1 R3 B
  digitalWrite(Y_ENABLE_PIN, HIGH);
, U; c$ f0 M" P+ Z8 O  R  digitalWrite(Z_ENABLE_PIN, HIGH);
6 A5 Q4 d# b! L* E, ]+ [. ~" K  digitalWrite(U_ENABLE_PIN, HIGH);+ H/ a( i# i' M/ a- J( ]' g2 e
  //figure out our deltas
/ I& V; e" m' k5 H  max_delta = max(delta_steps.x, delta_steps.y);- H& ?' `$ @/ g6 K9 O# z
  max_delta = max(delta_steps.z, max_delta);
1 a% J7 n) Z1 [* v2 \: K+ b  max_delta = max(delta_steps.u, max_delta);
# S0 S" p" W  N. J$ F9 A9 ~# R' n8 Q  //init stuff." S3 w% v/ T5 h2 i
  long x_counter = -max_delta/2;2 o3 i& @: g. T9 y
  long y_counter = -max_delta/2;, j1 d0 y  l5 C% M- d$ c* F
  long z_counter = -max_delta/2;
0 w# v, F) J. S) c7 n$ u  long u_counter = -max_delta/2;' |* Z7 {( |3 x" I) J
. i* p: {- k" U* T" N, A
  //our step flags5 B3 i+ n+ v7 s8 a% h
  bool x_can_step = 0;( h8 n* W7 x* H+ j$ g7 C0 Z. i
  bool y_can_step = 0;: Z! z( f4 v- y! v, e# `, q
  bool z_can_step = 0;/ M9 M9 Y/ A2 \: [: M6 x8 J9 ~' N
  bool u_can_step = 0;5 t( T, B# K2 h8 \% y) r
1 U3 v  {9 l; G( P, y' f
  if (micro_delay >= 16383); [! v2 B6 Q; f/ f
    milli_delay = micro_delay / 1000;
; O) q( w. D& }% w1 I0 D1 b6 S( s  else
% o% y( \$ [4 R' z$ d9 {    milli_delay = 0;  k; m6 t3 _+ N3 w5 J, z, y/ U, ~
; {: T/ [& W  h* M7 m4 ^

8 A: a1 c  L2 |* O$ J/ U9 d1 s  //do our DDA line!( T. O7 D$ R! E5 u* o, S' N! D

+ M4 }* ?5 j$ u  ^) P% l  do
4 z/ ^( I/ x. o2 b3 ]/ N8 |+ N  {
6 O  i; q4 x. I2 y4 l% S: v6 `    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;
/ o+ n* ?' ?( a) n3 l2 v5 t5 M    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);* I8 Q- H& A4 h. t" Y
    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);) s0 I$ b* ?. s5 @6 T& }
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
# y. p3 J0 l5 U7 i7 J    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);
* P) M" M; p( C& C! }, K+ W    if (x_can_step)3 O$ X: D; w: f$ I2 \/ ]- j6 Q" Q/ S
    {5 p, i8 |7 O. L
      x_counter += delta_steps.x;* b- z# {8 z! y+ I5 ^4 R0 _7 f1 q; k
, E/ k" G% Y  X: W3 S: S
      if (x_counter > 0)7 ~5 P+ [4 e7 A* C- N2 ~
      {
8 f+ X1 h0 C1 e        do_step(X_STEP_PIN);1 Q3 m* u: q; z# g
        x_counter -= max_delta;  k2 ]0 R) c6 i8 |3 E
        if (x_direction)
! \+ M# M+ ?# E         { current_steps.x++; x_pos++; }
- g6 r( H: V1 ?% H4 P         
- s4 l% \/ x$ h& e+ [6 J8 r1 ]        else
  |# o+ l- M$ O: i4 `: s$ K( K- K          { current_steps.x--; x_pos--; }% g7 b' u( |; X' D, f9 f
           V# u, m) W" M; W( l& k
      }
  O6 R/ @* d3 J% Q  D* b2 a) F    }4 V4 d* b5 \4 B, v9 i' U
    if (y_can_step)
6 e0 V. P% j2 E4 Y1 T9 f    {9 b( O+ ]$ p$ u
      y_counter += delta_steps.y;
0 T  P% [, [, m* `: w3 ?. C
5 k* x: r$ C) `+ i0 G      if (y_counter > 0)+ b8 S7 m7 L, ~- T. o
      {7 F  @, U5 F# V
        do_step(Y_STEP_PIN);
# D' V2 b* m% e/ A/ s        y_counter -= max_delta;7 K8 y  S) @3 R* O

+ l3 `0 I% c: H6 u; F- C0 `! m3 C        if (y_direction)
1 K5 V' v# U8 K: R  M0 K( V) @        { current_steps.y++; y_pos++; }6 q$ @3 k: H0 y. R7 h7 Y, J3 p* G9 e
       . L9 `( e: x$ u! A1 f0 t
        else
) T& H$ ~. l9 v# r' Z" l, V" u; r        { current_steps.y--; y_pos--; }
2 }% a' b- C: _1 j  H        
" D6 m* V+ s- u* |) w      }
4 F+ j. `7 l: w- \5 K5 O8 [  z    }
5 O# n1 Z1 J9 B7 S+ M
/ D& [: D8 ^) I    if (z_can_step)3 U: @5 B4 }1 x' y( j1 e9 X
    {( C! @8 ~. s9 b# h& f- _
      z_counter += delta_steps.z;$ M. m8 F7 I7 V* O
/ Z/ a3 c$ n: p& X6 l2 q6 R9 _6 e
      if (z_counter > 0)/ f2 ~/ h) }3 o: V
      {3 H( Z. P6 g/ x+ s5 _- h% Z) C# ]( {
        do_step(Z_STEP_PIN);
# @5 q: k+ l9 G0 F& z        z_counter -= max_delta;
) ]6 p: |0 U# q) S/ G* i( h  F. J- j2 u+ ?
        if (z_direction)
/ E3 q7 _+ i! P# i8 N        { current_steps.z++; z_pos++; }4 Q) }, L! c/ B  Q: @7 T
        . s  K  F# }0 [( f* ?3 B2 z
        else2 z. N! v3 Q  H, w4 q8 ^" R
        { current_steps.z--; z_pos--; }" y; ~. L; m, X3 w
        ; G# ^2 c7 I5 [* J; Q
      }' ~/ B6 T* |% n* n
    }
# n( [" ~$ O3 }$ h0 p   
- C: f) F7 s8 k5 ?    if (u_can_step)0 l5 P* g( t; ?! k. Z3 h
    {% I# ^  l* A9 w. o( L5 F* _2 r
      u_counter += delta_steps.u;
6 W  w& B0 x' z; K7 ]; A* b' R
; \' J8 v4 X' x) q- D/ ]0 K      if (u_counter > 0)
6 y5 g- d- M5 s1 {/ ]      {$ L  l1 Z& @$ Y) J9 N& u* w
        do_step(U_STEP_PIN);- B; |2 B( p- x- `
        u_counter -= max_delta;* `& p5 w- T) N  O" A
/ i0 R% U- W/ u8 ]  Q. F' B& I
        if (u_direction)8 o1 J6 D7 `, S
        { current_steps.u++; u_pos++; }# ?- Y. h% V! A" m7 j
         
( ]9 T% k$ p* v5 A3 g( h1 ]2 K% Q        else' e6 @9 g& f& z$ H
          { current_steps.u--; u_pos--; }* M8 a, \3 _. Q( \5 F
         6 W) V7 n6 ~* {% N
      }5 i, J9 j  a& j3 ~
    }- `# ?* y4 b; K; t# j5 ]1 X1 c) W9 z
    //wait for next step.
! r) c# x1 ?5 I. r  `. j  [) Z    if (milli_delay > 0){" ]* _6 R$ f& W/ J
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
5 F" ^6 R8 A: o( q1 U      delay(milli_delay);. \" @: R2 e% j7 j6 K
    }                ; F7 G2 q* o+ g5 U, z  L4 j
    else{
7 I4 M7 f7 f* k( P+ k; m; u9 d      //if (digitalRead(BUTTON_SP_EN)) SPEEN();4 T% d( X0 _$ s& {1 O+ C0 }. K
      if(micro_delay>0)delayMicroseconds(micro_delay);9 w. O, v3 b* K7 i9 l( m
    }; g5 T- J6 d' L  A7 H5 \3 \7 ]
    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);, p0 q2 a5 p0 D* ^2 o
  }
8 R7 W. F( j; c* Y  while (x_can_step || y_can_step || z_can_step || u_can_step);
! f: g  t) V3 w8 B: W
0 q  n' b' g3 c% n' M: G+ o: M2 \. a  r# i
  //set our points to be the same
8 s( Y( H$ O5 n* L; C$ J  current_units.x = (float) x_pos / X_STEPS_PER_INCH;, F* Y' `$ q8 [: P( @
  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;
) T0 S( L) G: Q$ F  b0 O! U  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
$ I, ]+ |7 g( @5 t" U7 s  v  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
* j7 G3 D% k2 e/ ], [8 V+ ], n; u  1 p6 R& T8 x" \  [1 B9 {
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );2 e2 r; I$ v0 U) R4 P2 F+ H2 g

0 M) U7 F: n9 n. P7 S6 U  long x_pos = 0; // x position in terms of absoloute motor stepps
0 ^) b- |8 r- V' |  long y_pos = 0; // y position in terms of absoloute motor stepps
4 B9 c6 e) f& c  y  long z_pos = 0; // z position in terms of absoloute motor stepps
* K- E8 b  g9 q! _# ^# x  long u_pos = 0; // u position in terms of absoloute motor stepps
% i: a4 |& ]9 p" }. {. Y- j  calculate_deltas();
5 E$ _9 k8 B& H  & G7 w  f% t5 B/ z* @) Z  [3 N9 O
}
. h4 M- `* b' H9 M2 c
" P6 W$ {2 i" B: N- [* ]6 t& Sbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)
% k, e# Q  m( `, A  ?{
2 ?! R0 q, S3 J/ u. N" H" y  //stop us if we're on target
. ]8 v$ b4 L/ X9 J1 x& w7 U  if (target == current)" _* a$ `. h* n; Y
    return false;
) ~2 m/ A* p: T2 P  //stop us if we're at home and still going 3 t1 Q- d5 P' p7 O
  else if (read_switch(min_pin) && !direction)# e$ a2 e. S- {  k
    return false;
# _* p% q( n! \) ^; y  a  _  //stop us if we're at max and still going8 a' r, ]% V  I- }- x/ x; |
  else if (read_switch(max_pin) && direction)" v( j, a, L& h' m5 ~
    return false;
% ]( U) _! y$ I" |( l7 W) o3 z/ v# Y" {' b0 k* W
  //default to being able to step1 _: ?" ~+ p+ r2 L2 u& l& k' X
  return true;7 K6 Y* ~  \" i. F
}; u! d: G! b6 V7 l9 @
% C% C) j% v4 }3 u6 w: ]
void do_step(byte step_pin)1 W# t/ I% O* J& y; `  i" D
{1 @5 h: D/ [# `
  digitalWrite(step_pin, HIGH);4 R5 L$ k, V! w
  //delayMicroseconds(1);
7 m# L4 k) ~+ u, o- ~/ U! w  digitalWrite(step_pin, LOW);
, i/ s$ X% U+ W8 `, k}3 O8 [4 E: R4 [' j; b& D
% n$ I, F0 l/ a+ _$ C
bool read_switch(byte pin)8 c: i6 x* l) f# o: o. J1 }
{( Y( j  ^+ N: s
  //dual read as crude debounce* T9 a$ G' m! n& s2 |. v6 Y4 q! E

, y: y+ ?3 m: m: O# D" V1 y  l  if ( SENSORS_INVERTING )
& L/ q! k' D! i0 K    return !digitalRead(pin) && !digitalRead(pin);
0 x( |% t# t) |" G+ d% Z  else; _' m- x: j* X5 D7 I6 w, I! V; e
    return digitalRead(pin) && digitalRead(pin);
8 c3 B$ m: O4 S1 {5 P}
5 e  Y+ Q2 G; d9 N6 c( H( |( D) z0 w" T* M1 r
long to_steps(float steps_per_unit, float units)
! R5 ]+ g! Z- j/ _- n6 N  {{
' r* t: P, e: J/ B3 x1 u  return steps_per_unit * units;
. ~& c4 L$ L7 U2 P}
; q' k4 x( f5 B
3 @) W# [$ l. U/ ^void set_target(float x, float y, float z, float u)) v: w/ h& }$ z- _4 ^
{
- c: G( C( q; \, U* ?: Z& e  target_units.x = x;
# M' j& u7 M$ @1 B% t/ O/ y  target_units.y = y;
6 Y: _. e* ~- M+ H$ Q& k6 j1 o  target_units.z = z;
! b  S" c  T1 c" p2 I  target_units.u = u;
  e" o( C/ B3 g/ Y6 l5 G+ H" s  calculate_deltas();$ a! k6 {1 ~# U1 k# x  \4 h2 f
}
( T6 @# Q0 R2 \& t* u
4 x( p# {4 X. u' d, c2 ^9 tvoid set_position(float x, float y, float z, float u)
# B' u6 l: s- ^# H{
' i& x8 D2 t6 ~6 \) m  current_units.x = x;
0 k! x' x9 @# Q7 d8 [6 {* g8 v! j) W  current_units.y = y;
' {4 E- W1 S! N2 m  current_units.z = z;& i4 j! H, Y! I$ I- S
  current_units.u = u;/ S1 y% s0 v2 z
  calculate_deltas();) [) n1 o- E" b- s7 \$ U
}; f+ \: A9 O0 G) \2 L  C

5 ^6 p. k  U3 w6 j* l3 Jvoid calculate_deltas()
; ^% s$ R8 w, H/ p/ T{
9 _$ N7 P+ H. B! C. r  //figure our deltas.% L( i% U) y- V4 A3 j+ K
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
3 y7 m# z# Y9 h. a- C  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);
8 y, s( d* @6 `! I/ r# r% v  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
  |6 W, u4 z5 m# ?4 s  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);
) J# H: B% G& p# ]* H, J9 b$ J9 v7 K4 K4 I4 F
  //set our steps current, target, and delta3 A0 j, J5 x. S9 t" H6 `8 C
  current_steps.x = to_steps(x_units, current_units.x);& A$ L5 V* q1 a8 o
  current_steps.y = to_steps(y_units, current_units.y);
" W% p  U' v+ S  _  current_steps.z = to_steps(z_units, current_units.z);% b; a; l: H' d7 z
  current_steps.u = to_steps(u_units, current_units.u);
4 M- M% m+ {9 A- M' I2 \( X
9 X* s$ S9 E( n9 \+ L. F  target_steps.x = to_steps(x_units, target_units.x);
' Y2 [* {4 S8 p: k  target_steps.y = to_steps(y_units, target_units.y);0 i$ w( s( t) \$ ~
  target_steps.z = to_steps(z_units, target_units.z);
+ m, |( g5 x6 H( `  target_steps.u = to_steps(u_units, target_units.u);+ d8 j: u( D% F$ E" k/ h
1 Q6 U# l3 d8 |9 {: V# g9 ?
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);/ x0 b/ T, k$ C; W
  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
" D) A9 g" q* e- ~% V& x- h: g, O  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
* X1 K$ C% g- a# K* |4 s  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);4 u2 i. h- j4 H
( J, N( F" H8 [- w# ^/ l
- O: C) R+ \( F& P. m! E7 J6 q$ x
4 v. [" T1 O- z+ k: S7 W+ l
  //what is our direction
2 T9 ]& K9 X* c9 |; R  x_direction = (target_units.x >= current_units.x);
8 U  L  F! f1 E7 v) ]( T, J0 \  y_direction = (target_units.y >= current_units.y);9 N5 ^; r0 |, @  `! y
  z_direction = (target_units.z >= current_units.z);7 `# P! j) J0 V- L5 }1 _" |
  u_direction = (target_units.u >= current_units.u);
% q( o) d+ T* q/ r% y) n9 v; {6 o0 ~6 Y% S) z
  //set our direction pins as well7 O( O3 g8 F/ a6 m( M" p3 F
  digitalWrite(X_DIR_PIN,x_direction);
  L* F$ K' v. X1 ~  digitalWrite(Y_DIR_PIN,y_direction);8 @9 k/ G9 j+ H9 K
  digitalWrite(Z_DIR_PIN,z_direction);
% p- C! M' |7 a  digitalWrite(U_DIR_PIN,u_direction);
7 s* T1 K( e" r( Q  s7 \9 o
+ }: t: A/ u4 C$ ]! r2 \  C' U  //繪制LCD
% ]$ A. F$ i( r6 b  LCD_DRAW();
' k( Q7 c9 h8 t" p" w; ^/ c0 d, q% F' f8 H
}) r! T7 h) X; w. |, _

5 r& `- i. j( H1 c( F* v6 O: X2 Z% f$ g6 f: S
long calculate_feedrate_delay(float feedrate)  K' `+ P0 I! f- }: C6 C3 \) L: E
{
: Q6 W( T" f: L  //how long is our line length?
  ~! b, ?3 i- I- s3 d) e  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);; d' R. {# {! j/ P, b
  long master_steps = 0;
, `8 y$ |4 u7 G$ c0 `9 I, i+ n# e* x, Q5 h1 K8 \% d
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;: Z9 n% K  ^+ K
master_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;# h; _- t9 t8 N
master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;) ~7 B( w9 w# F" ?; p; h% W9 E

8 w1 p7 G! d) G4 R+ i; [* @5 n/ N" o# Q2 Q8 H% W1 n: V
. ^1 r; T" R* K

$ v4 U6 E, H: {  [" h( a  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.
9 B$ P3 E- d; [  R4 A' f# B  //the formula has been condensed to save space.  here it is in english:
$ X5 p, n1 E* g/ t: L  m  // distance / feedrate * 60000000.0 = move duration in microseconds, a% [3 \2 Q) |$ p6 C/ a
  // move duration / master_steps = time between steps for master axis.
: N: t6 B3 R/ P8 m0 B return ((distance * 6000000.0) / feedrate) / master_steps;
# Q/ F& B7 }0 r, D- a4 ?8 _% r% d: }: E) g$ S$ J9 D
}( l+ T- j8 O/ M' R5 i% r
& r/ Q. t4 V2 L- x9 g: P
long getMaxSpeed()! b+ B8 H- B5 u, l* A
{  s: R& c$ t. A" y
if (delta_steps.z > 0 || delta_steps.u > 0 )
$ p+ u& W# l4 @    return calculate_feedrate_delay(FAST_Z_FEEDRATE);
9 Z; q1 y! `8 w! Q  else
0 F+ \& p. t: }4 [/ }$ `+ ^. ~    return calculate_feedrate_delay(FAST_XY_FEEDRATE);5 G3 c& Q& F  U
}
0 m3 O# H- e( I( v
, ^8 K2 q4 f+ \" V5 H, rvoid disable_steppers()
$ c! V2 `; s4 r5 s{: S1 S  v) x9 H7 v, Z* k1 V
  //enable our steppers
" p& a4 m6 r, W: ?7 r, e, p  digitalWrite(X_ENABLE_PIN, LOW);: T/ U0 z- C# `, C, p* i( M
  digitalWrite(Y_ENABLE_PIN, LOW);
' R* Y* M& T8 x* B& _  digitalWrite(Z_ENABLE_PIN, LOW);
1 A' _- Q) F5 U* w: R' P8 X" S( o  digitalWrite(U_ENABLE_PIN, LOW);
" l3 w8 s+ x$ H- h}
6 d3 @8 }7 R6 H5 e1 Y$ O* ~/ V+ C1 |9 [

7 l8 i0 S' D0 \3 Q- P4 Y7 I  \//繪制LCD' j3 I5 j" g" @9 V: H) z# M
//unsigned int DRAWCount=0; # h: B: s: I' j6 r  N
void LCD_DRAW()
! r9 D) N1 u. {$ g6 v/ G{# d  Z( r2 p9 T# x6 e& c# }
    lcd.clear();
" L3 F& d) j% V+ ^7 ]/ e5 V" G    lcd.setCursor(0, 0);
& r4 e  b+ o& I( `- u) M    lcd.print("    X=");/ d; V0 N: l4 W( K" g9 l
    lcd.print(current_units.x);
0 m2 c: ~$ B% `. `& D* J    lcd.setCursor(0, 1);
9 N/ \+ x  I7 T( F; s, {$ J    lcd.print("    Y=");
# b: n( L7 Q9 O    lcd.print(current_units.y);9 ?9 L" D# C! \" Y( d! U
    lcd.setCursor(0, 2);7 ~0 u6 ~. e( O6 H5 v
    lcd.print("Z=");
  D9 E0 h. i9 J    lcd.print(current_units.z);% a) `  u5 h( a$ n2 P
    lcd.setCursor(0, 3);9 d+ A: ]. U; e' F" x
    lcd.print("U=");' ^7 y  c4 o/ x6 `1 ^/ f" h
    lcd.print(current_units.u);* M! z' }$ e, O5 R) f$ Y3 e, M3 `
  }9 q  f5 ^5 L2 I+ y! C
// else if (DRAWCount>=30)DRAWCount=0;
7 {: v. u( K5 N7 E+ D  K//  else DRAWCount++;
9 A8 l4 Z6 F: }/ b" K void SPEEN()3 a8 c* E7 n# `9 s
{) _& ^% W, I% m9 D+ Y" F
   delayMicroseconds(analogRead(SPEEN_CTL)+1);
% b9 b9 K# }  P* ? }: I8 W/ o; a) _, Q

! G6 ?" L9 a$ _2 M. l% r* M# c, Y0 F+ k) X
//delayMicroseconds(analogRead(SPEEN_CTL)+1);. z; E6 K- Y: x3 y, f
//if (digitalRead(BUTTON_SP_EN)) SPEEN();
- v, g6 i0 A2 H7 z, x  K9 t5 _* T( }6 Y  u

+ O2 X8 r5 Q& O7 t
回復 支持 反對

使用道具 舉報

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, 2024-11-8 11:36 , Processed in 0.067392 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表