机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 22800|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑
+ Q# I7 z) y* e& ^. ]
4 \  K0 \# H1 T$ j* ?首先声明
) C/ n/ d- j+ k- [  B我不是专家,业余捣鼓* c  W- g* \% a5 }$ A0 c6 I. U2 w
源代码首先来自网络4 Y. o' J$ n" Y
开发平台Arduino及Maple' _4 n8 B+ x* ~0 `
http://www.arduino.cc/+ T! @/ k# A8 @
http://leaflabs.com/devices/$ G8 f9 O; q9 Q. J
国内活跃社区
' ]; \/ ~6 A" Uhttp://www.geek-workshop.com/forum.php
- a$ l" B8 k+ k& p. r/ P1 g9 }$ M& Z2 Y$ z6 \
竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验$ _5 L; x6 v' b( `& P: R7 }
来入门了解熟悉思路架构等
+ Z2 z6 s% k8 b. F) s8 t* C" e& u/ N0 t/ F) }
我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,5 C4 p# b$ |4 \2 y
许多功能还木完成,不过作为低档次得应用可能还有可能5 u$ s4 Z' K  `0 b* e
) C6 q% N) }) k' v9 s- y
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧& }# A1 i* J0 L4 A
; ~: _! O+ _' }* t7 T4 t8 ]
拒绝 所有的求
* o: b4 X+ G# U求人不如求自己 不然木玩
+ X6 Y. Y) f, Y& D4 C7 M! X, E, I5 e
( @4 M  [. s0 B. @6 w! O高手绕道 谢谢!4 q; A" `1 g7 a

! _% T) W& z5 a6 ?* z
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 4 t3 D4 J& P- s' x, N- `

; v- ], ?: r! [) \  K  ?GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东
; O/ T% d# X  @$ ^3 ?贴代码先* E" N" C+ J( ?% i4 h5 ]3 s
直接Maple的,某宝许多超便宜哈) a" P+ l8 ]6 h6 R% F- ^: J
晕,我怎么上不了RAR?
/ J( o. U% i, x. s想玩的留下 e妹吧
) c& d& w3 [4 G" Z第一个妹麻烦传第二个妹哈$ \/ g/ \& f3 v' k
我平常杂事多6 ]; y+ O  f" w( k! p5 E- @7 s9 y% S
谁放网盘上再,麻烦开放下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap% A% I( C7 T$ g6 `) F. Y
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)- [. i/ X, ~7 D
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)2 S, H: e: P/ C( o, n( e$ R' }
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)) ^) D/ Q7 T: H' L6 g, M
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
1 L: l1 {; f9 z2 r
: ^6 E0 J8 u) U) h: j) e- l// Arduino G-code Interpreter for Macro / Micro photography
% A5 D6 U3 F3 K/ s2 z0 u" N// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
) @) N3 j% A/ f- S//modified by YaoHan China 2010.2.15: \# J) x/ L+ [$ I% B, w" \: @
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
: d  T5 I; M+ _#include <stdlib.h>
4 e& `5 i) ~0 O/ V# O#include <LiquidCrystal.h>: S' R! W. o) M6 D5 l' F  O
& M; s: a% D7 Y, D, K
//启动(高电平有效)/暂停(低电平有效)  默认高电平
# S# w1 b$ W! t& ^- c/ T#define BUTTON_CTL 35
9 ^5 W8 d# c) c) A8 d  k
8 P4 m  u, {9 H5 M! k//点动模式键(高电平有效)  默认低电平
7 i# z( Q  C2 |5 L# Y, a% `7 C#define BUTTON_SS 36) C: o# o6 w( I4 L* X
3 R9 c; S/ e: n# n; ]: I
//点动键! L$ o# B/ b0 a, ]7 a
#define BUTTON_MAN 15* p8 c" g  H, d/ K$ R$ Q7 l$ N
+ U; g+ F- K( d+ l. k
//电位器速度控制, I! A# l. q& G, N  \. Q- D9 n% L
#define SPEEN_CTL 16
, N6 x5 z8 K4 C/ @! c: {
3 n' `: B, p: s3 @/ G5 z//手动调速使能
/ N8 U) h3 E0 @3 F8 g#define BUTTON_SP_EN 17
( f( n1 Y* w9 c* r- N0 p* F
3 @( ~( Y0 q7 M) c& ^, ]& _//LCD 类型 1604或1602
/ G1 f: p. R; E/ R#define LCD_TYPE 1604
" i) Z6 j( `( x8 o  M. u//LCD 引脚定义
2 g4 j, D* `0 U* X/ n( \8 c7 V#define LCD_RS 23
4 }. g) s6 @: X. N! `. i2 R#define LCD_EN 24
2 O* u6 t* C4 U( n) S& h: Q) ^#define LCD_D4 25
% n; u; _$ E" ^7 B" Q$ }- _$ f#define LCD_D5 26
) z! ~# O/ u8 U#define LCD_D6 27
; Y. v# G. V' m$ w# C" H8 b#define LCD_D7 28
2 [0 i& m! n, n$ M0 A1 C$ o" l( {  o* n
//命令字符串
4 t# I% R# i9 Z$ g* |% ?  R#define COMMAND_SIZE 128
1 W$ }  K1 e6 uchar word_old[COMMAND_SIZE];
# a, v8 F5 O/ E3 f, n; w( Sbyte serial_count=0;
! V3 q; v* ^* x. r# K: R3 R  wint no_data = 0;; h& [9 f+ J! ^+ i6 u) D9 u# i
//LCD 引脚链接 配置
" u" H* Q) u, D1 F8 G  d0 B2 OLiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);  P. w1 {& W* M: d7 A
//停止控制,高电平有效
* n- A  h) P/ g) a% d( o$ n" y#define BUTTON_STOP 30
& q% t; B! ], C& Fint stop_flag=0;
' A: D$ J- F7 ?. O- v
: W1 y  r( T0 H0 H, C, I, @' _. p//暂停; m0 ?$ S: G+ E* U$ N
void pause(){$ c) A( `: q: f8 U+ p
  while(!digitalRead(BUTTON_CTL));
8 n) A5 u) j% W3 @}
, T+ o. L- p, c8 H- B0 f; A# y5 O3 s9 G5 c
void stopper(){3 I# M: X4 g2 m  U% u
  delayMicroseconds(10);) y# V/ J( |5 J
  stop_flag = digitalRead(BUTTON_STOP);
5 z) a$ Q8 Z% Z2 ~8 [1 H) V}# a% ]/ e: u/ o8 ]
' |" g* U) j5 c& s& g  l
void setup()
. s5 G( z- G# J3 T{# f, a3 U, _# j, `% \& z# h' K6 k
  //show start0 V1 l5 t" _! t6 x9 K
  SerialUSB.println("start");
' s+ r9 h3 W6 q+ ~+ D; q& |  `; ^2 }
  //启动lcd
! F. B. A2 B' p" r    lcd.begin(16,4);: T" M1 o: V" ^2 C. Y
    lcd.setCursor(0, 0);
  ?+ W  x" p, R' p# k    lcd.print("hello, world!");0 j+ H; k' w! n) U3 N0 C

. {+ K- u3 B  U8 U- S; R. S  //初始化控制引脚及引脚功能
3 s1 _' a) {; @7 D% b8 v/ o5 J  pinMode(BUTTON_CTL,INPUT_PULLUP);
5 ~, M; e1 h' S  pinMode(BUTTON_SS,INPUT_PULLDOWN);: R& G$ d- v8 Y, _) p4 F
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
8 V* S+ e. Q5 |$ P8 r/ z- e  pinMode(BUTTON_MAN,INPUT_ANALOG);
5 E; _$ B1 o/ d0 r9 ^3 a
3 V- g5 C: R) J. p" B5 \  //控制引脚的中断
0 t# j4 }5 Y. C0 l: n/ c7 J3 Z4 x  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断& I2 H2 M, K* u; [% \
  attachInterrupt(BUTTON_STOP,stopper,CHANGE);4 E- h; m; b" |# E3 R
  \; N& ?- h  ]. a6 Y9 D
  //other init
. w7 h! v1 V9 O  Y) V$ R  init_process_string();; z% f* c* j6 {, W% C0 u; j. Y) R4 T
  init_steppers();/ _. F; P9 ^" G1 h, p8 g& x4 h
  init_camera();
; w4 W$ e/ L  z; i& J* C, C! o, A
9 O9 |6 R. x& u}
% x0 i2 a9 a$ }, }' O0 `( m6 J8 M
; W# I( d5 F, L: xvoid loop()
: P& a, W8 L% f2 v/ a{  k* a2 c/ x; T4 I: C9 M
  char c;% s# W( K& ~2 [6 E- r3 G% g8 b

2 f* g/ x1 y, j: g$ U& _- p; {  //读取输入的字符1 ^" }% X. G8 H3 N2 M( `9 h. a5 S
  if ((SerialUSB.available() > 0) && (!stop_flag))4 W4 P6 z- T% g" ?6 `6 ^
  {0 Q9 }* ?, e: {3 X, m4 W. G; q2 T
    c = SerialUSB.read();
8 h* V! A$ X; v3 K2 k9 ^    no_data = 0;
2 P- Z; `" \5 m3 j( Y! C( \5 ]2 s$ X; o' P* p0 I  v
    //换行符代表一个命令的结束# I( O7 }/ P& k  E: G: U5 R' ?
    if (c != '\n')
' q5 I+ M1 B0 j/ w: |% P    {9 t6 U  i% F; C, j* [+ x6 l
      word_old[serial_count] = c;0 B/ Y. Q* l+ q# u0 n# `2 q* e
      serial_count++;% C9 v' v# q- D' w4 s, B# l
# h  M% F! m5 b6 g8 x
    }
$ F, b# K; L5 B  J  }
+ T8 T# e# e3 D- P, k$ i2 Y0 C- f  //标记没有数据输入
! x, C8 P" x' y2 I6 X1 {  else
3 P$ e# \1 J# s8 p- s7 b  {
" }9 d* K) w3 O4 Y4 }    no_data++;5 a8 g6 A, \7 t& E8 u
    delayMicroseconds(100);
& ^/ p. I& v! T+ i7 v  }
9 t0 ^  U; y( _/ h2 B0 M: @
7 Y4 Z5 w+ d# w3 y  //if theres a pause or we got a real command, do it
, S2 C* J# u  q5 n+ x) h- [  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))
; n, G( W7 q3 |0 H* Q  {
$ W1 ^% ?  o6 Y8 I* |
  v) l2 m, s6 N6 k- L5 E    //处理命令
4 {* P1 M, g: e. Y% ?& `9 G: X% w    process_string(word_old, serial_count);! U, s: H. b& ~4 L, Q# }$ |
# T, n9 E5 o' L
    //清除命令
$ c) m& `% t" N$ r    init_process_string();
: U! C+ V6 A' |4 z  f+ V" s7 Q  }: W4 m$ H. m1 |
8 V+ e5 ~# N5 F
  //如果没有数据关闭电机
) {$ c' j* K4 f3 m# M* ]3 i  if (no_data > 1000)" G! F  Y: Q& y: R- ^
    disable_steppers();+ N! f, i; M) r0 J/ e5 m, e( p
5 n8 k( W3 w/ Z, J( d, @; _' k
  //如果ss键按下进入点动模式3 E% W2 c$ u& N: S7 y9 e
  //if(digitalRead(BUTTON_SS)) ss();! n# ^0 {8 {1 @# C5 `
  run_a();% m6 _; S  m, O- J! v3 o
}
. n* ^+ P7 j. H
+ R8 }! l  E( ~! L8 I  k/ {9 u//点动模式1 O0 R7 Z7 A7 a# O% u2 }  j
void ss(){
; ~$ @  ]& f; q- P; C' C
+ [& r% @7 A2 o; @: F" }  delay(1);
( \% \/ l0 O6 i, W2 j. q6 Z  if(!digitalRead(BUTTON_SS))return;
* K0 I* r; V: d2 C  if(!digitalRead(BUTTON_SS))return;7 q9 F6 O) l; D' H5 Z) l
  //init_process_string();
3 ^% Z- S9 s/ z# n) ~  //init_steppers();
* f0 p% `$ n) y4 f5 L  //init_camera();! ?( L; |+ D: [% V2 p# R

6 h' W2 W, J7 {3 E // SerialUSB.println("Step By Step Mode");
4 ^% p* T$ N! }& A* a* V7 F//#if (LCD_TYPE == 1604 )" z1 q9 D1 F' l1 b! S7 i8 h
  //当LCD为1604时显示要处理的命令
( @% ]( C" {1 W  u   lcd.setCursor(0, 0);
0 u- a/ V! M3 |% J8 R! q% y   lcd.print("Step By Step Mode");
* V9 q8 x9 s" W2 k# _//#endif
3 p0 W. J2 }# {% k7 T6 z3 d  process_string("G1 F5000",8);. P& M# g4 T) c- B6 x7 a2 p: E4 j
  process_string("G91",8);
" u8 t( T* ]# F, S3 M2 C; m) H  //init_process_string();
* D* P. ^8 X2 I4 ?$ C- g% |. P' z
# r( I/ h6 u# q' o& h' I2 Y) A  while(digitalRead(BUTTON_SS)){
, V' J( f6 G" O    int i=0;0 n3 R/ T6 Z0 ~& r% T% v" v
    i=analogRead(BUTTON_MAN)>>9;
& |% d1 \' @- G; C) F8 a) p- N    //if (i==0){break;}+ k: @% @$ @8 m& A4 t" u. u2 f
   //SerialUSB.println(i);" I$ O. B  F+ o* j) @( Z% ]
   //delay(1000);   
: K- y; L& t3 f7 M
( w' A9 D% h1 o& A9 M7 h. M1 h   if(i==2){
5 X& x" U7 U, |9 ]: q8 O      set_target(1000.0, 0.0, 0.0, 0.0);7 Q. o( V" a, k$ s4 h- c; b+ h1 y
      dda_move(getMaxSpeed());
" L4 G% I8 F1 z0 Y# T: E/ f      if(stop_flag) return;( h1 K* ?4 b4 f( d
      //process_string("X0.01",5);
6 [0 }6 l( V) p8 W3 B$ N      //init_process_string();6 d) l' U5 a' ^  V4 x
    } & E( T( ^4 J9 Z2 O" x! B! J/ E6 {
    else 8 x( `) A; _" y6 Z' m$ [! G% f
    if(i==5){4 |- j7 e9 Z" f$ d' H& a
      set_target(-1000.0, 0.0, 0.0, 0.0);, `0 a/ |# w( E
      dda_move(getMaxSpeed());
! f8 y& G: ^& i4 m! R      if(stop_flag) return;& ^& \( k& N: f0 a7 W! X3 {! Q) \
      //process_string("X-0.01",6);) x5 p9 a2 B( }/ F* E# [
      //init_process_string();
8 j7 u* E$ u6 F  S4 |    }
0 Y" g: _9 B3 O5 v! v: X3 I, u     
# o5 Z" o- F9 K" f  W3 a% x+ T* f! S
  }
& q' J" w; ~7 w4 Y0 o1 ], @2 Q) q: t, J( D5 `' e4 o
  //init_steppers();: x* ]: W# L) }. h8 m! L
  //init_camera();
% ~  F5 t% L9 i   // SerialUSB.println("Return To Normal Mode");
, w( `" ?( c! i     process_string("G1",8);
7 d8 T, o; E5 y1 T. b; U  // process_string("G91",8);* j8 _" ^* t# q& @- Y) j$ z* {+ \
    init_process_string();
6 w& a4 |  m$ ?! m8 b& K' V0 }( m//#if (LCD_TYPE == 1604 )
. }) h2 A8 g  m) O1 H# \4 a) N! V9 j  //当LCD为1604时显示要处理的命令
0 Y1 b9 ~6 N. E7 D2 V  // lcd.setCursor(0, 4);
0 }$ E3 q9 |/ ~, [8 w" q# |# l  // lcd.print("Return To Normal Mode");/ s; p1 I! A* ^! t, e: C
//#endif+ H% a; S* R2 Z9 Y7 c) d# g
}5 U; E! J  I/ U. N) W

+ e6 O3 o: \0 m2 b3 k( d0 C4 [9 [void run_a()# h) U6 j& ^+ W2 ^9 W4 ]
{4 v$ f- j3 ]% q( g
  //delay(1);$ f# Q/ B7 R5 i) U
  //if(digitalRead(14)== HIGH)return;# H4 R  F. K7 D: o5 Z$ F/ |
  //if(digitalRead(14)== HIGH)return;$ U& `" {% m* s* t, P
  //process_string("G1 F2000",8);8 E; R- O& b& g( A) U/ u
//process_string("G92",8);
; n" B9 |8 N+ u# D process_string("G90",8);( r/ m0 X/ f/ Q+ x& ?1 `
process_string("X120 F10",5);
, m. p4 r! G; T6 F: Z. r, X! W process_string("G4P2000",8);" o. q) B5 U3 p5 D; K* j; N
process_string("X10 F10",5);
; h$ D9 p5 k' [5 M( ?// process_string("M101",8);' a/ U/ z9 `9 b  r' k
// process_string("X-50",5);
. c; e5 M& G" H6 T6 X# J dda_move(getMaxSpeed());1 N2 t3 |- t9 Q8 \
init_process_string();
. H' t  B6 g0 r. _2 ]# N, L* o/ ]4 z6 b! g1 F
}
- R) C7 b* Z9 U. h# v
回复 支持 反对

使用道具 举报

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

( }! C( f1 }4 n7 V& q% P0 }6 ?7 R! x7 g, ?1 z
// 定义机器参数6 q* D7 b! m: a0 u1 m
#define X_STEPS_PER_INCH 400' U. v) X! \1 L8 C1 l& P. l+ U
#define X_STEPS_PER_MM   16.0; R! A/ K/ f4 v0 H
#define X_MOTOR_STEPS    200. f6 e/ q' [6 c/ F/ ~% [, g; ^* ]
5 l7 E) e* g$ f; L$ b
#define Y_STEPS_PER_INCH 400.0. s% Y2 k8 B: p4 m7 q, W' b: g
#define Y_STEPS_PER_MM   16.0
  t, C' z4 k, z- W$ k+ {#define Y_MOTOR_STEPS    200
& A, ]) _2 y. t( o" _- \5 R9 n- v+ G7 b5 G
#define Z_STEPS_PER_INCH 400.0% ~" o2 x+ K/ z: c$ o' n9 @/ |
#define Z_STEPS_PER_MM   16.0- ]+ e& R: E9 N$ b8 e9 ]
#define Z_MOTOR_STEPS    200* C# [+ o' v/ G- A

' y6 [9 K' }* d/ j0 L" j#define U_STEPS_PER_INCH 400.0$ n1 Q2 ~. Q4 \+ I, p
#define U_STEPS_PER_MM   16.02 p& k/ O( p) @3 A
#define U_MOTOR_STEPS    2004 ~& N7 V: \' `

" ~& `/ N- `8 e7 Z" j" R//最大进给率2 {# |% W6 b2 z4 @2 @) z9 r- ~
#define FAST_XY_FEEDRATE 1500.09 m6 X+ l) w% r; m# G$ c
#define FAST_Z_FEEDRATE  1500.0
3 {+ U. B) g" j  e#define FAST_U_FEEDRATE  1500.01 @! ^. x+ M% F! R
// Units in curve section
1 x8 f+ N! E7 {1 I#define CURVE_SECTION_INCHES 0.019685
5 s: B3 T. e6 f" {* I#define CURVE_SECTION_MM 0.5
8 _$ X* K# j, N) g9 H
9 e9 A& S9 I0 J# A' [& n, z, G9 Q4 y# }! b3 b- y* {
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
& ]( K8 o0 g* X1 a. ?2 f% j4 Z// RepRap opto endstops are *not* inverting.
- [. M5 h- \0 z#define SENSORS_INVERTING 1" x; r( |9 v2 s( v
; ~7 a. M. X- p, a% }
/****************************************************************************************
+ ~% ]2 u" G; x * digital i/o pin assignment0 o2 k& z" m0 H$ S, p* O
*
: t5 A5 J% @! ] * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5: ?7 y; s% n% W: M+ U6 s
****************************************************************************************/
3 u$ _+ w- x0 x3 u6 G& e3 A. Z' F, N
+ r3 |2 _) f1 P! D' x//camera shutter and control pins; ~3 ]2 Q- J& f8 x  f. D
#define CAM_SHUTTER_PIN1 29
% e8 S: i; H1 k0 z* L+ P7 v. o" A#define CAM_SHUTTER_PIN2 30' l% |- z1 ]& T# j
//#define CAM_AUX_PIN1 31 // analog 01 X0 ~; S5 x6 R+ Y+ m+ E  A
//#define CAM_AUX_PIN2 32 // analog 1
0 i: Z' T' u2 u+ u5 J9 V//#define CAM_AUX_PIN3 33 // analog 2
8 w9 H) Z2 l& i2 ?//#define CAM_AUX_PIN4 34 // analog 3% r" F# z) N/ y/ i  {" |0 w

0 z/ e+ e# t7 b$ V, b" `; w- z// stepper driver pins9 ]+ Q  i/ T: I$ x7 l/ g" m6 j
#define X_STEP_PIN 7! u  O5 }  S( z- b- @( @/ r
#define X_DIR_PIN 8
9 s! ]7 E( |( `* C6 O#define X_ENABLE_PIN 198 b/ N7 ~) z0 f0 s; v

: \( D( y! ?: S: k! [$ H: ~#define Y_STEP_PIN 9. N. k* v& O6 f' \: k/ ~
#define Y_DIR_PIN 100 H* d2 q* |- J: P1 N
#define Y_ENABLE_PIN 19
/ o. I+ e) Q6 @- D0 `! y! k# k
' N5 I3 [3 u% a' k: t* G#define Z_STEP_PIN 11
9 @; C2 P) {1 \- v) s* l#define Z_DIR_PIN 12) z, T. p" E6 r1 n& P; @3 r* B' y
#define Z_ENABLE_PIN 19& s" Z+ J2 Y# {# }; T1 J8 @8 l* p$ ]8 o
, R8 s+ \+ }  R7 a, s
#define U_STEP_PIN 13
2 ?8 e) I; a2 _/ i7 `#define U_DIR_PIN 14( j) B+ Y: ]% b. J
#define U_ENABLE_PIN 19
, Y: l2 D$ m7 e( C0 Y- L
  c) _9 Q" u# a- j* H& t5 q// limits not used right now- f  u+ D, B6 b
#define X_MIN_PIN 14
- {0 }9 M& i& g#define X_MAX_PIN 14# Q1 c5 i5 X" y# {4 @6 [4 S
#define Y_MIN_PIN 14
. O+ l) Q. }( G; t* J* I' o( L/ Y#define Y_MAX_PIN 14% q* n5 B% e/ k& j
#define Z_MIN_PIN 14
' S$ K& R4 Z( N8 }8 v1 N1 f# L#define Z_MAX_PIN 14
6 W2 R; S  q1 n0 m$ ~+ f! }( ~#define U_MIN_PIN 14- o  R: g3 V% b' ~
#define U_MAX_PIN 14
6 e5 ?- H% p/ I+ D5 Z0 f( J
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()
1 u9 Q; G! |5 W& j: h9 o; q{
3 c% `! Z! z: T2 w7 ~( A2 V. l  pinMode(CAM_SHUTTER_PIN1, OUTPUT);/ f1 l0 }; n5 s  S! U
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
" a" Z( g- A0 h  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0
( P# }3 S, x! x$ k5 i // pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
! |9 F5 R: T, ]- V0 I // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2' S0 N' l$ C% q5 ]+ D" ?3 q% }
// pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3
$ ?1 _4 W- i1 R3 Q7 h9 V! Y}
4 u$ ^& k! c/ d- R3 ^" _6 l/ M$ {
( m7 B& L4 h. h9 k0 Yvoid camera_shutter1()
1 u$ u9 a4 j& j/ u7 i{/ A, M( S3 O" o3 _
  // fire the camera shutter via relay...1/4 sec hold time7 u8 p- @, y" k. }
  digitalWrite(CAM_SHUTTER_PIN1, HIGH);) D5 {7 _, w8 V
  delay(250);
2 C) [, E6 _- ?, `' _$ t* x. |  digitalWrite(CAM_SHUTTER_PIN1, LOW);0 z' N- d( u- H

* L4 u' w" ]. }9 M/ D: a+ z}
8 l' x# S" I9 ~7 X+ K, i. [' O  j2 [2 h3 A' N# r5 `6 H+ c. n! e
void camera_shutter2()
8 ?! ^5 M* m6 n. q/ ?8 H3 m4 R{, w: Y; y7 D' J2 M2 h
  // fire the camera shutter via relay...1/4 sec hold time
. i- a4 y& J1 ~7 y( F! a( p7 H7 f2 {  digitalWrite(CAM_SHUTTER_PIN2, HIGH);! f  V6 n! P) g+ s, M& }  e
  delay(250);: S- U. j+ m9 s: A" y# g# L
  digitalWrite(CAM_SHUTTER_PIN2, LOW);
2 _9 f% u$ D! y0 t) C7 y
6 h( O% v1 O+ `}
2 g  u- x* v, D. X" r/ C+ y0 `4 j! ~/*# R$ ^6 v1 W/ g+ X4 ?& N
void camera_aux1_on()
7 L: z. K1 x0 @" A9 H8 M{
5 Y! i7 `! s9 N/ ]4 \5 ?  // turn aux relay 1 on" E- T( `6 y0 R1 P! Z$ m* O% T, x
  digitalWrite(CAM_AUX_PIN1, HIGH);
- Y! x+ M) Y; O* d4 M- Y}& u" g) L5 l% \7 j0 ?0 X/ l4 y
8 p, |8 l+ q$ ~2 _+ H
void camera_aux1_off()
8 z# U' Z- M; J0 U{; B' B2 P6 ]  ]( U4 r$ {. O
  // turn aux relay 1 off
, M/ ?7 m+ i0 r  digitalWrite(CAM_AUX_PIN1, LOW);" z. e% h6 e& O- b( G
}% j. i1 ?5 I$ P& z4 [# Z% x
, H$ w3 p% e" E# o9 y4 b* W% r
void camera_aux2_on()
) J8 x" r7 i- ^$ I{
$ s1 _3 a& N9 V) a  K9 v4 ^  // turn aux relay 2 on5 K. T  E3 T' c$ W2 k
  digitalWrite(CAM_AUX_PIN2, HIGH);
9 u& t: c5 V/ B( p7 D}
+ H& ^4 Z& m$ o! F+ n2 o; D( I6 W, k4 ?: [! ]! k
void camera_aux2_off()# t. h. z& q+ C
{
# ]& ]$ C; M5 l  // turn aux relay 2 off. O" G% i8 b7 q1 d
  digitalWrite(CAM_AUX_PIN2, LOW);
$ s; l  Y2 s) \1 `}) n& m9 r4 n( y  Q$ ]* u) ~6 ?+ J

$ G* Y; C( M+ o' z# ~- I0 svoid camera_aux3_on()
3 l. A9 ?0 a6 b5 J6 n. F{
6 w0 s% C  Q- Z  // turn aux relay 3 on
- h$ Q# N  q5 Q  digitalWrite(CAM_AUX_PIN3, HIGH);
; H/ b" s4 z0 k7 l; A}
1 i$ _4 O  Y. v2 r7 b3 E* F3 z" @+ R$ U
void camera_aux3_off()
/ P8 a; Y! h7 B! f9 X+ R{1 k/ S- ^+ n2 A' X" P) d
  // turn aux relay 3 off
9 z! t) W$ E. }$ ~  digitalWrite(CAM_AUX_PIN3, LOW);
; a. ^2 X$ k" m5 R) [}
! X' i% A9 @' m+ r) j* E3 c# C' p
3 e: W& E% G2 uvoid camera_aux4_on()1 K5 x( B; R% s: h
{, p4 y. L1 Y' Q) [: R* A1 g
  // turn aux relay 4 on
+ B7 O  U& K& G& z% F; P: u  digitalWrite(CAM_AUX_PIN4, HIGH);
, V/ R! ]8 A$ ^$ u# v  }}! m1 ~7 J+ V' a/ C: }

7 ]8 `7 A2 x+ U1 K% l. [3 vvoid camera_aux4_off()! T9 e; A1 ?# b
{
# a$ d5 ]1 [  D. Y7 R: A2 n  // turn aux relay 4 off$ x$ n" G3 g' z$ X4 {( F2 E+ t
  digitalWrite(CAM_AUX_PIN4, LOW);
$ g0 q# ~( m% t4 C7 }* ^6 N}
回复 支持 反对

使用道具 举报

发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错/ G3 f/ N: `& r' G+ d
请问楼主是玩什么的,用乐高玩具吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice.* E  {1 ]  c8 N& Q) S2 |
struct LongPoint {
% K% d/ C5 v4 `  long x;% G" e0 o! D0 @- G
  long y;
4 D1 A1 p  N2 X: c- a% C! |  long z;
4 i5 r" e2 t% Y8 [( j  long u;, |. }& z; j" g& D! o" M. V! O5 E, R
};
5 o8 g8 F! o# p! W4 G% q; t" S8 {8 R$ |9 X% |, a: P4 d
struct FloatPoint {
4 d* T) m; y' l: b; F) a  float x;
" h& A# U0 b8 V4 M, e* J+ ?0 a  float y;
" Q4 O/ |2 j$ D/ V' {  float z;
* N& L7 \! `# _  float u;
' F$ \9 O: D7 s+ T) n- q1 j) c3 q};
! j, i7 k: u+ {" \  d
+ S- {, P* B  i3 V) zFloatPoint current_units;: _: B# g( d. [
FloatPoint target_units;
, D' R! }( @# i! tFloatPoint delta_units;
5 I, d. D8 J) T6 C* e
8 ~. p& H4 w" J* N+ V! o, aFloatPoint current_steps;
: I) b4 V+ c$ ?7 p6 e# yFloatPoint target_steps;" U# B* t: P! n
FloatPoint delta_steps;! H$ N3 A0 Q; {3 Q% J+ Q, B" K
; c- N( C7 W, U! H4 [
boolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式
; N- T- A8 d- v- W- {8 C& q+ O- l' x" o, B& O
//default to inches for units. b  ^! ?2 Y' l
float x_units = X_STEPS_PER_INCH;
3 W, t) V6 X: Q% }$ g3 K5 yfloat y_units = Y_STEPS_PER_INCH;6 W" m. g! N/ Z4 Z
float z_units = Z_STEPS_PER_INCH;
& F0 e+ a) d$ {, J; kfloat u_units = U_STEPS_PER_INCH;6 k3 x8 ~3 U3 x. T6 s# E, ]( F' L6 A; S
float curve_section = CURVE_SECTION_INCHES;" N& q: e) [( U8 w2 k: w

9 t- s% |( ~! J. i9 w//our direction vars
; ?0 |" z- U: h/ dbyte x_direction = 1;5 Q$ N5 r' w1 |
byte y_direction = 1;: @/ Q6 b! N: i6 G  L
byte z_direction = 1;
! A4 P2 R8 e) d5 G* G! X$ Nbyte u_direction = 1;$ s. R5 N" N5 F, t% I
$ e7 n+ ~! n6 t7 a% R% S: y# A5 p
//初始化字符串处理
9 |0 O) q5 p' S- O" D- Bvoid init_process_string()
( b% r  z0 A/ X+ s" f! t1 M{
5 ?6 M$ w6 Z- s$ x* W  //init our command
) A; h. Z: `1 D* o: T  for (byte i=0; i<COMMAND_SIZE; i++)
, k& |) |- f5 b    word_old[i] = 0;
# C  B% R/ Y; [1 g  serial_count = 0;4 I) h* y9 p4 T, ]; m! c. ?4 u, H
}5 f- l8 J: z# m
: r4 s* t0 u+ k2 I, F" a5 R- P
//our feedrate variables.
* q/ u* \5 H  Pfloat feedrate = 0.0;
* e- C; Z% s5 F0 e5 g6 h4 w. y4 a- blong feedrate_micros = 0;
: E: X. R( B- \2 x* J
' O' g: \" `' U+ `//读取并执行命令
; N% H# z$ Y" Zvoid process_string(char instruction[], int size)( R9 V% |/ M" w' T- I0 |) J& e
{
) u) |# H1 O- @8 w. S  //the character / means delete block... used for comments and stuff.
; t* J' Y) P' E  if (instruction[0] == '/')
0 t9 N! D. r2 B1 w. t  {
& o6 ]- J! P, l) W3 }1 j" ~5 q9 a    // SerialUSB.print("ok");4 `$ H: y" M4 \* z& ~2 C1 I2 S6 W/ P
    // SerialUSB.print(byte(78));
  k5 D4 h: v( M$ {  [1 D    return;9 Q* V+ j7 }2 E' p% L- V
  }7 G8 C1 q( e% G# M
  //init baby!+ N& [% |2 N% R8 _, t  A
  FloatPoint fp;
7 w% q8 I# Y$ t! R5 |0 E) h* p  fp.x = 0.0;
& a, S; Z8 }2 i  fp.y = 0.0;
% g) x# O9 x/ F, L* ?' o  fp.z = 0.0;( b; h! h+ ~/ o0 h3 ]2 K  ^
  fp.u = 0.0;
5 V, L( h; B/ ~2 @) h/ H1 ~! Z* m( s8 p( @8 S+ ~/ z
  byte code = 0;
) L4 d1 h$ V8 m. ]
1 V* n" ?8 b$ O& X$ _: q$ ^  //显示在处理的命令
0 f. D( r" H1 [- e% v#if (LCD_TYPE == 1604 )4 i8 d8 r9 ^2 a7 k3 @/ n
  // lcd.setCursor(0, 4);
+ T4 a- v- V' r' z+ B  // lcd.print(word_old);0 X) p) r" x) r( k2 u1 t. S$ Z$ @
#endif
) l2 ?% `9 {) a6 q1 N  SerialUSB.println();
, H5 O( o5 ?) w8 ^. w& `  f0 N, n  SerialUSB.print(instruction);
/ Y( N5 Y3 w+ B% @( @7 f: `  SerialUSB.print("\t");
* k' M' S* {) J' T0 d6 M9 o. Y9 x5 P. X6 }7 ]
  //what line are we at?
6 b+ f5 x, S$ d: t  //        long line = -1;
6 K% X+ `; g2 s/ L8 u, t$ J& |  //        if (has_command('N', instruction, size))
9 e7 [) D2 a4 I$ E  //                line = (long)search_string('N', instruction, size);& _0 n6 i2 J7 L/ i4 K. W

3 G: B7 [& M* L4 k1 X8 c  /*6 \* W) l8 @7 H8 G# D. \( T: |
        Serial.print("line: ");8 O  S$ a2 h7 f5 k
           Serial.println(line);
4 Y3 X) H, R/ \8 M( q+ r           Serial.println(instruction);- }$ u1 d+ {& r7 e0 r
   */
/ [' A( T0 ?3 }8 d8 I  //判断是否读取了个 G代码?/ w7 }& V  }  B4 l
  if (
9 V* H1 N8 {. @* I7 C% t    has_command('G', instruction, size) ||/ d- G; }! g1 c0 P, N, r
    has_command('X', instruction, size) ||
6 G! {" W9 Z2 D) _- ?    has_command('Y', instruction, size) ||! Z* C. b1 |9 E8 E, E2 U
    has_command('Z', instruction, size) ||* U" B& q$ m2 V* p" a" u
    has_command('U', instruction, size)
3 P8 e. ?5 Y9 E# W0 u  H    )
( G1 Q0 r6 _8 k9 M4 L  {
+ m* q1 p4 p' c# z4 a6 x" I0 L    //which one?
, ^) s$ |! N' G" f    code = (int)search_string('G', instruction, size);0 g" T7 b* [+ B" e9 q+ D1 `
    // Get co-ordinates if required by the code type given
4 p" W5 C5 L! ^    switch (code)# d# m6 V1 l1 K
    {
5 Q5 D" y- v9 V6 M& n    case 0:
( l0 f" r' w3 z, n; @, C1 u    case 1:
0 k- q8 W: q( `    case 2:1 a; j3 d0 w: ?. `8 v0 O  i
    case 3:+ i# A7 F' ?# j+ M2 m8 G
      if(abs_mode)
/ l, [1 B+ b3 S/ ~% t1 c      {6 n+ X. N# F; o  c" i: z
        //we do it like this to save time. makes curves better.& R: D, h8 e( Q
        //eg. if only x and y are specified, we dont have to waste time looking up z.! r3 K% }2 \$ ~# X
        if (has_command('X', instruction, size))
# s& ?. w- r' B1 {          fp.x = search_string('X', instruction, size);+ j6 x! o: M: C& l. u
        else
5 l% V' v6 \- T4 v          fp.x = current_units.x;
+ M- w0 q# y5 h/ q* V
4 z3 m- F% [/ ^% _2 q6 y8 u        if (has_command('Y', instruction, size))3 e8 S: `# t" }& O8 i8 `! U
          fp.y = search_string('Y', instruction, size);
+ L& S3 K3 [4 K1 i        else
4 U$ |4 I9 U3 s0 V3 Y; ~          fp.y = current_units.y;
& j& u+ C& f, F+ H1 H; R% X. d- Y8 P, |- Q- x: T
        if (has_command('Z', instruction, size))
+ }" O3 C$ O* U. x7 F1 {          fp.z = search_string('Z', instruction, size);
+ q( {* g1 \" A: Y0 p8 J        else
0 \2 o+ |$ O' R" F1 t' i$ a          fp.z = current_units.z;1 ^1 _+ U5 a" |8 E9 O: `) Z
         
$ c# J, X  b) v& ^) x' r+ x        if (has_command('U', instruction, size))
& p# |! u* ]2 L9 Q          fp.u = search_string('U', instruction, size);2 o; O8 H" U7 i4 M
        else
8 J/ u9 d$ X$ Y4 n( d0 y9 E          fp.u = current_units.u;" f" E5 Y/ A" M" A) x2 z1 R
      }
0 k; F3 Y9 k8 k      else2 {. i, |* B8 [* W
      {
3 m7 E2 ?/ Q' `- t# m  w$ k        fp.x = search_string('X', instruction, size) + current_units.x;
6 ^: j: D# Y. A9 j9 J        fp.y = search_string('Y', instruction, size) + current_units.y;' T$ ~& `* [6 {/ F) W
        fp.z = search_string('Z', instruction, size) + current_units.z;
- o$ g) o' I1 p8 _        fp.u = search_string('U', instruction, size) + current_units.u;" N! f0 n9 [1 w3 D
      }1 K$ W' A5 B" e/ `. b3 h1 o" ?
      break;& ?1 l! e( ]! a' M
    }+ v( k6 E) i0 _& L9 h- _: L( S
    //do something!
0 I5 F% ?. `/ a( @    switch (code)
6 j( B4 O/ l% T. z" x4 G    {, T+ {, `5 z7 p6 Q5 E4 g$ P
      //Rapid Positioning! L& `9 p9 p: M9 l( {" c: R$ x
      //Linear Interpolation3 b" p) L* N& s: J" y. s8 I: i
      //these are basically the same thing.
# g, [4 N8 g3 R3 Q/ N$ w1 Q    case 0:
& ]% [1 j: U7 S' e! F- t' u1 C" S/ H$ o4 s    case 1:
. S. |1 B3 i3 h, C% t6 K2 N      //set our target.
3 l! k) s! V% n. F# V      set_target(fp.x, fp.y, fp.z, fp.u);1 x5 X' _  h# w% r; x' b# I
      //set_targeta( fp.a);: r( _0 Q1 ?8 {! E
      //do we have a set speed?- D) X2 A1 j, u# g; m+ `4 h$ w& U
      if (has_command('G', instruction, size))
: q! A  h( C/ @8 u      {9 }9 z3 R8 O9 F; D. c6 b" v1 e
        //adjust if we have a specific feedrate.
/ V" s. g' w' |+ S2 q1 U        if (code == 1)
4 t- K7 o" U/ H/ y; z/ w        {
9 `! H+ N- N, l2 H          //how fast do we move?
8 I) i, {; e: y1 B; R          feedrate = search_string('F', instruction, size);
# z' T( f0 G  {          if (feedrate > 0)9 t, @# c6 ~1 Z% C( a
            feedrate_micros = calculate_feedrate_delay(feedrate);; H( u% P; u/ B7 m3 M4 V; I9 P
          //nope, no feedrate
/ b6 `; I" C4 K4 `          else7 b' V: v6 \3 i% ~2 ~
            feedrate_micros = getMaxSpeed();+ C) j* v6 ~( ?/ h. `. T
        }
' f* K5 G2 ~0 J* P& }        //use our max for normal moves.0 z7 Y% b2 Z- f' w9 s, ?
        else) ]6 [5 S, s9 Y2 V  W9 O+ q/ K0 A
          feedrate_micros = getMaxSpeed();
, \& U! F# B, t- T6 q' a      }
' Q# d  l# D, a0 h$ z      //nope, just coordinates!
6 _9 O. d; L% v. S! @7 r7 u      else
! o6 {" X$ r/ F& w      {4 r2 N0 q, @/ S5 c9 H& R6 B3 k
        //do we have a feedrate yet?
4 W" X) E2 K8 B1 S/ R3 s        if (feedrate > 0)$ P" d* c+ s/ ~0 h
          feedrate_micros = calculate_feedrate_delay(feedrate);
9 ^, n1 ^8 f" I/ M* g4 g. L        //nope, no feedrate: F* ]9 [3 X' J4 v0 ^: l6 l% P- ?
        else
# K: H9 o. e6 S0 D3 X: }2 S          feedrate_micros = getMaxSpeed();7 X1 t) a8 i1 Z+ _" @% }- _
      }3 R/ m, m7 H+ j8 }; |1 I. {8 ~' X

  N0 o: g2 H0 i      //finally move.
. y3 y0 `( Y; r      dda_move(feedrate_micros);
% _. R" W/ {+ Z5 i, f! r' i      if(stop_flag) return;
$ A) y; {' \' V, B( Z) o/ P      break;
0 w5 d# q( K- l4 E
* H+ w2 }% X5 i" ~4 T  n9 b      //Clockwise arc- p* ~/ f, J6 O$ {7 `
    case 2:, V3 t) C. s' H6 Y3 Z
      //Counterclockwise arc) z- X9 e+ {+ A: x8 j% c# i
    case 3:" |0 @; K7 ]. O# R
      FloatPoint cent;% v; d8 m. p' Z8 ?! T* x
      // Centre coordinates are always relative
; x( ~# N! `; L5 y( R      cent.x = search_string('I', instruction, size) + current_units.x;
) U* b, c) z( k; a      cent.y = search_string('J', instruction, size) + current_units.y;& `* m& _. d3 Q& u7 N, e9 j. S5 R
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;% \+ X3 k( J; K1 Z- z! C
. @" K& z# H* [2 C7 d( l/ l6 @0 ?
      aX = (current_units.x - cent.x);+ O6 \; Z8 q* _4 E0 j2 E1 g: h5 K
      aY = (current_units.y - cent.y);, J$ c, ?+ m5 v4 E: }; ^1 u
      bX = (fp.x - cent.x);
6 s/ y* @. ~3 y      bY = (fp.y - cent.y);
) u0 [0 i/ V0 I  s9 V3 J2 v2 v4 ]
0 x" q, t/ l- S& G2 x; E. X! o% e      if (code == 2) { // Clockwise0 @3 W! G- S- c  O* H3 N
        angleA = atan2(bY, bX);
5 D1 `) Z0 M. h        angleB = atan2(aY, aX);  M  R9 `! ]  N& F) f% H  k
      }
4 M. N9 H5 k7 f; D# m1 H      else { // Counterclockwise
$ I6 Q, K" y: m        angleA = atan2(aY, aX);/ ~+ G; t( _  \! x6 v" C8 g
        angleB = atan2(bY, bX);
! F& D0 Z3 K4 D6 f" {6 o/ o      }2 C- H9 O" p' t9 z$ B0 a4 a# }! W
      // Make sure angleB is always greater than angleA3 \" O. O; H# ]3 i/ ?
      // and if not add 2PI so that it is (this also takes
  j& p: M4 n+ W0 A- c; }6 n+ {      // care of the special case of angleA == angleB,
4 d( |0 ^7 i) ^, u# l; ~      // ie we want a complete circle)
9 _/ b* v6 H9 O      if (angleB <= angleA) angleB += 2 * M_PI;2 D3 D( A+ f, q8 u, ?: M% M& E: J! G
      angle = angleB - angleA;6 A3 ^6 g7 Q4 C; @

5 V5 u; _) K7 z/ X      radius = sqrt(aX * aX + aY * aY);6 c/ N2 B$ d* C. F3 }2 w  Y
      length = radius * angle;- w% I& v5 X% E# K# a6 l
      int steps, s, step;
: t% X5 r" l9 O, {+ ^* I      steps = (int) ceil(length / curve_section);
5 Q9 r$ W+ \9 t4 h" E! f" h# ~% |; W% ^
      FloatPoint newPoint;
' F9 `/ Z8 q: E/ b& A9 V" d      for (s = 1; s <= steps; s++) {' Z& F" `* k9 L  d. N
        step = (code == 3) ? s : steps - s; // Work backwards for CW
5 ?9 V; R+ l/ P1 e6 t+ W        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
- {! x* ~* c! G. K, _0 F2 ~        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
9 j: `* R/ M9 g- [& O% \/ Z3 S. C        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
) Y* h* ^/ r; a2 h0 P/ S
6 S+ a( R5 S' {. s" @6 B        // Need to calculate rate for each section of curve
) R0 N! l9 a1 A* w: G+ H        if (feedrate > 0)0 T3 {" ]- ?" `8 I9 Y0 I
          feedrate_micros = calculate_feedrate_delay(feedrate);- |/ m; G' F) G
        else
7 V8 b" Y5 M! x) E: f0 A3 [          feedrate_micros = getMaxSpeed();
, ^8 C4 B( e3 _% b/ }5 [: R8 \. P: j: @1 m8 o% @, s
        // Make step9 j% h; z6 l+ E! g/ T: l8 }- R6 Y
        dda_move(feedrate_micros);
& H- _# @0 ^' b% \8 k' K5 }6 u        if(stop_flag) return;/ E& Y" @3 B* _1 T5 j
      }( |( f" C% ^, G! E, d* a/ X

) b/ J3 C- O9 ]) U5 B# D      break;
1 R: B, e+ x6 }: c) Y
- J/ _4 K) J) F) d5 l      //Dwell. z$ [1 d: U& a: W* K
    case 4:0 Z1 C+ E! o+ p3 q% Q' }
      delay((int)search_string('P', instruction, size));
8 O* G3 [0 I2 f5 C( S& E& J: k      break;
7 J! x) U! l6 x. z8 U! ]- \
$ ^- n& i# x- J- z) I- f: L& p      //Inches for Units9 C* @1 g$ L' A: F- }
    case 20:
8 f3 F+ n' ^% u/ j      x_units = X_STEPS_PER_INCH;& [! M" `" B0 P
      y_units = Y_STEPS_PER_INCH;' O- l! v2 ^+ e5 t
      z_units = Z_STEPS_PER_INCH;
1 V: k5 G5 P; u. d# I5 _( O! \      u_units = U_STEPS_PER_INCH;
! [7 S. D+ W" D, O2 s+ K( n% `      curve_section = CURVE_SECTION_INCHES;
1 O9 C2 w& C8 D/ E7 v& f      calculate_deltas();
+ r- A" O& r. q0 i- P4 j: R      break;. c6 g; \* }( b! }4 W0 R
. C+ a# t3 u8 J% k' Z6 ]
      //mm for Units' E, d( Q$ O  z1 K2 |/ G3 Q
    case 21:* g/ N9 f+ G+ [+ i. |  ?7 o
      x_units = X_STEPS_PER_MM;; Q0 c0 [# Y$ Q6 ^1 R$ ?7 k
      y_units = Y_STEPS_PER_MM;
/ _" ]9 A) G  J# u  R7 ?      z_units = Z_STEPS_PER_MM;: _* ^1 m- S& c1 T/ l
      u_units = U_STEPS_PER_MM;( w/ W& O9 I' k& q4 s2 O
      curve_section = CURVE_SECTION_MM;8 C+ f4 n1 E+ C* [7 x) H- A) [
      calculate_deltas();6 `. d. F! c: E3 o6 O, Z
      break;
# H) V; F1 t# ?: O8 C; Z7 j0 m3 T
! c* V2 m5 N  K# \$ \  A1 w, v; @      //go home.* z% B  s( H# M4 F
    case 28:
8 |2 m6 w7 H9 L) D/ [2 D      set_target(0.0, 0.0, 0.0, 0.0);% ]% J0 v: p+ `' B$ O+ V9 G6 g
      dda_move(getMaxSpeed());
  ^8 p9 t& ^+ c- D# Q! ?      if(stop_flag) return;
3 M6 _+ \# R: ]5 j# a* j      break;
, c. S4 V0 Q3 U: r- _6 Z9 c0 U& v9 ~8 R' l" y! B
      //go home via an intermediate point.
: U( Q$ `% g1 b& D1 y    case 30:
$ E7 }- ^. h  X4 C      fp.x = search_string('X', instruction, size);1 b$ r3 b9 P+ _' u. \% M5 M5 X( h
      fp.y = search_string('Y', instruction, size);5 x9 w/ y6 m. p  c
      fp.z = search_string('Z', instruction, size);: ]  X8 ~7 ]: ~
      fp.u = search_string('U', instruction, size);2 i, C  E4 B) `) M4 k) A+ A; [; `
      //set our target.
" c3 d" G1 J# N( ~2 ^0 X6 E# {      if(abs_mode)5 q7 D! ?! S3 z; \, E- q
      {) d# b) g0 k- n0 K- S1 _) D% B
        if (!has_command('X', instruction, size))
4 m/ \, i# V" z3 {; R          fp.x = current_units.x;4 d" Q1 V0 q3 g3 K$ t, ~
        if (!has_command('Y', instruction, size))
! v% @7 s* e# h% J, ^$ Q3 K          fp.y = current_units.y;  I2 r- o. d" O' M
        if (!has_command('Z', instruction, size))4 {8 m. J2 I; V8 `6 A' {3 z0 n% Z
          fp.z = current_units.z;  x3 A5 v+ B2 V. i9 I# e# o
        if (!has_command('U', instruction, size))
& q3 c2 x$ V5 @5 b# d* U: @          fp.u = current_units.u;. K& h9 x5 W% t! i) `
        set_target(fp.x, fp.y, fp.z, fp.u);6 k9 ]4 ~3 q- \1 _$ q! i
        
$ W. i; d% N( o8 H: B9 s# u      }0 q+ v$ l% e5 o3 x! l4 U
      else  G7 x/ L1 A& D; Q" g0 N
        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
  O# S. v' H* B& d. l3 s& t      
' S& ^+ b# p9 ?3 v4 S, M. S6 T      //go there.
' Z- M8 ]# ]3 H      dda_move(getMaxSpeed());6 w) ?2 \% v* Z1 p
      if(stop_flag) return;
& X2 P5 v* ]1 H; j
/ O8 g/ w, ^1 @' u6 l2 V. Q      //go home.: S& ~' V1 ^" F" e6 C
      set_target(0.0, 0.0, 0.0, 0.0 );6 E6 c* \) \4 x
     
2 _! f* ]( u0 t# f* g" J* D      dda_move(getMaxSpeed());: C4 I0 B! F4 t9 _
      if(stop_flag) return;% ?4 F/ ~2 C+ h0 A' \$ {5 l
      break;
6 o! j; o* g6 ~# s; a" M& D/ i' e& ]
# r5 }  \9 _- b* h+ r' w* E      //Absolute Positioning
/ W8 A' Q, K' ?    case 90:
. E! l% M$ ?+ x# ]" e      abs_mode = true;, n$ J' q/ O  v2 i
      break;
. V, O7 M& Q( E+ h: J7 a* S
  Q) i" F: @" [; _" E; O, A) c8 ]      //Incremental Positioning* {" o  S; Y/ W: U6 k# S9 F
    case 91:8 e1 t, b- M4 f
      abs_mode = false;
% N% w3 P( g- z8 d6 o, \      break;
/ e6 W4 k' }+ T
/ s4 d: `$ U" c      //Set as home! S- ^; ?( N4 C3 p' d
    case 92:; A9 p/ H3 t4 Q% x1 ]
     
; [3 ~$ h, f+ Q6 p      set_position(0.0, 0.0, 0.0, 0.0 );* ^" m2 l0 ?4 x( ?2 c3 _7 a& m+ c' T
      
- P* o* |0 |1 K      break;
9 _1 m4 M* a. W' @, {0 D* [8 d! H4 M3 Z+ K$ @) a$ t+ m* h5 l  t
      /*
* A! F# l# m) t7 F                        //Inverse Time Feed Mode  w2 o- I& I& z9 Q' q
                               case 93:: C+ k1 O4 V  t$ r; P( Q$ s
       ! G$ P* |6 J- c3 S- E. r9 g$ j
                               break;  //TODO: add this
& t9 Z- I$ c- i1 t      
" w1 b' h  X  U4 ?6 |                               //Feed per Minute Mode. ^4 t9 \& L0 O; t+ Q, W# m
                               case 94:
6 L4 {" f- d5 D& p1 D" E       - j4 m  T, r7 ^8 n
                               break;  //TODO: add this( A0 \8 V$ X+ T! N# |- R
       */8 V7 A5 [; u3 o( L9 V
2 u) p. Z; M4 [
    default:
3 x( S" p3 L- j& r# [: a      SerialUSB.print("huh? G");
* G1 R7 \! C% i& S) u; Q* L      SerialUSB.println(code,DEC);
2 W9 n: A; L- T2 B4 V/ z    }
) q  }; \2 V, t% F& M2 Z# [2 \/ j6 a  }' _8 I8 \3 i0 J- e3 W

8 T* L% j. X6 A0 E. S" a9 l  //find us an m code.3 O* c1 V. m! r! _8 Y! C
  if (has_command('M', instruction, size))
* i) y% B+ m# n6 C8 _6 ~# l  {
% O5 D: a: n4 i4 w' _    code = search_string('M', instruction, size);: ^5 x. e9 t, I: p, h( Q
    switch (code)4 S* V& e- F) M( z$ ]5 z
    {
) P& Y% u, m3 d+ ?9 C! ^) C      //TODO: this is a bug because search_string returns 0.  gotta fix that.4 m; Y) D1 V) I
    case 0:
+ D8 X0 N7 C' [1 c; {. w; }3 S      true;
. h$ r6 ~8 R1 p: R! K      break;
9 ~6 a. d/ p% D: [" |0 d; P5 z
8 u, }: l( A5 D- _( f; y    case 100:
7 h; \2 }- i* \4 [5 c1 K" m      break;
) i' r; n9 z. ?: H  z' q8 e
8 |4 m3 a  n0 T/ g5 G: |      // fire camera relay
. H) j5 q( h1 ~4 g1 H    case 101:+ \2 _  R; X1 Z
      camera_shutter1();1 D: x, }0 z3 C* b
      break;2 q- f2 E' T$ E) ^
8 `( n4 p9 N( E
      // fire camera relay29 U" c9 @: D4 ~( h- @3 Y
    case 102:2 b' u' U  E, h" M' I
      camera_shutter2();
! n; t! d+ ]1 }* n9 Z& M" |      break;& I2 n+ M# W. i
/*
1 j& d; y7 x' `" k      // turn aux 1 relay on
; i3 `4 b0 _- g( A    case 103:% a8 s+ h4 f0 b% f
      camera_aux1_on();- b* D  E# Y& K0 v2 l( z$ P: _
      break;: E1 G8 L; D  e" W
% L4 Z- e  T+ x, `5 T
      // turn aux 1 relay off
+ w( x8 q+ l+ i' L  c8 @$ S    case 104:
- V; I" x' s1 b, M5 N5 \" b; \      camera_aux1_off();, W! v3 k4 f& [* F9 y3 j; {
      break;& x/ C8 {6 z! Q$ p
7 g* L4 i8 j0 N4 K4 A! Y" N" Q
      // turn aux 2 relay on
0 [9 I% y" n6 K) ]% r( r    case 105:+ G3 _8 I6 f- h
      camera_aux2_on();! F6 P7 B- n9 Z3 N- H
      break;
# H6 k% ]6 G/ ?- ?7 [, j4 l4 _# W5 L  O* U# I2 ^
      // turn aux 2 relay off
6 w0 A% {8 z* K( G3 Z3 j    case 106:
" f4 u7 D% }0 Z* O      camera_aux2_off();
' A1 V" p$ ~- Z# ]  r' C      break;/ b. G: [4 D. r" @  H
7 B1 O. l! p# v; H' E2 O5 P, r
      // turn aux 3 relay on
' q3 `; d* H9 M) B4 P    case 107:
1 n! ?* t& D. f+ b- T7 F1 H      camera_aux3_on();6 ^* q) ?( E$ I7 _1 [8 A$ o
      break;
% `: t9 D: `3 o8 V, R5 R8 i# s5 }, l: p  z6 A4 h' K
      // turn aux 3 relay off  c: g9 `7 ^. w. p! {
    case 108:: B  t( f7 P* X: C$ Q" e
      camera_aux3_off();
( d- ~" w$ `% d      break;
2 g. `8 c3 `3 h1 R. p0 ^2 j7 C+ Q
3 ?' b3 c! N" H' N$ h/ O  {9 N      // turn aux 4 relay on- v3 _- q# ]! p1 C
    case 109:0 C% w+ L2 e5 r$ R$ o7 l$ N
      camera_aux4_on();
* b6 s+ \+ p/ V! L  C      break;7 ~6 [" l: w0 d
- V& Y7 O0 w5 E) M! y. a
      // turn aux 4 relay off
! G% r5 C- ]1 h, \5 V    case 110:
5 K- I# f, M+ w0 i5 J7 @2 i      camera_aux4_off();
2 m. S) e; S# A8 L6 z& @$ L' f      break;
. v1 u7 R" J) [9 k*/0 W! I- j) W1 _! b% r- {
    default:
3 w3 u* B6 M2 B1 T6 g, f+ W6 ]7 f% d) o
      SerialUSB.print("Huh? M");& M' ^0 J4 D3 o/ ^9 U+ E4 o9 O
      SerialUSB.println(code);
/ F; m$ }$ j9 m# d# }    }
. Z1 w# f% Y) o9 \- a  }& J6 n1 ~/ z1 B5 E& i0 v
1 T* e! }& B& D5 y4 ]
  //tell our host we're done.+ e; c8 h# O! o) D4 A
  SerialUSB.print(byte(78));
( I" x. R" F4 f
2 s% L: e3 J2 a. P/ r& t) X1 X# O}! O' _' b1 k8 @" }* j2 P" _

3 x8 n# ^9 [  q//look for the number that appears after the char key and return it
6 }3 n: W  o) m, e7 m' y' Ydouble search_string(char key, char instruction[], int string_size). i2 p  T) K+ B# ^
{
! Q  L8 E' p% C8 P9 t. q$ N% I  char temp[10] = "         ";8 ]- I; p9 G0 `. u& G
  for (byte i=0; i<string_size; i++)
; l# ~/ p" G4 b  {
1 _- T* f8 }" K( V8 A; n    if (instruction[i] == key)
$ i# d- C5 N4 U    {
, }4 [" a* f2 [0 b4 X# k      i++;      
0 Q* S5 n% @9 H# V: [      int k = 0;
& Q# a+ f9 O) p* h      while (i < string_size && k < 10)% x: a3 q5 E6 M+ L6 j1 Q; y
      {
& p/ E* t0 f4 k# _! b        if (instruction[i] == 0 || instruction[i] == ' ')
2 `4 {- M) c1 I# G- \          break;  ]2 R) d  M1 z9 Y, j
+ P+ J$ Y$ \; m8 q, o8 U& M
        temp[k] = instruction[i];! G) h1 O( U/ Q6 X4 F4 ^3 b8 }
        i++;/ B* z+ t5 b2 t$ D$ p) H! H
        k++;
; t5 C0 i+ b& M% o# p% d" H      }  _# q( |, U+ n/ _. \
      return strtod(temp, NULL);5 w  P* V# P( Z( t) H* e: a1 ]
    }
+ w( O! h" P1 T- ], K0 o  }
  `$ ^2 K  k& h# e- P0 k6 d/ A$ A& r
  return 0;( }1 Q8 ?  X6 U+ v4 n/ V
}8 |9 k" _: a% P$ Y! n! X# }
1 I( G+ L1 w0 F! \4 m
//look for the command if it exists.4 B5 C' ~" R, s0 I
bool has_command(char key, char instruction[], int string_size)
$ N9 s- w% y, L- [! a{
. ?+ {: h+ n9 k( y# N: z9 u9 p  for (byte i=0; i<string_size; i++)
& ^7 `' Y: ]2 f7 Y& b- V3 x  {, F& `$ X  C3 I. C& p/ S/ E
    if (instruction[i] == key){
- l0 ~, }7 U" [5 p: \" G- w
" u* l  C- r: }1 A: Q      return true;
0 K4 q1 ]% G# M. D    }5 P, i! o3 Z: t& G# v% _
  }6 N; A0 D1 p8 y& W1 D. k% N0 B& x
* _, K# U) S$ ^8 x- p
  return false;: r7 n1 @! @. ^' {7 p
}
) X; l4 s" O% I+ s8 H( `
  l9 I8 n( m8 n) H" e; t. ~2 M% ?6 h: J/ B7 X' r# W7 g
& ]* l7 Q7 p8 P# t, y5 e: Y# \
回复 支持 反对

使用道具 举报

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

9 B! x" P& ^, d( J1 v//init our variables# y# x- R0 V, e. [" B
long max_delta;
& y4 Y. R3 ^" e& P6 Klong x_counter;
) }2 K8 J4 ?. Y/ glong y_counter;' z2 x# V( h# M$ P! a
long z_counter;
' k. p# Z+ z4 l* B5 Qlong u_counter;  E& m3 W; V  F; q$ E; X, ^. J
long x_pos = 0; // x position in terms of absoloute motor stepps
9 w( s9 F! z. m5 s. z) Vlong y_pos = 0; // y position in terms of absoloute motor stepps
. T+ ^* r' `# Klong z_pos = 0; // z position in terms of absoloute motor stepps' U' o1 U6 v0 f+ z$ h
long u_pos = 0; // U position in terms of absoloute motor stepps
; r8 ]5 `9 n$ C1 w3 D4 A" B; U) b% M5 Y2 K8 y9 _$ ^/ g
bool x_can_step;
8 @4 H) l5 l( u2 `, X, G* Mbool y_can_step;
% D& u$ O4 h5 S4 B" i# i- wbool z_can_step;
2 `2 I% d0 M0 \0 c- r1 e  }bool u_can_step;
: d' w' T; h& i2 _8 Z5 S1 tint milli_delay;
& Z, O1 V: B5 E
2 d6 h* {( D4 V' ?2 lvoid init_steppers()& t8 A8 x7 e# a$ I" k9 a
{
/ g' A* E/ `- }, e9 N) F1 m- q  //turn them off to start.' ]3 d3 a2 {* P* t6 m
  disable_steppers();
5 h# Y; X& v" V/ h" ~2 b/ m  O1 Q- H7 j1 }
  //init our points.
( L2 K8 }9 D. h# o" c* `  current_units.x = 0.0;" _& Z( t4 Y) C$ B  L+ A' w/ W
  current_units.y = 0.0;9 K; k" N, p$ ~1 E. z: u9 N
  current_units.z = 0.0;
. D2 N' C# v1 X# W+ G" ~  current_units.u = 0.0;: M- U5 {& o8 w
  target_units.x = 0.0;
  a' [3 Z4 l1 A: t  target_units.y = 0.0;2 D4 P  l( F5 L6 w" A8 f
  target_units.z = 0.0;9 C' W- W. \, X8 R8 C4 s
  target_units.u = 0.0;
# W  K0 r$ F: X  6 z% [$ m6 L. G2 Z" E

2 d8 M+ E( r5 `$ r/ [3 I) S( M  pinMode(X_STEP_PIN, OUTPUT);
$ d0 I- U; t0 G2 X0 v% b- C: J  pinMode(X_DIR_PIN, OUTPUT);
/ M6 v$ `7 e6 G, r( ?; e  pinMode(X_ENABLE_PIN, OUTPUT);
7 q: ?# {( i. U! {# G& ^% Y/ ]  pinMode(X_MIN_PIN, INPUT);
! J( u- {5 D% ?, H" {  pinMode(X_MAX_PIN, INPUT);3 W7 t+ \3 K: v: g
/ X4 N# p/ G1 [2 I8 E7 W3 Y
  pinMode(Y_STEP_PIN, OUTPUT);
7 G' H0 s$ E) \9 s- r' {+ m7 p$ r) P4 g  pinMode(Y_DIR_PIN, OUTPUT);* g1 p. k% _; S4 d0 g. ~4 |  o
  pinMode(Y_ENABLE_PIN, OUTPUT);
2 d- L9 p  |* ?! g+ ^5 {  pinMode(Y_MIN_PIN, INPUT);: x+ j7 M$ Q" `. u8 {+ w; i
  pinMode(Y_MAX_PIN, INPUT);
5 L8 g! X( j5 C4 \2 _4 X' h$ Z2 D3 c5 W$ J
  pinMode(Z_STEP_PIN, OUTPUT);
  i) r  T& C; W: F7 w  pinMode(Z_DIR_PIN, OUTPUT);
# x) I/ g2 W6 C+ m  R  pinMode(Z_ENABLE_PIN, OUTPUT);
. Y3 R8 y* B- u9 x1 a  S3 J  pinMode(Z_MIN_PIN, INPUT);) c' ]' s. u$ C$ T6 _
  pinMode(Z_MAX_PIN, INPUT);2 R8 S* g- S! d% j: e9 k' W% e

4 t6 ^7 Z7 e4 {- E& L8 l/ j  pinMode(U_STEP_PIN, OUTPUT);
2 M" n" `: ?3 ]  @+ s: V/ `5 ?  h  pinMode(U_DIR_PIN, OUTPUT);0 w. `- [: L& q- q3 |
  pinMode(U_ENABLE_PIN, OUTPUT);) T% O8 i, H% H' v5 y, R1 H
  pinMode(U_MIN_PIN, INPUT);
2 ]6 T+ Q  H% V: [/ g' Z. R  pinMode(U_MAX_PIN, INPUT);6 s8 X4 W5 i; g$ X8 t
  //figure our stuff.
( {) G/ R; K6 V) |  calculate_deltas();
7 z1 z8 t1 j% [; S1 h6 H% m9 M/ M}
% _) A0 I/ n7 ?) _# O
& F2 P8 V0 X- m, G; Lvoid dda_move(long micro_delay)/ M+ g$ \- c/ d: @+ ^/ Q
{0 }- q8 j' o$ d# A3 p  x+ v
  //enable our steppers
" M4 e$ u( P3 \0 K- m% F& j: ]  digitalWrite(X_ENABLE_PIN, HIGH);
0 p. m4 W4 a9 U* i% Y3 d  digitalWrite(Y_ENABLE_PIN, HIGH);4 n2 l; H- r, B. P, h/ A/ b
  digitalWrite(Z_ENABLE_PIN, HIGH);
; t  t; B' E/ ^1 Q( j5 O- I  digitalWrite(U_ENABLE_PIN, HIGH);
/ V6 t# F7 q$ L! S5 d4 Y  //figure out our deltas
" a& p7 F2 c2 r1 S0 n( S  max_delta = max(delta_steps.x, delta_steps.y);
1 ?+ r/ r0 B9 I0 Q% n  max_delta = max(delta_steps.z, max_delta);
9 H$ ~% J& G& ^* W  max_delta = max(delta_steps.u, max_delta);7 I1 ~2 m* k; @! P
  //init stuff.
; J; S5 U0 ^7 L& C! l$ ^  long x_counter = -max_delta/2;8 B$ l. S+ J  p
  long y_counter = -max_delta/2;
; b3 K. }. h- p4 K1 n4 v  long z_counter = -max_delta/2;
- K" e4 N# z2 ~; z* E+ {  long u_counter = -max_delta/2;% D5 h* {1 d( q6 t* [8 @

) \1 S5 S. M1 W  //our step flags
/ z# h3 c/ a) r. {- t  bool x_can_step = 0;
: m" S; y8 r! _3 z1 h  bool y_can_step = 0;, T! I/ Y0 Q1 s# V: o
  bool z_can_step = 0;
  l- s' ^- K, K; Z6 T6 L. C  bool u_can_step = 0;: [4 e: _# @9 N, D  _
9 N8 l4 ^! e6 d5 {$ |- `8 S! h
  if (micro_delay >= 16383)0 m2 g% V4 B0 P4 ^
    milli_delay = micro_delay / 1000;$ ~3 @  d6 ]" |, A+ |; |: I& }+ T
  else
- I2 F+ v5 S  R) G2 v( Y1 P3 @* S    milli_delay = 0;6 S$ p& m6 p3 G# {
2 R. K7 W/ v  r. d6 M

( c9 T6 i6 ]2 u, n1 y  //do our DDA line!
0 F' T3 R: H4 A. w# o
1 t' z, D7 u% C$ @! u  do
5 `7 ?8 |9 S+ E+ E  {
0 s6 V4 q# E! o3 A$ Q8 E    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;) b" r( T+ n5 |
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
3 d: N5 {  W2 C* W: v    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);
) ~' {% Z& t& e! y4 q9 X+ f    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);5 k3 X# k/ f  `: B+ F* W7 [; @
    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);1 }3 S6 S9 H* w2 ~3 k: ^0 X
    if (x_can_step)
' ]% P6 ^. R* C* i8 d. `7 M    {
! X* y9 f$ M2 \3 Q; z$ R      x_counter += delta_steps.x;
  r, ~5 L5 A; l/ A* t
2 I+ F/ q, H0 J  c2 i7 m0 T      if (x_counter > 0)
3 ?3 E9 |3 `8 q& |( i      {; O# u" z( E+ }- p3 q$ g. A. R; ~$ x
        do_step(X_STEP_PIN);' L9 C6 I) z1 d. @5 \! @3 k
        x_counter -= max_delta;  [& K4 v* N, F- h5 m+ h: V' }; @5 ]
        if (x_direction)1 w% ~+ c* o- @( H+ N
         { current_steps.x++; x_pos++; }
' m$ {1 {9 {0 m; a          % L7 c, u9 e; |  }+ g
        else' K- Z! T! b9 G
          { current_steps.x--; x_pos--; }
9 V0 N& E" |6 z8 `1 `6 \$ |         
9 |' p. A$ s0 q( B  V. @      }% D+ \, g' l+ I8 s- t; U, r
    }3 h* y( ]2 m, N: Y: v
    if (y_can_step)3 n! o7 `9 M( a
    {' D$ ~! F' ^% A# O
      y_counter += delta_steps.y;
" w! ^7 a  R1 I- A
/ E4 W" N- P8 S0 J# t3 |$ v      if (y_counter > 0)( z) w$ K$ j6 i2 C
      {! n4 M/ A& d3 R* g. r- g
        do_step(Y_STEP_PIN);
" A) a6 ^7 v1 _4 t! T# d$ |6 F* o        y_counter -= max_delta;3 V9 S- v* ?5 z: P9 d5 N) @
$ \" M/ q; I% }3 Y3 g& `2 {! ^
        if (y_direction)" i* i6 k8 `8 [! O! g% q' O4 S
        { current_steps.y++; y_pos++; }$ C& i! e6 o0 A% t
       : E% G* ?3 Z6 a" u3 l
        else
! \, R. L% W; p# Y6 }        { current_steps.y--; y_pos--; }+ q8 G7 m& [! n) Z, T; U
          I, q5 f8 k8 P2 p$ C! m! v+ ?4 {2 O
      }! a) |; J* R) }9 X: k) t
    }
6 R% L; ^: |  X* K: W# H. q4 d# L: O! Y. ?0 y: m
    if (z_can_step)/ C% Q$ r: K* e) l7 s8 u
    {! o$ V2 N* W4 P7 d0 x! i" U
      z_counter += delta_steps.z;# G. ^* p8 i, M5 G/ m6 s
) O' C) u7 G% y- L$ n% V! q
      if (z_counter > 0)
* D# k# N; W& U7 u7 g: P      {' s5 p' X  Y; y4 `
        do_step(Z_STEP_PIN);6 Q, W$ a3 M' x' p1 ]. h1 i7 D( j
        z_counter -= max_delta;1 e1 z+ O: L' @" h

/ Z$ W) C$ {' \" z8 [5 Z        if (z_direction)( V+ d2 |  u: A& h( i; j2 N  v9 e
        { current_steps.z++; z_pos++; }
& {7 |" X/ w7 g" g4 b        9 r7 Y# Y6 g8 A! i" `1 z
        else; d# @6 D# P: H( ^# y9 ^. g+ v1 b
        { current_steps.z--; z_pos--; }' o, `$ a. S1 A9 v' K
        
4 d9 V! l; ~9 t% h      }
: Y/ X2 T* n' W! ]% j' L    }& ?2 q3 B: w- M1 ?% r/ [
    0 X: }# x# J5 W- ~7 |+ d& }
    if (u_can_step)% N6 U( H% C9 T" i0 U- b; s6 `
    {6 J5 F" y# j1 |
      u_counter += delta_steps.u;
# e3 U7 u2 t3 T: E
8 t2 a9 s: I; P  }( [" ]* L      if (u_counter > 0)8 s7 O/ n9 D" a
      {
% j( E+ g8 d  L, v6 h* E        do_step(U_STEP_PIN);$ B) |$ s% {, F* v) H- w4 K4 d
        u_counter -= max_delta;1 f$ N) N. ?1 {. l
4 p" ]( ^3 M- ?" H# o& s: S; h
        if (u_direction)
8 j' z/ @9 d  x- c7 r5 J        { current_steps.u++; u_pos++; }+ ]$ }% p/ h6 I+ G! t- K; l% [$ U
          * B3 K) |# O# B
        else
, ~5 g0 l0 B5 E          { current_steps.u--; u_pos--; }2 `4 E3 x( K( P, L& z' C8 y) O! c
         % z$ S# a9 V7 }- {6 D1 D7 V
      }
" T2 k6 E1 m2 H6 |    }
9 c6 n2 ?' q* B! X8 Z    //wait for next step.# l' T' O) ^: R6 J1 ^/ v
    if (milli_delay > 0){
) d+ o2 o. N5 b+ w      //if (digitalRead(BUTTON_SP_EN)) SPEEN();8 s# i5 @2 L6 T6 q2 r
      delay(milli_delay);
$ P/ j4 f8 H! f* y" @    }                % s4 Q) n# K, d
    else{
- N# U3 c6 M6 Q# l! g      //if (digitalRead(BUTTON_SP_EN)) SPEEN();/ t1 I: y8 r3 J: |
      if(micro_delay>0)delayMicroseconds(micro_delay);
, z- }3 Q( D8 T6 k- I    }
: U$ B! F/ K8 A& Y7 V. U; W    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
" G- C7 D4 V$ ^+ J  }9 g2 O# q- i, A# h
  while (x_can_step || y_can_step || z_can_step || u_can_step);8 f' `$ e, I' B, P
6 x6 Y7 q; J. |: F8 K/ @9 C+ w' R5 r
# d" c5 Z- v. ^
  //set our points to be the same3 y7 |0 R7 E' J4 @* Y- ?
  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
" f  X; l$ V* u' D+ x; T  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;9 N8 W- p" n; r+ }! b
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;
8 I  `2 P" h$ J( Q: h. p0 X, Y  L  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
  t& S& F- k4 V# m  1 ?# p0 P) f% O3 o2 a
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );7 `" l% e) H9 Z) o  x

1 @" P$ {2 h3 G  long x_pos = 0; // x position in terms of absoloute motor stepps
) f& Q1 _; L6 @( K/ O" Q& D  g. E  long y_pos = 0; // y position in terms of absoloute motor stepps$ ~5 w& i8 Q2 `" j" x
  long z_pos = 0; // z position in terms of absoloute motor stepps7 w2 @! t$ f( K, K) Q
  long u_pos = 0; // u position in terms of absoloute motor stepps( a) h& G0 @" }5 Z4 ]9 H' C
  calculate_deltas();& Q( ^+ d) M' S
    N+ Q' u: o# H
}6 K, O8 }  h# }  j
& |! U5 x; q# O  r
bool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)0 z7 H7 r) v, A! b5 l
{3 M) Q& v1 O# `  ]
  //stop us if we're on target% B6 ~5 m) \* q' E1 `0 ?4 F
  if (target == current)! Y% X: `/ X4 d0 `+ Y
    return false;
1 p( I" n, u2 D( S* X/ L  //stop us if we're at home and still going
2 x, C' D' q& g, t0 ]3 o- K9 l8 [  else if (read_switch(min_pin) && !direction)
3 x) ~  q) t  J- Y1 {' |, Z    return false;# A# `5 A# A' @& @2 G
  //stop us if we're at max and still going. @+ b! w7 r8 O: C
  else if (read_switch(max_pin) && direction)
' u" M8 B' Q+ _) b    return false;
9 p" {7 r0 @4 Z
* ?0 t5 O- U* O4 k& i  //default to being able to step
# [& B$ F' M3 w: v  return true;
' g- ?- D# r7 ]( J5 Y}7 G4 u& B4 p! x" A, G

" [$ s" E* p2 G* `; |void do_step(byte step_pin)
3 y: `1 b' K. ~' }7 ~. U{
3 C% b3 d& ~3 j9 E$ a3 u  digitalWrite(step_pin, HIGH);: P5 ]+ l+ ]+ \! U* Q8 j
  //delayMicroseconds(1);6 z) }+ V, A/ u6 @4 d$ J
  digitalWrite(step_pin, LOW);2 {: a+ t* U( w+ G  A
}
% k' p) Z3 m; S- B7 n0 a. x8 f
3 O7 S( S- P/ B, |bool read_switch(byte pin)) ?; C; _4 C/ b' ^* a5 u
{
; u! K6 m2 b  k  //dual read as crude debounce
+ G" V# j' l  D
; R3 i) B! y# }6 ^' C  if ( SENSORS_INVERTING )" h: p$ u6 R8 v3 c  f; W) O: i; t
    return !digitalRead(pin) && !digitalRead(pin);
+ P- B' K! n9 `) \8 A  else
; u" S# _) x" z' Q  w    return digitalRead(pin) && digitalRead(pin);
$ {4 h: m2 y+ P! A) `}/ Y; E. P" g' c6 O( r" s+ T

- i+ P" L& M, |& J; P8 Tlong to_steps(float steps_per_unit, float units)2 D/ O# [& |3 `# E+ B
{3 Y+ D' u0 X, O* f) S$ I  J
  return steps_per_unit * units;4 l- v9 Y8 b* ]& v- \8 ~, i, @
}
2 [' u6 T5 ]5 Q$ w
! t9 A, m- p/ G2 nvoid set_target(float x, float y, float z, float u)
1 ^  V) Y$ I2 c6 Z2 x{0 f; X( f3 c. p" e
  target_units.x = x;
0 S! Y+ u2 s, C  target_units.y = y;
: x2 i5 x& L8 w) V  target_units.z = z;
7 [' X" U) ^) d) _% v2 Q' _  ?  target_units.u = u;6 g$ L- C1 P; w6 ~  P
  calculate_deltas();5 N7 m5 m1 `1 @6 f2 E1 Y
}8 |1 L, v/ V9 L: p& y/ u$ V5 E" ?
) ^8 G) I5 f" m( }3 E. ^1 ]) I) |
void set_position(float x, float y, float z, float u)
9 Z$ I4 h# v- R4 t& _{
, g8 d3 U) u% d( D0 \: f  current_units.x = x;5 U  R2 j  g1 b! b% @
  current_units.y = y;6 W9 B+ B% G, A; g; A2 {# g5 f6 J2 ~
  current_units.z = z;
! }" B) e( `; u2 [! z  current_units.u = u;2 W1 d' v3 M7 k' u' k# p' S
  calculate_deltas();- j* \* n$ z( H% k
}
' J# c) n; o8 e, g% W& e3 J+ m/ ~
/ D0 P, I! r( c- D. R2 ^void calculate_deltas()) ]' s5 `# ~1 z7 I8 f/ ]
{
" h& C* X9 C( R8 m* S2 [  //figure our deltas.
: H8 y' ?; a$ Y9 Q9 n2 ]  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
7 p* ^0 ^, r' h7 o, Y' y/ E  ~7 O  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);: Q- ^4 t) w; q4 z! x0 w
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
) V4 e! j1 h; b5 t/ @  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);! ~2 J/ D( P/ Q3 T9 K
, E* u) y6 ^3 r* h' p' ]) @% S/ m
  //set our steps current, target, and delta
+ D9 O7 \4 a9 M' l: d- c% ^3 O  current_steps.x = to_steps(x_units, current_units.x);
9 I+ f# ^' L' Y* ]' s: O0 B  current_steps.y = to_steps(y_units, current_units.y);
! v( Y) D4 @: S. F# U# |  current_steps.z = to_steps(z_units, current_units.z);; h7 c; d* Q  n* z# H0 g
  current_steps.u = to_steps(u_units, current_units.u);
! O( |& h2 a8 Z& P% i  Z
- G; @- `& y- k( |" L; q3 n: O  target_steps.x = to_steps(x_units, target_units.x);
. U; |. u/ I  w( y. N! Q- L+ y  target_steps.y = to_steps(y_units, target_units.y);3 K3 i/ P. W3 h3 g$ F8 ^$ D% s- C
  target_steps.z = to_steps(z_units, target_units.z);
$ Z1 x5 K. E" ?' U9 W  target_steps.u = to_steps(u_units, target_units.u);
" n! V. D/ x/ W5 J$ P) D" T0 T$ f+ v' K5 b) M# w5 L$ \
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);, o. r* }3 O; N
  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
, H* j& Z* I8 G8 j  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
5 I7 T! G% |5 V. D$ Y  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);
& R/ w4 V  _* t1 }: X+ e7 h- L
/ @# ^, }  H2 G7 r1 P- u
* M! k4 N1 c5 |* m% r. c) _" Z! A* E( Q2 X
  //what is our direction
2 s8 y% Q  H; {# q" ^  x_direction = (target_units.x >= current_units.x);& v# N6 y& [( u9 z
  y_direction = (target_units.y >= current_units.y);
* o* l9 R9 s# \  z_direction = (target_units.z >= current_units.z);. |! `2 D. \9 M' |% i2 K7 w3 w
  u_direction = (target_units.u >= current_units.u);
. x5 C/ ]; n1 ~
- f5 e, `  s1 X  z' J8 _  //set our direction pins as well
4 o- N! n8 Y+ j. h9 K, R/ {8 L4 r  digitalWrite(X_DIR_PIN,x_direction);8 `4 @9 Z; q7 ]. T- V
  digitalWrite(Y_DIR_PIN,y_direction);
' H) n7 T! N7 i: b3 P8 S% O# w# _  digitalWrite(Z_DIR_PIN,z_direction);
1 v. v, Y6 p' n/ a3 S: A  digitalWrite(U_DIR_PIN,u_direction);
, h) s' h, l' V0 C' Z2 y
2 _# t& l2 P' F# F! ~  //绘制LCD  ^0 ]+ j7 L* ?# \5 l- n* ^: z8 }
  LCD_DRAW();2 W. d6 w! X9 q- C+ o& _( w/ ^, K

+ k5 k+ M8 P+ S! t. O& E5 o}
7 x# ]4 L& [1 `" s7 y' U. v- q
) T4 f0 Z: \, T
' K, I2 `% q- r1 k" S4 U9 \8 _- |long calculate_feedrate_delay(float feedrate)
7 J3 \: y/ Q4 G( w9 C. E& Z{! l: ~4 d5 a1 L+ V, P2 S! m* P
  //how long is our line length?) ]" m* d/ z, g, p; d  C% ~# C
  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);
5 i; [% e: w, W7 o; @  long master_steps = 0;6 t  n8 r2 n# j5 o6 F
/ I) W/ l/ O. y
master_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;% `! @9 s$ U% T( A
master_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
4 I' h1 l) [' ~' P# A. a: xmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
$ N2 `0 ]7 z' ]: y. K5 S6 k
) Y  X8 L: f  u6 x( G
  L, C, V0 C5 ~3 D6 I. O$ S, j9 ~6 @8 L. I; a) m% U

. |! N! p$ k* p$ d3 [0 ~# S  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.3 N1 n1 f8 W( l- A
  //the formula has been condensed to save space.  here it is in english:
3 L2 C" Z9 F( u) ~; r  // distance / feedrate * 60000000.0 = move duration in microseconds
4 o2 ^1 ?; d0 ?' a, t( d  // move duration / master_steps = time between steps for master axis.
+ u' V0 O" _) {9 G5 G% M8 H% b return ((distance * 6000000.0) / feedrate) / master_steps;
; l  H/ J9 H  [
; A4 I2 J" ~7 T4 K5 B9 T}
& O' u, u" [1 C' ^8 ^& ]0 \& \3 y
; i" L( @0 c2 \: I/ }) }* olong getMaxSpeed()
! N* @- y5 u& s3 L0 K2 M9 e+ }{
1 s6 V/ L) d% R' g% k) r& o% @ if (delta_steps.z > 0 || delta_steps.u > 0 )
3 z: q9 g5 ^# V, b    return calculate_feedrate_delay(FAST_Z_FEEDRATE);5 v; _. M& d, g# V; X
  else9 K, m. L  U0 a8 m7 O( _6 m+ ^, S
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);# b% m% j, T. Z8 v4 l7 d
}& z: P* {% ^. U; R' z1 t/ P' _% R0 O

% \6 x: w/ i6 R+ B& F6 @void disable_steppers()1 y- A0 C' m) v1 P7 \
{
; k+ f, H" y! X' A# e2 R' w' R  //enable our steppers
2 w- ?7 z4 F2 v& V$ b5 k  W1 o! V  digitalWrite(X_ENABLE_PIN, LOW);
6 i' k1 o: `& ~: j9 K  digitalWrite(Y_ENABLE_PIN, LOW);
) M! n: g5 W8 O4 j  digitalWrite(Z_ENABLE_PIN, LOW);: P# u! ?- [) O* c) s' Y- N: B
  digitalWrite(U_ENABLE_PIN, LOW);7 B! e# |5 T; [& K) ~
}
7 \1 N$ t/ t$ }6 e
) y6 f2 \2 Y1 a+ M* `2 i+ x
* u/ q( ^' F+ Y  I4 @! {//绘制LCD- E% p# T5 _  [8 B$ \- G
//unsigned int DRAWCount=0; # @2 b5 i1 B2 i
void LCD_DRAW()
  J3 y) T* L( l4 o/ m{
0 \7 C6 u, I: p- P1 q# p+ b5 f    lcd.clear();
2 Z9 c9 K$ d- Q( o; W- g    lcd.setCursor(0, 0);5 B  h+ p6 t  M$ L! R8 n
    lcd.print("    X=");( r% ?( ?$ n0 D4 Q# @" q* `5 _+ j
    lcd.print(current_units.x);1 p1 u, n: s7 I2 j! a7 [0 o
    lcd.setCursor(0, 1);- k/ Z; W1 R1 c5 Q, L/ ~4 e1 x
    lcd.print("    Y=");
  k# Q, ?' \  ^  Z/ A) r1 z    lcd.print(current_units.y);7 ], p6 j- j) H* S
    lcd.setCursor(0, 2);
4 b& A2 P8 C% t3 D    lcd.print("Z=");$ [3 _% ~' N. K1 \. O
    lcd.print(current_units.z);' Z1 o8 E" d: J% Z) t7 Y& g# p' O
    lcd.setCursor(0, 3);! K0 l; A+ }8 T# I$ v
    lcd.print("U=");
1 N& H6 j; R' j+ T* a# P    lcd.print(current_units.u);
" f" j; N' t# s8 c# l% F  }
- i+ [4 F7 q# f // else if (DRAWCount>=30)DRAWCount=0;
( P$ @) |# L" \& V2 n//  else DRAWCount++;
' i8 \, z6 D+ V6 [! i  l' B# r void SPEEN()
. R& N- [: M! ^1 r  J% c {
0 {- X' B, s& [+ b; n   delayMicroseconds(analogRead(SPEEN_CTL)+1);
9 X8 K4 K5 i/ v; M- n5 | }
3 S+ W: w4 Z9 Q8 f( L! L# f) i2 c7 x

0 M3 ]: ]& L8 y* ` //delayMicroseconds(analogRead(SPEEN_CTL)+1);8 P5 B  o8 K! j" j( r4 }* g4 y
//if (digitalRead(BUTTON_SP_EN)) SPEEN();
( `0 g* y3 T& F
! g# E4 f& X7 S* p# ~5 X
4 \+ R" i1 M0 V2 S) [& a
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:58:04 | 显示全部楼层
基本原代码的大概是这样,我是断断续续捣鼓的,玩到最后版的,后面我会找出来先测试下过,再贴上:)
回复 支持 反对

使用道具 举报

发表于 2014-5-10 10:48:17 | 显示全部楼层
请楼主发一份给我,感谢!178354773@qq.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2024-5-4 23:56 , Processed in 0.062403 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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