机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 24088|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑
( ~8 w3 u; F( E# m, {: l8 _4 ]  ~  [' o/ J3 v; ]- E5 D- @  E. Y4 _
首先声明$ ]* Y7 g% W: R% y0 g' e
我不是专家,业余捣鼓; s- m  l! G) e- j) _9 W7 e" g  a
源代码首先来自网络
/ {6 P7 A3 Y; k7 J$ n开发平台Arduino及Maple
7 e. I2 b/ L/ ^6 d# zhttp://www.arduino.cc/) v- j5 a, a: y0 l
http://leaflabs.com/devices/
, K* d" O& J* `1 t& [. M& I, L0 q国内活跃社区
# k) U1 \: ~2 I, P/ F" P& Fhttp://www.geek-workshop.com/forum.php
8 O7 K4 C8 G5 M6 D/ R; F- v( j0 l. {* o) V7 O
竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验
/ p; L! B( q5 g  V4 f来入门了解熟悉思路架构等
/ E/ D0 {7 m- M( V# B
0 {% f) Y! |4 G5 x5 c. b+ g( q- M我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,$ q& T! m( @" V1 Q( Z* ^
许多功能还木完成,不过作为低档次得应用可能还有可能/ |5 Y" w5 I+ f! ?
+ |! a3 s" W( R$ K! P. f# S
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧
, v! r* J% e+ l3 F# c( t
1 ]3 f5 g) H" H. @- p- j7 ]1 t! k# g拒绝 所有的求, K+ Q$ h4 I3 c) z2 t9 r; b8 p! b' z
求人不如求自己 不然木玩
: {4 k) a; ?2 j/ a7 _8 G8 ?3 Y, _% \1 v& j- w; r2 l" A2 C  ?6 _2 B+ l
高手绕道 谢谢!7 k; F# k& ]6 w9 ~

. d* R* q4 l. U; m' j* Z' k
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 0 i* E" e( s+ w  h: d! o( l1 z
6 W1 ]' n$ S! P& X' T9 p
GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东
' A7 @& \6 V$ q; ?贴代码先1 Q6 p4 }6 {6 P
直接Maple的,某宝许多超便宜哈3 B  d- q3 p1 _4 \: Z- ]2 I1 t
晕,我怎么上不了RAR?# ]6 [7 q9 G6 V7 i4 A4 v' O
想玩的留下 e妹吧
* Y/ d7 i& m+ S" o第一个妹麻烦传第二个妹哈
* W8 k4 r7 u& H$ `我平常杂事多
* D) n8 N3 e- C1 u! R谁放网盘上再,麻烦开放下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap$ l; l0 H# a& Q# Z2 H* l9 v
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)7 f7 [. M7 S- x" a; M- s. ]% T
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)
6 w6 N0 `2 `7 q( Y// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
; h8 q+ L/ m, g2 i9 n; ]! O; D: T% K; d// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)) ~% v1 A! J6 A" S% M6 w
, z6 k4 I4 n9 Y" ~
// Arduino G-code Interpreter for Macro / Micro photography
5 s6 k# h/ h, h2 O// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
0 l& _& Q) Y3 f: u0 p//modified by YaoHan China 2010.2.15) I( ?+ I7 C$ M" K& |' q7 _
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)/ x2 I# u6 W; ^+ a! c  C, D8 e
#include <stdlib.h>
' D; O) @/ s5 d#include <LiquidCrystal.h>
; y+ ^; C9 ^  H5 J, B) w
6 v" @9 v: w/ n//启动(高电平有效)/暂停(低电平有效)  默认高电平' D" T5 R/ S# I" D) `
#define BUTTON_CTL 35) |  m+ m" w) O! V/ U& y) N* f
6 f" n/ ]5 U' h* G) C
//点动模式键(高电平有效)  默认低电平
% \2 F4 f" Y# R5 z+ E#define BUTTON_SS 362 S% }( `/ \9 H; \+ R
$ @+ E7 [2 a- n( \& u
//点动键- y7 t' {) m% V4 |! A8 |
#define BUTTON_MAN 15+ p2 a% j6 d! C) p  b5 \
0 Y2 @' f% u, X7 R/ }+ O4 t
//电位器速度控制
: f8 u( T/ Y4 x#define SPEEN_CTL 161 L8 W0 B- f  R' A
( C7 R9 A1 _. u  f$ p) G1 D- [
//手动调速使能
1 a) g( N- W0 F* Y7 ?) D#define BUTTON_SP_EN 17! |& l- R% @0 ^# M

* O, Q9 k+ j* ?# B- V//LCD 类型 1604或1602
+ d( ?/ b1 A- o& x. ]#define LCD_TYPE 1604
# p% K  I% v8 n( p# Q//LCD 引脚定义
9 n5 v4 N' Z% \+ C#define LCD_RS 23& d1 d' y+ m" V' d
#define LCD_EN 24
7 c6 E$ ~/ X# `& e5 r$ o#define LCD_D4 255 I: |& J/ @/ i1 x# `$ w  P, n
#define LCD_D5 26
5 p% O4 _: J0 m2 Q$ e& @0 B* A0 v#define LCD_D6 27
0 k  F6 Y9 h- a#define LCD_D7 28
- p: M1 J' M7 _5 p/ C/ {8 @* T% r  K  T
//命令字符串1 C- Y6 j9 {2 }2 T( j/ I% P
#define COMMAND_SIZE 128
# r2 m9 X% P9 F. F* q/ m! q. A& e6 _char word_old[COMMAND_SIZE];6 z1 k9 j& i# a# Z" V; ]6 a% z
byte serial_count=0;5 `' n. w5 U# s; b8 p0 t, a) v
int no_data = 0;
* l/ \* ~6 W% u" w9 z2 q//LCD 引脚链接 配置" L( `$ R) p4 r2 \1 n5 F
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);; x6 u0 Q; S# m. ^  b3 q% v: z5 I
//停止控制,高电平有效( w8 ?* U: K, J7 Y
#define BUTTON_STOP 30% q# Y" N& t! p8 s
int stop_flag=0;5 f1 `$ U, g, l+ ~- E9 |; f
( O- J2 Q+ O" w- l) k1 i
//暂停. h# C9 y2 f& m6 b  Z$ |% G
void pause(){0 p4 G) [/ W  q/ _
  while(!digitalRead(BUTTON_CTL));
: ^% q" Q0 ?6 `$ r}
( Z' l  _" ]  D! F/ Y  a$ o+ j1 [
void stopper(){
" q' ]1 m* \: C  i3 ~% W4 i  delayMicroseconds(10);% H7 J% y% r# V, a/ h
  stop_flag = digitalRead(BUTTON_STOP);- c  {. g9 X( T* ]6 q# I8 W
}) ?# i  @. b! q

7 U% w$ F" B  }; P/ d& dvoid setup()
3 }8 y+ G# }. R$ W, e{
7 G! q. d& b4 o) q  //show start
/ o* X6 O4 v/ D1 h# W  SerialUSB.println("start");
* a4 I, m1 |% E7 _1 L( @+ u
3 |, F& @# B% z- z$ H) H4 i  //启动lcd! p. h, _0 h6 U, C) R+ z8 c
    lcd.begin(16,4);
9 o2 z4 ^/ {7 l8 l3 W    lcd.setCursor(0, 0);
( i0 v6 C  C4 P- v. g0 Z    lcd.print("hello, world!");
' u9 n: u- W$ \& F" v( X0 q
$ j6 f9 ~4 ~* i6 n. j  //初始化控制引脚及引脚功能! g0 E- R& U6 s
  pinMode(BUTTON_CTL,INPUT_PULLUP);+ z! u& p0 ^( l+ X& ~$ t
  pinMode(BUTTON_SS,INPUT_PULLDOWN);
, E7 v# \3 z% M  pinMode(BUTTON_STOP,INPUT_PULLDOWN);" k1 ~0 b7 e3 t+ i! X' F" Z& ]
  pinMode(BUTTON_MAN,INPUT_ANALOG);' Q* _( S6 y, q" P0 q0 f
, v. [3 a/ f' P# U1 W
  //控制引脚的中断8 `% U2 X0 Q9 v& S* L$ n
  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断
/ g6 N. {- I1 U* L/ H# s. B  attachInterrupt(BUTTON_STOP,stopper,CHANGE);
# q  b7 j& s- d/ K5 |6 R) ~0 @- S+ K% d4 N/ y) |$ _7 r$ ~
  //other init/ ?5 U2 Y1 k) A% `/ k
  init_process_string();* _/ |" Y4 A. K) A+ D
  init_steppers();
: M/ r3 k5 b+ z# B# C9 n6 C# @  init_camera();
& c  R3 Q  C0 i3 ^* y( t9 Z! S! b2 n4 m- e2 i/ Q+ `, z
}
$ \0 h1 R% k2 o" ~2 G8 `8 {2 x" a5 o9 i4 S; P
void loop()/ ~' y* v( y6 S' q2 A2 W5 _
{- I$ H* V4 m6 e- G
  char c;5 K/ a1 U0 s) P+ ?4 \1 o" B) d
# F9 L6 ~& i4 f1 ^. B  n
  //读取输入的字符
1 ?- l  P$ y- m; {8 V1 w  if ((SerialUSB.available() > 0) && (!stop_flag))
2 K+ }4 T5 _, M$ w4 k8 [9 @, M# {# Z  {
6 t  E/ \* n! g5 }3 j$ S    c = SerialUSB.read();
7 e) y7 x+ c( M# ?    no_data = 0;
/ Y+ i1 M+ ]& S* G, z. I
  F) O" f! h. v* [) `1 F0 K$ R    //换行符代表一个命令的结束
0 ?1 O' N; \4 N4 u+ W    if (c != '\n')
- ]0 a  }; y1 e# k% p: `    {
/ v0 t. e3 Q2 W      word_old[serial_count] = c;% Y  m4 J7 e# g, n4 z/ o& E6 a
      serial_count++;
4 K* T: n7 z& k* d. ^, s- ?7 q
0 d, O  t$ N# L# e    }1 ~( R" ^0 Q, m4 |# C/ b- t2 E
  }
' Q8 l5 M: Q9 ^  //标记没有数据输入) r4 R: R" s/ l8 u
  else
/ `6 m! W$ ]- d7 r+ @  {
- k) {& W  S' v* B6 @& l7 X% {    no_data++;1 U+ D* G0 C, t" C
    delayMicroseconds(100);  f! ~$ |9 _3 @  Y2 q) i
  }6 R: Q: l" G/ t- o
! H* s; T+ P- e& @
  //if theres a pause or we got a real command, do it
( C( [8 L2 A0 s% @  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))
0 B( h# J7 O: h9 t) u  {5 Q: z% V9 k4 g6 D: r

( p) T* @/ }9 `( t; r    //处理命令
8 b1 ?2 M1 c8 ]    process_string(word_old, serial_count);& U/ ~$ D  E) e' }3 s! e7 A
4 Z% a8 G. m% \9 g1 l" z
    //清除命令
! |2 H1 l# [6 G) n( t    init_process_string();
% w( X7 p% A, l% Y* I  }4 B  j0 p7 m( \/ y( I( t+ g+ l
3 `1 L  G: ]3 s) x
  //如果没有数据关闭电机( K" Y0 [, ~1 q
  if (no_data > 1000), M+ B  {9 V) H$ z
    disable_steppers();; W# \7 I/ y3 O) a5 w$ [# |+ D

5 o8 K+ x  r9 p% @4 @: f  //如果ss键按下进入点动模式! R. m$ n- H; X8 @% c- I4 a
  //if(digitalRead(BUTTON_SS)) ss();
' c& _( R. b. g" w! \! E. o  run_a();
- ^' C; b- B9 Y$ z}1 h& T9 ^$ Y8 L3 T) \' M3 N2 O

9 @3 c! |2 N* _1 Q  N) z//点动模式
/ {& K' W% Z+ L; x, y+ fvoid ss(){; U+ ^) j7 l  J3 h

1 ]5 J( v! R/ E+ Y$ Z7 n  delay(1);' I( k' ]6 n* ^2 _" g1 a% _
  if(!digitalRead(BUTTON_SS))return;
+ @3 h2 V8 _5 D  if(!digitalRead(BUTTON_SS))return;
$ _. {& {- E9 P  //init_process_string();9 t* \' R( S  [/ @0 u
  //init_steppers();: s4 ^0 {. `8 _3 U% e3 L
  //init_camera();
; I! e, R# V: i# _% [4 w! w6 ]# q7 ]% v- t% C1 @' w
// SerialUSB.println("Step By Step Mode");
7 u- W. ]  X6 A4 `2 U. i  l//#if (LCD_TYPE == 1604 )
' P+ g' N. {9 y. r0 o  //当LCD为1604时显示要处理的命令
* |5 p* r, ]- Q. k7 `   lcd.setCursor(0, 0);
, ?( w, Q" O9 l   lcd.print("Step By Step Mode");4 u( n: O( l3 i, M, ?' ]+ \
//#endif1 t+ L3 P7 d. t5 x/ o1 D
  process_string("G1 F5000",8);9 g6 b* c5 D- V
  process_string("G91",8);9 ~* J# h# L7 x/ F' I% b  Z
  //init_process_string();
/ b' a! o. K5 U' d  M" l* q) S; f/ U+ c, `* ~3 p
  while(digitalRead(BUTTON_SS)){
8 D3 K/ o1 f! o/ P' Q4 ?! F" L    int i=0;0 o) Z/ Q* T9 i8 _
    i=analogRead(BUTTON_MAN)>>9;9 b) q& l$ L: u2 y6 b
    //if (i==0){break;}$ `7 X1 y8 j6 G3 u
   //SerialUSB.println(i);! K7 G$ `: n5 K! V% X, A4 x
   //delay(1000);   " X7 j9 ]/ N6 b% L  \; {" J% H

! N9 G7 }, ?+ y3 ?) M   if(i==2){; l4 L& l, L! ]9 a. c' E
      set_target(1000.0, 0.0, 0.0, 0.0);
# I8 O+ ^8 ]; i3 l8 J      dda_move(getMaxSpeed());8 y% d5 E5 o  L) `+ F% s# [
      if(stop_flag) return;( M, C* S. X& X- E
      //process_string("X0.01",5);, L# G" m# h5 z3 n- r; i, |
      //init_process_string();
: a8 j: q. _- P+ Q" _" B8 d    }
; u  S3 e7 [/ G* O# o    else ! ~: u* F) L% [( e4 E: ^8 C
    if(i==5){( i. A' I/ u9 B3 f) I: M% D" }& x
      set_target(-1000.0, 0.0, 0.0, 0.0);- u/ ?: P- p1 |
      dda_move(getMaxSpeed());
2 z! e9 Y  t6 M% X9 k; E      if(stop_flag) return;# @& y3 {; d7 F5 @; s. q8 |! B7 o& a
      //process_string("X-0.01",6);
; A3 l6 |1 Z) ?& G# m: n; ~% j      //init_process_string();
+ i3 a5 F! j5 D    } ; l% S# S  k% i3 y1 x  b1 X
     
. L  U: q9 j2 a, V2 i( S
/ ~8 A. k% F5 k3 C3 W, _  }
' L; ]4 N1 E# r$ `+ ~
1 I% l0 w+ U4 B- A  //init_steppers();( L3 Q) G1 ]6 r
  //init_camera();2 t% y! d& O, s7 Q0 u( I" y; B% R" u
   // SerialUSB.println("Return To Normal Mode");
9 _7 ^+ X  D* R5 D5 `/ `+ |& Q. U( F     process_string("G1",8);
4 t4 T  ^/ I0 Y0 X+ S. U  // process_string("G91",8);6 q/ [& M6 d6 q+ V! F
    init_process_string();' H, u  t* h& x; J- Z2 p
//#if (LCD_TYPE == 1604 )1 R0 c. @$ s0 H0 l- |! ]
  //当LCD为1604时显示要处理的命令
% b/ m- C0 R9 [: [8 c/ A  // lcd.setCursor(0, 4);) r* o; ?" M- A; \7 X
  // lcd.print("Return To Normal Mode");
. |, @. U$ h3 `5 z//#endif% V* D# X" o% V$ g2 H! _0 x+ `! ]+ N, m
}
$ E; B7 w+ {4 f# \& M7 i
) t: _9 R2 i$ [" bvoid run_a()  t) v! g8 N* }
{/ }8 F4 O3 F3 k
  //delay(1);
6 n$ [3 u* P# R% @% A, y- A4 m  //if(digitalRead(14)== HIGH)return;2 T3 W2 {5 m6 a# Q& w, r% T) q! C
  //if(digitalRead(14)== HIGH)return;9 R2 F9 [" [) M8 f+ l  d0 Q+ V' n) l
  //process_string("G1 F2000",8);4 y" a5 U9 _) z
//process_string("G92",8);  _. x% Y7 a5 l. s2 H% v+ Y
process_string("G90",8);$ V3 z5 u; V0 H2 g
process_string("X120 F10",5);: V# U; k* h* d+ G7 }. [1 d( f
process_string("G4P2000",8);
  ]2 b2 q' R3 w/ h# D2 ? process_string("X10 F10",5);
$ g. _6 B! V+ g5 V; V2 X// process_string("M101",8);
( Q$ I& Q/ ^& r2 G, x6 J// process_string("X-50",5);$ \7 Q# d; A. h6 {/ k
dda_move(getMaxSpeed());6 W% \+ S( R& N: G5 u* J
init_process_string();
- |4 }3 {1 j1 @; U3 S, E* R5 l9 i2 I
}. G3 {4 l4 R2 [" d0 C2 b
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:53:25 | 显示全部楼层

, a2 s) R" z. t: J9 P4 m4 W: h! G/ O* g) q* q
// 定义机器参数
+ l! J5 f; S) ]+ H#define X_STEPS_PER_INCH 400; {* V  O: @( g/ F4 o9 _" O
#define X_STEPS_PER_MM   16.0; g/ `  R- E( O1 L# H: U
#define X_MOTOR_STEPS    200
1 v" n( {- M  d) b2 ]! P% w# B
0 E) _, e5 [8 C- D+ Q8 b#define Y_STEPS_PER_INCH 400.09 `& }7 h& Y9 E/ j' T/ @; B% ?7 s
#define Y_STEPS_PER_MM   16.0
! ?3 Z( R; L( [% |! @5 }#define Y_MOTOR_STEPS    200
$ E) d4 {& L4 T$ e! q% ^. k, v6 h! J* J8 I  O
#define Z_STEPS_PER_INCH 400.0
0 T3 S4 \3 {  }9 t! W$ D' c#define Z_STEPS_PER_MM   16.0* P# K% k" V+ D, S5 P: o+ b% h
#define Z_MOTOR_STEPS    200
. h1 n! ]) z4 @+ E- b3 a% [7 g) _* d. }* W  Q" ~# Z5 U  p
#define U_STEPS_PER_INCH 400.0
0 N. o2 w& x: P3 n! V* m#define U_STEPS_PER_MM   16.0+ Z3 c3 D( n* }! J) P7 F6 G: R
#define U_MOTOR_STEPS    200
3 a, ?6 E$ b. V, v/ B
8 r. E- D9 |, r5 K: j8 a//最大进给率
) A1 N: d' t4 y  G. S' K* j- P#define FAST_XY_FEEDRATE 1500.0
+ `9 V( l8 q$ l# P  X% `#define FAST_Z_FEEDRATE  1500.0# i: m& E" d# Q- H' ~, d
#define FAST_U_FEEDRATE  1500.0) q' k. p- S% D) ?! C3 c' q& H1 H
// Units in curve section  v* c; G9 F1 @, h. l3 h8 g
#define CURVE_SECTION_INCHES 0.019685. m- I' E. B3 {- {, Z' m. D
#define CURVE_SECTION_MM 0.5
5 }2 B. t6 x+ e9 L, L# s
' t" c8 B  X  b. C5 Z! P8 h
8 k7 q$ I& ?+ `! T// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
2 D0 [0 o& r& ?+ \/ z$ ~# P, [// RepRap opto endstops are *not* inverting.
, o6 s0 W  ]' Z#define SENSORS_INVERTING 1  a8 I/ ?, @( [* ], ~- `% }

; o. L4 v4 P' G/ b/****************************************************************************************) {# ]6 h! o, h0 l9 d. J
* digital i/o pin assignment6 |( X* s) s9 ^+ K; |, a5 ^/ o
*8 K& {) f7 R) Q! [) T
* this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5
8 B3 I% i/ j2 r7 ? ****************************************************************************************/
8 a  V+ n; c. V+ W3 R' p1 b6 P9 n8 w' i3 `- o/ K9 H4 _1 s' }
//camera shutter and control pins; e! n; C5 q/ }/ ?
#define CAM_SHUTTER_PIN1 29
6 P. r# g+ L! l: h0 S4 }( H#define CAM_SHUTTER_PIN2 30
! d, M& k* P$ B/ |+ M% f& R//#define CAM_AUX_PIN1 31 // analog 0' e& N9 D1 \* K6 m6 Q
//#define CAM_AUX_PIN2 32 // analog 1
# @7 K* e$ |6 [' d4 }# f) v//#define CAM_AUX_PIN3 33 // analog 2
7 z  K* [* U+ b; }. t//#define CAM_AUX_PIN4 34 // analog 3# `% c' m; r- Z& Q! y3 q& \; ?
* \" w- L7 ~* k+ o' i$ `9 q4 ?2 S
// stepper driver pins+ q- R5 z; E$ Y5 m4 ]) \: u
#define X_STEP_PIN 7& T  E; ?9 A+ x1 l( M& B# Y/ h
#define X_DIR_PIN 8( I, W5 h, ^9 K- `8 k' a: g4 X
#define X_ENABLE_PIN 19
8 H0 {. o2 x. p& p& J2 l' R8 D5 T0 f  v) S: s3 z
#define Y_STEP_PIN 9( C& }9 C0 [7 J  s1 J5 C# z0 p4 D6 _
#define Y_DIR_PIN 10
3 K6 x6 J4 }6 B+ c1 f& M#define Y_ENABLE_PIN 19! G2 ?  \7 H$ C* E

' `) t3 U1 G; {6 M# j#define Z_STEP_PIN 11. i8 D: p, M: P( Z
#define Z_DIR_PIN 12
& ~. Z$ o# r! y7 j; @" ]#define Z_ENABLE_PIN 19. S3 X! T. J" W4 [: \- C
: U- p6 M4 D8 z% V. H0 o
#define U_STEP_PIN 139 u: {& Y& M( e7 S5 u# u
#define U_DIR_PIN 14
- y' f: ^6 T  m8 Q) t+ }( m#define U_ENABLE_PIN 19; I  G4 j" c3 a

7 g$ t! B* l: o8 t: b$ S$ b// limits not used right now
. i- @# a; u1 H" U5 q#define X_MIN_PIN 14
% V- f% _, v# e5 o; |#define X_MAX_PIN 14
- c8 ^( H& S3 l# v$ P$ m' A#define Y_MIN_PIN 14; r2 ]1 h4 N# f, z& u4 q
#define Y_MAX_PIN 14& w2 b( j* [' p. Z
#define Z_MIN_PIN 14
. J% q! c9 [2 e+ y#define Z_MAX_PIN 146 H3 J" ]$ r: u% q1 Q% i2 x$ z/ A
#define U_MIN_PIN 14
* N: m! _( R5 ?5 t8 @  c  b. v#define U_MAX_PIN 14
% K; K- d- M8 `0 I+ j
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()
, V+ h' ]- i3 H$ C6 x: Y{
+ i5 e7 S, c+ i  ]4 p  pinMode(CAM_SHUTTER_PIN1, OUTPUT);4 v* J# i/ M' H6 G! T# e* f
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
- L1 |3 k( n4 {# L: H  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
% |% \/ Z  ]; B' t // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
& X8 M# x! |1 ?6 r* C" I; b // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2
; b  B% d2 D' [) [ // pinMode(CAM_AUX_PIN4, OUTPUT); // analog 33 V5 s0 n: b2 C! f, Z: Y4 T" h9 F* c
}
" `" e/ c: y' s- H4 E% k' K3 R  U! _8 \
void camera_shutter1()
6 P$ D1 h3 Y8 z{* y# f2 q  ?8 H0 h
  // fire the camera shutter via relay...1/4 sec hold time
: W0 e* r- V& g5 m  W' g6 _& O  digitalWrite(CAM_SHUTTER_PIN1, HIGH);  m) i& b0 ?' ~- H' l
  delay(250);; P& X& Q  d# L/ W
  digitalWrite(CAM_SHUTTER_PIN1, LOW);
! q# E( q1 |  V3 }) O5 v$ B# @
9 w# N7 s; E; C}, L9 h3 R, ^5 ^' T, X7 h! Y; R
5 L- f* @% r$ {* v; F& k+ e
void camera_shutter2()
: G& e- ]6 p' O+ e7 c, _/ B{5 f8 T  q3 B1 X9 p% n7 K
  // fire the camera shutter via relay...1/4 sec hold time3 k3 {( f6 W" ?, D- g
  digitalWrite(CAM_SHUTTER_PIN2, HIGH);( Y4 }9 g8 K$ S- S5 z/ x& x
  delay(250);
7 G9 {% h, d0 U) k$ F& T6 c9 k* M  digitalWrite(CAM_SHUTTER_PIN2, LOW);. H$ y4 s  T  b5 }+ R

6 h/ h0 ^5 H# S. e9 [0 B8 D}
4 Y  \' A& q, B# E& D5 W7 ?/*
  y( y3 ^: {) cvoid camera_aux1_on()
# y& Q0 T' N% ?/ \+ F, R+ W6 i{
4 b& v( V/ T0 T, \9 p  // turn aux relay 1 on* |9 D0 H! V0 M; n3 j! J
  digitalWrite(CAM_AUX_PIN1, HIGH);6 w5 Q' ~$ N4 q
}
) |5 x# \  A0 h0 L2 ?; }# O- y
) B& V3 d; g% p8 nvoid camera_aux1_off()( a" i/ y5 }; ^( W
{4 J. K, N. }5 Q7 C7 V
  // turn aux relay 1 off
; I/ n0 n. D- M  digitalWrite(CAM_AUX_PIN1, LOW);2 V- }: A. e. M- G; p
}# A4 c0 v/ b1 b6 [* |# w3 @
9 L# z# w% f& Z0 |- J& L
void camera_aux2_on()
: P* l# t. ~. J0 S2 M{
* |; q3 b8 v8 ]0 E& [/ X  u+ ]  // turn aux relay 2 on
  K/ b+ d2 J( f: W$ I) D$ {$ x  digitalWrite(CAM_AUX_PIN2, HIGH);( o+ Q& j6 G9 g0 q
}
6 l' o  |) C( H: S2 Z$ A0 V9 n1 M* w
void camera_aux2_off()
1 L3 K. s$ o( w, T1 m0 J% r{1 O' c) C5 ~! r
  // turn aux relay 2 off
9 P; @$ `" Q$ |3 V8 D8 F  digitalWrite(CAM_AUX_PIN2, LOW);
1 L1 I7 u$ i! s9 q6 _) K9 W. d6 p8 u5 x}
$ r$ I4 {2 `9 N  w
( T$ H) s  i! H* K4 Y: N  K0 evoid camera_aux3_on()
5 n) D# b- Q# i{$ T# X% A% G- S- c( h
  // turn aux relay 3 on
; F: P/ r6 p  t0 O! x# N: m  digitalWrite(CAM_AUX_PIN3, HIGH);% O: O7 z& q7 [9 a. x1 b/ b2 T) V
}
) t/ X$ W7 {1 I. @/ D4 T
4 X/ n. d+ ?% H" F4 Svoid camera_aux3_off()
* y/ F- |& z/ b" Z: ]+ [( h{0 u6 y9 l: }$ o# r) J: n8 [1 J0 r
  // turn aux relay 3 off2 L4 ?. O2 N! a' v  m" C+ V
  digitalWrite(CAM_AUX_PIN3, LOW);$ U* k; z& I/ R8 q
}
. E5 d& X3 @. z1 E  g1 M7 I, g) l6 p5 Y
void camera_aux4_on()
7 ]8 p: ~; A  O, [7 g( P{
" l. {$ j; n2 @* q  // turn aux relay 4 on
1 ^0 B2 ^" Q" Y7 a' U  digitalWrite(CAM_AUX_PIN4, HIGH);
8 b( c& c7 i" w4 {9 ^}
! R9 c8 B9 V) Z% ?8 o0 ^- b  N/ I, A3 y- H( N% s
void camera_aux4_off(); J9 h: A7 B: N+ @' |/ h1 D* j
{. P! f5 w& N0 P
  // turn aux relay 4 off. Q* P2 v: J5 h5 ~+ \+ B& ?
  digitalWrite(CAM_AUX_PIN4, LOW);3 U# f' n. y' q  m7 H* b( N2 U
}
回复 支持 反对

使用道具 举报

发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错
1 M' [0 O, {' i7 N6 S' A请问楼主是玩什么的,用乐高玩具吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice.3 I' h! d% X0 q6 X5 b) X. \7 |4 O
struct LongPoint {% O! U1 i: c$ c. x% {
  long x;; Z! z1 z7 N8 Z+ [, x
  long y;4 b. z  l1 f" r/ h, X
  long z;; c" w% w5 ]" B  J) o
  long u;" m2 a4 _" W8 ^5 b" R8 `, C- K9 O1 v
};
0 B( C8 J, t; w/ T
$ ~; x. T7 d7 d1 c8 Hstruct FloatPoint {. G9 [5 z+ G8 l; e
  float x;6 }" O% ^/ b8 J' o
  float y;7 I, e: T9 @3 S8 F
  float z;) i7 G7 j, {) y0 C1 y0 T/ m
  float u;
4 ]' u/ i! ?5 A! m" q- c, D; a};: v( X9 o- t( I9 ~- r

0 O% A% |( f* W/ `! c0 eFloatPoint current_units;
1 d6 @! ]" D4 a' W: b) g: {# e/ v2 cFloatPoint target_units;5 k9 j. y" A2 F3 s/ `
FloatPoint delta_units;
4 g8 |& ]9 R' X  O  D# D0 _& l" f3 p
FloatPoint current_steps;) K/ M! ]  P7 J5 V7 L8 r
FloatPoint target_steps;2 t: m/ j( I. y& v
FloatPoint delta_steps;
0 I& f0 f2 Q3 n' D3 U
3 Q5 H' G* Y/ i% w/ kboolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式
3 ]; f. k- z1 N/ q* d5 J' p7 s
7 e# ~4 X' C2 q9 q//default to inches for units
. Z" l1 H  }( }7 Z- |float x_units = X_STEPS_PER_INCH;/ s* v  |5 |+ f! N3 }
float y_units = Y_STEPS_PER_INCH;
6 b; B5 v4 c+ M, i% [float z_units = Z_STEPS_PER_INCH;' g& h* [: m/ s5 r9 P
float u_units = U_STEPS_PER_INCH;
- i/ a% q/ ~. E& Bfloat curve_section = CURVE_SECTION_INCHES;
0 v+ G! L; Q- l% I
& j. K9 k) F7 S9 h% o% v$ G//our direction vars! G7 f+ L4 N1 f* X0 f5 K! y4 E8 z
byte x_direction = 1;
5 v% o7 W* X$ I) O) ?7 P5 o( A$ Mbyte y_direction = 1;1 f- p6 D, T7 L9 X
byte z_direction = 1;
: G2 l5 E- j6 o5 c$ C0 f2 i$ pbyte u_direction = 1;* T) f( m8 l" \4 ]+ f. p, I% o) |
% D$ l- @9 r3 C1 Z& m) @0 ]% `
//初始化字符串处理
: G; g( R1 x- xvoid init_process_string()) S0 O( f2 g+ [
{9 i  P' M' l6 y: K( `0 D$ ?/ C* B
  //init our command
7 Z- a+ f4 Q2 R" a2 ?  for (byte i=0; i<COMMAND_SIZE; i++)
# y2 j8 p8 W. O$ K4 y* i: W- x    word_old[i] = 0;
, @3 |9 P8 k, }1 {& |5 S  serial_count = 0;
+ k* L: n5 Y$ S1 I2 l+ B}
- _7 f$ v* d& P7 L- D7 a" P) f' F' L, W# s
//our feedrate variables.% ]' ~; O# S* K4 F
float feedrate = 0.0;$ o* ]  F- P) F
long feedrate_micros = 0;$ y5 D+ m+ k9 L4 L$ f

5 B* ~7 h" \  R! [//读取并执行命令" S- m' [. E! h* c2 |5 y, I
void process_string(char instruction[], int size)
# x* l, [- {7 c( p  r. S) k0 x{
: v4 {' F; t/ N2 ?6 h7 L6 D* X  //the character / means delete block... used for comments and stuff.
" @% R3 k+ c/ ~9 H8 A7 a3 W: c4 Q5 K  if (instruction[0] == '/')
9 G/ N0 n$ k% g" u  R$ G  {
. y( C4 E- K( X4 X+ t    // SerialUSB.print("ok");
, Z; F+ D# ^2 y" t    // SerialUSB.print(byte(78));
1 t1 l+ L" r( r    return;
" ?8 {; E, d1 B$ c$ b; }/ c  }* e. ?' k( X0 V- [8 D
  //init baby!. e# r  T- J9 J9 Z
  FloatPoint fp;
" \. [( X: u& D' }  fp.x = 0.0;' i7 ?7 m4 `& v: v2 |9 ~
  fp.y = 0.0;$ g4 o9 ?$ O1 p" U8 V1 l
  fp.z = 0.0;) r- R5 l7 J( Y  g1 Q2 C- T5 E9 V
  fp.u = 0.0;5 y4 ]1 S' P* b3 m  T3 T8 [7 S
0 S( B- ]* b4 L9 x' Y3 N
  byte code = 0;$ F3 j# O6 S3 ]+ u' b. I0 A

# ?3 r  O" a. E; W: R# X. O  u  //显示在处理的命令
) K" b# ?" v8 @# M! ^6 m, v7 g  f; c#if (LCD_TYPE == 1604 )9 e/ r- L$ p7 ]& ]7 P: s( K
  // lcd.setCursor(0, 4);, Y) m$ t: I% _& N2 c( B
  // lcd.print(word_old);
4 T/ V* d3 [7 ^  p#endif$ q1 @  o$ h. O  ?7 {1 a' T: n. C
  SerialUSB.println();
; P  l5 s3 N1 `3 K" V+ D) Y  SerialUSB.print(instruction);  [! s2 I9 c8 s6 ?( @3 d
  SerialUSB.print("\t");
8 \+ o: X0 @& u7 b7 d# E% N
, }+ w  V6 C7 Y; X1 O- @  //what line are we at?8 B( r3 P& j. F0 N( R1 s
  //        long line = -1;
' a+ m0 \7 `6 G  //        if (has_command('N', instruction, size))
5 `9 m7 m! q9 X7 x" P9 u  //                line = (long)search_string('N', instruction, size);& J  j9 d! l* y. k4 i
7 n, T0 q6 s3 e7 f+ f  X: r
  /** [+ R$ b2 X% y- A9 [  D! o8 B
        Serial.print("line: ");
; h9 I7 U$ M! m           Serial.println(line);* u- `1 \! D) p3 f2 U1 q8 c. z
           Serial.println(instruction);
' b/ C+ J7 R1 ~, F7 [: h9 k" i3 w   */
. _/ l4 F6 J' T, b5 L  //判断是否读取了个 G代码?
# i2 B6 [9 K/ p  m! C* g8 O  @# ]: R  if (/ X. l9 x' p, n( u' d% Q$ _' ~
    has_command('G', instruction, size) ||
& E0 Q- L7 b) L! {9 d; ~    has_command('X', instruction, size) ||
5 N- B5 P8 f0 x0 }0 |    has_command('Y', instruction, size) ||
. _2 n6 B! E3 G! f    has_command('Z', instruction, size) ||! _" [  |$ y; n! X0 G) N
    has_command('U', instruction, size)$ |1 x  w: _. H6 z- j! ?' x
    )6 b- I# ~% }  B( I
  {
/ _5 n, x2 l: k- O' m8 l' e  a& A    //which one?
1 H: o3 l2 C- U) i" C: x+ F. ]    code = (int)search_string('G', instruction, size);
: n4 k) {8 F2 d6 g: ]0 _/ c    // Get co-ordinates if required by the code type given
# `' @0 I) q: q8 R( r/ `* P    switch (code)/ F3 c1 X; [# s4 q) z& ]/ |
    {
3 @9 `& M$ Z+ E) f    case 0:
3 a5 K/ D; r+ z9 z0 Z& [    case 1:
3 K- ^5 R4 a* q/ T" m    case 2:* `9 x& y1 k4 \2 k) ^
    case 3:
8 }8 S% g" S  _# F" d: A2 g      if(abs_mode)* k, i7 [( D* ?) ]4 e
      {2 e2 w& a* ~  L! t, E$ ~/ f
        //we do it like this to save time. makes curves better.
0 j$ X. A" W9 S' V        //eg. if only x and y are specified, we dont have to waste time looking up z.
& v6 T% k. W, F  ?        if (has_command('X', instruction, size)): h  k) m4 R+ o% S; q
          fp.x = search_string('X', instruction, size);
, i$ u9 n; }* y/ ^/ R6 C        else
9 m4 J. ^  _9 k  r. n- O          fp.x = current_units.x;6 l) O7 L3 v  W) l
+ ~% a5 S# h$ G& M; k
        if (has_command('Y', instruction, size))
! P4 u* E/ v! s7 R: W          fp.y = search_string('Y', instruction, size);# N) F, u. w6 g& S# x) c
        else
+ r, H- |1 [. M0 l. y          fp.y = current_units.y;
: E# ^: n  M( K- Q$ s7 k6 k' ?' W% `, U' ?" B+ M0 z, ^
        if (has_command('Z', instruction, size))
4 |7 R- a" O. F4 N          fp.z = search_string('Z', instruction, size);
  }3 U# [2 ?: [, |        else. h& r) z" k! a$ T
          fp.z = current_units.z;
' [# r, V7 h$ w- h         
( l. f8 y& f7 x# k& [$ h        if (has_command('U', instruction, size))
' ^/ w7 A& r) |: T' j          fp.u = search_string('U', instruction, size);( A% n# n* E4 R* K
        else
/ u5 P* K. d6 e, p3 Y          fp.u = current_units.u;
+ b: o4 I% g, _* f9 P      }
! ~- M5 u  K, X2 A      else+ k/ _8 c: M- n3 }
      {
, Z! Y: |4 I1 r5 n' O        fp.x = search_string('X', instruction, size) + current_units.x;9 x1 t5 G8 t( D, _0 M
        fp.y = search_string('Y', instruction, size) + current_units.y;; m; K/ L) Q, R/ R( d# H
        fp.z = search_string('Z', instruction, size) + current_units.z;2 Q; `4 N- v9 y  g. e; ^- m* P
        fp.u = search_string('U', instruction, size) + current_units.u;
( M' J- R' R4 l+ O5 @# S* L* q/ Y& r      }
3 _; V  m( g9 l; m2 L3 Z1 L1 `      break;0 j, J& W. o& R7 e# ?4 Q& g5 T
    }
2 `4 |* Q& `3 @; h    //do something!
: I9 D8 @; E. ~    switch (code)
  G8 Y) ?- N7 j4 Y. A1 @    {# |8 w8 W; k& W) C/ r
      //Rapid Positioning4 T0 p; y$ L! T7 Z1 C. v
      //Linear Interpolation+ U$ M* r! B+ R$ V' ]/ ], n+ k
      //these are basically the same thing.& F; G# R7 D  e
    case 0:6 U' ~) E/ ^% X& x( x/ ~  r
    case 1:% B% o7 |( m/ k* A0 n) y
      //set our target.
4 T. |, G: H" ]! A      set_target(fp.x, fp.y, fp.z, fp.u);, u$ J0 n; G- z
      //set_targeta( fp.a);! ?- n- e7 K/ u' U) m7 N, f
      //do we have a set speed?1 Q( I- z+ i, L: g
      if (has_command('G', instruction, size))
5 i/ Z& C7 h" u4 Y1 S+ P, @: v      {
2 P6 q8 o5 d* ^$ {! p4 F6 r        //adjust if we have a specific feedrate.
" Y& N/ i( Y! u+ w) c        if (code == 1)
8 t, s* `+ g, h0 o3 @) @; O        {* |5 I( ?: \) Z
          //how fast do we move?
7 H. Y! ]9 N2 E& v          feedrate = search_string('F', instruction, size);
1 I5 d9 q; i- ~( n. h2 {          if (feedrate > 0)
8 z% o4 T9 S2 W, L2 g' W- N            feedrate_micros = calculate_feedrate_delay(feedrate);
8 ]) M# T0 z' l8 p          //nope, no feedrate* I- f# R) H- Y8 Y
          else+ g. c' @4 B8 S
            feedrate_micros = getMaxSpeed();
' h# q0 y! Q0 P/ b% f! E        }5 y8 z/ v- {2 k; s0 [9 ~) ~) k
        //use our max for normal moves.
. d9 C" }  |. Z$ r5 n1 H  v        else
5 ]( o$ J: W/ `. n8 F9 g          feedrate_micros = getMaxSpeed();
- v4 Z. w$ V8 N0 S$ ~" q& v      }
$ _5 ]  i& Y" y! @      //nope, just coordinates!5 T$ R) G7 R. p$ ~
      else6 \/ S9 ~7 S2 ], r# X3 p
      {# T  F" O5 q" k9 y" U
        //do we have a feedrate yet?
$ u0 E  _; s2 s6 U9 w        if (feedrate > 0)/ y) H. {7 l4 N2 y6 L$ s- I& n
          feedrate_micros = calculate_feedrate_delay(feedrate);8 o" w5 c7 d% l- w8 \
        //nope, no feedrate
! L1 c# }2 e4 u/ R) ?- Z! z        else( s* Z% v% K% w% K2 W9 }* \
          feedrate_micros = getMaxSpeed();0 t5 \; p( U9 ~. s$ C: B" {
      }& B( C  L; e! l$ v; ^

6 {' B5 t  ^1 L1 Y& h( ?0 f) \      //finally move.' ~6 b2 p; W+ F: J- }# N
      dda_move(feedrate_micros);
2 w+ Y5 R; x& {  \1 s) p* s7 g      if(stop_flag) return;
$ ^5 C2 ]; D' d# f3 t: T3 [      break;8 A4 W; r9 X* I" u
0 |3 J) c1 d1 {7 O9 o* s1 [
      //Clockwise arc
0 W: j; c, P5 S' T- w( V, _    case 2:1 @3 g6 ]  c2 q! E  d& o2 w% w. c
      //Counterclockwise arc( T- S6 O' o, i; n, t- v
    case 3:
1 ?5 [! w& Z+ ~5 L; W1 E      FloatPoint cent;
; {3 Y5 K; y4 v6 A      // Centre coordinates are always relative/ \  |* S: m4 R; n$ X
      cent.x = search_string('I', instruction, size) + current_units.x;
2 i8 [, E( n. O0 e$ W      cent.y = search_string('J', instruction, size) + current_units.y;! k5 |8 A" g6 W% K/ C! R
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;. ~4 {7 B' j1 |# u& G
% y" ]! j! j, I7 J; @8 a- j; J
      aX = (current_units.x - cent.x);1 T; P+ A" h7 \' ~- a- L& @$ s
      aY = (current_units.y - cent.y);& u; Z+ [! d/ u1 I
      bX = (fp.x - cent.x);
2 W4 f: F. t, v# T      bY = (fp.y - cent.y);6 K3 y/ ^8 |" V9 h) q  {& m7 |6 u$ S

- x2 m/ G0 C# P* O. o2 f( q      if (code == 2) { // Clockwise9 t# \5 f9 F: G0 T( g( G$ q$ i
        angleA = atan2(bY, bX);2 c9 _3 z7 s+ ^3 b; R: l7 D% g6 V, X
        angleB = atan2(aY, aX);7 `/ h/ `* U3 d. _
      } 4 Y, ?+ I# d0 b) u2 P6 f9 `- o
      else { // Counterclockwise
4 X7 |& F( J5 H! l        angleA = atan2(aY, aX);+ J9 d) [& F( [7 b* \  f
        angleB = atan2(bY, bX);
3 ?% d% l# Q! n2 i9 M6 T' s+ W      }& L; ^: `% G! f8 @
      // Make sure angleB is always greater than angleA3 `' j# i8 ?3 l5 o; a( F
      // and if not add 2PI so that it is (this also takes
7 a- ~  @& z3 l3 _& {; ?$ K) l0 \      // care of the special case of angleA == angleB,
8 ?0 f; c. U; m& b9 j      // ie we want a complete circle)9 t3 ]2 J% ^$ ?% h0 M0 p
      if (angleB <= angleA) angleB += 2 * M_PI;1 I  |5 e9 X+ B! |% i1 v3 c
      angle = angleB - angleA;7 \6 Y" ~: H2 @5 E
+ a6 W2 ?5 o+ {' W, C3 |
      radius = sqrt(aX * aX + aY * aY);
* V  H+ I. `2 S1 k3 w      length = radius * angle;& L2 g3 i  ^5 w
      int steps, s, step;
8 {3 S) m5 Y& @- X5 z9 L0 ^      steps = (int) ceil(length / curve_section);
6 |* A3 g& k3 K5 \0 X
$ r) c: n/ Z8 ]( G, M/ ~      FloatPoint newPoint;
  ?, M$ v; ]+ I% P      for (s = 1; s <= steps; s++) {0 |" K+ ]" z$ F: d! m+ q% M7 x) t. A
        step = (code == 3) ? s : steps - s; // Work backwards for CW
, j* q2 g$ L' p: i0 j        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
/ r' m) B3 L, c+ n1 C3 N3 f7 Z        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
! ~# ~: F: N) b  d- t& n        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
. G* l9 ^! }2 T: A8 f. R) \7 @; b/ l* h# R: I" p0 _
        // Need to calculate rate for each section of curve4 M: ^* z0 P. J2 ?0 w5 M
        if (feedrate > 0)
$ M' K# |, G0 P3 r6 b4 v( [! T          feedrate_micros = calculate_feedrate_delay(feedrate);3 W' j) ~7 e8 a& B* _
        else2 I$ O" d# z8 l! V( A
          feedrate_micros = getMaxSpeed();
: X9 f. K* w% {# t2 q1 r2 R. @# W# G, Q) k8 W4 H
        // Make step
9 q% C: k; |  H        dda_move(feedrate_micros);6 Z& `8 B5 ?8 R/ \# t( r. q
        if(stop_flag) return;; \7 Y9 ]+ j5 B. s
      }
/ [& S" E5 l4 K3 }$ e- `+ K# P3 B/ L6 [  g6 D8 P! M$ x
      break;; h4 Y( g- p) @% R- E3 o

: o) N( v% v! B7 j, W8 H- g      //Dwell
+ ~/ w2 D2 }. }4 |( k2 \    case 4:
" g7 f9 s1 h+ @2 L3 r      delay((int)search_string('P', instruction, size));4 A1 t9 A$ j- M  t; d9 ^
      break;5 ~! f; j  `8 y% |: B  U; A5 T- |

9 ~6 T2 {/ D' v" r$ D      //Inches for Units2 \1 y5 n! B  V
    case 20:3 e% t2 v" r; x2 l
      x_units = X_STEPS_PER_INCH;: C$ `3 J3 S# r
      y_units = Y_STEPS_PER_INCH;3 C) X% R0 w8 i. O/ P8 l1 u2 H
      z_units = Z_STEPS_PER_INCH;
1 e! a& a% _+ G& C      u_units = U_STEPS_PER_INCH;: r6 j3 K1 e) _0 Q
      curve_section = CURVE_SECTION_INCHES;
0 Z7 B4 X- X- |: V$ o1 j      calculate_deltas();
6 E6 q6 q0 B4 e! w# j: y      break;# n* l2 j8 _' G% q
( s) K+ |; e) t; v* \  J7 u
      //mm for Units6 v7 M. h4 b( W+ q% F
    case 21:% h. R5 A  ^5 T# a& |" s! `$ }
      x_units = X_STEPS_PER_MM;. y6 |6 F3 l% f2 y: l) u+ `
      y_units = Y_STEPS_PER_MM;! U% l: ?* R3 b: {
      z_units = Z_STEPS_PER_MM;
! U+ S0 g3 B4 n5 q4 u4 y* ]      u_units = U_STEPS_PER_MM;. ?; ~$ H  I$ E# |2 X& ?
      curve_section = CURVE_SECTION_MM;1 {, ^& E; l( y, ], {5 Q; \
      calculate_deltas();; A  f$ `/ I5 V# m6 w
      break;
; d6 _( l: F7 s" f* W8 t8 w6 p+ m! j: A) k
      //go home.
: V  c8 R4 @' d7 ?% j9 z+ x* e    case 28:
8 Z3 N' q" U. v9 Y4 Y# g      set_target(0.0, 0.0, 0.0, 0.0);
4 x% Q  u  B* K" g" B      dda_move(getMaxSpeed());
& R$ Q, f( O( m2 p2 z      if(stop_flag) return;+ k& @* T! C8 E3 k
      break;
. k, p" ]: X3 [6 H$ Z
" E; [8 T* V1 x, j3 E: W0 U  x      //go home via an intermediate point.: @" `6 n- s) I7 P* t. d% u" Q/ w
    case 30:7 [" F4 Y; T6 E' ~! U
      fp.x = search_string('X', instruction, size);# n7 D' [+ D- ]4 q: f
      fp.y = search_string('Y', instruction, size);
8 O: J" i' X* i/ v7 w      fp.z = search_string('Z', instruction, size);
/ Y3 Z! N2 g" Q3 ?" n* a      fp.u = search_string('U', instruction, size);
: g. W$ {/ c0 Z  K+ K, S      //set our target.0 J, i: t( s7 U! X% L8 ?
      if(abs_mode)6 k$ h( a7 O+ s7 ]
      {
' {4 K+ C6 s, A+ \% S        if (!has_command('X', instruction, size))
; `! V0 |7 T$ P+ v3 |          fp.x = current_units.x;
/ A; h% I- A3 l0 _        if (!has_command('Y', instruction, size))
; }) E& m: P6 x8 q          fp.y = current_units.y;" U$ e! E) W5 x# `* O
        if (!has_command('Z', instruction, size))
, l4 M- h5 y) F- m9 S* i          fp.z = current_units.z;$ a8 v1 J9 k. A" n% a4 Y
        if (!has_command('U', instruction, size))
6 W( J: C3 E, j# e          fp.u = current_units.u;, C& v- |! Q* q
        set_target(fp.x, fp.y, fp.z, fp.u);
5 v, ^, F! n- K" B        + d  T0 s) `1 `4 E5 ?
      }1 g: t4 s6 Y7 _" N# Z! f. e
      else* @6 x: j- \( g
        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
1 T- Q8 p0 v- V( _; n       ! f5 F7 ]; `# d4 c( y5 L5 F* z/ |
      //go there.
0 D& F( U6 k$ h: }- g. f0 C      dda_move(getMaxSpeed());8 }! W6 d, z7 ~  _, _2 J: U
      if(stop_flag) return;
* `- K* ^( q' j; m$ P( q0 f2 ^4 O) R3 k9 N7 X
      //go home.6 P* I4 d% M9 D9 a
      set_target(0.0, 0.0, 0.0, 0.0 );
8 h; G0 i3 r! P9 l+ A& A9 ~; G     
+ O9 m& {9 `( o1 J      dda_move(getMaxSpeed());
0 J3 j6 g- }4 U- D% g1 z% O* p      if(stop_flag) return;) p9 X8 V$ W( i* ~$ x; v
      break;
' F, D6 t- A& Z* C4 Y& p" @5 {
& x- n2 Q4 J3 z3 l- V0 K      //Absolute Positioning: e4 o2 ], F- u  W  l: P
    case 90:( g1 [- c, C2 |: n8 d; O+ ^
      abs_mode = true;; c1 j# }: g: M8 O2 `
      break;7 v. ]  Z& z, R2 C6 v

! `% f/ [# r+ P  w, H3 @      //Incremental Positioning. M: m: t" V* H
    case 91:8 G& I1 C) \) p5 `; x& O7 Q  [
      abs_mode = false;
+ _0 A, }+ b0 y/ R9 g      break;
0 b% W+ l: W1 ~) A: Z3 {% {" u5 `2 D2 o# }( S
      //Set as home* f; T% V. W# \8 u& z% A" Q" }
    case 92:
2 Z- i' N6 ?1 a5 L3 x! j/ y     
2 g" E: w6 x6 H. s! S+ c  S      set_position(0.0, 0.0, 0.0, 0.0 );0 c) B5 f( I5 M& B- @. [6 k" ?
      
; W2 @1 X2 h. Z, [3 o1 F      break;6 V9 d. I" D  k2 L5 T4 O2 K
: J  X1 a2 ~/ l$ \( |  @
      /*6 N& z. E- o* u& t. P
                        //Inverse Time Feed Mode; w7 m' P. B5 p/ m* V
                               case 93:/ k& p* j# l# X$ P$ V& m8 l
       $ k' e& N; v  \+ F+ m  W; x( u( o
                               break;  //TODO: add this
+ c9 ]6 E, K+ e; G! L) K8 B5 w         A7 Q- q3 i0 b* K
                               //Feed per Minute Mode
2 y' f7 [" s! @4 f                               case 94:- l/ Y# A* A; D$ t0 v
      
- Z; q1 X( E1 Q) T& \% A. p( Z' L: e                               break;  //TODO: add this
2 S9 M' f3 A4 x$ p6 ~4 G       */5 a! p5 O  W: I0 }* m( `
  E8 h$ v; J* t# M3 K2 W% W
    default:
, `8 ?; `; E% F3 Q3 \  y      SerialUSB.print("huh? G");# H$ |5 q* \/ t* D5 q
      SerialUSB.println(code,DEC);; V( J( y) e2 \* t
    }4 G6 L2 n" I5 g9 I' F7 i1 j8 O
  }2 }1 n- N$ T1 ~+ }2 J
! [' ?( Q; w- E& Q* T: e
  //find us an m code.- k; r2 A9 a; g; a( v: }1 J
  if (has_command('M', instruction, size))
' S- s: q/ E" A$ l* _  {
, {! d/ X9 O1 o2 k5 f) m# g, c    code = search_string('M', instruction, size);/ ^  ]; |- ^+ P4 R3 B
    switch (code)
' Z4 Z% T; z% Z0 {, U5 e    {
/ Z4 ~0 E9 c4 ?& ]/ X3 K. t      //TODO: this is a bug because search_string returns 0.  gotta fix that.7 I; g6 {0 O3 |) U" I  S- u
    case 0:
* f: Z# F8 U4 a6 L      true;2 D2 k1 b" W( H" q5 }
      break;
' g) g; p% C" s
# O2 }9 l, W' ~2 _2 T    case 100:
7 Z: y; X  I" \& l% k      break;" W4 X3 D. @7 B$ p4 \
* Y4 Z; p$ m$ ]  ^% [
      // fire camera relay
2 p2 w! j2 q' ~4 o1 y) t    case 101:$ i3 F& Y. G$ [& [2 V; ?
      camera_shutter1();
$ V7 u3 r2 W, T# ~2 s& W      break;; f9 h5 M- Y5 Y( G: l
- \- c- O) D. v& k3 ]' A+ r
      // fire camera relay2' n/ @6 l: ^2 W
    case 102:
" ]$ b  F$ }* s, D+ E( C& W; l      camera_shutter2();* }. G8 L1 W, q7 c
      break;, p6 B% x0 o- ~0 O
/*
7 r7 z/ B# F3 {4 ]      // turn aux 1 relay on
7 X1 V9 j2 n" W/ d    case 103:9 N; \0 w8 K, n0 P1 P6 \. t$ d
      camera_aux1_on();( S) J1 N; Q+ J' w& K1 w
      break;! J9 w. D( Q+ K, ]) r* N7 N

9 k, H$ {5 h2 I      // turn aux 1 relay off5 h# Q. O4 @! A4 C! Z5 r$ j
    case 104:2 d# F/ _2 G% Z$ B
      camera_aux1_off();
& K6 w8 B$ Q" w% [5 E+ m; G4 H      break;
% L( D0 t5 f# L# a2 Q  I# `, C1 R3 h1 ~
      // turn aux 2 relay on3 [# p* u8 q/ D/ x7 K; }
    case 105:' v' e  W( B5 Y2 B
      camera_aux2_on();
6 {1 L3 e, N+ |3 s      break;) D2 Q4 v: ]1 F) f- c- i" ]' p. @9 L
$ i: E$ {5 D9 j6 U
      // turn aux 2 relay off$ q5 O# a" o: j4 C7 F
    case 106:+ x) W3 r/ L8 S) ]& E
      camera_aux2_off();
6 a8 k+ [/ g  |, c& c      break;* R* T- m7 t7 Y5 }1 e
- ^7 B" R* v* a- X
      // turn aux 3 relay on3 W- d# g& M8 L5 Q8 M1 v! u1 V" o
    case 107:
% b0 R2 F3 s# c6 U; n" {      camera_aux3_on();2 R, g4 p$ N$ J$ B3 t  M( Y
      break;
# C- A3 |+ C. \$ X$ ]4 d6 U9 L3 t9 E' D
      // turn aux 3 relay off
; H- H7 c5 k2 }# f4 [' s    case 108:$ G6 v, s. j: f( k/ g- W" U4 [
      camera_aux3_off();
! x  x% H% e1 d, H2 N  O/ H      break;1 x0 B9 s- s1 l0 Z& K

. Q+ \3 Q! h: L  G9 f" L      // turn aux 4 relay on- _0 D+ H( N. M# c
    case 109:
3 p% ]3 A6 D2 y/ J      camera_aux4_on();
( [. i( S& g  M3 o      break;3 P& z# ^6 S1 Y7 ?4 W
* T4 \4 v2 A% l- E
      // turn aux 4 relay off5 \# g% y( D, k; f7 u  T. t% q
    case 110:
. y) h% u/ R' H: K3 E3 P2 A% f      camera_aux4_off();
7 h% F7 u" P9 V# e* @8 Q      break;4 @5 a  W2 P0 q/ v& K& B
*/
& l* _& n0 o. i+ ^0 Z    default:) k0 p& g2 K" F4 C. L
0 P# g( h& k5 {$ N: C
      SerialUSB.print("Huh? M");! u- D# o( u- O* V7 p
      SerialUSB.println(code);
; U. H# i' X2 H' `) J    }
5 M+ B. `" x$ d$ u  }/ w7 ~# Y6 [) p. w0 {+ R
& T. k, o! ^/ R( j; k* H0 `+ k
  //tell our host we're done.$ }7 l5 h7 Z7 x- u
  SerialUSB.print(byte(78));8 Q4 P7 o) [( B/ ~
6 [( B7 b* G& b+ a, z9 S
}  T# e7 F7 g5 C, K
% v7 r2 O: z- D# \. D0 k
//look for the number that appears after the char key and return it
8 ~, L  H" ]7 D4 e# x  Kdouble search_string(char key, char instruction[], int string_size)3 y5 P" A& O) v$ x4 [9 W0 w; O2 Y0 G
{
; I$ U4 S" @/ }  char temp[10] = "         ";
6 d2 {/ [8 [) x0 \  for (byte i=0; i<string_size; i++)# E+ X: u& K& B+ E' z
  {' E1 t. o  }2 y6 E8 Y5 e+ q) z1 B
    if (instruction[i] == key)0 ]$ h$ N- z+ ~6 e# {
    {
9 @/ g, w+ n" b% [, A" P- d' ~$ v4 g      i++;      
5 u, t) u6 v6 G  I* J0 ~; F      int k = 0;5 _% \( n0 m6 q! t- w
      while (i < string_size && k < 10)
6 Y5 r$ a2 w; E3 C$ X# o& I3 y      {+ W+ K, }: [: ]% _
        if (instruction[i] == 0 || instruction[i] == ' ')
( i2 z  B2 P$ W/ o# Z; M          break;, X+ D0 M0 y; T% n9 D) K6 r  b

* k4 Q+ k; [& m4 J/ Q  M" t        temp[k] = instruction[i];9 }  G& |. f( ]! j
        i++;
9 k; Q) X6 N0 ]" A        k++;6 `. s% C/ d2 r* U8 Q( ~9 V
      }2 i# B, ?" |0 W% \4 Z2 y
      return strtod(temp, NULL);; e6 i! Z3 X2 ^5 n1 C7 u
    }+ M. P) {4 J& t( X! R! ?. Q$ R
  }
: t7 [  H, F3 G4 k( k8 |, @. P' l+ d$ I
  return 0;+ f0 A4 c( ?+ o3 j' K* V( e4 k8 l
}
  k' K6 Y8 Q) |0 j7 i6 F
% S; M5 @+ _8 u$ D. m/ k, V) X( _//look for the command if it exists.0 \8 ]5 t/ b2 G) O
bool has_command(char key, char instruction[], int string_size)
# A$ `# |! S( V- S3 G% K{/ H; M- ~$ a( F; S) L7 _6 y
  for (byte i=0; i<string_size; i++)1 q4 L) u* F9 A
  {4 v" k! l8 o4 F, v; R0 D0 m$ l
    if (instruction[i] == key){
' U! d1 S; n  G9 x
- ?! {+ E- V* t2 d2 Y- Y' ~" ^2 t9 v      return true;
- m4 l1 G+ f% _8 U) B- q6 C    }: m( a# i1 P! w) K/ i
  }* c# c% c% L/ {; b5 z- _

" ?: n; n9 x) o' t: x8 X  return false;
9 j8 a* g* f! `0 W+ K1 P% w4 I}; d! s/ w# P! K. ~( G# J4 |

$ q7 Z8 {# r0 S
( e& s5 s$ ^" v% n, N: i9 }5 Y/ v, x2 P3 |  c
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层

' A' C6 v7 V, y- c( [' }# v//init our variables
$ z8 W4 C9 M1 k9 |3 R" ~) M! x5 ^0 g+ H& Hlong max_delta;3 ]: E7 Y" ~: Y( }, ~* w" q* ~$ I  O
long x_counter;/ ^* ~) e% ^- W& ~
long y_counter;
3 S% m) W0 C! G: h  L: dlong z_counter;
; }: F" h5 ~* {3 P0 ?long u_counter;
3 d1 P$ C$ U' v6 clong x_pos = 0; // x position in terms of absoloute motor stepps
- g- d) J. |) i) r1 [( w5 glong y_pos = 0; // y position in terms of absoloute motor stepps& W4 \$ T) n. A5 D
long z_pos = 0; // z position in terms of absoloute motor stepps5 n5 ?+ _& m. t5 W
long u_pos = 0; // U position in terms of absoloute motor stepps
7 V# n/ h" S  i+ s
, n* ^- U! ?/ c' p9 zbool x_can_step;( S/ ]: m' Y' p' z* W
bool y_can_step;
2 n$ p. |1 b4 P$ f# d) L. D: v/ |, B+ gbool z_can_step;5 V1 t/ Z9 `$ v* ]
bool u_can_step;
/ j3 J( U7 _! G) I9 wint milli_delay;
" y! f9 r7 e9 p& i: w0 D/ ^5 k% K% h* `: T% u7 ~! c+ q
void init_steppers()
' t( `/ D6 }3 g9 |7 T( Y4 C1 B{2 F) A, W  _, e5 r
  //turn them off to start.
, W) _- O) k7 l: G  |  e: Q1 e  disable_steppers();
/ H8 }5 V0 H; D0 U* n- U6 p' ?: g$ j7 z: L8 f' v/ T
  //init our points.
' C4 M; ]5 [9 J, H- x" h  current_units.x = 0.0;
! a2 j, s8 ]$ r6 I  current_units.y = 0.0;
! _$ W5 V4 x" @4 E5 s  current_units.z = 0.0;+ C2 E- |( }$ H
  current_units.u = 0.0;
$ ?* f1 S% X( ]; K  target_units.x = 0.0;! Y6 g% n$ X* ^& p
  target_units.y = 0.0;9 t9 Z9 z# \- i
  target_units.z = 0.0;
+ L, z: @3 `1 z) b1 {5 j2 S  target_units.u = 0.0;
; f+ C! p8 N1 w( O' B6 s; k' m  
7 m6 Q) r4 `! S5 B$ a) W3 J
. D+ o) b" q% S, r2 x2 M  pinMode(X_STEP_PIN, OUTPUT);2 s. c0 K* L/ b5 h' i7 P
  pinMode(X_DIR_PIN, OUTPUT);) K& k4 U5 W7 C# m" I
  pinMode(X_ENABLE_PIN, OUTPUT);* z3 e. X, I* F. `3 e
  pinMode(X_MIN_PIN, INPUT);+ C5 S' z5 \% z$ e( c: |
  pinMode(X_MAX_PIN, INPUT);3 O/ W( M6 F3 q8 x) m* H% t
0 I$ S1 h( y6 O6 `% L& [
  pinMode(Y_STEP_PIN, OUTPUT);
  h. {8 o7 z+ Q# j+ m  pinMode(Y_DIR_PIN, OUTPUT);8 @% o7 ^/ J% \  u! O
  pinMode(Y_ENABLE_PIN, OUTPUT);4 F; t4 P# t  T' W; Z/ \
  pinMode(Y_MIN_PIN, INPUT);
0 b+ B1 Q; u' m+ ]* O  pinMode(Y_MAX_PIN, INPUT);
# o5 N2 B0 @/ j3 Q) o2 {
: F# W& c' ^2 c' }' d$ Y) F  pinMode(Z_STEP_PIN, OUTPUT);2 M# \+ l" ?% C5 S+ V; z1 T
  pinMode(Z_DIR_PIN, OUTPUT);$ e+ Y- n4 w/ }0 v3 w
  pinMode(Z_ENABLE_PIN, OUTPUT);
0 u. E- M+ o: C/ P4 Q6 v/ m  pinMode(Z_MIN_PIN, INPUT);
! `* Q. q: M: L4 T  pinMode(Z_MAX_PIN, INPUT);
; l: w" h& h1 c# _" V9 [
, h3 k% m/ }" x' p, \  pinMode(U_STEP_PIN, OUTPUT);
- ~1 f" d% ]$ A+ N( r: D  pinMode(U_DIR_PIN, OUTPUT);9 i- U- r2 M: P! Q; ~4 F9 k2 H9 C% \
  pinMode(U_ENABLE_PIN, OUTPUT);7 e; m& u+ s! E# A* _8 r3 E
  pinMode(U_MIN_PIN, INPUT);
( N0 g' Y! q- J1 \# W, a7 S  pinMode(U_MAX_PIN, INPUT);& m% a+ p9 h* R4 n( ?. v
  //figure our stuff.
7 `1 W" }; n* j  p: P  calculate_deltas();6 `$ k, I* m6 A) y% k$ i% n
}& e( s) j; Y' K+ d  m

3 ]0 J4 D! M- Q  ?5 P6 b' Fvoid dda_move(long micro_delay)
/ L. V  u9 L6 x8 c/ |{) V$ J4 w% |8 q" F' f" Q
  //enable our steppers
( R( u0 I" v) l7 m. k  digitalWrite(X_ENABLE_PIN, HIGH);% u' W3 T" w# U" a4 J
  digitalWrite(Y_ENABLE_PIN, HIGH);
  }4 a) p- `' Y& l7 S) ^7 h+ T  digitalWrite(Z_ENABLE_PIN, HIGH);/ P) s' H% G6 g; _# U
  digitalWrite(U_ENABLE_PIN, HIGH);
8 g' C6 m' N1 D+ T# K% V0 X  //figure out our deltas3 k2 t; ]; k" a+ N
  max_delta = max(delta_steps.x, delta_steps.y);
7 O# V# Y" Z+ ~$ E/ t- R& K2 I  max_delta = max(delta_steps.z, max_delta);$ V: ?& G; k; n, g
  max_delta = max(delta_steps.u, max_delta);
: K: M+ S* n2 Y' [: E' L+ X  //init stuff.
3 O2 W0 d+ {/ m$ T1 x/ P" u5 l  long x_counter = -max_delta/2;
& ^& s- W' `  I  L, x; g1 g3 R) f  long y_counter = -max_delta/2;
, N3 Y$ c8 y; l  long z_counter = -max_delta/2;8 n' ^, Z3 R) o$ a
  long u_counter = -max_delta/2;
0 C8 _0 O/ a, }) N
- E/ R8 h& `; f1 J, d' ~  //our step flags8 P7 _" v$ e! v$ C: ]4 i! F5 ?
  bool x_can_step = 0;. ~8 N- d  V# x: a- U7 M& |
  bool y_can_step = 0;
/ C1 n5 \  H' f: I) g  bool z_can_step = 0;) z; O1 \% t4 {
  bool u_can_step = 0;- k. {5 [4 |" B& X7 w. S

' Q$ k$ H! `  Z5 b! k7 Y  if (micro_delay >= 16383)) X3 z7 o/ c4 K! N5 {1 B, v
    milli_delay = micro_delay / 1000;
1 t# \# K7 f. C) Q6 u3 I* l" D  else
! g- ^/ Y5 P" [* u1 [' Y5 ?* m    milli_delay = 0;
" ~% P4 A+ o2 _: A
7 ^& ?8 ?4 Z) M
9 K7 K3 `( v: w. I4 z7 m! ]7 P6 Y# a  //do our DDA line!% n' @& r% W6 F) R8 A3 `  G# }4 D

, C. j/ F& u; T9 p; v- O! r4 P  do
3 D* r( v) e+ L  {7 X5 V% i/ a' [3 m2 g' E" y
    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;9 t1 U- P# a& b3 n* M; b
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);1 ^  q% P$ Y/ i. |2 b% ]- t
    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);+ Z: b( U! g8 `* w! c1 Y0 P
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
% d. V" v  ^9 i( m' x0 H3 C    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);3 r( G4 X0 c0 _8 N! h
    if (x_can_step)) X$ `! r5 d) |. c6 f  |
    {
5 ~- N6 f! P/ z, g      x_counter += delta_steps.x;$ X, S. w; D' R& q9 H
) ?! E3 |) L) ~5 x
      if (x_counter > 0)1 i& E6 O5 y. O9 w* h) _) A9 Z& B
      {
, v2 @# F! ]) U) Q2 m( U        do_step(X_STEP_PIN);
1 E! y8 q/ S- Y4 m+ z        x_counter -= max_delta;
+ G6 Y  Q& L- N        if (x_direction)4 H: \/ @; X( \' g/ O
         { current_steps.x++; x_pos++; }- h- R( Q8 X% c; S+ H
          " Y5 ?* m. k$ ]! p( P- K3 d# ?7 F5 E- r
        else
/ o. o7 }1 y) e- R" ^) V  K# ?          { current_steps.x--; x_pos--; }
9 m; f4 _/ x2 _& w( n7 h; M6 I         7 q% Z  I8 ^& n4 S1 y. C: ^1 \
      }2 ^, }6 a" N' W$ b4 [$ l- D
    }
3 W) V# g$ s8 \    if (y_can_step)
; D& J& W8 b; X3 L/ t" g# [    {
1 H) S: C* v% X2 Z1 r$ o      y_counter += delta_steps.y;
8 Y0 a1 r$ [5 v' o. z. U0 w# D3 ~5 @1 E, g. f! s; m% R  j0 i
      if (y_counter > 0)2 Q4 c/ k; `1 Z* Y/ k" @
      {  G, `. g& d. c1 o; O. z. j
        do_step(Y_STEP_PIN);- A; V$ t3 C. ], u( u1 w4 Q0 A
        y_counter -= max_delta;9 j5 ^, m; I* U2 _6 l& m* I
$ s- f# f2 ?/ ?9 m* D6 u
        if (y_direction)" }. u( y4 ~# K" ]7 q9 P: z
        { current_steps.y++; y_pos++; }' l" T# ~$ I, u* s! d8 G
       . I2 v2 m# b) E+ z, o
        else
7 S0 v  l/ w$ U- u        { current_steps.y--; y_pos--; }% l! k: T1 s4 `% ?1 l+ V
          F! p: V+ \; j4 Z" D9 D
      }! n# c! J5 C- z1 L' r# x0 }4 G2 V
    }! ~$ K3 o$ y' S1 i; ~' Y
# V9 s, N, v: Q" f
    if (z_can_step)
9 |! H3 X' J9 r+ n0 ~# z1 y: j9 _    {
& P& P  r3 B7 p& e  v& k+ W# X      z_counter += delta_steps.z;. Y2 C9 N! Q' {1 t8 o- c* x7 }& m! k

1 m) |% t! x! V8 Z1 ^6 G      if (z_counter > 0)
( t4 \+ J: E+ n& G      {3 x/ q# x7 z7 R4 h$ H7 g$ {- ]7 x
        do_step(Z_STEP_PIN);
; r2 a! y' @# L4 e$ Z        z_counter -= max_delta;" r$ O2 q" z3 F- M6 ~

7 S. _& w9 `$ @# h! x* @4 i        if (z_direction)
1 e: b  b; A) l4 Y+ r2 S  Y7 M        { current_steps.z++; z_pos++; }1 P. d2 H/ A1 V
        
( L  g9 w" U0 u# Z1 g        else
: U! X" G  I' E" I$ u        { current_steps.z--; z_pos--; }- A4 j# r' o- }
        
2 `& V4 `' q+ g8 o( y/ i      }  Z5 M' W6 C+ ^. s4 ~) K$ q
    }
9 d) ~" Z. s7 f, N! o   
2 `( x+ X# M5 Z. y6 k' e    if (u_can_step)' p! ?. z& U4 r+ M0 J9 v
    {
  y! u) @: P( s% F. e      u_counter += delta_steps.u;
  x$ ^+ `( u6 U
- I/ m) C, z! l7 f! a  `8 S      if (u_counter > 0)/ ]9 A* b% f& Z
      {* J8 a  W* h2 y5 K
        do_step(U_STEP_PIN);
& k: {7 ^$ t5 C  h, d+ a        u_counter -= max_delta;
! a' t( Q0 x4 G3 u9 {% a. H
8 m- V& N' N) K3 K- ~- [        if (u_direction)
8 p. l: m1 E. a        { current_steps.u++; u_pos++; }! X5 a3 u: J8 X9 E7 e
          5 Q% R4 v7 u# V6 P4 [3 y# o
        else5 \" v& \+ i0 f! `  b$ y
          { current_steps.u--; u_pos--; }
$ o7 ?! R1 B5 ]6 T% a6 m* ?         ! i3 G7 f3 M& l7 _. Y5 [* R/ c
      }* d5 j  G) S) i: U7 i% c
    }. |/ W9 C9 `7 L
    //wait for next step.. E- f6 L& Z0 b! l( K
    if (milli_delay > 0){
* B" s7 C, i! g- U% K& R) e      //if (digitalRead(BUTTON_SP_EN)) SPEEN();% B" D% p, s8 }
      delay(milli_delay);) u, W1 A) w7 C1 g. g
    }                $ J6 S' n0 g% n3 v& D
    else{8 i: T2 s) |# q8 s
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
0 Z! ]$ _( h! x2 [- T9 t/ ~0 z      if(micro_delay>0)delayMicroseconds(micro_delay);
+ h2 [, S. y, M    }
# _9 C7 M+ Y4 t% ~& [    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);, A9 |$ w! P' O' C5 B5 j1 ~: S" X
  }% E/ s; W" t3 K9 B, [3 a
  while (x_can_step || y_can_step || z_can_step || u_can_step);! e+ d) N& ?0 o
' q( A, g. w  L8 p/ X, I6 r

, R7 l- h& }+ [4 T+ Z  {& a  //set our points to be the same3 s/ H. a1 Z# ~5 F# Z6 v
  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
$ _# ~* M# T! m6 r  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;; z; W' e) V4 R7 K7 |  V4 k
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;2 d. b( r6 C- Q) J* b  Z
  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
  E- {9 X; k# r! b+ {% O  ; U1 L% B( U( C4 j# V: f. x0 W5 ]$ G
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );  S/ [" u# X; J. t* t. A
- ^8 [9 b* q/ }) ~
  long x_pos = 0; // x position in terms of absoloute motor stepps" {6 ^, S  H, M" H! n, h6 ?- i9 R
  long y_pos = 0; // y position in terms of absoloute motor stepps' Z6 p: c3 E& n8 h! K. l5 n
  long z_pos = 0; // z position in terms of absoloute motor stepps
/ ~* V# _1 f, B; M" ?, N  long u_pos = 0; // u position in terms of absoloute motor stepps
9 t- O7 i& R' v* ]/ h0 C  calculate_deltas();% {/ x! ^: j0 I9 T
  9 V1 S/ S) Z( [1 r/ G: v4 @
}) f2 \  _3 E  L
# r" z* Y! T/ i% q$ [
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction). i4 I! Q+ e" N6 a# t) I
{
$ c' |9 E7 F# y' |8 p; @4 s  //stop us if we're on target
! s2 ?+ x- Y: O. k# j  if (target == current)
. I! M' i# p- E4 L3 \$ F- S$ z    return false;9 p, e( G3 `, U- p/ f+ t1 W) T
  //stop us if we're at home and still going 6 u8 u8 L. w, ?! e1 T* j
  else if (read_switch(min_pin) && !direction)0 P- E' Z; J: h* Z+ }& S6 ]5 Z
    return false;
/ ~( A  ]6 H* _, Z! @* I9 R3 x  //stop us if we're at max and still going
8 Z# b# B' A6 O, b7 A  m! o  else if (read_switch(max_pin) && direction)
! Y. s) M. m+ n' O* B    return false;
" [5 C+ q/ |' q# Z; |8 D% H  c  B# g+ l% c9 T& g
  //default to being able to step
" ?! h  F( Z- `( }( d  return true;9 m8 J4 o5 t2 B, w. K
}+ ]: h9 r) U$ I2 n

, `, O" m6 w  d) Qvoid do_step(byte step_pin)
/ d# \; Q: K8 |" l# l% D7 ~{3 ^# H4 ^2 U6 I, k8 C, v0 h# k2 m
  digitalWrite(step_pin, HIGH);$ L: N; k! R* X  T0 F8 U
  //delayMicroseconds(1);" L5 v) G; t7 K5 Z* \/ \. t
  digitalWrite(step_pin, LOW);
, u8 h  X$ b- Y7 @& Q}
# f8 I2 O& p1 ?3 a5 ~; o4 w( j
bool read_switch(byte pin)
( r) d# U/ l6 A3 K0 v- b' d{
" |" y) \( L& ^0 k0 Q) {6 p7 t% g/ }  //dual read as crude debounce
: }$ F- j* {- X1 C1 Q) K# B8 F4 {, F
  if ( SENSORS_INVERTING )  B5 h0 i3 `' C  _
    return !digitalRead(pin) && !digitalRead(pin);
- {" v% @& T1 v0 o: G1 j' r  else! L; w* j$ d5 j& A9 F+ m5 l9 b
    return digitalRead(pin) && digitalRead(pin);) G- @% t' Y: w( W, t0 \1 M6 Q
}
0 g' M8 ]2 J6 Y/ ?/ Z' l1 D6 Y$ [
5 c$ u% {2 q1 f$ o: \  t( Zlong to_steps(float steps_per_unit, float units)
; p, I' M6 z9 t# ~{
0 |- ?8 _& p3 a  return steps_per_unit * units;
- i9 Y# k0 `# E! f: [}
" Z+ ~6 z* ]% l* n, @4 N2 I: c' E6 n8 B5 V
void set_target(float x, float y, float z, float u)
0 K% n' j$ f# b% i{
% l; ]# b8 v% g& N5 ?  target_units.x = x;
& {0 E. c! i0 ]$ L  x5 a  target_units.y = y;
5 ]0 ~. L) a. ]  target_units.z = z;' j, F% U; ~5 k% L" a
  target_units.u = u;, C' E3 l8 g& _0 }0 s* \
  calculate_deltas();. s; y- b! w2 O: }3 N# s
}1 S3 M0 H& W! G6 U! i+ t1 P
+ i& @3 l/ C- l
void set_position(float x, float y, float z, float u)7 c+ H& t  Z/ j7 a
{
/ R: o9 e% }& g# m2 y: g. O  current_units.x = x;
1 e" L0 {- Z# y  current_units.y = y;
7 `. V9 Z7 R9 J% e  current_units.z = z;
  Y* S5 s. U  y& F4 `  current_units.u = u;% v! S/ H! f9 y- w5 C, i! a
  calculate_deltas();
# g4 a, f& Y+ F& `: o}, R" z1 @- x; Q

* }: J- B6 K5 K$ `# I1 avoid calculate_deltas()) }4 r5 l/ }% }4 [
{
: w: U1 [: ~5 d: [; |6 d  //figure our deltas.
0 D. t, Y& U% l, u: p' {4 ?  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
1 g2 t/ |/ d4 X( o" x2 `& Y' X3 A  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);
0 ?* C# a4 i: T  D4 P: I  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);' `2 T7 Q( y1 ^0 }6 n+ |/ Q0 o0 o
  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);
8 j: [) Z& \! q; X# `/ p
- W" b% [* T- m* ^* s0 V( d  //set our steps current, target, and delta
6 E: r( b- w  C: D- B( {7 _, x1 w  current_steps.x = to_steps(x_units, current_units.x);! m$ v6 ~' [( a: c* c
  current_steps.y = to_steps(y_units, current_units.y);+ c0 B4 n0 |: o6 G
  current_steps.z = to_steps(z_units, current_units.z);' K; Q& D1 R# o: a# y2 N" `! m
  current_steps.u = to_steps(u_units, current_units.u);: Q/ R# Q  X* }- o

" F  J. @- _8 r7 @5 |/ ^  target_steps.x = to_steps(x_units, target_units.x);6 S0 K: q( K) s# R# m' U- l7 v
  target_steps.y = to_steps(y_units, target_units.y);$ V) _6 L- W! t& i
  target_steps.z = to_steps(z_units, target_units.z);3 c# ?2 h: B0 i  _7 x" J- E
  target_steps.u = to_steps(u_units, target_units.u);5 K7 ?4 {. p0 |6 i+ @0 N

; q& ]( x& M4 ?2 Y  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);$ ]! W( {! R8 ?% R# @, }
  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);2 }7 p, i. P2 g
  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);& H( b4 ~1 \6 K3 v
  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
9 Y9 e7 |( a8 d/ E8 }( }1 z' e, J- i: M& @8 t7 Y

- Y6 t) S8 F! N2 E6 w% A! f( U4 g/ Y, d" E, S8 I$ g# G' N' a' |5 p
  //what is our direction
$ V( g0 i& z/ b! }8 Q  x_direction = (target_units.x >= current_units.x);, f: L2 `2 t% n- F; Z1 S4 j
  y_direction = (target_units.y >= current_units.y);: _9 a6 k+ A; i- _) ]5 t" i% g
  z_direction = (target_units.z >= current_units.z);4 b# M+ b( [, _
  u_direction = (target_units.u >= current_units.u);
* F9 ^! X- S8 k* H9 D8 M: J' C) Y$ K- F7 R$ Y& D2 D2 T! d. N
  //set our direction pins as well
* y: `9 @2 m% V" ^  digitalWrite(X_DIR_PIN,x_direction);1 K* Y; A3 {0 R. ]
  digitalWrite(Y_DIR_PIN,y_direction);6 ?# V9 H; Z8 p3 V: u; ~2 ?
  digitalWrite(Z_DIR_PIN,z_direction);
# g4 [6 ?# V/ s1 A  digitalWrite(U_DIR_PIN,u_direction); + l0 |. a3 n, L1 [% c4 c
& ~3 ~# q0 F! U' F0 e3 H) [/ i
  //绘制LCD2 y5 _" B0 a! ^5 [0 M! f
  LCD_DRAW();
, C, r& k+ U. ^3 E
, s7 d" m8 C$ n}! r4 ^: L+ o# R$ H/ C& _7 {: j
( q8 V* j2 _# c/ y* R  ^+ s
. k: k) g3 e+ W, x
long calculate_feedrate_delay(float feedrate)
) }) f/ B3 B* O! r7 C{
4 y+ A4 w7 O% n  //how long is our line length?* P# y/ o# B, G9 p) c! M& ^
  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);
) _" d7 `; w' n7 p7 z0 r  long master_steps = 0;
6 I, R6 u# j( F* e- a) w3 P+ I% J/ {& R
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
  w% o" |9 b  \3 m( w% N. w5 qmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;9 C9 i# r7 c# ?$ a! {% K' U3 f  D
master_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
% ~8 j) s, a- z* V: C% }, P$ x, m5 y) z5 c$ \3 t4 S* @

9 [! j+ [& m/ q" n& d3 a
1 V2 j6 _4 a0 Z7 Y) h7 ~
/ u& J% V; P8 z2 ~$ C, H7 t  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad., W9 I) p1 l6 k/ Q7 c# m, Z: W/ p
  //the formula has been condensed to save space.  here it is in english:, c; W4 A  A! L
  // distance / feedrate * 60000000.0 = move duration in microseconds
6 D( b, c# o+ Q5 |% _  // move duration / master_steps = time between steps for master axis.
" F1 J. w! I' o. t$ @, y5 ?( z return ((distance * 6000000.0) / feedrate) / master_steps;
  r+ \' b. C3 W; u1 ^$ `- Z" Y: _  t  f3 ]( E! ^
}
8 ~; `. }) F1 f9 i* @. T* u3 @- d* G  L& N# v: N
long getMaxSpeed()
' B3 G8 a+ N' O, x5 m5 U. {{. ^* W7 Y  `- U, f8 ~- v
if (delta_steps.z > 0 || delta_steps.u > 0 )/ q" r& Q  b: g! c8 }0 s# x
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);8 z, n. l0 n; q! T* `3 O7 u
  else
7 d# i3 _6 A* K/ t! w; j8 ?; ~8 v8 z) `    return calculate_feedrate_delay(FAST_XY_FEEDRATE);0 p+ }3 m8 \) l& `7 C" g
}9 J# o' r. ]* ^5 L; U
) B9 d% k* E! x9 j) O4 _
void disable_steppers()& `3 J; j/ W6 x/ [8 q
{
: p# U% ~3 c% i+ v" y. U  //enable our steppers: @0 Q. y; P$ e' [, }" I
  digitalWrite(X_ENABLE_PIN, LOW);
1 B: S* J  x7 D: _  digitalWrite(Y_ENABLE_PIN, LOW);+ ~. R# }- \0 p+ f
  digitalWrite(Z_ENABLE_PIN, LOW);2 h# K0 M  S% x- X
  digitalWrite(U_ENABLE_PIN, LOW);( ^8 r  |: E% B+ s/ p" a. o# O/ R9 G
}- Q# }$ |) E( K7 q: [

( M$ u' m* p; a, X0 W9 O! z8 y% w$ i9 K) l- J
//绘制LCD; G# U! S9 d9 ^. n
//unsigned int DRAWCount=0; " Z  Q4 K1 s! ~, m2 f+ ]
void LCD_DRAW(), h- }+ u+ G( e* S' U7 I
{/ ~7 U  f5 F. Y+ i
    lcd.clear();
- ~, |) u) _7 S% n& `- J$ j    lcd.setCursor(0, 0);
. q" v4 a0 E. `, }3 `    lcd.print("    X=");
$ u' D2 c, m! N0 x    lcd.print(current_units.x);% P* z. u7 ]6 y: q! D
    lcd.setCursor(0, 1);
; W3 R' d* g- B) W. \8 I    lcd.print("    Y=");
. I0 y! s) f& O    lcd.print(current_units.y);
; }0 E) s5 ^3 v) I3 ]    lcd.setCursor(0, 2);$ N8 q: r5 _9 h  I: N5 k2 T
    lcd.print("Z=");
& }& W4 H2 t6 }  w5 V, v/ H4 G    lcd.print(current_units.z);7 H% a4 Y& S' D/ `" c0 m
    lcd.setCursor(0, 3);8 U2 z& P  _% e' z: {8 [
    lcd.print("U=");) k5 R  q0 E2 q* ^$ e- L
    lcd.print(current_units.u);
; x- v" {0 n5 F7 x+ @8 ~8 z  }9 h4 ?, w( L3 v' g$ ^7 @  E
// else if (DRAWCount>=30)DRAWCount=0;
; S( t6 l$ t3 Q1 w/ n5 Q1 X8 B//  else DRAWCount++;7 i+ B) u1 P$ F( W* O
void SPEEN()8 _  B$ r2 j# |9 p
{
" x1 A) o- V* y. Z0 g   delayMicroseconds(analogRead(SPEEN_CTL)+1);# N1 R! d3 l* `8 a* U
}, Z' e$ J' F0 t, s) F
$ p) n& _  u! @3 o
' k# o  j' U% u! q  c# f
//delayMicroseconds(analogRead(SPEEN_CTL)+1);7 [2 ^  V0 d/ i5 C; l# u
//if (digitalRead(BUTTON_SP_EN)) SPEEN();' @  x: a$ l, E: `8 x; o

" e7 \1 j& z# H/ G2 b6 q5 z/ `3 [& w" f% [$ Y! [
回复 支持 反对

使用道具 举报

 楼主| 发表于 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-7 23:13 , Processed in 0.068036 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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