久久久国产一区二区_国产精品av电影_日韩精品中文字幕一区二区三区_精品一区二区三区免费毛片爱

 找回密碼
 注冊會員

QQ登錄

只需一步,快速開始

搜索
查看: 24429|回復: 14

運動控制入門篇GCode_Interpreter

[復制鏈接]
1#
發表于 2014-5-10 09:39:53 | 只看該作者 |倒序瀏覽 |閱讀模式
本帖最后由 xmdesign 于 2014-5-10 09:42 編輯 ) y/ [1 w' ^; l" k; n5 n& i3 e
$ b9 S! c, y* N: l" {
首先聲明( [6 b! S4 H2 Q
我不是專家,業余搗鼓
' K* M3 R! Z. t6 a: K) I  `6 x源代碼首先來自網絡
9 ^* G1 k# O1 n/ I/ F6 C: y6 x" H開發平臺Arduino及Maple
& _4 \5 [" o& u2 p# R( ^http://www.arduino.cc/
2 f+ [. ~$ |, g3 d+ {http://leaflabs.com/devices/9 h! |9 K- C4 t
國內活躍社區" K8 H  Y! v$ {" x3 V3 r7 c- \
http://www.geek-workshop.com/forum.php 8 N0 F7 z3 Y- K# X, ]

4 ^7 b3 t" M2 S竟然缺少積累,首先尋找最大的開放式開源平臺,這樣可以積累全球范圍的創客和DIY者的腦力活動經驗
' `- o9 n( b( w  \3 F. ~) T. K8 V來入門了解熟悉思路架構等' j# G" m. G+ Q% j5 C( i2 G2 w: h

* O8 i& F7 b0 j3 k) t% B3 Z我只搗鼓了下8位機Arduino 移植到32位機Maple 方面一點點事情,
) V, A: n, Q3 O8 R2 W, s- w許多功能還木完成,不過作為低檔次得應用可能還有可能1 e  p' q" L0 c9 G
  p  l, e2 }" x$ e2 P3 I  g# z
我自己也是個半桶水,再樂意玩的起碼自學能力還是要有點吧
0 o6 b4 q% \9 H# c8 O4 R( I9 n; e" K1 M: {" q
拒絕 所有的求
4 D4 M3 p  d4 s/ I求人不如求自己 不然木玩
* ~( ]5 u; ^6 O* `! e/ M4 ~# b! r: C. c/ S6 Y) R& Z
高手繞道 謝謝!- m, n/ |* G4 Y3 y+ H
% W' ]% f2 B# d' g
回復

使用道具 舉報

2#
 樓主| 發表于 2014-5-10 09:48:37 | 只看該作者
本帖最后由 xmdesign 于 2014-5-10 09:52 編輯 8 }. A4 H, ~4 U$ {; H  i2 M
$ d! l) {9 u/ B
GCode_Interpreter是比較容易懂的,木那些寄存器等蝦米開始不容易懂的東東/ |6 P+ q8 [- M( c& R, ]0 A
貼代碼先
4 V/ b( B% j" C# }直接Maple的,某寶許多超便宜哈9 C# p- Y( f4 @
暈,我怎么上不了RAR?- D. t. B5 T; G, e- P- ?* t
想玩的留下 e妹吧
" w  m0 W2 s% A! N( g' {第一個妹麻煩傳第二個妹哈' u3 H; P7 n3 Y5 w
我平常雜事多
, [/ S- ]& m7 X, L) n5 t; h誰放網盤上再,麻煩開放下
3#
 樓主| 發表于 2014-5-10 09:52:53 | 只看該作者
// Arduino G-code Interpreter for Rep Rap
& b9 S$ k2 X+ @9 Y// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
$ H/ ^7 u5 N( L- ~' p7 X& q* R2 h  v// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)
9 N) @/ \" o& s" Z// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
" z- e0 V' A* U// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com): k# t$ l( q( u4 O. Y
: `4 s  I, X# M& s+ P" w/ E
// Arduino G-code Interpreter for Macro / Micro photography
$ g, r, z; M* N// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
+ _% p2 G% w: e' y3 W1 q; S/ @4 h//modified by YaoHan China 2010.2.151 s4 N6 e# V# o- q7 ^
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175); Z! |; ?+ k( ]- n: s7 s
#include <stdlib.h>
, v0 ~- E3 y- k- z2 @#include <LiquidCrystal.h>
9 |3 r! ]4 |9 I& F' p
' y0 A2 j, y- e8 M% V  W//啟動(高電平有效)/暫停(低電平有效)  默認高電平, m1 J; k# \/ d( q2 p6 o
#define BUTTON_CTL 350 K+ I# G5 h& E$ x

" i# _4 M& B6 F" K" b+ k//點動模式鍵(高電平有效)  默認低電平5 ]. U$ Y- n$ g+ E+ a- a2 O$ t
#define BUTTON_SS 36
/ Y9 a6 x$ S+ D, V8 _! d) y2 C' @4 a1 m" G' H- p( U6 r1 F
//點動鍵2 {. C+ W% A" o8 M
#define BUTTON_MAN 15
5 ]3 D2 V: m, A- L5 V' c  A) P0 f  R- [3 U  t( W$ i
//電位器速度控制
. ?  |$ [6 y8 L1 B0 u, F0 W#define SPEEN_CTL 16
+ ^4 K1 ?# R1 V- B2 {2 t$ ]0 a5 n: M' R' |+ K% k! f. L
//手動調速使能# V& v; S# {) o! @' o8 p8 q9 D/ k6 m/ P
#define BUTTON_SP_EN 17
1 w6 t- \: O0 h& }0 Y
6 o, y! k$ V) F$ i//LCD 類型 1604或1602
1 r2 ?- o) [9 k; i, `7 G1 L#define LCD_TYPE 16047 M; Y* y5 b2 ]3 r2 u
//LCD 引腳定義
5 D: `/ T1 g- |/ n" @4 l. w#define LCD_RS 23
8 R( z) L9 y) c+ P3 }% i$ w, v#define LCD_EN 24& \1 D4 d8 M5 C) a; g
#define LCD_D4 25) E  h( l7 a0 H# }0 G6 N4 B8 |
#define LCD_D5 26  q6 T0 g4 O$ K% Q* g+ Z
#define LCD_D6 27
) [! O  v$ o  N( L#define LCD_D7 284 y( p2 u6 x; W, y+ [" H+ {) [
, S! ~: F, G! ~+ z& {* ?2 C$ T$ s
//命令字符串
) @) b% f' M* g* k; X- W#define COMMAND_SIZE 128
4 W; ?7 M6 i1 b# \, v. j! U8 |char word_old[COMMAND_SIZE];5 A4 e4 S0 P9 G% u6 ~. k) h
byte serial_count=0;
" J! p  s" D) a1 n5 t. Cint no_data = 0;
$ v: W0 J; X# A3 H( V' ^/ Q0 R//LCD 引腳鏈接 配置
' B4 d: X9 M; o. nLiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);1 X" @0 U  \. i' n* Y% r
//停止控制,高電平有效
( k1 K; C" d  e3 n. W, V#define BUTTON_STOP 307 C) Y2 m1 N% V& H+ _& X
int stop_flag=0;
5 I1 J( M9 j9 [4 X/ Y4 V5 V% {, w- l* K& w
//暫停1 ]& M8 z: x* A3 B8 ?
void pause(){
9 \1 D& |' E( h8 K: f' b( Z  while(!digitalRead(BUTTON_CTL));! `- D& J1 F' i; q. _$ O, O" G
}( v7 g# ~8 A- y4 x8 p! k  g, N/ n  S

2 c3 V% O3 z' v9 T4 wvoid stopper(){
% [" r; X& t5 s! ^. `% j* B  delayMicroseconds(10);
: z/ R! w# p! q% _: L' {  stop_flag = digitalRead(BUTTON_STOP);
- ^# Q  W/ h+ w9 T}
* x  @8 k. _2 i' ]- h& {, }/ ?# b% r1 x+ L
void setup()/ D9 |9 [9 y' ^; I. m4 l3 E: o7 G) b: p
{
' v& u4 A7 N) X# m5 N0 d  //show start
% I& a0 g  Z- C  SerialUSB.println("start");4 u6 u& t: r" h% @& j; ]' p; _
& K/ U4 ~* O5 B  Y$ e6 \3 [
  //啟動lcd
/ G6 H) q& T: o# L    lcd.begin(16,4);4 j. W0 s( N$ ^: r& {
    lcd.setCursor(0, 0);
; h; C+ {: _" ?3 V0 k  O% v$ v    lcd.print("hello, world!");
0 E( O+ U# r( o6 j7 ?( w, r7 t1 |2 P+ ?, T
  //初始化控制引腳及引腳功能
1 w0 b9 a0 G8 G; B0 p9 j7 r- p' Y  pinMode(BUTTON_CTL,INPUT_PULLUP);
, e2 _5 `5 Z9 {5 l  pinMode(BUTTON_SS,INPUT_PULLDOWN);$ Z! L3 |& Z1 i. K$ ?6 _7 ?6 v
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
" P3 ~# [# m& u& y9 \( [  pinMode(BUTTON_MAN,INPUT_ANALOG);: P' g7 z# h$ F: H  H4 ~5 _3 s5 I
  [6 q9 J1 \8 j, x' H
  //控制引腳的中斷
% M; B4 B) f( v  attachInterrupt(BUTTON_CTL,pause,FALLING);//暫停的中斷
7 R# b! z4 \- j7 R  attachInterrupt(BUTTON_STOP,stopper,CHANGE);
3 n* n' C3 f: n6 L1 W; ^- A4 w+ Y, p# `4 B
  //other init1 m. Q  R" _* P7 a/ q
  init_process_string();" j: N) ^1 \' B- X4 x1 @$ D0 `
  init_steppers();
- s8 K" ?/ y  Z1 m$ v# [  init_camera();
" w* m# g+ n3 T; d
) `2 P0 a/ V! v}  b8 [& E6 A# C- r# C9 @

4 U7 H5 c0 l  fvoid loop()% D' Z/ M7 Z$ F* r" h
{$ M" u) l# Q4 r; }% x2 Y' I
  char c;
' R" F- u  P/ o+ m6 s% Q8 ]
: U' z- V: a; m! T3 K" ^. W  //讀取輸入的字符5 H7 H- R7 N  w0 L! D+ o
  if ((SerialUSB.available() > 0) && (!stop_flag))
7 P: ]4 g* M( g5 f  {
/ A' l  I7 W) v/ a; B    c = SerialUSB.read();" b: v& p) p$ ~) B& ~3 y
    no_data = 0;: L) v+ G# Y, ^' r9 R

; m, S1 i5 d% |9 B$ @- _+ l    //換行符代表一個命令的結束
* u% W* u- N8 [3 r8 y; w1 ?8 V    if (c != '\n'), @& {8 X1 ~* B' t
    {* n# }- C; u4 w' V8 P
      word_old[serial_count] = c;/ a' X$ x6 O+ N. |- N
      serial_count++;0 o8 h1 ?% J+ @4 R

' ]: U* S: m& j, K    }
" @2 U  R) q% x3 ?' W  }
: I& Q4 t) v2 ]* H! a! m4 G  //標記沒有數據輸入
. f" V, ~2 J1 n  else
" @# a/ n) D5 }) |5 D  {, M1 E' n0 I; o/ R6 L2 U& [
    no_data++;
" f4 Q/ N- j) H4 v    delayMicroseconds(100);& g3 n; c5 [$ \- a+ E2 R7 V- A
  }" B/ o5 }+ T, x# b/ ]
, r' m, r& K0 K' a0 T
  //if theres a pause or we got a real command, do it" h2 g; v% @3 Y' o
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))* _/ _5 _/ a( L% [
  {
) r8 h  D1 p9 {
; C' G; r& c6 M# W    //處理命令
7 \1 v) v4 Y2 M! H    process_string(word_old, serial_count);  n! r, ?! Z2 U$ y& t1 z8 j
( f! t2 R" x  F% }' M2 [
    //清除命令) H; ]" V5 [9 l6 @
    init_process_string();
$ O. d. m3 O4 ]8 |+ p% G' d* f' Z1 p  }
- j* G0 o1 E. D& ?2 @. X% J2 X: b' c7 n
  //如果沒有數據關閉電機5 e" G5 V) C* e: W8 ]9 f
  if (no_data > 1000); @& W7 D. r0 \" s3 p
    disable_steppers();
8 q4 K0 r$ ^# s3 `; v! i( H: U( H( @* N/ L  q- ?: V
  //如果ss鍵按下進入點動模式
! x* |3 F: w$ d/ s6 o6 {( X* i$ r# l; W  //if(digitalRead(BUTTON_SS)) ss();+ t, o" @6 Q4 y4 l
  run_a();
% `# t; ^3 H3 ~9 V- N5 _2 I, H}
& p. G) g- I; m. d% G# O2 b2 u. Y" X% s7 G% a: @9 W" t( c. ~0 b
//點動模式+ P/ }# V+ L' I
void ss(){
* _0 j  o( O+ W) w9 M% \7 L7 f
6 v+ s6 z! E1 v% e/ ]; }  delay(1);, Y1 b6 j3 u$ h6 C! u1 {5 A
  if(!digitalRead(BUTTON_SS))return;
5 P3 q/ P+ s& T, |" w* ^  if(!digitalRead(BUTTON_SS))return;
0 \* u; Y0 Q( w0 v1 f# k! h  //init_process_string();
8 {2 @6 @% B6 A5 n: H+ e$ s$ G  //init_steppers();: g% e/ T/ l& x2 w1 t3 B
  //init_camera();  Y% j& r3 }& Z0 t0 W

7 f6 A* V! O1 v& h. d // SerialUSB.println("Step By Step Mode");5 c) q  H( E4 j
//#if (LCD_TYPE == 1604 )  o# N" L  `& a4 W; _! d+ a  Q  m
  //當LCD為1604時顯示要處理的命令$ U- w, [4 h4 t) L
   lcd.setCursor(0, 0);
) y4 J% {3 n/ h; s$ |   lcd.print("Step By Step Mode");3 H% x- O% V0 j2 _8 A4 K7 m
//#endif
5 v2 e. P4 [6 A! B4 V' q  process_string("G1 F5000",8);1 _( j8 v( e  ?1 S% [: U
  process_string("G91",8);
; l+ X6 @- }' ^# @  //init_process_string();" V: K/ e6 k6 _: y/ ?
4 x* [* p: B) z0 P; O6 h
  while(digitalRead(BUTTON_SS)){
2 E/ o3 o0 M% x# n    int i=0;
/ p" {8 x" t( h* \9 p$ T: I0 q    i=analogRead(BUTTON_MAN)>>9;, Q: ~- @( M, D2 s( j
    //if (i==0){break;}& X- T% B8 Q2 R4 _& J4 Z. C
   //SerialUSB.println(i);
  w$ `1 |2 K" w8 r+ D$ W   //delay(1000);   0 q9 k7 ^" y) s+ n: j9 x
. w( Q5 w$ ^$ i% g1 f# y$ V. d
   if(i==2){6 K8 o! Y# O6 k9 s. y
      set_target(1000.0, 0.0, 0.0, 0.0);
7 g' A7 d) ]8 p3 H- i0 b! @      dda_move(getMaxSpeed());: l  z( ?" H9 |5 s0 N( B
      if(stop_flag) return;6 d& q# _, B4 J, k+ K
      //process_string("X0.01",5);& r& {" m9 r7 q& i( I
      //init_process_string();# M3 s( z; V' b* I3 F" U
    } & _* v- c+ K; ~: q% y3 K  O! q
    else % M3 z* o4 [2 o) N; r
    if(i==5){7 W" T# `, g0 X# e1 B
      set_target(-1000.0, 0.0, 0.0, 0.0);
8 y% W; v9 b" ?( E( Q! v      dda_move(getMaxSpeed());
( w: g! d$ ?$ ~6 \      if(stop_flag) return;
6 m$ V8 `5 C- X; t; U2 h, t9 X      //process_string("X-0.01",6);  X. ~1 @8 _) u+ w, x$ j2 E! i6 T1 B
      //init_process_string();7 ^( ^5 u4 b  w, T4 B
    } : m3 S6 K5 m/ N/ a/ Y. \
     
  h6 E: t  D" s5 q, c) L6 N' o: g# Q' f% J
  }
' u( L9 [- e* R# M8 i) ?4 g  I' I: H7 i+ t, W% K( U, R
  //init_steppers();
$ R% I5 y! I9 q0 N% e  //init_camera();
7 i$ J6 B/ N1 v+ U   // SerialUSB.println("Return To Normal Mode");! J4 B) O: n7 Y" n1 ?& [  h
     process_string("G1",8);
/ x! m. `( N4 M+ ?4 L1 Z* [" U  // process_string("G91",8);
! |! `9 K" D- G    init_process_string();4 J9 R# k7 n: @! p: c' \& F# m4 }
//#if (LCD_TYPE == 1604 ); u+ ~6 {# R! I
  //當LCD為1604時顯示要處理的命令8 o7 r5 P( R3 `2 @: G" D
  // lcd.setCursor(0, 4);; ~' G% O+ i) I: Z( ]6 u! h( {
  // lcd.print("Return To Normal Mode");
9 R! m2 r% e1 r& ^! z0 ]* h( H//#endif
1 e! Y& q' A3 s3 i  }# U, l}
3 |; m* F- C& x! h$ Y8 |1 S4 y5 X. b- _2 i" |: x
void run_a()
5 Q; W/ P! O( t5 j. j& g{
. g' [$ R3 H# \) Q6 X  //delay(1);" w7 O; U8 p1 \3 u- ?2 D3 r; z
  //if(digitalRead(14)== HIGH)return;
3 x% V( j6 X9 \  //if(digitalRead(14)== HIGH)return;0 i+ E, f3 j2 ?. p
  //process_string("G1 F2000",8);" g  `/ M$ \7 M/ X3 k: S
//process_string("G92",8);8 ]( U$ k2 `+ G
process_string("G90",8);
+ d, C$ d7 v) L9 B! m# r7 J9 Q3 W process_string("X120 F10",5);
% p- \& s1 O9 i process_string("G4P2000",8);- H: [% U1 H! h8 ^& T3 p1 @% n
process_string("X10 F10",5);
7 v' Y0 v" l3 K// process_string("M101",8);
( i, h# [9 j) E& ]* H: v$ g/ G// process_string("X-50",5);: w9 B2 ?( H2 Q1 S$ L4 A
dda_move(getMaxSpeed());2 r" s1 E7 z8 W3 D1 A- |, y
init_process_string();
! W# B9 G+ s! [. Q% p$ @
, G) X% Z' q+ E5 a% U}
- c% w2 D5 ~( f/ H3 V+ j. B+ c
4#
 樓主| 發表于 2014-5-10 09:53:25 | 只看該作者
5 f. r1 Z* f$ v6 y* e' |

0 {6 E4 o# E& ?// 定義機器參數
, q- P. u  o; B" i" e* q& z( q#define X_STEPS_PER_INCH 400
; q' y" k0 U0 @, ]# }#define X_STEPS_PER_MM   16.0
  Y; b8 n/ N! x8 o! V' Q! [# v#define X_MOTOR_STEPS    200
/ ?1 ~6 r. u! O7 X) h/ _9 A- |5 K; S  u/ V6 Z$ [, r0 H. W
#define Y_STEPS_PER_INCH 400.0$ V; t6 B' J# Z" }6 ]! I: V+ D
#define Y_STEPS_PER_MM   16.0' ~( ]6 V* F* A9 N8 F
#define Y_MOTOR_STEPS    200, {3 C: Y: K5 p5 b2 C; K
- m; [! h' z3 K9 B4 D8 z
#define Z_STEPS_PER_INCH 400.02 Y1 L: \. i/ `9 t* B) M2 j! f
#define Z_STEPS_PER_MM   16.0
2 A% \5 U) O$ c8 y) K#define Z_MOTOR_STEPS    200( N# @, n/ t% V! G7 O/ u
  u+ J1 Z7 w1 H8 k8 ^- P$ L
#define U_STEPS_PER_INCH 400.0. L' N% r  Q0 ~+ {* A( N$ J
#define U_STEPS_PER_MM   16.0
/ I$ ]8 i& E% t( g4 S#define U_MOTOR_STEPS    200
. r( r) \. X+ _0 q2 ^6 s# G/ e9 }
, j; H+ j1 T" p6 y//最大進給率; y2 K0 u: a) Y' @9 Z
#define FAST_XY_FEEDRATE 1500.03 \* }. g1 o( X# v$ B, u( b! v; [4 S
#define FAST_Z_FEEDRATE  1500.0. b- w6 [& f7 G4 G6 [
#define FAST_U_FEEDRATE  1500.04 }0 u) J  q8 U* E/ G
// Units in curve section6 w# F) B& r. x7 k7 i& c
#define CURVE_SECTION_INCHES 0.019685
7 r) }+ Z$ ]; X* x9 O#define CURVE_SECTION_MM 0.5
' D+ p! \' Z& p+ S. }" H  M; e1 T6 t( c' l+ C( q

7 g" D' L+ ?5 U" E$ D' B# k// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)' @- @/ O( B0 l: {( H# k
// RepRap opto endstops are *not* inverting.: m6 S  E* [! V6 ~8 @
#define SENSORS_INVERTING 1; t9 N1 b. y9 v- X" C" y

8 U) l! [2 P" ?+ @. P9 h/****************************************************************************************% `2 u, j& h! i- B5 X. c0 T
* digital i/o pin assignment# b* U- k3 s4 e, y2 @
*
: ^* ]3 h& c/ a * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5/ ?  z0 Q1 O) G" |0 X
****************************************************************************************/
8 O& Q8 t$ n8 B8 p4 A0 i: W" }, s' j) I8 ~
//camera shutter and control pins# A3 w$ X- |( I+ [; f8 k
#define CAM_SHUTTER_PIN1 29- X9 d, v8 P* t' E# X4 d4 J6 M
#define CAM_SHUTTER_PIN2 30
& J2 W- @' L3 f; q3 E//#define CAM_AUX_PIN1 31 // analog 0
* Q5 e# E1 K. |' f$ Z//#define CAM_AUX_PIN2 32 // analog 12 B  m0 C, d. M, E9 X6 h6 U
//#define CAM_AUX_PIN3 33 // analog 2
: }5 G( `+ F7 ~  Q  w0 [! E$ U3 G//#define CAM_AUX_PIN4 34 // analog 3
( z$ M8 d' Q4 K" Z: Z
4 X6 @/ L: k8 p; m# C! X5 N& b, I// stepper driver pins
; ]: q2 T" \/ X0 c" u; a#define X_STEP_PIN 7
8 p. i5 B  @) C/ g#define X_DIR_PIN 8- k9 \/ v. o" L6 J
#define X_ENABLE_PIN 19
: |3 @) @3 {: _1 E* f; X- Z9 v  m8 T
#define Y_STEP_PIN 9
7 ~  e& @: d; J& Y#define Y_DIR_PIN 10
! H) N- m' X5 r# e1 L9 }#define Y_ENABLE_PIN 19
6 E5 C# r' g1 g$ q  S
7 R, O; a! J8 A/ O& X. a. u#define Z_STEP_PIN 118 H7 C  D8 E$ P& @2 L: s' A4 i
#define Z_DIR_PIN 12
, a9 k' p, Q+ n5 f' O+ J; m#define Z_ENABLE_PIN 19. X2 K8 J/ C4 ?! k/ g; ~
3 r$ t( r- j/ W5 D( A
#define U_STEP_PIN 13
* w# m/ n9 _: }#define U_DIR_PIN 14
& Q# f6 z  J+ U- u4 g) Q& U9 R2 K#define U_ENABLE_PIN 19" k. Q( B) o4 I' `5 D1 `6 l7 `

$ S: ^& e1 l% x// limits not used right now: \, I1 V! t( N3 E
#define X_MIN_PIN 144 b  M' a. }+ p/ Z
#define X_MAX_PIN 14  b( V0 N2 h' I2 |& f) s
#define Y_MIN_PIN 14, s1 ?8 i% ~  }9 R
#define Y_MAX_PIN 14
: l% C$ ]4 m& |$ {0 X4 D8 t#define Z_MIN_PIN 14
/ i1 ?, Y) c( z( l; @2 Q* y#define Z_MAX_PIN 141 d8 U0 i' S* q, k$ N
#define U_MIN_PIN 141 M7 }, O1 P. L; s
#define U_MAX_PIN 14. @2 y8 G+ U. o: z% P! u- p
5#
 樓主| 發表于 2014-5-10 09:54:26 | 只看該作者
void init_camera()
: t$ y. E* L, N' Z1 r, @{
: f% W( k3 P* D5 D  pinMode(CAM_SHUTTER_PIN1, OUTPUT);. R" g# ~1 D7 d' k% Z8 [
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
1 o3 X$ S+ r: r5 |4 s  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
* M! R/ }$ q; `8 C+ n  G9 d // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1* G: b! t1 S1 K2 m/ Q: j4 W) {4 o& P
// pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2% |5 x! [4 A- K( D
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
9 E; A0 n2 F4 |5 F4 Y1 M5 H$ D}& ~& B9 Y* V; t7 f; [5 a

/ g& e3 S0 f& `+ W' @) cvoid camera_shutter1()4 a5 R% k7 Z; L- `' P& H( Z
{2 k  `# V& Y* X# A" R
  // fire the camera shutter via relay...1/4 sec hold time
+ N1 v  z# ~8 u. x+ Y  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
% s3 g, a4 P) |$ G  delay(250);
: K! c! w* M$ }. W  digitalWrite(CAM_SHUTTER_PIN1, LOW);
7 F% G+ G& h7 T8 P( n! ~' D5 S9 v" x9 c7 W* Q0 |% Y0 A) R0 ^
}% H  F# d. y; n! a7 i
) }, x: ]7 F& h
void camera_shutter2()
: {" U9 G1 Q9 [8 R! Z9 ^3 s{0 h; a$ W4 C- [# T% p
  // fire the camera shutter via relay...1/4 sec hold time
+ Q! u+ `$ `2 H! [* G4 V7 |. }8 h4 f  digitalWrite(CAM_SHUTTER_PIN2, HIGH);
4 A; T) `1 i4 }* F* F6 W  delay(250);& p9 H9 e. Z8 X* ^. D( J- \
  digitalWrite(CAM_SHUTTER_PIN2, LOW);
, C8 ~, k- S) g2 L: E  J( W  n4 o
/ d' V0 m& Q2 [& Z3 P* q}  ]3 ^0 B6 V4 S% f
/*
8 D8 W6 Q4 k* M5 X0 w; n# [( Fvoid camera_aux1_on()0 C, \% d7 y' P7 [  ]) x1 z
{8 M" s4 @( n( T" R+ X" x
  // turn aux relay 1 on- I& S, d. B  o* Q: K  X. n  c9 A
  digitalWrite(CAM_AUX_PIN1, HIGH);
& e0 |# E3 S$ t}
' k! ]. [" `. n. l: H& s& ]; ]
6 y- F6 U5 p3 {/ d  Xvoid camera_aux1_off()
* D/ j' }! p! v: j) O{
5 g& H7 `* t% H, J: v  // turn aux relay 1 off
, |9 ~" Q; Y. \6 D% N- b( F: v  digitalWrite(CAM_AUX_PIN1, LOW);
1 s; H2 F( p; N8 G; y2 h}' p& ^4 Q% o( Y# T% |+ `" \" q

" Q5 E5 T6 ?4 ^' q: E0 Uvoid camera_aux2_on()1 p& O( l8 y; A7 c; E( W7 W
{; Y" P. h# v& X4 U% u( \& E1 s
  // turn aux relay 2 on
3 |+ I5 j8 U7 ], P  digitalWrite(CAM_AUX_PIN2, HIGH);- J2 q7 I) _9 L+ f7 q1 J6 p/ S3 G' m$ o
}
$ C- t/ [- [1 g. g4 H2 n$ z% i: W$ \" i
void camera_aux2_off()
0 E" b, m3 U( H# A6 Z( _{
5 k0 L9 p# g9 q! x4 |; J  // turn aux relay 2 off
, Y/ B8 R/ {6 `" j  digitalWrite(CAM_AUX_PIN2, LOW);. T3 q8 P& ~$ d. S/ `
}9 d& v$ X8 G+ {# q- {3 w- U1 |

0 f! l3 p" u! Z3 |void camera_aux3_on()
9 Y9 r$ z2 |# Q: j8 r2 S{  d: D# ^) N( c" Q) b
  // turn aux relay 3 on7 u( o- \2 \. a  d  [- `2 I1 }
  digitalWrite(CAM_AUX_PIN3, HIGH);
- h  T; A- g+ G& v/ b' ]}( K# T9 R6 X" d( v2 e2 A' g
, @2 J3 Y8 C$ Z0 s, i
void camera_aux3_off()4 F8 S+ _; x1 x* T
{: [; f" U5 c2 j' p# T
  // turn aux relay 3 off3 q. f* m  N( w# p2 y* B
  digitalWrite(CAM_AUX_PIN3, LOW);
6 [% r: F, k# ]' n3 b+ |}
! B: N" V' Z2 b6 _
9 q+ \3 S. u: c5 svoid camera_aux4_on()
8 D1 y. F$ D5 Y0 J3 ]{; c8 n+ J2 F- u8 u# p5 j4 \
  // turn aux relay 4 on/ I/ k+ i# C5 G" s& v7 i+ q
  digitalWrite(CAM_AUX_PIN4, HIGH);! G8 o5 f$ t$ C1 d9 c- P
}6 f9 o. V$ H# k+ X/ j: i5 q& _

6 ^& n0 p8 H. u3 }void camera_aux4_off()+ f3 F1 u  ?7 O8 c8 J/ g9 G5 R
{
7 E1 h  Y3 T3 e  // turn aux relay 4 off9 \+ f6 t+ I" y2 G. F; l
  digitalWrite(CAM_AUX_PIN4, LOW);6 P6 c# }1 b# S7 I, w
}
6#
發表于 2014-5-10 09:54:28 | 只看該作者
樓主推薦的網址不錯% n6 L; z% v. K) X
請問樓主是玩什么的,用樂高玩具嗎?
7#
 樓主| 發表于 2014-5-10 09:55:02 | 只看該作者
// our point structure to make things nice.( }9 W! D7 n5 R# L  s
struct LongPoint {6 B% ?6 v3 W$ ~# U
  long x;
. Q: J. y0 a( p. ~  long y;# E' H( X0 v& y/ Q6 S2 P
  long z;
- ?. M( L) k( L- ]  long u;
# J7 I8 S$ [' `};" `8 M) W" a/ I3 k" t
) {* q0 [5 C$ b  F  Z# h$ r
struct FloatPoint {
. z: Z' F1 @6 c, _  i* L% Y7 E/ P: i  float x;
4 p+ W: u& `- K, U, r  float y;5 _1 g  W" e, o* o
  float z;
3 ?9 I- z( V9 R/ P  float u;
2 V! r2 M4 Y9 f3 E};
! X% Y/ z  h) C' r1 O
- w# |9 I, d! g8 d) `  rFloatPoint current_units;
' ]; y/ {, v9 r( W, w7 k/ e8 b" B" kFloatPoint target_units;
7 |; ], c# z, [FloatPoint delta_units;
% g+ Y; K1 R+ C0 [9 A2 |3 i, p; D/ N
. a5 l6 j" v3 k' z. @$ iFloatPoint current_steps;
- y- E9 j7 c  [; O- N% C3 cFloatPoint target_steps;5 ~  v/ Q5 d1 ~
FloatPoint delta_steps;
, b2 w9 c, E5 U8 v$ i8 X* b
+ |4 T3 o5 D# }' M+ gboolean abs_mode = false;   //0 = 增量位置模式; 1 = 絕對位置模式
) u) r: f, T8 A3 j9 _! y0 r- p* O8 w! \) W4 t
//default to inches for units
( Y% n5 }- T0 I2 {3 b9 Vfloat x_units = X_STEPS_PER_INCH;/ x0 b' z- K* R
float y_units = Y_STEPS_PER_INCH;
$ g$ z3 A# ^) e2 mfloat z_units = Z_STEPS_PER_INCH;  g7 S; R8 @4 J4 ]. B
float u_units = U_STEPS_PER_INCH;' o7 S: `) X$ D' ]+ \
float curve_section = CURVE_SECTION_INCHES;9 D4 i/ c, W( V5 I  S& ^: ?
5 t' W$ Q" `0 b* K
//our direction vars
( X# S2 {8 X: k8 w$ h! e& F% x* r+ d$ rbyte x_direction = 1;, Q* P. O" i. w  d* o( Q  @
byte y_direction = 1;% s1 {. x9 s# n- ]
byte z_direction = 1;) X+ ~# }: o, {% B
byte u_direction = 1;
. N# B  w9 P! d# q3 Z6 p
* g0 o1 U* q7 i( d. z! h//初始化字符串處理$ D7 S, m/ F: s
void init_process_string()
+ v) M- T5 ]; S, E# g2 V( _{4 m5 ^- o  d3 V
  //init our command
6 O6 U# v* V8 L  for (byte i=0; i<COMMAND_SIZE; i++)
( B- ]! R' M$ q, k$ l    word_old[i] = 0;5 ?$ {8 [- S0 L1 r" u  t
  serial_count = 0;
6 `- `, y/ u$ x}
' j  \1 x. d/ Q* X4 y
: ?; K0 T. @/ m& B: {4 t4 _, K6 ^//our feedrate variables.
: H6 z4 x' v& U8 \' mfloat feedrate = 0.0;/ I+ I0 j$ b0 }! g/ J  g$ Q
long feedrate_micros = 0;
/ l8 v: M5 X, H" P7 x
# e6 L8 |% v& f/ x- j//讀取并執行命令
( _4 Z& _- N; \4 g/ Y1 {void process_string(char instruction[], int size)
% L3 ^3 v- Q+ `/ B5 O6 m$ _{( t( \) e! M$ u% @4 u8 {! K" ~) R) V
  //the character / means delete block... used for comments and stuff.
, O: N9 ]. R- Q0 ?$ B2 O3 \  if (instruction[0] == '/')
% V; A- u+ e4 u. ?4 A  {
. L, s- {# X' d" a    // SerialUSB.print("ok");$ d# {3 W& e" o$ \& h% }' {/ {
    // SerialUSB.print(byte(78));/ R! z; C) _) l0 P! w4 s. z
    return;2 l3 o5 C' L6 S3 {6 b% V
  }. H7 U! }  d! e! K0 c
  //init baby!! c: Z0 [; @) l* l9 q; j  \, V2 H
  FloatPoint fp;# K3 f2 ~9 ]8 d" b
  fp.x = 0.0;
8 J( Z" s: v* i2 Q  fp.y = 0.0;0 P9 h: Y# X% V9 k* [
  fp.z = 0.0;
6 J* D5 K$ u! r& B  fp.u = 0.0;5 s: \( d# t4 O# A
$ L+ K" x3 \0 Q" t/ P
  byte code = 0;! J* `# d( |# `. l. q
( v0 s( k' s! t+ ?' h8 @& U
  //顯示在處理的命令  A0 M% `7 x+ r- G1 R$ u6 x3 P) ^7 j
#if (LCD_TYPE == 1604 )5 D% h" t: B) {6 M% w7 U
  // lcd.setCursor(0, 4);4 G  N8 ~( c5 H
  // lcd.print(word_old);  l2 e% M( g0 ?! F8 d: ?3 S: @
#endif
3 R& D) H) @; u  SerialUSB.println();
7 K& |  Z3 Q7 u  u6 x  SerialUSB.print(instruction);
7 _) _  f3 f( H6 w+ k% c1 h, w  SerialUSB.print("\t");
3 a) P8 T0 ~& [* a" u
" c2 |: D9 E- b/ Q  //what line are we at?5 A+ w' S( [3 e$ M& J3 a! e: H/ I
  //        long line = -1;
% c; V7 `  G; c; }, K! c. c  Q  //        if (has_command('N', instruction, size))9 B" n/ }* p' P5 O- z
  //                line = (long)search_string('N', instruction, size);' X% Y8 Q2 y' f4 R: e
3 \6 x) P/ @& c+ t" y5 {$ ~
  /*+ w! z2 c7 I8 U( c
        Serial.print("line: ");, T+ e  W7 |  h: L
           Serial.println(line);
2 f% b3 N6 j8 y           Serial.println(instruction);  a$ \' p% K5 ^% f* p8 A1 O  H: G
   */2 \6 t6 [+ X' ^) [% H  \' E$ y  o
  //判斷是否讀取了個 G代碼?' G& q5 W4 Q- e- b5 l; X1 G
  if (
  T, E: d( L, x1 E    has_command('G', instruction, size) ||. L8 w3 y( O" Q- p! I; r- k) {  f
    has_command('X', instruction, size) ||, m0 w. w& g1 \7 Q
    has_command('Y', instruction, size) ||
4 J) ]& J4 j5 w    has_command('Z', instruction, size) ||  C4 m! i. B- P, s
    has_command('U', instruction, size)
/ x" P% X4 u9 m; \/ c. g+ ]    ); {3 C& n8 [% G+ t7 A3 K
  {
# p& R" P/ l; D. }; ]    //which one?1 ]* x" U9 S5 e# k% B! w8 I
    code = (int)search_string('G', instruction, size);
" |# M4 @, t, v0 }! y; i    // Get co-ordinates if required by the code type given: s9 M3 m6 |. N9 B( S8 f  a
    switch (code); x6 W- F; R, F/ ?8 l
    {
; `3 t+ Z; }  i/ O; {, n, n    case 0:& Z0 ?$ W- P1 X5 A' |/ C2 O
    case 1:
8 E* x  v" t9 L3 k    case 2:1 H9 h, w2 D& Z# U& m
    case 3:. N! c# h* }$ t& F1 L
      if(abs_mode)
% A( t! `3 i+ p      {6 W6 ?: C" ^9 B4 r+ E( |4 C; K
        //we do it like this to save time. makes curves better.+ ?. h% V3 s) x- j
        //eg. if only x and y are specified, we dont have to waste time looking up z.! l5 V% ^$ z. q+ p- @, l$ p2 X2 H$ ]
        if (has_command('X', instruction, size))
4 y. k5 U5 I, w4 P$ `8 b" I- e          fp.x = search_string('X', instruction, size);% d$ l$ G5 k$ F
        else) ?. a4 l. x7 i
          fp.x = current_units.x;
2 O7 [$ @2 G7 q. v3 N' ]2 d' L; H$ s+ ]4 i/ C5 G2 l$ a
        if (has_command('Y', instruction, size))8 I  ^, X& t; h) m
          fp.y = search_string('Y', instruction, size);3 F* {, {9 i9 ]
        else- r; Z0 A: X& _* @
          fp.y = current_units.y;1 _% {$ ?& H" S. x7 y5 H8 ]; j
, Q/ C' o0 h/ H( D+ x+ N: T
        if (has_command('Z', instruction, size))4 u* U' ]4 E& \6 e9 U+ O
          fp.z = search_string('Z', instruction, size);
5 L: \& ~4 h! v3 Q" Y        else
5 q7 v% c4 C  r3 W6 u5 ?: Z          fp.z = current_units.z;
. }! ?) Y6 S4 T0 z1 N7 G          3 [) k; X% W- W2 I3 k
        if (has_command('U', instruction, size))  N9 x& p& M( E1 A; G
          fp.u = search_string('U', instruction, size);: R, L1 a# g6 Y8 {" r9 F
        else* j$ T2 G, M2 `$ _% a4 K% T2 M
          fp.u = current_units.u;
" A5 W% `( k, ^) {& ]      }
4 a% U" i& D% e& z      else5 c' \  ?/ h' a8 r5 {5 `- n& K0 A
      {7 J8 F6 R8 \" i, U! {1 t/ ^
        fp.x = search_string('X', instruction, size) + current_units.x;
1 D# h. }2 R! y' E  L/ v+ I) |8 S        fp.y = search_string('Y', instruction, size) + current_units.y;* G+ M4 P+ `8 }$ N
        fp.z = search_string('Z', instruction, size) + current_units.z;" J6 L/ U0 H' n
        fp.u = search_string('U', instruction, size) + current_units.u;
. K$ T- p4 W& c      }% Z5 u. r$ o! ~& w' z! F
      break;
1 k5 p7 v# L# d4 W    }: L; T9 F2 D6 p0 u. X' R% W
    //do something!
8 n. _$ i& ^9 o& S: _9 P+ U    switch (code)
: }1 z0 m" R+ [    {$ L, S/ _- Q* G0 Y
      //Rapid Positioning! E; n  I' J  A3 e
      //Linear Interpolation0 J) j3 O* C1 _& W% Y6 C( b
      //these are basically the same thing.
  l; c0 h6 h- _0 C! k. ?. y    case 0:7 g; U( P; H  [5 ]8 ^
    case 1:6 \0 b7 k! U- n; r5 d: m' c/ Q# L# n
      //set our target.; D2 s" k8 B& R' k- ]% q
      set_target(fp.x, fp.y, fp.z, fp.u);
1 }2 P, g) Q9 p1 e( P7 j      //set_targeta( fp.a);5 \0 e9 n# F  P/ h2 q7 ?1 s/ S) L
      //do we have a set speed?% m1 o/ T4 P! ^( n) f
      if (has_command('G', instruction, size))' J4 w$ P% L% k/ b
      {5 T# \* V6 u3 j. \1 b, _; `" E
        //adjust if we have a specific feedrate.
8 h: m. [; c+ \: @3 h        if (code == 1)
. e3 m+ T) H( ^3 }% e1 X6 z        {, Q0 V9 u/ q% U" J3 ]
          //how fast do we move?
3 N9 ]: M! m- t0 X/ J          feedrate = search_string('F', instruction, size);
5 u9 E: o' I# V" n8 [# P4 V          if (feedrate > 0)
9 Y" W* x2 E0 |0 V* o  [' r% L            feedrate_micros = calculate_feedrate_delay(feedrate);
+ u! S4 C. R1 O& w# D% m4 Y# n, D          //nope, no feedrate: x3 v5 k" v) f
          else3 l' h4 Y: {+ r" ~* z/ L* O
            feedrate_micros = getMaxSpeed();
9 [( t9 s: \. a        }0 y" A% G3 Q* ?/ g2 m" D
        //use our max for normal moves.- k+ n* \5 Z, d7 ?2 v
        else
! p" @: @$ y7 c          feedrate_micros = getMaxSpeed();* U1 I) Y3 i! S# P% [" H
      }2 u9 l1 o) i% i: Z7 c6 E& n
      //nope, just coordinates!/ k. I% h1 Q) o  X6 b
      else
8 B8 D: P7 s- g# J. y9 K      {2 t3 \# C: c' L) n! G' W" d
        //do we have a feedrate yet?, X9 |" w0 d4 s" \) r: y+ b
        if (feedrate > 0)  |1 |' p# a1 W+ f
          feedrate_micros = calculate_feedrate_delay(feedrate);
# q0 R, h( B, B- E, W+ o        //nope, no feedrate
: k  O. E& ]! [        else
# T7 p3 l* E5 }4 j          feedrate_micros = getMaxSpeed();& p+ ^* D) R9 b3 D8 i* E
      }
" i  h; [6 B2 B  _) ?# `9 S; L  s9 L$ U6 F9 W0 m
      //finally move.4 P! K) v0 t- S) }- K
      dda_move(feedrate_micros);
3 P" T! r) |5 B1 y& ?      if(stop_flag) return;  k% z/ Q# P% w9 t* J: f
      break;
/ F$ L5 V, P6 H5 Z2 p( i: C4 L2 D9 I3 e( f- e
      //Clockwise arc: q. i( G; u; Z. u0 ?0 m5 o
    case 2:
" Q4 F. x, b. S1 c; y# E% y      //Counterclockwise arc
  i$ M5 x5 A# g/ i) c0 g. v. `    case 3:
. M0 g  e7 n3 Z& i5 |      FloatPoint cent;
" r$ F4 n' V# I: g" c6 U      // Centre coordinates are always relative0 w9 W% x+ l* L& Z1 y& p
      cent.x = search_string('I', instruction, size) + current_units.x;& Y- f! o* g5 C7 V6 d
      cent.y = search_string('J', instruction, size) + current_units.y;* W  S( L9 e/ u/ L3 v4 ^0 }$ h
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;  i* [  K& e  [" h

1 ^7 n* v! P) W) G      aX = (current_units.x - cent.x);
; \1 p( N2 F/ L1 m6 a      aY = (current_units.y - cent.y);. d7 K! _6 v# X
      bX = (fp.x - cent.x);
- n" W! d+ G2 \9 H1 e/ S      bY = (fp.y - cent.y);8 c  \' l  V* A+ u* Q6 n& w

# L! Y$ d" F1 P$ g6 {+ C      if (code == 2) { // Clockwise7 d' F& R4 ]/ s, r
        angleA = atan2(bY, bX);
' C) `" R& q& Z6 Q        angleB = atan2(aY, aX);: A' |7 Y" N& V: T( r' ]
      } . E' b/ E3 A9 \# ]( T! S
      else { // Counterclockwise
' ?7 h+ t4 B* J9 s6 t" H( F, w        angleA = atan2(aY, aX);
! o) A) p, A4 W# m* q2 J% K4 g        angleB = atan2(bY, bX);# k8 `( F6 W; U/ z) [, v5 a1 p
      }; D8 `4 F- ]: q1 n- S0 [
      // Make sure angleB is always greater than angleA& z4 d  H- t* \0 J
      // and if not add 2PI so that it is (this also takes0 `% g9 L5 _6 P
      // care of the special case of angleA == angleB,) [& I- c+ S8 O' i) v
      // ie we want a complete circle)
- l5 J- k  B* r' C) W8 E      if (angleB <= angleA) angleB += 2 * M_PI;- |! s7 ?) h% |0 o* |0 v; v4 G, l
      angle = angleB - angleA;
$ E6 b% U) O% o) u6 z$ `8 c8 @8 C( w
! s: P; U, `/ m/ H/ J      radius = sqrt(aX * aX + aY * aY);
4 E6 l7 L- y" Z$ t3 a8 T      length = radius * angle;' z% ^! b  f- B; H  B( E6 n
      int steps, s, step;
' `, J6 a5 O8 S6 ?      steps = (int) ceil(length / curve_section);+ J& o3 a, H+ X, Z
3 H% \4 ?: }) A- K4 t9 O
      FloatPoint newPoint;
7 [0 i: H. s, y8 _4 {9 k6 V; U3 m      for (s = 1; s <= steps; s++) {& k/ A' w: R; u4 j
        step = (code == 3) ? s : steps - s; // Work backwards for CW( {& t! [: i7 u! [2 d; X" d! `
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
. R3 |$ Y8 o$ o* p% t4 p: b2 i        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));3 G3 x6 ?. w5 K1 Z& S; M, X6 l
        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
/ u3 w7 g- Y0 X
% a0 E0 Z7 Y/ T* [$ W, W2 Q8 X: Q        // Need to calculate rate for each section of curve5 K6 t' ?- B8 o; q
        if (feedrate > 0)- ]# J2 }* N3 q+ \/ L; N
          feedrate_micros = calculate_feedrate_delay(feedrate);
; L9 I+ T9 H6 \  Q: Z; L% ]; r" z        else
. i8 @7 c2 e7 c' [7 a          feedrate_micros = getMaxSpeed();3 y" W2 z& X: |6 E" W! b* u. L

; k* h8 ~# e& Y3 }  J        // Make step
, H% G5 y# f2 s: U& a% p        dda_move(feedrate_micros);- a; ~, |3 \/ m
        if(stop_flag) return;
, J+ N- X# ]# Q6 k3 v: P      }
) J8 Q# c' o6 Y  |+ j0 M$ t% h
: T2 Z& B8 t' D9 I0 Z7 x# C      break;
' c" M) V+ ?" h6 x% H! R8 E0 G# D$ `2 t! ]4 |0 ~, R2 U! `
      //Dwell
, h  v5 x2 E! G, n, r6 n8 O    case 4:5 }9 f$ S6 q) @9 G' \7 h+ t  L2 V
      delay((int)search_string('P', instruction, size));
/ c, u" {3 R! y) Q      break;' o5 z7 s6 s* `, V/ d# s# u

+ i9 @# b5 c' T' E* z0 F      //Inches for Units& D% _! K& p8 p8 o  a
    case 20:. {: D, R+ t: N
      x_units = X_STEPS_PER_INCH;6 l( p2 i! s$ O/ P- S/ g
      y_units = Y_STEPS_PER_INCH;. C% C' W1 i8 e: S. G0 s
      z_units = Z_STEPS_PER_INCH;% r. D) {) l* V7 ]
      u_units = U_STEPS_PER_INCH;
: f# f1 y; ~0 W3 H5 k- b) [6 X      curve_section = CURVE_SECTION_INCHES;9 I& m" K/ `2 F! @3 n
      calculate_deltas();
7 ?$ T# c' X' V# E      break;+ [% d" T- ~. L+ Z& f: Q

# d% M& `6 C' ~! y9 P      //mm for Units: y- X% t$ b1 c" u
    case 21:+ a* ]0 V, F, ?/ w
      x_units = X_STEPS_PER_MM;
0 M1 k, W$ O2 r# E      y_units = Y_STEPS_PER_MM;
  S9 c) c  o, O# f8 M4 p      z_units = Z_STEPS_PER_MM;, V6 ~+ t$ b& a$ ?6 O( `4 C
      u_units = U_STEPS_PER_MM;
' e8 C' t- j% w6 I      curve_section = CURVE_SECTION_MM;
, K* D+ T! w( {$ P" S      calculate_deltas();
, q1 [8 K' K' W7 k1 F* n7 x- d      break;
7 B$ r0 N% y  j* M9 [0 D7 F6 b& D0 h3 q: \. @& h
      //go home.7 c/ _: l) A9 O; V/ s
    case 28:( Z) u+ D; I7 R% \8 |
      set_target(0.0, 0.0, 0.0, 0.0);" x4 I7 G* i% }
      dda_move(getMaxSpeed());
9 m5 H# A* o" Y# Z3 m5 }' y4 I  a+ C- x      if(stop_flag) return;. t9 u) n  @2 y+ V& N
      break;
0 F0 j+ Q) d1 n/ s/ S' l" S5 {
" D7 {/ Y" }* H0 x; x      //go home via an intermediate point.
  e$ ~' T0 V  J1 e7 O, b    case 30:
" s0 x; @$ \( j) W! G9 f: L& N      fp.x = search_string('X', instruction, size);& K2 a) U& t4 W' j- Q4 H( f8 Q0 f
      fp.y = search_string('Y', instruction, size);$ L8 J  t* v( |
      fp.z = search_string('Z', instruction, size);
  g8 Y; Y! R5 A* s      fp.u = search_string('U', instruction, size);
5 R3 D/ Y( F) D      //set our target.
# a5 {% G6 G3 [0 H      if(abs_mode)# |( \9 c7 q" K- U! b
      {
- f9 h2 d$ k1 s, o' {. m2 z! C        if (!has_command('X', instruction, size))+ H& L) ^) y# p' L* B! W
          fp.x = current_units.x;
: `6 a- a' V4 g5 Q9 T) Q        if (!has_command('Y', instruction, size))4 X8 k" b/ s* j
          fp.y = current_units.y;2 X7 d+ X3 w. T3 r' f
        if (!has_command('Z', instruction, size))
% Z) s% Z9 Q7 @: r          fp.z = current_units.z;/ |- v1 q/ g0 `( M/ x4 e
        if (!has_command('U', instruction, size))4 O& M0 s4 ~* D+ W2 U, d
          fp.u = current_units.u;
2 S  G0 C5 ]6 K  Q; G3 I  m2 j  Z        set_target(fp.x, fp.y, fp.z, fp.u);9 V8 S) c' D  e' B. b: Q
        ! K6 u9 Q% h7 s, u9 s
      }
! Q: \: L( }' t, o5 @5 v/ m      else1 x- d1 L5 I8 {- t
        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
3 l- g* s( Z2 m* a0 _& A( M, U      
& t8 _1 b  a$ F, s0 u6 F      //go there.
* T/ b2 ^1 M% I8 `      dda_move(getMaxSpeed());
3 j, S( x8 l" O6 K4 b      if(stop_flag) return;
7 P' b  x: \3 n; g2 D% U8 l# w1 ^$ L/ D: Z2 W3 r3 Z8 |
      //go home.
. Y  r4 ]$ T6 V$ L8 ?& M& F' e  \4 o      set_target(0.0, 0.0, 0.0, 0.0 );4 |: P5 T" u& s$ @8 O( T
     
+ X8 Q9 X/ b3 q" ^9 W6 w; Q2 O      dda_move(getMaxSpeed());
2 Z" J" X6 u9 F( N: o      if(stop_flag) return;; o! |! T. e9 t" w3 c3 i
      break;
4 u* t' y3 i2 o( C% e
+ [) d" j8 d$ ?" v) N* o6 E4 v      //Absolute Positioning" p% o3 ?' c0 v% p" ?- z
    case 90:9 L/ m2 K: N+ J+ [# T; z2 a
      abs_mode = true;
/ y$ z$ L6 [% `+ H. |3 _7 M  A      break;8 c& d/ ~* o" Q( I4 ?4 L: r

7 p9 [+ a; w( F+ W4 z      //Incremental Positioning
% H) j' @3 t$ V/ {3 _6 f" b5 g& k    case 91:
1 ^( M/ `+ q6 o" R. f7 G      abs_mode = false;
3 Z7 j2 l; a" H      break;! n/ G7 o+ b' M  D9 _2 `& T3 U
1 x/ s( q0 D2 l+ @
      //Set as home4 N" L+ z* |7 U9 k# x0 ?' }
    case 92:
! i; @% K3 ^  Z7 A     
( ?  M9 r& \: i  C' s      set_position(0.0, 0.0, 0.0, 0.0 );: {7 k/ ~" }! m4 ?
      
3 d; {1 d+ R4 @. K% k      break;2 f+ D8 s2 h  C* J" |( q, Q2 [

  y; w* R1 {) `* d0 Y      /*7 K0 r6 s8 Y3 \* p$ u( w- g
                        //Inverse Time Feed Mode
9 j- I. R# j% K  K+ {% P6 D6 W# O                               case 93:
6 L0 y' o6 M4 s& b4 m8 \% [       5 q/ k* K2 J9 R$ o5 A& P' y
                               break;  //TODO: add this2 k( Z: R3 q/ Z. ]% m9 a
      
. L- ]0 R1 m8 x$ W                               //Feed per Minute Mode5 i/ W" Q  G6 C6 H
                               case 94:
$ f/ z  U1 e% Y" b: t5 ?       + x; R/ s- w8 C4 J* x* C4 O8 Z0 V
                               break;  //TODO: add this
. {, G4 a- a- a& P/ ?       */  A7 S2 F4 {% ]+ O3 p
" e7 s% h! @$ ~. y" Q5 y+ _5 ^' u
    default:+ z9 s( b& s' W; Q
      SerialUSB.print("huh? G");
% M1 z/ e$ @2 E) k$ T$ k. L      SerialUSB.println(code,DEC);
7 O5 b) A1 _  o" x  O& X    }
7 @! x! I$ Q2 q& `2 ^. c# t  }
  E( c9 Z! _2 g% X- b
5 @4 j& x& |. Y4 V: [  //find us an m code.. p7 I6 g& @3 w& A* r. I  {
  if (has_command('M', instruction, size))
% G* ^; R% A1 Q* q8 c2 x5 u  {8 _8 j, j& y6 g1 ^
    code = search_string('M', instruction, size);4 ^3 W  q$ J  s. P6 C
    switch (code): D+ f3 q: C$ X2 B& x) }, i
    {8 [/ a6 J1 i9 ?: e5 a
      //TODO: this is a bug because search_string returns 0.  gotta fix that.
3 d  p" Z$ I8 M+ W" x+ v    case 0:
! e4 u/ C+ o# T$ k! v* Y      true;
& S3 T4 u) k4 t- g. p9 s: p      break;
; X, ]' l$ w8 e; t. i. o/ |
; |* m0 k4 [& X8 p2 V8 i    case 100:: g) |! \. V6 m; F
      break;4 B, o6 z! }+ l7 I
& R0 Y) E' G0 @4 `% M
      // fire camera relay% y0 q: _- q1 J3 z( s' E. H
    case 101:3 ?8 H4 \, R6 g8 J# r0 _
      camera_shutter1();
4 A- L" u# M7 O- Z$ m      break;! G3 d0 ~' L' v

; e+ k5 ^$ Y$ x. S" q      // fire camera relay2
! N6 K  w, J: X9 a: Y    case 102:- u8 b! |3 L1 L# _3 x
      camera_shutter2();
) Y8 c; R9 x9 n) |% K$ o      break;
, n. w" v/ q/ J, E/*
+ B# Q* D" A) q: g9 ], E      // turn aux 1 relay on- S* ]* G. g3 z2 e& G
    case 103:
4 ~0 L  C! u; j      camera_aux1_on();2 b! _" P: ]  n8 u' p
      break;! l0 ?  m' g! N. s
9 V& P' p2 x0 K: Z/ W! u( y
      // turn aux 1 relay off  t% l+ [- k8 F+ E2 T' ^7 @7 i6 v
    case 104:; Q8 r( Z6 e( |; A
      camera_aux1_off();
) [# e  m1 I# d* S9 E- q1 ^! Q      break;
* C/ r$ f, n6 V4 ]4 H) X; y8 t0 C; w  E$ S+ f* H0 G1 @
      // turn aux 2 relay on1 L7 l4 E" m! ]/ k0 @$ X
    case 105:
6 t; ^/ o! O/ l; [5 i      camera_aux2_on();
, E4 U9 S" m0 |, N7 L! w      break;- s2 `$ e, Y) u# d

' a7 F! e+ m" g$ h      // turn aux 2 relay off6 k9 o& L" v* y% Y5 n. l( [
    case 106:
, Y. D4 r3 ~/ s0 _; O      camera_aux2_off();
0 ?" r; ?; J0 V& c0 b      break;
( k. y0 Z4 {/ ]  A# k. n: k$ U7 @  v4 B) @
      // turn aux 3 relay on
7 [/ q- n; V1 `3 O4 t! W' L    case 107:% v' g. `" v( `; i) U& `
      camera_aux3_on();& C) E6 \9 m$ G- ^2 J
      break;6 H# s/ m& ^0 _5 W8 q1 e2 a& m$ S

/ Y, r4 ^2 e+ L$ m      // turn aux 3 relay off# Q: L, X. P: c$ d8 l; {
    case 108:
7 f( Y8 f# h7 w- Y) @      camera_aux3_off();- d" F3 t  l8 H' n- `" [9 f
      break;/ r" f2 A; @9 N; V; K. T* N, W# F& y

. ], H3 ^  J3 k2 S8 D      // turn aux 4 relay on
, R+ H# t7 j$ {% l+ ?    case 109:
* H! t9 p9 J7 }6 O: J      camera_aux4_on();% T5 ^* S% f& M' e
      break;
4 G( c+ h4 g$ E# K3 z- b% T8 f0 [# ^
      // turn aux 4 relay off7 H  f, u* L2 x. j8 c; d4 n/ D
    case 110:
$ S; e- y: F- u# {      camera_aux4_off();8 {# v' G1 f! {. X
      break;4 x3 H5 A& {$ X$ _" z1 M
*/
0 @: w+ I  }; L/ P2 ^; E" J& `    default:
) C& J1 M, g) O2 H3 p4 _, |! s2 \( _  @/ q
      SerialUSB.print("Huh? M");# h* p. P/ Q  p# Y- D
      SerialUSB.println(code);' @% m( x4 F2 P# \
    }1 k: G5 h; y8 t, x) E% X5 E
  }
$ X" L6 H3 z$ y. n- Z
9 a3 }& c8 j( m# Y% [- G7 ~  //tell our host we're done.3 Q- Q( J( S9 o) K
  SerialUSB.print(byte(78));
; F& `1 ~# h  |$ H
, i4 t8 X+ w" r, i3 [; o& y}
2 e/ k- D0 m; v1 L6 x: S9 q4 k" y) ^4 K$ n* X1 ]4 l  e
//look for the number that appears after the char key and return it/ x6 i- P; t% F
double search_string(char key, char instruction[], int string_size)
/ G) [- A2 N8 [{
1 _% y6 e' l& B9 v. G" t1 e: E/ N; Q  char temp[10] = "         ";) Z6 C7 |3 m$ P
  for (byte i=0; i<string_size; i++)9 Y, E3 l0 N$ j
  {
- t/ Q- B5 g2 D/ U    if (instruction[i] == key), K) d0 g' E4 ~. m9 e! G0 d
    {
; j4 w, y& Q! S  H  H      i++;      
: G6 h* O4 h5 N+ [& M      int k = 0;
! ~" b# J# t* \      while (i < string_size && k < 10)5 U7 u, J! V7 t3 X. J4 {0 d
      {5 e& ~5 `0 {7 z; G
        if (instruction[i] == 0 || instruction[i] == ' ')
+ |0 f& ^* _5 s$ n; m" p6 R+ m          break;
% b  `7 b4 t1 f# z1 o- J9 ?* s/ J4 h3 H: y( e6 X; x5 y. ~
        temp[k] = instruction[i];7 T" C$ }2 a5 ]) }% Q4 K
        i++;
( k* {) X" {4 B9 F' G        k++;
' h7 I0 ?5 |1 G% s( ?6 R  Y) z7 [      }6 a& U8 n, q6 S7 ]& T0 N
      return strtod(temp, NULL);
* x9 ~1 f2 w# b" N% A0 W, n    }2 F/ X0 \  \4 f( F/ p; ?  Z" O
  }4 D) Q6 [4 t" C8 z; d

& t- i, P4 a- `! X) b  return 0;- G" B" U: j6 K( X: l
}, x! S7 e8 d# _4 O* t7 ~! r

8 P9 v0 j; l8 z2 A0 f' @$ Z( C$ ]//look for the command if it exists.
; B2 f* f( k' @bool has_command(char key, char instruction[], int string_size)
' T' j# K' d( I: X3 D% o{( @1 T5 W* K/ B$ W
  for (byte i=0; i<string_size; i++), v5 ?# W9 O  A2 x0 q& h# u
  {
* l" Z4 t1 L2 P& d; T6 \    if (instruction[i] == key){  P+ K+ ]7 J3 E% Q0 u6 c

! s; V- m% ~+ S  d, E% T      return true;  c+ |8 U9 W* |- o
    }" I+ n0 }* \5 z
  }
5 w6 L- v' y, l2 o; y
5 n, T5 n3 z1 q$ t5 b8 }* P  return false;
2 }% d8 O: T" t9 q8 m}9 B) ]& H+ h  ^  ^2 J1 i

; h/ D. V2 B) p# Z$ {" `$ j: Q' v9 H" Z5 j7 e

7 @( _4 h. g! L/ o
8#
 樓主| 發表于 2014-5-10 09:55:37 | 只看該作者

5 @: w* r+ o" K" P  t//init our variables
! t+ H8 n0 ~) B, _long max_delta;
, p: z  R& A: O2 D) r7 n; dlong x_counter;
/ P/ W" x! [8 c9 blong y_counter;# L6 S$ N4 W$ M4 V; p) S+ R/ I
long z_counter;5 J' M( b' y1 I/ c( M8 j9 {$ r
long u_counter;# |2 o0 |/ |+ Q$ Z4 F  U8 u7 ~
long x_pos = 0; // x position in terms of absoloute motor stepps
3 m/ z7 e$ S% Q: ~/ y. U8 Vlong y_pos = 0; // y position in terms of absoloute motor stepps
3 w7 X# Z( ?' S& Xlong z_pos = 0; // z position in terms of absoloute motor stepps
: D% S, M% O+ wlong u_pos = 0; // U position in terms of absoloute motor stepps
. n, [  @6 l* W4 g) E! K. X0 I
% k" a# P1 B. x- C9 d1 \* z9 p! ^2 {bool x_can_step;
/ `4 V8 q8 g2 i# T+ Jbool y_can_step;
6 {& I$ D; Q1 D- p; Ibool z_can_step;  G5 R, l5 k# N" n# t9 k/ K% ]1 T4 r
bool u_can_step;6 z7 r1 Z- Z6 P6 p+ [# d
int milli_delay;
( R7 X2 J' G9 ]# J7 k2 X" o' p8 O( ?, ~% N- M. d$ W) G7 @, U% k
void init_steppers()$ O' M- ~/ ?" X- T9 t
{" e" T2 o+ Q, I0 V
  //turn them off to start.
+ o- m) ~: g; f  W  disable_steppers();9 |8 S7 f8 G# F. \" k4 G" H2 |

  R  m6 i5 O2 L  //init our points.
3 O1 L- y& g6 g( H5 x/ R  current_units.x = 0.0;
" W% r. Z4 {2 y0 q, N  current_units.y = 0.0;
3 i( \' L: i, d  B  current_units.z = 0.0;' O) m- M# j+ s4 N$ k, g. A
  current_units.u = 0.0;9 Z/ M" P2 a/ N4 l) J
  target_units.x = 0.0;/ m2 i4 `! t( H0 L! I# T5 {! j6 _2 S
  target_units.y = 0.0;
$ `5 q& I' H+ E' l6 G3 Q  D  target_units.z = 0.0;3 W- k: F4 H( }0 w1 s1 j0 L* b( ]3 {
  target_units.u = 0.0;
$ n' h6 [+ k) B2 R6 Z4 a3 q  2 w4 I9 J# x1 w8 H& [! W2 c7 P

. G' `  g- ^3 ?: `  pinMode(X_STEP_PIN, OUTPUT);
, k+ {% I& \8 F  pinMode(X_DIR_PIN, OUTPUT);
' c& F8 s3 l0 }9 \$ O. U( b% z  pinMode(X_ENABLE_PIN, OUTPUT);6 P8 I$ x# m/ x& ]; l
  pinMode(X_MIN_PIN, INPUT);
3 i; p+ c5 [) _& f% I8 a; n  pinMode(X_MAX_PIN, INPUT);
4 |% c. O# r$ k, W8 a( _8 v* c
! O6 o0 F& c! o$ p  pinMode(Y_STEP_PIN, OUTPUT);
0 |% Z5 J& n7 ]( Z  U, k  pinMode(Y_DIR_PIN, OUTPUT);
; q8 ^0 _0 J4 m" d6 a' [# y7 l  pinMode(Y_ENABLE_PIN, OUTPUT);
4 [7 w- L; x/ o+ Z; |% M& _  v  pinMode(Y_MIN_PIN, INPUT);
( W" \! a/ g: H' h  pinMode(Y_MAX_PIN, INPUT);
. m4 Z$ n% }, p0 y/ \9 i! ~$ R9 P( z( B' W' X
  pinMode(Z_STEP_PIN, OUTPUT);
+ Y1 z" m/ i5 }) f  pinMode(Z_DIR_PIN, OUTPUT);6 s2 k  _, {% m5 S0 T" {
  pinMode(Z_ENABLE_PIN, OUTPUT);2 W, I3 s& r1 z4 X
  pinMode(Z_MIN_PIN, INPUT);
0 d6 V; b1 l1 g: H5 c  pinMode(Z_MAX_PIN, INPUT);2 M6 v/ H( v. w

/ v5 N, z5 m$ ]% }  pinMode(U_STEP_PIN, OUTPUT);7 S% n: I6 [; J2 F# s! T7 j, d; }
  pinMode(U_DIR_PIN, OUTPUT);
* s( m8 |  D6 ]  pinMode(U_ENABLE_PIN, OUTPUT);# c! s/ z: }6 f" C3 p
  pinMode(U_MIN_PIN, INPUT);- d+ {6 f: L  |# c8 N5 d3 \
  pinMode(U_MAX_PIN, INPUT);; f' l$ A, z- L. l; h( F! p- w! o
  //figure our stuff.
- O- e, d  t: E% S0 [  calculate_deltas();. H; d( [+ g( w. `3 K
}
' _$ `  w) n& w, R; C; ~0 A, G8 G. T0 l2 k$ o7 [; e
void dda_move(long micro_delay). D9 g+ Z. U( |* F8 J7 h
{
! t' i0 ?2 |- x" F1 D  //enable our steppers& s: a6 G7 ~9 S- r6 y& t6 ~
  digitalWrite(X_ENABLE_PIN, HIGH);
7 E' {1 p+ A6 G  digitalWrite(Y_ENABLE_PIN, HIGH);7 ^+ D$ T! F$ m9 j
  digitalWrite(Z_ENABLE_PIN, HIGH);. v% V$ H; ~. T
  digitalWrite(U_ENABLE_PIN, HIGH);
5 c  q3 ~5 ~* C1 f6 {  //figure out our deltas
# E& R! V  p* `/ d  max_delta = max(delta_steps.x, delta_steps.y);. Q# w+ U  @! Q
  max_delta = max(delta_steps.z, max_delta);
: q6 w& T2 k/ u4 M3 r, Y  max_delta = max(delta_steps.u, max_delta);3 \( M& _3 l  e# x9 @2 N3 `4 m
  //init stuff.
6 |) c  O8 N9 g% S- L3 t5 }  long x_counter = -max_delta/2;
  J% M! x2 F* s+ ~, _" T8 P% B  long y_counter = -max_delta/2;
* @' r$ N. _* o  long z_counter = -max_delta/2;
3 @( s1 ~- ~, B  long u_counter = -max_delta/2;
& ]5 j6 }" l# p& J. K4 p; L' N7 ]  S( U% C- E' p; `9 z
  //our step flags
7 ?* O! M$ ^' `$ [# B$ `  bool x_can_step = 0;, O  P4 K2 i$ L, x: p3 J5 z
  bool y_can_step = 0;8 o6 {7 M* R& g
  bool z_can_step = 0;
" S3 y0 i& r$ L# Q5 j3 m& I  bool u_can_step = 0;! t- K; t$ o' B0 ~* n! N+ {

" R; v4 Y" I/ M) V( Y# }! K  if (micro_delay >= 16383): T1 T; w! h& ]+ r
    milli_delay = micro_delay / 1000;- v; E; y) z" a
  else
- d- Z5 H" A& O  n    milli_delay = 0;
5 O- Z/ x" a& Q* v4 A/ d. ]
- |! q6 b! L7 `) P8 F- p4 G' j. p6 ^  @! H
  //do our DDA line!) j$ R& D( i+ B

/ n* Y9 d& L1 \6 W) J" J' F  do
! t' T  E  _" K1 O0 |' i  {
4 @- K- {$ U$ _7 ^    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;& c3 L% y0 g. E/ K
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
# a% k' b: ~' J5 o$ Y& B    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);! z% \9 i" L" g9 M4 z1 ^- B  O
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
$ K' m# w- {$ E& u! d    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);
0 b" I7 M1 M0 L8 F; \$ j. w  l    if (x_can_step)$ Y/ l9 O1 ~$ [0 z0 U* z
    {. y/ U. q2 j  P
      x_counter += delta_steps.x;6 l! T0 s. a# u1 D
: v! }4 D( ]/ }
      if (x_counter > 0)- h+ d5 A0 U* }) K6 J
      {9 a, E. e$ [, U9 k# L6 @/ ~9 [' {. M
        do_step(X_STEP_PIN);, N9 }2 f! J/ z$ z, j; |$ F
        x_counter -= max_delta;' j  a0 x# Q; y( }  u' ]
        if (x_direction)! U1 d( @% h7 A4 U. J
         { current_steps.x++; x_pos++; }
+ T' n1 D* r+ c& N; S         
0 D3 p0 _8 g' }9 p6 _3 ^6 G        else+ R6 l! F$ Q4 o" Y
          { current_steps.x--; x_pos--; }
  H6 ?8 A# k; i$ X$ A: d% t         0 i6 R0 L1 w; n" g$ P
      }9 c* z4 Y" \9 \/ R
    }
$ V, T' {# q- ^7 M$ k- B& F, r7 i: ~    if (y_can_step)- E. E0 F' t/ g! G" q8 r3 q0 }
    {
% V& T- u+ t6 p      y_counter += delta_steps.y;
+ z7 g! r3 _6 T% a! K2 n" a6 v9 e# {: J: _* @8 B4 o
      if (y_counter > 0)3 w0 H. Z1 Z7 E: |1 t9 r
      {6 e" h4 d+ n  q: t
        do_step(Y_STEP_PIN);  E6 I( |3 c" n* W9 U. e
        y_counter -= max_delta;
# M$ F/ r( X  @7 f3 Q* `9 l# F9 ]! P$ I
        if (y_direction)
% x/ @8 m6 E% P9 _/ c0 G  E5 z        { current_steps.y++; y_pos++; }
/ h$ d: I  q! y$ y* g8 F% `$ G      
9 @' I1 U, ]; ~        else
0 `) f, _* \& b7 e, {- o; B        { current_steps.y--; y_pos--; }
4 s  o# c* w9 G# K6 U, y: z          R) o9 P) h, X
      }1 \/ h6 C9 ~9 S5 L) X) v6 t# l
    }
- Z! g9 o* L+ c9 L
! K4 B) L! p& L2 @    if (z_can_step)
! T/ s- v1 K9 O  Z$ }: t9 k    {
8 l3 h: W) V% R' w1 j% Z: c1 J      z_counter += delta_steps.z;' N% |3 V' T) J$ F

2 j/ r. K6 P  _; J% `      if (z_counter > 0)9 G* |& R. Q% F# ~7 W
      {
; H2 U5 R- w5 R. T" m        do_step(Z_STEP_PIN);% _8 f; Q6 B) t1 G6 j. r
        z_counter -= max_delta;
) n, Y4 y8 h) g; M/ t$ M" W9 V5 S4 G% y4 R: k- G0 f
        if (z_direction)8 c8 H3 ]4 L4 L( b) w) {
        { current_steps.z++; z_pos++; }0 N5 P" h6 ]  t+ l  I
        
2 W8 @9 v8 R' W! i- x( U1 l        else
. M* n4 P% L$ @3 S* _        { current_steps.z--; z_pos--; }
; Y& o5 }' T* z; V        : @5 |' ^; r9 a' j; M
      }" d# J" w& x$ C$ [6 w
    }
* n# [  m: W  Y, s9 P   
! D3 c3 I& N% T( z' l2 l' U/ S    if (u_can_step)
8 P( w# P8 n' }    {
0 P% A& F0 W, a0 A9 u      u_counter += delta_steps.u;5 H4 W/ C0 v7 z9 s, A: q9 }6 m
' q6 b; c7 O8 d2 p+ a$ V6 j
      if (u_counter > 0)
" F( r  n% r$ f/ Q4 r      {& Q' o- Y2 t8 k/ w: J9 q
        do_step(U_STEP_PIN);
- ~& _2 e0 E8 ~7 q        u_counter -= max_delta;, {5 \/ \7 {: L& \* P. }, f
; ^: w9 d6 Q, y* a, u
        if (u_direction)
8 M% Z1 i1 U9 q2 t6 g: O: Z        { current_steps.u++; u_pos++; }
5 l) E0 @- f3 y+ @& p          - L* ?/ y  {0 v% L; O1 M( d
        else
! }% {8 W1 r- `8 {          { current_steps.u--; u_pos--; }
: r0 G1 J* O$ m# G5 \9 W         
/ a) Z; ^* g: |      }
; e/ {4 b" n) [    }
# o7 G- A5 Y1 y4 E+ g+ @    //wait for next step.* Y" n  p( D3 B$ |. x3 [/ G; `
    if (milli_delay > 0){) n$ [; P3 ^0 r! l
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();# P% H" v6 o0 e7 z; u
      delay(milli_delay);9 E% c8 x+ o1 S  h
    }                $ C8 R, g) y0 d, B
    else{
6 `! U1 Z5 b2 d      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
. @8 c0 t0 u  L( B$ Q4 e' k: H4 `" W      if(micro_delay>0)delayMicroseconds(micro_delay);
/ B$ F- l) E1 _% N0 ]    }
7 t3 X) d" y$ n3 {0 v    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
$ o6 g* \% y$ \2 H1 {" e  }
+ J# S( L+ z6 n2 _; p7 s( S3 o  while (x_can_step || y_can_step || z_can_step || u_can_step);, |/ q7 {6 _6 I
0 s4 [+ A# `" l1 D
$ [1 H' m8 Q, e( A7 K
  //set our points to be the same
( E' j. g: z; M  Y2 V8 a% W. j  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
2 Y0 d# U; z4 e$ H+ `5 ^- V. K  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;1 }/ F# d9 z2 l. z8 R. Q$ I' o
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
% ]5 ^' P" Y- a8 w( u  current_units.u = (float) u_pos / U_STEPS_PER_INCH;$ a3 R4 j0 i7 y' g  |* ~3 n# l
  ; T6 L+ j+ y4 k2 p& [  y
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );6 ]: P5 @; x$ h( }. I5 X3 R
2 ^* @2 R" C5 E
  long x_pos = 0; // x position in terms of absoloute motor stepps
2 ?! w2 [0 Y* R# t3 B  long y_pos = 0; // y position in terms of absoloute motor stepps
, s' \) F& c- I  Q" A  long z_pos = 0; // z position in terms of absoloute motor stepps
* q$ O! q) O; X  long u_pos = 0; // u position in terms of absoloute motor stepps* M% H* C! p+ {. M  R+ V4 z6 b
  calculate_deltas();: ]- u, g0 n/ ]3 P- @- V
  / H! v. r5 A- e2 o
}
9 P+ L. A2 v; F5 U' A1 f) Q4 R" G: s9 P/ I5 P: ]: m
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)) m0 p7 a& R# W. \& J5 r
{) ^% _" Q% G7 a6 s7 U2 n; e8 u
  //stop us if we're on target  C+ C9 |, F5 G3 P% r7 t: W
  if (target == current)
: p8 Q" K0 R: @: }+ b3 b/ z    return false;
$ ?# ]1 J1 {1 N  //stop us if we're at home and still going ! K6 |& |4 p& P% f# b& o
  else if (read_switch(min_pin) && !direction)
. t2 q+ [" B  B) v5 F3 t7 N: A    return false;3 H0 ?+ d. q) R3 F: n8 _$ x- X  H
  //stop us if we're at max and still going: z4 `7 ]8 X+ ?% e7 D1 D) j
  else if (read_switch(max_pin) && direction)
: Q" ~% a4 e0 W# \. M    return false;
0 ^1 Y0 P4 M% ?+ c3 l
+ l7 L7 D2 c- f" B) L  //default to being able to step
* e; k& N& ^* J# R  return true;" w! v  \7 y: [6 m8 ]' h; m! c% K
}( Q1 E+ A/ ?- n' G6 B
( U  [& Q/ f- [6 v8 H
void do_step(byte step_pin)
. a4 l/ Z- _: I5 S# N# o/ i1 M1 [{, Q4 d5 d8 z+ o0 x2 R
  digitalWrite(step_pin, HIGH);
7 V: @- T4 k; ~- p! n3 X3 j  //delayMicroseconds(1);- g- R6 H# q5 a
  digitalWrite(step_pin, LOW);' L4 n* i2 A, w- p' p
}
  y) ]& {- l3 f- V8 F) y8 j" m$ d9 z4 \/ t# Y) }  p
bool read_switch(byte pin). m; G( i+ p9 g
{
1 k( j! o- e8 R5 N4 |  //dual read as crude debounce
7 b# v8 n( h+ S  s5 {. u& O6 V8 I0 e  C* |7 i' Q. _+ T
  if ( SENSORS_INVERTING )
" r% A3 M. T( Z    return !digitalRead(pin) && !digitalRead(pin);/ G/ J) ]( b: B: |) t# r
  else1 ?3 c. b# a( P5 R& }1 U2 \! p
    return digitalRead(pin) && digitalRead(pin);3 l  A7 K, o, E  M( K/ u1 F
}/ m' r' Z6 M9 M9 y/ B# @" R; |

9 a1 [# C" S+ |( r. c" [. A( }long to_steps(float steps_per_unit, float units)
% n# X, V% M$ b{
" K0 h& @( I4 A# {" I9 A% E  return steps_per_unit * units;
5 j' f3 e; j) b8 M: Y- ^}
+ y* \6 K- C. f& E/ a. H! o+ q/ {$ O* _3 ^' n7 U
void set_target(float x, float y, float z, float u)$ R7 P% e; p" m3 M+ T( B3 ]
{1 y, w1 W) \7 Q( C* Z5 B# U
  target_units.x = x;, [7 R3 _6 [0 L; s: R
  target_units.y = y;3 @! q9 U7 U& t6 n, y1 l  a
  target_units.z = z;
, J" c! \. f# `1 M8 R  target_units.u = u;
$ S. ]8 {; ]" w' t* A! O  calculate_deltas();" r. @7 `' N& T3 _# |3 j& j
}, ?$ o1 ?; ?# p3 J6 u  s# Z

! r+ S0 _2 |' A) o# U2 T& M4 Mvoid set_position(float x, float y, float z, float u)8 }8 p4 Y2 q/ V! H& E
{8 b! J! A! E1 q
  current_units.x = x;" K6 }( R7 }# ?: c2 V  N/ W
  current_units.y = y;8 y7 {. R! T# S
  current_units.z = z;$ H1 q( k& @/ r
  current_units.u = u;
* z  g& O# o( C( J* E/ G$ W  calculate_deltas();
% U. O4 d# r3 X}
) b$ {! l! W( C, p' `9 ^
8 V, }7 x5 y+ ?0 c+ svoid calculate_deltas()
  V; v' J0 X, u{/ A2 Z6 j  r  [% k# F- D9 F/ S* M9 e  @
  //figure our deltas.4 l- u' l9 I6 q5 e/ {
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
  a- O, B0 S( F  G$ P' {5 [  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);
9 w# A' @) y2 @7 H0 e  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);( u& U5 h9 R. [& v
  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);- {; R9 Y/ l  `& Z, }
( Y# {3 m& s5 R$ s# }
  //set our steps current, target, and delta
1 g9 g+ w3 X" O1 O6 V  current_steps.x = to_steps(x_units, current_units.x);
6 {9 x: C7 H& r* \# M! ]  current_steps.y = to_steps(y_units, current_units.y);' K/ u! R& M  D8 w+ v2 D) U7 C6 j
  current_steps.z = to_steps(z_units, current_units.z);
4 J, @  M' r2 [$ e6 [  current_steps.u = to_steps(u_units, current_units.u);8 y" a* A6 g; R, Z0 w7 p0 F: e
. ~6 d" X. N9 N
  target_steps.x = to_steps(x_units, target_units.x);
5 d( j! {) ?; b8 W/ \7 J8 ]6 Y" K$ X  target_steps.y = to_steps(y_units, target_units.y);
+ ~* ~# B. z  e" K+ c- r  target_steps.z = to_steps(z_units, target_units.z);
$ c1 Z3 o% ~3 c7 r0 u- P7 T' N  target_steps.u = to_steps(u_units, target_units.u);
9 o; y4 u6 U9 @- Q1 H$ f1 d. ^- N; e
; V: R0 S# i3 e- l# C  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
, U" Y6 j: B9 D- j, T  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);1 y( M* g. O2 K0 y+ d
  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
& E5 x; i. F3 A9 D) J. q( C& }  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
1 I7 u) x3 g" h2 j( C$ W0 l; W  v" {) J, M' V  D! F+ U
! {4 z/ u4 i' q6 y$ V2 \" C
0 M% {" h$ Z! o. Q9 L- ]7 l8 h: m
  //what is our direction1 K# r0 }2 U& v( h1 b* `( i4 v
  x_direction = (target_units.x >= current_units.x);
( V1 ?3 d& n: g" E  y_direction = (target_units.y >= current_units.y);% W3 |% i. o: K' E# T3 q, j
  z_direction = (target_units.z >= current_units.z);
5 i/ g6 N$ e; Y7 w  O/ @8 @  u_direction = (target_units.u >= current_units.u);
+ Z! x( G1 {& m. K$ N1 I
9 m$ B; D# `6 p& U+ n. C8 K3 {  //set our direction pins as well
1 j$ z. ?: k, M1 H7 X  digitalWrite(X_DIR_PIN,x_direction);
0 O! e$ p1 U" M" K  digitalWrite(Y_DIR_PIN,y_direction);
; t* o7 b3 p4 o! c6 K  digitalWrite(Z_DIR_PIN,z_direction);3 S! p. R& L+ u& N1 l: c
  digitalWrite(U_DIR_PIN,u_direction);
* i6 R8 ~9 s  n- }
- f& l9 R" G% \4 v  D( H* a; o3 M; L  //繪制LCD+ j$ h5 \* [( c5 e! |
  LCD_DRAW();
7 W+ D) c( N5 B  [9 U6 z3 U4 V7 J  V2 k! a! p7 N* D
}
1 n' B" j5 a  f4 {) p8 e$ Q" h2 R
  |6 w0 t% d* Z$ q* E' Z6 ~0 U
long calculate_feedrate_delay(float feedrate)) J+ y  z( g6 o
{4 A9 ]4 A% O6 J  V# T: {8 V# \, |0 P# e3 P
  //how long is our line length?5 [9 j/ ^/ V6 ~5 N: @9 ?
  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);
9 r+ ^2 s3 K  N; \# C  long master_steps = 0;5 U1 x/ b0 G  }, N# N

2 C( P8 m# `0 _) Jmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;7 U, G& t" A8 C, M- B
master_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;% e  ~- k& ^5 A. C) G
master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
9 D9 U' F* b8 C6 ^& W( i; \  E1 s/ O* V- J. j6 e

2 U& C' x: D+ P5 I4 x+ P% S/ W* V; Y3 u( N2 F. I9 Z; O
: i( E. s6 H9 h- D9 }* M( u8 z1 |
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.% H: ]5 P* V, N& }0 Z" N# ]6 j
  //the formula has been condensed to save space.  here it is in english:: b" H0 a2 z  A7 x
  // distance / feedrate * 60000000.0 = move duration in microseconds
% i  ]* Q  R/ x" d  // move duration / master_steps = time between steps for master axis.
3 A; j; q) U" c/ C return ((distance * 6000000.0) / feedrate) / master_steps;
4 g0 C/ n7 O& o" V( d" T( N4 I+ H7 p1 X( ?0 M
}  O4 l# I2 z: \# _! l

8 T' E  F4 E& W; w2 ?3 n- z( Mlong getMaxSpeed()
- u" U3 s$ R. B5 j: [  {$ t{
* A. H4 y/ Y6 D/ q) a if (delta_steps.z > 0 || delta_steps.u > 0 )% m! h- ^. }- j. N
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);/ Z/ p. l. V6 O' s: a
  else! n9 U: e' `" @- f
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);" U/ w7 Y7 U  x6 f$ u
}
" l3 X* q; }3 c
2 J1 Y2 U+ C+ z! a8 Dvoid disable_steppers()
7 G  Y1 e" S/ V1 y! E5 B/ [9 x& A{
: S4 `; r) L# o( u) H0 b& P  //enable our steppers# `8 T: j7 W0 ]) }  v5 G+ c
  digitalWrite(X_ENABLE_PIN, LOW);) _/ t4 ]6 x4 g0 g8 k
  digitalWrite(Y_ENABLE_PIN, LOW);  `0 }4 M/ Q' a4 q
  digitalWrite(Z_ENABLE_PIN, LOW);
; O. u0 U7 |/ E& h; z8 \' t3 Z, g. \! a  digitalWrite(U_ENABLE_PIN, LOW);. W+ U0 p( x  ^8 k$ c$ r
}* `) W/ M, u5 c! t$ J2 @0 C
& D) Y+ d) D8 S) E2 s
: u& r+ h7 _( U, y
//繪制LCD7 N! o- j2 A8 T' I, A2 T1 r
//unsigned int DRAWCount=0;
9 m) u& x6 v# X4 ?$ @5 xvoid LCD_DRAW()
7 P+ `) p$ k# L: F7 ~9 E{
0 S6 c% k- \/ d2 i' g& f    lcd.clear(); , `( M' e! a. g6 H7 N; R
    lcd.setCursor(0, 0);
* N# ]' }+ s& \9 A" r( m* i, c' R    lcd.print("    X=");
- K; {9 Q! I( H& t+ L! }1 W9 J    lcd.print(current_units.x);
% m, ?8 F3 U) `$ C4 U2 W    lcd.setCursor(0, 1);$ e& L8 L" v; n) |8 U4 W) H/ ~. S" M1 E
    lcd.print("    Y=");( x. \  L( m& a" z/ M$ [3 P$ K
    lcd.print(current_units.y);
5 a: m5 e/ T5 D    lcd.setCursor(0, 2);
' X% E5 a( e, L    lcd.print("Z=");
9 D! X: I0 Q3 [2 c- G    lcd.print(current_units.z);2 j; }/ C( c6 B0 E2 \7 M- E- d" l
    lcd.setCursor(0, 3);  i2 R: P  m  i0 l* H' c' c
    lcd.print("U=");- C5 s5 y8 y6 f7 l( j, @
    lcd.print(current_units.u);
" {( H+ i# C" b- }  }1 O7 ^$ ?5 {3 z8 [8 l/ J) W
// else if (DRAWCount>=30)DRAWCount=0;* v7 [' d" C  r, N
//  else DRAWCount++;4 Z( Q/ C+ ~+ W4 b  a3 i
void SPEEN()9 W8 W8 s# }: Z$ t" [6 \) z8 d4 Q3 ~
{
. d% o4 F0 Q7 s9 @( z0 Q5 b   delayMicroseconds(analogRead(SPEEN_CTL)+1);
% }7 ^/ h) \8 Q0 c) X }
6 f6 E" U: c0 W) U
# {. G* {; `  E: l5 X4 S8 ]0 T1 `5 k/ i1 n2 _
//delayMicroseconds(analogRead(SPEEN_CTL)+1);
: r5 ^+ |, O4 A5 w //if (digitalRead(BUTTON_SP_EN)) SPEEN();
7 M7 C) i* _; A" F9 e8 ~! ?5 K4 P9 ?5 G" m, \  ^/ z% \/ {: a
* L4 V# J4 S. P4 |, ^
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-7-26 15:29 , Processed in 0.075631 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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