找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 24291|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑 5 B3 @5 \5 ]- t* M7 j

) f: H3 u# q4 ^" @8 p; m' `2 _; w$ g! t/ k首先声明. W7 Z3 b& Q0 e& `( H
我不是专家,业余捣鼓& G' K8 P7 B( D0 ^
源代码首先来自网络0 W9 g, M! n  F8 R+ y7 T
开发平台Arduino及Maple
! f: u- W! @8 |. yhttp://www.arduino.cc/$ ^* w8 m0 S- \/ T* M
http://leaflabs.com/devices/
( u" O9 m0 E; }! A: v$ d国内活跃社区5 O2 J- l5 f6 ?
http://www.geek-workshop.com/forum.php $ t3 H) j" K$ F$ m% q  N

2 l* a! Z! f, \+ P竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验% M/ n: k% X5 v' x4 ?
来入门了解熟悉思路架构等
6 i' F' w% m( W% p* j5 f; ^) D4 u6 z0 Z+ o4 {3 d
我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,- \4 q+ X% z# U9 U+ b
许多功能还木完成,不过作为低档次得应用可能还有可能4 F; B3 {2 i7 z" K% w

- U8 {4 r% {! |! O7 K& h  k) ~我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧
: b, Z6 k/ T4 H. \. h0 f6 z( L' l6 `
拒绝 所有的求
1 n0 o( F5 @: [; u4 _求人不如求自己 不然木玩% ^3 R" a; y9 b8 G5 Y

: j2 z5 a5 b2 a- M2 `# z0 b% B: o/ ~2 W高手绕道 谢谢!
' e7 T7 c2 N, l) c4 [
2 I* m) d! k' V: F$ ]1 K" B
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 : J. k( ^* M# D6 }

7 n; m- q0 b& ]' _2 N  v- |GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东6 \# A% ]. N% @  S
贴代码先
8 M. q5 p- m6 {直接Maple的,某宝许多超便宜哈8 D( k1 G( C; ~
晕,我怎么上不了RAR?
$ F" R- n' b# A: u7 \9 {想玩的留下 e妹吧; O$ s2 u+ L( M/ O% w$ W
第一个妹麻烦传第二个妹哈
0 r3 d" O$ g  B7 E# W; ^4 b3 Q我平常杂事多# @( \& @! g$ I! A. @0 V; N, w4 H
谁放网盘上再,麻烦开放下
 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap  C! M4 b9 [. A
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)' @, m, d) X5 K! Q# a7 f
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)) d7 T+ ^( c4 X; q, g: e
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
/ A0 O+ v* D5 D7 T: P: o// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
; C; v! V+ N  {/ y$ ]! k1 e+ g  P+ }3 p" J5 w
// Arduino G-code Interpreter for Macro / Micro photography
* v" A5 l6 l$ g6 f. F// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
. v2 F5 O3 b1 i" L3 W) `//modified by YaoHan China 2010.2.159 w& {0 V, d; f4 S( P
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
7 n% x& H2 r/ H3 }- h#include <stdlib.h>
1 j4 T0 A3 H! q; |- J; l#include <LiquidCrystal.h>
# H- X$ R4 L4 t
5 z3 ?0 q$ i7 z0 d2 R* [  [//启动(高电平有效)/暂停(低电平有效)  默认高电平4 Z3 K! @- `/ e7 z2 y. `0 I" @% B
#define BUTTON_CTL 35* z3 H) r2 R' r3 w( V% v- S
8 n- z  S# K. E1 m1 C
//点动模式键(高电平有效)  默认低电平
  a6 Q* \9 h' x; ~#define BUTTON_SS 36
! |$ C* I! [" Z7 E( i8 `- ?0 M
( m9 J0 _, j: s6 n# T4 k//点动键
' z) b# ^+ n2 j) P% b2 k#define BUTTON_MAN 151 Q6 j7 s; q: L% D

! i' z5 U' a) b) |8 l//电位器速度控制
# J# i* R# d5 f#define SPEEN_CTL 16" X, x- C: L0 ?5 _6 R5 L' Y
0 V. |0 C/ M2 l7 [- N
//手动调速使能
4 B/ ~3 z0 ^* _$ {#define BUTTON_SP_EN 17) D( y. E3 o4 v- P, A

1 @( c3 b6 O/ p; a) N//LCD 类型 1604或1602. d$ C+ I+ U) d* J1 H: ?  f( ?
#define LCD_TYPE 1604
4 Q2 S8 l8 p" S/ S3 R: i//LCD 引脚定义
5 c- o/ W. x3 n' T0 J7 E#define LCD_RS 233 {/ D; }) s; n# Y! f! w
#define LCD_EN 24% ]9 d! e3 O7 R6 R; I% l
#define LCD_D4 25& \/ \' F1 `( m
#define LCD_D5 26
- C* j! X! @% u' U#define LCD_D6 27
! p1 T0 d% V1 m7 B3 b( A7 U#define LCD_D7 28
+ y9 I8 t* Q' z; k$ \; K4 y) G
2 Z8 P' _% a% v# y//命令字符串
5 X8 N4 @+ w6 f3 v#define COMMAND_SIZE 1288 x5 d! Z5 l4 `& a& s& g
char word_old[COMMAND_SIZE];; C  w1 a7 x- M
byte serial_count=0;
  v( D8 }$ Q1 t. _. Oint no_data = 0;$ D! y$ J% ]3 P2 Z7 V/ u
//LCD 引脚链接 配置
+ H6 W2 ?9 O" b$ @( HLiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
1 E8 T3 Y+ n3 `# q; R* G( V//停止控制,高电平有效
' j4 X( `  R& P9 c: x! J' i( H#define BUTTON_STOP 30
' S+ ]! Y4 @% C# O+ W$ Mint stop_flag=0;; P; A+ c4 v6 y
; Q$ j- n. z: F8 d& W  e
//暂停1 @& y! K5 n1 T6 n& u8 l! B
void pause(){6 q! y) F6 r7 o
  while(!digitalRead(BUTTON_CTL));3 j. m* |( f* {- O
}
) L5 ~& V" g* Q
6 o+ Q  q' ?4 J( Y! b5 v2 M/ M6 Lvoid stopper(){
) O, ?) l7 t$ c( {  T  delayMicroseconds(10);
3 B6 A& J* S7 H. \+ K* O8 f# X2 f  l  stop_flag = digitalRead(BUTTON_STOP);9 g" o; }3 a' K
}
1 t* W* q3 J& t$ K- ]
7 i( D% [: Q! hvoid setup()
9 e9 h! c# Q/ z/ I' h' D; q0 {4 H{
7 |, x3 v* b7 f) I" E. ^1 j  //show start
1 P/ T; ]. [4 k0 {+ d' c6 Q5 `  SerialUSB.println("start");7 r8 [  K; ^& B

, y) ^3 A) ^3 f: Q( u  //启动lcd
! O9 }! y  N* e' |    lcd.begin(16,4);4 R1 r1 ?6 X/ s; E; H
    lcd.setCursor(0, 0);* z: H0 J' t+ l' k8 W! O2 a8 o
    lcd.print("hello, world!");: \% R( H4 U/ N! n

( N+ Y9 r1 P4 ?9 ~1 `! p  F  //初始化控制引脚及引脚功能% R/ B  I3 i+ a( `7 x5 r
  pinMode(BUTTON_CTL,INPUT_PULLUP);8 ?5 z* b0 P. _0 ]  w4 K* t
  pinMode(BUTTON_SS,INPUT_PULLDOWN);5 I1 B3 W' `6 C8 u! \: B) z4 S  F7 H
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
  P7 r% n( m. t* Z) L; x; r  pinMode(BUTTON_MAN,INPUT_ANALOG);6 R% `- A; i: v6 m  @

; F' x# J7 Z. O1 \  //控制引脚的中断
( E2 ~: ^* N! E1 B. k5 _, a  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断
" R5 v) B# D2 g9 }- `; r  attachInterrupt(BUTTON_STOP,stopper,CHANGE);( |/ H, I- m6 E

7 r7 o, m( T! [  //other init. a" d. v- {. [- N* G  E
  init_process_string();0 f9 r3 y0 S# y4 r. x+ E& U  `" v2 A
  init_steppers();
3 p* H* [, \% E% Q" p0 n8 U: D" L+ f  init_camera();9 [0 _! o' t* o: i

5 |. A0 b& C. C7 ?}
+ k* I: i1 b  f7 s( k* j3 X% [, q+ v9 o0 D- l/ W
void loop()
2 }' @) o% }* A5 n6 b{
7 Q( D5 p% w9 n& _6 m* Y- M5 N1 b  char c;7 L: K! o4 W3 I, M: f0 d/ H& a) N! {/ P
* @  W: l3 V+ l  T$ y5 w5 h
  //读取输入的字符1 D$ Y/ }) C' p
  if ((SerialUSB.available() > 0) && (!stop_flag))
0 k" V& ~+ J$ W) R- D  {
  j  I- Q* A. Z2 K    c = SerialUSB.read();
) x1 w9 T; ~  R& s# c    no_data = 0;
4 Y7 i) k9 X0 J! t
1 L7 W# W' c8 ^$ S    //换行符代表一个命令的结束
) D5 K7 r% K1 E+ m2 s% C    if (c != '\n')
! M  {" \9 v. S" k3 D, a: \    {
5 ~* I9 O0 n5 J7 q      word_old[serial_count] = c;4 u  u2 K2 x0 C6 `0 v9 M
      serial_count++;
7 n1 Q. j$ D: V: U  H, ?1 B3 H4 ^) L
. {0 U$ _3 T/ {: _2 X3 ?/ Z    }
' P" U/ X$ N) W# ]# C  }2 N3 I) |/ I5 M& k% L! T
  //标记没有数据输入! j! k5 `  {* P2 H3 {$ e
  else0 a- F4 O+ A! j
  {
, g! k" w; ?% E5 ^/ q    no_data++;
+ j' F7 R6 F3 w' X; {; A' r3 ?    delayMicroseconds(100);
" Q) X1 A* K- `) i; q  }
6 S  C( s  w" p# N3 b3 }& H/ l. p) _6 d) T4 `
  //if theres a pause or we got a real command, do it" H+ Q4 t5 d3 R, f6 H; {+ C
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))
8 s- r% e  ~* \" g3 D  {' c% [# M" @$ r
1 H& r* L3 L4 `8 t
    //处理命令
& y# L1 S* z" Q2 l! X    process_string(word_old, serial_count);
6 Z* N; G3 V4 M! G& Z- g- w  _6 h
3 }) ?* U* p, h' R9 d1 k- E    //清除命令9 {; l; R' {# D/ Y! z& A
    init_process_string();* S0 t# T  c/ h
  }
+ y6 _1 k5 x) [
$ A2 s6 c; u7 E2 C- P. {  //如果没有数据关闭电机
, E2 \7 F; k! A: |1 O  if (no_data > 1000)- ^4 A9 c$ i! m0 l) }6 O9 I. H. Z
    disable_steppers();
! h0 P, H, y! P: Y2 E- N: T8 `4 D4 v" b( f9 p" x* d
  //如果ss键按下进入点动模式
* i" j' i- F! n  g4 Y8 _2 s! q  //if(digitalRead(BUTTON_SS)) ss();
9 V" J" P  h5 n- z8 h  run_a();. [1 |$ R; F7 i+ G8 R4 M( N
}* R5 W0 C" _7 ~+ Y" c
9 d! c  _- [' L8 k3 b
//点动模式
) E: d, y* q; C4 B7 @9 [8 Svoid ss(){* }2 j6 q1 O  ?) b
7 U( A& r) m  X9 [
  delay(1);9 b* ]( o  L- I; W$ {
  if(!digitalRead(BUTTON_SS))return;
% j. @7 x2 n9 ~: Z- u  if(!digitalRead(BUTTON_SS))return;
7 l; V2 F5 u: M# R0 C( @7 r  //init_process_string();
6 ?; N; b6 e/ n+ z8 u9 ]. J  //init_steppers();
' b/ ~7 D( m' h" Z5 ^. C; g  //init_camera();
5 M; o9 @0 C% W. A  N8 K
' p2 b' G6 T; D8 F7 V0 t' v8 I // SerialUSB.println("Step By Step Mode");. h4 ~3 A- k% `: @4 j* m0 T. N
//#if (LCD_TYPE == 1604 )
7 w2 A" o' w, K  //当LCD为1604时显示要处理的命令& w; A" e- d4 U- b( \/ h" ?
   lcd.setCursor(0, 0);
& O$ Y. N( n2 O9 i* N1 d   lcd.print("Step By Step Mode");
- w- d2 @. p0 k% Z//#endif
- K  {3 \1 e6 z: h0 t- H/ {5 u  process_string("G1 F5000",8);7 M: S& \# @+ b
  process_string("G91",8);
) [8 d+ o& u2 Y( _) p, ?  //init_process_string();7 q  X! Z+ Z. t( ?3 r4 l
7 ~$ |! ^9 M6 V3 u# p+ i
  while(digitalRead(BUTTON_SS)){
" V# e1 c0 e4 g0 x, L$ l7 k0 Z9 ?    int i=0;- M9 q  u0 ~: c6 w7 j: }& d8 T  @
    i=analogRead(BUTTON_MAN)>>9;
% u# |% B( N& V) L" c+ j; d9 ?    //if (i==0){break;}6 k( O; \% P) ^0 M  {
   //SerialUSB.println(i);7 _6 F9 K: _) W: q5 E8 \
   //delay(1000);   ! E) i0 J* W$ d( h) s5 G+ P" C

4 H% Q' W* Y5 P: o. D. X   if(i==2){" s, `4 X  `/ g. p( }
      set_target(1000.0, 0.0, 0.0, 0.0);
- V0 a$ \" U3 D, K      dda_move(getMaxSpeed());
) u0 G  G1 q' `3 y+ D      if(stop_flag) return;
3 q9 ~' z$ X, ~8 s2 L: I6 g* P0 o      //process_string("X0.01",5);' ^  K2 q7 i9 v7 t5 G0 y
      //init_process_string();
8 G7 M5 t9 |+ u    }
7 f& z6 F( _2 E& x. `    else
2 z- k' Y4 o9 T9 H$ j6 _" l" B    if(i==5){3 V" j& W# ~: ]% C; t2 h
      set_target(-1000.0, 0.0, 0.0, 0.0);: G- T  W0 @* H6 u
      dda_move(getMaxSpeed());) V4 O' w& P6 R# [7 k9 D, a7 k  P. `" L
      if(stop_flag) return;
% Z; g7 o) O. x      //process_string("X-0.01",6);  w$ h# l9 x$ s/ h4 z# w
      //init_process_string();
7 L- m2 G4 D) W: Q! L8 O    }
; A: b8 |/ Y7 h% U& ]! n: Q: n' Z     
4 m* G, n) U# ~- X# |% w: c' g' _
  }
( @) W' t$ d6 b* \# m/ C$ x" [' ]9 p: M- e! v( o' W
  //init_steppers();" x! O; h9 c% M, ~
  //init_camera();* t* E& F) W: S  M
   // SerialUSB.println("Return To Normal Mode");4 q3 Z' l) I( X4 I0 p6 h9 d# L' _8 ?
     process_string("G1",8);
- A# Z6 Z2 A  |/ @  // process_string("G91",8);8 o2 S7 U/ P! k' L4 X' P! h/ C! _
    init_process_string();; g: C/ E& }, i# g$ x% V  X" g/ S
//#if (LCD_TYPE == 1604 )  J; u: Y  ?- u" m+ y& Q; }( A/ X
  //当LCD为1604时显示要处理的命令
4 @- }" A( L& d% Y: T' K) z. y$ k  // lcd.setCursor(0, 4);
& u5 k) T2 c7 J0 y- x7 ]% X  s  // lcd.print("Return To Normal Mode");
* y1 y% l, m' @7 T4 F//#endif
" U! n; b8 m5 ?( L}
! u  X9 Z; q" F  \! B# O* y" R; [+ Y& o: B- }& B4 H
void run_a()0 y, a* V* a$ Q$ c, d4 b
{
6 \1 W  c* O! d  //delay(1);$ @8 Q, s$ m6 y; T7 ^1 N
  //if(digitalRead(14)== HIGH)return;
5 R6 Y7 ~: X4 N; s* C$ F' A  //if(digitalRead(14)== HIGH)return;
9 v/ N, B, M/ Y9 F( G  //process_string("G1 F2000",8);5 O5 F- w4 u0 L. d
//process_string("G92",8);
* d" u3 J3 A* t process_string("G90",8);
" Q, e. ]. P7 b9 }5 L process_string("X120 F10",5);
+ l' C6 t0 L' F- }9 Z) F  p- \3 v% } process_string("G4P2000",8);
8 D: T" Q2 Z% m, ^. W1 _  `! {  M7 B% o$ I process_string("X10 F10",5);
' F/ G  Q/ s8 k; Q* z// process_string("M101",8);( V+ j: s, l. U& L, A; G
// process_string("X-50",5);
) n- E. K' H9 o% W- f( u1 t dda_move(getMaxSpeed());( x' v" O( Y& T1 R  n2 z# k
init_process_string();$ K" t1 r& r$ J$ ?6 b3 }" }; M
5 A! o. u9 }, M7 V8 C
}0 w+ `- m% t, X7 H8 ]5 r# {4 w
 楼主| 发表于 2014-5-10 09:53:25 | 显示全部楼层

4 w3 b4 j* q* X$ s3 \" x, Y! d; y
8 t5 _5 S; @' u& d) g9 ~% U// 定义机器参数. F3 i, p  v. u+ }" H
#define X_STEPS_PER_INCH 400
7 m* b8 T$ L; N' X( c- g0 C" g( z#define X_STEPS_PER_MM   16.0
% X; K6 e- A7 `% O' ]# S#define X_MOTOR_STEPS    200
- p: y' n- R- l( G: Z' Y: I3 U5 Y2 @) \" D& U1 w! }' ]
#define Y_STEPS_PER_INCH 400.0
' q3 W, @" T$ f+ H#define Y_STEPS_PER_MM   16.0) v7 I3 e1 u5 U( U+ @
#define Y_MOTOR_STEPS    200
# @: E( Q+ G4 N6 p* A
; M5 k, Q- M  ]3 W3 Z1 @#define Z_STEPS_PER_INCH 400.0
# w# O/ D( U3 H/ u$ |: |- [# C#define Z_STEPS_PER_MM   16.0) x: f, d6 ]4 h$ k3 N
#define Z_MOTOR_STEPS    200
; g- I" ^2 f. D" X& M7 I/ ~, `: G7 P. d0 c6 i! z, K' T- b1 Q8 H& A
#define U_STEPS_PER_INCH 400.05 J& g8 e* g7 d& c
#define U_STEPS_PER_MM   16.0
& e2 y' c6 H; h; v4 n#define U_MOTOR_STEPS    200
; Q3 a- Z1 t1 u  \. S+ g3 ?; f+ Y, u& v2 Y7 ~0 F
//最大进给率
6 n! g5 _, e" C. t% Z! }#define FAST_XY_FEEDRATE 1500.0
, b7 t! L7 X0 V8 d4 g7 p#define FAST_Z_FEEDRATE  1500.0
! _+ ]% U* K  c#define FAST_U_FEEDRATE  1500.0
# a" @$ U" H! t+ S1 {// Units in curve section
; v6 Y6 S2 |9 v" x#define CURVE_SECTION_INCHES 0.019685
; Y( a, B$ o, |8 \& |( V% \#define CURVE_SECTION_MM 0.5
- F) c, r3 [* V  p6 E7 e
2 Q8 m3 k  g; k* @) \; [
: G. L  k1 l6 x+ P8 S// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
! p; B7 H: z  Y  V' T  W// RepRap opto endstops are *not* inverting.
! C' o7 v0 G- t8 a: P4 y#define SENSORS_INVERTING 1: b, s# f7 y( L4 l  F
8 n- O" l# ~% C. _
/****************************************************************************************, n- M% i# i; n
* digital i/o pin assignment
8 A( |$ p1 L$ t5 k% r4 |' r% x *2 f2 [. Z* [6 d7 u2 ~3 T* s% Q
* this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5
& Q2 ^7 p, N+ ] ****************************************************************************************/
" ?/ h: q: M( i( F0 i% N1 ^
: H" R, m8 |# a, |//camera shutter and control pins( I9 s/ O  g7 @" g+ R! D1 t0 f
#define CAM_SHUTTER_PIN1 29  O0 l. P* S, w; m" N! ~0 l5 H9 h
#define CAM_SHUTTER_PIN2 30# {1 i. |5 I. }9 ~1 G& f$ ]8 U0 z
//#define CAM_AUX_PIN1 31 // analog 01 [, O. P% h1 u- E5 W& l* z
//#define CAM_AUX_PIN2 32 // analog 1* H2 k1 A1 A+ _* R8 j
//#define CAM_AUX_PIN3 33 // analog 2, ]5 N: I+ w6 l0 S
//#define CAM_AUX_PIN4 34 // analog 3
8 M; J% B" Q2 C4 {; ~0 H) ]: X1 r2 T" ]
// stepper driver pins' K; ]2 x$ D3 w+ T) h3 [
#define X_STEP_PIN 7
& O, i+ O, a: S/ K. O: |, ]#define X_DIR_PIN 8/ C/ d9 b  E. r  V8 m$ K
#define X_ENABLE_PIN 195 k; M& A, ?- B, [
9 I+ `  S% T4 ~( ]
#define Y_STEP_PIN 97 c3 X+ p8 H% |0 `5 @: O
#define Y_DIR_PIN 10
  b- H. c4 W  o5 s8 d' B#define Y_ENABLE_PIN 19
1 Q; B7 ?! F0 e
' o4 o- V; P! z. W! U" ^6 o; R7 ]#define Z_STEP_PIN 11
  @/ J$ v" o3 B4 E% G#define Z_DIR_PIN 12
2 _, b  G8 e3 ~#define Z_ENABLE_PIN 19( u. r9 O! D8 [" M% s
0 D8 T; V! a$ c( u! F% i
#define U_STEP_PIN 13
4 `' _. b& Y* s& _9 X# W/ s7 S#define U_DIR_PIN 14  j  H2 C$ }5 O
#define U_ENABLE_PIN 19; E& ?6 m% F- P8 J! j: h0 g, H
* J5 g" D7 P, j8 E. b: ~
// limits not used right now/ f2 O6 U# ^' G+ R$ f! u( ]3 M
#define X_MIN_PIN 14
% t: U  l4 x( v5 {# K& _* S& b#define X_MAX_PIN 14
# M2 }- t& D. x9 ?) p, O#define Y_MIN_PIN 14
0 f3 h5 A0 P6 X; @" ?( D#define Y_MAX_PIN 14' C1 I! Z) O4 T) q' `  I
#define Z_MIN_PIN 14
* g  e/ y5 {: a+ l1 x- X9 O8 I#define Z_MAX_PIN 14
6 I$ Y$ N& c  C0 C#define U_MIN_PIN 14
1 m# ~2 r. I( j- [7 X" p#define U_MAX_PIN 144 f  K' K" J, ^2 G  z5 P0 T" A
 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()  s. [6 b+ X: p4 i# V+ \% g; i
{2 u  N  a) T7 i0 Q
  pinMode(CAM_SHUTTER_PIN1, OUTPUT);- L6 B1 i- i5 Y$ d7 m; W  b/ Q, B
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
5 l9 m' W: q. N  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
4 L% Y+ S# Z. I' k6 P% d. |/ o // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
$ K& |7 l; r7 v! I$ J  T // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2
8 Y( U6 d" L( S // pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3  w* M- c3 `/ ]" `+ i1 q0 P
}' j2 E( k2 r8 h! E/ A, D* |! n
. n. R; r, X" }
void camera_shutter1()
+ E# _5 U- |" U2 v% U" x" N{
9 _$ l! D9 Z9 [( w  // fire the camera shutter via relay...1/4 sec hold time
3 N3 B) U6 L! L- j  digitalWrite(CAM_SHUTTER_PIN1, HIGH);. H) ?4 R1 b" e
  delay(250);
0 Q, d  F, _3 `% q5 p  digitalWrite(CAM_SHUTTER_PIN1, LOW);
: m/ u7 W: p' w+ n% \+ {! ]: _2 g( z+ q
}
8 i+ u- L4 G2 f/ k- C- B$ p5 C
( i1 D- |+ K7 jvoid camera_shutter2()+ s. e( A8 r. r
{
: O7 T4 {- d2 i/ [1 M  // fire the camera shutter via relay...1/4 sec hold time6 w2 s) ?! p+ {# S6 y
  digitalWrite(CAM_SHUTTER_PIN2, HIGH);  F" N; \  W$ T- Y1 G
  delay(250);
$ A4 F  z) s' @  digitalWrite(CAM_SHUTTER_PIN2, LOW);0 e- K3 K0 X3 H2 Z/ J

& W! T9 l9 U$ k2 a}
- t0 t' V% c) R+ r9 ^/*
; a( p& ~$ i3 Y0 W9 q5 ?; f+ Yvoid camera_aux1_on()
1 Q5 W2 u3 C! S/ }8 a7 U{* w) p( U  G. C/ e9 C) m) d
  // turn aux relay 1 on
" M$ ^1 G1 |# {9 L; Z% H- `  digitalWrite(CAM_AUX_PIN1, HIGH);- ]% H/ x/ B6 E: u: T" x
}4 e3 H, y+ o3 M3 C& N( J+ h% s
& L7 N+ Z* Z) S2 v8 ~+ H
void camera_aux1_off()6 R, F4 ]2 Q* K8 K" R
{! f5 ~: ~& L! b0 f  p
  // turn aux relay 1 off
$ x1 q& |. B4 k1 O+ w  digitalWrite(CAM_AUX_PIN1, LOW);8 I5 q- U- ?/ V/ y! H
}
2 |7 X6 X. ?2 q$ b2 O
  P8 ]& J8 g% i: [7 gvoid camera_aux2_on()
* f3 `3 N3 E! X8 G{8 a. `* K5 y- g1 f9 I/ t
  // turn aux relay 2 on" L1 }0 C, v: [1 k& a9 d; b9 F
  digitalWrite(CAM_AUX_PIN2, HIGH);
6 u, Q1 W# c: P* P5 D}6 a0 B0 \. q$ u; `
4 M) r% l, ~7 _. M
void camera_aux2_off()
3 a+ \% ]. V/ E9 e( _{8 s# ?, p' \0 E- ^8 r
  // turn aux relay 2 off
7 L9 ]6 c! M5 Y, s( T. q! d  digitalWrite(CAM_AUX_PIN2, LOW);
5 o; H2 M" r0 I! I: r( N}
% v# i+ p" h4 R2 w9 U) B
+ ^2 D" {" l( ]6 h8 U8 pvoid camera_aux3_on()
$ m  C2 g" n; U% V{
' h9 d: y- @# T" t8 M8 n  // turn aux relay 3 on7 o$ V- P2 E% A
  digitalWrite(CAM_AUX_PIN3, HIGH);* d3 v( y+ a/ m" D
}
8 E7 C( s: u& ~2 n3 @$ E
! ]. B7 K2 V+ dvoid camera_aux3_off()' }, ~7 o1 [0 M' n
{; S7 m5 d( a3 t7 B# u0 I1 ~) k- i
  // turn aux relay 3 off/ P) I/ @  p1 t  O; w: B8 {
  digitalWrite(CAM_AUX_PIN3, LOW);8 j& {, r$ D3 _3 D
}, |. [' M' ?3 a3 O- T( s# S2 j
5 `, _) _. ]: G
void camera_aux4_on()) }' ~6 J2 B# b% E; x
{2 q, q/ K% z# V/ S
  // turn aux relay 4 on2 F! u" j6 L8 Y( M+ e% ^
  digitalWrite(CAM_AUX_PIN4, HIGH);9 x6 I7 E; s9 V% U" ~% Q
}9 ?, ?+ R& I7 W: T5 X( @6 K
+ G* a6 ~& e9 Q. X2 \* [
void camera_aux4_off()
# ^9 R+ t3 H3 B3 N7 ~; }$ E1 N{) [; `+ ^0 ]" y  ?! B
  // turn aux relay 4 off) W: G. F  ^1 T' ^9 R" D
  digitalWrite(CAM_AUX_PIN4, LOW);# j7 g7 p5 ]& j" ?$ E2 L5 K
}
发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错$ F  m+ L/ j8 O; g0 Z/ J" a
请问楼主是玩什么的,用乐高玩具吗?
 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice.4 G+ a: Q; q9 ]! r& i7 E
struct LongPoint {! |7 f6 L! c# X. B( X( J
  long x;
! z* F$ k# o, v- N7 U- w/ v" ^  long y;- a8 i3 c" _* v# m! b+ v4 O4 q' v
  long z;, o& @' ^& p# ?, w9 D
  long u;
7 n3 d$ A% v* u3 }% n) t};) }) y# X! k8 A8 m; Z, v: e

% i2 i  |. \% ~% u: H; Tstruct FloatPoint {4 r( o& ]) k% E: }8 o
  float x;
  R& g$ H5 i2 ^$ n) _! g  float y;6 r3 k4 V/ Q& r: S  L$ L) S
  float z;
' I" o+ Q( ~& x, C0 H  float u;" w( D/ `  N8 M! F# S$ ^
};* t% p0 l+ k" R3 a" {
. a2 m7 i; {! I
FloatPoint current_units;. ]$ `( J# r! r% a2 i) k
FloatPoint target_units;; D- M, `& f0 V7 N" |& i% v
FloatPoint delta_units;
$ }1 _1 N# b/ x) v* ~- K
% E4 j# I5 l9 u  I5 N$ z. r! lFloatPoint current_steps;
; ^8 C+ f/ S! RFloatPoint target_steps;
' Q! J& y) |2 p; d0 j& R& YFloatPoint delta_steps;
* V# [6 r( M1 f6 m7 @2 T" M
5 m* O, \) A7 K+ i+ U) j8 rboolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式4 W# N( O2 |1 a/ g
! Y7 c$ i# |2 |7 S8 r5 }
//default to inches for units
  {0 x# B8 `$ k9 q9 g4 Mfloat x_units = X_STEPS_PER_INCH;# m( K: U) L6 B
float y_units = Y_STEPS_PER_INCH;
: w2 ]  v% U7 e) zfloat z_units = Z_STEPS_PER_INCH;: W, _, L3 G: {! y2 T3 @3 g2 O
float u_units = U_STEPS_PER_INCH;
: C) p5 k  v* g& V  R9 Rfloat curve_section = CURVE_SECTION_INCHES;8 x4 \9 L# N  n$ g
9 w+ M. F( C3 L5 h/ t, P& N
//our direction vars
8 P  |6 \0 u! F6 ]; I# C' Lbyte x_direction = 1;& [, f: e3 c7 g0 A6 q3 B6 Y
byte y_direction = 1;
- P) R7 }# `1 X5 L, l( C  M8 Tbyte z_direction = 1;
7 K5 Z2 B  Z( {4 \1 fbyte u_direction = 1;0 T0 ]! t+ w; w' p  J- X. b1 G7 F/ t, i
, `9 U8 t! ~2 ~" F9 j6 }
//初始化字符串处理
" M  a, T# B! s/ f2 r* ~( E! kvoid init_process_string()) V$ b( L4 s% k: g- E
{
* l5 k& u. A, _- B  //init our command& j9 V# E( F4 ?, U  e# I
  for (byte i=0; i<COMMAND_SIZE; i++)
7 k' I# G5 J: f6 v1 W    word_old[i] = 0;
" Y' E$ \& p+ d5 L  serial_count = 0;2 b, Z* S, z' a8 d+ \8 Z8 F% b
}
! K* y; f; O3 k4 z' P) [3 ~2 E* ?# l; ^; X4 {
//our feedrate variables.
  i2 o2 m( D+ T9 hfloat feedrate = 0.0;
9 ]# H) D' `; |long feedrate_micros = 0;
: D% _) f5 f* S: i! q' r* V  w7 ?& u% \4 I0 X; m7 z$ p
//读取并执行命令
2 }. V7 I. [, L3 J* nvoid process_string(char instruction[], int size): x( I, O" w0 s; z
{: ~7 l2 \" V5 D( b, c+ T, r
  //the character / means delete block... used for comments and stuff.# H; Q% A2 R: {  k1 W- [
  if (instruction[0] == '/')& C" Q$ T6 E/ p( ^
  {
; A8 M8 Z  E# a1 ~# \& l    // SerialUSB.print("ok");# p. I6 X* ~! ~; b+ e
    // SerialUSB.print(byte(78));& G( j7 h5 {/ ^" l* ^
    return;
9 v9 h2 }& l  u6 e% s  }
  p$ z/ d0 `' ~' O; X+ s3 J  //init baby!5 |7 [- a* {9 V  C) O: N' |
  FloatPoint fp;
# b6 C& I8 o* M; o4 F$ Q! D  fp.x = 0.0;
# q2 |0 I  a5 v# V1 A  fp.y = 0.0;
6 k+ H- q9 ]0 d  fp.z = 0.0;5 e, W. M! l. b. `2 i: P
  fp.u = 0.0;
: A: [+ Z8 z; j
! @) [# Y. R' a  i  byte code = 0;& F( L! G) z# {" k/ d

; H: ]- W9 O4 C" `/ [& Q. q0 r  //显示在处理的命令, x9 x  S3 s$ i$ E
#if (LCD_TYPE == 1604 )
( L& j1 Y: W' J; a  // lcd.setCursor(0, 4);; H+ e( L6 s! j3 u; C& x; r
  // lcd.print(word_old);
. C& V; Q6 v; u& W" N6 |+ q#endif
* U& z8 c6 A- C7 U  SerialUSB.println();  L! h, E  n% U+ y) ]
  SerialUSB.print(instruction);
) k0 C6 M, [# |' n% t5 k% O  SerialUSB.print("\t");% @' I6 f4 h' J. ~0 W

# r: i) \0 f* p7 B) [  //what line are we at?- o& f8 h8 X+ [: b
  //        long line = -1;* Y0 f. p+ V8 W/ s+ [8 u
  //        if (has_command('N', instruction, size))8 j4 j$ n+ M$ x: Q
  //                line = (long)search_string('N', instruction, size);. B. `% Q) \# n7 q: x+ m  F

( G5 p& L( F" Q( S/ ?9 o  /*& n- O+ O' A5 B8 Y" d/ s4 a
        Serial.print("line: ");# G, o( a6 P; T4 j7 C
           Serial.println(line);8 m; h& @& T0 E: O: `
           Serial.println(instruction);
7 P) |+ @4 |! ^   *// y  i: C, E0 ?, [
  //判断是否读取了个 G代码?
7 C1 p! H0 S: f; t, S  if (
9 Z6 h1 ~) D/ r4 z0 G+ A5 Q    has_command('G', instruction, size) ||
4 a2 O. b0 l* |, k5 T    has_command('X', instruction, size) ||
5 |. o1 E# v3 B    has_command('Y', instruction, size) ||
' v% l2 C/ E; W  V+ A( M* c( S5 p    has_command('Z', instruction, size) ||
  x0 h  [% `2 c& ?" y4 t2 N+ |# }; S    has_command('U', instruction, size); N, Z* \6 I& j$ n2 Y- A9 r8 }
    ): \; B7 {& ~' o* K; F% G9 o
  {" v( U8 `; n  j6 q& D
    //which one?
( i$ V6 [1 J5 }- l* x$ |" d    code = (int)search_string('G', instruction, size);8 {) s2 C5 W6 M* U  U& ]
    // Get co-ordinates if required by the code type given
  l. g8 U( w7 u, b0 t! l    switch (code)# }# Y  W% D5 S3 M- U
    {) t2 U; L. k/ y* [5 {& w" U
    case 0:0 Y1 @9 Q9 W; Q# @$ L
    case 1:
1 v; ?2 R  I2 }* w    case 2:
/ H* J- v, a7 X6 Q% I- {$ A+ u: }    case 3:
, H# T2 o2 F7 E6 F2 Z* C* p      if(abs_mode)( e7 ~4 n+ X4 g9 ~
      {* `6 ^, X! |5 b8 I5 [$ S  _
        //we do it like this to save time. makes curves better.% z/ F1 L4 x1 r
        //eg. if only x and y are specified, we dont have to waste time looking up z.- |6 P) H$ k$ W5 T! G+ w3 v
        if (has_command('X', instruction, size))
  ~1 F( B) C& x) O. i! W: O7 C( n, |3 K          fp.x = search_string('X', instruction, size);
+ v: @. s; y& e' g- c8 A        else1 _2 h0 b! w' C0 {- r0 z( Z8 n
          fp.x = current_units.x;
4 C& K2 G3 A. h
9 {1 }  m+ u8 _% d) D: _/ Z        if (has_command('Y', instruction, size))* x5 C5 j  N& R, y& x
          fp.y = search_string('Y', instruction, size);
, N( @6 M1 Y( D' |" v        else# L, y! ]9 N$ x! j6 v; m
          fp.y = current_units.y;
9 }& A7 H$ s% T0 y, y$ Y) C" ~5 x
5 [- n# }( k- q( Z1 `        if (has_command('Z', instruction, size))% _# j7 q4 k3 d- R: z) c4 A% \( u3 \
          fp.z = search_string('Z', instruction, size);  E+ e3 n3 \0 N$ o6 l
        else+ s0 q; M* Y2 f: i" S& t
          fp.z = current_units.z;
) m# U7 r4 n) X% \- U          % B# b/ |# h# W7 U8 O
        if (has_command('U', instruction, size))
  y) U) ]& Z" k9 s          fp.u = search_string('U', instruction, size);
5 \. a# a1 D6 ^        else7 }8 G8 C. J0 N2 |+ U' `
          fp.u = current_units.u;& J. p# O5 y( c9 T4 f& u8 m+ G- F
      }$ Y5 L6 \0 k+ j+ O# E
      else: W9 ~1 }! W2 ]: c: V
      {" b+ B& |6 y8 N- l( }' J
        fp.x = search_string('X', instruction, size) + current_units.x;9 f* K* N- b7 N/ a" M
        fp.y = search_string('Y', instruction, size) + current_units.y;
% w5 t# @1 E8 y8 V  U) U        fp.z = search_string('Z', instruction, size) + current_units.z;6 R' Y0 Q1 c5 }! k- ~
        fp.u = search_string('U', instruction, size) + current_units.u;7 Q) @' L* H# q) S% M& `
      }
6 i1 `9 P" [7 ?3 n& b6 ?      break;
' D6 i5 X# ~' G* t$ m    }9 A1 H9 U* u/ Y! V, `/ [1 r( \
    //do something!
5 c, B7 w, \# v, H3 A# M( m4 P- [    switch (code)
4 g: Y+ {$ i$ m- @, O' _4 I    {
0 F% i3 s! t9 z. b9 y3 J8 A) i      //Rapid Positioning1 ~, u0 I6 `* l
      //Linear Interpolation) r# T2 t* M% p& S' K
      //these are basically the same thing.
& V3 [9 X* V4 k; Z8 B    case 0:* W; ?) T8 C5 o5 Z& O
    case 1:$ S1 k( ]4 l% L* Q$ U3 U7 n8 a. t, D
      //set our target.
/ A. R$ @' E  ^% R7 [* n. X      set_target(fp.x, fp.y, fp.z, fp.u);1 T( c3 i2 w7 R7 n) k, k% H6 e
      //set_targeta( fp.a);9 L2 \8 e' ?/ n$ v5 Y5 D
      //do we have a set speed?
- e, y1 k, a5 f! ?3 \1 y& v) X      if (has_command('G', instruction, size))
0 u6 n+ f! h2 r' c5 B0 i      {0 P. D9 q* l4 L: R8 ]
        //adjust if we have a specific feedrate.5 Z$ Y' y( M* [2 h( _+ M
        if (code == 1)
; c( w; ~8 g- E% _& n        {
% K: p5 a0 M1 g7 k( h" g3 T' p          //how fast do we move?( @" n2 x  H% J  P. N5 I
          feedrate = search_string('F', instruction, size);
* Y0 _" m8 ^3 o, @3 D% `          if (feedrate > 0)
. i: g' a. C. {# k, x/ }( v  k            feedrate_micros = calculate_feedrate_delay(feedrate);! r" r: W0 ?" w: g6 z
          //nope, no feedrate4 ~, l' B9 j) t, ~! _2 J
          else5 d, e" m- c2 Z- G. J
            feedrate_micros = getMaxSpeed();
8 F4 k: R5 S) Z( F4 t! V2 R: Y        }  B* z. k5 ^, C0 v) `
        //use our max for normal moves.
+ T' {. d, n% ]& ^! I4 [/ m        else
7 I/ _; s; v: C, c8 I          feedrate_micros = getMaxSpeed();' L3 y, g+ Y: X: V) q  R8 F: z
      }1 ^+ {8 `; j0 R( h* H# V
      //nope, just coordinates!
) o& S! u0 W3 z+ X5 `      else
2 x4 M0 W; B& _/ o+ c( Z" T  C      {5 v% C  p4 o# P' A+ Z' b6 S% {
        //do we have a feedrate yet?
9 e: O# R5 ?: @/ i! A9 J$ @, u        if (feedrate > 0)
! w* K6 a+ H" a          feedrate_micros = calculate_feedrate_delay(feedrate);
( U8 N% r" Y9 e4 i4 T3 l; H  m) @# m        //nope, no feedrate: r6 U# X0 c0 w% G1 D
        else& `. j+ n( W; }9 X" x. r/ V
          feedrate_micros = getMaxSpeed();* {8 R6 a5 @7 l% G/ _
      }
$ p, \7 T, V/ L+ {) o& `$ n# ?8 o, @8 P4 x
      //finally move.- c. F, T  s1 m% g8 D: Z  [
      dda_move(feedrate_micros);
8 s- O2 p! Q5 q      if(stop_flag) return;- @; Q7 A/ A) X+ _4 S8 w2 y
      break;
3 W8 S8 x! u  D: j) v
0 H$ p- M$ F' w" Q" b      //Clockwise arc; z! O6 X2 U2 U0 k# o$ w3 B. o7 {+ q# r4 A
    case 2:
0 Z1 n4 |0 [" Z0 W5 ^& F3 h. l5 ?- x      //Counterclockwise arc4 R5 t$ l  A7 l, {& q
    case 3:
* e/ C8 |; B" n9 H( c8 i' g& l) F      FloatPoint cent;
3 q" m" S& P% r3 n: c, p4 H, J      // Centre coordinates are always relative/ `- }: [! C% T
      cent.x = search_string('I', instruction, size) + current_units.x;6 H5 l  @3 u; e* e$ W6 P' y
      cent.y = search_string('J', instruction, size) + current_units.y;
! N" `" L' E2 ~0 |      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;2 k3 ?2 k9 a+ _' y+ y

0 N) _3 I3 u; p6 g$ Y% |/ ^      aX = (current_units.x - cent.x);
; ]" l, o* D3 L4 n7 A      aY = (current_units.y - cent.y);; p0 ?3 F, B2 E3 Y8 q0 I" t0 V
      bX = (fp.x - cent.x);8 g$ S. ^0 T. X, {3 ~5 K
      bY = (fp.y - cent.y);
3 K9 i. c8 o) p& ^) Q$ |) O8 ]9 t5 x8 }4 j. X8 P
      if (code == 2) { // Clockwise& b9 Q4 {$ Q! `3 d
        angleA = atan2(bY, bX);9 b& ~& M; [" u, L4 @) N8 y; ?( s
        angleB = atan2(aY, aX);
4 B; L5 l) e; O      }
5 `2 {# R. L$ v% d: `$ b. I( {      else { // Counterclockwise
  v$ c! d4 C) c! z/ ?  P6 Y        angleA = atan2(aY, aX);% u, V' W( d/ c3 C
        angleB = atan2(bY, bX);- g( I! `, |1 t8 [5 C) r
      }5 _" q/ c" F2 h! p" V/ |1 F
      // Make sure angleB is always greater than angleA
7 O' v5 x) K, j" A7 v% Y' \, j. h: n7 q      // and if not add 2PI so that it is (this also takes
5 E. v' o% t7 F$ n      // care of the special case of angleA == angleB,
6 k8 i6 W% Y; _) t. c0 \: \      // ie we want a complete circle)
' W3 g$ F* t* n$ R# x+ |9 v      if (angleB <= angleA) angleB += 2 * M_PI;
, H" o  Q' l) ]  v! M      angle = angleB - angleA;  x# u0 j/ f5 M4 c# c

2 R% b) S; Y6 z" n2 u4 P      radius = sqrt(aX * aX + aY * aY);, g* L: ?2 \" u; C; |& i' o
      length = radius * angle;  p9 n& K8 O) T7 A
      int steps, s, step;2 E1 B1 P9 j* d3 r0 u1 |2 H
      steps = (int) ceil(length / curve_section);
( Z# s& o+ \" O4 }3 m; `0 ~2 D" a; i7 M  a/ ~
      FloatPoint newPoint;
! H9 Y) ]. y6 p; f. g- w      for (s = 1; s <= steps; s++) {
/ }* i+ b5 [+ a6 g. T& u        step = (code == 3) ? s : steps - s; // Work backwards for CW1 T  E* Z( d0 d- g
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
3 B( z, p. I' `7 m- I, ^+ ]( |        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
  `2 W- _% \7 ^, a; u        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
4 ^4 V9 ]0 s& q' _3 R6 `9 v7 ^5 a. G$ d) t8 i( d0 o/ `9 ^9 }
        // Need to calculate rate for each section of curve1 t, m; l" W/ A* m
        if (feedrate > 0)
3 C( y' C. w- _1 T' A5 J          feedrate_micros = calculate_feedrate_delay(feedrate);
  E4 S% m3 C' S. o2 C  @2 c        else- \- |. J5 R# j
          feedrate_micros = getMaxSpeed();) w6 S1 h) a. P% J4 ]) j
% M: o4 C' j4 |" R2 y4 m. Y
        // Make step
" N! {5 H. n) Y        dda_move(feedrate_micros);
" W. F. E6 g( Q" G        if(stop_flag) return;* {0 G. A/ `7 W" p  w$ w: U0 U, X
      }) q6 z" Z/ @1 \- y% j+ Y, U4 q2 T
$ g. i& ^7 _2 V. i
      break;
" J9 G- n' E+ H+ O9 d: ~" e7 P" T+ q' p5 t0 V
      //Dwell
4 h! ]& L  q/ k, W* c; k9 D    case 4:& ^6 |5 L  T9 X) K* I' O9 d) V2 t
      delay((int)search_string('P', instruction, size));
" _0 s8 ?9 Q  j; a      break;2 q9 R* V( _0 F. y
% l# A- |( s  y8 a
      //Inches for Units/ K+ @3 `& m$ p& O, R3 d+ ?# g, `
    case 20:
# S# F& T0 ^0 w      x_units = X_STEPS_PER_INCH;8 G7 b+ ^7 V9 u( _; E6 [
      y_units = Y_STEPS_PER_INCH;
. K$ ^2 _$ e  Z6 `( I% K0 g      z_units = Z_STEPS_PER_INCH;
2 J- }" ]. \$ H4 p" w6 q      u_units = U_STEPS_PER_INCH;+ i* L' E" o( }, Z- Y: [
      curve_section = CURVE_SECTION_INCHES;
+ g0 k+ b3 s2 h" g      calculate_deltas();- P- U' U) `0 O$ \/ O- g; n
      break;
5 o. u% l' Q* n7 I; R) `/ ^$ o. Z! |$ r$ Z6 r( ^& v9 T
      //mm for Units. Z' P5 y7 t# J7 i9 P3 x# s8 b
    case 21:
; M+ X2 K2 s/ E  J" \  _      x_units = X_STEPS_PER_MM;
0 X7 f* r! p, J6 G6 @# C7 `1 e  }      y_units = Y_STEPS_PER_MM;
/ `% |* E: R" {) A- A4 m. S      z_units = Z_STEPS_PER_MM;
0 F( M$ F6 ]: s* ]      u_units = U_STEPS_PER_MM;
$ O2 j! u9 O$ z; u, ^- \; P9 [' q- ?      curve_section = CURVE_SECTION_MM;" Y+ r5 W$ S: ?
      calculate_deltas();; i3 M3 H' t0 _; B* ]
      break;
' S4 K5 m4 f. q/ v2 P1 ~
7 |. N8 ^# u; c# i% d( [      //go home.
7 i/ i" W3 Y. s& j5 L# ~    case 28:
9 I, g) l& X. Z) V      set_target(0.0, 0.0, 0.0, 0.0);
& D9 W. ]& |5 Z' L      dda_move(getMaxSpeed());
# g1 m( j# m& R6 q4 M+ K' @      if(stop_flag) return;
+ U  @) I% v8 q  x' ^; _3 T      break;
( X; y6 W; H  e& J
- m& \3 L) ?' V: l* Y      //go home via an intermediate point.1 l, R& H. p5 d- X& |9 S
    case 30:
8 F$ i% _  v2 z* b7 F      fp.x = search_string('X', instruction, size);& z6 z% S6 K3 k' ~1 d
      fp.y = search_string('Y', instruction, size);+ W4 ~1 f4 t4 y* y9 i  `
      fp.z = search_string('Z', instruction, size);
. W% x3 ^: r3 C" p      fp.u = search_string('U', instruction, size);
. s5 H4 R0 z7 O' o" z$ m1 Z      //set our target.5 K# u5 q. C5 r; @, z5 T
      if(abs_mode)5 y" n9 M( p; e( o  v
      {$ ?7 H  w% f3 }3 `2 J' K2 r1 v6 p1 s  v; K
        if (!has_command('X', instruction, size))
+ I( O2 I2 C% {1 `# ~  u5 Z* T          fp.x = current_units.x;
9 r& @" n; Q7 R  h- s1 Y        if (!has_command('Y', instruction, size))3 R- z; G3 h9 \# v
          fp.y = current_units.y;
$ ?) i& h+ w+ A% F) N3 d        if (!has_command('Z', instruction, size))
& L* k2 b+ `2 R& h          fp.z = current_units.z;
* W4 i7 f: C1 S7 ^, l- \6 {& L        if (!has_command('U', instruction, size))
0 c  B4 \4 G- W4 w8 |' f          fp.u = current_units.u;) V) E# h" b% J; U
        set_target(fp.x, fp.y, fp.z, fp.u);# }' x' b) ~* G
        $ x0 X5 K) B: F3 B$ f& O5 V
      }
/ q) v. a- M' ?8 [% ^+ c) k      else
. {% R) ^3 I- b- L0 U        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );0 f) I7 e  ~2 y+ C2 M
      
7 p8 J! p) [4 h2 C' A( d5 O      //go there.5 e2 W$ Q% G* z
      dda_move(getMaxSpeed());
- N* Y/ O& A$ I! H# h3 R" h  I: N      if(stop_flag) return;& `# |" T" |: S' A" C
* L0 d0 \; U& {$ r" Z$ q5 l
      //go home.
8 N4 h. V1 Z: N+ r* S% @4 N& E      set_target(0.0, 0.0, 0.0, 0.0 );3 V  {1 u/ t$ U8 y* J5 }5 g1 q
     
- C9 ]' X. h0 V- C      dda_move(getMaxSpeed());& S" P4 M: s& \! {0 r- g
      if(stop_flag) return;% z2 Z; ]" k( h3 X6 z3 Z" t  S- R
      break;# b5 d+ U7 M" h5 r: N5 [6 w% v
! q2 P8 H. Y+ s& u6 C4 Q- t
      //Absolute Positioning0 z5 ~6 L+ a$ _5 f1 |
    case 90:
) h3 M) y( s2 Q! y# p$ E% z      abs_mode = true;; \7 I% L' W/ {- u
      break;! M" l( J* V: B

5 t' F4 Y4 i& n  I      //Incremental Positioning
- O+ f0 {; I) ~  a8 }1 M! p  N    case 91:7 W) x, {0 f: `3 p; n7 |+ k% ^
      abs_mode = false;7 d! i! z* N! `8 L# p/ C* X! A
      break;2 h5 C8 |5 f5 q6 v; ~. W

' Y0 p+ ]( J% W+ }- _& X; d- o2 _. E/ e" X      //Set as home
( ?  X; k7 x5 O* q0 \" C; l; B    case 92:
7 G0 \' l6 L3 ]& K4 g% O     
. l! v& D4 {: Q1 R5 a4 r6 ?      set_position(0.0, 0.0, 0.0, 0.0 );& x  B( w8 G; L2 C+ \
      ( J, H4 c2 i7 O& O: Y$ |1 v% n8 ~
      break;( w5 d3 ~' u2 Q, K4 h

& S" M0 D& y+ }! S+ v* @      /*
' `8 A; E, q8 [6 y                        //Inverse Time Feed Mode
4 l, B+ N% v2 t# o' D% u4 u                               case 93:) a3 ]. {6 i; j& c- O
       " M6 K( Z8 Z! F
                               break;  //TODO: add this1 _' D4 S8 G4 J! q# J
      
6 k  f; J' u% v5 L5 Y) `9 P                               //Feed per Minute Mode- T4 U( N8 R/ o1 @$ M! N! _
                               case 94:. V" Z* \, V) j1 Y1 s& j
      
! w% l: S2 y/ u2 d; G  t! B                               break;  //TODO: add this
8 D) |6 q% b5 Y0 Y/ j6 G       */: f/ M2 [7 Q. i- s% x( S
4 ~' w( R7 d$ Q$ ]. Y( V* G* K
    default:- ?8 ?  s; `5 G- x& b8 P! {# O% i1 F
      SerialUSB.print("huh? G");
: e: J2 L& w! P6 X# e9 B/ ?      SerialUSB.println(code,DEC);
: _. I" Q: |* }" q$ G5 {    }
* U: k- A. ?) n/ ?. C% S( e: q  }9 \: c" r# i, @* x# _
& a. W. u* c$ O! s
  //find us an m code.
# H& T" l8 j9 M) H7 z! S  I  if (has_command('M', instruction, size))
! c$ ?+ S$ Q: s3 @, k- Q2 G; Z  {) _0 d( d4 u% z, n. R& }& ^
    code = search_string('M', instruction, size);
! i% T+ `  s4 u% X    switch (code)$ c3 H3 A% `* H0 U/ M& y7 A# U' ^
    {0 J( c' k4 k' y! p: ]
      //TODO: this is a bug because search_string returns 0.  gotta fix that." ?" o' _7 u5 S
    case 0:
3 o7 D" Q3 P/ J! b0 N      true;7 F5 u. m7 |/ ]" E  I/ ~. u' k
      break;; x2 z+ R% H. F5 k

' i% D# _1 X4 K# N" x# s    case 100:( Z- J9 T2 z4 \: i% C6 P5 z
      break;' m* u1 D0 i* T3 R

+ R# v% n3 ]: z- |+ A/ d& s- N4 E! c      // fire camera relay4 I- v, Y7 y% `1 L% O4 v( f3 S$ A
    case 101:
& `/ m9 m4 P5 j      camera_shutter1();, i4 J( k9 m) ~6 q1 Z
      break;, ~7 d$ }# f7 s3 n
. f" S1 P  }! C' r0 n7 Y7 B$ f
      // fire camera relay2' [9 L- k! H7 v  O* Z* `
    case 102:
# r( N4 N) O  w) }  U      camera_shutter2();
4 I7 k4 N* ~2 R- K) n      break;
# y8 s, P& q2 h  x5 G( j' U: m3 l/*& L% L& a8 q& n4 a; [1 ~- p2 ?
      // turn aux 1 relay on! q; f6 m* w/ O0 Q, `: Z" x! R4 y
    case 103:9 |9 T' g1 r: s# h6 t5 N
      camera_aux1_on();; G' d2 T( d# @5 }1 k5 }
      break;
7 `- ]$ s5 e0 D0 J9 d
) r1 b" q. T& l. j/ X      // turn aux 1 relay off
! C) ?0 a; ^4 N- i1 _    case 104:
% F6 o, B7 d6 P" D2 H% u. E      camera_aux1_off();& L- X+ d! Z7 {5 S# e% E1 F/ n3 Q
      break;" N  y4 ], a' d( Z! q( i* }

# m6 |! U' Z! u2 u3 P- e      // turn aux 2 relay on4 W6 I. n4 b8 r3 m# j8 ]
    case 105:
% W" f7 _* q! n- D8 W* q) f      camera_aux2_on();4 e! m; V' _# z2 F! L9 x
      break;( p3 i/ T- t+ l* ?6 ^) A; j

/ E/ x- p6 F+ i      // turn aux 2 relay off
6 ?8 x% l$ I3 l' x2 [    case 106:% K2 U; t5 P9 g' V* w" t  U# d! U
      camera_aux2_off();, \, f2 q+ g& ]* u
      break;
- T2 }& h- D7 |" U
8 {) r6 [. h; {  f2 `4 }7 w( c      // turn aux 3 relay on
2 Q! y" C& X" I2 x    case 107:
) P8 |1 k. c( D! o4 ?      camera_aux3_on();
3 H1 B- R9 L4 L      break;
# D5 A; C: `( k' f) i5 @0 u
+ `3 P! ^# X( M; x      // turn aux 3 relay off
% f- d% `8 V4 Q# }  @* K! R    case 108:# {8 T3 l  j  w9 e5 j+ _
      camera_aux3_off();
6 r7 v7 v( n' {) j      break;0 i  v8 a: L- p' F  f

' T9 m0 N0 }- o6 }2 z$ H      // turn aux 4 relay on2 l8 l1 N. F9 U6 c
    case 109:
3 @9 e) j8 G4 L2 W& g      camera_aux4_on();6 [$ ~0 y* |" v0 x. ~+ ~" T! `
      break;
1 O" N* L) T% v9 _/ A0 B) g
) ^$ }3 [$ G6 W, o9 O6 r8 O      // turn aux 4 relay off( C5 `- w- b! I0 V0 k$ d8 R
    case 110:
; n; d3 }$ h/ d! A2 d; z0 i! `      camera_aux4_off();
! p9 Q+ C, a) {, r( R7 y9 E1 \      break;
# {9 d1 W+ O7 p*/
; o/ q6 \8 H5 y7 t: f; C    default:
) _  U3 d. b. C
2 C' Q& X. d3 ^2 z; p" q4 [      SerialUSB.print("Huh? M");/ W9 z( C3 j& h- h/ N  f
      SerialUSB.println(code);
/ F+ ~- ]+ W( B9 o7 u3 E    }2 E9 ?0 D" b' O, T1 y
  }
1 O9 r+ b3 x+ d' f, I# I$ l; G2 ^4 b; y$ ~+ i, h
  //tell our host we're done.
5 p3 }; F( D5 l; F6 x6 s  SerialUSB.print(byte(78));
, C% t+ _1 h  e2 R$ I. R1 y5 G& |; ?9 p
}1 D4 _* k" J, G0 Y
$ s6 ?$ L0 ^  n. ^6 Q
//look for the number that appears after the char key and return it
2 V9 I6 F6 N+ Z0 {" [: Bdouble search_string(char key, char instruction[], int string_size)
0 @1 I! C" c) _1 d4 x) J! A{
: N% b) s2 c' W4 h7 F  char temp[10] = "         ";) n/ P! V  ]- ^! a( S/ [
  for (byte i=0; i<string_size; i++)0 R+ P$ `' h8 \& C5 P
  {
: d7 f' j0 Z! E. [, I/ o# k; e    if (instruction[i] == key)
# F# r+ n4 D9 J/ w1 z. Z    {) b% P  e' N. s; S' h
      i++;      
. }' R8 c! K! H4 D8 j      int k = 0;
! _) M8 ?5 E* P$ d' ?# N      while (i < string_size && k < 10)7 Q1 W) i+ `! v! }; y; A; {' S# W! a
      {
) ]9 a7 N* O: B2 q4 {  r3 a4 t* m        if (instruction[i] == 0 || instruction[i] == ' ')
  @: ?+ U, f3 G, P: c          break;! \. ^- T' i- r

5 r4 A" x3 L0 \        temp[k] = instruction[i];7 X  R8 B* d  k! n6 b8 v
        i++;! G) M' w# `  Y, i2 D
        k++;
) Y) [  ?1 G5 n# P) C      }
* J3 l3 c) @5 @+ I& G) Y" E      return strtod(temp, NULL);
/ C6 j$ E/ a3 E. ?" _9 A    }2 D9 f9 j1 Q5 }7 h  P$ L3 ^! `
  }
: \8 E5 [, d4 H/ s  S
% l+ K6 w' `5 d0 a( w- \% U  return 0;
% l0 o# r' S) F. A2 o2 }1 d, I' P% I}
. O5 Z! j. |3 I( \" V3 Y7 f
3 O' y2 @& h; A/ j7 U//look for the command if it exists.  s' D" O0 g% p6 p
bool has_command(char key, char instruction[], int string_size)& q( {/ z; v3 H4 w! x, o4 U
{3 O2 }* d$ {$ ^( ~4 A( Y) M" e/ T
  for (byte i=0; i<string_size; i++)
, S( x. y$ x1 z, U  {
: r" y, c/ ^" r- u2 q    if (instruction[i] == key){+ X. v( f; q( o+ U

5 l' `6 e* H% T      return true;- f* m4 a  }& f% V6 B# c/ G7 h
    }
  n) u3 I( Y: U  }  }
- ^' t4 ]+ ], i; F2 X
9 X5 }& S- s. O1 t& S( ]  return false;
& ]. U: r+ J7 [+ t6 N}! d' Q$ \7 T6 y# |

8 g5 H2 H1 R9 Z3 \8 ?, v# @; B( s( d. s) o& l

4 g* i. q6 N: R# e7 |. T0 I$ g* {
 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层
6 @' w* d* {% U; o' _% p' P" n
//init our variables
$ O# Y+ ^4 h3 H+ P6 vlong max_delta;
9 h8 a' l: A$ g% Blong x_counter;
0 q9 [4 y! O$ k- D) V) Llong y_counter;( w* N# B; X: `; {
long z_counter;
3 y+ E* c9 A+ Vlong u_counter;
8 o5 t2 f9 d* J3 t. X3 S% M% [5 llong x_pos = 0; // x position in terms of absoloute motor stepps
4 N) D7 ?* s# N8 l  @. _long y_pos = 0; // y position in terms of absoloute motor stepps
5 w7 r3 J; P8 X! i0 z8 Xlong z_pos = 0; // z position in terms of absoloute motor stepps* E3 D5 w, v- ?' C- d" F& o$ C( J
long u_pos = 0; // U position in terms of absoloute motor stepps2 s" }0 R' n) _' B

' W' t( H! I3 Ibool x_can_step;
9 m& U8 m0 j/ S0 a$ m5 \: ]3 Sbool y_can_step;, r% Z/ r. m5 F+ _$ R$ f3 Y
bool z_can_step;
9 K. u+ W+ Z" lbool u_can_step;
. t% p7 m6 r* ~2 dint milli_delay;% R8 X/ b5 Y6 S* n9 f# G+ D0 V
3 k  [# c3 G7 i9 b5 ~- k/ j+ l
void init_steppers()& r& x  n6 f0 H- Q! M
{
3 P: |# T1 z9 l' i+ L  //turn them off to start.
5 d& k4 v5 x6 Z4 S6 @& Z  disable_steppers();
) P+ @7 g6 A% _* R" ~: `( G! L2 `) p# O: |$ i( e( V7 G! g
  //init our points.
0 v3 I% v$ v( M4 C  current_units.x = 0.0;
' F, c' {) R# l4 h9 J: W: K  current_units.y = 0.0;3 [! G* g2 e* w3 d' Q: e
  current_units.z = 0.0;
5 t3 H4 _9 d1 D% U  current_units.u = 0.0;, k' e3 Z: q) w  i
  target_units.x = 0.0;9 F3 H8 f, \/ I$ C
  target_units.y = 0.0;
# m8 i) X# @" w0 a  l  target_units.z = 0.0;
6 ^' N, F( z. W* `- ^) [2 f$ Y+ G  target_units.u = 0.0;8 \+ n% f% s2 L  |
  
5 d1 h! W8 j6 Q3 u& S# u  X  O" `" O8 Q' g
  pinMode(X_STEP_PIN, OUTPUT);
! a3 s. f" d7 G% T( L2 a  pinMode(X_DIR_PIN, OUTPUT);- a& D: _3 ^1 z
  pinMode(X_ENABLE_PIN, OUTPUT);
5 O9 u- P' W; L9 n, y  pinMode(X_MIN_PIN, INPUT);
; {7 \+ r4 o4 \" T  pinMode(X_MAX_PIN, INPUT);0 B& v6 L/ [( a5 _- t
2 [. Y6 g' B& L# p" f9 b, M4 k7 q" y
  pinMode(Y_STEP_PIN, OUTPUT);
/ f$ Q0 X' Y  y: c! a* T& W# |  pinMode(Y_DIR_PIN, OUTPUT);
4 P% O" x) b  W0 n' ]% |0 T  pinMode(Y_ENABLE_PIN, OUTPUT);- q. F" \# \5 w  v
  pinMode(Y_MIN_PIN, INPUT);
4 q. b3 Z$ i2 i: P1 t+ j  pinMode(Y_MAX_PIN, INPUT);
: @' d& A6 h; u1 `! B" I7 O
+ S5 k8 C; i( W: z# Y: z  pinMode(Z_STEP_PIN, OUTPUT);
8 G4 r9 x" a2 e* p2 g' B  N  pinMode(Z_DIR_PIN, OUTPUT);) g" q5 \" v# N0 h* x0 x4 I1 A
  pinMode(Z_ENABLE_PIN, OUTPUT);
7 c  X4 B3 c% t+ B; m" k  pinMode(Z_MIN_PIN, INPUT);( n$ e8 I( _0 N# Z+ x: O+ f
  pinMode(Z_MAX_PIN, INPUT);# d+ p- m; W# H/ A% t2 g
, i" d! L0 z% u+ r, |: c3 }6 s
  pinMode(U_STEP_PIN, OUTPUT);! A$ j+ h0 D$ c( Q; [* p
  pinMode(U_DIR_PIN, OUTPUT);
2 v& C6 ~" M' ^& L  pinMode(U_ENABLE_PIN, OUTPUT);
2 {* G6 `, L3 e) [6 ]# m! y  pinMode(U_MIN_PIN, INPUT);% W7 D0 ^1 L' n$ n
  pinMode(U_MAX_PIN, INPUT);
- R- F+ ?9 h# o4 ]0 f  //figure our stuff.
( X8 r/ N6 L# L9 N  calculate_deltas();
: v& o+ L0 y8 ~: F$ @}3 h. l  [# f) q$ r3 g0 {

1 ^) M2 g- N9 R4 m2 ]' F2 [void dda_move(long micro_delay)
  O* m7 \+ K" T2 \( ^9 a- E( d5 M- ?{. h6 }; _0 @) p: v
  //enable our steppers
# e6 r1 V  A  @) {+ L7 C  digitalWrite(X_ENABLE_PIN, HIGH);
& ]& j; c; u/ a  digitalWrite(Y_ENABLE_PIN, HIGH);" M; f5 {; r2 j2 i
  digitalWrite(Z_ENABLE_PIN, HIGH);
! {4 b0 z$ e8 C* R% P! Q( ~& y5 W, `  digitalWrite(U_ENABLE_PIN, HIGH);1 j0 B2 P2 s; J6 Q; i6 u
  //figure out our deltas
7 ?" z, i7 b' J7 ?7 L# L/ Q  max_delta = max(delta_steps.x, delta_steps.y);4 i1 Y' N. y6 P. G5 s
  max_delta = max(delta_steps.z, max_delta);
8 Q' G+ \5 V& f  max_delta = max(delta_steps.u, max_delta);! O  \2 p2 ?- r; L
  //init stuff.* f8 w# ]+ P. U' Y% y: J( m3 D
  long x_counter = -max_delta/2;$ F( t4 X7 Y& ~2 n. \$ k. y6 m
  long y_counter = -max_delta/2;
  F' H$ y6 P. f2 ~7 ?8 v1 l9 G7 {  long z_counter = -max_delta/2;3 g! k9 L! ^. t+ I
  long u_counter = -max_delta/2;, C  l! q7 z" X! h

' E# h$ ?3 ~' Q- K) W  //our step flags  R, |* _0 E- S# P& H4 y# Z
  bool x_can_step = 0;( ?6 I1 E" P- u2 |1 R
  bool y_can_step = 0;! w0 }) L. @! `, x' G3 H+ E
  bool z_can_step = 0;9 r; Z3 P5 Q8 Z" R$ J
  bool u_can_step = 0;
. w3 U# f6 Z% K' u( O4 W, e. n7 b$ j
  if (micro_delay >= 16383)4 E8 v. }' N" h* V; I
    milli_delay = micro_delay / 1000;/ u8 G# l' s& _
  else
& {% B' V5 I1 Y1 V- z8 L9 U    milli_delay = 0;
2 f; z- F" i: |  j2 b" C3 F4 L# M
5 H9 c+ ]2 A; G4 m) m
  //do our DDA line!) e# S2 Q& b) u2 c  A
* `( ]1 y! Y# X! I, N9 i
  do0 j8 C2 b  V0 h9 Z+ ~
  {
6 u0 B+ U" a: c+ h* ^/ ^% [, e% P    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;
% m6 ^4 r; a' L# @( V6 O; J9 X    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
& T) Y0 `. a5 ]& L+ C3 e    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);
% r& R* Q& i( t2 B" f+ g9 _3 v- W    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);( R4 _# D+ i' L
    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);
; }/ ]' X9 b( T2 E, N: F    if (x_can_step)- P% n9 k' q) ], J/ ?4 A8 A$ U
    {
/ H; c. j  `. {9 j2 x# }: F. ~      x_counter += delta_steps.x;
. u0 {8 H. \( e2 M- Z: k- O. f1 q
! H. c2 _8 Q2 u* \& ?      if (x_counter > 0)
: @' b! r6 V( _! J9 Y; k) F  H      {7 J$ ]( W" E& q$ L: h+ s
        do_step(X_STEP_PIN);; o( b6 z" F9 V% x" f
        x_counter -= max_delta;
( `9 M9 ?9 X* M. B; m: u        if (x_direction)8 o! m# l( b3 ~( R. y5 M
         { current_steps.x++; x_pos++; }
& @+ z+ i* f7 e! u" U* R         
9 n& u$ u, Z5 X# l# k4 J( l- G        else$ D* Z5 S* X% }0 z2 r5 E
          { current_steps.x--; x_pos--; }) \; e! |4 W1 N: g
         
% @/ B( t* b+ K2 ?. b" i+ c      }
8 ~0 z' s; e. \8 ]/ t' j    }* ]% B( J# C/ {$ o
    if (y_can_step)
' s9 \0 X8 z9 z5 U    {
% N; ?- I% a3 E. H  S+ _      y_counter += delta_steps.y;) r  N5 h8 n$ c, o4 X. @. n* |

5 A4 f: [' q3 ~/ t      if (y_counter > 0)! d  X% D9 n& C1 Z
      {: B- s0 j- F# e0 @# j. I
        do_step(Y_STEP_PIN);
- p- t$ H0 O' ^9 e9 [' K( _        y_counter -= max_delta;
/ ~. }* Y) }! s2 S- l
% }/ d9 p6 i2 y; ?( ?( ^        if (y_direction)  ^* s) J  R- ?) {3 I* f  `
        { current_steps.y++; y_pos++; }/ a/ l/ j' U" [$ c0 g& ?
      
( d4 s" Y* O; f$ C        else6 p! U% Y* r! ]% F4 R) U
        { current_steps.y--; y_pos--; }
2 n8 x- P) e1 h+ Q9 f        * o1 l0 L$ p: O3 u! A
      }
3 F' c; r" g% ]/ z: F5 q    }* y) |2 ~) B$ r1 e* k, h* A2 C6 b9 K
( `) h0 }2 r; j0 l$ p
    if (z_can_step)2 [  T. B$ z" b( X3 ]
    {3 M$ s" ?) \9 v/ N
      z_counter += delta_steps.z;
* s. P' A: P3 x: j: ~. ~+ T& ~+ ?3 w( x. o
      if (z_counter > 0)$ b3 k" i7 n/ y; |4 p$ @/ B
      {1 `  ?. T5 [6 F
        do_step(Z_STEP_PIN);
- G. g: d5 \: b        z_counter -= max_delta;
! T( ]' j3 N7 k0 V2 W+ I
0 Z* @: w, X1 Q+ d& R        if (z_direction)0 n% z) f2 B3 X8 ~
        { current_steps.z++; z_pos++; }; Z" ^& C% j; `3 c  z+ f; h4 h# Y' b
        
5 i; e5 }1 f+ E4 q' }# g        else8 o& n9 H3 {" q9 ]# Q. F
        { current_steps.z--; z_pos--; }
8 A9 D$ k  }1 W$ R( `        
- ^" d6 `4 d% f  `% _      }9 i) v  ~$ t+ g, Y& U/ i
    }
( Z0 |4 ?' ^3 X" o% g   
8 u- O( ^0 v8 R7 a8 X4 f    if (u_can_step); V7 V2 v2 N( l( D
    {- H% ]% u& R$ W+ K4 n. c
      u_counter += delta_steps.u;
9 B& G* E9 k" w8 D7 K# ]# G* p8 u
, i+ H' E2 ?$ K' C( ?+ m      if (u_counter > 0)
) G& p* h( g; l- L: j      {+ h* t. e4 v- k
        do_step(U_STEP_PIN);- f6 X2 }; I9 w
        u_counter -= max_delta;
  m* m- N9 a- S  d: a) i% ^- V3 P+ t5 v% d) @4 B* n0 v3 [
        if (u_direction)/ B1 T+ d/ T$ d* ?8 ~2 @) }% q/ r
        { current_steps.u++; u_pos++; }
# g8 J) t; R. N  w3 c; T! h9 f          5 M5 i+ u) v* q2 N  }; S  l  J
        else! Q. J7 m5 Y: A9 t
          { current_steps.u--; u_pos--; }5 f5 U5 d& Q0 E  K# w+ e  Y9 V
         / k) a" l, H( g& i9 N% B
      }
0 D/ f- O5 _- b( U9 W- h/ l) |    }' Z; A8 M5 ?) K4 r' `$ t
    //wait for next step.
7 V" w& d$ x6 O- n+ p; N    if (milli_delay > 0){
0 [0 w; _- r5 K/ G9 W1 q; s      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
& G; F. `% B4 r) J      delay(milli_delay);* L6 w" O4 b' C* u
    }                + k) {, C8 w2 X2 q( x
    else{, r5 A9 }: S3 F9 x, i
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();  e; u" F( i( |5 F5 z) s8 B. F$ B
      if(micro_delay>0)delayMicroseconds(micro_delay);+ Y1 s% r, ?# U0 B6 G
    }
$ o1 u8 }. k: _+ b! u1 k# E" k" B5 G    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);0 p9 \: S3 X2 Y
  }( g& {; X3 s7 K) c2 @8 G
  while (x_can_step || y_can_step || z_can_step || u_can_step);
, d# E$ t. ]$ L5 o& z3 ?7 U$ @- W

6 k; N. `! G1 u# f  //set our points to be the same
1 G0 n" s6 c: j; Q( `% ]  current_units.x = (float) x_pos / X_STEPS_PER_INCH;1 j5 A1 Z% [8 I4 r
  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;* \' V* b+ [3 |7 j! ^; N6 d
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
7 h# }0 M/ a7 j6 Z  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
8 Q  G) ~  T) M- U  5 ~$ n9 H6 w. c- Y; n3 i. t, V
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );
3 D7 q+ O) E/ J, d
: W/ o# ~* q) f) `; y  long x_pos = 0; // x position in terms of absoloute motor stepps
) Q! j' ~' G% c# b7 [& }8 Y: q$ k  long y_pos = 0; // y position in terms of absoloute motor stepps
' _4 i, F: g) S' _4 b, p  long z_pos = 0; // z position in terms of absoloute motor stepps
1 u1 S! c3 N; v1 z  long u_pos = 0; // u position in terms of absoloute motor stepps$ Z  c) c2 g9 e
  calculate_deltas();
: n' z0 [2 `4 K1 A/ H  2 _" X- {8 @: c; z
}# M$ |- ?+ `7 o

  r5 ~( n3 v' G* l5 t; F: pbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)
) E1 w  I+ c. M# g{& G7 r, U! K1 h
  //stop us if we're on target1 U0 Z4 m% a3 j. |
  if (target == current)
+ n4 D. I( f4 [! d* ^    return false;
0 n  S! n. _& S! C: _/ N6 O4 N  //stop us if we're at home and still going
2 B; n, R1 z! O4 q" R3 |/ p; H  else if (read_switch(min_pin) && !direction)
6 F( @# I) a6 ?" I2 ^/ |    return false;
/ y$ l0 m0 \$ H2 k  //stop us if we're at max and still going
$ c& x2 d& m. \5 d7 j8 I  else if (read_switch(max_pin) && direction)
3 ^2 p  C' I9 O* R. I" @    return false;
3 r% F' O+ j! R2 {% P+ R
% R. e) p7 h8 s* [  //default to being able to step
. H& s( w  _6 P$ ~, q  return true;0 n8 `3 r/ c/ o5 e4 ~* m
}2 g" \/ I+ a! Q/ `; i
* q5 F+ h5 i6 S3 I8 H, b7 B/ f
void do_step(byte step_pin)9 p) ]6 m; C4 e+ H" y% b" }
{! r$ W9 K7 L. V& I3 R
  digitalWrite(step_pin, HIGH);
+ u/ Q$ S: ^5 V0 L! [* ^  //delayMicroseconds(1);
# P% f1 x- p5 t9 l  digitalWrite(step_pin, LOW);
2 `) X* ]. L- Q; b}) i9 ?0 M: G1 P0 ?

4 t1 P5 [$ r' R+ t0 Rbool read_switch(byte pin)
3 C9 D7 b+ c: W1 D- o8 [{
9 C& ~- C  w1 m1 ]  //dual read as crude debounce# d& h0 A1 q  F. F& b- M
3 i+ `! T" \3 j5 o& @9 m/ |; o
  if ( SENSORS_INVERTING )
% I3 x8 \; _/ ~4 ?    return !digitalRead(pin) && !digitalRead(pin);$ d6 C4 A0 h2 D3 ^. j/ Q
  else
( k5 g9 y" c7 n    return digitalRead(pin) && digitalRead(pin);
4 n6 M6 K1 W2 g" ]% O: \}
* ~4 L3 s0 T1 F6 R0 p7 A) @
. \( E' M9 }+ C3 \2 X. ]# `long to_steps(float steps_per_unit, float units)+ \3 T0 }0 \7 y
{
! T" m- ~) c' B& b  Q  G( J  return steps_per_unit * units;
) `: ^! w( G) z& w* v! v1 g}, L! @8 ~6 u6 K- j- S  C/ d# ^% I

+ q5 A) ^/ X- P7 }9 ?void set_target(float x, float y, float z, float u)
' u, {" U* ~5 T$ u4 h& v" p{* {5 \/ F. g/ f6 l! \; i
  target_units.x = x;8 Q4 @3 Q9 V0 o- V. L# ?3 X- u8 a
  target_units.y = y;! L! M2 L9 `7 |" f  ]& p: [; x5 v
  target_units.z = z;: {2 `3 R/ {' l' i
  target_units.u = u;; d3 e( Z' f% n& U2 K- ~
  calculate_deltas();! A5 D: C+ e8 c) V1 U6 ?+ Y
}
2 R( H7 Q* L3 M5 v4 ~
/ O0 j$ C, i6 f, {8 nvoid set_position(float x, float y, float z, float u)
" h# L) t9 N5 r2 I# p{
6 J# H3 r" p  m1 T* l5 x) N3 a  current_units.x = x;3 P$ u& A! o6 z! T# ^3 r9 b5 l
  current_units.y = y;" S! C  h4 W3 D# H3 \  B
  current_units.z = z;
( n/ E9 Z" B1 @0 a, d, k! B5 D; v  current_units.u = u;
. [/ e: [3 E; F! ]5 f/ Z  calculate_deltas();7 i& A2 s: {6 K
}0 q1 o6 o1 q( o2 ^

. F! n& N1 A/ d$ \9 B* y! Rvoid calculate_deltas()
; M( r, F$ T6 M2 V5 w5 P1 V{
/ H( a7 k6 i1 O4 `. d9 e5 W  //figure our deltas.- a% m' T1 d! j8 ^
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
$ g* U9 u* W% e' r" u  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);
% T  M. l" Z+ Y) S3 ?  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
* \$ o8 L0 r8 [) Q: z/ Z  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);
& F, d: f* z1 S1 ^4 F6 A$ }8 ?! b3 f6 v: B% _+ _
  //set our steps current, target, and delta
1 a4 B" h8 W+ }% \+ M  current_steps.x = to_steps(x_units, current_units.x);
) Z: H1 H% U; t4 |  U9 E  current_steps.y = to_steps(y_units, current_units.y);
) T3 X4 a9 Y5 g' w9 o. \- p: Z% q  current_steps.z = to_steps(z_units, current_units.z);7 `# X" k: |' Q3 C1 O( `3 k
  current_steps.u = to_steps(u_units, current_units.u);
, \( @8 R- a& o" o) F8 r+ c( a& X# {
  target_steps.x = to_steps(x_units, target_units.x);5 B' V: K' z2 ]7 c$ S/ z. }
  target_steps.y = to_steps(y_units, target_units.y);6 \1 H0 v5 b/ J" n! Z
  target_steps.z = to_steps(z_units, target_units.z);
. y7 D& M; K- A0 ]  target_steps.u = to_steps(u_units, target_units.u);
; |# r; E) }0 ~5 a6 q( s  Q( E- \5 ]! K
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
; p6 y7 G, b& m# C8 J! p  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
* l' K7 }% _$ g" Y% {# n  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);: q3 Y& k7 W9 I$ t) T1 I- q3 n
  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
$ H" Y  u- X7 L5 J% ?& `, z" s* a. B. {. u! C
2 M9 k0 W2 X7 @+ K

. O" [: S, s, j8 U$ b/ |' m: T, l  //what is our direction
  v3 ]8 L# X1 B/ z/ C  x_direction = (target_units.x >= current_units.x);
- ?; f% r* f5 r& C  y_direction = (target_units.y >= current_units.y);
7 k! g8 \/ L& I# i& n9 p6 P  z_direction = (target_units.z >= current_units.z);
0 C  X: W- L; h- [  u_direction = (target_units.u >= current_units.u);6 u$ W0 z& w' k! K9 w/ Z
  p; \0 ?; g3 K! r
  //set our direction pins as well
0 t& s+ l/ p  A1 N1 I2 m# x  digitalWrite(X_DIR_PIN,x_direction);
; p" R7 s3 y0 w3 n  digitalWrite(Y_DIR_PIN,y_direction);# ^( _# F; y0 q/ e: t
  digitalWrite(Z_DIR_PIN,z_direction);7 C6 Q, P; ^4 {& R: ?! b7 e
  digitalWrite(U_DIR_PIN,u_direction); & w, Y( n. A$ [$ ]5 X9 v
) M( R9 b( d  A6 Y7 a  M  }; G# q
  //绘制LCD
' i) i; X. c8 ^1 U  LCD_DRAW();
, t6 C& T, d6 [# A& u) Z5 a7 ], h# g# O% R! N
}% u! O0 k3 D9 U3 `4 c
5 V% @' f' ^2 b; g4 B

+ t' Q! ~8 M$ Ylong calculate_feedrate_delay(float feedrate)0 p$ C# z: k6 T: c; ]6 v
{/ ?. R9 f3 t. F2 o
  //how long is our line length?2 B" e" \4 N2 U+ [# a5 Y
  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);
. {' K& k) a* Y3 |; g$ G- @# P. i5 `  long master_steps = 0;
; R4 d8 X; U2 Z* O; D/ A
% L$ |& |) k9 Q8 s7 Mmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
: @; q$ s8 ?0 o& dmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
8 G6 \; o3 ]6 G. wmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
" ]3 @0 ~. [- @2 W! W7 D5 W, l0 |( H9 w

+ J! j5 Y% @5 B3 @7 m. v+ b+ T0 e2 }! o
& g, z( p0 z. @. z9 Z
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad./ X  @& K7 F  Y, {
  //the formula has been condensed to save space.  here it is in english:! H) a/ p2 r! |1 }( Y$ z
  // distance / feedrate * 60000000.0 = move duration in microseconds
& \3 {3 S& w4 Z/ ~+ S1 {8 ]  // move duration / master_steps = time between steps for master axis.
; ]( U. A" ?3 v8 F/ ?* A) q% x return ((distance * 6000000.0) / feedrate) / master_steps;
7 B/ n. z- ^+ ?1 S
% k: I! D+ \$ }4 M$ h. L) }}" Q1 O" ^3 I, i; j' x& h1 o

5 ?) e( V& _  w; Rlong getMaxSpeed()
- ~9 `+ a2 \, U/ S" l* d$ P{
1 e. B# f4 `  @- n) B% ^! L if (delta_steps.z > 0 || delta_steps.u > 0 )2 ^# g+ E+ [4 P/ L: j
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);
4 G+ j1 t5 X( Z6 {8 ]9 G0 k; ]: x  else1 b1 Q3 E+ ?' Y( m. u
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);
# U4 r! `+ E8 ]0 }3 G6 U}
  X5 k; m( G/ E! J0 ?
" c3 F/ G' x! t. ~/ I& Dvoid disable_steppers()( L) G! S% m% p, T% v- ~2 l
{
' K+ U% V( C& ^4 h& \" ^  //enable our steppers
7 ^# Y9 O" F& z5 X) {( a3 |8 n  digitalWrite(X_ENABLE_PIN, LOW);
2 B. H/ W  R( P' l& C  digitalWrite(Y_ENABLE_PIN, LOW);
7 U" u' K+ i' E- o  digitalWrite(Z_ENABLE_PIN, LOW);$ c9 g7 g7 C. \% j
  digitalWrite(U_ENABLE_PIN, LOW);; b0 t" l$ `* G) `+ |+ R6 v" `! P
}6 [$ B, ?9 }4 v" _( _' L

" \0 u" D' |  B% k! F2 L1 m: Y' N5 _. h2 q, k
//绘制LCD. v% L  d! G, }. M0 v5 m- b8 Z
//unsigned int DRAWCount=0;
: ]) _* k5 ^9 {! fvoid LCD_DRAW()
# l% ]( O: L- S4 E9 E% W( H" s- m{  f- E6 O. h- h9 R2 @! h- E# Y
    lcd.clear(); % F' I9 a8 U; E
    lcd.setCursor(0, 0);
/ K4 Q. ^9 I3 ^2 Z+ @, C" h  A    lcd.print("    X=");! V/ R2 G* ^" ?0 y
    lcd.print(current_units.x);
/ [1 o; C1 V" X3 A: c    lcd.setCursor(0, 1);; I8 i) b  {+ F* A0 J8 b& }- u1 o
    lcd.print("    Y=");
0 [9 E$ c2 ?% U2 R, p, c) ?    lcd.print(current_units.y);
1 v! m" f) H- {( t8 e  H  V    lcd.setCursor(0, 2);. o4 ~7 i3 P" k# O! k$ B7 G
    lcd.print("Z=");8 @7 Q; Y: A" Z; T% T9 i
    lcd.print(current_units.z);! z: U. X1 Q# u# _8 ]
    lcd.setCursor(0, 3);
6 {) w; G- S3 Q; [' w8 ~% R  @; h    lcd.print("U=");4 L8 F3 {( v0 N5 P& z
    lcd.print(current_units.u);2 f* A! B- c& `* L; s" U% D
  }3 ^" g/ X. s% g1 I) R( I
// else if (DRAWCount>=30)DRAWCount=0;. M4 b' F+ b: S& l' Q/ O& S3 s1 }
//  else DRAWCount++;
, N: i( v: k% ^, l. p- G! o void SPEEN()
# d# @5 l5 T9 R; p. Q$ F4 { {! |. J- o" r9 a/ Q
   delayMicroseconds(analogRead(SPEEN_CTL)+1);" h, P: [1 o; Y! D) ?
}
( G2 }7 C  E8 W9 l) W1 k! `
2 R& y+ f& c! w- `; _
3 X. B7 G; m. v% X //delayMicroseconds(analogRead(SPEEN_CTL)+1);1 V: y: U( l1 r; r, N$ O
//if (digitalRead(BUTTON_SP_EN)) SPEEN();
# [  E9 R1 H; P" [3 S; a" \' m* K! w; j7 ~/ K0 P* F, D

4 U; v; b$ L, }' n( G  P9 y# D8 h
 楼主| 发表于 2014-5-10 09:58:04 | 显示全部楼层
基本原代码的大概是这样,我是断断续续捣鼓的,玩到最后版的,后面我会找出来先测试下过,再贴上:)
发表于 2014-5-10 10:48:17 | 显示全部楼层
请楼主发一份给我,感谢!178354773@qq.com
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Archiver|手机版|小黑屋|机械社区 ( 京ICP备10217105号-1,京ICP证050210号,浙公网安备33038202004372号 )

GMT+8, 2025-6-19 05:25 , Processed in 0.077517 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表