机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 24091|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑   w5 X+ k2 V* F: z6 e) _) q
& a' ?8 x5 h9 L5 X# m0 X" m: u" d
首先声明; x  y2 Z5 A4 I" C8 T3 ~
我不是专家,业余捣鼓. A$ V* a, t! q
源代码首先来自网络. U; ^, u3 x/ \: t5 k9 ~: y# D
开发平台Arduino及Maple0 e; N  M% g' y. a
http://www.arduino.cc/
  q, }( G; C4 H- r* g- o8 U# [http://leaflabs.com/devices/
- n2 [5 N9 @  W! j! j: T9 ^5 |国内活跃社区
+ [; ?! O. p3 d. Khttp://www.geek-workshop.com/forum.php
& ~& S! }+ f- ?1 n
0 _, d0 [6 o* C. ~* f竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验* N" p- ~5 s) |! I
来入门了解熟悉思路架构等+ Q( H6 g% n+ M2 \- n

! K1 G9 B! U1 F0 {3 _8 B" D我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,6 A0 I3 R5 Z" _9 L7 k& W8 u6 J
许多功能还木完成,不过作为低档次得应用可能还有可能
& |" U& m  o! G/ ?9 }% {# V- G, x
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧6 I' _9 g& [: `9 ?
8 X1 }1 _( s/ ~6 J, c+ h
拒绝 所有的求1 R2 e# v: ~# g% E: i
求人不如求自己 不然木玩0 s- O8 ^( E# R* ~' B$ @9 n

7 T- N# ~" b# C- ~) Y' e高手绕道 谢谢!% \7 a% R% F. L0 c1 h- B( S1 E) V/ F
  S- S+ w$ v$ [% l
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 ; p' I& N* D$ u; h5 G

$ [' V) b* j) d# H1 C+ SGCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东
- o9 y& c: m' V贴代码先
; y. s$ {7 a7 O8 T9 s/ d2 n. N直接Maple的,某宝许多超便宜哈- a3 k/ f3 F, f5 X% x/ f
晕,我怎么上不了RAR?& G/ G. S) s2 t0 d3 g
想玩的留下 e妹吧7 l5 L. ^3 M+ w3 i
第一个妹麻烦传第二个妹哈4 Y' p" s6 ]) P+ o8 j& ^
我平常杂事多0 T5 S7 g) T% A4 j2 ^# F# p1 d9 O
谁放网盘上再,麻烦开放下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap/ ]  H- S+ A0 g4 V
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
, M' Y; Z2 K" ]# Z8 r; v" r5 `// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)
/ i# v" j5 m' B: @% I) ?2 J// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)7 L$ l) u: T' M% ^& y
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
- y) v! K' y3 R/ N$ f2 L8 {
" o/ A7 f+ ]7 g( Q/ _// Arduino G-code Interpreter for Macro / Micro photography
0 N* B  f, G8 {0 D3 t* _// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
* Y" k& J% l) g1 x//modified by YaoHan China 2010.2.15
% j7 k- Y  v- A8 g0 p3 Q& N//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
; {$ h# D' J4 d6 b8 h' U7 t% V3 O#include <stdlib.h>3 k. @8 j$ s$ c% K) p. S: E
#include <LiquidCrystal.h>4 p6 [  O: d! X1 u1 C# K, J, S4 C
& Q% t, w$ p1 \) `  K' ]
//启动(高电平有效)/暂停(低电平有效)  默认高电平: l, P% ~, {. _$ @" O
#define BUTTON_CTL 35
' U# K; E0 ~$ Y3 A9 O: L: o+ L3 Q' x0 j# m! e: ~. v
//点动模式键(高电平有效)  默认低电平4 d! C, W; M  _7 R
#define BUTTON_SS 36$ F4 j2 ~: V2 G! T% G/ f4 _8 p5 ?

; o8 N+ M+ a6 {( m* U& G+ p- f//点动键3 u- q% P8 I( ~. i) ~' z
#define BUTTON_MAN 15
' F" m) m) l$ H4 z0 D4 B" b8 ^8 B2 R1 j( p* K2 p. E
//电位器速度控制7 y0 w( k  B6 t8 ]
#define SPEEN_CTL 16& ?2 k7 C8 x$ m/ C

7 Y" [. W  e8 U- W0 D' Z//手动调速使能3 w. f7 {4 Z2 y. H
#define BUTTON_SP_EN 17
7 u/ M  P$ t6 }7 P' Z/ T( S6 A8 X9 o& ]/ i8 \" X# D
//LCD 类型 1604或1602
! K- M+ c6 d$ D; b$ j#define LCD_TYPE 1604& V1 _# S( g4 G3 d1 v7 d
//LCD 引脚定义; J$ g% z, i" t& a# [4 z
#define LCD_RS 23+ J) |& a) W; I. D
#define LCD_EN 24
& y0 o: o3 Z* k( \4 T* p#define LCD_D4 25
0 B. b" W6 e4 k+ B/ {4 N#define LCD_D5 26$ W1 ?' n- C2 V6 @0 j
#define LCD_D6 27
; S! N( R+ G$ Z0 Y+ m0 W6 r$ p6 S#define LCD_D7 28. k) O4 |9 [7 J/ [

' d; |+ @2 F6 I' u//命令字符串
! I/ }+ {9 {7 k2 h& N( P; X#define COMMAND_SIZE 128
+ y% f7 P: V1 ^" Z) h% u" m7 ~1 D) Tchar word_old[COMMAND_SIZE];* N$ b. z, N  x
byte serial_count=0;' b. F, j" A0 _# m& c
int no_data = 0;
! N4 Q" W$ Y/ Z0 D( n' j//LCD 引脚链接 配置
* C' \  W# E) fLiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
4 S# P7 G5 i! l4 ]! h8 V0 g( `//停止控制,高电平有效
3 v" Y; d2 q: m2 B) H6 B8 A$ y8 [#define BUTTON_STOP 303 j3 W1 f/ }7 W* \: m4 D4 \
int stop_flag=0;! z1 _8 E# E6 n6 R1 n, B- ~+ B: V
% N. P( U7 X1 s$ `6 r
//暂停, p* R  O5 T5 e' k: H" v+ s
void pause(){
$ d2 r7 f/ {/ b* N7 U# T8 X  while(!digitalRead(BUTTON_CTL));( e  H# f8 B% b! f8 i$ o3 c  p
}
3 w0 N, |/ L1 |9 F& Q# \2 o" w
  [* I* p$ V" Q. o# k1 b" ^void stopper(){
- [+ X+ ^3 y' I! Z& J% Q  delayMicroseconds(10);
+ z& ~6 K; I: ?3 X  stop_flag = digitalRead(BUTTON_STOP);
$ F( d- {( K& v' d! D}
1 A' ]+ o; e: t( |; X4 e8 t7 `2 |( o- l
2 K% W/ b9 `9 e( q4 ~" V. Lvoid setup()2 U0 B5 d! d) d8 E7 I) w
{
$ \% `0 Z! h1 |! _- p) }1 h  //show start
0 u$ V* m/ ]8 ?. @" K# [5 j4 m  SerialUSB.println("start");
" N$ ]$ o2 u5 U# n0 h  n2 O9 L2 e: n. y; |, v, x6 D: u* O' o$ c9 V- K
  //启动lcd) C3 X8 j# S3 Z- ]- [2 b2 u4 B
    lcd.begin(16,4);
- g- B4 B1 Z  M4 M! i9 T    lcd.setCursor(0, 0);, y0 E+ o* I5 |5 c
    lcd.print("hello, world!");1 v4 ^$ @" e$ Z! F) w+ d, ]

) M0 i8 d3 Q6 K. @2 M  g  //初始化控制引脚及引脚功能
7 a  N9 E& \2 \1 ~3 K+ u  pinMode(BUTTON_CTL,INPUT_PULLUP);4 {2 _- J# }4 R7 ^* N; H$ |
  pinMode(BUTTON_SS,INPUT_PULLDOWN);% N1 z# v$ `# T! s
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);) @* T, N! c$ C8 k
  pinMode(BUTTON_MAN,INPUT_ANALOG);5 {# k- O8 j+ i7 Q) K: A
% Z% m& F4 g; V. N$ r. [
  //控制引脚的中断* x; K) G% t" t, E( C# W  i
  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断
6 J0 J4 O" M/ ]! N  v2 D  attachInterrupt(BUTTON_STOP,stopper,CHANGE);8 I8 y( {8 o, A% k

7 o1 |$ K* O% Q% U2 r8 G  //other init
2 e# ]; ]# X& i/ f5 b( t; y  init_process_string();
+ d; r2 @: l7 x, o! m0 C  init_steppers();, N' X9 ~3 _7 y8 H: B& ^
  init_camera();
! C0 s/ r3 J" Q" y# ?
5 p7 V! a" y5 a) l4 |& }$ l3 Y( ~}
& K8 i3 v) e0 }; J' R& S( H
' L& L% h( p' n9 l0 z# l. t, D3 Gvoid loop()
) Q9 F: j* s' Z& r6 o4 A1 a; x{
9 t1 S& E. D9 v& E: M$ f+ q  U$ L" _  char c;
* A# G% b% t6 q2 h0 R6 E9 l9 f# H! ]) @# E  Y" o. p
  //读取输入的字符+ @% E. Z9 T+ \: e: h( P: P
  if ((SerialUSB.available() > 0) && (!stop_flag))
# A+ _' t# d% \' @# V  {
* J4 o8 c/ T' X* z; n5 z    c = SerialUSB.read();
6 B3 K6 N2 G+ @* _# o5 A+ z5 @" Q6 _    no_data = 0;( }, \5 V4 Y: {3 z4 I

# @# M, W3 q1 |, p    //换行符代表一个命令的结束
( j! H/ E+ H! p2 G. h. k+ `    if (c != '\n'): G" |% Q3 _: Q' ?* b& I
    {+ Q6 S3 E+ }; U9 J7 D8 x1 ~
      word_old[serial_count] = c;
& n. X) D& \7 u3 X0 g2 g      serial_count++;
! h2 y8 s9 K, p4 r' f) m% D; ]4 C2 o) x/ T& p8 u
    }6 v+ s; W+ g5 ]( k, G* e4 `0 Q5 i
  }
& L8 y. d' Q+ O. {  //标记没有数据输入" x: {0 U% G9 N9 F1 x$ {% [
  else, p* m+ K3 S% [! {& r% {
  {
9 L& I$ e) e& q    no_data++;
% l" I0 V% N& I& F: L4 J    delayMicroseconds(100);
( J0 D5 N* x) i3 H6 j& d" R$ G, |  }
4 I8 U" ^3 M$ _
  h7 k" I4 N5 ?& o) B  //if theres a pause or we got a real command, do it! _8 d+ G4 p- p/ W2 u7 N9 B* N+ G5 |
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))' x3 J* p+ b5 V+ {; H9 J' r% S, A
  {$ R  @/ r% s! X* a( c) H
9 p2 z# P% ~; \6 S0 X4 \9 E4 Y
    //处理命令
2 G- \$ K4 _7 s. v3 {    process_string(word_old, serial_count);% b: c3 o4 S% N+ }$ Q3 Y6 l
# G, {  T  R3 y3 H, w& R
    //清除命令
2 C/ U1 U, a$ s- u$ y3 E5 X" |# |    init_process_string();
% D9 F' J) r2 |5 S  c  }
5 Q0 F7 Z" i- \- K" B# V3 ^
! w3 U: E: V5 J) \6 G1 l/ o# p  //如果没有数据关闭电机
  O+ E0 l" n1 E0 L* Z$ k3 L  if (no_data > 1000)
8 S% G; |) ?% [, l' A$ S, A    disable_steppers();& T2 z: K8 `$ m
) c1 N7 e" l5 Y% ^( }5 n
  //如果ss键按下进入点动模式
0 h% o( T! t: K+ e, v/ ~  //if(digitalRead(BUTTON_SS)) ss();
4 f# d$ T* B) f( G  run_a();
' n0 j" Z0 o7 F- x  N, H- l}/ i  I. u0 S7 h1 b
3 z, o* y  X' q4 j
//点动模式
+ k; a7 _- {6 Z/ A% D' Z9 ?void ss(){; C9 A; e0 I8 J. U
: F2 X4 O5 y% J0 t8 m' i
  delay(1);
* u# H: ?3 N$ N+ H7 V! l  if(!digitalRead(BUTTON_SS))return;
( X( J% d$ N) c- E1 k( d+ V# R  if(!digitalRead(BUTTON_SS))return;
3 f( X( T: r' K, o  //init_process_string();
# r  _& }% o) `. ?" C  ?" F+ C) z  //init_steppers();8 ^: p) r3 M2 {# |; x# @
  //init_camera();2 F7 B) h' P2 M/ V4 o8 V) Z

' e6 A  O) R8 W // SerialUSB.println("Step By Step Mode");
4 K6 b8 g6 U0 {% o, ]//#if (LCD_TYPE == 1604 )
% ^# B, G' `0 V$ ]" C  //当LCD为1604时显示要处理的命令
* r! U" R8 u5 `8 v8 B   lcd.setCursor(0, 0);& w) \1 M5 `8 S" i. b/ m
   lcd.print("Step By Step Mode");
. ^' ^; `( ?! x  R+ a//#endif0 U; @  ]. P5 Y3 }6 _
  process_string("G1 F5000",8);
- ~( f& ?* \  r+ i  process_string("G91",8);6 B' u! Y+ W  t2 H+ r5 I- ?7 s
  //init_process_string();
+ O9 K2 \, g, X8 h! S: t& y0 B( m0 ~' f+ ?
  while(digitalRead(BUTTON_SS)){
- }1 e! b$ j" M* ^* L, p4 I( a" f    int i=0;
5 I$ R/ E0 E- c    i=analogRead(BUTTON_MAN)>>9;
. |% j4 z8 b( j/ N7 L5 g    //if (i==0){break;}% I5 l, J7 w3 l) G8 ]
   //SerialUSB.println(i);
# ?4 E: Q* {( w! \   //delay(1000);   
) \) _$ I4 {& k4 ?7 m. a" o
1 `8 u& R9 s6 R   if(i==2){; f; w/ I! _% o+ K4 l
      set_target(1000.0, 0.0, 0.0, 0.0);
/ `* R$ }0 ]+ k' X/ [3 m      dda_move(getMaxSpeed());* R2 k9 B$ E& C/ a2 z' x
      if(stop_flag) return;
+ W/ F: E$ L+ }7 `+ |9 U9 `  i      //process_string("X0.01",5);" u8 n1 h& U. y$ K2 r  {
      //init_process_string();1 Y2 h4 C7 k( C. r- m3 T
    }
: c0 t" E) |- W7 ?* e2 }8 `    else
! W5 p" O0 X, a6 A) Y' d1 E0 l    if(i==5){, N, S% m- l) Y3 r7 D
      set_target(-1000.0, 0.0, 0.0, 0.0);6 ~! G+ P9 A! s( d' m1 D
      dda_move(getMaxSpeed());% h* k" v: L: ^9 H* ^& S8 q' H
      if(stop_flag) return;8 V# w5 V( o1 \. Q7 h) G# R
      //process_string("X-0.01",6);* g& }. l- E, V- x8 O2 o
      //init_process_string();: k4 B$ V/ w# {% f* m8 l6 O% l
    } # |& \8 V' \" v+ W$ w
     $ o) W4 M1 R, g( W; \! }# g) d

+ T  Y! u2 @$ Z1 \  }. ~# y) T  F) s5 G" h6 \' G. y% J. l

. V- J3 R' k9 t" a: U& ]- x! x  //init_steppers();
1 t3 T. I3 T! v7 h/ o  B  //init_camera();
: R8 n% c. @5 H   // SerialUSB.println("Return To Normal Mode");
8 _) |- T0 Q) y1 O# g/ U% E     process_string("G1",8);, X4 ]) d$ D) K3 O5 P& ~
  // process_string("G91",8);+ N0 r1 V/ G1 y1 C2 P) B3 d& s
    init_process_string();7 t9 E2 p( v% Z" Q
//#if (LCD_TYPE == 1604 )
$ a  E. f# h8 s% ^5 m  //当LCD为1604时显示要处理的命令
+ n3 T# V' J* ?& `% F) X2 H) c/ z$ [6 `  // lcd.setCursor(0, 4);
6 l8 Z1 y# P4 L( n* r& p, Y  // lcd.print("Return To Normal Mode");
3 W" m& |6 [9 |5 p/ c4 i//#endif: v# f- E6 ?1 T2 M7 S# [
}4 Q+ f6 \2 q4 l& e2 P8 U

' U$ |% G# O' w- y0 q/ e5 zvoid run_a()
8 G; ~. C* s8 ]/ q0 q' I7 R- C{! D. v9 C* K- b% v8 e! @$ p0 j+ \
  //delay(1);2 N8 I. p0 V1 b* r% [% ^% r
  //if(digitalRead(14)== HIGH)return;
" S: g/ c6 }( S6 [# F  //if(digitalRead(14)== HIGH)return;
8 s  J; I8 f( o9 u1 i- N5 ~  //process_string("G1 F2000",8);$ D) s: s. `3 v' b: g. U8 G. i! i9 l
//process_string("G92",8);5 s! q+ X( X) A; x4 t
process_string("G90",8);2 b' D$ ?; b) U" g1 M3 X
process_string("X120 F10",5);+ r7 E6 T( t6 U" s
process_string("G4P2000",8);7 i+ _6 w2 Q2 Y
process_string("X10 F10",5);1 s6 J2 R2 ^: O  d$ H7 ]  f3 u6 v
// process_string("M101",8);
6 [5 p8 A& h4 l5 z* v+ m' A// process_string("X-50",5);& w* u# f% T1 C# w
dda_move(getMaxSpeed());
% ^, S* A  U& J$ I init_process_string();
* A6 k  t" }" Z. N$ b5 O3 k1 H7 U: w9 C/ @3 V+ F* j
}) r0 K6 t; S% o5 t1 D) `
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:53:25 | 显示全部楼层
2 l$ P6 c( ?4 f" }0 |
1 }+ _( ^2 j6 W1 k# }! R' A
// 定义机器参数  o) p0 r3 X8 j1 I  d- s' o( t7 B" C& _4 P  v
#define X_STEPS_PER_INCH 4008 F1 q# b& B: Z
#define X_STEPS_PER_MM   16.04 H. V/ E$ u# E* t
#define X_MOTOR_STEPS    200
% ?" P: m0 ?7 }$ ]: u2 ?- |$ ~1 h4 i* Y  ~7 [
#define Y_STEPS_PER_INCH 400.05 U, G0 v" b! x6 S
#define Y_STEPS_PER_MM   16.0
" y0 v$ y! p- ~( b& L& z9 T#define Y_MOTOR_STEPS    200! ~& a3 R' t" I9 y
  V6 l8 \& C; d1 N! v( }
#define Z_STEPS_PER_INCH 400.0
3 ~6 y! m, L" i6 {) h8 \1 K; l6 `#define Z_STEPS_PER_MM   16.03 L' l- B: O- Q5 t2 d( ~
#define Z_MOTOR_STEPS    2009 M, p4 g4 V* \7 c
, j8 f/ m7 n. g& N
#define U_STEPS_PER_INCH 400.0
3 p+ h; V7 \+ H# Z#define U_STEPS_PER_MM   16.0
) u3 @9 h1 b1 G+ o#define U_MOTOR_STEPS    2008 t6 R& |) B; P9 q# ^4 A! q
" T9 J  _$ L, W6 P4 \" s
//最大进给率$ ^5 n5 W6 u) t: P
#define FAST_XY_FEEDRATE 1500.0
* y- s+ o" |  O% M8 n9 X#define FAST_Z_FEEDRATE  1500.0- b( O% \& h% ^" z
#define FAST_U_FEEDRATE  1500.0
* C8 g2 U, @( T2 L& a/ _2 {// Units in curve section  J# L$ I% I  ]6 Y# u
#define CURVE_SECTION_INCHES 0.019685
) @$ l, A/ o) C#define CURVE_SECTION_MM 0.58 C& H  }  @$ O' k) }

  a% C0 t. I0 Y9 D& w  m; u, A
. m9 t" o5 G0 Q% a/ `; y" s6 P2 V// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)! o9 O# d2 R6 N6 Z8 P  I
// RepRap opto endstops are *not* inverting.  T6 q/ y% g1 \/ ^( t0 L! p
#define SENSORS_INVERTING 12 g% I: R) h$ u  d

' d; O7 ^" Z5 C- v6 b: u" p/****************************************************************************************
0 q/ S- A. X# q" P * digital i/o pin assignment
1 i8 K2 _. z7 h; g  |8 W *) u* {. s; I6 K5 ~2 F* q0 F/ N8 d
* this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-53 ~9 h, `  _9 G
****************************************************************************************/
8 }' h) ^  t. N5 @3 F. w% q! i. x# N/ O: j" ~
//camera shutter and control pins( |) d6 F+ b+ o# q* q
#define CAM_SHUTTER_PIN1 29% q/ I6 i+ b+ n0 D
#define CAM_SHUTTER_PIN2 30
& I* h$ V. w1 n$ s( G/ x) U//#define CAM_AUX_PIN1 31 // analog 0
9 I2 c; ]6 e8 ^& ^/ ~  x//#define CAM_AUX_PIN2 32 // analog 1
2 n+ k4 I# V4 a2 _# d# z//#define CAM_AUX_PIN3 33 // analog 2+ r* K7 L- u( z. b' Z
//#define CAM_AUX_PIN4 34 // analog 3
+ }8 W4 Y1 `# i# w& G  P. }  }8 \  p- \0 t
// stepper driver pins' X5 h. f5 D6 _7 s" N
#define X_STEP_PIN 7
. |! _1 K9 [, P# @4 n#define X_DIR_PIN 8
6 t6 a' M- |' L0 a8 Z#define X_ENABLE_PIN 19
  Y, h* s, \' B1 O
9 O# d, h0 e7 ]# g0 Z: y: w#define Y_STEP_PIN 90 _! r) M' ~' F5 U6 j# W' \' s* f1 W
#define Y_DIR_PIN 10
+ q# {) _; P$ U' N: l3 s+ e7 \) x6 \#define Y_ENABLE_PIN 19$ N. B5 b5 K" n, R3 N3 z1 h
+ Z$ {. X' Q/ U9 A. f; U' Y! [
#define Z_STEP_PIN 11- u" B5 s7 ~: g8 [; G5 y2 H7 o" k
#define Z_DIR_PIN 12) L0 ~7 K- P2 T7 s
#define Z_ENABLE_PIN 19+ x' V6 t- p% [% ], ?
' E( j3 L( p9 t, M: G  u% u
#define U_STEP_PIN 13
; @, f5 ~. r4 v$ ]3 I0 f7 }- O#define U_DIR_PIN 14
" X/ w, {' g6 o& [#define U_ENABLE_PIN 19
$ _8 C8 x) [. k+ P# J
6 `# y; ^8 r5 v8 G// limits not used right now8 c: [' `5 s4 v0 e/ w6 z, h
#define X_MIN_PIN 14) ~, b; }6 `( J7 x0 t9 q
#define X_MAX_PIN 14
2 O8 v* c$ ^( I- G! o#define Y_MIN_PIN 149 F- j$ `2 ~; X
#define Y_MAX_PIN 14; l2 }# C3 U8 G1 @3 F, Q
#define Z_MIN_PIN 14+ o$ E0 U, ~1 d' F9 t6 a$ m
#define Z_MAX_PIN 145 e0 k# ?! N; c8 P3 b  D
#define U_MIN_PIN 14
7 m& R1 a" L3 i% d, [( p2 j- q/ {9 _#define U_MAX_PIN 14+ G3 u5 f  I2 r/ n5 \9 V
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()  i( W- f: Q+ z; X9 `6 d. b: N
{
4 o* {+ H; a) o1 K7 t  pinMode(CAM_SHUTTER_PIN1, OUTPUT);
$ F$ s5 m2 P4 z2 I* f9 b- Q  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
5 y3 y7 J: }' N( {2 t- V  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
; [. o* l4 d' H& T // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
% l) R" h8 w& U, b& H6 t0 w1 w // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 29 d$ J2 m/ P( B
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 37 |9 Z& l" ?' g3 R1 f! }
}# O6 L& O% f+ f& M) X" w7 e- K
9 ^% t3 e4 e" s- I+ n
void camera_shutter1()
/ R- N8 u. E1 T: G{
8 V+ Y. `% m# y0 Q0 e) n: p" c  // fire the camera shutter via relay...1/4 sec hold time/ ?& w0 x& y0 q2 h
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
; |$ R, B2 [* I8 l% y  delay(250);& d+ ^1 X/ J4 p; Q
  digitalWrite(CAM_SHUTTER_PIN1, LOW);+ N) M- w* j! T8 y. R/ C

9 o. b. Q* s' s; B% W$ @: ^- R8 ^}! A  H0 W" k$ M0 c. {9 V5 {
8 ^/ k" y/ \" l( S, Q$ c% D1 Y6 i
void camera_shutter2()  X! I1 K6 O% {2 t7 `- `1 f7 Y
{
, a3 N9 I1 S$ |# J* i  // fire the camera shutter via relay...1/4 sec hold time) P. R% B2 A9 F: V9 B! H5 F
  digitalWrite(CAM_SHUTTER_PIN2, HIGH);$ Z1 j9 Z4 Z- C/ N  H
  delay(250);+ Q6 c& u3 k; q: l
  digitalWrite(CAM_SHUTTER_PIN2, LOW);* N' s5 v+ @/ {& z; d( J% l3 i
* Q2 ?$ C) G  w4 s0 r' L
}9 a  J, U# v% I& l% K5 u3 x8 S
/*
0 \8 T) x7 D! m- L. O. C  E& k- ~6 Lvoid camera_aux1_on()
" e  p# v% g7 j& f{
; n  i8 D) g/ K: T, D( q* K7 J7 \  // turn aux relay 1 on0 k( T# r( [4 J7 n9 M" P$ u
  digitalWrite(CAM_AUX_PIN1, HIGH);
- A8 _$ E/ R8 c$ v9 o}4 g' `2 v, C2 x, l

$ F4 j  @* R- d) s! Kvoid camera_aux1_off()1 q/ W4 Z8 T* o: }' }
{* I9 @8 _1 y' v5 u+ K
  // turn aux relay 1 off& @, h' d: {& Y' K+ N
  digitalWrite(CAM_AUX_PIN1, LOW);8 p, P+ c( z+ \$ s4 z4 ^9 ^; U
}
. Z7 d0 H* U9 {+ I% t' @. d; f% f4 X: C
void camera_aux2_on()% v( B  T" o! {1 }
{
2 g5 w+ Z" w' B  // turn aux relay 2 on7 }' F" C: _$ J4 J4 _8 F
  digitalWrite(CAM_AUX_PIN2, HIGH);- g. M7 [/ Q- J: {
}3 o2 O/ Z; c4 q  {. T. F8 l7 F% d

# k6 \$ G4 d+ Q" {& nvoid camera_aux2_off()+ e) o. _' Z6 u$ g# t
{
9 ^3 H+ p- X. E% ?0 z  // turn aux relay 2 off$ J! E7 J/ y4 b* I& S) j& B  ?* ~
  digitalWrite(CAM_AUX_PIN2, LOW);
* M' P( Y4 |1 o2 Z% k0 o. \}
9 G- P; v. N  q% \. ]
1 f  E- r: g$ U9 `4 S9 W9 Uvoid camera_aux3_on()4 @4 M9 L9 l* j" D# j
{6 _* x) I' a, Y/ C# }7 C" R, ]
  // turn aux relay 3 on6 s5 K' G. e$ W  y/ d, s
  digitalWrite(CAM_AUX_PIN3, HIGH);- Q8 n$ N* h) V% d( a7 G
}; c) s6 U5 Z$ c

; ~4 |" u1 ~6 y; b% R1 `' xvoid camera_aux3_off()
3 |9 m6 W( w$ i  s{
) n! P1 o9 t' e4 N. c1 [, L  // turn aux relay 3 off
. ?0 t3 q( K+ h! G- v) v. z  digitalWrite(CAM_AUX_PIN3, LOW);
0 n# @) e6 K$ W5 o. d}  T/ a# V' ^* @. ]! n) N7 U: {
3 Z1 P3 b0 |' b) y$ d' |
void camera_aux4_on()
& L5 z2 k! f2 `8 r  _{
+ L6 S2 v* f# m' s  // turn aux relay 4 on
# E  E. ]. Y( a+ N" q* S$ _- s/ @7 A  digitalWrite(CAM_AUX_PIN4, HIGH);
) s0 Y4 N% \* t8 b( D}
7 k) ?/ G: ~, v+ I( g+ H2 I; J% v, K- m7 s. W6 ~4 d
void camera_aux4_off()" z2 L0 }$ Y& J3 u/ P
{' x& g7 [1 `' c8 f; P1 J+ ?
  // turn aux relay 4 off* J  A, f  y& o" N( `
  digitalWrite(CAM_AUX_PIN4, LOW);9 s* U" E3 I+ \3 S
}
回复 支持 反对

使用道具 举报

发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错2 C' P1 |3 l8 E% d
请问楼主是玩什么的,用乐高玩具吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice." ]# h( n, q; s5 D
struct LongPoint {
9 Z5 m3 r. q* |+ Y! ?5 \  long x;
6 k1 r1 r& d% [- z! }  long y;4 T9 S, x5 D& U9 I
  long z;& P% A: j6 t3 B; c2 y0 _, g
  long u;4 ?) O" A% s5 H! T2 ]4 l  E
};
6 u9 P  k- H" }7 U: C: Q/ c9 J% o4 D+ f/ J6 S0 Y1 u' G% j! \# Q
struct FloatPoint {
/ D! {, Q- x7 U0 R' L0 g  float x;8 M( e/ w' ^* r' S0 @; e& r" F; g
  float y;" N: `7 ^+ P( m7 [3 C
  float z;: h% R! A: s+ Q5 b8 w
  float u;
* a) X" r# e1 W3 v2 W};( }) u/ k" U5 \
# u: O! E0 j" f4 @$ ]
FloatPoint current_units;% Z1 a2 t5 D, @1 T% a
FloatPoint target_units;
/ n7 l- Z+ H* B5 d" X2 ]FloatPoint delta_units;; l1 v- @$ l. p% w5 F& b$ |1 v; q
* L) [+ ]/ c7 B. j9 t/ ]# B7 J# _
FloatPoint current_steps;
( |5 I) o- T2 q& a: `: f8 SFloatPoint target_steps;
; X2 p, ~2 @8 I* u1 p1 @FloatPoint delta_steps;. s) l% l& V2 u" _' e

% Y% J5 f! ]7 u  V5 `9 I+ D  x4 p( Lboolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式
3 w0 j' C4 V9 K4 k8 P+ R
8 |6 Z8 r0 D/ n; `' a& n4 f//default to inches for units
8 ?% y, e) `7 S. `float x_units = X_STEPS_PER_INCH;
7 \! X' P1 ]( b: {# wfloat y_units = Y_STEPS_PER_INCH;  S) f% ^' ?, `! C9 d, B8 w. i
float z_units = Z_STEPS_PER_INCH;
+ z. S, H. f! D) Afloat u_units = U_STEPS_PER_INCH;
8 e6 [9 i1 ], e" Z/ Dfloat curve_section = CURVE_SECTION_INCHES;, h: P4 q% I1 `; j+ E! G

3 o; p8 e7 M/ l' f- O//our direction vars' [" B, J' @1 ^- w& C. A/ ]7 C
byte x_direction = 1;
9 c5 d1 `. W" b" Hbyte y_direction = 1;+ r3 @$ D8 D  A' _" S8 U8 ~" r
byte z_direction = 1;! t0 B/ v7 z' v. ~  E0 S; ~3 [: K
byte u_direction = 1;( F# c" z8 J. R  F
# n5 R' C1 K8 t- Z: ~9 Y. ]
//初始化字符串处理. d7 Q, K* a3 E9 ]5 u. p7 d4 Q
void init_process_string()
- G/ B! }% k: S{
' b7 f  P- \7 p4 s+ R  //init our command
3 t3 a+ d/ l. I8 x) d6 n  for (byte i=0; i<COMMAND_SIZE; i++)) W. t- j/ Q# z, R" s4 Q' g/ u
    word_old[i] = 0;
* s4 p" h; A! O  serial_count = 0;
3 C, [" i8 C" j& S  t& ^8 X4 N}
- D' d1 L7 L! D& `9 M, [: [, a# @0 T6 T1 v
//our feedrate variables.( ^  t; }" n4 j: _5 }. q7 S
float feedrate = 0.0;
! o9 E# g0 W. b8 t9 |3 V9 ]- G& Glong feedrate_micros = 0;% h% u( x* a: \1 v- }; Z
% Y# b- c& a4 W1 L+ ]
//读取并执行命令1 R) y- e& C. p0 O9 y5 L1 g
void process_string(char instruction[], int size)6 x' l& f( ]) q; ]0 ~1 ?
{
, g6 ~+ n6 a# |& G# O4 e1 C  //the character / means delete block... used for comments and stuff.
+ e, I  m1 z! F& S5 c  if (instruction[0] == '/')& w9 L  x- `1 u5 A( ^9 x5 h
  {
9 ~; O1 U) i, B/ C    // SerialUSB.print("ok");7 G4 n- X  ]& H3 y7 L9 L+ r
    // SerialUSB.print(byte(78));: Y9 A. B4 P: C
    return;* p3 _( H3 k( A( y) B6 z6 H# m* |
  }; V2 O9 g5 M3 w7 u& J( f
  //init baby!) o+ i) t  v0 Q9 S
  FloatPoint fp;
* ^' \6 }7 r- l9 y& {  g0 T* n  fp.x = 0.0;
* n) d4 p6 U8 z2 [6 l* g* O  fp.y = 0.0;
7 G* D  O# L: M/ A8 X/ y7 c" D  fp.z = 0.0;
- n$ Y; D& F+ Y# V: V* S  fp.u = 0.0;5 m! Z& K. l+ {* ~4 J" H
" `* q. |7 Z0 N  n% g
  byte code = 0;
4 ^; S, v+ B% u  P* m8 R9 j$ S5 [' ?+ D
  //显示在处理的命令/ V5 i, o3 w  I' x8 q& C) q
#if (LCD_TYPE == 1604 )
% e/ \- A# o8 {) _" N: \  // lcd.setCursor(0, 4);, D' l" B& V/ m7 S2 {
  // lcd.print(word_old);9 V# r& C; {) G" [6 Z" _5 q% c) N
#endif% |' @7 H, b5 U, C+ K
  SerialUSB.println();9 u% ?: c( E5 c" X' Z8 \8 Z; \
  SerialUSB.print(instruction);
2 q9 H  I9 g3 y2 ?7 B  SerialUSB.print("\t");2 I4 m( F6 P4 L4 ^
& w# c/ Q( E3 \% }7 @: e
  //what line are we at?
/ k3 z) K& U0 W  //        long line = -1;
) U/ h) I2 X& {1 Q  //        if (has_command('N', instruction, size))& R6 u) |4 S; u  `( q
  //                line = (long)search_string('N', instruction, size);
. p0 l1 V( x! ~$ v: d! D, N4 w1 y% I' x8 ^& \
  /*1 u2 e2 I. P4 T
        Serial.print("line: ");5 K( A+ W- n8 ?: M$ G! B
           Serial.println(line);
3 E  y; }* R  n2 _5 o/ B           Serial.println(instruction);
1 v/ x8 K; B1 x( L2 |& i8 R   */
9 r+ K! `  j4 Y% M" I  //判断是否读取了个 G代码?
% G8 S. M# V# o" y  if (
3 o, A% M/ Y1 C  P; f5 f4 e    has_command('G', instruction, size) ||
. ^' P2 w  t8 e! n0 M    has_command('X', instruction, size) ||1 u9 `$ o& w$ k. l. e. P3 n
    has_command('Y', instruction, size) ||
  e5 R. w# c1 @" f3 A# S+ [* X    has_command('Z', instruction, size) ||
! |% k& [' ~; X- I( D6 }    has_command('U', instruction, size), u! o* Z/ |! O( x; _. q! ~6 B
    )0 h+ a7 d7 j" Z  Y4 H
  {  H+ i9 j5 A6 k2 k9 l  K9 O% e0 G
    //which one?
; Y" Z! w& l+ \8 L    code = (int)search_string('G', instruction, size);
" t  @7 ?& o" ~5 I    // Get co-ordinates if required by the code type given6 w: x) t! i9 }  a! b9 d
    switch (code)
6 s# m* _0 Q' F) w  z    {  S3 i% V+ c7 n* O$ `
    case 0:
- d/ Q$ N/ f$ M* C% W& d    case 1:- v1 U4 Q& _8 ^; x
    case 2:
7 M; a% E. k/ x4 K    case 3:$ o1 Z0 |2 V; V7 m' B. t
      if(abs_mode)
, u. d* k2 W2 @0 X7 y      {
' O! f7 T& w0 u8 ^, R+ w5 N        //we do it like this to save time. makes curves better.8 m$ ~1 }) k- ^7 v9 S" D3 k
        //eg. if only x and y are specified, we dont have to waste time looking up z.
0 c3 a3 W' ~! F: p' D        if (has_command('X', instruction, size))" K) ?, P9 J! d( s
          fp.x = search_string('X', instruction, size);% @7 a& h: D' H' h3 A
        else) y2 D  J* x! j) Y
          fp.x = current_units.x;7 \' [2 _1 D0 ~( B2 g

# z( x! L: z/ |) y+ p; b9 i        if (has_command('Y', instruction, size))
! J. E, L! u% k% V0 Z& v          fp.y = search_string('Y', instruction, size);
, s: W7 k. Z# `* m        else, s6 G- a& K* m  J9 M
          fp.y = current_units.y;
0 @$ @+ q* f# g+ k; Q& k( o4 F
7 ?- `* Q% U! ?' _' N! `- G        if (has_command('Z', instruction, size))) i6 b3 P, w+ x0 G$ {
          fp.z = search_string('Z', instruction, size);6 V1 C" g: m# R( G& C2 z
        else/ A2 ]. v, h) s% f2 s6 Z
          fp.z = current_units.z;
' Q; }6 ~6 K. {7 }+ T          ( p0 L! I5 \! k* t( N+ C9 p
        if (has_command('U', instruction, size))/ a9 N' u/ Q5 S8 p+ q: V
          fp.u = search_string('U', instruction, size);3 o0 v+ F' C  P6 x' `
        else
$ z, [' `* T% P4 F, k          fp.u = current_units.u;4 F# t. g  G& ?# X
      }
7 [# ]/ D7 x, ]3 p5 m+ y      else; [# B2 F. V9 d$ J
      {
2 H+ h; b9 w% d( y        fp.x = search_string('X', instruction, size) + current_units.x;
; W, j/ s  c  `# C        fp.y = search_string('Y', instruction, size) + current_units.y;
' q0 B/ N3 b) \1 N# }        fp.z = search_string('Z', instruction, size) + current_units.z;/ ]$ e5 g7 w) c5 n# q: C
        fp.u = search_string('U', instruction, size) + current_units.u;% ~4 i. M, k7 j7 O
      }2 j$ o9 j& a/ @3 P1 }
      break;
; k2 I9 ~4 {6 y+ m& U7 J    }
. s8 |% \3 i. W1 \# D2 m; t0 f3 \, @    //do something!
+ A- U  R3 ^; p  g$ N! j( ?& D    switch (code)  j0 q) t2 I5 q
    {" [  U( F# r( i+ c! ?5 f
      //Rapid Positioning+ f6 q3 L+ P: S$ X, a0 L( M6 V
      //Linear Interpolation
4 S, e9 @4 U5 y% ]* z3 @7 B      //these are basically the same thing.% |3 m1 B! W/ h! U" n
    case 0:$ r* u5 X* G' g5 F9 r# |) X( T
    case 1:# e( S" U4 V- q" j6 w8 A
      //set our target.
$ f) p8 T' ^% X. O1 n$ \      set_target(fp.x, fp.y, fp.z, fp.u);
6 U) b) G8 w. T/ |! M: L! B; E      //set_targeta( fp.a);$ J) S2 `: X- W% f. D, {
      //do we have a set speed?
9 ~$ Z. H% O) I1 S$ J: ?. q      if (has_command('G', instruction, size)); U, Q/ p3 t! u9 k5 T& m
      {; a) m2 [$ }5 H
        //adjust if we have a specific feedrate.4 F/ w; S3 S6 w; A/ w" F
        if (code == 1)3 ]! l9 k- n0 D& I
        {
$ F2 `/ h. b7 w. c* t1 s          //how fast do we move?  C8 P# R& V) ]: U2 @% b2 a9 Q
          feedrate = search_string('F', instruction, size);
$ j7 x8 j% e! [# l7 |          if (feedrate > 0)
, Y% r" q2 _/ Z" a! [* z            feedrate_micros = calculate_feedrate_delay(feedrate);8 C* i3 I1 @, s$ A' }! P9 Z. u
          //nope, no feedrate
1 x2 U& f) N- b) M: b          else3 Y7 o8 \# ]2 b# f" G+ ]7 t
            feedrate_micros = getMaxSpeed();3 n9 q+ `' f0 Y1 T4 j
        }
" d) ^* ^1 o/ T) I9 m, C        //use our max for normal moves.! F2 Y' a1 z% }5 g, r/ z5 n  M
        else( M; v) I8 S' }8 @& {! {" z# i0 s
          feedrate_micros = getMaxSpeed();$ z  M4 O6 x: w1 e/ {" ~
      }( \, U7 [( h" K( g! e) i4 k
      //nope, just coordinates!
! T8 e! ~3 g% U$ ~, Y      else
. U% q# N8 y0 J- H8 U( l      {
7 e% L. E7 ?6 c2 s) f& m8 X        //do we have a feedrate yet?
+ L+ }+ O/ k' s        if (feedrate > 0)
8 M3 A% J" y' W( s. c2 N          feedrate_micros = calculate_feedrate_delay(feedrate);
% Z/ t  J1 V& v/ w- d0 D5 J" O4 n        //nope, no feedrate8 n2 ~$ G) f" V5 s3 s  p) y2 t
        else
5 e  O3 R( z( A* i* [/ G& w+ Z( x7 x          feedrate_micros = getMaxSpeed();' I1 R  {6 M1 j! o, q
      }! o& w' {/ G" b* h/ u
' W5 s8 j; w  g2 J9 c% u) A) @
      //finally move.
; g  m; O" v0 J; @! g" B) J      dda_move(feedrate_micros);
0 T/ }; P8 X  p0 ?! b& p' t      if(stop_flag) return;! R2 T$ H9 x9 H* `7 f  J8 C7 Q
      break;
. I. M) [& N/ ]; r  L& B: \# V* c
& c4 D; Z0 Y% c- i( Z      //Clockwise arc
1 E" ~  _# ]) ~- b' T    case 2:7 c' Y" w" p" T, n: z
      //Counterclockwise arc
- o0 u# M- r* A7 `. j) x    case 3:
0 k/ r# d+ u1 |9 N& F      FloatPoint cent;8 z7 d  _: `) |5 j/ ?0 b
      // Centre coordinates are always relative
! G% E9 Z& G& F& y" t      cent.x = search_string('I', instruction, size) + current_units.x;
: K3 l, p$ w% I4 t  C3 r/ o      cent.y = search_string('J', instruction, size) + current_units.y;
' X3 _+ i. J6 R: h5 A% w      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
: Y- A( @1 l: _# B9 O/ l  C$ x6 ~* i, ?3 U# ?
      aX = (current_units.x - cent.x);# A+ L+ T2 B  @9 A* H: c
      aY = (current_units.y - cent.y);& Q" ]. B4 C# e/ l
      bX = (fp.x - cent.x);3 G2 k$ f( z1 Q# W& S% H: V
      bY = (fp.y - cent.y);
  A, {" F. _6 _/ Y
* }' O- W* k6 B* O      if (code == 2) { // Clockwise+ V* W9 V8 O, W) d9 q2 F! U
        angleA = atan2(bY, bX);$ F8 @" A7 y3 s8 k% G9 _0 i
        angleB = atan2(aY, aX);8 O; a/ d! P. a2 L2 f$ |
      } ) d$ z1 |; y, `
      else { // Counterclockwise. X+ j+ a6 v( w( @4 [" ]) e: V
        angleA = atan2(aY, aX);  W" a7 O' s, K: T- j9 k6 v4 ]3 t1 @
        angleB = atan2(bY, bX);4 l7 V4 H# P* {8 s6 w1 t
      }; Y& G9 }+ _0 D+ A! f8 Q% e1 T2 B
      // Make sure angleB is always greater than angleA, J  Z) T) ]/ Z* r2 g3 B1 @
      // and if not add 2PI so that it is (this also takes2 x. ^; i( L9 m5 I: N
      // care of the special case of angleA == angleB,
7 |" j( F% t/ [  q4 b+ k      // ie we want a complete circle)! g7 {: ?# A0 Z
      if (angleB <= angleA) angleB += 2 * M_PI;
+ M' B2 t2 c3 d+ L" K      angle = angleB - angleA;/ m+ w2 b8 l. b# a: Y+ E# Q: o
9 F9 i0 s- a3 }
      radius = sqrt(aX * aX + aY * aY);
! H; j. Z" s3 B4 R7 O      length = radius * angle;3 K% B" C8 B9 t6 i" B# S2 l: S
      int steps, s, step;
6 H8 }4 y. T2 G      steps = (int) ceil(length / curve_section);1 l1 q! H: e  U; S" N( u
* m0 d; R7 u% s& T: A1 [
      FloatPoint newPoint;
! @4 T% Z& F% u/ b( t6 Z: L: X# ~      for (s = 1; s <= steps; s++) {7 k" R& p; T5 S" }8 C& @) p
        step = (code == 3) ? s : steps - s; // Work backwards for CW- L' g2 ?$ [: }5 I9 J- w% z
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));, K, N: w( y( z1 Z* N
        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
9 I: r' G3 p! P2 m        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
/ G  A/ s6 x3 f& k2 J% h$ J( k$ n8 d$ R" \8 x
        // Need to calculate rate for each section of curve( V: k+ H5 A4 n! [  i$ e
        if (feedrate > 0)( r# S1 y0 c1 d7 ?- }
          feedrate_micros = calculate_feedrate_delay(feedrate);. c( v: c* C# V, Z# F0 j6 y
        else) p) h4 H2 a7 D
          feedrate_micros = getMaxSpeed();8 \- h& {1 x% p( z$ D
' n1 r% ~5 w: r# \4 d
        // Make step7 X, N, f( E4 J4 F+ X! J: }
        dda_move(feedrate_micros);
' _& `! i. w5 g" T/ g: F        if(stop_flag) return;
- V+ _% d$ Y- R3 v; Y: k% _- Z      }4 d7 t3 z) @0 z$ M

( o# [9 j- x: h6 T3 a      break;! R- ~& F! k  p" t" M- m7 o

3 s# Y$ x& [3 F8 p6 q8 T6 G2 q      //Dwell
. J3 f; A3 l9 c( m0 O- t" x9 G- t    case 4:
' {8 |8 l  C" i3 V5 Y+ ~0 U1 s- m      delay((int)search_string('P', instruction, size));
3 |0 O! @( T; Q3 S      break;
. D& @, A+ C' R) L7 N/ J$ S
9 c- \6 l- _3 ~9 Z      //Inches for Units% V0 @$ C6 M! ?9 }4 M
    case 20:" ?0 J( A* k& z2 U
      x_units = X_STEPS_PER_INCH;
% \9 ?, U5 g9 B; b3 ?- z      y_units = Y_STEPS_PER_INCH;
4 Q! r4 M: H3 b0 ]9 ?* d* L      z_units = Z_STEPS_PER_INCH;! I  u& K8 }( m  V, ~( m& h9 R
      u_units = U_STEPS_PER_INCH;
  t! ~' ?# y0 r2 X$ M8 d      curve_section = CURVE_SECTION_INCHES;9 f. I& [; C7 i6 X
      calculate_deltas();0 @( @9 X4 g& h$ F% F( q5 `% K/ V
      break;
3 }8 f6 t( L' H' [  h! @" t4 i/ I- I0 ?0 g8 y
      //mm for Units
' {& a" V2 a/ ^( n; e    case 21:% z) t6 Z) ~7 m5 x/ ^* q
      x_units = X_STEPS_PER_MM;
* M. r* h9 o9 Q2 z      y_units = Y_STEPS_PER_MM;* z* g) Q4 K7 [& v9 h
      z_units = Z_STEPS_PER_MM;  ], z. @$ N- J7 @9 w* f/ z- i
      u_units = U_STEPS_PER_MM;! F! @$ O1 }* L1 u$ e
      curve_section = CURVE_SECTION_MM;
3 }4 {/ m" j' u' g# D" H      calculate_deltas();' t# e& Q: T) @6 ]2 t
      break;! s. o' N( U2 l/ E
1 U4 Q8 S, I. Z4 B
      //go home.
: o6 k( E% e1 z2 Z; B. G5 X; `    case 28:
" s% e/ W/ f6 C& p- x' l( {' l      set_target(0.0, 0.0, 0.0, 0.0);- h8 l$ v' }- b# j0 d
      dda_move(getMaxSpeed());
  {& f% u! U8 k! h      if(stop_flag) return;" v6 T1 }9 m1 I( C
      break;
* }3 N8 Y: {- x$ Y4 I
6 T9 g1 h9 o7 x  Q7 j      //go home via an intermediate point.
2 x9 v$ ~  b+ o0 m( s& j  E1 z    case 30:; R5 O* F" [" k* e6 R
      fp.x = search_string('X', instruction, size);  G: F4 z: S# l5 X' T2 D
      fp.y = search_string('Y', instruction, size);' Q+ ?4 U) Y4 f- y
      fp.z = search_string('Z', instruction, size);
% e1 _$ _& X4 t+ P% J      fp.u = search_string('U', instruction, size);
- p1 |$ @  [  U4 g. t      //set our target.
" P2 _& b& V% L1 H" E9 ^& X' r& L      if(abs_mode), ^+ [+ w9 m- u6 p, c
      {
* O9 @" i  A; X4 b! @        if (!has_command('X', instruction, size))6 w& t+ x$ t, _# d( y
          fp.x = current_units.x;+ K4 O4 h. j, J) S# y
        if (!has_command('Y', instruction, size))
& M2 g  ?( N& ~$ T          fp.y = current_units.y;- t# e: x4 }: A* ~3 r+ P7 s
        if (!has_command('Z', instruction, size))
) f/ G2 f( p3 L& p1 _# k          fp.z = current_units.z;: `4 V) M8 W# I
        if (!has_command('U', instruction, size))
7 i4 x) v: u; m+ ?# J+ B          fp.u = current_units.u;
* G, f) S; y7 V; H        set_target(fp.x, fp.y, fp.z, fp.u);
7 G) }" c8 f9 _9 T/ T+ q        
/ u4 O  }; Y  k" [! B      }
2 o  ?) H2 M, J+ X# |% ]( _      else  N/ i3 `8 j+ ^8 k
        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
! X4 G- J2 L; V7 j6 S/ G       9 O4 s' P' H9 N$ ^' J4 O
      //go there.: f% c; _$ v1 }
      dda_move(getMaxSpeed());
" N2 K/ r* D9 p1 J      if(stop_flag) return;
& t; A5 k6 L. e- C# t0 Q! V
" t* c( ]0 D* V3 p  x) M- P* w      //go home.
& |6 Q% V$ M4 o/ X: G8 O( }7 A  {      set_target(0.0, 0.0, 0.0, 0.0 );
8 H' v# J" u' {     
6 o# H4 M* u3 F( R& R* w      dda_move(getMaxSpeed());
0 u5 n  }7 N& @% r      if(stop_flag) return;  _$ R, l. U1 I( k8 [& i
      break;
3 V% K; F% t7 Q4 V. N5 F. o/ I
# N7 O3 _0 a  t! R      //Absolute Positioning. j8 G; Z7 m8 `' P' a# S  l  t  B# w
    case 90:8 {4 R4 f8 B$ s5 ]: p
      abs_mode = true;& A6 L  }# o( Z' @6 y
      break;% F) {3 K0 e* W. J

, U+ {8 u/ k8 e      //Incremental Positioning) [. q, Q( z$ C# A
    case 91:* x  [( t! S% K& h  t
      abs_mode = false;
6 E7 ^) E, h2 M, J) t3 H1 d      break;4 `) ?5 b# T  T' e  ~% N
7 i! f  p/ {3 W- F3 ]* d
      //Set as home3 s& ~* j! {/ H# ^
    case 92:
) e. h# O8 P6 Y2 p& L9 x5 [" v     ; D9 o! _$ D' I' e8 M
      set_position(0.0, 0.0, 0.0, 0.0 );
# e/ z+ b8 ?; G0 @      
: G! O4 L; H9 d  ~  Y      break;
3 i$ G% G) Z9 v# P2 o: u$ H5 h( s7 g2 p3 V
      /*) u: m" R( N4 ]9 y3 D; Q
                        //Inverse Time Feed Mode
  r2 n, S: `) ?1 x                               case 93:
6 L$ `& ^8 U- I' ?' B# }      
2 q4 V. d7 H$ N                               break;  //TODO: add this
1 b7 R! u. m, g- Z      
4 a6 m- X. ]& j6 U( o7 p# n3 n                               //Feed per Minute Mode( _. c! w0 e# o+ P
                               case 94:
+ \% I9 K$ ^6 y0 ^       ( c3 B) d' S0 K& J4 `
                               break;  //TODO: add this, C; ~% U( ?8 B  }' i4 v
       */6 Y+ I) s. O9 |' m5 r$ i
2 Y0 S+ H. P9 O0 }, |/ A! e9 @
    default:
* G! [' o, t+ m% F8 c' Z8 Q      SerialUSB.print("huh? G");5 K# U) O# S7 \2 \
      SerialUSB.println(code,DEC);$ e4 ?8 A9 T; i3 R3 ~, Y3 J
    }
$ o% @( b  u% l& b+ I8 G) I9 G& h  }
8 B( ?1 r2 V  B+ Z+ F/ A5 h  V. E0 L* e# R, s7 _( @! k5 @3 N
  //find us an m code.
* d" I0 K& V& t  if (has_command('M', instruction, size))6 b4 Q3 ^0 W0 w& J/ v+ `
  {3 z# I5 ~* a3 E4 H4 m7 `/ M
    code = search_string('M', instruction, size);! `' R+ N3 J- X7 A2 u+ [+ g
    switch (code)7 Q4 n% g0 y4 r) X) G
    {
* C. s! d$ g$ ~' F! r/ W      //TODO: this is a bug because search_string returns 0.  gotta fix that.. C' {; z+ _) ~1 t  ?* x
    case 0:
4 F+ U! _9 N+ q2 G. i2 g8 a6 O      true;
8 C" t. Q" c+ l* c0 U4 E' e- h8 `      break;, Y$ l3 k+ I+ i+ _

) x7 V! }8 |4 b" k& d. K  P    case 100:
  s& ~$ [; g( u: v) w4 O      break;
3 N* r! q5 {$ ?
! G, T/ N0 g; E% f+ O2 w      // fire camera relay
: n1 B# F8 ^. a. b) @& [( d3 Y8 I    case 101:
: X/ C- ^2 k: V/ I) ~      camera_shutter1();
8 L) A: Q9 m" ]      break;. J7 V% r  F" x

- W5 M2 h' ]9 `; _3 L7 U$ b      // fire camera relay2. \9 z  s! D: _8 c0 N  L
    case 102:
' m1 ]* V6 I2 J& d      camera_shutter2();
/ o7 c; Z0 |) N5 k! o5 t0 T6 g3 K      break;8 R" C) }8 N& x+ o
/*& n7 m5 g: c6 N) f7 d& _
      // turn aux 1 relay on" z& A8 R* P& }
    case 103:
8 ]3 N0 J$ W+ ^" m2 j8 Z      camera_aux1_on();
( \& O% P5 a1 @2 T      break;
# U2 r( W! \3 ^0 M, i" S( \( [) t
      // turn aux 1 relay off* R2 e2 q* V8 n( ^
    case 104:1 k* a2 G* s) u
      camera_aux1_off();
4 M; S9 A; F$ p- O9 k7 O. u      break;6 t' \, L- E& u2 K1 n# X* m- p) r
7 c" z+ F8 l+ ~6 t- c! W& ~
      // turn aux 2 relay on
) w. _- Z5 f: j, @9 Q    case 105:
4 k" p5 k+ o0 j, H  a: e/ ?) W) W      camera_aux2_on();
  u/ n2 @& H: o! Y9 i      break;5 a& }0 J# y" e7 \! f+ x/ _: q

$ O" c8 |- d! X4 g! [0 a# Z6 J* n5 W$ s      // turn aux 2 relay off
2 z; A: O1 d% t; Z9 ~, ]    case 106:! z1 a6 Z* l$ B* `7 O! Q; Z
      camera_aux2_off();( B+ d/ b( V: i8 \0 A* |4 C
      break;
4 Q+ P8 F3 x. {7 |* |6 r& h3 q9 W; v& r/ u& ]2 r5 g5 ?
      // turn aux 3 relay on
! @- u/ d/ J( `6 z    case 107:# n/ i5 r* o1 H
      camera_aux3_on();9 {0 r0 ]9 ^  O6 O' y
      break;
& Z/ P, a, V+ c0 ]' g. U; z
& ~( ?$ O# c. Z# |      // turn aux 3 relay off
2 e3 w+ D; P" U' R, [! \9 @    case 108:
6 i7 d1 s* J' H3 Z      camera_aux3_off();: h3 x. k* J: `" F& k% f( ~; {
      break;
# s+ U( r9 A5 q( o& A. g5 a: x' a4 Y$ l
      // turn aux 4 relay on
8 Y5 j: v/ C1 q    case 109:
! S$ w1 a& o' v% @1 @4 `( H      camera_aux4_on();4 {( d7 [8 C8 ~4 x- n$ p0 }9 ^) I
      break;
0 n5 N  o2 ?+ \6 T& O. B6 S2 k, e$ I9 f  q) W5 Y" L
      // turn aux 4 relay off2 n- n5 [4 G0 U4 W8 ~% }5 `' f
    case 110:7 |6 [, I. N& i* F
      camera_aux4_off();1 I' K3 Z: ?' g9 \
      break;+ J' b8 s. i9 v' d* }$ O- x; S
*/8 S* S( D% D: [3 o4 S
    default:; a0 k4 Q. b4 p+ F7 o5 t. q! m
6 H9 n; e* H" s! s4 m
      SerialUSB.print("Huh? M");: Z2 T8 H- ~: J9 y: l! O
      SerialUSB.println(code);6 W9 `% l7 A& s% g/ X2 m) v% w
    }
2 {1 `4 G: G6 d( k$ [8 B  }
- a) v; _2 R( f- k8 j$ ^0 X; N& @
  //tell our host we're done.8 T; z: k' }- ?! |) q
  SerialUSB.print(byte(78));4 j% g" @9 K. p+ b- _) |

; E; a) H4 d/ G+ l$ a3 r! m3 V}
0 D6 q* D% G+ [7 w# g: Z% {1 V) z; n+ F0 U( n4 A
//look for the number that appears after the char key and return it
4 E$ ~! |& d2 bdouble search_string(char key, char instruction[], int string_size)
# U8 B3 d' }  c) l{) k8 u" [4 T: o9 E
  char temp[10] = "         ";
8 {& v! \1 A6 y4 b& c$ w  for (byte i=0; i<string_size; i++). k% \) L. U# J' m* C0 [
  {
0 W+ C0 A8 l+ n* A+ n) r    if (instruction[i] == key)/ ^# Y( H4 S( J8 g7 o% r
    {  d3 _. I$ S# s" N. L4 z, g4 j- j' Q
      i++;      
* N3 j+ {  t4 |- \7 O      int k = 0;
% M* S- E4 j, u' v# w- f      while (i < string_size && k < 10)
5 y" W; P8 [5 {5 O7 z      {
$ f) f; {* m1 C8 H        if (instruction[i] == 0 || instruction[i] == ' ')
* ]' t0 Y3 a/ D6 a          break;
$ G6 {7 j7 r4 Q/ o' v' U
5 S* W) `$ `9 ^& ~        temp[k] = instruction[i];
& }! V: q# J9 T9 r; _        i++;4 A' V. V. W8 p/ R# u! a$ z4 j
        k++;/ \8 a. A6 ~% n; S
      }  F$ Y+ {3 ^/ o! l
      return strtod(temp, NULL);
0 X. G3 u7 r* C! z" F    }. Z+ F* L# S2 a! m4 q
  }: _% ?! D; t# Q5 E! B* d, x
% u$ N$ ?/ G' X% O0 y
  return 0;
7 Q9 z+ {. u9 w! r+ K4 Y' _}
! Z" m3 h0 P+ p5 h! V1 ?  M
* z! ]6 p; S$ c) V! [# f//look for the command if it exists.
: X6 Q0 p- }& b% obool has_command(char key, char instruction[], int string_size)
" A: f" W- X5 }" L& T( n7 ~5 f{
( U: r5 A* E* t: ~3 l4 B  for (byte i=0; i<string_size; i++)) q" x7 q+ z. j8 @" Z
  {+ p; _3 t; r0 |; h8 ]/ k
    if (instruction[i] == key){3 R. O/ f2 S! o: r9 s/ o5 M; d
. }2 l3 R8 R0 h' ^0 L
      return true;
; w& r4 Y: X) d% M    }$ G4 H9 M$ H" k6 i2 O
  }. H8 j* K; j: j( n

( [! Z. s  f" @. S  return false;- X# V* S/ x1 Y+ Q& X
}4 i0 j7 \* l8 |2 j2 `
0 v5 p6 t: [8 D' p  P3 {  p

; D$ y0 \$ B2 \( i, c3 j, Z$ D9 Z$ s  k8 ~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层
( X8 D5 X' A8 r1 z+ U' N: c
//init our variables  t' n5 ?8 Y5 y+ I7 g2 q/ \0 H; J
long max_delta;
- Y) C1 |! [8 K; G, Dlong x_counter;' w5 z; c' |$ L7 }5 R8 ?
long y_counter;
- t- Q( j7 s) ~1 q' t; Z+ d) xlong z_counter;
! F! l1 ~4 O, r, w8 ~2 u5 N; flong u_counter;) [' d) U, A* K4 O; Y
long x_pos = 0; // x position in terms of absoloute motor stepps
) S4 Y( m: }, S+ W6 X4 T% S# Slong y_pos = 0; // y position in terms of absoloute motor stepps+ L" y& @+ v3 [
long z_pos = 0; // z position in terms of absoloute motor stepps
) o- N: b7 L$ ]+ c* [long u_pos = 0; // U position in terms of absoloute motor stepps
: d( b( y! T0 d+ K% \+ }
9 u6 ]6 X+ D* L  l* F$ x3 I9 Kbool x_can_step;
0 y; v. ~, n9 q# o% b& Cbool y_can_step;
. v$ i9 Q: M8 `: H$ X* mbool z_can_step;
9 ~- k% b. u, Y1 v7 L4 ^bool u_can_step;8 h. n7 ?6 x5 Z5 g5 `0 A! D5 s( n# e
int milli_delay;
) h4 f  S+ f- k! ?4 y- E! P
  l( K0 B7 Y0 ?6 [% O2 Uvoid init_steppers()' G# _. T1 q6 s+ [
{3 b( P$ ]: U& |! o, s/ m& X
  //turn them off to start.
9 c1 E& q+ Y- v& v4 z0 `9 J4 V  disable_steppers();
. b/ [: c, U% g; A6 V1 Y( E( Q) o0 X2 U! p" n
  //init our points.5 C! m- T  w" K1 A3 S( H
  current_units.x = 0.0;
6 r; N# Z. |9 g  current_units.y = 0.0;$ [( ^8 O2 K+ x7 r8 _- z$ E+ g
  current_units.z = 0.0;
! B& _5 q: o: x0 Q  current_units.u = 0.0;: x( {; Q4 e( o* f
  target_units.x = 0.0;2 }  M$ S! k4 C# Z
  target_units.y = 0.0;5 f( \5 R8 s0 Q8 k) X6 S
  target_units.z = 0.0;
$ T& a8 w$ w. i; q9 l8 @  target_units.u = 0.0;: |9 H; c/ j/ F2 L; U. [# s
  7 P+ e. A; c$ Y; m3 E+ F

9 D/ K7 v: o5 G1 P9 p, M4 u  pinMode(X_STEP_PIN, OUTPUT);0 K; U1 z9 Z' @  g% A
  pinMode(X_DIR_PIN, OUTPUT);4 p" @) a# e% O0 k  j2 A
  pinMode(X_ENABLE_PIN, OUTPUT);  c8 ?' [2 G) Q1 A
  pinMode(X_MIN_PIN, INPUT);2 J2 A' J+ X# ~; F( g9 O+ h, D
  pinMode(X_MAX_PIN, INPUT);& b% d, q+ ~7 g! ^( }0 ?

, f4 J7 u/ I. B! U  A  _# c  pinMode(Y_STEP_PIN, OUTPUT);
- [. |  r  a: `6 S  pinMode(Y_DIR_PIN, OUTPUT);
0 K  T4 ^6 f4 v1 l; z& w  pinMode(Y_ENABLE_PIN, OUTPUT);
2 E9 z$ L8 w6 F0 c- f  pinMode(Y_MIN_PIN, INPUT);
  A: R+ ~! h5 ?6 E+ V8 K. k  pinMode(Y_MAX_PIN, INPUT);+ J, y. c; ~7 x: i7 U
4 J! B- ~4 H7 `7 ~* }
  pinMode(Z_STEP_PIN, OUTPUT);  O0 i; z% B( @+ [$ v% W
  pinMode(Z_DIR_PIN, OUTPUT);
5 ^! w& J9 Y- c5 U( D4 o  pinMode(Z_ENABLE_PIN, OUTPUT);
" D) u3 S" N4 S  pinMode(Z_MIN_PIN, INPUT);
: N! J9 N4 w# H5 \3 M5 {; x7 T  pinMode(Z_MAX_PIN, INPUT);7 T/ u" v7 Z; n' D0 E

& v+ }% W$ k5 W/ l6 a  pinMode(U_STEP_PIN, OUTPUT);7 C* T0 V* @' |* L' ^* p* @
  pinMode(U_DIR_PIN, OUTPUT);. k& F/ k- Q' Y+ g
  pinMode(U_ENABLE_PIN, OUTPUT);
3 i- {" R3 p8 U3 r/ O% _' ]  pinMode(U_MIN_PIN, INPUT);, `3 U+ {" n- m( I$ d
  pinMode(U_MAX_PIN, INPUT);. U( ?5 S; I/ z5 ?5 C. \% q4 _8 x
  //figure our stuff." ~! @2 z/ Z# N5 O
  calculate_deltas();
1 K- d' f5 z; C0 H}
  |: g1 L8 g& d4 @2 P! k  u, ]& {9 l: B4 k1 l
void dda_move(long micro_delay)
" M4 ^9 f5 V$ G% f; c{4 q5 C9 I* V0 N) f7 `
  //enable our steppers
/ J# V; H! |- M- {/ m  digitalWrite(X_ENABLE_PIN, HIGH);
+ _2 S" i: ?* A, o  p4 w! s- H! b  digitalWrite(Y_ENABLE_PIN, HIGH);! G3 [8 \0 N3 O6 B  E
  digitalWrite(Z_ENABLE_PIN, HIGH);0 {" @- W: _' E$ \2 A
  digitalWrite(U_ENABLE_PIN, HIGH);
4 J, G/ G; C; s" h6 s4 N$ b: ?9 ?: z  //figure out our deltas0 S5 v( ~0 n+ u4 }- f, H, s# `) F
  max_delta = max(delta_steps.x, delta_steps.y);' n8 L# t( V6 l
  max_delta = max(delta_steps.z, max_delta);+ X: W# ]% Q# @2 n& u" o* k
  max_delta = max(delta_steps.u, max_delta);5 H3 `3 Q, q8 }$ `5 a% e% L
  //init stuff.
& a+ M" _9 b' Y: H3 s% i% }  long x_counter = -max_delta/2;
! Q- P/ d; j/ |! T, z  long y_counter = -max_delta/2;4 q# C" ^  h) e8 n1 N
  long z_counter = -max_delta/2;
+ \9 p# y/ W1 b$ ^, o$ T+ N  long u_counter = -max_delta/2;
8 Q% I- E% J* o' v/ `3 b
. ^. l. T" U+ ^  //our step flags3 u9 a2 z4 v- J
  bool x_can_step = 0;
- F0 g$ p! p2 X$ R  bool y_can_step = 0;9 a) \) d; I; F# k' N/ H7 g! I; R: ?
  bool z_can_step = 0;+ e5 T) s$ L9 R$ v+ o0 N
  bool u_can_step = 0;
6 S8 u: b+ y" t! {+ t
( N. Q& z, F( Q6 }  if (micro_delay >= 16383)  G7 l+ m2 `, B7 L/ L
    milli_delay = micro_delay / 1000;
1 u* G9 w% t( a; u$ {, @0 F) l/ }  else, m! D; Q. }8 b) N3 v
    milli_delay = 0;+ J: f7 m6 h5 V0 @$ D5 T8 t% K
. f# u) R# {- y2 A; m( {' K" n

$ S9 Z8 ^! H2 g  s- _2 Q( `  //do our DDA line!
6 x3 b, W& _7 C0 o, l
) r0 p) s! K/ p9 s  V  m  do- @2 e# o- `7 G" V- r2 D! q
  {
( }2 H; @1 O1 ]. H4 P    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;' K1 h. N, L  ~, ^
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
, B! z0 O) u, P. [    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);7 T8 m$ Y# C3 U6 K2 K+ C+ _- q: E
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);3 ^2 `; s" S4 \* ^6 l. I
    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);! F! P: q- N( j% W
    if (x_can_step)
0 I, \. _$ w, ]    {" [3 I. A+ L. Z/ A( z) q3 ]- H- t
      x_counter += delta_steps.x;
" x) _  E" _) T: @- t& s& Q! x0 |7 e. v1 U. V
      if (x_counter > 0)
4 }$ s2 m% W9 o2 V      {4 q# L0 v: I0 [- x+ P
        do_step(X_STEP_PIN);+ s8 L; O6 u' K* ]* l
        x_counter -= max_delta;
4 T- g1 U; c6 E! [        if (x_direction)
, N/ j. c7 m: \3 Y! [/ b1 x         { current_steps.x++; x_pos++; }7 B/ s6 U& s6 I/ R- u3 Q
          ! j: R$ ~+ {& g7 o
        else% a; z, U$ B" `: C- O) q
          { current_steps.x--; x_pos--; }; E1 n3 x' D$ W
         - Q& X0 ]+ w: T, l- i: n3 ^; V
      }5 H. \, H4 |+ ~% l
    }8 q! J6 K$ j4 ?7 U1 @
    if (y_can_step)
) y# Z; z8 m4 p    {" S5 G  W' |( |" k5 i* F$ y' D
      y_counter += delta_steps.y;0 y% c6 t: h! ?: ^2 w4 `! ^

% r6 X$ p$ \' q/ z1 ^/ |      if (y_counter > 0)& w1 N6 I  `6 K6 n
      {" b$ \3 ]" \/ b( w# Z9 w+ H$ S" t
        do_step(Y_STEP_PIN);
& G% m& d2 ^( M0 E. @: F        y_counter -= max_delta;1 D5 d) n& q# x+ R" \: _) K3 Q

! {+ _8 r& f  u1 z! h, |4 M        if (y_direction)
! d  v3 o9 e! R, v: ?$ j        { current_steps.y++; y_pos++; }
9 j0 u, k" y" W9 t' G: h% i4 q      
4 s' u0 n3 ^. F& a: K2 W1 j% ^        else
! z7 Y* v" x# w% A# s        { current_steps.y--; y_pos--; }
6 G8 m  p' g4 D4 P0 u! O3 B/ p/ y        
2 ]5 D0 }& e9 R1 K- _# r      }- Y5 {( h+ ]9 }2 X$ p/ {" C4 S) P
    }- I+ k- L& z3 g+ d# K' `

) l' N2 u1 Q* [* J4 s' d    if (z_can_step)' E6 l" W- H1 }- ?7 J  w
    {
8 r. s* n+ F0 Q9 a! O      z_counter += delta_steps.z;
6 C) c, A5 O9 J( K0 ~9 `
  B( |, W, B& J9 ^0 t      if (z_counter > 0)
. N1 k+ j% G9 ^/ K8 o* D" M      {
' y8 P0 `. f% z( H        do_step(Z_STEP_PIN);
% q: |+ v9 O* k) l6 s' x( q+ J        z_counter -= max_delta;7 G" N% P7 ~( \2 ~1 J% N; I/ X
) Q/ N6 B# R5 K- ]) _
        if (z_direction)/ V: m5 }, w9 D+ M( b! @
        { current_steps.z++; z_pos++; }; }' r& }! v) B+ M8 @6 l5 A
        
, u5 t$ ~, R0 U; T) Y* v4 ]8 G  D        else
, G0 x+ C1 O& B$ e& k  b        { current_steps.z--; z_pos--; }8 L5 `% Y5 B  P: e/ T7 K, ^: @5 D8 X
        4 Y/ X1 e/ ~6 V) X
      }
# x: x% y& d( x# m/ ~    }
; W7 L) A6 R8 M    / n) m5 T; H' Z9 Y3 h7 w
    if (u_can_step)" t. O. k  F- v+ B% P
    {
4 |5 `6 M4 m$ L) Z: \      u_counter += delta_steps.u;6 j/ H6 T! h8 a- A! w3 M0 q% d6 {7 \

, D8 i  d1 V, U% Z      if (u_counter > 0)
1 t( m2 g, {" A( x" y* r5 E* k3 N      {4 r. }$ D& J2 f8 r0 d. n: t) X3 L
        do_step(U_STEP_PIN);) ?9 b* X0 M% m+ w
        u_counter -= max_delta;3 s! m" p6 S7 [3 X

# H9 a8 _1 }# p& A0 r3 e# Y1 x1 s        if (u_direction)! R. m* ^- I5 T5 K* e" Z3 n4 b
        { current_steps.u++; u_pos++; }
+ `9 j9 I7 l5 k0 J          5 ~5 F! v; ]/ k0 e& c
        else
7 l0 M3 J3 x- a7 C/ X- j2 z+ q$ ^1 m          { current_steps.u--; u_pos--; }4 W; _( m' |) T( E1 Q/ l3 y! H
         
) u/ x$ g5 l: v      }
% j" l" w8 s# n& Q: n/ C    }
2 b6 e! v+ |& r$ Q! ]    //wait for next step.1 ^) M! L$ z$ c7 B0 Q
    if (milli_delay > 0){
. u& }9 N* h, A  _, K. @: S5 @1 q      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
+ C& H) A; i1 Z+ O* G5 y- y      delay(milli_delay);
* u7 T" [1 i5 z0 |  m    }                & k, d& I; k0 g8 ^* z
    else{, b! \( A6 k4 W9 |# ~2 b
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
0 }/ u' ~( d: a7 {# B; n      if(micro_delay>0)delayMicroseconds(micro_delay);' u4 S+ e) n% n+ h6 W7 Y& V/ O
    }
' l' ~+ I4 R$ d5 p" R3 T    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);# k" J+ R; N. q+ q  R
  }% K+ n. j& {5 |
  while (x_can_step || y_can_step || z_can_step || u_can_step);
# O% i9 O% \" ^& M
3 ^2 S% j+ K0 x) `& a6 G6 S" K2 p8 a, m$ g5 v1 i  U  g
  //set our points to be the same
7 v" c' o9 L8 r  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
5 m5 O, x7 S( f* U  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;1 u( p& t/ w: u5 @) l
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
- M8 d+ z4 n2 \4 C& f" N# G  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
/ s4 L$ p7 \0 ~+ r0 m. q! |  , l( ?9 Y/ _" |
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );
6 l/ x3 v7 g8 x0 Z 0 X6 x% }7 n4 A' B7 D
  long x_pos = 0; // x position in terms of absoloute motor stepps( T7 G* n/ o$ n3 E6 ?  h
  long y_pos = 0; // y position in terms of absoloute motor stepps7 Y  B' Q, d. d2 Q2 Z- \
  long z_pos = 0; // z position in terms of absoloute motor stepps
/ ]- u5 q# S1 t; w  long u_pos = 0; // u position in terms of absoloute motor stepps2 r6 G$ {4 A0 T- L/ o
  calculate_deltas();$ J% N9 g# z' K" O4 @/ |
  ) \6 J$ S3 r8 j$ f5 N
}- T3 }4 `( h' n1 e

5 u% Z5 A7 A! W9 tbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)( d' Z; i# B* ?  @2 H+ r* Z0 I
{; Q, T7 R2 p8 G' @+ B  ~+ F
  //stop us if we're on target2 @+ @/ q0 W. P. R
  if (target == current)
$ {4 i" m5 `3 B" @    return false;
8 z. ]3 |/ Z( C" C. W# f  //stop us if we're at home and still going
, R2 W+ X6 u  |- V% R6 A- c  else if (read_switch(min_pin) && !direction)' |% S% I- B# B5 E
    return false;5 C% e; y7 O; C5 |! ?) h2 b
  //stop us if we're at max and still going
' [! A3 ?2 x* g6 a& `  H$ Y  else if (read_switch(max_pin) && direction)' Z: X- `$ I' a9 ^
    return false;" h+ R, q( W8 e5 [. A3 S. A
# ?! u; y2 [$ L+ t6 t% f
  //default to being able to step
! X+ r' L6 H" `. A4 M  return true;
9 V4 K: a' O$ W}+ z. C) J' Q* [2 n8 l( w" {4 ^" f
6 k0 W7 c* R: E7 ]! L& V- l
void do_step(byte step_pin)
. E8 _! I- f; R{* t. `" R# a9 u6 H$ Y2 {
  digitalWrite(step_pin, HIGH);! x' \4 s& e. N) b1 H, n9 E
  //delayMicroseconds(1);
, X0 b" K6 K" p1 w. X4 w  digitalWrite(step_pin, LOW);
( \* E# B* Q/ m  x' Y, Y}  j% ]+ C* g4 F' z

& z. Z6 J1 P( M4 O; _bool read_switch(byte pin)* h+ s5 w1 r, k! h) H8 X
{0 |4 i! H, O/ O" S/ b- u
  //dual read as crude debounce. u/ f( M+ I; ^3 l1 }

3 t: c. Y8 g2 s6 p6 C6 I: C  if ( SENSORS_INVERTING )
& O" T) i0 w6 r6 ]2 E    return !digitalRead(pin) && !digitalRead(pin);4 Y8 C6 T. H8 a( N, G0 J
  else4 s% j$ L3 A: f  Y
    return digitalRead(pin) && digitalRead(pin);
1 c. |2 M" F" n}
7 Q6 I1 {+ S" z9 K/ E" m
( `) {- ?/ Z. `9 Zlong to_steps(float steps_per_unit, float units)
; M, ^5 I  t1 T( p( _! E{
. B! G1 p% d/ b$ ]6 m1 K( ~  return steps_per_unit * units;$ f9 F2 y1 w% ?
}4 h1 m, c+ h8 g9 J& Y/ @: I! |

/ t% V9 @- s% h6 K! y+ s5 Jvoid set_target(float x, float y, float z, float u), z0 l* C  ^4 ^$ Z- [
{* U% w2 f& f+ V# V
  target_units.x = x;/ J/ ]3 d  a# E& w
  target_units.y = y;
5 ~0 H. ^9 ~+ r" m  target_units.z = z;
1 d. Q% {3 I; w9 K  target_units.u = u;
0 D& Y8 _" M; N  d  calculate_deltas();
4 c2 {; X9 E: q# `8 A}+ T+ F4 Q. `% D

8 q9 ]& x  w5 U( F. ]' R' xvoid set_position(float x, float y, float z, float u)2 |7 E3 E+ T2 G  L' s) D0 w
{2 V, I6 y/ q" ]$ Q+ r/ K  Q4 t
  current_units.x = x;
3 H! `/ A9 T  @& E( V/ S9 D0 A  current_units.y = y;, g2 R' T$ H1 V: _" \. A$ F3 h
  current_units.z = z;/ h& [8 m2 M+ d1 Q7 Y6 E: m
  current_units.u = u;
! z. Q" {/ u7 K3 {) l, n' r7 p7 F  calculate_deltas();5 }2 b/ E" s6 Q
}* Z+ J; Y2 b$ {, s( u+ F4 w

& ~, d, s5 E$ L6 V( b% K. l7 ?void calculate_deltas()
  I. U3 n) x  u& S! u$ V  H{! Z. F7 y8 w3 [/ ]/ M. c/ R* r) t
  //figure our deltas.
* S4 ~' W( N7 D: ~/ I+ A  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
( C" q8 B# b' B7 _  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);; h# G2 W" }' \+ M1 Y" z
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);$ \2 n; J! `* w" f, h- d
  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);3 p2 O- w( H: W
; @4 l" [6 H$ b/ l1 [# Y
  //set our steps current, target, and delta6 N' M1 p+ ?: B7 d
  current_steps.x = to_steps(x_units, current_units.x);
6 v) V- O7 k2 `  current_steps.y = to_steps(y_units, current_units.y);: [$ }) W  V- i* `
  current_steps.z = to_steps(z_units, current_units.z);
% d9 a0 Z' v6 N. _/ o! _& n  current_steps.u = to_steps(u_units, current_units.u);
$ u3 [7 w3 F0 s5 ^# {( D; V& k7 V9 F
  target_steps.x = to_steps(x_units, target_units.x);
' b: `2 g- i6 m, g- c1 @  target_steps.y = to_steps(y_units, target_units.y);
+ K- ?0 V# d5 b, x% I3 }  target_steps.z = to_steps(z_units, target_units.z);( h/ v# e. R. d7 n
  target_steps.u = to_steps(u_units, target_units.u);" y+ {) I  C$ L4 h

: R9 o1 C& R/ u% h  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);5 Y% N4 _7 C/ E5 ]! P# J
  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);) ?2 \8 L1 l/ c
  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
( |  p& L7 J! O6 r  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
' t; P# |. s+ `) t/ g
4 Z, R. v; f- e; T) T' P- K+ w3 `" `+ V
/ N/ a) u/ j7 g
  //what is our direction
; ~0 t; z6 f+ B, }$ L( c  x_direction = (target_units.x >= current_units.x);
5 Y* E% M. R; P# O( H+ w$ ?. F  y_direction = (target_units.y >= current_units.y);
  T; i0 V6 A  I& }8 S% {# g6 o  z_direction = (target_units.z >= current_units.z);7 G* z1 p7 g% m6 D: r3 V% G" R: w, ^8 q
  u_direction = (target_units.u >= current_units.u);% t7 t# \( P5 g
1 L  _0 p" J  g/ y1 q; ~+ z1 Z
  //set our direction pins as well
% b7 o, A3 D; \# i  digitalWrite(X_DIR_PIN,x_direction);
/ p% b: Q5 S. l- B  digitalWrite(Y_DIR_PIN,y_direction);
2 _2 F1 c# b0 X9 S2 S  digitalWrite(Z_DIR_PIN,z_direction);6 p9 K) d4 x/ a; d) O- z- {
  digitalWrite(U_DIR_PIN,u_direction); + |5 }0 D4 o# `. o, m

) C7 i! U' K8 p: o9 q  //绘制LCD
# z  G2 S$ G/ Q( R+ U9 K. M  LCD_DRAW();
( U; `. w; l$ z0 {/ ^
  o4 V& w( l& u6 G5 f2 U1 z: ?}
6 A8 ^& i) {8 S' r$ r/ Y/ x! o+ j4 q: x. {( q

7 l$ M9 @* ]$ ^/ ulong calculate_feedrate_delay(float feedrate)
$ L. Q0 C1 f6 R2 h  e& z  v3 K: R  H{
8 }1 e1 h7 s8 y; F  //how long is our line length?8 C; K- h, h  a7 V& `
  float distance = sqrt(delta_units.x*delta_units.x + delta_units.y*delta_units.y + delta_units.z*delta_units.z + delta_units.u*delta_units.u);
, d) p+ f  V* _0 [' ^  long master_steps = 0;. J5 J5 |8 F7 ]) _: q9 s7 C

% M2 {8 j, Z9 A9 ?8 Xmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
5 r7 q0 S2 c0 L" ymaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
/ C$ X2 Y4 a! Z" T  kmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;, r8 \  {4 _) N/ n" r- v# U8 P. l

- I2 r( y, J* b# a1 @9 s8 ]' _* x& M3 L% R/ D+ k

% x; c$ l3 a8 [" W 3 K) I/ U; c" k
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.
/ z8 j" O- c$ [% k+ J$ b8 C% }  //the formula has been condensed to save space.  here it is in english:
  J& T$ Y( b3 {- u  // distance / feedrate * 60000000.0 = move duration in microseconds2 c; [; U, @; V' p! N
  // move duration / master_steps = time between steps for master axis.
' d1 V, r0 K: F return ((distance * 6000000.0) / feedrate) / master_steps;
/ X* [& a# B' h9 \# k! G! |5 p+ f8 m1 c6 @+ k% t
}
+ c: z5 V) `) X  }5 D$ U8 n6 Z6 r  L+ G4 J' c: d* [/ ]9 U
long getMaxSpeed()7 L$ I5 a3 y. t' s3 Y
{
4 h4 M3 C* g8 I3 _) a3 |' @ if (delta_steps.z > 0 || delta_steps.u > 0 )" @) ^, b; h- h, b: h  D! N
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);
( r1 i1 z$ R, Z) [  ^7 c2 x  else3 C% w6 l! H  b: Q7 Z
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);
2 y6 N4 M; n. [' U# s}, E! Z! F  Y% X( Z

) a8 F# V; y. _3 rvoid disable_steppers()9 s; H2 Y. o% H3 ?
{; e% s: i$ |5 A; _
  //enable our steppers' l4 D. w, j0 f  V: S  i2 k8 `5 n2 u' t
  digitalWrite(X_ENABLE_PIN, LOW);  H# a  h( n; Y
  digitalWrite(Y_ENABLE_PIN, LOW);7 ?' o2 v' K6 c
  digitalWrite(Z_ENABLE_PIN, LOW);
, t" z6 G8 y" e5 [" x  digitalWrite(U_ENABLE_PIN, LOW);
/ o9 I& ^" P5 x3 h- N9 m}$ u0 Y$ B; S/ t6 ~$ {
) R  E* s" T# M& Z/ _

% [) U% \9 s, O0 v& w/ P//绘制LCD
; J' d, J$ u3 i* D//unsigned int DRAWCount=0;
* N9 L+ f; o. n! uvoid LCD_DRAW()1 T5 E7 N% R) J' m! a, l; s
{2 f7 r  r' n4 f" M3 Q
    lcd.clear(); + m# R# N7 `  r% G$ C" p8 e
    lcd.setCursor(0, 0);
' a" G& f" F% {5 H. B; f9 Z    lcd.print("    X=");
# w) V  _% B. P2 L    lcd.print(current_units.x);
) j; J* ~6 q, M6 _* d- Q! J- [; X8 C    lcd.setCursor(0, 1);
/ G" }( ?/ S9 i' G5 K% R    lcd.print("    Y=");$ E7 S* M0 N! g% f" R# B
    lcd.print(current_units.y);
9 N' t1 B3 A$ F0 M7 W$ w( H. e: c    lcd.setCursor(0, 2);
( g8 O: R) A- V    lcd.print("Z=");, M7 q- D/ z( e7 I1 M$ s3 ~" Z
    lcd.print(current_units.z);" z) c* v7 }$ y2 R+ g' G
    lcd.setCursor(0, 3);' M3 A7 Q! C% n8 I
    lcd.print("U=");
7 r2 ?; q6 Z0 E    lcd.print(current_units.u);
$ A1 W4 |( i" X2 f  e7 h' b  }
! T4 }  K) T; Z/ H) W8 H // else if (DRAWCount>=30)DRAWCount=0;# u9 b9 |; ~0 X
//  else DRAWCount++;1 ]" y  W: o6 A6 n3 c7 [! M
void SPEEN()
; ~+ C0 i; L. C# s% |3 ]) x {- w) G$ k: l$ A% c
   delayMicroseconds(analogRead(SPEEN_CTL)+1);" X* @7 V4 o& ?4 g% r) m
}
/ y9 i) V3 v! M" Z! m
5 i; w  X/ e2 O* g) ~! O# ^2 }2 z( w: n! g- V" g# p0 K
//delayMicroseconds(analogRead(SPEEN_CTL)+1);
6 G8 N0 H: i) k$ e- Q //if (digitalRead(BUTTON_SP_EN)) SPEEN();" n  U; j, q' z3 ^2 U
; P" P7 c8 V# a, ^+ z: E& n

/ F$ ^3 M+ ~% c6 y1 R
回复 支持 反对

使用道具 举报

 楼主| 发表于 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-4-8 06:31 , Processed in 0.082458 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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