找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 24361|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑 9 F" d) K) ]; M" n' R' _% F* a% G

8 P# `7 \- r0 r/ }0 p首先声明
  L) J" X; N, l( k" p6 j4 [, x$ |我不是专家,业余捣鼓# ?0 x( R4 M+ C
源代码首先来自网络
: V) k; s# W( D: j3 `4 u! [5 i开发平台Arduino及Maple* r6 T" g7 e% ^! g& D# y- K
http://www.arduino.cc/
+ y4 j5 E8 B( P- @http://leaflabs.com/devices/
2 S6 X- n% z8 }4 a* R国内活跃社区
4 y( z8 R/ L  t1 t. S( }3 ]http://www.geek-workshop.com/forum.php . k/ j2 k) G4 ?( K. G: Z; U5 c
) B1 J% U" T$ g- W& n
竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验
- e  l0 g$ O( A来入门了解熟悉思路架构等
* z8 n- T: o! G- `0 a! l; x  g# @
我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,8 \) w, F" S  F6 _5 n( H- w4 V
许多功能还木完成,不过作为低档次得应用可能还有可能
9 V6 N' o5 b" C1 ~8 G7 {- k' s  Y1 o
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧- |) b  O" `/ T& ^

+ D1 A! }. a" x3 z8 X  Z拒绝 所有的求0 C- G0 w& m/ x) p
求人不如求自己 不然木玩
- o+ Q5 S5 L/ p0 u8 K$ w+ V& X% k! \# `- E# _
高手绕道 谢谢!
" i- j; O5 c- e" J7 y; Y% r- s; J. M/ v
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 # N1 G; _, O2 {9 o8 \3 X; U1 P
9 t9 I( f' d6 O5 `! `3 ^; b
GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东6 v, S' g% N1 C2 G  l/ y7 Z& p
贴代码先
: H. R- M5 A% m; X: M, y直接Maple的,某宝许多超便宜哈
# g/ n+ B- p, {& H/ L晕,我怎么上不了RAR?
' A% }' H  T; T想玩的留下 e妹吧9 T2 y( R* O4 g. l" r0 U8 \
第一个妹麻烦传第二个妹哈
! Q' Y! R+ G: Z5 ]1 L6 j+ n我平常杂事多6 Q9 L; L/ ?& l" p' S
谁放网盘上再,麻烦开放下
 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap
) B6 L$ l& Y- V/ `) P// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
4 b/ t* i% q4 {+ g* `5 x  R/ L1 ?// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)' y; K2 l# j( t- D
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
, K0 A' F& C1 o6 Q7 B0 s" d3 y// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)6 U% o1 `+ K8 h2 }- |

8 r( s4 r1 |$ X! G/ S" V2 B) {  O// Arduino G-code Interpreter for Macro / Micro photography; ^& y* U! D2 F1 h
// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)+ P& |# F6 [' A( K7 y
//modified by YaoHan China 2010.2.15
1 B* V$ c7 ]& D9 s; \//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
+ b8 ]  C% k% w2 E' s) F" ?! W+ k1 C2 ?#include <stdlib.h>
+ n  {+ c& K1 }' N8 ^) O#include <LiquidCrystal.h>; Q# }9 s* c$ [% V9 U
- m& h" h  I* G" [; J$ S( f
//启动(高电平有效)/暂停(低电平有效)  默认高电平5 g' ?- v3 k* V7 \: h5 D& P# ?
#define BUTTON_CTL 35
  M; g1 _! r# x
$ m* w# o/ f; M3 i( \( n9 P//点动模式键(高电平有效)  默认低电平) _$ X) a0 s$ A3 s. m
#define BUTTON_SS 36) L# Y$ b, |( B5 w2 ~
) P# ?: }/ M# `
//点动键$ |& h# x2 ?$ Z. H2 Q- o
#define BUTTON_MAN 15! ?+ {2 D' D( M# M6 T4 E

. w% `) M9 P( k' m( w, @" U+ w//电位器速度控制
+ \& }# y# ?5 O, D* o#define SPEEN_CTL 16; U: i  ]) h4 j0 j: _

) [( s% D7 {% F//手动调速使能; `5 \7 s2 N- n8 a9 L7 j9 m7 j
#define BUTTON_SP_EN 17. |& m& M/ W) d- O# Z  o- J
5 i! y, w% b5 l
//LCD 类型 1604或1602$ Q2 M: k$ e  j, X
#define LCD_TYPE 1604! p& u* U  f) ]+ m
//LCD 引脚定义
5 d0 B* E  N+ J- j/ S5 V: @#define LCD_RS 23
& X6 l( g2 ]4 w9 x& b# h#define LCD_EN 24
: @( C1 Z+ S, O6 |" ?5 y#define LCD_D4 253 z' H. {0 Z0 C; W
#define LCD_D5 26
! A) S4 F8 }0 `2 e' Z/ |9 F0 l3 }#define LCD_D6 27
3 K( y9 X/ U/ }, U( P# [  m6 y" v#define LCD_D7 28/ y- c: \* y6 H. \5 P! Y2 }
" i2 E9 Z  f6 U/ N+ o% P5 l
//命令字符串+ X, u, Q9 @$ y7 x8 y% c
#define COMMAND_SIZE 128
  o: |- G% {) h- _7 K* hchar word_old[COMMAND_SIZE];
7 C% z$ \  b" D7 [, D. j0 O! Q2 kbyte serial_count=0;! q; s# R" w. y0 x0 @4 m9 I% J
int no_data = 0;
& n' S3 x; G9 I/ l//LCD 引脚链接 配置8 I0 ~' F" p5 N$ q' u% {$ B0 A  a
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);* g, u: q& Z, H, o1 ?% J1 J
//停止控制,高电平有效/ q" z8 E' E9 a+ e2 `& j
#define BUTTON_STOP 304 j- ?" T( T/ ~4 V  S8 g9 j4 o
int stop_flag=0;6 F# G4 {9 {4 z0 E5 s$ @. @
% a6 U& {! X0 g
//暂停
! Y5 L3 N/ y1 Vvoid pause(){
. p% D. H( N" Q& g- W0 W  while(!digitalRead(BUTTON_CTL));
5 l* X) E: |3 ^}
, m% R0 j" `( L% P4 h  I' R7 X6 P3 y' C" X: k/ a
void stopper(){
" n) f$ ~$ E- {  a5 x* e0 l  delayMicroseconds(10);
( A5 e" h& \6 M& @8 }6 p( N1 G9 I  stop_flag = digitalRead(BUTTON_STOP);& Y5 [: d  }) |3 v* M3 P4 u
}
: z  E  R+ `; u  v: J' Z4 i: K# Q: W6 `
void setup()
0 k9 C: G3 d# h% r4 `" ?{
: S* ~. k; k1 c2 k( |/ ~  //show start
: R( D! g9 U% c  K* c' a  SerialUSB.println("start");% K5 U! m5 |* V5 p8 {

! i% Y, m9 u+ M' r  //启动lcd
6 t3 W* D1 H# Q6 G    lcd.begin(16,4);
" e1 A& G8 a+ f$ J8 [; x1 L    lcd.setCursor(0, 0);9 z: T( e# |' r( q& R8 f
    lcd.print("hello, world!");
3 I/ R5 x. ~! Z; `# f3 R& Z6 x8 v% Z4 x+ ]* @2 K* y! q
  //初始化控制引脚及引脚功能; t$ A1 L9 C! Q% F
  pinMode(BUTTON_CTL,INPUT_PULLUP);
+ m! C6 [1 W, v+ ^8 S1 s5 ^! i  pinMode(BUTTON_SS,INPUT_PULLDOWN);
7 F! S1 v9 {- {: _  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
. S; E( P2 d5 Q2 B; n  pinMode(BUTTON_MAN,INPUT_ANALOG);
% U9 i% g, F* B; f
1 F5 I8 r7 M7 b4 M% p  //控制引脚的中断
+ r' p4 K1 T0 L  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断
$ ?6 K% k0 x* N" n8 o% V  attachInterrupt(BUTTON_STOP,stopper,CHANGE);) e& E4 ~3 r4 K

; {; [' ]2 |0 ~2 z  //other init) s, B- f0 _. A2 V
  init_process_string();
2 _3 |8 |' Y8 f. |1 j7 H4 b3 }  init_steppers();
% J0 R1 O+ O$ L* _9 ?0 c  init_camera();
# h7 Y5 g5 P  j2 O7 h) ~  ~) M  W9 A
}' a5 X& @' S& O3 D

! F- }3 G8 Z! k- Rvoid loop()2 |: J9 v5 _) V1 R; ?4 A
{
" a$ e0 M* M1 Y  char c;
1 a& T, |8 t# H; z6 |4 l3 m$ r
. c6 O" l6 C7 N) P4 G' b, H8 a1 c  //读取输入的字符2 l8 v0 x! ~) k6 `' _
  if ((SerialUSB.available() > 0) && (!stop_flag))
3 b1 G* U% ^# ~/ G& `  [2 V  {
$ n$ a. V" j- K3 J    c = SerialUSB.read();
$ |% ]7 ~+ S* {- G    no_data = 0;# b4 r% I6 k" b1 w
& h2 m1 v6 e4 p. h' {- Z* g
    //换行符代表一个命令的结束7 C4 D; J5 T! B" y9 Q1 h. ~5 E- z
    if (c != '\n')( s) Z: h. N. ^+ g: e; v* _% B
    {
- E8 W0 y" o. H7 ?, o1 H1 S      word_old[serial_count] = c;
4 m) y% @# }0 p' ?      serial_count++;9 U8 T7 G- S" n+ ~# b1 l
- b) v$ D; T% z2 r6 T1 @# i; k5 I, V
    }2 j0 ^- G# P, M
  }
% N6 E4 Q+ s& w! `6 e  //标记没有数据输入- D  o: [& _5 U5 r& ?& ~3 i
  else
. r) l6 _) `! }, c; U1 g  {
9 J& L7 C! s% H; K    no_data++;! [1 d+ m4 a+ c0 _5 ]! N" {
    delayMicroseconds(100);
' C% {% W3 A& I3 \; R# M  }
4 v+ F) h3 s: t- `/ a0 ?) y2 _$ F2 A' @, G8 R/ D9 l
  //if theres a pause or we got a real command, do it& c& i2 q' f, l2 m7 ^3 M
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag)), G% f, d8 p% {7 u
  {
2 d* ^' ^7 d- D3 _: k' r( [) y# V6 J/ \4 Z( _8 e
    //处理命令& {* H; ]  l" j, P
    process_string(word_old, serial_count);
" U0 r1 T9 n8 Y
1 N5 }: i% `) G- c& j/ |    //清除命令- P. ?0 c. C- F! c+ z
    init_process_string();' Z7 Y' Q5 E& i$ N5 _1 y/ `# x4 v  T6 ?
  }
, V( F7 a; s9 B6 ^8 b6 |- {$ v+ j  w$ ?" G, U% G
  //如果没有数据关闭电机
3 Q+ K2 S+ k. i" p5 `  if (no_data > 1000)
5 T, G7 \' }- d; W0 m8 R4 p5 D1 R6 i    disable_steppers();
, N3 v" `- m9 P: a5 i/ ]
" t& F4 b' i* F0 \: s  //如果ss键按下进入点动模式
0 v( ?3 c9 c: C3 Q- v5 u  //if(digitalRead(BUTTON_SS)) ss();
% D* P$ c1 W/ b6 t  run_a();
$ l7 N9 \* i6 k" r0 M, R% n( {}5 K& Z2 e+ Q0 M% ]# I

8 h; C& Z+ l3 q! j% c8 Y* _# @//点动模式
( b; c* N& v2 wvoid ss(){
8 [" g1 s" r* N# c
7 A" r; W. B& C/ W  delay(1);1 B( X9 @( R! j0 m* P5 n/ w! g
  if(!digitalRead(BUTTON_SS))return;( o: J4 |& M, P; I9 K1 D
  if(!digitalRead(BUTTON_SS))return;
1 M* c, ]3 e: Q6 s' I  //init_process_string();* k; n* W7 Q; a5 `) c
  //init_steppers();
1 s1 p+ U* b* I  z& R  //init_camera();
9 N3 c2 V: c& h& L# T  a/ `6 O: K  N, D
// SerialUSB.println("Step By Step Mode");
; m$ m. d3 t, M3 M; v. f//#if (LCD_TYPE == 1604 )! e/ B/ }, m+ n
  //当LCD为1604时显示要处理的命令4 Z! C6 T6 t! b% `4 m9 C1 h& E- A0 R
   lcd.setCursor(0, 0);
; o6 x9 T1 U. R( L- [. D0 p% Z   lcd.print("Step By Step Mode");
! Y$ t8 G3 b: F0 k; [9 s* n: t//#endif
0 k! `8 P  p$ x  process_string("G1 F5000",8);
8 @  J  F# F" T  f/ Y( |- L  process_string("G91",8);
; W' w2 a+ ?$ N- w% o( o+ k  //init_process_string();8 n- n" k+ J- ]) N9 U

6 I  D' P( j& B! G$ K3 K' _* q# D# _  while(digitalRead(BUTTON_SS)){" k# j# r) f" J' ?3 Y
    int i=0;2 a, C$ ]" k! y0 f8 x. ]3 U
    i=analogRead(BUTTON_MAN)>>9;
2 ?+ O! o5 s5 O6 y4 U    //if (i==0){break;}1 M. j+ g" ~5 U" s
   //SerialUSB.println(i);
. A& Y) R! y: {  v" e1 m6 |   //delay(1000);   6 ^0 j+ t% }2 s8 l3 B" M

7 L( j3 k7 ?8 Z9 Z; z4 T   if(i==2){
) K" e2 ?3 n9 q8 J0 V& B0 |      set_target(1000.0, 0.0, 0.0, 0.0);# r" R7 i1 T* J7 a  [  y" i
      dda_move(getMaxSpeed());7 |  b9 e( z. w, u8 k
      if(stop_flag) return;
8 A! n, Z! H/ n5 }2 h- W$ X1 M( w      //process_string("X0.01",5);
$ E0 z  n' r& M$ d  e7 _5 J      //init_process_string();8 c  n& N7 {& D! x" Q" C  D( M4 p
    } 8 M/ u* F% h6 c1 T$ h" T
    else
+ F# ~; d/ z, v( {4 ~0 ~    if(i==5){0 W3 W( y: G8 V7 Z+ Z; Y! ^
      set_target(-1000.0, 0.0, 0.0, 0.0);+ f( b) z; l. ?4 A! |/ X
      dda_move(getMaxSpeed());
; Z9 P4 X4 x( N      if(stop_flag) return;
3 l7 J) C' W* q: `      //process_string("X-0.01",6);' H- {# L( t/ G
      //init_process_string();& B$ H7 e& W8 l
    }
1 U, i" L2 i+ X0 S+ J( O! W     
& m$ t6 f6 f  [- d- M
; i# z) L1 c' u4 \  }- o7 z" i4 y; f2 C
; I, N" R2 R3 M
  //init_steppers();
( |; W3 y4 g2 p9 m. g! n, Y  //init_camera();$ f7 S4 s8 x- H4 l$ M' S
   // SerialUSB.println("Return To Normal Mode");
, ]6 J7 v8 d* |* }# D) M     process_string("G1",8);
+ u7 n. r; a; m  d  // process_string("G91",8);
, G( H  r# t* @    init_process_string();
1 w: r! q' X7 E& [//#if (LCD_TYPE == 1604 )7 S6 Q; M# D+ E* O
  //当LCD为1604时显示要处理的命令
# {& @2 v* f- A. o  // lcd.setCursor(0, 4);5 C/ t: z$ {/ F) `* J: d, G8 l
  // lcd.print("Return To Normal Mode");
! j& Z# [4 m) D6 P( i! S//#endif/ k! |; n: l/ }! t0 V
}
' F1 `2 g7 g6 ], z7 s. i/ ?. e9 t  i  G2 g; d8 e1 t
void run_a()0 P. g2 G$ d/ R1 O
{
, z  B- I1 R; I6 c9 E  //delay(1);: B. j' w0 g" v
  //if(digitalRead(14)== HIGH)return;% v; t* r2 n8 n4 L: I6 g
  //if(digitalRead(14)== HIGH)return;
: {' v- [4 T; R  //process_string("G1 F2000",8);2 i3 F4 N+ p  q/ n2 M" z! J2 U) Y9 Y
//process_string("G92",8);. V) a0 y2 |' T" x; l
process_string("G90",8);
9 t: A3 t% H9 {' t- i. q) \ process_string("X120 F10",5);
; S" U; M  K/ e) P+ ^  W9 P$ D2 ^/ Q" } process_string("G4P2000",8);
7 T; `; S8 G+ ^, H0 G% q6 `* q process_string("X10 F10",5);  ~9 s0 k" \& [( e
// process_string("M101",8);
7 Q! u, }& P3 s// process_string("X-50",5);
; k3 H& S* k) i dda_move(getMaxSpeed());; |/ `! t4 \4 u- n
init_process_string();* {' g: T/ ^* U, T0 q

: p6 G1 A& A/ ?$ U}
7 w; u# J& D# M8 h0 k
 楼主| 发表于 2014-5-10 09:53:25 | 显示全部楼层

/ y' L5 C1 B9 @2 B7 M
6 T* l2 A; f; f5 V: ]// 定义机器参数% D. k+ a. f+ p: N3 e
#define X_STEPS_PER_INCH 400
7 H: j/ j! J8 O$ I9 t- t$ d#define X_STEPS_PER_MM   16.0/ y+ o$ q8 o0 ]+ l  L* J; B1 G
#define X_MOTOR_STEPS    200& X1 X# q$ q8 Q

; T) x' I1 \) W* V! I- z# K& }#define Y_STEPS_PER_INCH 400.0
6 T! j5 j- H' Z1 V9 g' H% ~#define Y_STEPS_PER_MM   16.08 b6 p8 D8 K  X: l: Y( G. ~
#define Y_MOTOR_STEPS    200) o- W; }$ y: z( Q4 N
6 o  e5 C! M1 I! F
#define Z_STEPS_PER_INCH 400.0$ R# o# |; V+ v3 f& ^
#define Z_STEPS_PER_MM   16.01 @1 ~4 A+ d# W$ X4 l) `, \
#define Z_MOTOR_STEPS    200( N: K( s% R& q5 K6 `: E& q

1 b6 j! Z8 D9 \+ ^8 i4 z#define U_STEPS_PER_INCH 400.0
1 r# E9 ]2 M' j/ U5 s' y9 z  T#define U_STEPS_PER_MM   16.0
, f+ E  |/ c" F1 Q' E+ ]7 ~1 `#define U_MOTOR_STEPS    2002 l* L1 ~$ c$ G
+ C2 q9 s$ w# e
//最大进给率
! F4 u0 D. ]2 E( V+ M8 p#define FAST_XY_FEEDRATE 1500.0* W! M0 K9 ~3 `& |9 \* P( u
#define FAST_Z_FEEDRATE  1500.0
/ |, J( E$ Z% F1 K#define FAST_U_FEEDRATE  1500.0
$ r2 Q8 r+ W9 w// Units in curve section
; ~" L0 v2 s/ x#define CURVE_SECTION_INCHES 0.019685
7 a, k/ J0 T5 H4 F7 K#define CURVE_SECTION_MM 0.5
5 P4 y7 A7 u0 ~% v9 n' Q6 o
$ F; k# J' n) W- \# }) l: T3 D) F; L* J' S4 F3 ]. [& }. j
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
/ [) h" }5 h3 n: b" I# l" ^/ J// RepRap opto endstops are *not* inverting.( J& [0 Y$ ?- w/ `) o
#define SENSORS_INVERTING 1, @9 C2 q) B2 G  v4 F2 J. ^

  n  |8 L& @8 ^! P  ~8 z/****************************************************************************************
# c* Y* Y- S- L3 M% @% v% b * digital i/o pin assignment
9 v* K1 B. A, B$ S) l8 w( x+ A *
( L9 e- S0 t8 `% V# |" J * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5
: k3 ^! G. N4 V ****************************************************************************************/
& I2 G' c6 t/ |4 U  W6 E0 L& x. P( o  G+ a* c
//camera shutter and control pins# I: Y* ?. m/ S1 E0 l0 H) R! |. [
#define CAM_SHUTTER_PIN1 29( P! Y5 j; {# u  h
#define CAM_SHUTTER_PIN2 30
) s; x3 E2 n( v  J1 ~9 @2 e; `0 e& W//#define CAM_AUX_PIN1 31 // analog 0- d; f. O, Y& [, A) M- u6 _
//#define CAM_AUX_PIN2 32 // analog 1
2 W, R. K$ z3 I$ |: s//#define CAM_AUX_PIN3 33 // analog 2
, S8 ?6 ]  c; r% B( P//#define CAM_AUX_PIN4 34 // analog 3. z: w9 V$ `2 m$ |9 l/ p* J

* d1 n6 h$ t; H; Q// stepper driver pins
- ]: [9 J2 ?. r+ ?% q9 r! Y7 _#define X_STEP_PIN 7
( w4 z. F8 G# ^  P& q% R. O#define X_DIR_PIN 8
: Q+ a" z- F9 k$ m6 W6 D#define X_ENABLE_PIN 19( d  B  x# Q5 o# `: }& a( K$ o
1 V1 V  J0 A& |" S+ z; z% ?. A0 q
#define Y_STEP_PIN 9
+ {6 x! A! {  q4 u' y$ h#define Y_DIR_PIN 109 g7 g5 }$ a" N$ @4 j, c# i
#define Y_ENABLE_PIN 19( o. f, \! B/ x( V3 X: {8 U

. l# s+ j6 o; K5 j8 r7 Z#define Z_STEP_PIN 11
' N/ d: m* u* `" j#define Z_DIR_PIN 12' q+ H- A4 M2 t3 `- l  J; T; g
#define Z_ENABLE_PIN 19$ n( O* Q, i/ H/ {2 u9 N
6 k/ Y: e% X! [0 z  _4 X$ `$ y5 j0 b
#define U_STEP_PIN 13
7 p# P3 N9 u7 @8 ]8 `9 t& w#define U_DIR_PIN 14
4 ?/ k" q  E4 d% N#define U_ENABLE_PIN 19
5 H3 f0 ^: [* l
9 A+ _$ n( h5 R" J: a// limits not used right now
$ P1 P% _$ f0 k  m) `. B& ~0 C#define X_MIN_PIN 14
2 q+ W& G; d: o$ O/ X3 g#define X_MAX_PIN 14
. A4 \$ |- }& _, f) A0 A#define Y_MIN_PIN 14) i! |5 `; H: e& b7 p/ o4 h0 Q% A
#define Y_MAX_PIN 14
7 }: }/ D, z) o' w#define Z_MIN_PIN 14; f; F6 a5 c, _+ W$ ~
#define Z_MAX_PIN 148 ~$ K0 O( Q% V3 o* X8 M, W) J
#define U_MIN_PIN 14
/ _2 n) B( O# S% o+ `# W#define U_MAX_PIN 14
) |' `, V3 \1 g$ y8 ?2 {; _/ v
 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()1 T% Y9 X' b6 `2 O: j) g+ [
{
: I: G% I7 h+ B: e  pinMode(CAM_SHUTTER_PIN1, OUTPUT);
1 `1 u# x8 C. Z6 W3 {3 G, Q  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
" |/ Z% r/ U, \5 U- x3 f  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
: u) d! O. h, r- P5 t. q // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1& @& p: U, S2 t
// pinMode(CAM_AUX_PIN3, OUTPUT); // analog 29 x  x8 B- \# J$ e# P
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
3 U3 y1 c7 Q! W7 _" t}; {7 W0 ]& g$ [+ m# d
8 S, _0 R1 w$ N% i2 i3 C* E
void camera_shutter1()- [! p* B' ]) \" t
{
- D9 K6 j& A/ h* G, _  // fire the camera shutter via relay...1/4 sec hold time7 Y2 u8 N% e5 N( ]9 }2 x& l
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);" G  e1 _+ T" g
  delay(250);
6 x9 V- a$ C  `; ?& W4 c6 z  digitalWrite(CAM_SHUTTER_PIN1, LOW);
, A/ E( `5 \/ V, h3 \" c" O5 A) S
}
6 U2 `9 l) \" r6 ^; q" b& T% s; D1 N: k+ H) u" ^9 Y9 T$ q7 j: m* B# Z
void camera_shutter2()# d" J# H# n+ o7 q
{
) u, e( G9 e! [  y5 E& K  // fire the camera shutter via relay...1/4 sec hold time: g, e6 s* E/ Z* J/ G" S  H6 \- T
  digitalWrite(CAM_SHUTTER_PIN2, HIGH);
* U& j& N0 r) S: S  delay(250);
0 o6 A5 E& }9 D+ X$ Z* s  digitalWrite(CAM_SHUTTER_PIN2, LOW);
0 S, v  [  I: r& m3 h
0 ]" r/ H: D# R- y' ^% E}- j) f8 h' \. i, V, X" m4 H
/*
% P1 x$ Y- q- ]+ n, `6 Fvoid camera_aux1_on()
. U  G6 F* ?! C{
/ H: h6 n" m6 @4 ~/ s9 h# @  // turn aux relay 1 on' W; R! ]/ G' v7 ]- ?% F
  digitalWrite(CAM_AUX_PIN1, HIGH);7 T( h# {  ?& [* v
}0 y& H" W0 ?: @& |( c6 Q

. t( D+ k. ~$ R- nvoid camera_aux1_off()) p# c8 L" [; U3 ]# b; W0 V; s
{
* ~: S; g1 T" Y  // turn aux relay 1 off) S3 q" v' H' @9 P$ p
  digitalWrite(CAM_AUX_PIN1, LOW);6 ?5 C* a: ]" J' q
}5 I: v: b( d1 F! k& H3 s. A

  p" R9 Q" A1 e' p9 [1 X1 g9 `void camera_aux2_on()1 ]' |) W/ L, u! B1 ]* y& k
{
* I7 I; e3 U/ g% w6 h  // turn aux relay 2 on# Y( D/ N) W3 p. `4 D* y! f& Y, j
  digitalWrite(CAM_AUX_PIN2, HIGH);3 q; _7 C- J) |
}
3 ^! k8 G" k" ]8 d1 L5 s$ [  F5 a9 m* |& ]4 N
void camera_aux2_off()
; n8 M6 a: L4 \! }& h, i5 I; `{/ }- Z3 T, W) o6 |
  // turn aux relay 2 off$ u7 e, X4 o$ M' l7 ^3 v0 c4 A
  digitalWrite(CAM_AUX_PIN2, LOW);
' f* ?& U7 L% N0 G}: a# h3 O; u7 i1 G) k! R& b4 u
8 I' h: r. B$ J+ V
void camera_aux3_on()
' L0 n, ~8 u8 [- m' S0 N; Z{7 C& F4 D6 x2 G; H9 G: |5 Y. X: @
  // turn aux relay 3 on$ y; r: e$ g5 D% C2 z
  digitalWrite(CAM_AUX_PIN3, HIGH);
5 W6 Q+ m8 j% u# I}' b" Y- H3 K8 ~9 [5 a
4 @( s" Y: v6 ?8 n
void camera_aux3_off()
  Z3 r# F# G0 J( k( B0 S* b1 M9 G{& Q6 N7 m' e" @  g8 ?/ |5 i" i' G
  // turn aux relay 3 off$ h5 ?7 M  r" s7 F: m
  digitalWrite(CAM_AUX_PIN3, LOW);0 S2 a6 Y9 s! Y5 I
}9 V8 g; M' _( J; k, A

2 t& ~1 R9 J4 O" H0 V5 H- }void camera_aux4_on()
- z8 Y8 Z+ Y, A, Z! n8 i( Y! I% p{
% b2 d3 F, e, j7 I  // turn aux relay 4 on
3 B- x8 Z; [* |" ]& @& }" j$ J; A8 z  digitalWrite(CAM_AUX_PIN4, HIGH);
6 X# {9 o; J. B7 Y0 e6 P}
* V2 V& j7 X  w* Q0 c7 _
( P% Z7 C) h' Wvoid camera_aux4_off()/ [( j7 E' t7 w" A) d) B
{$ W/ e3 f1 x5 N5 v
  // turn aux relay 4 off5 M9 [  [/ j5 f0 }
  digitalWrite(CAM_AUX_PIN4, LOW);
* Q  x  H) k, o0 ~' X}
发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错5 l( c& u1 Q' Y! w* X
请问楼主是玩什么的,用乐高玩具吗?
 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice.4 o' D9 v1 p0 u! s! J
struct LongPoint {
9 w9 O2 w0 F& S) ^$ K' L: k7 p  long x;* W+ M* h( d: I$ C7 @& L( k
  long y;
# c) |& i; |1 [/ m5 g  long z;4 E" V2 I" V" T
  long u;% \, R" l1 {2 j& }+ Q# S
};
1 a2 a# c, C" F0 z! A" v- g0 K, Q* x
struct FloatPoint {
' T) j' Q- I( {% n: D  float x;
9 ~) ~( Q9 q& ]4 T6 Q* d  float y;
. p# ]! P$ ]& m4 T  float z;5 L  I6 `, r7 n/ C  P
  float u;
) r( d) T' V5 K};9 ~  n- M0 |5 v0 r/ n
- G+ H- O( G5 f3 d2 a
FloatPoint current_units;2 i. w6 P* ^5 Q
FloatPoint target_units;
; Y6 |8 O( ?! [5 F9 zFloatPoint delta_units;
) ~9 `1 d5 _8 U2 C# \! d: {: \4 E  O. \) [
FloatPoint current_steps;
- |# v$ z4 K7 Q$ W* _FloatPoint target_steps;
- P% P4 b& m3 S4 [* v/ AFloatPoint delta_steps;
6 m( x$ z) k* O& D- i1 @! G
2 q. j8 F( p" W/ P; Lboolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式
3 s4 ^4 S* S1 B6 h$ x7 ^: v# r2 I- C; P' @- e
//default to inches for units, B1 O' D2 F+ O2 I8 \* B/ o6 ^1 i% ^
float x_units = X_STEPS_PER_INCH;1 M, F6 Q% O0 x. I& c8 o8 u
float y_units = Y_STEPS_PER_INCH;
( g+ m% W) I- a% \+ {# ~float z_units = Z_STEPS_PER_INCH;
: p5 J* Z/ B; U0 n4 Mfloat u_units = U_STEPS_PER_INCH;% ~) a& K; a& T1 }
float curve_section = CURVE_SECTION_INCHES;8 ?* ]: y' u, Z) z9 k& p

5 R. G' ^0 B" j8 X  n//our direction vars
3 ~0 u3 C; Q* lbyte x_direction = 1;6 B! h# D* ?( m. g
byte y_direction = 1;
- ]/ r9 p6 V7 H* v8 R2 ]4 e& B; a! sbyte z_direction = 1;
$ b6 w* s' y" }+ M$ Nbyte u_direction = 1;
, Y; P0 k4 {; N* \
4 X- _2 @" P) B, K- H' }" g//初始化字符串处理+ j3 s; n& I. ~6 V, A
void init_process_string()
1 q( a! Z5 ~, M: g( A{
! X' r, i# H6 U( s9 R' e  //init our command, }$ w, [. e  e8 d* y0 Q
  for (byte i=0; i<COMMAND_SIZE; i++)
" [8 G9 X0 k5 G/ q    word_old[i] = 0;
! Y8 c. T1 I1 H2 I/ _- I  Y" P  serial_count = 0;3 o' S* F  n+ C  E3 p
}) d. h2 |7 y: P/ h! R. f
: }+ I' I/ L1 a6 O0 }/ M
//our feedrate variables.4 _7 Q9 ^( u9 g9 y4 q; X- \
float feedrate = 0.0;
. v- `$ C  _% k2 F8 e$ glong feedrate_micros = 0;
5 Q5 a8 L1 S; P4 T+ [/ V/ I# e# \1 l( X6 P" i) n% D) f* i1 ]! V: X
//读取并执行命令
% ^& t/ f: H/ h; \, q1 rvoid process_string(char instruction[], int size)
# e7 C5 u" U# D3 X6 i' }" o, A2 I, M{3 @# @4 t3 R/ n7 t; U
  //the character / means delete block... used for comments and stuff.+ g' Z# Q# f3 [. B& Y" R
  if (instruction[0] == '/')/ H: d" b$ v( l0 D+ p* {" ~5 I
  {
" ?. w% b0 Y: Z8 i# F8 W2 }    // SerialUSB.print("ok");/ f/ y+ f* B8 F! n0 a. L
    // SerialUSB.print(byte(78));6 v. i3 I, y4 Y' n5 I7 f
    return;
0 u4 g8 m6 x$ P  }4 E6 y/ D: x( K" A9 S6 q9 M/ ~
  //init baby!
* ?  C5 }! o  c2 I0 v3 M& t  ?. Z. K  FloatPoint fp;. ^: j( I( i( C( j# O2 P- V( P
  fp.x = 0.0;
5 H+ j$ W) [( {  fp.y = 0.0;
' f, v7 m9 ^2 r8 ^' l0 B  fp.z = 0.0;
/ w2 K$ _; R' d* p& ~  fp.u = 0.0;
( ~! N( y7 d" B7 f3 o4 k3 I, D. e
  byte code = 0;9 D, T2 y! |" T9 v
8 a% M7 {  X# b. |
  //显示在处理的命令, M/ @* S5 e, K. |9 f# ?
#if (LCD_TYPE == 1604 )  B0 q1 h2 r! i; H; S& `$ L
  // lcd.setCursor(0, 4);% B' V# R4 }. D6 {5 W/ T; ~
  // lcd.print(word_old);7 p9 n8 M4 i4 Y: H( q) ]' N5 K6 _
#endif3 W5 Y) @; V0 I3 a; i6 L0 @8 N/ g
  SerialUSB.println();# ?/ _, v0 f9 m4 s4 H& E
  SerialUSB.print(instruction);1 c( d; Z9 p3 t0 I
  SerialUSB.print("\t");
3 V' u, N  b; v6 [4 T) f
. I/ H8 k0 @5 ]5 y6 ?4 t  //what line are we at?2 N( E& Q& T# i# J, v
  //        long line = -1;$ D- r+ ?, K# f
  //        if (has_command('N', instruction, size))
! }! W9 {, a! `3 ?. z  //                line = (long)search_string('N', instruction, size);% L4 S; U8 S" U1 E' W# [0 b

7 B$ B3 N7 x, E( j( V1 X4 o! Z  /*
2 ]0 ^- h3 t7 h3 O' C- g        Serial.print("line: ");- C2 D/ v4 d/ H' A) S8 r% ~
           Serial.println(line);4 p0 U" R3 L: J4 ^# d8 n
           Serial.println(instruction);
) Y3 A# r3 L* Z   */* p7 l! L* ~( v) I, B) q; c: [
  //判断是否读取了个 G代码?
) j7 |$ z, t  j0 D. N  if (6 _. d( L( O/ w
    has_command('G', instruction, size) ||
3 i, w; p. G4 s6 ~( p    has_command('X', instruction, size) ||& s0 j8 ]; k$ \
    has_command('Y', instruction, size) ||
1 L% h" U4 l0 [1 F* ~( x% X# R    has_command('Z', instruction, size) ||
( L9 W. C) M2 x( _0 y' f    has_command('U', instruction, size)
" i9 C. j6 H6 |( R    )
( K6 D4 h# B0 k+ N1 c  {
2 S- M' }6 o: s    //which one?& {$ g' ?3 O7 X, C# I+ B2 J
    code = (int)search_string('G', instruction, size);
( `7 C* \+ w" O5 l! t0 E    // Get co-ordinates if required by the code type given0 A" ^- b4 O2 N' v2 ^
    switch (code)) ?/ X' l+ u4 ^! |% W4 ?$ y: K
    {
* o: y0 u; }: c8 s    case 0:
+ J7 S# ^. B7 C3 O- t' O% [1 F    case 1:! B2 p* ^8 N1 Z1 K$ v
    case 2:5 ]- I7 x8 X) f- X. l
    case 3:9 B! L+ T: y2 V4 j& p
      if(abs_mode)
( t6 N+ q: O( L* o0 g. \3 ]      {
' J" G- T+ M7 e. r: P+ I2 V) ^        //we do it like this to save time. makes curves better.
; C2 z3 d$ k6 v* Y. U        //eg. if only x and y are specified, we dont have to waste time looking up z.$ x2 t3 m9 |# q& n/ `: l5 n, B8 E
        if (has_command('X', instruction, size))
) v& h, b- l, Q" w: u1 j4 |* y          fp.x = search_string('X', instruction, size);
& d6 w8 }8 x' Z. k. Y, ^        else
+ A3 ]+ Z  ?; o- u          fp.x = current_units.x;  V0 o( x1 F9 m7 T
1 S- I: k# C7 }' y
        if (has_command('Y', instruction, size)), O3 n# n# _3 A; n6 t- e0 e
          fp.y = search_string('Y', instruction, size);
: G, q/ I# Y* H/ [% e        else
: a% R, K' B1 K7 \          fp.y = current_units.y;
5 V3 b( J) j! P2 L, e( G$ L7 _( h$ ?5 k" K4 K" ]. h% P
        if (has_command('Z', instruction, size))
! o) E8 Z" s- {  z8 ~          fp.z = search_string('Z', instruction, size);
( H; {) T5 m! Z8 c        else# R5 q9 t' c7 i7 h( ~# _4 B
          fp.z = current_units.z;
! _. N, _& x0 e9 l/ T         
! B0 @  J1 G2 m1 K3 `        if (has_command('U', instruction, size))$ [  W* H5 z) i1 z3 a- Q
          fp.u = search_string('U', instruction, size);5 @4 Z. ]- o6 @; _& [0 c  c
        else
1 I4 e6 ^$ \: H          fp.u = current_units.u;# ]2 }8 n2 }# r6 ~: v* j
      }9 t5 n$ P! \8 l
      else; q" K1 H* h. g+ w$ a6 r$ x' W
      {+ i; s# K# {4 l
        fp.x = search_string('X', instruction, size) + current_units.x;+ o; s' e% ~7 [- x
        fp.y = search_string('Y', instruction, size) + current_units.y;6 c$ k. f- X6 ?/ i' E
        fp.z = search_string('Z', instruction, size) + current_units.z;/ ]$ G6 a9 ~- c% B- D" g
        fp.u = search_string('U', instruction, size) + current_units.u;
: T- s* T/ {" Z8 \; M8 W      }
# `9 Z. p; X" m. m# A% Q/ ]5 W      break;
( p9 N( _4 G* }" p. s0 o$ N) X    }
) F$ k" [% K: Z# U# M3 O    //do something!
0 O( @- K# R# P6 J* W' ^" f) F- O    switch (code)
4 x% B9 c' {) S+ a) u; X$ L. {# s    {
) u! V* W3 ~7 [. \9 C0 B' z7 x, g$ ]      //Rapid Positioning
, |5 _9 y' ^; D6 h      //Linear Interpolation1 L% q$ U( B' ?3 }2 B: j: r( c3 ]
      //these are basically the same thing.
- d1 ]3 F% t4 V6 a3 t- M    case 0:6 @: b8 O0 A5 D& A4 n$ Q
    case 1:
$ c+ s" M- s# \& U  U      //set our target.) K* @. Y- M* @! G2 g- N6 t
      set_target(fp.x, fp.y, fp.z, fp.u);
) Q$ T( f; q3 `7 ~5 b  T      //set_targeta( fp.a);1 S4 f0 T) x) x; @; A8 ~) k0 c) t
      //do we have a set speed?
- {" `  ^& }0 j- C      if (has_command('G', instruction, size))( t/ d! v' l8 T, Q7 O/ A
      {
& X, N% H8 T2 v- Q4 `        //adjust if we have a specific feedrate.3 V3 {% x- g6 |( u
        if (code == 1)0 m" ]8 c0 S- g# I: Q" h" Y) d
        {& \3 ~6 F" {& l* Z" f* z$ C
          //how fast do we move?$ k. R7 p/ C8 k0 C; @  u7 Y
          feedrate = search_string('F', instruction, size);. K6 j9 _4 ]/ R' n2 j) X
          if (feedrate > 0)
" d" n+ m$ D; I, L) |$ P            feedrate_micros = calculate_feedrate_delay(feedrate);& u- l$ o* h1 o1 h9 b
          //nope, no feedrate
! |3 [% p0 z  G. B1 p7 m9 X: w          else
* w$ {$ d8 s7 t  W            feedrate_micros = getMaxSpeed();+ Q5 |8 d4 ^2 q' J( l9 ?! s
        }) Q/ h& Q) \( m+ X- t7 u; g( z, Q
        //use our max for normal moves.
6 T. K4 n9 i2 f* _8 k+ f4 b: k        else
+ }6 q2 l0 t) g          feedrate_micros = getMaxSpeed();+ n% G/ z+ I) M# ~5 {5 {
      }
+ C9 P; I3 D2 a0 p5 ?3 v6 T      //nope, just coordinates!
3 y2 z7 T8 M+ p3 m/ b  U& L& l& n* n      else& b- I& G+ o. O; a1 x6 H6 D
      {, g; {& k+ j$ r! u% s0 J3 I4 k
        //do we have a feedrate yet?' b# C, P/ y; O
        if (feedrate > 0)! @# H) k3 H4 E9 d' X
          feedrate_micros = calculate_feedrate_delay(feedrate);5 m, ~. I4 d. p- R, z
        //nope, no feedrate
, |% C- f  i- ^        else& J/ a+ E$ \& A5 z9 e5 `
          feedrate_micros = getMaxSpeed();) u9 K3 d/ _( n, J2 t8 `# N6 j
      }4 T5 j$ y4 W. w( r

9 I4 s* i. r0 x  I% E1 s3 T      //finally move.+ l- L! R. O) m+ b
      dda_move(feedrate_micros);
, [7 T4 h6 ?% o/ k# m+ c. T. K/ c      if(stop_flag) return;$ Q' c, _) b4 j
      break;' q" U  R/ g- t' v% A

) I7 F6 T, W0 j1 J8 |% d      //Clockwise arc6 a* @+ V! X4 z
    case 2:
/ J' [+ b. U9 n$ j( d, g& l' _- d      //Counterclockwise arc
( R0 S; y: {5 L6 y4 {    case 3:
3 L; m' I- M: u6 s3 K      FloatPoint cent;+ c3 q& ~& z; y( `
      // Centre coordinates are always relative
% u' U0 @2 m7 w/ H: h+ r      cent.x = search_string('I', instruction, size) + current_units.x;. X9 |" t$ _; ~% I
      cent.y = search_string('J', instruction, size) + current_units.y;
5 h( h- R7 X% t% n# }      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;% l0 y, n- c# \% v( a) t% ^
" V( U8 v0 Z( u7 [4 W2 ~9 o1 z
      aX = (current_units.x - cent.x);/ f* o% L" B& F! ?& E4 {/ S
      aY = (current_units.y - cent.y);9 M3 b$ o; z+ o
      bX = (fp.x - cent.x);; I' |' Z2 j( i
      bY = (fp.y - cent.y);6 K! N2 W/ Q* O% R

7 M6 r2 g( Y8 Z+ r      if (code == 2) { // Clockwise$ X% G; G+ @9 H( c) D" H
        angleA = atan2(bY, bX);' e5 F9 ^7 {' ]1 @% N7 p
        angleB = atan2(aY, aX);* F& m/ \, O  Q+ @; @7 K  d$ I! c
      }
* j5 P! O! G7 Q, L      else { // Counterclockwise! o( h# \/ w- m) n' o; R
        angleA = atan2(aY, aX);0 t0 j1 Z/ \4 n" G
        angleB = atan2(bY, bX);
2 M; s$ \, g- T- e9 \& U+ p* j      }
- r8 @" y8 q3 p      // Make sure angleB is always greater than angleA
; c' z6 _$ ?% P# K- U7 f2 X# Q      // and if not add 2PI so that it is (this also takes; M2 p4 R+ k* n  x0 g; `
      // care of the special case of angleA == angleB,8 k8 B' P. g0 ^5 R9 x
      // ie we want a complete circle)" r8 S% p2 D6 W# D) V
      if (angleB <= angleA) angleB += 2 * M_PI;
+ V6 f1 K& Z( w      angle = angleB - angleA;
% v6 l# I0 U  r6 E& s5 V
2 s. Z: e  s4 s  }" m9 ]3 D% t      radius = sqrt(aX * aX + aY * aY);
6 Q" p( Y0 c7 e$ F      length = radius * angle;
% s6 J# m0 d4 w/ _      int steps, s, step;3 ], v% {2 P0 c% v) \1 O, V
      steps = (int) ceil(length / curve_section);6 a4 \$ i4 P8 H5 }  _# `- y
8 u( Y3 B$ [* v# {1 ~# u9 P5 p
      FloatPoint newPoint;
1 `7 Q4 t" g# p1 ~4 q5 ^      for (s = 1; s <= steps; s++) {9 F5 Y+ u( c* N# P9 C; x% d5 `6 h
        step = (code == 3) ? s : steps - s; // Work backwards for CW4 S; @; Y& W- C6 b0 v' S. }
        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
. f( C, T$ N7 C8 @( C2 |        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));: z, f5 p' q1 t( M4 T, _/ \
        set_target(newPoint.x, newPoint.y, fp.z, fp.u);7 ~: B4 {, Q- P; W, [+ {

# l2 P! M+ i/ x1 _  [& s        // Need to calculate rate for each section of curve
. e& O: M" ~; A        if (feedrate > 0)
7 O( D" t6 o# X" J          feedrate_micros = calculate_feedrate_delay(feedrate);
' q, o- t. n% i  T+ Z; d' b' Z        else
: m) r) n' h4 G8 E6 y          feedrate_micros = getMaxSpeed();
+ @" O8 G2 C' I2 _2 y: H
4 d. E( F+ }7 B0 u. T        // Make step
! ^& f1 \0 M0 w0 x        dda_move(feedrate_micros);
% S7 m6 J% k* _: T2 [1 _2 I        if(stop_flag) return;6 Y1 u" p1 o4 B, t1 s
      }% m+ [' @6 ]. L5 M8 Z
7 |, r$ C( W& w& T6 Q: C6 V& `8 u
      break;- `7 }! t4 e3 P' Q6 Z& J
5 ^- g- I: k  b! z  D0 K
      //Dwell0 {2 {# u2 w" Y
    case 4:* {$ P8 p0 E9 d7 f8 t7 u7 k
      delay((int)search_string('P', instruction, size));
1 O' M. P) u# H5 J      break;
* \- J2 X7 o9 U. B0 f- S# h
" Z, |- ^: V, B: @* Z      //Inches for Units
% N+ T: \* D) m* s) z    case 20:
+ S' Z/ ?: _0 M5 S      x_units = X_STEPS_PER_INCH;) Q( A2 t5 N5 a/ ?$ E
      y_units = Y_STEPS_PER_INCH;; \, o7 w0 `/ `! ^0 ]
      z_units = Z_STEPS_PER_INCH;$ r2 L* a. E% \1 Y
      u_units = U_STEPS_PER_INCH;
' N/ y0 B1 G5 f# m& e, ]      curve_section = CURVE_SECTION_INCHES;/ h$ a9 h3 e3 r6 }0 O
      calculate_deltas();
& D" r+ ]% W  B1 H9 D      break;, ^. A* [! ]. f& X' `
' N! b/ C, H% e5 L) N/ d! R4 U
      //mm for Units
, n. ?1 s% e# ^    case 21:. r$ q2 n, u  |, Y" X; X6 y+ G
      x_units = X_STEPS_PER_MM;
  g- i5 S; P# F- Y! E# J      y_units = Y_STEPS_PER_MM;, e1 J# N' S. J" n6 A$ l
      z_units = Z_STEPS_PER_MM;
; Z2 ^8 F- ~  u/ e! R0 r      u_units = U_STEPS_PER_MM;& k  ~) q! c# T0 ~. F
      curve_section = CURVE_SECTION_MM;
) N. `4 j; f* A1 D      calculate_deltas();* y2 A6 i+ z/ H3 h* K
      break;1 v! M: n$ g2 J; Z* O% P6 }& D2 d
6 E* b9 f8 \# k8 f  g8 m8 }
      //go home.
) @- _* e) v4 g1 ?4 q    case 28:
. i3 w1 E$ h# S: K4 ^6 L$ F) _& j      set_target(0.0, 0.0, 0.0, 0.0);
# d; [' k: ]4 ?8 y6 N      dda_move(getMaxSpeed());* X0 o% t5 `0 w% d
      if(stop_flag) return;
/ W  v$ m0 f' w6 H* h8 V6 F      break;$ u7 K& `* I( b3 f$ H
6 V- c7 w! |% |0 F" \
      //go home via an intermediate point.
" _- \7 @* L" T! A; `1 s) [    case 30:# H# O$ ?+ N. j4 N7 ^. S5 J" s& f$ p
      fp.x = search_string('X', instruction, size);+ n- F& B: ?, O: o" Q! l: K! P  y
      fp.y = search_string('Y', instruction, size);
: x% \; a; x3 s' f3 [; F/ q- H5 v      fp.z = search_string('Z', instruction, size);! {# c, q5 _% V- r* b
      fp.u = search_string('U', instruction, size);
: _3 i; n: K. h2 f/ \3 {# _      //set our target.
7 _2 q5 P6 O0 I      if(abs_mode)
6 }) |3 W7 v0 ]9 S8 V- x& Z      {, K& _- Y0 K* H7 a- q2 L
        if (!has_command('X', instruction, size))6 ~% M7 j; Y: e8 g7 w; s6 K, Y
          fp.x = current_units.x;
( @: E1 ]6 k" `6 V/ ~        if (!has_command('Y', instruction, size))- Q, j, `( g) Y" X
          fp.y = current_units.y;. D1 [' [# C0 w+ s
        if (!has_command('Z', instruction, size))
5 x5 z4 h" R! T" u9 o0 I& t; B          fp.z = current_units.z;
% I# P# M$ S7 r        if (!has_command('U', instruction, size))
7 o, H( K/ a. j3 P! R          fp.u = current_units.u;$ X* u# m$ D7 ^, j5 D) D
        set_target(fp.x, fp.y, fp.z, fp.u);8 u! m* z) X2 ?# I+ a" e. a6 n
        
. w; p  A" z; M* R% O      }! d2 a1 S' n) r' V; b7 L6 G% v! Z; |
      else
6 S% v- J, K  E        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
# I5 T( M, q) q1 L      
7 n/ d) y& R. h  ^: T$ _- r( j      //go there.6 n' ?8 A- p6 ]4 q
      dda_move(getMaxSpeed());. ^6 a" N" w8 j1 R
      if(stop_flag) return;% t8 d4 s: t! a0 }  n! u8 Z

  y; F7 ]! |4 M      //go home.
, |( `4 E- V$ v( B" E9 f      set_target(0.0, 0.0, 0.0, 0.0 );
# ?: G) `9 C* C9 \9 J9 K       k9 A6 k7 N, y
      dda_move(getMaxSpeed());! I' X$ `3 m$ g* x" ~# X
      if(stop_flag) return;
9 t- w! k5 T# s( K- u9 n      break;& D  e" [0 M; O/ G( e, D. C

) R) M, T, [! F+ j/ r. R      //Absolute Positioning
4 Q& t2 ]" c3 `  U9 v* J+ v2 s9 G    case 90:
6 Z6 V  b8 c9 m' z, g      abs_mode = true;
! _: M) o7 J* R" D0 v/ P( i5 j+ _      break;& I) q; Q& ^/ x6 ^2 P* ^* m# B/ _

1 `, U* e' }* A$ B      //Incremental Positioning
& n; R- u# {7 V( B1 h    case 91:  ]9 l4 R) M/ X- U
      abs_mode = false;8 a. \3 a0 y; e' F0 t
      break;- U7 y/ o$ V) c# ?
1 ]" U" u$ p2 R$ O. e4 ?" d- {! J0 o, I
      //Set as home# v/ r/ a. E0 T& Z! L
    case 92:
! [$ K  h# @) K. r/ |) b     + y2 O* }3 }5 A. u4 i$ L5 q" h
      set_position(0.0, 0.0, 0.0, 0.0 );
7 q. w: g$ R: o      2 N3 f3 V; J$ d# x" J
      break;0 X7 p% E8 r$ ]; |, ?8 S9 n: R

' Q+ m3 v2 }8 |0 ]9 D      /*) D% f, S% E6 P" \! B
                        //Inverse Time Feed Mode
* x+ k$ p( E$ B1 `- A' _                               case 93:
+ r- I3 }  I/ v# ]7 V      
* b  @+ p0 A, t. n, A  U4 C                               break;  //TODO: add this
# {- A/ E2 ^- J1 v! G0 z& r' `       # @4 q2 A9 h, U9 J3 K# j- G
                               //Feed per Minute Mode
3 `1 w& ?& t1 `0 r% A; d5 {$ g9 s                               case 94:
7 x; c9 l) w0 v1 l0 G- r" J      
" T6 w, O( p2 o, K7 T# g                               break;  //TODO: add this
  O% a* ~3 i1 D$ }3 D       */
0 q7 Z! f& l$ [, X
" F7 c3 \- F" c* d7 ?5 R    default:5 b9 @# W  J; Z: N# @3 m
      SerialUSB.print("huh? G");
: h1 J! @0 G3 x8 S      SerialUSB.println(code,DEC);6 c8 n/ a0 n6 J
    }5 b4 J9 @* u8 v2 p4 A
  }$ ^; }  X, _/ K- [7 x4 O8 D

3 C% w5 B0 l/ @; j) i; D0 \  //find us an m code., D- h9 E& O% b, Q8 W/ l
  if (has_command('M', instruction, size))
9 [1 C3 A# \! E" N- x* T2 m" O  {* z0 w9 q) m+ q7 W; n: K+ \! G( j
    code = search_string('M', instruction, size);
4 ]9 |9 `0 z" h. i( j- ?; O4 ~    switch (code)# x( {5 b# L0 P; v4 z  R& K
    {
5 B2 C& k. |/ G" S& _- V      //TODO: this is a bug because search_string returns 0.  gotta fix that.
! @+ a2 L/ a, k/ ~# {! X3 A# x    case 0:
2 J" J; X) A# e. g9 M      true;
& |. h* h6 ?9 f, }      break;
& }% q6 m4 d$ [* [  r; F" [7 H& B
, a; [, L( _8 z9 N    case 100:& w1 H1 G$ O2 N& k2 D" L* l1 T
      break;
; A+ c4 [( D* D2 f* }$ @$ u0 ^( T8 G& P3 A, c
      // fire camera relay/ B6 X. V# m$ ~( `+ b: i
    case 101:) l6 `7 r5 M* P& [4 a9 u
      camera_shutter1();
* u/ S7 {* ]7 u4 Z1 _! S      break;' \# v( H! e, c. ]1 j- q! a! }

) g: A5 b1 ?( O8 c! H+ _" l      // fire camera relay24 E1 @4 T; `# ]2 _9 @$ N; ^! \
    case 102:
' n2 W- o" T7 M8 s      camera_shutter2();
. t8 G$ @7 ]: Y2 J9 Y      break;* o# a* O& o+ Q9 s) p. i
/*
3 S. S) r, ?/ R: I      // turn aux 1 relay on1 ^# D9 [9 d6 K6 f. H3 ?3 _
    case 103:1 ?# g1 V5 L+ u1 S; r
      camera_aux1_on();
& R$ J' G) V; C" z+ ~      break;; D) y  g0 p* k+ z8 L

0 p( J! ^; F1 Q: w; |/ {      // turn aux 1 relay off7 o7 Y. {+ W! Z/ q  Z
    case 104:% x5 b; ~/ a1 C9 G& i
      camera_aux1_off();
( [: \* z3 F) ?( Q; k      break;
! K2 ~& F. j& O" ?: `/ r3 y. M8 _
4 L6 g- F: \7 P/ x      // turn aux 2 relay on
% A2 I8 M+ v+ t8 a    case 105:
& O: [" j- J6 \5 I0 I      camera_aux2_on();+ x9 i  N3 J+ x2 z
      break;
2 R+ g5 x8 W: `; @; V, l  t6 z; G2 T% a; G- u6 D
      // turn aux 2 relay off( S) [# k3 S6 \+ ^1 }* K+ F) @
    case 106:3 A/ q1 x8 ^0 Y& S$ v% x
      camera_aux2_off();
: Z( A. u! _9 k1 ?& i2 z+ @! w      break;( M+ B" p2 A# I0 _1 `& B
9 N# Y# p' B( y/ V' T+ V
      // turn aux 3 relay on
* v& v% U. D5 i, k4 i    case 107:
5 D9 q' K' g8 a. v. _6 R. H& g( ]- }      camera_aux3_on();3 ~  p& L9 S$ v+ ]1 s0 r4 M" _
      break;
4 A) r9 p  e3 k4 R: Y4 T& }
, O/ ~* \  b$ C- c/ j" L      // turn aux 3 relay off4 y2 l3 v. B( P8 a7 N2 q' `. T; O
    case 108:8 s2 {, h" q. o$ ^/ ]
      camera_aux3_off();! v+ ]2 l  _# f- O- x! ^) k/ a5 E
      break;0 R6 n% s. o7 D! {8 _1 x
7 z7 \/ V; w( _, U, @8 k
      // turn aux 4 relay on& F2 Z- C" Y, Y+ a- L+ \1 R
    case 109:
% b1 [) S/ i4 O0 ?; _' ~* A9 p      camera_aux4_on();
. v7 G2 z: J3 T# v2 L0 P      break;8 i9 u% L+ Q5 R' \6 z

& ]! `% `- ~- a# Y' C2 F      // turn aux 4 relay off
. E! _: L1 q% {    case 110:. V. Q* Z/ M. Y0 H3 o/ `% ~. {
      camera_aux4_off();
9 l- x' g  X- B7 `$ J0 C) z" j' w      break;
8 t0 _# ~' i* Z% Y*/
- \; k7 B8 v  s# B0 o    default:
1 ?1 w- [& }% U+ h, r! _
, v6 R4 c  L& |' c& ]% ^2 k" A      SerialUSB.print("Huh? M");
, X( R/ A% ?' ]6 Z, V      SerialUSB.println(code);
# ?. E9 G. ~! Z4 u- R' [$ z    }
9 T2 e. y3 z& w% }7 z  }$ U+ G- G- d) N! ]" L, W0 d

  S/ X/ S% k+ g! N  //tell our host we're done./ b. ]/ W& ?7 I0 C8 `  o6 i
  SerialUSB.print(byte(78));
/ Q9 O6 F; }  v# x. h
' z7 f0 z2 a; d1 j' s" j2 f}. S) Q* [; k7 \# r

2 Q* M* N' |$ m3 w//look for the number that appears after the char key and return it+ s1 E. C2 \- g* F' q* j
double search_string(char key, char instruction[], int string_size)
# K& Q- b+ x" G# N( S{
, N1 j: j5 L. Y) u  char temp[10] = "         ";
( J( P/ m+ N  N) L0 F" _+ ?! u  for (byte i=0; i<string_size; i++)+ x1 P6 B5 ?0 O, ~6 i
  {! O* M3 T$ y7 P
    if (instruction[i] == key)* ?: L' h+ O; C, a4 x
    {; e* O( n  t7 J: n# _3 }' p
      i++;      
* @. Q" _( a% {/ R+ v      int k = 0;+ y: ~4 Z+ Q# x& ?
      while (i < string_size && k < 10)
* b" l! U/ D3 R4 D      {
7 g2 O1 P7 m1 U0 w        if (instruction[i] == 0 || instruction[i] == ' '); T; _+ J1 L8 L8 P
          break;- B, o# e# V* O! O- I8 H. H

  P; I. ?% k, E+ Z1 P* [        temp[k] = instruction[i];
- Y" r* N: g' Y1 F" Y5 r        i++;& ^- c1 z0 [$ }- x$ Z# Z
        k++;
+ G; \, j) Y0 a. ?- E) O      }
9 W9 ]) ]0 H3 n      return strtod(temp, NULL);' S$ g4 j! F- J
    }1 [8 M& A$ o" R* P, G5 j6 @8 p
  }
( k1 d" w. Q$ m2 r0 {4 m" |6 E6 o( d  S% ~
  return 0;! [$ ^/ `2 P1 i: u
}7 D  s! ^. b$ f+ `6 ]

, s0 @% K  Z/ w& ~9 B//look for the command if it exists.
; e" r0 e) V" S# y0 R0 rbool has_command(char key, char instruction[], int string_size)
/ i: V  k& d: e7 ?. Y+ \{' |# ?- V$ u+ }. J& h2 G
  for (byte i=0; i<string_size; i++)# w; ]4 Y) D9 j' u5 a7 k
  {
$ Z1 s: x8 Q+ k, D* u) R6 {    if (instruction[i] == key){4 e# Z: ?( h2 D+ P
/ i8 u2 [7 Z- y+ V4 }
      return true;7 {0 ~& y+ E. M  n) @
    }- D9 h- t' I$ f  r/ _6 V2 k3 e  o
  }
0 w$ h# b  \! e' f5 I2 |: h: }1 [- x+ c2 w3 r: g0 I1 c
  return false;( D7 [* I5 U" J3 \1 L
}7 m. [9 V3 A4 K4 k9 g5 e
4 `5 P; z/ l! W% `+ ~% e) k

; ]. m% \. ?6 ]* Y9 f6 [
6 d- \( g7 K- _! k, K, s
 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层
/ v9 k0 N( d& B5 w
//init our variables
8 B' p( N# }1 `. a  ulong max_delta;
3 l; ]; B0 ?4 b1 h8 Glong x_counter;$ F2 @  R; A8 ?8 ^  N6 q
long y_counter;
2 Q5 B  r  ^  Q, Slong z_counter;
+ e5 v3 y# G$ e; glong u_counter;
1 l: o2 a% U0 Z7 }% `; X$ R8 qlong x_pos = 0; // x position in terms of absoloute motor stepps
% O* o1 C) v8 Q* t: `long y_pos = 0; // y position in terms of absoloute motor stepps2 m6 V6 M9 a( U  Q: Y& o+ C
long z_pos = 0; // z position in terms of absoloute motor stepps
) _$ f: ]) a* K7 T$ \long u_pos = 0; // U position in terms of absoloute motor stepps
$ ^; y% j: H6 `3 B$ p* \( C! u3 W7 x( D7 F2 K
bool x_can_step;
- }/ t$ S$ u4 O" cbool y_can_step;
7 \  M8 @* J( q' L% ~bool z_can_step;5 ]$ x% U, ?* w  a! K: z3 K
bool u_can_step;
9 o* Q& E" O, C: h) D$ z3 iint milli_delay;" J6 h' F2 N9 i( @3 m( J

. ?' F+ ~  p  c' C+ U1 Yvoid init_steppers()
# q/ Z4 C2 C) Q$ I{6 `! a( s3 o/ a- Q0 M# T
  //turn them off to start.9 \2 ?* b4 j1 ?5 Y
  disable_steppers();0 {; X# z; ]5 Q$ a

( ~$ T5 s" x- G) }) C  //init our points.
; Q6 z& g7 q/ Q( |; N8 ]& k  current_units.x = 0.0;
$ f% }( d4 h, v( h2 {  current_units.y = 0.0;" g1 G) w  G- ~' l
  current_units.z = 0.0;
' }: X/ [6 G% Q. u! ]  current_units.u = 0.0;
) Q! a7 }  J" l7 N) ?  target_units.x = 0.0;" K" _- K" L3 d: b+ p
  target_units.y = 0.0;
  g4 F* B, h2 b' j5 f  target_units.z = 0.0;4 A& l, \) l2 w. i  v, @2 {
  target_units.u = 0.0;
8 O. J9 n' B/ [  6 u* _: |- S7 H1 L

. w% J7 p% z( U& S  pinMode(X_STEP_PIN, OUTPUT);
, S4 u4 j; B/ }# L7 O( n  pinMode(X_DIR_PIN, OUTPUT);  f9 t* `8 H8 J  [2 c- W! a
  pinMode(X_ENABLE_PIN, OUTPUT);
& y# u3 }0 h! N3 ~  pinMode(X_MIN_PIN, INPUT);( i: \, z+ z5 n' R
  pinMode(X_MAX_PIN, INPUT);/ Q3 z: c! m- m) {0 g4 g+ y
( n( J  s& Z2 H; B1 C
  pinMode(Y_STEP_PIN, OUTPUT);( [% B! a+ ]5 [
  pinMode(Y_DIR_PIN, OUTPUT);
3 A' i# x- E+ ^  pinMode(Y_ENABLE_PIN, OUTPUT);8 r/ |$ }* x9 I. X' ^9 l$ Z
  pinMode(Y_MIN_PIN, INPUT);
8 N$ u9 J7 T5 i2 j0 k! J7 ?  pinMode(Y_MAX_PIN, INPUT);
3 K2 a3 T2 i' A, p. u; d* A4 i" {6 z& ?3 R) D
  pinMode(Z_STEP_PIN, OUTPUT);
) \' D: }; |7 K& O  pinMode(Z_DIR_PIN, OUTPUT);( P" G  F) k" K- A. F
  pinMode(Z_ENABLE_PIN, OUTPUT);
+ `# Q; n0 `. f* d  pinMode(Z_MIN_PIN, INPUT);
/ F, K+ B  v- m9 T+ o2 q  pinMode(Z_MAX_PIN, INPUT);
, Q; }* u/ d/ `7 F1 J; D" D& h5 G& @5 X
  pinMode(U_STEP_PIN, OUTPUT);% \  I& G7 X8 H5 V4 Q
  pinMode(U_DIR_PIN, OUTPUT);
3 C1 k" a- p1 n+ }" x$ a. D/ I1 N  pinMode(U_ENABLE_PIN, OUTPUT);
; h4 \$ Q2 c! w  _9 ~( R  pinMode(U_MIN_PIN, INPUT);
2 W  Z- B) Y: u* c  pinMode(U_MAX_PIN, INPUT);
2 w, e, N" `7 Y: A/ V; }  y  //figure our stuff.
7 |/ \3 o5 q) Z6 O* E  calculate_deltas();( S: |: [4 W9 K2 S
}
. u6 v, U! p' e' u
) i) x  W# \) h1 j" D4 o5 ?void dda_move(long micro_delay)+ K- e) F, ~) K, H/ E5 c% R* M
{
9 o, [9 b& ?: g4 A& a, k  //enable our steppers
  g2 N3 C  z# J4 _% v: C  digitalWrite(X_ENABLE_PIN, HIGH);
3 `- G+ E# q. P  digitalWrite(Y_ENABLE_PIN, HIGH);
' E% s5 _% O" Z. f' Y! Y& u; ^  digitalWrite(Z_ENABLE_PIN, HIGH);
0 y' m7 X; a) ~/ M; B; ?  digitalWrite(U_ENABLE_PIN, HIGH);0 t) T' l5 m( H9 G- F" L
  //figure out our deltas3 s  P, i) U+ G
  max_delta = max(delta_steps.x, delta_steps.y);
6 y4 R" g7 n4 {  max_delta = max(delta_steps.z, max_delta);6 ^0 q8 f0 t- h* Z3 N
  max_delta = max(delta_steps.u, max_delta);
0 y/ U& @# q" h% w9 o  //init stuff.7 i: l: B+ o+ ?+ j2 m
  long x_counter = -max_delta/2;6 s  l" M, g, u6 _* O" w
  long y_counter = -max_delta/2;
4 h. t+ B1 r. ]4 F$ b& E& ~  long z_counter = -max_delta/2;
9 l) k; {5 R& J; R  long u_counter = -max_delta/2;
7 w1 z4 Q! `+ Q6 w6 t# H: v& @1 A- }4 U8 I% x
  //our step flags2 B, x8 `, X" \. p" e# e
  bool x_can_step = 0;
' M$ Y; r2 ^8 ~& i  bool y_can_step = 0;
& y/ e) S9 t1 }9 g  bool z_can_step = 0;/ y6 E$ `2 e) K* x5 f. C
  bool u_can_step = 0;
' Z/ D% ^# H0 d1 i( p: P5 |; m$ g8 F9 c+ `$ O6 g! k
  if (micro_delay >= 16383)5 {! _: [- V; Y; C
    milli_delay = micro_delay / 1000;
7 q1 {1 p  i0 r  else+ J# ^5 a/ ~; c+ M
    milli_delay = 0;
. p1 P6 B. K- H: S/ Z. c+ c7 W* F: _3 F2 |  M. z* a

/ ^% I& k/ a. ?2 S. K  //do our DDA line!
0 g+ g9 j/ K! I/ j' B
3 O6 e5 X, @) |6 b5 r  do) J& ]( i$ x" F5 b
  {
+ U2 w/ E( b5 I    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;1 V; A6 S, |6 }9 ]; {9 m
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
5 E7 J: Q. y% z- ~: U    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);/ U9 ^+ G- F' W
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);( O& `. r! D( ?5 O
    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);
( G+ y. D& ^, G0 q: w' c    if (x_can_step); F8 \1 A6 N2 C& c0 p
    {/ N" _' |6 F/ R6 M) w3 O& d  T
      x_counter += delta_steps.x;
1 _9 C2 |, _; f! j
% t0 G0 _4 E9 X      if (x_counter > 0)* |. N4 C" i5 T3 D& Q5 \+ R
      {0 \) M  D; F1 E- I
        do_step(X_STEP_PIN);
9 |1 Z+ t, C7 u% b! l# R        x_counter -= max_delta;
) r2 ?4 \5 @, R* Q5 F; z        if (x_direction)
3 k: u  W! r. t& k8 @3 ]         { current_steps.x++; x_pos++; }
) n1 X6 X' Q! M* d: @/ v* y         
! S; x$ Z/ h- d" l        else
  s0 _4 Y2 ?6 _8 ]2 g- ^          { current_steps.x--; x_pos--; }
7 a3 k( w& {- \% R) c         
3 x: h2 A" t9 l: p' A      }
# S$ n, z% f7 k! @( a* R    }: w. E7 }5 k4 A
    if (y_can_step)
% i6 |3 _3 J# ^# L! R5 T    {
, f- x  C; }( h  [7 a  A      y_counter += delta_steps.y;' F) M" g/ J9 ^( j( O9 m- D1 v1 a

+ \! I0 F# h; b& K      if (y_counter > 0)0 a% i9 Y4 |" w2 T' ?- F
      {: ~( r% j% }5 U) S" t! q- r
        do_step(Y_STEP_PIN);7 M& X3 S; g7 i" s- `
        y_counter -= max_delta;
8 Y+ \: r! b* U5 X1 |, n3 r$ b4 Q6 V( d
        if (y_direction): U; M4 A& \2 @9 \: f4 R
        { current_steps.y++; y_pos++; }7 m9 O3 b) ~1 J; g! c
      
4 p" |+ j- D  y2 C/ P& J        else$ m) y) x' j! |& v5 v2 Z) `% |& }
        { current_steps.y--; y_pos--; }
/ z8 Q& J, a: ?5 P  i        3 u# _- \$ M( y" _7 C  z
      }
8 q& E% |' N. O+ O: s: e7 K& [) g    }% M- d- K- K6 B' @' Y+ Q2 |% I0 _
# Y' x0 L+ E0 d4 A3 `
    if (z_can_step)  e: M' t* c+ j  g) n* i& a
    {
, X0 S7 L# c  e1 s& r      z_counter += delta_steps.z;9 u# K. k) }! N+ }3 ?$ g% I: I

) o1 h' f. U4 l3 O, x. i* g      if (z_counter > 0)3 V: x% R, @( [8 Y" f( |/ i, Q; W
      {) J5 z6 L3 _) Q% I( Q5 s( s
        do_step(Z_STEP_PIN);8 J) b! v7 _4 P6 r% J& n
        z_counter -= max_delta;
* R4 m( l/ i, G
8 r0 t8 O% i2 n; m. t# b9 }8 a        if (z_direction)+ A2 Q( a3 }' g" A/ c: v
        { current_steps.z++; z_pos++; }
( S: ~# F. f* v2 |; K          |; C- ~6 l( a, D( i
        else7 a* L" |9 v  ~. u' U
        { current_steps.z--; z_pos--; }
/ R) [; r9 H7 `' z* I        8 I/ X7 r0 P/ i6 g% k
      }
4 `4 I- ~& x8 K0 B+ H    }
: h# I; O+ u! ^    / G+ |' k7 ~# ^/ ~4 U
    if (u_can_step)+ }5 l% Y3 _* @3 o
    {
% d  Q8 X- u8 K# @4 u! ~      u_counter += delta_steps.u;
8 j! G# y& Q$ p+ F3 y$ {6 ]$ ]$ I, ]- G3 d/ d/ O; A3 h
      if (u_counter > 0)# |) @" Y, l$ ^( V$ I" O
      {$ ^2 v. A# O$ z+ h1 [, h
        do_step(U_STEP_PIN);
5 f: t& `5 u. p# T7 q- s( l        u_counter -= max_delta;
) F, N* D5 F! Q; o9 c- U/ }, ?
/ h( B1 P) u4 r$ A& r8 c        if (u_direction)8 C* o6 G! j9 k! i( o* g
        { current_steps.u++; u_pos++; }% O+ `; b1 c0 L3 T, r9 f
          + V  v2 J: g$ [5 h, R  F
        else1 e6 Q% I# Z  Z$ y/ J) m" f
          { current_steps.u--; u_pos--; }
$ P2 s; K  p  }, E" W9 r         1 c6 X* x" U/ D/ M2 ^
      }
* ?7 i6 y2 I( D7 r$ f6 v8 W$ u    }
+ g7 z: H5 B4 U, q: @    //wait for next step.
$ q1 p3 y9 C2 K0 a  M4 f( I; l    if (milli_delay > 0){/ L; w  j/ v3 Z) @! K
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
* O+ X3 |3 v6 f# G0 k      delay(milli_delay);7 y3 V* l0 W, k0 [# d: k# A" l
    }                . o8 X9 o# t& R  h0 Q7 ]. H
    else{
8 T# Y6 s" {  S7 {& b      //if (digitalRead(BUTTON_SP_EN)) SPEEN();% N1 T; k. [# Q
      if(micro_delay>0)delayMicroseconds(micro_delay);" r( W* F/ A! X3 _" h
    }6 `/ {! f# t6 D8 a) R4 Q+ V- O
    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);9 q8 [, i& [/ Q, X& E) B
  }* \3 l, |0 S% x+ e5 R" S# X# Q
  while (x_can_step || y_can_step || z_can_step || u_can_step);
) }) o5 |$ x" r- b2 z  P5 g2 `% l( ~+ U& q0 ?
) T7 L* z. M/ g$ d1 {& {: r/ w
  //set our points to be the same
1 k& }6 A& N' |  current_units.x = (float) x_pos / X_STEPS_PER_INCH;+ W) L9 S( ~) i# _/ @% |
  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;2 H, n4 Q# ^. g# Z' `
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;* ]4 P/ g' ^& W, W0 z) \$ k" ?# o/ Q
  current_units.u = (float) u_pos / U_STEPS_PER_INCH;2 S) A3 m8 `# @( ^" B* j2 I4 }+ w
  5 _) T+ X8 N6 G, n' f$ G
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );
8 h, S. ~) c4 P
) I1 t% q, v: E$ x) |  long x_pos = 0; // x position in terms of absoloute motor stepps
9 o* k4 I! K% Y" e: H  ?4 q  long y_pos = 0; // y position in terms of absoloute motor stepps
! I. y+ ~) `, k. m5 C& R  long z_pos = 0; // z position in terms of absoloute motor stepps- d3 @2 {% U& w* G& q
  long u_pos = 0; // u position in terms of absoloute motor stepps
7 h( R! C9 G0 m* ]- ]- j7 M  calculate_deltas();# a3 }6 T3 ~& `# k, A
  " `% r! w7 \# I% z" n' }$ s$ ], O! @
}
7 {0 [4 {# {% F$ g* f. e, ^* |
& b- ]0 P, Z; Q1 @bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction), \* X% Q7 W& J* ^" v" u/ M
{
4 b( ]2 l7 o6 o) [& P  //stop us if we're on target
& x7 C+ s( h" n) |% ]  if (target == current)
( n, I- R/ ]* ~$ F! V) [! h2 O9 j: Z    return false;
$ A3 [1 D& |" ]0 P. g0 h  //stop us if we're at home and still going 0 U, V" ^. R4 Y5 C
  else if (read_switch(min_pin) && !direction)
6 J3 D6 U& h$ b1 u" t8 ^" t* e4 Q8 [    return false;$ z' u% n9 d3 l( f/ x
  //stop us if we're at max and still going
5 K3 t" N. s' g" y  M5 @, e6 B  else if (read_switch(max_pin) && direction)
  \( y' m7 Y% f% u: m' {    return false;6 n& E4 O2 }9 o# x6 v
' B* s- h4 k' p* Q, W+ y
  //default to being able to step
$ n& ?& A: i7 I) {! S  return true;
7 i3 }% Y# h- A- f}+ `- Q7 i& F( d  P/ ^9 v: V! w

" L, W  M/ s8 Q7 K: K$ Y# nvoid do_step(byte step_pin)
3 ^0 M" L/ L) R{" }$ D# `, M+ v1 [, n" x% z
  digitalWrite(step_pin, HIGH);
8 u; c1 X+ k" |. }( D  //delayMicroseconds(1);
7 v, R5 Z; {8 {8 v$ |1 ~  digitalWrite(step_pin, LOW);
) @3 }( I5 Y8 T4 v& K% F}
; L/ }8 B2 c1 u4 d& y# q) J6 a! q& {, s3 U0 S! H6 o
bool read_switch(byte pin)1 `$ J& `* Z" Z  m9 |7 u& ]* \
{
3 r2 ]+ Q& Z+ a4 w/ `$ _  //dual read as crude debounce8 l& c: Y8 W+ B% c0 F

+ W3 T5 |  a* X( q4 ~* Q  if ( SENSORS_INVERTING )
- |$ V, Y/ J& C  R" n    return !digitalRead(pin) && !digitalRead(pin);
- {! E! ?9 x  T6 m: U2 Q/ w* W3 k8 l  else7 d6 _) H$ J0 i7 i5 S6 S
    return digitalRead(pin) && digitalRead(pin);; `4 J# n& M( G7 f" Z# U6 o9 e
}* f+ [3 W+ ^0 n" T4 q* R
+ D) X0 w$ P  [9 o! a+ t1 g
long to_steps(float steps_per_unit, float units)
4 v5 \/ @; o7 s: y7 U{
3 y/ G' u: J* j5 d+ k  return steps_per_unit * units;
2 w. }) W$ s( R9 U- M" B* b0 m7 ~}" R& s) t6 O* R
4 @# e9 h+ Q9 _
void set_target(float x, float y, float z, float u)* Y) Z; P7 q* E" E4 q9 J* ]1 x' @- @
{
$ a, u( l4 R  g/ A1 F  m  target_units.x = x;3 A3 f; a$ r: _5 ~0 y& B
  target_units.y = y;
! }3 w% }: y$ g$ ^: @  target_units.z = z;
) G& }5 n# z$ B/ a# |, L  target_units.u = u;4 L' m6 P- {6 ^: Z; @6 q8 k* p
  calculate_deltas();/ \! G1 c5 v, y# G% V
}! P! N6 u0 `, Q+ G0 q; y
+ B9 m2 k6 X4 Y2 @( d7 k
void set_position(float x, float y, float z, float u)
9 A; L6 g3 K4 [: [" @{9 V: j0 w% y) B; N4 Y, G8 z
  current_units.x = x;) U9 C- P$ U" R, Q4 z! @& v$ l
  current_units.y = y;* E) L0 L" M( @
  current_units.z = z;
- U9 k6 q; ?3 q) u6 u! F  current_units.u = u;
) }  [9 E2 o7 `! e  calculate_deltas();. p0 v) s8 f% b9 v/ N6 l9 P
}+ K$ U+ H4 P5 Y) F! ?

9 O9 r- Q/ b) ~- o* tvoid calculate_deltas()3 F# T7 F, e5 W2 |# j
{
1 I7 p( C/ u8 i  //figure our deltas.5 M( M) L9 m, J' a; Z
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
5 |# S9 {& q+ L+ n* {, Q# r* r  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);0 Z7 O- t9 S; L* `0 G0 Q7 d; c9 v
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
3 U# y9 q* k' b$ {1 k; f  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);
  A; e( J0 V1 ^8 z0 b/ z; A& I  J# R4 h2 S6 |' k- ?; M1 {3 W" e( n3 N
  //set our steps current, target, and delta
0 R' R: W; V; `5 p2 T$ b  current_steps.x = to_steps(x_units, current_units.x);4 I$ W/ s/ D1 X4 c
  current_steps.y = to_steps(y_units, current_units.y);# Q: j, O1 \; A. R" Z; M$ I* K
  current_steps.z = to_steps(z_units, current_units.z);) x; j* {/ o6 y) M6 N' ?7 j
  current_steps.u = to_steps(u_units, current_units.u);" r  _. h3 s5 s8 e5 D0 p8 g1 U9 O

4 e. X+ P9 X) _8 U  target_steps.x = to_steps(x_units, target_units.x);/ {7 p* x1 i' ^, G0 m' u8 P
  target_steps.y = to_steps(y_units, target_units.y);" J  t, s% \( J" V8 z, R" l  U
  target_steps.z = to_steps(z_units, target_units.z);
; k, H; g- O6 R- D1 `1 _( S& [, D  target_steps.u = to_steps(u_units, target_units.u);7 r5 r5 q& H4 m' [  l
% `/ E/ J8 D3 r1 c& ?
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
: [- q& z5 R) w" m. _8 A  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
- V3 J7 G! N& a) m  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
/ r2 N  @+ F( T( M8 ~0 b  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);1 v4 V2 f: A2 v% U0 v" v
0 e$ q! z  H* m3 n
: u8 B3 ?) }; S" n7 B8 k. g

0 I& l4 C3 T# F  //what is our direction0 ?& i2 f& \% y% i: o9 {% W& ~
  x_direction = (target_units.x >= current_units.x);
) v' d5 h/ D, b1 ^  ~5 E  y_direction = (target_units.y >= current_units.y);7 S# j0 S! l9 O6 a4 \9 w0 K
  z_direction = (target_units.z >= current_units.z);
$ s) a% O( E1 |8 w9 i8 s, V. {  u_direction = (target_units.u >= current_units.u);3 @6 j/ X$ T5 l) k' m1 f( {6 M, k
0 d4 N9 O7 S! m5 ?3 V2 e$ f
  //set our direction pins as well
% y( J9 q- y/ j' Z6 w* b; N  digitalWrite(X_DIR_PIN,x_direction);
/ H. P9 b7 t. p8 n  digitalWrite(Y_DIR_PIN,y_direction);
! }5 ^4 f6 g) y4 E6 r1 O  digitalWrite(Z_DIR_PIN,z_direction);# s& w( z& l7 l# O- R
  digitalWrite(U_DIR_PIN,u_direction); 1 `7 m, R. p4 c( |* e

7 Y+ R: Z8 L! A' A  //绘制LCD# ~) n& i+ b6 t+ l$ S5 o
  LCD_DRAW();3 K6 B; L5 p1 p* v, P. Z

9 j0 ^( N# M: S: ?2 H  K$ t}
0 r! w$ y* E7 F% c: e! [5 B& R; ]& c5 g( {
' R, F* {3 H6 J5 x
long calculate_feedrate_delay(float feedrate)
6 e  ~! Q) ^3 ~6 w$ ~2 T{  A: v' w. |4 U% C& _# \
  //how long is our line length?
" g4 j3 |+ j; D) I/ @2 e  float distance = sqrt(delta_units.x*delta_units.x + delta_units.y*delta_units.y + delta_units.z*delta_units.z + delta_units.u*delta_units.u);
  ^; z7 f% O+ o$ A2 n- P$ z  long master_steps = 0;
- Z9 _: J& i" b- Q( }2 [" c) p* n0 M0 z- A0 {% W! h. i
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
. P& I# i5 F; e6 j4 p  q6 f$ i- qmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;! s. N' P6 I, n/ \4 E2 \
master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
4 b3 H- v1 z  c" h
/ D! W2 k9 \6 k  r7 o
: Q4 F7 B6 O- V+ h- `3 C2 ?% |3 }) D; \0 L: Q  d
/ X! Y& ^. d0 t3 _4 y; m  v
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.3 `6 M6 m7 J5 F2 h( b' P- M
  //the formula has been condensed to save space.  here it is in english:
+ n: C- Q5 h8 Q8 ~. X6 J  // distance / feedrate * 60000000.0 = move duration in microseconds
- G' G' |( M% t! `/ M% |  // move duration / master_steps = time between steps for master axis./ T( b- h4 V. H% G
return ((distance * 6000000.0) / feedrate) / master_steps;8 g8 m+ y5 b. U) n7 g7 b. {
. r( U5 g: l* F$ Z0 ~6 R1 E5 W
}
+ [+ h) l0 [) k1 y# d& B9 N
# c% V# `+ s4 X/ Plong getMaxSpeed()0 j) B6 D# D3 I+ D2 }4 o
{
" ?9 e! R5 E7 J" K if (delta_steps.z > 0 || delta_steps.u > 0 )9 K9 C, u' f7 H" C; N# G6 j  {+ V# l
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);2 ]6 k3 a! O# e1 L1 ]$ k6 T
  else- R* O4 m6 Y+ s) ~  Q8 D8 h$ T+ U$ U
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);
' f# T. C/ K7 q}
5 v2 u+ i7 s, v" I. h# i
4 R9 w- ~+ \. b1 wvoid disable_steppers()
$ @8 H8 A! n8 @. a2 C5 |4 I+ s: C{
5 T( f- T2 ^- @4 }: @( K# }  //enable our steppers7 F6 E3 X. l! n, Q- m# Y7 M
  digitalWrite(X_ENABLE_PIN, LOW);  \4 @2 t9 |  b! f8 c2 }) L
  digitalWrite(Y_ENABLE_PIN, LOW);
( E/ F, w; |$ X9 n  digitalWrite(Z_ENABLE_PIN, LOW);0 i, s1 n, X: l* k
  digitalWrite(U_ENABLE_PIN, LOW);/ p* _; H+ e2 h3 R+ M! d3 x
}! [9 K6 n( j$ F" c& v3 h! }* q" i
' s; v3 h: f3 }
: H" `$ O0 s7 d5 `+ \% F
//绘制LCD0 d: g! O* k7 T) f, r: T) \
//unsigned int DRAWCount=0; 9 t- I) M. U, C: ^7 m
void LCD_DRAW()% q1 y0 N1 v8 }6 U+ i
{8 `) d0 {( |2 g8 O% O% U
    lcd.clear(); & u, v+ |; D( @6 {. F
    lcd.setCursor(0, 0);
' f. \5 {+ B  ^! T  r8 ^2 w6 H    lcd.print("    X=");
! O! B0 t' `8 v. Z; ?" S    lcd.print(current_units.x);" t- r8 C0 [4 h. h4 X+ v* g8 Z# i
    lcd.setCursor(0, 1);
, e8 A: J# U" x* I    lcd.print("    Y=");
4 J# Z* j1 E$ s    lcd.print(current_units.y);' F; ~5 |9 c# B0 R
    lcd.setCursor(0, 2);
! v) ?9 m; ~" _    lcd.print("Z=");
9 L# f# t, O* K: a6 m. v, O    lcd.print(current_units.z);
8 L* _/ ?- z5 ^$ H    lcd.setCursor(0, 3);
3 T) M2 V$ b( I2 H0 S9 Q    lcd.print("U=");
0 M5 d' b0 f8 F9 l    lcd.print(current_units.u);
, h; g: D! t( c' t  }) b/ O( G" @. F+ q, {/ F# B5 O( L
// else if (DRAWCount>=30)DRAWCount=0;
) s& E9 T2 f8 J  m; I+ O" Z//  else DRAWCount++;+ k4 G7 Y9 z& C/ l: b% r
void SPEEN()1 `6 t% Z+ o, _9 M1 k: S
{
3 ^" h# W5 A7 v* R, F. U# ]   delayMicroseconds(analogRead(SPEEN_CTL)+1);
, Y" K, F8 [) K! ? }
- _' W: L0 d. T, ?6 {1 F* ^: `4 x" p8 |! \/ X. p

! J3 _5 z' L/ f2 a( q //delayMicroseconds(analogRead(SPEEN_CTL)+1);' h4 h& h. O/ o! ?1 B  }1 ]) k
//if (digitalRead(BUTTON_SP_EN)) SPEEN();
* N5 w+ V- k; D& q+ R8 N/ \! c% t% N' K8 |

" Y7 c4 _& \' w* d! z+ M# 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-7-4 11:11 , Processed in 0.113856 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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