国产日韩欧美久久久精品图片|国产综合有码无码中文字幕|国产一区二区综合视频|国产亚洲精品电影网站在线观看|国产精品一区在线

機械社區

 找回密碼
 注冊會員

QQ登錄

只需一步,快速開始

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

運動控制入門篇GCode_Interpreter

[復制鏈接]
跳轉到指定樓層
1#
發表于 2014-5-10 09:39:53 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 xmdesign 于 2014-5-10 09:42 編輯
5 L3 ^# \) I. V. J% F3 x# ]0 t: G7 b( d
首先聲明
) r" d( ]+ T* m) C; _" T我不是專家,業余搗鼓; x3 d( B! x  t) _
源代碼首先來自網絡
5 p4 H- w" O) e開發平臺Arduino及Maple, E: w3 e$ E! {8 w
http://www.arduino.cc/
- ]" u% K) [9 m% _; p3 O7 @; mhttp://leaflabs.com/devices/9 d: R1 X2 z$ I( D
國內活躍社區2 J( ]1 w% ^+ ~. c( e
http://www.geek-workshop.com/forum.php ; {2 V0 q& a$ L3 b/ B

& N# ]# f% T/ f$ K3 H竟然缺少積累,首先尋找最大的開放式開源平臺,這樣可以積累全球范圍的創客和DIY者的腦力活動經驗
# H4 g- C4 d* w# C來入門了解熟悉思路架構等
- u" e3 b& z; _- W
! \1 l* o: F( r我只搗鼓了下8位機Arduino 移植到32位機Maple 方面一點點事情,0 ^& ?* M  w- r# m$ `
許多功能還木完成,不過作為低檔次得應用可能還有可能' S6 q$ b) |2 ]6 K
! ?! n/ `( Z* V2 P
我自己也是個半桶水,再樂意玩的起碼自學能力還是要有點吧+ _6 G% p. C4 E8 }
2 w& \7 e. p7 U2 m# C7 h% |
拒絕 所有的求6 ]" C& z7 }+ A, X( X7 h- ^3 f
求人不如求自己 不然木玩/ S8 Z5 H5 ~, U+ ^& f7 y

' f/ b% N$ m; w/ u/ g高手繞道 謝謝!
: @3 i, T, v; F, j5 @+ C  l/ m0 m1 I
回復

使用道具 舉報

2#
 樓主| 發表于 2014-5-10 09:48:37 | 只看該作者
本帖最后由 xmdesign 于 2014-5-10 09:52 編輯
# ]; J% f' y# m+ I$ @# N1 J, B7 n/ S7 r$ K9 r, h0 O/ f
GCode_Interpreter是比較容易懂的,木那些寄存器等蝦米開始不容易懂的東東, ~/ B+ [+ M3 E% B+ _, X
貼代碼先2 L1 X0 O+ q$ H; \0 z
直接Maple的,某寶許多超便宜哈$ @4 n" [  |0 h' H+ O
暈,我怎么上不了RAR?' l* E3 F3 t8 m3 p8 ^
想玩的留下 e妹吧
1 X3 @7 v7 c/ X- e5 V/ K, v* }2 R9 [第一個妹麻煩傳第二個妹哈
$ t6 M$ Q/ {+ T& |3 c6 X% z; M) W我平常雜事多7 n3 Z8 n) E1 L8 J
誰放網盤上再,麻煩開放下
回復 支持 反對

使用道具 舉報

3#
 樓主| 發表于 2014-5-10 09:52:53 | 只看該作者
// Arduino G-code Interpreter for Rep Rap6 o: S" U0 f" }, P
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
3 N! D8 i; Y" ]4 b: W// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)6 D8 F, O8 n6 W8 l5 Y5 ]
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
1 v& D8 ]. H5 i: c4 ]3 n// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
) u5 z/ _# t; |- a4 E
& G+ U4 s) I' T$ [, x% l3 ^// Arduino G-code Interpreter for Macro / Micro photography
  K/ s/ A$ |3 {3 F// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
8 _3 \4 e" s2 ?" Y8 O0 {0 t  j7 ~//modified by YaoHan China 2010.2.15
  n9 T, m; j) F4 b/ b6 G//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
6 t6 D& B# U: m4 R, A% j: y, h8 q#include <stdlib.h>
6 d: t2 j+ `8 S7 |4 Z6 p#include <LiquidCrystal.h>/ E: L, j, m& o
9 J& R( Z2 b) b' V& Z4 r8 j
//啟動(高電平有效)/暫停(低電平有效)  默認高電平
) |! d7 T# g# _! D' k4 Q$ D' A#define BUTTON_CTL 353 p* c3 s1 G+ V& R
0 K2 m$ F4 }6 f
//點動模式鍵(高電平有效)  默認低電平$ X- g* \& P, s* ?
#define BUTTON_SS 36
  t6 e5 V  ?4 `: V3 R* b/ b6 Q  \( G5 t* h
//點動鍵7 X, ^0 _5 ?0 H* `  R
#define BUTTON_MAN 151 m$ S# B* K; d

3 O2 e8 k" H6 M$ h4 A//電位器速度控制9 p! c5 Q4 {8 L/ f
#define SPEEN_CTL 16
4 |# W. E7 [+ _( r$ b" a
4 J# O$ S+ W; Y, @/ a' u//手動調速使能
  e. }. c* X8 n. E8 |. U#define BUTTON_SP_EN 17
8 N6 w" k. V) {8 r7 F, R& d; J: j' e" y: S8 Z2 r
//LCD 類型 1604或16026 N, w( }7 s) I1 P; P
#define LCD_TYPE 1604' e8 O. w+ _8 T- d# F
//LCD 引腳定義
" y6 `3 n- e  i#define LCD_RS 23
# m* l5 S: U( \) I- J3 M/ r#define LCD_EN 24
6 |9 X$ r# t" w" s( Y8 G#define LCD_D4 25
8 ?5 R5 K% C. z* n#define LCD_D5 266 ^& ]1 _  f: b4 J# k
#define LCD_D6 27
; J$ O2 b2 R: ~/ U) m! ]: Y( r: _#define LCD_D7 28/ K, g- C, ^  Y2 }3 m) f% ^" @

1 S) C: Y$ a/ l//命令字符串
* J  n* F+ X; }- |; H9 t#define COMMAND_SIZE 128; ]( E- y2 @0 L8 H+ S! X
char word_old[COMMAND_SIZE];
' J3 ]+ n/ }' j  j# C( Z" bbyte serial_count=0;
& N1 X) F  {. N' W! @" cint no_data = 0;% O" Y- N5 z; f2 j6 s
//LCD 引腳鏈接 配置9 k( U9 d$ q, V6 J
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);, t+ ?8 i. g2 F$ y0 d
//停止控制,高電平有效3 k1 T, C& I6 r9 ]4 m
#define BUTTON_STOP 30
! o( Q/ ^3 O3 F7 }int stop_flag=0;
, j0 n/ J: W9 M" q
) e7 i6 C& d5 F0 Z& ]& m//暫停- [7 w9 ~9 K0 @$ L  R
void pause(){
. |' u( c0 h/ j7 I$ P9 y1 a  while(!digitalRead(BUTTON_CTL));7 ^" Q, e1 N; o. Q( z6 [8 U
}
' n# E: j$ Z  E6 b2 [1 s
8 C, [! H. `, _3 R/ a$ j: Rvoid stopper(){9 V  @$ E3 I- l$ h, n
  delayMicroseconds(10);
+ w  \# J% h! X1 G  stop_flag = digitalRead(BUTTON_STOP);
, {1 |6 T, c" ~; i" u& [5 I+ v}
4 p* Z* K' V/ D" Z- N
5 |1 `7 `+ i9 P' i2 Q. O1 d* Jvoid setup()& P- |9 `- v# P, w6 n
{8 j" d; l6 R8 I
  //show start
& F( t' b4 K+ K$ U) p5 H- J% S+ n1 `  SerialUSB.println("start");0 ^, n) E+ \$ q: t% N( x
1 o2 x7 x/ E8 B5 \) M/ ~8 ?
  //啟動lcd
' k( K" F3 t+ ^: I2 F+ D" W    lcd.begin(16,4);6 i1 P1 _" g- u
    lcd.setCursor(0, 0);
8 l5 w  }0 @4 X! ?0 K1 s    lcd.print("hello, world!");1 @- J+ f8 T' l% f1 p# \( @

/ H1 B3 N) W5 L  //初始化控制引腳及引腳功能9 e; ?- @* }3 A. l8 d
  pinMode(BUTTON_CTL,INPUT_PULLUP);) e( R9 R4 {1 p3 \  r
  pinMode(BUTTON_SS,INPUT_PULLDOWN);
! R6 @2 w1 ^+ f  pinMode(BUTTON_STOP,INPUT_PULLDOWN);  g2 j0 v) j: h- b
  pinMode(BUTTON_MAN,INPUT_ANALOG);
# e4 }* K) z6 M" l, u/ ]( N/ e* g: K3 x: f7 ~2 e. P
  //控制引腳的中斷
4 Z' S" {. L) c8 i/ o; U8 E  attachInterrupt(BUTTON_CTL,pause,FALLING);//暫停的中斷
% Z' F7 ~) f. A. j" r  attachInterrupt(BUTTON_STOP,stopper,CHANGE);
7 r" ~) l  @' f4 _$ U3 p, w$ ?7 S/ Z) P1 L4 [, g. V8 T
  //other init
: _" x* i; I' D: j- ]8 T4 ?  init_process_string();7 ]9 [  k& x7 D; h
  init_steppers();
$ V8 K( B5 @! a$ n, O2 [- O  init_camera();9 r8 c5 }+ E) K" E2 J

, u+ J9 q2 n+ l/ M# H}3 }+ c7 h# U/ P: s6 L
- f# D! r  _- B" z) i8 n
void loop()% T/ V3 Y; p. I7 j
{$ p$ o  r+ w4 b$ ]/ e2 e
  char c;2 D8 e0 m4 p0 s1 F
5 X4 ~" z2 i) c" _# s# X
  //讀取輸入的字符! r; e. Q$ ?! S% f: O$ O) a3 H0 U
  if ((SerialUSB.available() > 0) && (!stop_flag))
2 o2 P, X* T* C$ W/ J  q  {
; o2 y5 D4 t3 U6 ^) F5 b0 Z% _    c = SerialUSB.read();, ?3 u% l  o' J9 W4 L$ w  K
    no_data = 0;
( ]$ r8 r+ |- a: K" a# l0 t2 [8 o4 u, t1 K  J9 Y
    //換行符代表一個命令的結束  L: a8 e, a( F9 v
    if (c != '\n')$ }0 I2 ~4 _3 B, J! l
    {
+ Y1 c# X$ k+ n, B+ t      word_old[serial_count] = c;
1 X, B7 R1 @% l& T: K6 x      serial_count++;1 I2 v$ g# N  Z1 D& D% E
9 Y' A' `* p) \+ p% {
    }" U' A/ c5 S* r* M# s0 W# B
  }: q# ^5 N* a# _" }
  //標記沒有數據輸入+ g! j) M; P( Z
  else9 a; `( R) `5 v! q: Q2 o
  {
- d, e# K. {( @7 X( N# E3 o9 ^    no_data++;; u' g& T9 {0 r  j, l+ g
    delayMicroseconds(100);
/ X" J$ u1 Y5 t. p, ^9 b  }  F7 i+ I/ n7 o9 [  g) Z5 n, |

# O. P; m6 b8 m) Z% l  //if theres a pause or we got a real command, do it
1 q" S% D  R, b! {' ]. y' A2 y  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag)). W/ g! G% T  Q5 i! Y
  {0 f8 x" u$ s$ q! m) C

; y' k0 T6 I2 _" s) ?7 q    //處理命令) \- O/ C0 @- n* |
    process_string(word_old, serial_count);
6 y4 z7 {" ?+ H! |9 `0 o" s1 G
9 X0 s( S6 q+ n) a    //清除命令
' \7 g# w3 h) m    init_process_string();( n/ I, ?4 K: z/ T0 R+ \' P
  }7 R  \, `$ s% n! V$ {- r
# f3 m& E' S: B# T  N" H- J) j
  //如果沒有數據關閉電機
" Y0 h# ~& ]5 D  if (no_data > 1000)9 i5 H  B( r) \  n4 M% a: \4 H. C
    disable_steppers();. T1 K% E  J8 y# {+ U

4 j. ^% N4 x6 j9 j. D8 D- I7 c! o8 _  //如果ss鍵按下進入點動模式
. \1 |2 f0 c. o. l. T+ u9 l  //if(digitalRead(BUTTON_SS)) ss();' c9 S" B+ `* S1 o( l# B. e
  run_a();! ^# t8 A" M* z, c3 A: H5 H% k  @- Z
}
4 {& Y' C9 v% ^( d. u
7 {7 o$ y% G! J! C. ]//點動模式
. B5 K2 p6 J7 l+ ], F3 mvoid ss(){) S% |" C# U( R! e

$ I$ a% s+ G6 g& O1 o& q+ Y$ w  delay(1);
- a6 o9 e, d' v5 U8 ?% E  if(!digitalRead(BUTTON_SS))return;
$ f3 |! t6 H0 w  if(!digitalRead(BUTTON_SS))return;
0 m( P' ^0 P" _2 T+ C4 B  //init_process_string();, s& ^. E; R6 X- e3 L7 p6 z7 W& p
  //init_steppers();
/ |4 s- E0 G8 n  //init_camera();
% O! s/ n7 k4 X+ l$ x7 ?1 O* j  p8 g9 {
// SerialUSB.println("Step By Step Mode");
$ x4 G" w! g/ }7 M+ ]/ K6 E. X//#if (LCD_TYPE == 1604 ); T- I. T5 x4 [0 Y9 `) t) A7 T7 }, k
  //當LCD為1604時顯示要處理的命令
* ~2 l; R7 \# R. Z   lcd.setCursor(0, 0);( ^8 [0 n/ c& Z6 C, _- X; ^. K) v
   lcd.print("Step By Step Mode");
% ?7 d3 T6 ?) m7 r% J//#endif$ B( \' V: F6 K- x- u9 M
  process_string("G1 F5000",8);  a7 S: {. \  a+ a# W
  process_string("G91",8);4 z3 T/ P# T; ~0 w* z
  //init_process_string();" f( T, X6 ]3 B6 s! w7 T
  V: D/ V9 s! x' R. X, f7 ~+ A
  while(digitalRead(BUTTON_SS)){
3 C  A- _+ E8 J) \1 Z0 F    int i=0;
+ x7 W4 q" t  \    i=analogRead(BUTTON_MAN)>>9;: b/ M1 K- c. J, O6 I" Y
    //if (i==0){break;}# R, s9 Y5 a. k% o0 L3 B  a( ]
   //SerialUSB.println(i);9 g3 L- F8 p# ~) e
   //delay(1000);   ' \; G) w8 I+ Q  A6 U7 }  G
4 d# Y* z7 W( ~% [# y  c" P3 A
   if(i==2){
/ U( R. A6 Y/ [+ C' h& T      set_target(1000.0, 0.0, 0.0, 0.0);
5 X+ n( Y# t- H- ^& t- w      dda_move(getMaxSpeed());
2 R7 Z7 J% ~/ L      if(stop_flag) return;
+ s5 h3 b% F- N! x4 I      //process_string("X0.01",5);
& `3 a8 L2 K$ d2 h' p- p, C8 _      //init_process_string();6 {8 \( a" V+ D
    }
3 |0 V: `" d! A    else : @: A7 _  h* N0 j: h9 o
    if(i==5){; b5 R/ s5 K$ W& N
      set_target(-1000.0, 0.0, 0.0, 0.0);
* G) y; ?. \; \, K      dda_move(getMaxSpeed());0 N9 B) v8 ^6 j5 |* Y
      if(stop_flag) return;" V& B, R+ e3 M9 j& H
      //process_string("X-0.01",6);
3 {; I# H$ z! e: m  d" j      //init_process_string();
6 f8 k9 l# @9 q/ C    }
0 c2 f; n& U! `$ Y+ [5 q     
9 K3 f0 v" a' v, K9 L1 I
/ A9 V* }+ a( N- [, t3 a; y  }
0 |, ~$ V  R9 q
( {# P9 o0 A3 d9 a1 ]- s" V+ ]  //init_steppers();
1 B8 g+ l3 h; C  //init_camera();
/ r0 @1 f. X7 a9 P4 v2 @  T' l  ]   // SerialUSB.println("Return To Normal Mode");0 _5 G; p- V& p# p" Q9 m/ }  ?
     process_string("G1",8);7 X# N& A3 x5 U+ s9 Y; z4 A
  // process_string("G91",8);: {1 M) s6 S! J6 I9 x
    init_process_string();
5 }- \$ l3 O! m//#if (LCD_TYPE == 1604 )+ m& ~' \9 l/ x5 G- O
  //當LCD為1604時顯示要處理的命令6 o% g2 ~2 [& J8 Q9 @  `
  // lcd.setCursor(0, 4);
- C* M+ z6 I) ^3 R1 N  // lcd.print("Return To Normal Mode");+ w# }7 v! m0 U5 g3 t* H8 r
//#endif; j( a: R! @8 e7 F1 @3 Z" x
}+ ~$ J- j: p8 c$ G' d3 Z

' d! O- ]% W: ~% Q& F+ rvoid run_a()
( D0 H4 m7 `3 Z1 C3 ]! G4 F{
8 L' `3 R7 f9 W2 o% \' I  //delay(1);
6 I1 u  R; w: u1 `  //if(digitalRead(14)== HIGH)return;
' K8 U3 x6 j1 t9 K  //if(digitalRead(14)== HIGH)return;
8 f! u* t! G# D  //process_string("G1 F2000",8);
( d8 b; \" b# d' X' H: H# l //process_string("G92",8);# T0 F( Z2 i) r' b' L" x
process_string("G90",8);
5 [' B( i, @6 D1 U2 M8 V  z2 j/ \ process_string("X120 F10",5);
" h8 ?& V% g6 F. L process_string("G4P2000",8);! o* w; o* |: ]2 ?5 A8 w7 T: P
process_string("X10 F10",5);$ E7 f2 S% N. u+ m' v' z2 t
// process_string("M101",8);# C5 m4 r3 J$ ^& k1 {1 g+ X
// process_string("X-50",5);3 o; Z0 U0 V* Q' F+ }6 |
dda_move(getMaxSpeed());
# J# l* y, |! m5 G8 a5 n; J init_process_string();
. U8 _8 d) d5 N. n1 R; t' p5 I$ A: g1 k3 B. R+ x: D- s
}+ q4 [6 `2 B; U  D9 V
回復 支持 反對

使用道具 舉報

4#
 樓主| 發表于 2014-5-10 09:53:25 | 只看該作者
# k! z$ N% }0 R
  m" j1 z1 I4 f% Q) Z7 w: A
// 定義機器參數& t4 i1 O: ~) E" j& k9 F$ `5 y: w
#define X_STEPS_PER_INCH 400: |! N& W3 b( E% N6 ]& X1 D. \4 e4 F
#define X_STEPS_PER_MM   16.03 ~, i: q0 t* W; g3 j
#define X_MOTOR_STEPS    200
+ a5 F0 Z; G8 a! C( K* B6 f; Y/ f* ?. b" [9 m3 z
#define Y_STEPS_PER_INCH 400.0
6 e1 R& p# P+ j6 A, n9 F#define Y_STEPS_PER_MM   16.0
: u, d  c+ u6 X; `6 a#define Y_MOTOR_STEPS    2003 V$ x; P/ P( n& \1 M8 e- ?
5 r0 B) S' ?& N! G+ B
#define Z_STEPS_PER_INCH 400.0
' e8 I7 L" Q3 K7 p. }2 F3 i0 v#define Z_STEPS_PER_MM   16.0
$ p; }. M+ E6 H) t' R- R#define Z_MOTOR_STEPS    200  E4 ]' u+ `' N

3 u( c4 u! E4 v+ S) N5 z, ~#define U_STEPS_PER_INCH 400.0
* S. q- a( t& Y#define U_STEPS_PER_MM   16.09 Q; J: Z6 N0 x- V% R$ d
#define U_MOTOR_STEPS    200
9 ~+ {2 G# P0 u* ]2 h* ]
  g2 Y( ?- N; \( h& g+ @3 z//最大進給率5 f, D1 }* D) i
#define FAST_XY_FEEDRATE 1500.0
0 p, ]6 ^, A2 H1 {; N#define FAST_Z_FEEDRATE  1500.0
& _% e) u, F) Q0 B, L#define FAST_U_FEEDRATE  1500.0! @0 \4 H( ~* S4 Z
// Units in curve section) p" k- H- c! a" N7 ~+ e* L# p+ e5 Z
#define CURVE_SECTION_INCHES 0.019685
. Z8 F& ]" E3 h#define CURVE_SECTION_MM 0.5
: v( J* v3 _+ s7 W5 f! p! f. l- x! Z7 j- E% e& r3 S

* \+ h& U" X# }6 C  X# D7 z// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
" U9 l4 c7 q1 O% ^. [! Y// RepRap opto endstops are *not* inverting.0 E! X& J/ M7 P* }2 m  O4 U( q! g6 A
#define SENSORS_INVERTING 1/ i9 G6 k# n+ j+ v+ d

2 Z  a% t1 o/ |; u! m5 [3 M( w/****************************************************************************************
( N+ c4 E7 ^3 d$ R' L7 w5 e. d8 | * digital i/o pin assignment
. M3 F6 H) G2 C# C  ] *. e+ I& }& m; Q6 S4 l* x
* this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5
* z( j0 R3 ~* _ ****************************************************************************************/" G- a' z1 r, [  j+ P5 c. q
7 Y0 l4 C3 i1 b6 c5 }* X( `& \% a
//camera shutter and control pins' U$ D3 U  f9 u5 e6 e* s7 W
#define CAM_SHUTTER_PIN1 29
+ [* J+ L& t# s( v' @: U4 j#define CAM_SHUTTER_PIN2 30
5 ~) j( l; G' s' A//#define CAM_AUX_PIN1 31 // analog 0
- z# K3 ~2 P. r" m) b//#define CAM_AUX_PIN2 32 // analog 1/ M  i6 g, j8 D+ S2 _8 F2 F
//#define CAM_AUX_PIN3 33 // analog 2
4 O2 ^4 W6 j5 W0 T% j//#define CAM_AUX_PIN4 34 // analog 3
6 G( B: O7 M, Q2 I. g- f" o& X: E# E3 ?! Z3 O
// stepper driver pins7 v( p0 U7 j' w, l0 O
#define X_STEP_PIN 7
& `, f. G2 E+ f& s#define X_DIR_PIN 8
) A5 u, ^0 L: I8 h) n+ r8 W#define X_ENABLE_PIN 19/ h! U" l, [4 k$ Q/ S
' N$ [/ T) n: y  ~8 L; S
#define Y_STEP_PIN 9& U! |9 g0 a& }
#define Y_DIR_PIN 102 i2 q1 a5 A+ s4 y0 i
#define Y_ENABLE_PIN 19% {4 R/ l8 q! f& k" O/ @; e
2 m; Q) N& D; d+ t
#define Z_STEP_PIN 11( K8 U, k, x0 Y+ c6 ]+ a" V, M. k/ z
#define Z_DIR_PIN 12
6 p  t" p, o* M3 \% T#define Z_ENABLE_PIN 19
% k: _; l! X( D" X+ X7 f7 h/ k9 I: n6 J( T
#define U_STEP_PIN 138 P. h, k* C: {7 M; V, \( e* P
#define U_DIR_PIN 14
. l, f* H" p6 p4 y, c) T6 i, A#define U_ENABLE_PIN 193 j. B! \0 I8 t
0 A+ c+ A$ `% K. S9 O
// limits not used right now
9 q/ ?  I) h6 T#define X_MIN_PIN 147 P8 _( }$ S- H  _6 p8 v
#define X_MAX_PIN 14
" n9 {% q& Y* |, S% a#define Y_MIN_PIN 14
6 W9 {8 _& _+ _8 \. \' f2 y#define Y_MAX_PIN 14$ a1 h7 T; |" x. r# q# R
#define Z_MIN_PIN 14% u- V0 ~, A, b2 ^7 _1 r
#define Z_MAX_PIN 14
8 ^% E! _/ o, d! p' L#define U_MIN_PIN 14# A' j# T8 B0 R0 N
#define U_MAX_PIN 14
  c- _6 t6 h1 s) s6 R
回復 支持 反對

使用道具 舉報

5#
 樓主| 發表于 2014-5-10 09:54:26 | 只看該作者
void init_camera()0 e0 _7 V  b, ?& w
{
( ?5 U  ?' V' D1 N2 T  pinMode(CAM_SHUTTER_PIN1, OUTPUT);
& h, Y# J  J) K1 o1 d7 V  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
* T2 I9 F" B8 O3 n! V  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
% }! \$ h4 A' W& W+ s // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
( M3 i5 i# F' m' @" n' ? // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 20 I  b( S6 J0 w: z+ w+ f% Q( N' y  j$ v
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
: |8 w$ L' U( ~5 k( e. _9 i4 }% x}
. P5 i, P5 F6 ?$ r) H5 [, p* j5 ]" L4 I
void camera_shutter1()
  Q% c5 F* |" Y5 ?3 A{
) ?# y( Z8 ]: Q, w3 z% I& Y, a  // fire the camera shutter via relay...1/4 sec hold time- G2 w/ C* z  {; _' K/ I
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
- E( K1 Q$ Y; T. x/ G! E  N  delay(250);: @/ W6 x& U# R9 i6 q2 Y
  digitalWrite(CAM_SHUTTER_PIN1, LOW);7 k$ P: D% x. S- O( D: v. I

' h1 @) O$ W8 f}6 V, T) {+ U* S7 a1 u& `% a, j
. G% K: O# @" A, e5 w; A5 I
void camera_shutter2()0 B5 x% C9 h* Z( K* I9 |& Q- E) [
{
+ N2 R3 u  I, O2 x  // fire the camera shutter via relay...1/4 sec hold time
& y' {4 ~4 q0 B& z  digitalWrite(CAM_SHUTTER_PIN2, HIGH);: i1 i- ~: S) B3 _6 S7 {; Y
  delay(250);( U4 f, X0 H% {2 i) U/ g
  digitalWrite(CAM_SHUTTER_PIN2, LOW);
8 S/ z- d/ B8 @7 A; W2 t+ [$ w0 d8 [. m! O3 \
}- _  d3 D" R8 {' M; v1 o
/*
8 W7 R' l" T0 T! {$ w3 Zvoid camera_aux1_on()$ U. H7 A, {- R
{
' [2 F! q( v+ i5 a. p6 Q. Z9 C' J  // turn aux relay 1 on
9 B+ x3 \9 S4 u, T& u- Z  digitalWrite(CAM_AUX_PIN1, HIGH);
% r- f  G1 ]+ H7 C0 k}
. c0 l: u; ?% p
0 x) y, u" Z0 f- h; w6 {: I- ^void camera_aux1_off()/ V/ Y3 M4 Y- H# Z6 x- z
{
4 x, r* `7 w) J& P# x0 _# Z! A& Y  // turn aux relay 1 off4 Q$ w) H; j/ d" f) p3 t' k+ {
  digitalWrite(CAM_AUX_PIN1, LOW);5 ^" O! e2 e6 i' E, Q' D: T( c
}- R5 e' F# `, ]1 b

9 ?7 U; ]5 i# T% e. Uvoid camera_aux2_on(), I  v: J* y% O/ B! F4 U9 H
{. s. T7 x0 @& D7 K; l' G# f: r1 q
  // turn aux relay 2 on
1 m1 V% x/ d3 T1 B8 T  digitalWrite(CAM_AUX_PIN2, HIGH);% M# V) A1 `  U) n
}
" q+ w+ l/ V6 h' D& `6 w
# j! G: q* C2 }5 tvoid camera_aux2_off()5 ~5 Y) N# l# o  u( e
{
5 q4 t! G; d# j/ m. b  // turn aux relay 2 off
, d9 P) w6 O! v9 A; F  digitalWrite(CAM_AUX_PIN2, LOW);( L2 S4 c6 T: R4 a
}
; P. v) z1 Q/ t1 h
# c2 v9 e0 n, s8 L9 [: Qvoid camera_aux3_on()
9 m9 a8 R" z& k9 J{9 q3 Y7 {' Q+ G1 {# b7 n0 s  t3 V
  // turn aux relay 3 on
/ T6 _* p- Z8 y) Q" t( N- j  u7 M  digitalWrite(CAM_AUX_PIN3, HIGH);
# l( |6 s  @8 I5 @}2 N% j! N7 r& G9 y& e. I5 [- Q) n

7 O, K. L, U! E0 H! ovoid camera_aux3_off()9 {. `$ T9 V, n0 c1 N
{- E4 M/ V& d" C- M( i# K  @
  // turn aux relay 3 off/ g7 n5 ^! P( |
  digitalWrite(CAM_AUX_PIN3, LOW);8 o9 m) C4 D) _0 s3 v1 i
}
( E) J: a/ ^/ V
/ U, s% g& W# e. z+ O: F, v6 Wvoid camera_aux4_on()
2 `% S4 P5 }1 s- O' T4 a6 d{
# C: W$ h0 }3 R1 E  // turn aux relay 4 on
9 m" C& ], E) h: v- T  digitalWrite(CAM_AUX_PIN4, HIGH);
6 _9 z; K5 S" j' P$ o/ W* Z9 c4 `}
" r# a& {; _% W4 s7 G# q; [5 ?0 {
void camera_aux4_off()' m2 s9 E! c- z9 i
{" }. ?& ^( B( ?! k8 U9 K
  // turn aux relay 4 off
- P1 Y) t; e4 @  digitalWrite(CAM_AUX_PIN4, LOW);
' H5 c+ q$ N5 _( b}
回復 支持 反對

使用道具 舉報

6#
發表于 2014-5-10 09:54:28 | 只看該作者
樓主推薦的網址不錯
; J5 ~+ t# j' j4 I$ U1 ?請問樓主是玩什么的,用樂高玩具嗎?
回復 支持 反對

使用道具 舉報

7#
 樓主| 發表于 2014-5-10 09:55:02 | 只看該作者
// our point structure to make things nice.
7 t9 p, |+ X, d$ A( Tstruct LongPoint {/ u# w! l& ], F. r+ Y  \' X
  long x;$ w. ~/ X! L/ `$ {1 `0 g
  long y;- W6 X; e6 z; r2 I* {" N
  long z;, ~7 z; t; O! |! `+ m$ H
  long u;
+ `2 ?8 E/ I! C  @};
& C: w- t/ f+ g% a9 C4 |. j# a) B" `  i- h
struct FloatPoint {
( f- Z. P* `% `  float x;
# l1 U  w! g9 x% V% x8 w. |  float y;3 E# U. p) E* h! c2 N$ Q; X
  float z;7 h, q/ s9 H4 s; n% V
  float u;
( T, s2 t! W6 X- H6 [};+ }9 ]% X# v' C( V2 }1 B- c9 M

6 c" J1 u9 T/ M  IFloatPoint current_units;5 X5 d2 `& J1 |# q# F+ j/ Z
FloatPoint target_units;) F# Q# u$ X  _
FloatPoint delta_units;% o* I- C1 S5 H. Y1 f0 p

1 Z8 I% `1 ?3 \! P0 PFloatPoint current_steps;
* R, Q0 F$ O% ]$ z$ K# UFloatPoint target_steps;
& w8 p# a% S! GFloatPoint delta_steps;
7 b" h5 \, T* s/ g  v! g: s4 ~; f3 g$ @0 [& N& J
boolean abs_mode = false;   //0 = 增量位置模式; 1 = 絕對位置模式. l% A* L' X* |4 k5 n& K+ W/ K
8 J% `" A. ^1 U' Q2 e; Q
//default to inches for units
/ n1 t5 p+ I, s. h5 o) q, |! u8 N; Ffloat x_units = X_STEPS_PER_INCH;
& S5 [; B- y# B0 T) ^0 W- U- Sfloat y_units = Y_STEPS_PER_INCH;# Y+ {& H  i' x# R* \% w
float z_units = Z_STEPS_PER_INCH;
4 T! [/ t3 ^& n; F! C+ Z8 L) d# o) Afloat u_units = U_STEPS_PER_INCH;
& a! r% L2 I1 k2 l7 w% `. efloat curve_section = CURVE_SECTION_INCHES;; w  Y$ O$ P0 l( Q& h) }/ f  m
7 x+ G& ]3 D7 I8 @' h
//our direction vars: k- p# _" P" g& `
byte x_direction = 1;4 L, z6 b3 W& L0 h( C2 k- t  I
byte y_direction = 1;# g9 y$ A2 K* ^" `5 V/ j9 d
byte z_direction = 1;4 f: p" a9 o4 k+ a
byte u_direction = 1;" y/ K) j3 p7 R4 l' J' I
+ r0 T* p0 z+ {/ \# i; J* A$ P
//初始化字符串處理1 w4 k' m+ ]3 c# s  b( Y
void init_process_string()
9 U7 {  Y, c' \8 f% c: h4 m5 J8 w2 }{
9 d6 w' G+ r9 d( s5 O* C7 [  //init our command
1 W/ ]3 a4 h( S; U5 w  for (byte i=0; i<COMMAND_SIZE; i++)2 B; X5 t$ d( K& P4 K8 H  r
    word_old[i] = 0;1 U0 R' b" C( p! I* v
  serial_count = 0;8 ?2 m$ @; O! [( b
}  q# U( s* P4 q! D7 Y

* @0 u% M6 T0 _3 U8 O* x//our feedrate variables.# T/ ^+ `1 m: [( i/ B" F  J
float feedrate = 0.0;; \3 q7 A5 `3 `; T9 l( ?9 l
long feedrate_micros = 0;. l& ]  P* Q* Z# ^! W( Y
; w4 _% P: Q4 W3 X, H1 \7 C
//讀取并執行命令
, q+ Z7 M( S9 r( _0 C* A! Lvoid process_string(char instruction[], int size), g' B9 F1 K: A4 l
{
; I: \! I+ C+ g  //the character / means delete block... used for comments and stuff.3 Q9 N6 Y0 E+ w3 P7 H& {' [
  if (instruction[0] == '/')
+ f! U& `/ [4 o1 Y6 O3 x  {* ]2 t. \9 F5 i) E7 L) O' o+ S- `
    // SerialUSB.print("ok");
( n0 d% c3 I  W9 d. p    // SerialUSB.print(byte(78));
) [. Y1 T; [6 R5 I( ?* Z6 _    return;3 Z2 K8 r1 G5 Q0 Z( o! x
  }
+ ^  S4 ~6 s: @$ T  b" Z6 k  //init baby!
0 i) A( e- P' G5 c# Y! k  FloatPoint fp;0 D- d1 c) d$ Z7 W3 U3 |
  fp.x = 0.0;/ l  s7 p; m6 x1 k& L
  fp.y = 0.0;
" E3 U$ F) J2 |& W  fp.z = 0.0;
8 L4 _+ L, M& d- X  fp.u = 0.0;
  B7 k2 \4 P0 M% l, p" m' C/ _- Y- X+ \
  byte code = 0;% O, R* e- Y. u/ v4 Z6 ~
8 R1 g3 o" b  C; D2 g
  //顯示在處理的命令
* S+ R- B+ N4 L) Y. e#if (LCD_TYPE == 1604 )
& Q& Q4 Y; ], J  // lcd.setCursor(0, 4);6 E! I  e% F, H2 x$ w
  // lcd.print(word_old);4 t! y. J+ N1 \1 o
#endif
8 y4 v: e9 e0 w3 S* i( v  SerialUSB.println();
+ `2 S7 c/ B+ l$ y' A  SerialUSB.print(instruction);
/ K. W) d2 ~7 K  SerialUSB.print("\t");
/ z0 o/ J/ w, x6 P% x4 u4 A$ \
3 ^3 q, M/ ~  }+ k/ ]  //what line are we at?! g2 h, A' f) U5 o6 L1 y
  //        long line = -1;
+ y1 ~0 ^, Q5 o" p4 z7 L  //        if (has_command('N', instruction, size))# G5 ^! }! h) J- ^2 m" @
  //                line = (long)search_string('N', instruction, size);
$ U+ r# Q9 o- E; w1 F% a( {! t) g7 R; y
  /*6 L: e( I, H0 |3 }  ]) C2 U2 U% B. V
        Serial.print("line: ");, f6 [% C6 `. j% b. r1 C
           Serial.println(line);
! h6 q: I; |3 J0 m0 ?, Z  ]+ W           Serial.println(instruction);
. o# _( W' D% g. u6 K3 @   */
& z0 ^7 m' p( T0 l2 o8 ^  //判斷是否讀取了個 G代碼?# e- p4 Q4 `/ Z% G1 X* \6 x' D
  if (  _1 ?$ n% N, {5 [) o' u- Q' T
    has_command('G', instruction, size) ||
! ~5 [- g+ H% M1 T+ @2 y, i0 |    has_command('X', instruction, size) ||3 K; i) X2 c, [4 F, }0 Y2 Q7 R
    has_command('Y', instruction, size) ||
# [+ r- Z- d* i2 B7 ^8 P, g% N    has_command('Z', instruction, size) ||
/ t2 [+ B6 X/ E# h; v    has_command('U', instruction, size)
5 O2 @+ a3 [7 P    )- Q' e5 a, ~* ]7 D4 g: q& O
  {% z4 j, M7 M; W3 R
    //which one?- |* U8 E7 r/ E$ @0 Y( \7 a/ q6 b  s
    code = (int)search_string('G', instruction, size);
& q. v" A  X/ R& k) z7 d    // Get co-ordinates if required by the code type given8 k( s( [5 `0 v% w# `- C9 r" }
    switch (code)3 V$ B/ g& C, }/ h1 V
    {/ O7 {5 y/ d+ r5 ]  H  K
    case 0:/ @8 v  S# u1 M4 t" r+ Q
    case 1:& d0 @, G$ }: O  {& N* S6 t8 b% b' X
    case 2:" X' O9 s" A  _6 u0 Q( h$ L
    case 3:9 Y4 J7 r0 y6 T" F2 i2 _4 P- G9 x  a
      if(abs_mode). n; F2 N$ j/ K2 c! L+ y
      {
- ~5 r# E% {* a        //we do it like this to save time. makes curves better.
; o! `2 J' T: n, V4 T- c9 ?        //eg. if only x and y are specified, we dont have to waste time looking up z.
, L* Y4 _. {* K+ Z        if (has_command('X', instruction, size))
' S8 d$ _5 c* `" i- B" w6 u          fp.x = search_string('X', instruction, size);
0 @% v- K: o/ C/ n5 n        else
1 l1 ]  J) b4 {$ C) u          fp.x = current_units.x;0 z8 h3 J4 }: G4 c* [5 `. w

2 G7 |( S) N  k/ y: j; a        if (has_command('Y', instruction, size))) g( ^' S0 U- n5 V' a' V
          fp.y = search_string('Y', instruction, size);
* A, S( o0 q0 r! S7 a        else
7 r5 H. A- u' n1 D          fp.y = current_units.y;
, k; P! v2 Z2 E6 o& q8 ^. |5 o, k- B$ I5 l9 \
        if (has_command('Z', instruction, size))9 w; `6 V7 J, t+ w
          fp.z = search_string('Z', instruction, size);
, N4 R9 F! T5 s        else) a+ G+ \8 c" X
          fp.z = current_units.z;
" e. ?' x1 b9 R7 w1 s( c          % F8 ^8 z. c  t7 [4 |7 A
        if (has_command('U', instruction, size))
5 C1 @7 b% [- u) z1 O; k1 w          fp.u = search_string('U', instruction, size);
( @1 E$ K; k$ K. |" f        else& b/ r7 U" b$ p3 m3 b2 c
          fp.u = current_units.u;
4 L# k" \' U: ~2 H      }) g% m* t9 g& D  W
      else
( P7 R+ \9 s+ o9 d/ A  \, m      {
% Q  b. y! l( q4 r0 n! A        fp.x = search_string('X', instruction, size) + current_units.x;9 [; n* N' \$ x4 j* B% l) A
        fp.y = search_string('Y', instruction, size) + current_units.y;! G* w. F$ R' M7 q" E
        fp.z = search_string('Z', instruction, size) + current_units.z;
: j) d! z7 O, N" q# L        fp.u = search_string('U', instruction, size) + current_units.u;
/ w; P1 m9 o7 B* ^" V; G      }
3 A) y; {2 J8 M      break;2 ]; c# x; E' n3 n% v6 C+ a9 F
    }
5 A! l. z; I" o    //do something!( e; w9 \4 U4 R; s2 w
    switch (code)3 q4 z/ {. m+ |; c: I! F
    {
2 _5 N+ c0 a5 Q5 W/ J1 w3 S      //Rapid Positioning
- K: G, D9 m- i0 p* n      //Linear Interpolation' E% p& p; x8 `( v2 I& `
      //these are basically the same thing.! S4 _2 {7 y; I6 Z8 v3 y/ I. n
    case 0:
5 W: r! h% C4 t' W$ @: E4 n& A" _! d4 Y    case 1:
: K, @4 y/ m! o8 S: \# q      //set our target.
2 g" C8 _+ F  J0 w5 P* _- z      set_target(fp.x, fp.y, fp.z, fp.u);5 x1 e! b; R2 I8 |6 E3 [3 A
      //set_targeta( fp.a);
4 x! V  m( E5 A7 C5 Q5 c- B      //do we have a set speed?
, A9 Y* I* ~. T0 H  g' R: m: ~9 T      if (has_command('G', instruction, size))3 L+ F0 O- y3 Y( f' [8 s
      {
+ I$ B9 ~9 P! Z) w' M( y/ p$ g        //adjust if we have a specific feedrate.9 p! y3 N' L5 _# u. A
        if (code == 1)
. W+ d+ q3 z5 S6 d. S" q9 `/ H        {' l8 q- l: x6 R- e5 _
          //how fast do we move?
8 W; J$ U  i' n  ?( p2 O. v; M          feedrate = search_string('F', instruction, size);
* B! W# X! D  y, ]& h          if (feedrate > 0)
; e; h3 U- a& c# Q8 x' j7 q- |            feedrate_micros = calculate_feedrate_delay(feedrate);, M4 [. `" {- h: G
          //nope, no feedrate
# p9 F" `. x6 h3 K4 L4 K8 u& j( o          else/ Q( c9 A) w" ]+ Y* W
            feedrate_micros = getMaxSpeed();6 L/ D7 k4 @+ y: z
        }; M3 F2 V7 @2 t" u) x  N
        //use our max for normal moves.3 E* A/ K. y' a) P$ o
        else
& ~2 A6 e% h" y+ O, M& a# g          feedrate_micros = getMaxSpeed();
9 _& x6 k3 q% O$ n% O  K' z3 `      }9 f( _$ }5 x5 Y  |: c, y7 k0 k
      //nope, just coordinates!6 e. g' y5 l+ _8 L
      else; v6 `* m5 c  [" Y/ c0 C
      {
  ^7 n( s7 M$ W( T4 v        //do we have a feedrate yet?! o* x5 \" \. C, C  _$ o% M8 q3 p
        if (feedrate > 0)0 p* I9 i7 B9 F8 ^
          feedrate_micros = calculate_feedrate_delay(feedrate);. s8 }) V8 b& E. Z
        //nope, no feedrate$ Y& U2 n/ J: b' K
        else
" N; c3 R" i5 l: t1 f/ Z* ~          feedrate_micros = getMaxSpeed();
$ f$ C; ~+ {. f      }7 z. H  e9 \4 F2 D
, G( m4 U7 i, F0 `% A' X2 q
      //finally move.9 R* S6 ?# Y0 `2 S# j, _: b
      dda_move(feedrate_micros);) k7 q7 D2 |7 J+ U0 ~. G1 [
      if(stop_flag) return;6 B4 b5 t4 k: x. J) w# u8 V! F
      break;
; f) t5 X- {6 A* D8 O6 R
1 I6 s! c7 v2 j' v- Z. u4 O  A" S      //Clockwise arc
4 O9 M9 e' y) e2 V! t7 C    case 2:
4 a5 Y; ]- k0 w3 b- F4 W      //Counterclockwise arc1 t/ l3 `$ _% T
    case 3:
+ q) t7 O* A+ h4 u' g8 d      FloatPoint cent;: N, S( ?0 m7 x0 E3 a8 s" m
      // Centre coordinates are always relative
$ W, Q8 o" i7 y8 @) L1 W% s      cent.x = search_string('I', instruction, size) + current_units.x;6 s4 D- Q5 K0 y5 \# }2 n2 m
      cent.y = search_string('J', instruction, size) + current_units.y;- i3 P; S( ~9 c% d
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
, D7 A- ]& p/ N% _  y! y0 e- T. N) m& e" G! n3 D
      aX = (current_units.x - cent.x);2 f8 p% }2 B* T4 B& M9 l- s
      aY = (current_units.y - cent.y);& h0 Q3 Q. N, v& ]+ g( R
      bX = (fp.x - cent.x);
! h* w% t# r/ E% x& r      bY = (fp.y - cent.y);
/ F  D% X% c2 P& d; }4 j
$ y8 X) v8 e% J6 W9 z9 r  c      if (code == 2) { // Clockwise1 r# I( V# ^5 I' a
        angleA = atan2(bY, bX);
5 h, K% l3 V! K, r0 C+ a+ d        angleB = atan2(aY, aX);8 Z7 X# J" q# v: J
      }
3 }8 O9 x1 Q3 H1 o! s/ j3 }! R      else { // Counterclockwise! J2 l- X+ D; l* W8 m
        angleA = atan2(aY, aX);
6 G5 M$ p( v& n0 m        angleB = atan2(bY, bX);
$ e$ W6 @" m5 A3 D3 V: z      }& r& \5 q2 w/ U. F- z, C9 m+ {
      // Make sure angleB is always greater than angleA
- V* \1 v7 u& r) B      // and if not add 2PI so that it is (this also takes
; H6 x* H6 H) C9 f      // care of the special case of angleA == angleB,, n2 h4 f" ^( T5 a" p
      // ie we want a complete circle)
/ c: z  c" s- [! q9 a8 n* X' N" P      if (angleB <= angleA) angleB += 2 * M_PI;* Y; \) d! O* A6 i' ^0 w! n
      angle = angleB - angleA;0 s& R) [. a+ W  g/ k

9 ^, d3 J7 j' K  e, d8 T3 {: w6 x      radius = sqrt(aX * aX + aY * aY);
. k6 ?5 s: R3 m$ @7 c& l4 Z      length = radius * angle;7 W1 k( @$ E! D' g( y+ H
      int steps, s, step;
9 d1 i( m+ j6 r3 v% D. A      steps = (int) ceil(length / curve_section);
% g& s% y& H8 d( o# m5 |# Y) M0 U' }
4 a" a0 R. ~% ]' f* e/ [      FloatPoint newPoint;
$ m, n- M, F( c# Z0 y  Z      for (s = 1; s <= steps; s++) {7 \* O: `; i- k
        step = (code == 3) ? s : steps - s; // Work backwards for CW- V1 r5 J( X: e! A
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));$ M# n$ _/ b/ F+ J# T7 m& |% h, N
        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));  ]5 r; S7 I9 C8 Y+ q. h# y
        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
) U9 Q( ?( O* r8 Z1 G0 J9 Z
' w. S" o* y; o: s        // Need to calculate rate for each section of curve9 h5 M9 Z2 a) G- Y
        if (feedrate > 0)
- \8 |% S3 n1 n& j* r3 J  [          feedrate_micros = calculate_feedrate_delay(feedrate);
; {. u) c2 d# _: U        else
) o# H" d4 |. n" L5 l- x# ?7 q          feedrate_micros = getMaxSpeed();
# T) v0 |& y8 `7 b" b1 t- ?9 |4 j( s; R7 c+ S" Z
        // Make step* `7 H6 W+ N3 X( j3 |& i' W' a
        dda_move(feedrate_micros);+ F, k! {. I! g% K- ]2 _4 M
        if(stop_flag) return;
) I$ L$ X" L- N      }
- n- Q% f0 s  @# T6 g1 G5 A9 V# n" {5 k# F2 D* K
      break;
9 w) y1 |$ p% g" c- M/ x5 D) S" V8 D; V& l5 U  k
      //Dwell
5 ~9 o$ c& X' g. V" O    case 4:
; o! B3 D; u" Q2 z# h. Q      delay((int)search_string('P', instruction, size));/ [- v' h- _% l/ H! t0 ]
      break;% K" y9 O2 G0 K( S. K8 @) d' v
" ~: r5 E  d: }4 @+ C9 z- x
      //Inches for Units0 ~' g) c1 n- A& y4 ^
    case 20:+ f9 u+ r9 ~1 v6 x+ F# ?. F+ Q
      x_units = X_STEPS_PER_INCH;9 a9 J, ]7 o2 U. o+ L" d
      y_units = Y_STEPS_PER_INCH;1 _/ }' z3 r/ T" `% `9 }" T
      z_units = Z_STEPS_PER_INCH;
  E* Z6 q* N$ K9 d, O      u_units = U_STEPS_PER_INCH;2 i' R7 E. \1 S4 x% {! F
      curve_section = CURVE_SECTION_INCHES;- m* R$ d5 v/ _5 ?+ ~0 |9 {0 p
      calculate_deltas();3 @) G% W' z6 j1 p
      break;
, Q) g+ F0 |; e1 ~; m+ I4 u; L/ U) ]- [( K: |3 C4 i
      //mm for Units
. @( J1 c( ?! B" V% @; T2 O2 k; ~    case 21:
& i/ u- o% w( t      x_units = X_STEPS_PER_MM;
3 V5 Z+ h6 p/ r, G7 ]+ E- n      y_units = Y_STEPS_PER_MM;7 c1 U) y! `( `% ^
      z_units = Z_STEPS_PER_MM;1 G' L% F9 T6 l2 Q
      u_units = U_STEPS_PER_MM;
4 ]( B) ~3 }0 {3 _      curve_section = CURVE_SECTION_MM;
- a, r( A* W" _$ x" O      calculate_deltas();
; h$ ~5 o5 b) w1 y( E: p: N" h      break;
2 l/ y2 ]" `9 j. \1 \
! a2 h' T+ B& b1 K% t' q* s( F      //go home.2 |: e* G$ p0 \4 ?$ O4 s7 C
    case 28:
! w& L# X& {/ l      set_target(0.0, 0.0, 0.0, 0.0);
. v& O$ X) ^6 n' F. A3 @$ f% N      dda_move(getMaxSpeed());$ \9 o8 H) g- j( K: g
      if(stop_flag) return;$ ]1 o* U4 h$ [( y
      break;
! U4 B7 A* N* Y6 {8 k2 L9 L7 N4 b3 Q$ _) `/ n# e
      //go home via an intermediate point.
- u8 P4 i- \* \$ ~& x( v1 R    case 30:3 ?( n) U/ x6 e) p, \- \
      fp.x = search_string('X', instruction, size);9 y- f3 i9 I$ B) ]/ d) i
      fp.y = search_string('Y', instruction, size);( _7 o6 {7 Q" A# I8 q# B
      fp.z = search_string('Z', instruction, size);
% U1 `1 {  r& c9 B      fp.u = search_string('U', instruction, size);
" Y) X9 D5 f5 p4 S  [3 y. u4 B      //set our target.
' J- @" W+ x* i8 e: f      if(abs_mode)+ i% ]; D5 v- Q
      {
' k' v: U" O- X! k% k: z        if (!has_command('X', instruction, size))& e1 h$ @; o6 d, s  l$ `
          fp.x = current_units.x;
- M9 M+ S+ p; B2 j6 f/ D# S        if (!has_command('Y', instruction, size))6 e+ b0 S. R4 j* W( z9 P: l+ T
          fp.y = current_units.y;, [+ Y) g# Q1 S% `6 O( e$ K7 C
        if (!has_command('Z', instruction, size))
7 H& ?/ i6 v9 s- W" d          fp.z = current_units.z;# f" K, `6 |' S; k  H! c& G' o! C
        if (!has_command('U', instruction, size))& K& Z' |' t6 Y# J. [" H0 b
          fp.u = current_units.u;5 d2 p5 S0 w  D+ t7 R4 W; f
        set_target(fp.x, fp.y, fp.z, fp.u);' o' Z5 B/ p+ r+ X* _  m
        $ c" _5 p7 V0 N5 D' Z: ?0 z  ^) \
      }
3 t5 f9 E+ `3 v) D/ s: N! x      else
0 S4 u1 D! r8 G& O# E6 k        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
& a) Q& {& r; ?' L7 h       " h5 k+ f; R1 h, f' X! j+ x0 c/ \
      //go there.) R$ Q9 b* b7 Q8 k+ A. X- Q
      dda_move(getMaxSpeed());
% @+ E7 t8 W2 F' q      if(stop_flag) return;
6 ~) ]6 b3 j7 w/ y% ^0 q; l# v4 T' Q  T" b
      //go home.# x- w; \- [( l5 U. S
      set_target(0.0, 0.0, 0.0, 0.0 );- k9 W9 j) A9 ?# O- {7 H' W
     . g8 l$ b! }3 a- R: O
      dda_move(getMaxSpeed());1 ?* n+ Z. {8 o9 T/ {2 A; E6 s
      if(stop_flag) return;- }, @/ y1 {# M" ?; ~  U- Y0 ^
      break;
- G. b% j" v4 }2 M' X! T( S
( g7 ~" N8 l2 `7 B      //Absolute Positioning9 E, ^" W; B/ D2 t& {# P0 p" t
    case 90:
" l' \$ s  @9 V6 |: B, T1 P- {      abs_mode = true;+ O8 L4 O# t8 i5 [6 J
      break;4 N" o" z4 L, N, e2 T
5 K* h1 E4 `+ z$ S/ m" X
      //Incremental Positioning
( M" X  `0 {/ G& e/ q    case 91:
0 g5 K+ i7 _+ K) e9 F" T      abs_mode = false;9 H3 W, w$ q" ]4 D5 A
      break;* n4 w, N8 |, V6 x2 f
& l: K8 p8 a! u5 N, C) r" Y! p+ Q
      //Set as home1 M( y9 r/ |. B9 q
    case 92:
; R$ U2 M2 |; c8 g4 P     - A# k  g* v5 i( _! [& V. q" [
      set_position(0.0, 0.0, 0.0, 0.0 );1 R0 w/ s# [; u  Q- v$ ?" x- ^
      
  g- y, k& |! s. U. D( s1 p+ M  S      break;
, }  E3 @0 J- x$ ~( q$ r0 I  X# ?" U4 {1 r2 p
      /*  K$ P- l% w& x; h$ d
                        //Inverse Time Feed Mode% r) V9 [2 ^; G! t4 ], Z$ @, p/ I
                               case 93:
8 ?. x( p9 ]- |# c       + Y7 _; N$ }; Z8 j3 \
                               break;  //TODO: add this4 ?9 g% p- `2 S: G
       ) V5 S2 p& u2 {% k
                               //Feed per Minute Mode. U- E+ N! N, l
                               case 94:
5 T" P  l3 r; S$ d9 B      
% y3 b( w: _$ `7 O                               break;  //TODO: add this+ x! H$ J1 a  Y5 H) M. g
       */2 ^3 B6 `) k8 E- {' ?( \& X" U
4 a1 [, i& d; o% t% `+ r, g2 l
    default:
+ T) D0 g" |2 {- c: P% F) {      SerialUSB.print("huh? G");0 _. k# u1 l* b! K' t; d
      SerialUSB.println(code,DEC);
% k9 C5 y/ H$ _" n9 V( a7 o2 w; s    }7 ^& Q0 I0 N; X) H( J! E# C
  }
4 z$ x' o3 B0 H* v* `) F0 h2 l; T/ A4 q. G1 D% `' T1 R
  //find us an m code.
& G' O& K4 D8 v; n* D8 O  if (has_command('M', instruction, size))) q5 a% T. f) L% P; W
  {
; M5 `; F$ X+ ^8 \$ J; k3 j+ @& C    code = search_string('M', instruction, size);6 T+ S$ m3 x2 `. f* D
    switch (code)' i* u8 v) p2 V5 N* k3 t, I* K
    {
& `  p( `& T( X, ]+ {      //TODO: this is a bug because search_string returns 0.  gotta fix that.6 [" P# z7 j$ s* J  R1 N  M0 X- j1 a' y
    case 0:
; ]9 q: W  I2 D4 H2 l. s" U      true;7 m) O6 a5 h5 H* K
      break;
: M4 P+ k9 o( ~7 R  z. N
' G1 }% H* R$ [1 v* z, G    case 100:
, A& e6 g" _+ k. i9 M      break;) ?+ o2 e* Y) w6 S/ h

& q* m4 |5 r6 r( U5 U/ ?      // fire camera relay- W  t4 L: Z8 p
    case 101:
7 p! A8 u; k& {' e2 I      camera_shutter1();
( ~8 E- B- U/ r      break;
. v( w+ p7 f! g7 q0 g9 M+ |0 P4 i! V/ q+ V0 U1 Y% M2 t
      // fire camera relay29 ]8 v8 b; k7 v; u
    case 102:. }8 b' ]$ O. D4 Y, z/ M
      camera_shutter2();6 n9 \" p1 S0 L  A
      break;
; Z# r% w' E  E& v; g% B8 d/*
. N1 `# k1 E1 j1 a* _      // turn aux 1 relay on  J  {$ y: y+ ~, m+ h+ b
    case 103:
0 b3 _$ G& e( _      camera_aux1_on();
: x/ o* X/ J+ A1 _$ p; y( O  i: u      break;+ G: T+ q: s7 `4 v4 ]& R
* }9 z6 [3 D- ^. {
      // turn aux 1 relay off; q3 S0 F2 h, v% `2 e
    case 104:
+ X3 f# f& y) S      camera_aux1_off();5 U' V0 X) i" {0 u# X
      break;
  G, D/ M! @7 H$ q3 x( x& Y- p  J( x' ]5 ~8 e
      // turn aux 2 relay on
* z& ^8 B) e7 S* L2 v    case 105:, R/ g# J9 Q, J4 O" F9 c# @
      camera_aux2_on();
! B. I6 g* o" M      break;
* w) g( g0 w8 Y; y0 s% h2 _
. Z& ?+ J" O8 W& R, p  ^      // turn aux 2 relay off
: I2 T1 P" X- g8 d& W    case 106:
4 w* E* `( M8 @1 m+ l      camera_aux2_off();
/ f) V- V6 O; Z' x      break;' i0 N  o; w# i% u: s7 O
7 Y& i4 t5 U8 Y9 |4 [4 Z7 U' u7 G
      // turn aux 3 relay on& E; w) j2 A8 p8 D) I6 H
    case 107:- \1 I" K6 t( b: e, x
      camera_aux3_on();' Z' ^, I5 Q+ n
      break;" Y9 F0 d" Y- _4 |
9 f# S7 x7 F3 {% J- ?
      // turn aux 3 relay off
. l7 h2 I# d! H2 R5 l7 O    case 108:
% E, {) s9 N, ]8 E8 F, {      camera_aux3_off();
' t% J' W3 L2 J% M9 J. R$ P      break;1 g/ ]5 m$ Y2 u# t. K4 O, U
4 Y. W. \! t' `- L
      // turn aux 4 relay on
7 N; T* `  H4 Z" f. D2 O& Q    case 109:5 [! J; u0 S' d! l# \
      camera_aux4_on();
. i( ^3 n! Z- _6 e      break;
8 Y4 d0 d* ?. R6 n/ d2 W0 Q6 E' G7 h' S5 [8 f; l' y; W9 ?8 N
      // turn aux 4 relay off
3 C1 t$ j! h9 t0 M    case 110:8 A7 Y0 D, F* ^
      camera_aux4_off();. n. z8 T( y4 N' D+ S! z( K" C$ @# T  r
      break;
  _2 g, i) u+ C*/
# J+ _. c/ ]9 s) @    default:
+ G& Y2 l- Q' e  {
' V( ~3 w3 `& j      SerialUSB.print("Huh? M");
; ^/ Y  A% T6 [: c" I0 c" S      SerialUSB.println(code);8 ~" d6 F0 [; g5 R9 ^6 T
    }
3 i! U7 Z% G* Z: i7 X2 M2 o9 i+ @  }5 s" u& _9 n3 o

+ U: }% s1 U! V  {( i3 ~  //tell our host we're done.3 \1 x. G) X$ j2 ?4 x6 o
  SerialUSB.print(byte(78));' i! [7 [4 T$ V* X

. K5 U5 F3 Z# ?/ `4 o; G/ |}
" |' U. T7 Z' J8 h( v
  i/ v( U9 s9 e" x( y//look for the number that appears after the char key and return it1 R0 O- N. Q+ q: B" \$ C7 @, U
double search_string(char key, char instruction[], int string_size)7 E3 ~, d6 t7 [7 f" J) o6 N8 p8 \% j
{
1 R$ g9 w$ v6 V' l$ b  char temp[10] = "         ";' A& _0 r. T8 k) m3 m
  for (byte i=0; i<string_size; i++)- v) x- Z4 G( |3 V
  {. n3 L! x6 I' z2 T& ?
    if (instruction[i] == key)  N, C6 i3 x+ q% H; `' x
    {
& T. P) \! h6 j' g      i++;      + W, h( X. C6 W! \7 t
      int k = 0;" s# W0 i% u1 I: ?* @% F/ p7 I, W6 `
      while (i < string_size && k < 10)
  _# L2 g1 i7 a      {
: ~; \1 H5 E" Q        if (instruction[i] == 0 || instruction[i] == ' ')
, e1 N; G/ D. z* y' `' {+ o          break;1 |2 d# v2 i# M/ V* r
7 ^0 o% }/ I  d
        temp[k] = instruction[i];: e6 e# Z% t. x7 _
        i++;
9 s6 U# F/ L! J        k++;
1 [) S. [  q8 a7 g0 O      }
5 M2 }5 u- S; s% a0 V      return strtod(temp, NULL);9 E! }7 g7 p5 ]5 s6 P
    }* W, E; N+ k9 M
  }+ i$ ^* n4 \1 B5 Q& H% v/ ^. Z6 t( U

4 u" O0 w& u6 h" t* w' L4 a  return 0;
1 Y& l: [* N$ k/ X7 A9 o}( z# x) z. r' I5 O) D  r

% F, M5 c+ ^$ J9 J//look for the command if it exists.% t! @/ E6 R0 ]6 P# I, C2 d. t
bool has_command(char key, char instruction[], int string_size)
. y* v. s2 q& ^- w. q7 z{% h/ J* R) k* b) A% a
  for (byte i=0; i<string_size; i++)
& f. l0 e- ]/ @8 n. \  {3 b8 l! v; F* k: t
    if (instruction[i] == key){
7 Q- T, ?; v: x( `6 h1 u1 M' e+ s5 K: f5 ?9 }
      return true;
6 [) p- q- ^2 m8 B    }
& h: P. F' I$ k* G7 y  }
* q& `3 R1 D' q/ U& ~' u. _: j2 x+ K& F& M" X2 s, S
  return false;$ `: n5 U1 c/ N( Z
}
3 D! T* b$ J" r2 Q* V  }- e6 v  L' N

, b. c% V1 a$ i3 Z; b5 \
6 C9 [* W% t0 s( K% Z8 X/ U) M
回復 支持 反對

使用道具 舉報

8#
 樓主| 發表于 2014-5-10 09:55:37 | 只看該作者
# z1 _9 n5 M" [  k4 P# ?( G
//init our variables/ K% |8 F( N; v) w8 S
long max_delta;
. K: y+ e$ M3 p( P5 m8 }" jlong x_counter;
  v( Z) H6 t5 E1 y8 blong y_counter;
2 a& a/ A+ g: c  j0 o4 u0 [long z_counter;1 R: n9 O! r; L7 m3 c
long u_counter;: V. P9 i0 _# z: ]& Z
long x_pos = 0; // x position in terms of absoloute motor stepps
2 z3 d9 }0 i( r9 glong y_pos = 0; // y position in terms of absoloute motor stepps
: J' t% Q  P0 P* s; Q& Clong z_pos = 0; // z position in terms of absoloute motor stepps
& l8 ^; R( F; u6 n$ qlong u_pos = 0; // U position in terms of absoloute motor stepps
8 c7 j6 m1 W' t5 m& Q7 c  A' e* {1 ]7 I
bool x_can_step;
9 J- A- C& f' I4 nbool y_can_step;
! l' w+ b, x' G+ ^& G9 \1 jbool z_can_step;
9 J' d+ ]2 S; Fbool u_can_step;
7 ]3 f/ s$ W7 y# g6 N' u5 \+ W. _int milli_delay;
8 Q9 }& q1 `0 u( R. K# u: z
& L- S, c% p9 K6 |void init_steppers(); V0 j: N$ p! X: s2 B& x8 ?
{
( C) X& p, W9 e  l. y( m! ]" n  //turn them off to start.
# ]( k" J6 C+ R  disable_steppers();
/ V, V# l; S6 b; A4 K9 E2 v
/ J) c: q: d4 |- K  //init our points.1 B% ~0 s0 q1 [. {/ O6 Q; o
  current_units.x = 0.0;3 \' m$ y  U5 b- U
  current_units.y = 0.0;. [# M0 Z+ y4 s; R! P; W
  current_units.z = 0.0;
4 [, X3 D& r$ {# R8 y1 g  current_units.u = 0.0;
4 J* Z, o1 I' g5 o4 u5 x  target_units.x = 0.0;( ^$ X$ P! N' ?' O4 |2 G2 n: j
  target_units.y = 0.0;* O& f4 o) Y- A8 j  }# S
  target_units.z = 0.0;
& F, q* l1 }0 X! z% H) u* x% Z  target_units.u = 0.0;
  p" R! ], k8 H9 y' N6 o& @  ! N) R# l4 `# c7 d  f) H
+ u/ f- R! \$ p$ J. ]- X: w
  pinMode(X_STEP_PIN, OUTPUT);
+ ^& e" [( V* P5 @$ u  pinMode(X_DIR_PIN, OUTPUT);
- `* v6 V- X* ^: L6 ]  pinMode(X_ENABLE_PIN, OUTPUT);2 A0 I/ N6 \% \# s! w3 L* |9 h
  pinMode(X_MIN_PIN, INPUT);
1 G. |( E; W, G8 P0 A: T  pinMode(X_MAX_PIN, INPUT);! s/ K4 l" T/ ?) i
' Z3 p3 ^4 |7 b' M5 X2 h5 j) u$ ^
  pinMode(Y_STEP_PIN, OUTPUT);
0 F3 X; M' P4 G0 {/ j  pinMode(Y_DIR_PIN, OUTPUT);! I$ N. A" H' M6 E
  pinMode(Y_ENABLE_PIN, OUTPUT);
3 X* j0 I4 w* D3 h- F! j  pinMode(Y_MIN_PIN, INPUT);  s) x% t- m: {. G7 e% c& w
  pinMode(Y_MAX_PIN, INPUT);
! V* v0 [& r( ?) f( M7 S
9 Y7 B$ k1 n9 z: E% ?9 O  pinMode(Z_STEP_PIN, OUTPUT);* Z* V. X4 H( b# f5 n6 c. {, Y' f
  pinMode(Z_DIR_PIN, OUTPUT);
  k- u9 |$ {& p' D5 [5 w) ?  pinMode(Z_ENABLE_PIN, OUTPUT);
; e: x& p3 H7 S0 Y3 k; b$ o3 }% C  pinMode(Z_MIN_PIN, INPUT);
0 Z! D* L% b9 U6 v$ W$ r3 r  pinMode(Z_MAX_PIN, INPUT);% i$ V/ s" k: V1 \  v- g6 Q7 ~
5 i; G" Z; {3 T- w; m
  pinMode(U_STEP_PIN, OUTPUT);
% U& i8 M( c. G- k  pinMode(U_DIR_PIN, OUTPUT);
( D" g" w5 }. b+ w7 {2 U  pinMode(U_ENABLE_PIN, OUTPUT);9 _: s* S  c: \6 I+ W, M3 m- V( p
  pinMode(U_MIN_PIN, INPUT);
" x1 M  p* m2 p, R' o3 S1 C* F" }$ l  pinMode(U_MAX_PIN, INPUT);
, _0 @9 A2 v" T5 Z% i  //figure our stuff.
/ U9 d- q# ?& F3 ]  calculate_deltas();% `2 Y# S. k% _
}6 s# F, m( P1 Z! `
. Z+ g; B+ O8 `4 f9 [% |6 u
void dda_move(long micro_delay)
- \$ ]0 n% R/ O! M{
% d8 `; }7 J8 s' m" `3 U  //enable our steppers3 J. f- U* W* G6 c! M
  digitalWrite(X_ENABLE_PIN, HIGH);
$ r3 e6 J( ^( u, |* o8 i0 K  digitalWrite(Y_ENABLE_PIN, HIGH);$ P1 N4 v: U* v4 [/ ^% W
  digitalWrite(Z_ENABLE_PIN, HIGH);
( t* l" ?+ q9 P4 ]5 W: u  digitalWrite(U_ENABLE_PIN, HIGH);
6 N% A& w" g& Z  //figure out our deltas; t, R9 r6 J/ a- `
  max_delta = max(delta_steps.x, delta_steps.y);( p" }. q" |0 M8 E$ L$ U+ T5 f
  max_delta = max(delta_steps.z, max_delta);+ d" Z0 R6 n/ T7 B
  max_delta = max(delta_steps.u, max_delta);
" a6 \" ~) t( H6 z4 \  //init stuff.* @3 y; E2 p$ m, S4 r, e( ?# l8 _
  long x_counter = -max_delta/2;! T5 v3 m" m6 J# H. d6 |9 L; L
  long y_counter = -max_delta/2;
3 f9 U) k- v2 A- m8 o  long z_counter = -max_delta/2;. e. b. t; d" B' I/ @8 w6 d
  long u_counter = -max_delta/2;" w. _/ [: M  h- m" ^: T3 s& y
5 b0 |, U8 j& c+ \* m' ^& s
  //our step flags% K9 _5 N. r+ w0 X: C6 b/ H
  bool x_can_step = 0;* x  N# S. I( X1 m1 ^
  bool y_can_step = 0;5 H4 ?0 _- d2 F  H  }' v1 d# ]* s3 H
  bool z_can_step = 0;
6 o. h/ T" I; b9 }5 N. ~& L; H3 ?1 t  bool u_can_step = 0;. w6 H, F; e9 M8 g1 L: s- `+ N+ H

# k; q% T5 N; F1 B. _( |7 X6 B  if (micro_delay >= 16383)
- a/ q2 D' x9 l$ C  x5 Z9 {    milli_delay = micro_delay / 1000;
; w/ q  f! F8 E- K! }, H5 ]  else
4 ^2 U# s1 R+ X5 @    milli_delay = 0;
1 B+ N. S/ n" j; t6 v' _  S. i+ ~+ q

* [' ]# a( l6 N0 g  //do our DDA line!- R' A2 O% l  Q, f

1 ~0 d) R+ I$ c# A  do
, N& `& t7 w# h/ F0 J3 |. ?  {
+ R0 f; A" S/ w  [9 |    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;
6 E, c' q# @* q    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);1 r# j/ S" o/ y3 |$ M5 z0 Q$ ?2 F
    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);
, _( ?( ~, z1 V+ d) P, Z2 X8 x    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
) t: v' q7 Y- K+ a2 d8 b5 ?  w- w    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);' y: n- ~0 C4 t# V$ r7 N. f
    if (x_can_step)
+ g3 t# {" X( i0 @+ W    {: M6 p5 \5 D+ L0 l% w
      x_counter += delta_steps.x;
: f& b5 L/ k- M$ l3 z8 E! V* W4 H
8 j3 n) ]1 g" m0 @/ ~! w% \0 u      if (x_counter > 0)
1 A* C9 i( J8 _# M' [. U      {' U% o. Z  P# j# _- V
        do_step(X_STEP_PIN);
  |' ]" t- Z3 V1 H        x_counter -= max_delta;/ N2 C1 u+ i$ H6 G$ g
        if (x_direction)- u/ I8 S5 i! k- y" \/ W+ H# Q
         { current_steps.x++; x_pos++; }
# f  @% K# I1 u6 p! z9 O/ C5 n         
( J: x' u* ^9 P% N# O+ i0 X& J7 e        else1 J9 C# r, h# [0 ~
          { current_steps.x--; x_pos--; }. M; s  W1 r" l5 E' i- o
         
; w; V! l( h  c! o' n- e      }/ E0 |# A: c; W, v
    }7 b; U3 e' p1 Q6 _; Y
    if (y_can_step)
) S& Z! Q, x, n( \6 K5 \; f    {
: G; i0 {. Y# f& i      y_counter += delta_steps.y;
8 C* y( w. U: j  G% j% q
! s/ N6 [' K$ a0 _4 ^% t. M      if (y_counter > 0)
! S7 A, z( q0 Z: l+ y      {
' U6 q% b. Z( x. H, q        do_step(Y_STEP_PIN);
- i* Y" k/ j4 @        y_counter -= max_delta;' v( F# @$ D! i5 A0 G, g4 H+ T

% N3 |8 a6 O$ z. [) y! O        if (y_direction)4 h4 z, a/ y* Q: y: k# K
        { current_steps.y++; y_pos++; }, j4 A6 S& A5 a  B
       - H  s# D  ]: Q5 y
        else( f8 b+ H# ^  h+ ?
        { current_steps.y--; y_pos--; }1 ?/ H* N" m% N0 _' ~3 G
        , `" [- t! o. C: v
      }
3 g$ P" O+ ^  R7 F    }
# N2 T0 [; u' Q+ |
- ^) w& Y4 @7 D( ^8 d    if (z_can_step)5 N. C$ d9 {* K" m/ V) m
    {! j& M1 ]. _! u' n% l
      z_counter += delta_steps.z;
. M$ ^/ o# U4 R/ @# g
6 ]  O7 D, `' c      if (z_counter > 0)7 n" _' X$ B* j% }
      {0 H& G( c2 ?4 V2 r- X: l
        do_step(Z_STEP_PIN);
; O$ b) C. n0 Q5 F0 a  `$ `) {        z_counter -= max_delta;
1 N  T/ _& H" s7 R
  b& ^5 J( G9 t# Z, q, ?+ c        if (z_direction)
( r4 G4 q3 q4 ~" K5 T4 n        { current_steps.z++; z_pos++; }
! L' k! n1 I  n7 P5 W        * b7 Z. L$ p* z
        else
. s. _$ C0 F5 M; r& v3 P        { current_steps.z--; z_pos--; }
( A1 p% L! u3 y* e0 `* N5 `* G& p        
; z: A8 j8 J+ ~& J! T      }
2 m: D7 I- W7 a/ q  E0 X    }$ L# U  L3 X6 ?$ k. S# J
   
8 D8 B7 }. v- j( t    if (u_can_step)* ]! D7 N5 n. [) o# B5 r
    {, e' w" |3 _6 W9 p9 o/ ^
      u_counter += delta_steps.u;3 b- ?! v7 i1 f
( A: _  X( v' Z2 h- b5 B
      if (u_counter > 0)
% M$ v7 y* b% d7 b* G      {
, g" D4 F/ h0 K! i        do_step(U_STEP_PIN);
6 r( ]# t. ~- m3 t        u_counter -= max_delta;: d8 N+ b1 n6 y6 b) O; j; R
: I# S3 ]& |7 A# K. N
        if (u_direction)# P- s+ A0 m$ B6 |" n
        { current_steps.u++; u_pos++; }) v7 u# d" W" ~$ y) i
         
2 j4 i1 ~, ?' V+ u- x9 I9 g        else3 ?3 ?% ^( p6 A# `3 K" f% d
          { current_steps.u--; u_pos--; }- E; Q- V, ?1 M6 A% ?# P
         
  z1 Q$ ]3 @* |  F: [! M# S' Y      }$ o5 N/ T/ F" c/ ], _) `
    }
5 B$ u! \4 \0 t* F" s' y    //wait for next step.
( ~1 {% G, [$ |- P    if (milli_delay > 0){; L9 d% h9 D# w' o( o! J' L# i
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();2 r7 P9 b; U7 Y: P& f$ W0 M
      delay(milli_delay);
7 c: C: ^' X* t    }               
3 K9 f# U. n7 p7 |0 Q* K& B    else{
4 F6 U2 p' W+ L  E% [3 I      //if (digitalRead(BUTTON_SP_EN)) SPEEN();, d+ {) d4 o0 [3 B" g9 g% c( y
      if(micro_delay>0)delayMicroseconds(micro_delay);
* M2 }  p- D1 P0 ]# U    }" T* K; K3 X+ j: c
    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
! G# y: G* ]  x( k  }
; C- J' y4 v* M% ~  while (x_can_step || y_can_step || z_can_step || u_can_step);
1 Q  d8 K2 b% p5 a( `0 E. \7 D+ x
) f! ^" k$ Y* o" ]# x$ `8 A, X! C2 N% Z- n+ ~, m& Z
  //set our points to be the same& H( Z9 [. a& h; X1 T0 i) v
  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
1 n6 Y# v$ r: L: F3 j  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;, v! U8 u& C1 t; W9 p; b. W
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;( |; @& B1 m3 t) Z  n9 V
  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
% \3 O; f  A  x9 z+ [+ L6 S) V  
+ w3 u5 M/ [, l6 }2 x- Y  set_position(current_units.x, current_units.y, current_units.z, current_units.u );
( N4 k" {3 H6 T+ g- v6 g
( M1 K7 Q: E7 N0 y  long x_pos = 0; // x position in terms of absoloute motor stepps
4 z2 }9 G5 C, I5 f4 i  M1 l  c6 P  long y_pos = 0; // y position in terms of absoloute motor stepps0 e9 `9 j/ ^" C+ L5 c
  long z_pos = 0; // z position in terms of absoloute motor stepps
3 ^3 }7 A% B7 N8 Z6 d  long u_pos = 0; // u position in terms of absoloute motor stepps
+ |/ m. R; I" T  calculate_deltas();
0 H0 q3 v" d% ]' e0 d% L: P  5 r% ~0 ^' S6 e& A. g
}( Z1 b, |& J6 z4 O$ \
& W3 {1 g, S% G( x; u" a
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)! ?6 [% u7 n, t4 P
{! ~# i% y6 Q- m
  //stop us if we're on target! @3 N- u4 l7 B; C4 X
  if (target == current)
% y& Q" q! ]! `6 P    return false;
) p2 M" a+ [$ F) H: V6 ]( s7 ~' W" f  //stop us if we're at home and still going
- e  t1 R0 s/ N7 o% I  else if (read_switch(min_pin) && !direction)2 i$ ?# p  h# U1 k" ~
    return false;. E! @- I- y8 \3 f1 u7 V
  //stop us if we're at max and still going$ z$ c$ z7 o$ O" Q8 ^
  else if (read_switch(max_pin) && direction)
1 i. _. T( e+ A) Y/ `3 L  j" a/ r    return false;
1 ~9 X+ g- V: {0 d! C3 y8 R2 z- |, h% {8 m+ L2 |
  //default to being able to step
/ |( Q" P; M( n  f2 [) P% _+ y& g  return true;6 r* ^* w: E2 o% U) H
}  e0 y. {8 k5 t5 D# ^

! q3 u% h/ t/ [5 \1 gvoid do_step(byte step_pin)
$ W7 v2 F, ^$ x8 s2 m% E- n. r{
+ p, [. [% m$ w  |  digitalWrite(step_pin, HIGH);
3 l* X" K% }: j8 d/ F  //delayMicroseconds(1);
2 J" z& M) o  F1 [  digitalWrite(step_pin, LOW);6 w, S( r( V7 ]; \
}0 `: ~& U, {9 r  T& s, M
% V, K( h: Q. z$ L
bool read_switch(byte pin)
1 x" j- W5 Y! |( _  u7 n0 D4 }7 u{
7 _* J: I6 W8 \& t) o/ O  //dual read as crude debounce
, x; w% S. p- I2 l: y3 D, V2 F# f( ]" q$ S. V2 Y, H
  if ( SENSORS_INVERTING )" H, q! a+ K1 f7 o  D/ ?. |& W
    return !digitalRead(pin) && !digitalRead(pin);
) G; W. v/ n# {/ h3 ~  else' Z# g0 p7 w4 Z! A
    return digitalRead(pin) && digitalRead(pin);+ r: ?3 S2 I) @* r/ n1 P& Z
}8 B" }: ~2 y8 G; ]# v
" q) x! C! S, u% F
long to_steps(float steps_per_unit, float units)
# K5 ]1 E9 ]! S0 a- |% H. h{
/ `7 r6 N& s( ]: X4 W# N  return steps_per_unit * units;1 t5 Z6 |) z7 @
}
  k# K( e: B; I8 m  q1 W7 ?
. d7 T" x8 A2 V, A7 Dvoid set_target(float x, float y, float z, float u). V+ B3 t2 E$ \, @) h
{
: R0 n* j3 V8 [# |* B# n  target_units.x = x;- p1 @: q" L, j: ^: ?1 e
  target_units.y = y;/ |. t: P. U5 `9 B7 H3 `
  target_units.z = z;
: h! }/ [! b& R6 Y- d  target_units.u = u;( d/ S* h, U" ?- ~) F% V# }
  calculate_deltas();
0 q" q  n' k% n}, ~- f$ @/ {7 k4 V0 K  X3 B* R/ M
0 T. D! P8 n! c1 A7 \% _
void set_position(float x, float y, float z, float u)$ c/ C! O) P( G* |
{
& n. b/ V" b- {9 r6 v  current_units.x = x;# k" y  x" Z. }% ~5 q; A% x. g
  current_units.y = y;
+ m- `8 I3 M3 c2 A$ r- r8 {  current_units.z = z;6 y; [) E! x4 {+ h( f
  current_units.u = u;/ E( l4 N9 Y1 \: ]# V
  calculate_deltas();4 G0 G- n' S0 W  q2 ]  n
}2 n/ L0 E1 T3 B( B- W( N

& Y$ ?  [5 A3 J( H' ~6 J( I" Qvoid calculate_deltas(). }- s1 D8 g& u6 d8 a, L
{5 A" L5 A. h6 o* H  ?( s1 F5 s
  //figure our deltas.
" H( Y$ N! ^9 D- I: f! h6 t- I  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
8 E+ R& K$ H" i4 C2 j  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);8 l/ h8 O1 ]) _& M
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);; P# z5 u" W0 {6 H. j0 c& U# o* z
  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);5 V9 |, j# B4 ]2 |# H9 b4 Q3 N

# V( G4 ?2 {# o  //set our steps current, target, and delta6 e3 ^1 J* i1 g$ d) b) S9 J& Y8 z4 q5 D
  current_steps.x = to_steps(x_units, current_units.x);, U% s. Y5 a; s0 V# I
  current_steps.y = to_steps(y_units, current_units.y);
9 Q* P# N; q& J' s; y0 _, I6 y+ ?  current_steps.z = to_steps(z_units, current_units.z);, D# u, k) _. P3 P) J
  current_steps.u = to_steps(u_units, current_units.u);5 E6 }: w+ E9 h3 q; P4 H

5 m# \# @+ ?+ P% Q  target_steps.x = to_steps(x_units, target_units.x);
2 Z; V: b7 y' b  target_steps.y = to_steps(y_units, target_units.y);
1 G  J4 J' j+ k) G  target_steps.z = to_steps(z_units, target_units.z);
$ a* E) M5 ?# W  A7 }  target_steps.u = to_steps(u_units, target_units.u);7 Z! Y/ Z" k* e/ j$ c

& W! }4 h& X3 t. I; w3 y8 q  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
6 ^: @# f2 h1 C  o( ^1 o& P  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
& m# C5 P; N. X/ i; S  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
/ J- h9 h( b2 R( r# x+ N% A. ~  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);6 H3 B3 v" X3 u. V: T9 a
/ w5 k0 G" t8 a4 R  X0 \
% W1 D& w9 ^- `1 m6 c% l6 r1 f
! H! I" l1 }& J9 m6 T
  //what is our direction
8 p& }7 x: D8 j* r+ Q( O7 V1 a5 ^  x_direction = (target_units.x >= current_units.x);3 s, b$ n7 _# ^- y$ u% a) ^
  y_direction = (target_units.y >= current_units.y);/ [$ f# W5 `  a: r+ @2 E
  z_direction = (target_units.z >= current_units.z);
% E. q5 r5 ?* M: Z4 d  u_direction = (target_units.u >= current_units.u);
5 n; _) V+ m6 _6 a; x; V$ Y) Q$ w+ f# Z0 n5 X" [; r
  //set our direction pins as well- }' i  O+ Z& w* ^# G( f$ l1 L0 z
  digitalWrite(X_DIR_PIN,x_direction);
  O6 G# a! }5 p0 ]1 n2 z" p" m% {  digitalWrite(Y_DIR_PIN,y_direction);4 _. }. N) q" P9 ?
  digitalWrite(Z_DIR_PIN,z_direction);
7 q9 A$ o$ }4 k" X- [  N  digitalWrite(U_DIR_PIN,u_direction); 5 L6 F, D- F8 P, [& m' z8 r
: m) b0 ^" S: i9 k/ U* X: j
  //繪制LCD
& \& x* H7 C# T" G  S  LCD_DRAW();1 s2 O) F) G  E& F* a0 @

6 t4 o) q/ f7 b! Z: u/ Y3 c4 Y8 w+ R}
: k- X# C9 t$ b8 z1 V4 V, C& K" M# G( y# F% G1 D

8 p/ x0 I4 U3 A; m& ^  Mlong calculate_feedrate_delay(float feedrate): O2 q4 G- I& f' E
{: L8 Z5 p9 q" m5 C2 v. x6 i; M7 F- C
  //how long is our line length?
# S5 S. R& p6 M1 g! G  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);
6 k4 I7 }( k1 x8 b# l  long master_steps = 0;/ y( q2 w0 d8 [7 g: p- w  n

0 w0 @" ^. ~# ^2 G+ m, V) Rmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
9 o4 \* t3 [0 Z6 N6 U" gmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
! |' f* P, `( a( X9 k3 X6 p7 Vmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;: D9 O" r# p' u

9 h. u  |7 V1 e! l  {) a$ W7 G, v/ ]( ~4 a

) C# N+ g8 d$ Z ' W7 s( n! G3 s3 ?( ~" _: y# k
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.; X; r- ^, H" @. s5 y
  //the formula has been condensed to save space.  here it is in english:
3 s9 s, C# j- u, N4 _) S" X  // distance / feedrate * 60000000.0 = move duration in microseconds/ b- ^6 ?, m' n7 O9 ]
  // move duration / master_steps = time between steps for master axis.
" B8 v* v4 m/ }* M return ((distance * 6000000.0) / feedrate) / master_steps;
. b- X& Z- G! C0 O' I9 ~( L* m8 B+ W& j# Q  C
}- N: @0 }, ^6 q- p( B' E3 N2 Q

# ?, r' Y/ c1 t  dlong getMaxSpeed()  B0 \+ D& S9 U; e* L% p  J1 _
{$ y& Z$ @3 d, j' V# W  r6 h+ m3 ]
if (delta_steps.z > 0 || delta_steps.u > 0 )7 s7 P, {9 U+ }' [
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);/ j5 }4 }/ G1 v, @1 u! O+ {
  else
+ b. @/ n3 O8 S    return calculate_feedrate_delay(FAST_XY_FEEDRATE);. V  \$ ]( @8 }- s0 b: b9 R( }
}
$ P6 e  w9 J5 T& |/ m& ?3 Y( L  K3 J8 X; \
void disable_steppers()
- C/ N. }( L6 x1 h- k/ m{6 Z8 j. @1 a% \( j8 _
  //enable our steppers3 G- J& O* H7 H- h  O% G
  digitalWrite(X_ENABLE_PIN, LOW);! n) d* k7 ]* l' c7 @( T* H. ~
  digitalWrite(Y_ENABLE_PIN, LOW);( d% f1 D: q7 x3 U, E( k( @/ y
  digitalWrite(Z_ENABLE_PIN, LOW);
& d9 B1 l% R6 ^; a: i8 ^" a  digitalWrite(U_ENABLE_PIN, LOW);
/ a+ o: B; |/ m5 B9 C}
* _: C8 @7 f$ E' V  Q% ]' s" l( J. F' b' y

& j  `7 i, E3 G1 `  x//繪制LCD& [$ u- R8 T+ m; ^5 L* f
//unsigned int DRAWCount=0; 4 l: K( @; Q& u! i# A0 D
void LCD_DRAW()
, y* u* J. H0 h; Q# C. y{# M! n& |. Y( S% J$ n
    lcd.clear(); 1 e. t% ~4 g/ R9 K8 l7 {! ^& a
    lcd.setCursor(0, 0);
7 M7 M% W& J3 }: B; z, M+ H0 [: M/ O    lcd.print("    X=");7 `1 |5 F* G. s0 Y
    lcd.print(current_units.x);) B+ y/ {; b; J+ O
    lcd.setCursor(0, 1);8 V( }  R3 o3 l4 |% l" b" O( z
    lcd.print("    Y=");: y8 p0 s* u  @& }6 ~
    lcd.print(current_units.y);
6 S* T$ L; E4 U2 @/ W    lcd.setCursor(0, 2);
6 F0 V/ [0 `9 Z& z; D$ g; q% F) C    lcd.print("Z=");
4 b. K8 E9 F# v    lcd.print(current_units.z);6 k/ k5 K/ s2 s
    lcd.setCursor(0, 3);
, u8 E' I6 s! J5 X    lcd.print("U=");$ K, p) j  I; W1 e3 M* M9 ]
    lcd.print(current_units.u);
5 q5 _( A, Z, e0 o. Z4 u  }. Y. a6 J- m$ E9 N* R
// else if (DRAWCount>=30)DRAWCount=0;  d  h! @6 o) t' O; O+ a( T
//  else DRAWCount++;
+ `, D- O+ T3 ~ void SPEEN()' L& A! {5 J+ T; f/ B
{/ c: J1 l  k& v' T
   delayMicroseconds(analogRead(SPEEN_CTL)+1);
& o( h- E9 O1 \0 ^ }$ v6 {; R8 V" o
: C% r' d8 ^* {, B* }+ |! i2 G

; O- {8 X3 T# _( g/ d' D //delayMicroseconds(analogRead(SPEEN_CTL)+1);9 u! q& J, \2 A" E6 h7 e6 K
//if (digitalRead(BUTTON_SP_EN)) SPEEN();: K! }/ f& L, [& B( o8 F% P
) l9 T8 J- w6 `/ [0 W

9 f. M% C8 m* J( Y6 }: d% S- P
回復 支持 反對

使用道具 舉報

9#
 樓主| 發表于 2014-5-10 09:58:04 | 只看該作者
基本原代碼的大概是這樣,我是斷斷續續搗鼓的,玩到最后版的,后面我會找出來先測試下過,再貼上:)
回復 支持 反對

使用道具 舉報

10#
發表于 2014-5-10 10:48:17 | 只看該作者
請樓主發一份給我,感謝!178354773@qq.com
回復 支持 反對

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 注冊會員

本版積分規則

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

GMT+8, 2025-5-5 19:08 , Processed in 0.077266 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲人成无码网站| 乱人伦精品视频在线观看| 久久久无码一区二区三区| 蜜臀av国产精品久久久久| 精品无码国模私拍视频| 草草地址线路①屁屁影院成人| 窝窝午夜理论片影院| 97se亚洲国产综合自在线不卡 | 国产av熟女一区二区三区网页版| 中文字幕亚洲码在线| 亚洲成年人一区二区三区| 国产婷婷一区二区三区| 国产精品第一区第二区| 无码人妻丰满熟妇啪啪7774| 国产精品久久久久9999小说| 久久亚洲私人国产精品| 精品自拍一区在线| 亚洲欧美一区久久牛牛| 亚洲精品毛片一区二区三区| 欧美国产日韩亚洲中文| 亚洲一区在线免费视频| 欧美激情,亚洲综合一区二区| 日韩成人精品视频一区二区三区 | 国产波霸爆乳一区二区| 亚洲一区二区精品视频在线观看 | 乐播av一区二区三区| 老司机香蕉久久久久久| 亚洲中文字幕在线观看一区| 日本不卡一区二区三区久久精品| 亚洲暴爽av天天爽日日碰| 天堂va欧美ⅴa亚洲va| 在线免费观看av一区| 天天爽天天爽天天片a| 久久久精品波多野结衣| 亚洲色婷婷婷婷五月| 国产乱人伦偷精品视频不卡| 国产精品午夜剧场免费观看| 中文字幕亚洲色妞精品天堂| 国产成人片无码免费视频| 丰满岳乱妇在线观看中字| 国模无码视频一区|