机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 23592|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑 $ X+ A' p$ P( ?" F

2 z( T1 ~2 |3 p; r0 a+ l: t首先声明7 `0 b0 ?- A& ?) n  J( E3 k- N
我不是专家,业余捣鼓7 _, ~" p. b2 ?8 X0 X! I* v
源代码首先来自网络
/ R+ v  e# m$ f6 z9 |3 g" Z& G开发平台Arduino及Maple
( @/ x" i& E" ]% ]7 Hhttp://www.arduino.cc/9 B; X8 R& f3 f" q
http://leaflabs.com/devices/8 A: L8 D5 J3 A4 l1 d9 v$ j
国内活跃社区
6 Y  E" i" v) x$ {& r( z  Lhttp://www.geek-workshop.com/forum.php 5 `+ v' h* Z- L
4 s3 c* t+ N; E9 q2 d8 w5 E
竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验
+ Q# c% {' }/ p! h4 Z$ E5 L3 R来入门了解熟悉思路架构等
! ?6 m  y4 q9 ~! x4 {/ H- @1 ^, L' `! s3 i2 I9 J
我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,
; v% q1 d; k+ W' {+ w许多功能还木完成,不过作为低档次得应用可能还有可能4 w6 f# f6 G& V' A" Y+ z4 ]
9 @4 C& l3 I; O' K/ z% f, j
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧" v2 F0 V, Z6 p
% c+ R, H' w8 U0 D9 R
拒绝 所有的求
( o4 z# N; B# P" E求人不如求自己 不然木玩4 k4 D9 U% M" N
2 P& w$ x7 V6 v! F  X0 u0 L1 K
高手绕道 谢谢!' g5 _3 w$ o. ?) d

9 U" K: c8 \' {, m5 W4 Y0 o
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 8 s, H9 O2 l2 {
4 e3 h, }: \" Q
GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东
2 {+ {* F% C" H7 Y, Y1 v贴代码先5 O) _' T3 I# s6 M) B$ M1 U
直接Maple的,某宝许多超便宜哈
( ~9 U5 Q5 e1 b晕,我怎么上不了RAR?
$ G- X. v6 n# p  c想玩的留下 e妹吧
7 `0 g  H9 R, j8 X5 v. P/ I, m" I第一个妹麻烦传第二个妹哈
% q' j$ D4 q) L. g我平常杂事多2 u, G' K! S$ Q: h  h
谁放网盘上再,麻烦开放下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap
, e$ B6 T1 N1 o2 U  c; D$ g// v1.0 by Mike Ellery - initial software (mellery@gmail.com)( z4 |0 E) l/ ~
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com); w. k2 ^# C/ s* x3 l3 ?0 j, G, j
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)6 E2 V8 ?1 y" ?5 d* p* e
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)4 Y/ u- D* B, o4 g7 g: H5 T- P
3 N- s$ H1 A) h
// Arduino G-code Interpreter for Macro / Micro photography
% K+ [  y* Q4 M9 i3 ]* y// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
9 ]5 K& j$ V& J) j//modified by YaoHan China 2010.2.15% K' G: ~$ i# @# m
//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)
6 k/ c& I, q8 T/ t( i& z% n- r% B1 w#include <stdlib.h>, Y" ?+ G8 R& k+ A: |9 e
#include <LiquidCrystal.h>
4 ]( X$ w% p0 i  e' D
7 [1 W. a# X; `& U( N5 ?//启动(高电平有效)/暂停(低电平有效)  默认高电平
% b: U+ A% k# j0 h3 _: t#define BUTTON_CTL 35/ q$ x3 ?9 S4 _6 E7 O" Z

5 Q+ J1 z" y- `//点动模式键(高电平有效)  默认低电平$ |3 Y0 y3 V4 [5 o  e/ |
#define BUTTON_SS 36  y" k7 A% R$ i. J7 X7 a. L

3 z2 r0 S5 n) R  d/ k//点动键
! g0 ^% x  {' q$ z% H- L' o6 ^#define BUTTON_MAN 159 v$ G. ^# S8 D: \. u/ L  P) ]1 `8 ~
1 B( n# G; k) a
//电位器速度控制" W/ A2 H. r' L+ S2 C- z7 _
#define SPEEN_CTL 169 b+ _5 f! V' k; W: W* p- C- F3 D

- d1 `; `1 I3 t7 u! b8 @//手动调速使能7 P. \- t/ ~# L) ~# Y( y( i
#define BUTTON_SP_EN 17
' T- b# ~  m8 |) U- h% _% t$ Q
1 J; c2 j/ ?9 U//LCD 类型 1604或16029 z4 t3 X/ v7 Y0 [
#define LCD_TYPE 1604
  w! @* Z& O/ t- L5 A//LCD 引脚定义
, |& K: x  J0 X; N#define LCD_RS 23
+ H; B. }0 M) B. \! R/ A#define LCD_EN 24
0 A8 l8 e+ E/ x7 \. P/ P3 {4 R! C' E#define LCD_D4 25
7 z# {( i7 s8 o3 K0 U' t#define LCD_D5 26* K) Z" I! ^. G- `1 T. M: c
#define LCD_D6 27. A" @# T0 d  f$ r
#define LCD_D7 28
! @! p$ ?0 _% ?& Z. [, o0 V$ A8 R* n
; K" _: B4 L7 v1 l6 v2 H: M# s//命令字符串
& n- y- O( K. x: z4 j- B#define COMMAND_SIZE 128
% {3 c2 ]4 C: R3 Z. E3 Dchar word_old[COMMAND_SIZE];# S$ b9 k7 `. i0 V, ~
byte serial_count=0;
8 A4 {. F* @* R4 Dint no_data = 0;
+ K- s; a2 l" |+ p6 \2 G/ _//LCD 引脚链接 配置3 U$ o. v9 Z9 k0 p$ d3 m
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
% Z, ]1 |, g- U% h5 y3 x3 J' B//停止控制,高电平有效
5 f4 L0 B6 p- S3 `( j  i#define BUTTON_STOP 30
+ ]! B) y: n+ a8 wint stop_flag=0;
6 c8 E  J& f! m( \8 P3 d& f5 i7 A2 S  b; S/ O6 S' Z# T  R5 G* V0 D# Q& |
//暂停
' H% A8 v) i7 Dvoid pause(){
- V' b' Q' p( }% b" `8 L( j- N$ _  while(!digitalRead(BUTTON_CTL));
* n, q# o# q& P}
! y( f* W; l) Y& D6 s" H2 y! {2 Y% V! i5 H
void stopper(){" j7 X3 u/ T4 C$ P" N
  delayMicroseconds(10);
& t6 C/ c+ [( [  stop_flag = digitalRead(BUTTON_STOP);
- Z' Q! ^( L3 J8 l) L}3 q) x1 }7 U7 u/ @8 L4 ^* F0 Y2 ]
& c/ ?3 {- W2 r- J6 h! H' o
void setup()
- b- J( U, Z( T5 U% M; P{2 ]9 m0 L- d0 d6 ]1 M* M  |
  //show start
3 i& i/ j; }+ O) k: J  SerialUSB.println("start");! X: _# O9 h2 [
' W0 j. k! ^4 i- h, x
  //启动lcd
; e3 ]: {6 t& V    lcd.begin(16,4);
3 |! s- y7 O) [2 J1 w) v9 N) F* E    lcd.setCursor(0, 0);
5 ?, J) ^' l5 ~4 p: o8 G9 m5 u    lcd.print("hello, world!");3 A% @: a! }/ J+ \
: g  G" N$ Z) a  C- i! s" q
  //初始化控制引脚及引脚功能. h% `. p% E- q0 p( {
  pinMode(BUTTON_CTL,INPUT_PULLUP);
8 H; [) a( |$ o! b* [5 ^$ e  pinMode(BUTTON_SS,INPUT_PULLDOWN);6 J) I1 J$ h" e. L
  pinMode(BUTTON_STOP,INPUT_PULLDOWN);
. c- ~; Q: d# I  pinMode(BUTTON_MAN,INPUT_ANALOG);% Z1 X+ U$ ?& ^& b5 f

2 N0 [0 ~& N0 P: ?) G! U  //控制引脚的中断5 n6 Q" v/ M& r6 @. M8 o& i/ d
  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断3 f( f. i" K! S% X
  attachInterrupt(BUTTON_STOP,stopper,CHANGE);+ U! G3 c' t! v) I! [. B
7 H) F/ X+ v  o0 j! J
  //other init3 v4 q9 ?( c: p2 e; G
  init_process_string();4 T  G. A# H1 z$ \! Z: ~
  init_steppers();% i3 x: ~; Y+ e7 J  g  G) f
  init_camera();3 [! u) `& D, f
2 E; G+ e8 |" h% Y
}+ ^! v% @# u* G1 t* |/ X* N
6 E: L" W6 O3 X1 ?6 D
void loop()
2 |# D% _+ J" ?4 O4 A8 R{4 q; W" r9 Z- `
  char c;
: B! p; E" w! r0 s. X( Z
7 B  y8 o" y# Q2 H6 a" ~9 }0 i  //读取输入的字符
/ G0 Q7 r4 u) z$ ^  if ((SerialUSB.available() > 0) && (!stop_flag))
! S* h; y( ^3 p1 r  {
* Y! ]6 o/ _, T$ T7 Y4 b    c = SerialUSB.read();1 s1 N6 J$ v3 S- e* D! z
    no_data = 0;
; W1 t; Y' ~- v9 `1 n, A9 D2 U; E2 N/ Y2 m+ F
    //换行符代表一个命令的结束
4 j2 i. N, \( A% h    if (c != '\n')4 D$ t( E, Z$ q3 x
    {0 T. X' w$ c. r: T1 @; Q
      word_old[serial_count] = c;
. j7 Q( R( ~9 a" r      serial_count++;/ F/ n5 p1 t. u. M+ x  J

! z$ }5 Y( I2 g# ^/ u    }7 h( r  p$ k% [! P6 W  g9 b  L. u
  }+ y( D; W" o) {7 J+ L* b+ e
  //标记没有数据输入3 S# O% o% z/ n0 I7 s2 M5 H8 O  o
  else
5 ]1 E8 I" `( m" P4 E  {* \. o0 O2 j7 v
    no_data++;
+ s! ?+ ?, M' U- m6 K; D' t    delayMicroseconds(100);: q6 t' K  |5 l) F! M/ Z& ^
  }8 q, i# b: b; K. u$ d* U
- V# y; N' R7 Z1 i5 L* {" B
  //if theres a pause or we got a real command, do it8 L" R* m9 M" X7 o' b
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))$ `  ~" D' h: r! s- Q5 n( d7 B
  {
  ?. Q# y1 d; |7 L  n& z
6 I& i4 L  C8 Y4 W, M2 u9 h    //处理命令
" O* l- W9 u! c  v    process_string(word_old, serial_count);% V; f0 W, V" a/ v8 g
$ j! _: y0 W5 W8 w' R6 M
    //清除命令, V/ j* ?) \4 A' T+ r
    init_process_string();# }- Y. `! w! W  b: E, L8 w
  }
/ X4 h0 C  P1 d0 B( Q3 `( [  K5 s, A: z6 ~- \
  //如果没有数据关闭电机0 U8 L: [/ {7 `( H
  if (no_data > 1000)
/ V7 E* }& |$ m' w6 d1 l; @: S; e    disable_steppers();
$ c) u6 ]* `* n- b5 F3 H5 `* |8 Q  n$ @, Q- N, F$ S
  //如果ss键按下进入点动模式
% E8 ^' a7 o+ g4 i) U6 ?. V  //if(digitalRead(BUTTON_SS)) ss();8 _7 |5 k/ R! ~  D: n
  run_a();
  M5 ]2 I5 B1 Y+ ^}* p( O% t1 H  T8 {; A: R

* @- Z) ]% R* x& z( \! B0 @- g8 C" t! X//点动模式: f& ]& f: |+ Z3 a6 _" z( n
void ss(){
! O+ k1 B1 Z' P: E9 O* s! k7 g2 G# ?
6 n! t4 N" \- D! I  delay(1);$ R( Z; o. m$ l
  if(!digitalRead(BUTTON_SS))return;
; b3 f. g8 ]0 G( a( k  if(!digitalRead(BUTTON_SS))return;. L/ G/ [+ ^, r3 [! I
  //init_process_string();
4 ^( q0 p( Q, I) |( t7 K% ^+ s7 T  //init_steppers();
2 a6 ?7 t+ a: |7 I' s, c, v% h+ N  //init_camera();
) A" f0 ?* T- i3 y* T
& e" y' c4 m3 t6 C7 B' B% v // SerialUSB.println("Step By Step Mode");4 A2 p5 F) O3 w
//#if (LCD_TYPE == 1604 )
, z' x) E  n+ {& U3 a9 ~  //当LCD为1604时显示要处理的命令0 B+ c3 s& V7 x0 _3 v
   lcd.setCursor(0, 0);
' |3 E" M! J$ B* d   lcd.print("Step By Step Mode");
* @; Y2 s; G" ^% c# C  }//#endif
' p- F( X5 s9 n; C0 u  process_string("G1 F5000",8);
5 ]5 y9 p& L) w  process_string("G91",8);8 T( f, C- b( i+ x$ e% h
  //init_process_string();
7 T) T' u9 C& ~3 i$ v! Y9 }8 v% f+ ]& g- \5 u! v' L, b$ z7 T
  while(digitalRead(BUTTON_SS)){8 i6 m' K, w7 j# B3 ^
    int i=0;6 {: q# ~, `7 f0 F; y% Q: Q1 D+ q8 H
    i=analogRead(BUTTON_MAN)>>9;" w" \0 `! C& `3 Q6 m1 T4 [& _
    //if (i==0){break;}8 |4 M$ L. n6 _# {* J
   //SerialUSB.println(i);
  K% d; \# j( q4 a$ I+ I   //delay(1000);   5 S; G9 @8 B# G+ v& s/ P& Q# k
# g4 U/ J1 v4 F, T* B8 p
   if(i==2){3 A% P. U" m0 k* k4 b! [* ?$ m
      set_target(1000.0, 0.0, 0.0, 0.0);9 I1 y  X, \; C8 X7 e/ I. _5 z% b
      dda_move(getMaxSpeed());$ D8 H$ F( s6 S& Z7 Y# d
      if(stop_flag) return;# ]# ?3 f8 g' `1 g, p
      //process_string("X0.01",5);
8 S% e$ @1 X& ^! T0 C1 Z" I      //init_process_string();
! ]2 j1 S* S7 t: |    }
7 r. s& u7 x, z6 C. E    else ' F# x- F8 a3 A
    if(i==5){
. w$ G) h" ^* g: l) q      set_target(-1000.0, 0.0, 0.0, 0.0);6 m/ g+ S1 |- T2 l# S4 h3 s" z
      dda_move(getMaxSpeed());
* U8 ~6 ^% H. f0 v$ N2 P      if(stop_flag) return;
9 t6 e* O3 R' Q; ?+ @      //process_string("X-0.01",6);
! i2 a# x2 x3 P# v; D      //init_process_string();/ g# G* S+ H4 s2 {( T4 ^
    }
: Z1 N! @9 B. Z     
( D" k8 v6 W) c6 p/ g" `# t) z; h- @3 i. [2 K' q
  }% M3 W/ E# A- o! E, s
( H8 f4 O. e& j
  //init_steppers();- f  g! k8 ?$ c  n1 z" P* d
  //init_camera();
! @; [6 ]( s2 Q5 v3 D& b3 |/ m5 _   // SerialUSB.println("Return To Normal Mode");9 }, {; ?0 v; E0 c$ C
     process_string("G1",8);
* W+ f% q- y" B- z3 b& ]  k  // process_string("G91",8);
6 `8 b% {6 `" T3 B  b: J' K    init_process_string();& G/ e* Z: f) z0 Q( M
//#if (LCD_TYPE == 1604 )
  Z: c7 E3 n4 M5 s0 Z  t& g  //当LCD为1604时显示要处理的命令8 K7 w' j" `2 s, s5 M
  // lcd.setCursor(0, 4);: x' R( ?# b: ^6 f$ r
  // lcd.print("Return To Normal Mode");
. E5 A9 o3 t5 M) C% g" c% k/ w//#endif( h% c* ?) y/ }  A0 A
}7 I. w6 _- Q' k

+ d  G6 c( Z9 D+ Jvoid run_a()% u+ A7 H9 v* z2 O0 {  E0 t. K
{/ w7 J7 {$ D, `7 \
  //delay(1);
" o% P! X; ~3 J( m* X/ h  //if(digitalRead(14)== HIGH)return;0 }# @3 W# J" u
  //if(digitalRead(14)== HIGH)return;6 O* J* \6 [+ D! F
  //process_string("G1 F2000",8);: z9 R7 ^7 y% ~# }% K2 ]9 {
//process_string("G92",8);# o, r4 R1 H, y$ W! {2 C- _
process_string("G90",8);$ d6 |% J( S6 }
process_string("X120 F10",5);, \; y9 ^! i7 y- V5 q, y1 L  [
process_string("G4P2000",8);
8 \# O( ?% n2 \" h9 I' l) x process_string("X10 F10",5);
6 T  ~# T/ I+ y0 K4 K// process_string("M101",8);
4 x8 ^0 ?* t2 }( K// process_string("X-50",5);
( o# k/ }5 j% U1 U dda_move(getMaxSpeed());. c! H5 u' ^- P" P' R* \" ?
init_process_string();
: L: o; c* ^! w! B6 u- I
1 u5 J) Z' b* w}
1 p5 ~" m# M# w
回复 支持 反对

使用道具 举报

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

, O3 ?4 ?1 w9 W- V; F3 E4 ^( A: n2 h% p. i, u
// 定义机器参数9 w3 I$ v8 K- {" c$ j$ b0 k
#define X_STEPS_PER_INCH 400
; W* i+ r# @+ G! \# v! |0 T5 D/ e#define X_STEPS_PER_MM   16.0
* _) c( Y0 Q' x#define X_MOTOR_STEPS    200
! |5 G6 |; u8 I2 u
- ~0 \7 b) ]# O7 t#define Y_STEPS_PER_INCH 400.0
% d$ s( @0 E. y3 |; Q( r% V#define Y_STEPS_PER_MM   16.04 h0 B7 h& L7 v6 @
#define Y_MOTOR_STEPS    2004 I) O' y5 A9 q  @' m. W. W0 T
- C8 E0 x0 P% N, I" P
#define Z_STEPS_PER_INCH 400.0
  e+ k3 g0 }& }$ I% i#define Z_STEPS_PER_MM   16.0
7 \/ j  m" A: }' w7 G3 x1 d. `#define Z_MOTOR_STEPS    200
( O7 s0 x6 M# ]! {4 S
7 f7 L# X$ u, _% N" w( k#define U_STEPS_PER_INCH 400.0
; T2 m, G* l7 _! \#define U_STEPS_PER_MM   16.0$ Z7 [$ T0 q) R, B# p( W1 B
#define U_MOTOR_STEPS    2002 L( o) W$ T* \+ P
. i5 d4 f; d2 ]3 n- `6 W
//最大进给率) D3 S- J3 @' z% f# K! h
#define FAST_XY_FEEDRATE 1500.02 V/ f6 D, s5 U% t6 u0 F
#define FAST_Z_FEEDRATE  1500.0
- b- v4 O; z  ^#define FAST_U_FEEDRATE  1500.00 N9 D4 m' q8 D/ i+ H
// Units in curve section4 C  U5 C( W; _! w
#define CURVE_SECTION_INCHES 0.019685  A/ K( }$ s" e, o" D$ W
#define CURVE_SECTION_MM 0.56 ?/ W% F. b. F+ h$ }: d

: O+ L* a# k& J( d4 y2 [5 \8 Z( S+ V) R3 D
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
% C6 v5 @$ B* l8 ]1 A  K' `* _// RepRap opto endstops are *not* inverting.
7 }( m  e! y% Y  a3 a#define SENSORS_INVERTING 18 b4 g+ ?0 B3 E) N- T6 s+ Q1 n
0 m" _" H' C1 s  L% G  ]2 d) D. C5 P
/****************************************************************************************
# t. E" z( B0 ^4 m9 l( b6 T8 L * digital i/o pin assignment/ S6 N* C( r( e' b; k7 f2 q
*
, H6 |& Z$ k% `& V * this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5. ~6 m" O1 a# O, E! P
****************************************************************************************/
" g, x! k! X% D; V8 L  f( s1 X
, |6 H# N) y- _4 ^! m8 y0 n. c//camera shutter and control pins
' i0 l3 W' w: l0 }) V+ d. Z#define CAM_SHUTTER_PIN1 296 i6 E: `1 x( V4 s. k( R
#define CAM_SHUTTER_PIN2 30
4 Z8 H& O7 s: ^% p. ]//#define CAM_AUX_PIN1 31 // analog 0
) V( z, J/ |+ {/ I//#define CAM_AUX_PIN2 32 // analog 1, Z" A0 T8 b2 |2 Q0 ~  M. }
//#define CAM_AUX_PIN3 33 // analog 2
0 P3 e# C! e8 J) h2 M+ L/ w//#define CAM_AUX_PIN4 34 // analog 3
6 S( U7 _+ C9 P* z5 b  t. e! x, Z# T% w4 n0 J2 d
// stepper driver pins
6 C/ e+ k8 O0 `/ x6 q( v$ @$ J9 q#define X_STEP_PIN 76 P8 r) Y0 L2 Y* q' Q
#define X_DIR_PIN 8
+ g, r; C8 M7 a9 O! u, B#define X_ENABLE_PIN 198 _9 q& d1 L  Z7 S& a1 m
7 n7 E: E# d8 u$ G
#define Y_STEP_PIN 9( V( Z6 W7 Z0 a2 l
#define Y_DIR_PIN 10: J/ q2 n9 G/ e! q
#define Y_ENABLE_PIN 19
/ {$ c, ?5 e. X, z6 Y+ j
5 `  a, I+ E3 V6 n1 e9 ^#define Z_STEP_PIN 11% k+ M. p/ Y% `5 d# K2 `# N
#define Z_DIR_PIN 12* M; z$ F. h# N0 M
#define Z_ENABLE_PIN 19
0 t( H+ p; V% a" L
: x7 G: b8 N# g# Q4 t0 [8 ?6 u#define U_STEP_PIN 13- c8 ?5 K1 g" U8 D# D, v$ h/ t
#define U_DIR_PIN 14
4 d2 X6 z% c* I' }0 T4 a! \) C#define U_ENABLE_PIN 19
1 V) T4 L0 G( j4 _% ^2 R- a$ P: a  w$ t% J
// limits not used right now
1 d; c  n- v# }6 u$ N#define X_MIN_PIN 144 `- h9 D* o- `' A
#define X_MAX_PIN 14
/ v& \  E9 ?8 B- `& t2 m, f$ L#define Y_MIN_PIN 14
8 D/ w" }$ @1 b& Z* m#define Y_MAX_PIN 14
5 E: J6 ], `  H. F9 f8 W#define Z_MIN_PIN 14# i6 j5 m  K7 R; e
#define Z_MAX_PIN 14
+ E! r) u" w% U4 [4 z+ ?#define U_MIN_PIN 142 L/ o* E& ~/ W3 J) f5 g; N
#define U_MAX_PIN 14
( }& o5 ], |& X' y9 C/ T) A
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()
3 g, H1 g$ d/ }/ x6 v6 K: g{
( [, I% o, t: O* t  pinMode(CAM_SHUTTER_PIN1, OUTPUT);
+ c( w) d* ^  o5 r! Z% O  pinMode(CAM_SHUTTER_PIN2, OUTPUT);: P% {6 t7 H) j  Y4 ]3 x, }0 I
  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0- y& O* d" o0 R
// pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
  j3 \9 L5 e( z: Y3 D  y" o6 N // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2
- q( v$ n0 E" C5 B // pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3. N& G$ T% D( m2 t9 X% z
}
+ c7 y  x1 S9 \$ P2 `& E! G9 S. @! D. m8 R
void camera_shutter1()
7 t4 W% l6 |( \+ p/ F{6 e, I3 {5 N) k( @$ L+ h9 A: z! f
  // fire the camera shutter via relay...1/4 sec hold time
5 S# @3 X% v4 ^6 s8 P/ b* L  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
; S! {7 p5 s/ {* R3 D" m  delay(250);
6 I  C6 W  G7 R' I& ]: d- n" G  digitalWrite(CAM_SHUTTER_PIN1, LOW);0 j2 N+ Y" V; z) F
. C1 a- C, s! G9 S: C
}+ Y, p! p! u- I* Y% ]+ h2 t' s$ D" k) g$ S
. f8 ?' m' \) ^  M1 V
void camera_shutter2()
  U: K0 `2 j4 K) F( L{
6 D7 j4 E; T8 v( t5 M  // fire the camera shutter via relay...1/4 sec hold time, Q4 z( i6 |& o( K. q1 A1 ?
  digitalWrite(CAM_SHUTTER_PIN2, HIGH);- S+ N/ T( \+ b. Z$ ?
  delay(250);
' F$ K& i. L8 Z0 A( f6 \: U' O  digitalWrite(CAM_SHUTTER_PIN2, LOW);, h" k0 N8 ~% ]" _% K/ j# x* X4 z: p
& H8 x- m' Y( h+ I1 N
}% R" ?6 |; V1 T* T, j
/*7 H4 x/ A: \, D. \8 w% k
void camera_aux1_on()
! F+ R/ T5 k" |3 }) p# C5 t/ W{. _* ?1 H9 A1 r) C5 ~0 T
  // turn aux relay 1 on, T( b; b, l! t
  digitalWrite(CAM_AUX_PIN1, HIGH);# `' j4 Y& V7 |4 t" ?% T6 `2 X
}
7 d$ t$ n" q% j8 T: C% ~: @
* D" i( N% e& |: x+ Svoid camera_aux1_off()6 a8 v  E' x2 {$ Z  z0 Q( g+ |
{5 m: P5 `. e+ h/ Q1 C, |! c
  // turn aux relay 1 off
9 ~( L% W1 A! }$ u6 V% y* [  digitalWrite(CAM_AUX_PIN1, LOW);
2 v* d/ z0 P1 ]6 O2 W}1 e; z. c  s/ p7 @6 d* V

& U8 H) a2 q2 X2 S, t- m% @void camera_aux2_on()  j! R0 R( P9 h) w) H2 {" p. L
{9 K  t9 d2 H! w
  // turn aux relay 2 on
8 u, `4 L" }, u  digitalWrite(CAM_AUX_PIN2, HIGH);7 t+ e* M# t8 e% z$ s
}
$ z. s8 X# l6 a
# H- R% w$ d& p8 n0 mvoid camera_aux2_off()( X; y: P5 J( E/ |3 s5 `; I
{
5 g8 P9 g# c' \7 C  // turn aux relay 2 off
. k1 ?- y* V/ N4 _  digitalWrite(CAM_AUX_PIN2, LOW);8 R5 y$ q* V- M) |/ ]
}
! u8 U- F/ Q0 n4 D! ~1 [' t' j
" e! `% _) N, }  l8 v/ c1 l/ q$ Tvoid camera_aux3_on()9 ?( P; p) i8 r. Q
{( X% V: ]- @4 v5 X- F- U
  // turn aux relay 3 on' [9 }+ X/ p4 I" Q- B
  digitalWrite(CAM_AUX_PIN3, HIGH);
* i# X9 L3 x* p6 r+ i  Y/ J5 R}
1 T% c# A5 _6 C4 G3 P& E
: e# W( b2 M3 v% y) wvoid camera_aux3_off()9 l% T# y- K& {. U
{' I4 J+ l# l1 ]1 S( d
  // turn aux relay 3 off7 l1 @; i7 ]% o. P9 j1 B. j  p
  digitalWrite(CAM_AUX_PIN3, LOW);" X( _( x6 }4 u' Y' ?
}
& r! [% o% S: W6 j
) A' I$ E6 i" ~. F4 {; n0 I% }void camera_aux4_on()8 h5 g. }- N; G, C
{# P6 @; {, m9 g6 w
  // turn aux relay 4 on
1 x* L; c, k; W, @9 X  digitalWrite(CAM_AUX_PIN4, HIGH);( a8 u4 l: D, ?# W
}, T9 Q5 R# b7 A

3 x$ `' l2 w2 e- I* j$ Tvoid camera_aux4_off()
7 \! I% y) p. f  X5 o, G{% h2 [3 X2 \: z# @$ q0 }
  // turn aux relay 4 off2 B  _" _2 Q  n; z( Z
  digitalWrite(CAM_AUX_PIN4, LOW);; B/ Y- ^' x  p& h' `  _
}
回复 支持 反对

使用道具 举报

发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错5 V- Y/ I7 r7 }/ M8 r2 r1 [
请问楼主是玩什么的,用乐高玩具吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice., o) ~5 p( {  t2 y$ w$ l6 W
struct LongPoint {; @$ a' ~9 `/ @0 C( O5 d, i9 U
  long x;
2 Z' |- _) Q9 C& d: K# Q: B! r( S  long y;' X0 i3 A4 p$ v+ g6 a) h5 x/ h
  long z;# D& H2 f9 y1 X/ |6 D
  long u;
" C: E" m% t0 J3 t};" |  d# m4 V( @! V4 x

- D$ P; `  D2 x/ V& K5 F# g2 Estruct FloatPoint {
- k! h2 c; E8 g$ c  N  float x;, ?9 x8 |1 {; T0 x6 Q+ i" [
  float y;2 u# _5 `" k. L! e* M
  float z;
! \' M- z1 \6 V0 `3 Q  float u;
* J. D: t1 B4 J+ B2 F2 x$ c};
/ F: N" c. h0 Z2 T/ f1 m7 P* u7 W% \5 P0 x) x+ H* x$ q7 n, n
FloatPoint current_units;
2 d, H6 a9 E# E0 I$ L: HFloatPoint target_units;
9 p7 B/ T! |) \* l! w9 d2 |FloatPoint delta_units;! K) k2 a0 S2 q) A/ S

, c# z! C, X3 u/ i* F+ aFloatPoint current_steps;
! o6 {/ W7 T6 V6 R6 h4 FFloatPoint target_steps;
* g$ Z1 H0 O3 q* n* k$ k. c' ]FloatPoint delta_steps;7 c+ J5 Y% `2 [% d7 ]1 }/ b, n

* I% a/ n- m6 i: @8 qboolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式3 z: \& j- _) G: W4 a1 B
: X- E0 W9 z# X9 q
//default to inches for units
# y' ]9 N- n% x# c- a" ifloat x_units = X_STEPS_PER_INCH;
* t5 U* g% S$ e1 Q% Ffloat y_units = Y_STEPS_PER_INCH;
; R$ D. t2 k: o8 d: i4 sfloat z_units = Z_STEPS_PER_INCH;1 a$ W9 y# ~" g3 U9 ^# h
float u_units = U_STEPS_PER_INCH;+ H5 p* p4 T* |$ V. {
float curve_section = CURVE_SECTION_INCHES;2 U9 K0 F" O3 T3 j2 L
& o* c: I1 v8 U5 `8 @( s$ l
//our direction vars
7 N2 n1 |5 _% g: c" S. Xbyte x_direction = 1;8 b3 ?, E- M0 c5 L& Z. D3 C
byte y_direction = 1;
4 Y9 h& m: M+ h, o. \byte z_direction = 1;
* @% p( X- P- `; U# s8 q2 Ibyte u_direction = 1;" |' `5 e1 p- \1 d3 I# v7 h
, u5 P4 X9 i% A8 y) Z1 B
//初始化字符串处理
1 Q( k4 J: e) E1 w% Zvoid init_process_string()* U, r$ |5 r: W- A
{7 R) [& g; e1 B% _
  //init our command! N$ w6 M7 X' Y4 B! {
  for (byte i=0; i<COMMAND_SIZE; i++)  e* Q4 @; ~  [! b. v4 @
    word_old[i] = 0;
0 a( y: A. [; o  serial_count = 0;: o& o7 c' R/ E2 K7 m
}
$ P$ I) n+ \" r. L: b& d% U! E' S5 X: T! C
//our feedrate variables.
! L# u6 Z4 p# G3 `3 |+ Hfloat feedrate = 0.0;# F2 a, j3 j0 z
long feedrate_micros = 0;% T* G0 J9 Y# b( Y) l

" N# [9 O7 z+ Z3 F//读取并执行命令
3 U* g# T) t6 `. u. h0 c" xvoid process_string(char instruction[], int size)- Y- E0 H  d9 X/ Y( f/ j7 u
{& r" {* |1 \% z+ L
  //the character / means delete block... used for comments and stuff.. \$ \) c/ U( B  |
  if (instruction[0] == '/')
: Y& n% m# G' ~! C  {0 q" U1 M( X, `! z
    // SerialUSB.print("ok");! ~" u6 c4 k; F. S
    // SerialUSB.print(byte(78));
5 B2 A4 v) z) N7 m    return;/ F3 Z9 c8 N0 H" x4 c
  }3 x/ X/ Q. Q/ t9 c2 q6 c+ L
  //init baby!
9 F1 n3 R1 r+ E7 D2 _  FloatPoint fp;
; @3 e% }- V' l1 r' ~( Q  fp.x = 0.0;
% j* k1 c0 d; S- A  fp.y = 0.0;
0 v+ U: F5 j8 `& H; y  fp.z = 0.0;4 ^3 ~1 S1 l1 [8 p
  fp.u = 0.0;5 K" Q' v. n) X# w
. T4 U& f+ a0 i; ^5 N
  byte code = 0;
+ x% Q, ~2 H* P4 w5 Q' i: e* `- i7 k( E3 m0 n
  //显示在处理的命令4 X+ n$ w- j0 p4 o
#if (LCD_TYPE == 1604 )9 f' `) I$ T7 R4 R- X
  // lcd.setCursor(0, 4);- W4 g/ Q& A6 s/ ]* ]8 I7 I& j8 ?: F
  // lcd.print(word_old);; x! H5 j- W$ @! R7 t
#endif
6 ^9 u+ C8 }8 H3 }; Y" H  SerialUSB.println();. r( c# |- A# y" F$ @& K( {
  SerialUSB.print(instruction);% c. l2 _% R. a) y( ^) S( y
  SerialUSB.print("\t");( y  t9 r/ Z9 O5 N) d

* z% k# U/ H1 |  //what line are we at?
5 F% s6 L( }/ @/ I2 S( x' U  //        long line = -1;" ^4 c' h4 l7 N- v3 p, j; z$ g
  //        if (has_command('N', instruction, size))! m  P0 B- w9 ?1 _
  //                line = (long)search_string('N', instruction, size);
$ N' ~2 E4 G" J/ M: }9 b
- h: ]6 [& p, }# u  /*! r& ?5 y7 k, {5 \3 W
        Serial.print("line: ");
4 K0 Z0 J! H# @  M7 r8 X: I9 o1 R) e           Serial.println(line);2 l6 [; @9 r: H* v
           Serial.println(instruction);
) R1 n" f9 ^% g# v   */4 f0 }; X0 k4 u7 J5 V/ k
  //判断是否读取了个 G代码?
; y' g+ O$ t, r, t  if (
( ?0 K, P, a; ~- m" E7 t1 x) c7 K    has_command('G', instruction, size) ||
: T0 `2 y: g: i; }8 I/ ~5 q    has_command('X', instruction, size) ||7 g, _  `# ?+ N+ V9 d. E
    has_command('Y', instruction, size) ||6 h$ U6 B# J9 ]' S
    has_command('Z', instruction, size) ||
7 @! x; j% A7 z    has_command('U', instruction, size)) P; |3 V' f; J) F, t
    )
% \( c% K: ?& d* ]/ x8 S  {5 Y& h/ P0 U) e) S; }, p
    //which one?! s) n7 T( Y6 P' c+ U4 Z- P- u5 L
    code = (int)search_string('G', instruction, size);# F# {1 L3 z; W/ x) A
    // Get co-ordinates if required by the code type given
8 O4 `# V  L/ E- N  K2 R    switch (code)
" b4 z' f  L* C8 i; L    {
" `3 z2 I7 l8 u9 m6 L; Z9 v    case 0:
* F$ ~7 p* K/ Q1 w$ S1 C    case 1:
) Z% F& v( e& Q/ l2 E1 I% p$ K1 @    case 2:6 H; w9 j; ]+ E) Z& F
    case 3:
+ O4 a/ _4 g& G/ x) g% a      if(abs_mode)
  i" n/ |' F# R& M" ^5 W      {  J. W1 k1 O+ ?) E7 W, q+ w
        //we do it like this to save time. makes curves better.
/ F1 W- x+ t; O3 k- Z        //eg. if only x and y are specified, we dont have to waste time looking up z.
6 f" \& x$ R* I3 l& F9 O2 b" h- j/ w        if (has_command('X', instruction, size))
/ y' b1 E. Q# }) E! i* W# I( F          fp.x = search_string('X', instruction, size);; N. H6 s8 K' \: O4 z) t" S9 o
        else
- x# a0 y2 Y0 u: V0 t) v( \          fp.x = current_units.x;, m& H: @) c; _

. P4 M& c1 I8 I4 }        if (has_command('Y', instruction, size))
! S- w  M- ~) K) _5 J1 F2 k  s* K1 V          fp.y = search_string('Y', instruction, size);5 u3 c# [; E! c) }2 G$ j7 b
        else
. v, p; O5 H0 j" l          fp.y = current_units.y;
; ]) r4 j( Z8 b. I8 T
9 J9 F, k/ H' @1 W; F        if (has_command('Z', instruction, size))/ y% k) o" [" m
          fp.z = search_string('Z', instruction, size);
' M& }  z6 f, \  E9 w, `: k  h. Q  c        else
- _% M! T8 _+ c- s, z$ \          fp.z = current_units.z;" I9 [, o: \/ o7 l
          / Q0 l; J. R2 O; Z; [+ U' B
        if (has_command('U', instruction, size))9 }, R* @' \& c
          fp.u = search_string('U', instruction, size);# b  |. M& Z5 l* T4 d  s! A
        else
' |0 X6 O1 A3 R) k8 f7 w          fp.u = current_units.u;
  [9 l( ~* w" f# A' K( C      }
* n  C( ]( ?* [      else0 T! D6 s5 t1 O5 T$ b
      {& f) g9 v* u4 O8 p4 g& c6 Q
        fp.x = search_string('X', instruction, size) + current_units.x;; j7 D, Q% u, n
        fp.y = search_string('Y', instruction, size) + current_units.y;
) x; V& p" f( l7 l% @1 l        fp.z = search_string('Z', instruction, size) + current_units.z;
! }6 q0 |! U# F6 h        fp.u = search_string('U', instruction, size) + current_units.u;
6 k7 }7 N2 U' l% h( h, U& T      }
4 ^. u7 a! r' @0 S3 {      break;' T( K: M0 b; w. y* S& G1 {9 f, V
    }! A2 d3 v: Y( C3 G5 L+ r
    //do something!# ~; x) X0 [: K$ u0 h- s
    switch (code)
% l6 Q# K( G' r2 D* U( H% E    {+ M8 i+ O" X  q
      //Rapid Positioning
3 O& J; ^* A: u! k3 s7 J9 X5 g# c      //Linear Interpolation
! H8 v* D/ n: d. v& _( @      //these are basically the same thing.
. ^5 ~# v0 I( E; q% ^/ q: Z+ s0 A    case 0:2 g+ _8 P- M& c: l% [' E
    case 1:- P- n/ M: E' t6 x
      //set our target.8 U+ I% A( T' ~$ {/ Z
      set_target(fp.x, fp.y, fp.z, fp.u);
, b( \. c4 ~0 B, \      //set_targeta( fp.a);) o! Q% r+ D, A7 c' i
      //do we have a set speed?
' G; w) v, k* x. u; W      if (has_command('G', instruction, size))
% b2 j# i. t. I6 j* Q% P      {
& X: d# c4 t! T! |9 ~+ U, ?5 o5 ]        //adjust if we have a specific feedrate.! d3 M0 Q1 j" J; |
        if (code == 1)
3 K9 k1 ~1 x- t* z( F        {
. w$ L& [1 k! l- Q2 i1 F* @" W          //how fast do we move?. F! @. X% s; m
          feedrate = search_string('F', instruction, size);
+ t7 X" \, ]3 O" Z          if (feedrate > 0)
* [2 k, g" }8 C: W            feedrate_micros = calculate_feedrate_delay(feedrate);
" k! S$ R, L1 o6 Y7 a% A4 }          //nope, no feedrate
9 ?8 i, H) t+ R# v' x" r# _+ t          else
% Y  T, R; K" z7 a) _- I* W            feedrate_micros = getMaxSpeed();
% ~" {) D& B6 ~' i" q        }
8 `7 B# N4 e. G4 D" i        //use our max for normal moves.1 [; v# {" S* e
        else
/ b! R8 |* s; [* p' N5 _          feedrate_micros = getMaxSpeed();' c( _8 J1 b3 m0 \% E3 L
      }
$ Q. b0 C* @- I5 w8 g. }      //nope, just coordinates!
+ U+ W# c9 q8 M! V! K7 {. b) i      else; x5 w% E+ Z% F0 R9 Y5 k5 z: ?) P
      {3 R* y+ o( v) S+ Y  r0 J8 B- u1 [
        //do we have a feedrate yet?/ T, T  q0 o( N8 W* t, b+ {% Q; y" w
        if (feedrate > 0)
. |( S# Z7 ]  W; D          feedrate_micros = calculate_feedrate_delay(feedrate);2 Y  d, n. v6 o* P# C. p/ }$ T
        //nope, no feedrate
9 t) ^  l/ E, i7 N$ y        else% ?: o( w7 o- n8 {
          feedrate_micros = getMaxSpeed();
7 K2 O* t3 n$ |) Y4 i      }) B' R3 K: K5 M$ t; ^( h
) {6 u  H  G3 U$ t, q
      //finally move.
5 P- {/ H0 z# h& X# h' }; r      dda_move(feedrate_micros);
; {3 w8 F* f& m" P% L      if(stop_flag) return;
+ _) @/ y0 C: C. N3 B3 \; M      break;, P, i0 B( I9 F& N$ W

- c7 K7 I. |. j, {2 v! ^      //Clockwise arc
6 g0 j$ [5 v/ r: }    case 2:
  u* }/ L6 d: |+ F! }2 [# l      //Counterclockwise arc6 m6 W' V2 j5 d! Z) l" c8 Q
    case 3:
, L+ P0 Z8 U9 P      FloatPoint cent;$ \, l+ v" i' ]
      // Centre coordinates are always relative9 T+ a, s6 Y0 c. a" p
      cent.x = search_string('I', instruction, size) + current_units.x;7 l# V; T5 o/ N8 E
      cent.y = search_string('J', instruction, size) + current_units.y;" B! v5 w7 b* E# m' {. M
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;( r3 ~# V+ s* }, M* o
3 y9 \6 h! Y! n. u2 r
      aX = (current_units.x - cent.x);
: H' ?% a. U, G8 F9 ?+ I      aY = (current_units.y - cent.y);, K- i* A8 E. m* m5 K" C% m5 g
      bX = (fp.x - cent.x);+ Q1 _, K/ ^7 k( r6 b5 j$ f
      bY = (fp.y - cent.y);
/ J  o0 E1 J# N
- N% e7 Y5 P+ E5 [2 Q; i      if (code == 2) { // Clockwise1 P! x# b3 A& N7 c4 a3 p( v
        angleA = atan2(bY, bX);% P# R7 L. J) ^1 P* b
        angleB = atan2(aY, aX);5 P! ?3 A% O5 V: L
      } ' r1 {' N. e5 Z" d" O
      else { // Counterclockwise
* Z8 q$ C$ O. D' K: {        angleA = atan2(aY, aX);
* Z& J# B  K6 T3 u, `, R: i; K        angleB = atan2(bY, bX);
6 g/ R$ R% a0 j1 v      }; ^' n& [2 b# q* A2 s7 {
      // Make sure angleB is always greater than angleA
( B0 a( q% v3 k  B8 f: B. o      // and if not add 2PI so that it is (this also takes
4 H" ?( }6 O7 x/ L: _      // care of the special case of angleA == angleB,# Y' b% C! V# K/ Y9 i9 t- O
      // ie we want a complete circle)
; W2 N1 h, S( N$ a      if (angleB <= angleA) angleB += 2 * M_PI;! J, t- ^, L2 F% X) L: u
      angle = angleB - angleA;' H* m5 P- e/ S  p7 f2 h0 l
& [/ o& X; Y; |; G% \
      radius = sqrt(aX * aX + aY * aY);& o# X  U2 h* P1 Z* {. a
      length = radius * angle;. S3 |6 n7 ~2 Y4 |# L
      int steps, s, step;, I$ R# _. l; g1 M; w- h9 ]4 s0 C& N
      steps = (int) ceil(length / curve_section);
- K" C* U1 k  y
2 z+ q! @! e# J6 ^7 y2 D' P* ]      FloatPoint newPoint;! D% L- u% J) |! Q2 Z$ L# _$ z
      for (s = 1; s <= steps; s++) {, l1 h2 G) f9 S
        step = (code == 3) ? s : steps - s; // Work backwards for CW
) p( F1 c! O" B+ K8 g        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
  ?: c& k5 L& x! {% U5 H        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
; T9 _$ }% C; y0 F- J9 W        set_target(newPoint.x, newPoint.y, fp.z, fp.u);7 R& D# t& Q/ k$ g7 B

" @8 T# r' ?3 O' b. o/ \8 Y3 w        // Need to calculate rate for each section of curve
( a  Q, `- X' ~! t# ?        if (feedrate > 0). D- s& ^9 M% e) U' i  Y0 J8 M/ m
          feedrate_micros = calculate_feedrate_delay(feedrate);! y( O! F' Y, G/ c
        else5 y" `" G/ r6 o, j( f0 J- t
          feedrate_micros = getMaxSpeed();  N, s0 r6 [* `, o6 O0 B4 S' z- r
/ }2 i' _5 P9 e, X* W3 c: f
        // Make step
  N0 r: W7 N! u: G- W0 E        dda_move(feedrate_micros);
/ O/ x2 Q" [$ @: {2 }/ h        if(stop_flag) return;8 V/ s# P2 e: I) j# W
      }
! Q8 v/ {) N) r7 Z: N- Z4 T$ X( Q; s' _9 Q2 b
      break;# g3 X3 d$ F1 |4 O1 y2 d9 X
& D' H% d  w$ G2 h/ M% W% \6 p
      //Dwell4 f$ \' m1 r+ z  v) o! W
    case 4:; V6 w2 [4 ~. J; Z
      delay((int)search_string('P', instruction, size));
+ K. V* e( e; ?0 F" G6 F4 S& ~      break;
4 {* o2 A- u! v4 X. R% F
3 T7 ]2 i! j+ p0 E      //Inches for Units
1 ^; f* G8 q+ g0 i4 \) m    case 20:
# e; h5 y* m4 H      x_units = X_STEPS_PER_INCH;5 ?; `: i0 D* ~
      y_units = Y_STEPS_PER_INCH;7 v& v' l; X' z7 ~
      z_units = Z_STEPS_PER_INCH;
2 g3 ^- L% K. _6 y      u_units = U_STEPS_PER_INCH;
8 V8 ^* x+ G4 |" T4 {% ]      curve_section = CURVE_SECTION_INCHES;
* F& r8 C* M8 b6 P      calculate_deltas();
" c+ R6 ]" U) w- x; l      break;! i0 `: F8 ]. b) C, @3 A
4 f7 c5 O% J3 N7 v; i
      //mm for Units, R6 o# L* t6 ?
    case 21:6 s$ x7 F  z7 l
      x_units = X_STEPS_PER_MM;
" W5 ~, J* B( W4 g  X+ g3 M      y_units = Y_STEPS_PER_MM;
7 ]  s8 ^: W% q1 {- s4 A. F$ G/ L      z_units = Z_STEPS_PER_MM;
4 B- Q# j3 h7 n      u_units = U_STEPS_PER_MM;8 S9 j! |) ~* w2 [- }) u. ]  w
      curve_section = CURVE_SECTION_MM;5 N" C9 B" Q0 K9 r+ H* }. @% ~
      calculate_deltas();. y4 U1 }9 T! ^, e5 ]  D
      break;* _6 E" f3 h" U2 o% r& Z5 |9 h
- J9 Z" n, _$ r7 N
      //go home.
! e1 F5 o: P8 u+ b$ p* G    case 28:/ l" c( L' @: Q+ r8 @
      set_target(0.0, 0.0, 0.0, 0.0);5 O! A0 o( A4 J: O# ^
      dda_move(getMaxSpeed());
" O% Q+ L* G1 g5 y2 {6 G2 g      if(stop_flag) return;
, Y! t0 D$ c* u  {1 y% A      break;: D: l+ T  q9 \8 g. i+ o  a

$ m0 `. {' u4 s      //go home via an intermediate point.+ s" U- F6 I3 L. P4 @1 X
    case 30:
+ h6 d4 D+ X, {( z9 L6 P2 J      fp.x = search_string('X', instruction, size);& f. f! `: q1 t0 `# k
      fp.y = search_string('Y', instruction, size);
. S. A8 t/ ?% o      fp.z = search_string('Z', instruction, size);4 G: L1 l8 x9 Q, Z1 d
      fp.u = search_string('U', instruction, size);( e0 A# j$ a- [( t+ _
      //set our target.
$ {2 J2 J/ u+ h/ `4 f1 t3 z      if(abs_mode)8 M/ b1 Y- m) p! |
      {8 z5 h$ U) d: [  N6 t6 H
        if (!has_command('X', instruction, size))/ \- N% u$ }9 F* n5 R3 C
          fp.x = current_units.x;' t& ]3 }8 X) O0 }$ Q2 q
        if (!has_command('Y', instruction, size))% F' t7 e) ]" Y2 V: q/ n& _
          fp.y = current_units.y;
3 M. X3 y4 Q5 o( V8 N        if (!has_command('Z', instruction, size))& T" G- o4 q' I
          fp.z = current_units.z;9 W8 \" k& j( S1 h, Y. g
        if (!has_command('U', instruction, size))6 h& ]0 t# h! I2 p! ^, y# ~* W
          fp.u = current_units.u;: b1 ?% q2 Q4 I( S: I" x7 F/ p' ~, h4 T( S5 U
        set_target(fp.x, fp.y, fp.z, fp.u);0 N- Y  o6 ]6 l$ }6 G
        
9 f9 l! X, [0 n6 s( P      }8 W& n  Z: ?  i- {5 K) f
      else
0 r, E: F2 p; d5 u; E; B        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );- y; M, R9 a' I% ], q6 G
       ; e9 B! K: K* e+ V' x& K
      //go there.' @' j0 @- Z# E% J' @  w% C
      dda_move(getMaxSpeed());
! b% n3 L. m, s) e$ f      if(stop_flag) return;# C( s% D- O( @, k9 q# f

% j. o# X! [' H9 H; W      //go home.
3 L; v6 n& Y; o+ {# X2 Y$ {      set_target(0.0, 0.0, 0.0, 0.0 );0 x' S  _6 a- b$ g/ Y: y( {3 F& o
     
& H" V8 w" {. e, T! l5 Y      dda_move(getMaxSpeed());6 l$ s7 o! y4 J  n7 b. m' q
      if(stop_flag) return;
) Q* c! J3 M+ C; u% ^9 u1 H5 T      break;
2 O) m& h# M6 f2 U- Y  v5 f( J2 F3 `" Y+ t, }/ K
      //Absolute Positioning
' `" U& n( |5 c1 _: y    case 90:
/ {/ w( q$ Q9 a8 I$ h2 x      abs_mode = true;
9 i  p0 X# f* p: G1 P+ b5 e, p      break;
, o' r/ u7 Y* f" t; A0 M! ?% W( H, Z0 L
      //Incremental Positioning
. x. k( _5 v" N    case 91:
8 m) m7 d3 v' [8 X4 j/ L      abs_mode = false;
5 F4 P! v9 t( ?2 V$ {; ~      break;% O7 o7 ]4 K+ D$ z) W4 L+ n

. b: m' p1 s( {" c: a7 ]      //Set as home* P; Z/ A: Q+ O; Y0 H8 a3 D- a& b
    case 92:
, `. R8 N; |% T4 Y     
- h& m0 x4 o! Q      set_position(0.0, 0.0, 0.0, 0.0 );& i6 ~! ?* ]7 Z: a/ F
      
* y% E- `. r  T7 r& B( T      break;
( n: n$ R6 B( c& @8 S1 k
) B* i& X  s9 y* I& {6 C$ J- ?      /*
7 `6 I' k4 j7 n( {7 u! N$ M  G                        //Inverse Time Feed Mode/ B3 u8 V8 }9 \- k' Y; d
                               case 93:6 X# j1 U8 k& N3 o: q
       ' K! b  G0 r9 K* S) ]5 \4 `
                               break;  //TODO: add this
3 n4 Q* Z) r2 B4 @; o5 d/ W# Y% J  {      
7 W8 T2 B: S* p                               //Feed per Minute Mode
' K! X# n- B3 m/ ?                               case 94:
7 u3 x# A2 P1 k2 ]      
2 G! y% A! M, b0 q9 o% C                               break;  //TODO: add this2 a' ]4 v/ \3 J& C- \
       */% I1 t1 j" A4 }. _: E% d  O
, W! F1 \! ]( v3 l( [# y" a
    default:
; Y/ j9 \) _0 z# D      SerialUSB.print("huh? G");7 G8 }6 M. p; q7 K& T7 w; K4 @3 _) R
      SerialUSB.println(code,DEC);* n$ L: `3 f, Z9 z& V  Y
    }5 J: }7 q/ N! }2 C6 f
  }4 l9 X7 @( e/ x# t

4 Y- f2 `5 `% d& L  //find us an m code.
7 [% Y4 {+ f. u1 `" J& |8 r& G8 ?  if (has_command('M', instruction, size))
$ F/ [6 @2 b. n: E  {
$ s8 v6 X/ T7 X/ G4 b3 w    code = search_string('M', instruction, size);
# q" k7 B3 `5 `    switch (code): [, I- T; ?" z5 t7 s2 ~* p! x3 O) [
    {9 D& R. ]8 |4 b" m- M
      //TODO: this is a bug because search_string returns 0.  gotta fix that.. E7 A8 j% U, f3 i; {- |
    case 0:# i. `, H3 \1 H2 s; ~
      true;
9 w) r3 A! C( x  v% u& ^# D      break;: y( x- Z# ?; }0 t
( Y! [7 k2 x) J  s# f2 j1 A: B
    case 100:
% o: ~) K5 p5 i# Y" |! \* {      break;7 @2 }4 _( F7 N* B1 \
& Z" p0 a6 m# t
      // fire camera relay- [8 L9 A% O; h' a' G/ _
    case 101:
, Z  ?  ?" a) {4 H; \+ F      camera_shutter1();5 W! @3 l* ~# R% e
      break;2 l! ~/ m& s, |; R0 n
& S) d' I: G& F
      // fire camera relay2
; @# i3 P4 Z/ e- s. X9 Q    case 102:
$ r' P' K7 h4 y, h- E3 {2 A      camera_shutter2();
; r# }: k& `5 N/ z9 F+ ]- }      break;
" \4 m! Q- t7 G/ k/*
+ X7 H; \3 o/ Z3 C+ {# O" O      // turn aux 1 relay on
# x: W" }+ q4 H" F7 ?! q# L3 i    case 103:$ G7 K( g8 T! W0 S9 \* _, K7 n1 u% J$ @
      camera_aux1_on();) G" q# A" H1 R( Y  S  D1 D
      break;% D9 d! B! ]1 v) j$ u

8 J) ]3 V' _9 \+ W7 I      // turn aux 1 relay off
, f# Q+ B' y5 l; Y3 r2 Q* |    case 104:
1 ^; v6 o* ]/ N9 ~      camera_aux1_off();
% `2 c" Z' N6 w0 U1 n* ^" e      break;" N3 j6 I7 Q$ u3 ]3 \- C0 i5 z6 f
! x$ a9 G3 h1 L! r
      // turn aux 2 relay on
6 U) k4 u# `( D9 H% t7 _* N. u    case 105:4 f* X) O2 p# ^4 }2 Z
      camera_aux2_on();
: `$ c1 P) i/ C' l  s3 u7 j1 U2 C' N      break;
/ d4 }' P+ ]5 Q' J3 M. |, |' t0 b( Z
      // turn aux 2 relay off
& I+ ^* z' F/ W/ _* f4 G- r    case 106:
# {& ]9 F3 ~4 E/ F+ w" W      camera_aux2_off();
4 a3 B, a# [! f, V      break;' }+ `9 d) Q3 q  B) g! q
! x$ J' n2 v& F3 n: N! x
      // turn aux 3 relay on. P6 [2 ?+ Y6 F. {# u1 _6 Y
    case 107:
0 x7 V  D7 ^! @2 p      camera_aux3_on();
/ K2 g' M+ x, K. k* [      break;
2 N! J% S) ?6 L' w
+ e" t* k2 A: T" X* C" D      // turn aux 3 relay off
$ W" H7 X3 H" @# x* j    case 108:
: P1 {2 O; B" U0 p      camera_aux3_off();$ w% T6 S' m4 `
      break;  O5 E! R( C- \# R1 T/ X

" ~1 h1 \5 u7 @! j" c) b      // turn aux 4 relay on
* Z7 u% U. W1 k4 C# T    case 109:
+ J3 L! l6 t4 A. v. I0 e      camera_aux4_on();4 f0 g8 @" }- r$ x' i+ _
      break;4 P1 D  O4 D; X' e! [2 n

; H$ _; }1 i8 L$ d/ o. L1 O      // turn aux 4 relay off$ ~1 V% f% H' s7 J
    case 110:8 D2 G5 ]' d9 N8 @5 R$ T1 s
      camera_aux4_off();
; ]0 t$ n5 t* o8 _5 z3 B4 W! k      break;
8 P8 {2 G, J0 u5 F9 K6 w( ~*/! r% R  x3 S) I) ~( T
    default:) Y# A' H; i$ d# W" K, [

+ _$ C, @# q* M      SerialUSB.print("Huh? M");
9 b+ H5 z1 C- V      SerialUSB.println(code);' i  {# ?0 ?8 Z4 ^' f
    }
" Q# E4 j$ c. S. j8 f- E  }0 f8 Z  I; \7 U4 _7 n2 u& J" F

$ a6 P+ a* R' g( c$ d% Z  //tell our host we're done.
3 R7 P' a% E& T9 t+ \. B% P  SerialUSB.print(byte(78));
) E1 m$ R5 h; u' @
4 o# K. \! E& U1 E# I2 C. r: l}
# D6 \# M& q3 [' [2 i* o9 }/ A$ Y6 w+ \: N
//look for the number that appears after the char key and return it/ l  o9 M$ ~. h9 p+ v
double search_string(char key, char instruction[], int string_size)! _+ B" T/ q3 T0 i* k/ Y
{% c2 }$ D! W& a+ n. u: p& f' C: m
  char temp[10] = "         ";9 x8 a4 N8 }* I3 T7 F2 C1 x
  for (byte i=0; i<string_size; i++)- A1 s: l# l# A' e- U+ Y
  {
9 ?2 a$ B# P, U* h# @* j: `5 z    if (instruction[i] == key)
/ ]7 d9 b' |& ^5 O    {1 o8 q" V; H( u# o1 m4 b# K% P3 O5 f
      i++;      1 O* n+ h  S& t) Z/ }- [
      int k = 0;0 T  H' ~* F7 U/ y9 r
      while (i < string_size && k < 10)
. v0 a: U: L) k  e- G* k. x      {
" r0 Y0 [) ~! |$ _' C        if (instruction[i] == 0 || instruction[i] == ' ')
' e4 Z/ L# k- ^% h) v2 J( `          break;
9 F) L: E# O. H; f  }3 ?# C) Y- N# N  w; t
        temp[k] = instruction[i];0 \, ~$ b8 {9 I; i
        i++;, d2 G5 I6 Z9 h1 a
        k++;( n  p3 W; F8 T7 P. @% r
      }
% J. S0 G! J& d# X/ i2 a      return strtod(temp, NULL);5 Z" B  y" h9 P3 F1 \! P( h, W1 g
    }+ s) Y) c3 Z4 d  E; O
  }2 d- ~" _0 x' t" D

1 K8 \3 {7 h, \4 ?  b  return 0;& @" X/ E, S3 Q
}' @$ X! B( n- I' A( h/ z' K% x( g

6 Y  J2 v( s! K3 b//look for the command if it exists.
1 Y$ ~" K' |0 E6 ?bool has_command(char key, char instruction[], int string_size)' }5 q6 a7 A& _
{( N9 N! u; G: y% ~6 ~! ?' g
  for (byte i=0; i<string_size; i++). l& m3 G/ M( I+ C0 d  V
  {
6 t5 C7 b/ H9 R    if (instruction[i] == key){
: e: a* N1 b4 `$ V# d: P$ }& I1 ^% X8 X
      return true;2 w! X9 i7 Y/ a1 M# j
    }4 m2 f9 x; X! P, E
  }
% R, x9 i8 a5 D, w" y' C7 c- ~; ?5 ?( f% q' i2 T
  return false;) [* k" P: {  y' P+ t. p0 l
}. T" E! a8 C; T. ^5 T8 C
2 O. ~. [6 M* R# J" H

5 W' ~  z6 B+ s  _4 F: G
# ]6 g6 S1 d# Z4 y% |
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层
3 H5 Q8 D3 _( N* s6 {, q7 p
//init our variables
8 K) {" v, z. Mlong max_delta;; `0 B5 k' G2 C, E/ T# k/ v/ s
long x_counter;
/ R7 g5 V" K$ ~$ \. Elong y_counter;
5 C; j( E) Y1 Z* mlong z_counter;% ?# Y' M7 m; O1 W0 o3 t4 o# T
long u_counter;
1 k& W: C# l0 s; \8 x- I% r7 jlong x_pos = 0; // x position in terms of absoloute motor stepps
% U" u& D% L) U* Z6 F. f8 _! Blong y_pos = 0; // y position in terms of absoloute motor stepps1 f5 R7 g, V  G3 I5 J8 r
long z_pos = 0; // z position in terms of absoloute motor stepps
' T5 J; g: b+ Xlong u_pos = 0; // U position in terms of absoloute motor stepps( h0 y/ h+ i  s" [9 E4 {
2 v  h+ ~& u  }3 h
bool x_can_step;
* w5 h( C$ P3 `/ Mbool y_can_step;5 [. S/ W& u" h4 B1 ^' N1 d; c
bool z_can_step;
7 k- \) N9 J- Kbool u_can_step;+ m5 p0 B  D* Z& ~& i, a
int milli_delay;
: l5 b5 j: f: I. U" R) h
  L  K+ U6 ^! ?4 M8 y  K0 bvoid init_steppers()
: L& J+ F* ^& ?) b; l( {) S: |& O, F{3 K1 d* W1 K' a  p) Q( Q8 O* r
  //turn them off to start.$ Y/ p! |! K( }! _" }7 ^
  disable_steppers();# y$ Y* e( r$ i+ J  G8 P- `

/ Z* M: g- a0 |, R' k( Z1 I  //init our points.! N! Y1 B- K& {5 {8 h  P4 D
  current_units.x = 0.0;$ Z0 e7 ]$ \' E& B! f
  current_units.y = 0.0;
: P3 E8 ^! w8 c! a3 u0 U4 Q; V  current_units.z = 0.0;! E/ K% ^0 H& w/ |$ S4 S. _
  current_units.u = 0.0;; @  K0 ?) d- `' B, R3 r
  target_units.x = 0.0;  J3 S" l9 `- Z% K2 q
  target_units.y = 0.0;) b% Z1 l. W# ^* C7 q) @
  target_units.z = 0.0;; h) N9 I0 y. Y2 k
  target_units.u = 0.0;0 w+ ~3 V( R) w! |3 ?+ y4 r( w
  
+ \7 t0 Q3 k6 M9 W4 e, v2 b0 J: }6 s! \, a2 r- [8 l# i
  pinMode(X_STEP_PIN, OUTPUT);3 y$ P8 O% ?( @
  pinMode(X_DIR_PIN, OUTPUT);3 P7 c# k+ j; ]1 ]
  pinMode(X_ENABLE_PIN, OUTPUT);' N+ Y  o& C$ p' n8 R' S) B
  pinMode(X_MIN_PIN, INPUT);' Z9 ^- Q1 [' Z6 M1 p' [
  pinMode(X_MAX_PIN, INPUT);
. k; }' z$ {- ?1 a8 a# M5 E8 K' u- S# F9 I6 N
  pinMode(Y_STEP_PIN, OUTPUT);
  d9 O, {6 p5 D( v  pinMode(Y_DIR_PIN, OUTPUT);7 d2 q. _, F% ?+ q
  pinMode(Y_ENABLE_PIN, OUTPUT);
, H8 a. Q3 p* a2 ^' U* E  pinMode(Y_MIN_PIN, INPUT);
7 ^8 k0 s* C. a" m5 g6 j0 U  pinMode(Y_MAX_PIN, INPUT);
8 [, f( o8 W0 K# T( g1 M% W0 G# n8 u2 F& n  @
  pinMode(Z_STEP_PIN, OUTPUT);; h7 H  d# o) P9 a4 p9 T4 }
  pinMode(Z_DIR_PIN, OUTPUT);! E4 W7 i! b! L  X/ l, Z
  pinMode(Z_ENABLE_PIN, OUTPUT);* u. t0 d0 x  _5 E! {  A& v( i6 m
  pinMode(Z_MIN_PIN, INPUT);" ^$ k  D' L0 |4 Q
  pinMode(Z_MAX_PIN, INPUT);
7 x3 r1 f2 e% k2 E8 e
. v( @, P4 H3 E  q  pinMode(U_STEP_PIN, OUTPUT);
9 M( o2 F1 }& l. A8 F0 O; q  pinMode(U_DIR_PIN, OUTPUT);  F# o$ D9 V# j5 z% U
  pinMode(U_ENABLE_PIN, OUTPUT);" s# Z# e2 A$ o+ ?- G  I8 H
  pinMode(U_MIN_PIN, INPUT);
& f+ v) g7 v+ a8 t: a  pinMode(U_MAX_PIN, INPUT);, S2 l  v0 G; m; U) O; e
  //figure our stuff.
" J. O2 s" u$ `8 |4 j, }  calculate_deltas();
; E2 \, M0 y- ^" B}
. S- a7 r* Q4 a; m
' z% P/ Y$ B1 g  Q' f1 B8 C% Rvoid dda_move(long micro_delay)3 o0 L: }3 n& C; Z4 [
{5 a# j8 w( U0 c7 A8 a3 g% X8 E
  //enable our steppers3 A1 e  O* @+ ~
  digitalWrite(X_ENABLE_PIN, HIGH);# v$ P. Q1 V3 F/ E6 _( ?" ?
  digitalWrite(Y_ENABLE_PIN, HIGH);. {8 n" L: a' n( U* q4 x) A
  digitalWrite(Z_ENABLE_PIN, HIGH);
- D5 i8 O2 Z' h' y  digitalWrite(U_ENABLE_PIN, HIGH);
8 O3 G  |1 y4 A" q& F1 B7 G  //figure out our deltas
! i6 c! L& ?7 n% n  max_delta = max(delta_steps.x, delta_steps.y);. d: y  i1 `" f" h3 @; g+ b
  max_delta = max(delta_steps.z, max_delta);
" {2 c* P7 K+ b& J5 k  max_delta = max(delta_steps.u, max_delta);
% Q1 @1 \+ U+ D0 E! j/ Z8 E+ z  //init stuff.2 q2 \( i' q+ M! h: o
  long x_counter = -max_delta/2;* W: G4 }. X- M: y
  long y_counter = -max_delta/2;
' W) \% x- k+ |. }. k# e3 ^( \  long z_counter = -max_delta/2;
1 C: G4 g; k8 p9 o/ D. ^4 n$ B  long u_counter = -max_delta/2;
; P& }3 Q/ E% H  u1 T
& A; j* ~1 f' l2 `* Q5 R# S9 x3 B  //our step flags
, S! j( O2 Q5 N" ~: I( R% C; q  bool x_can_step = 0;
8 J4 l( C. `  N/ y) t  bool y_can_step = 0;2 @7 K) A$ B- ?( I& V0 v) y9 \
  bool z_can_step = 0;
0 P* P. I7 b6 a: r5 V  bool u_can_step = 0;4 t. ]* v8 f: `6 ?/ s5 ^& ?" c; o
& g6 b- C# V; C4 a4 s& U$ C8 c, w
  if (micro_delay >= 16383)5 ?! ?! F0 Q7 L* G6 Z" I  y0 l
    milli_delay = micro_delay / 1000;
1 H! [# D8 I, @" ]/ [  else
4 a' S7 r* c3 z0 `. p9 B    milli_delay = 0;
2 ~  W. A; d( |' ?$ B# n$ }6 @0 G8 W. M

9 Z, p$ x/ u: J4 P  //do our DDA line!! c/ d0 k, C. x1 A+ l3 ]
: |+ x7 o# V- P% s+ n+ G
  do. n! G9 l+ N. t4 x
  {
& q4 z- t% [6 s( K    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;  [5 r. [4 U: X, q  z+ Z0 r
    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
8 q) i& U" |- y    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);
" ]0 z- C/ w( @! ^    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
7 `: |( t" l# C* C- d  H: _    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);
$ v) _7 D+ d& z: Z* K    if (x_can_step)
5 M  k! ?1 M' |) l4 f3 b    {7 ^7 ]0 n; p  G
      x_counter += delta_steps.x;
5 |* L% a7 d5 x8 l: a2 l5 Q5 k: q6 ^! n2 F$ r: {) \8 F! h# S
      if (x_counter > 0)
; N- P$ }' z7 I0 W0 W      {
9 W; W4 K  x; v: v9 p        do_step(X_STEP_PIN);8 W; L9 e2 m0 ]1 h0 \$ Q6 {! Y
        x_counter -= max_delta;
; O8 d. B1 J* G- J. X+ X) x        if (x_direction)
9 N4 L& _: p( d- N' s) d3 |6 D0 {         { current_steps.x++; x_pos++; }! W9 A/ M. i% e( T/ L% T) z5 h3 u
         
$ F. S" g, m8 s1 N; D* k/ o5 W* t        else
2 G9 Y# Q5 e1 O  h7 b          { current_steps.x--; x_pos--; }& x" H# |6 w, a# I% B" T% I
         
( D# [8 h+ Y6 T3 F      }
/ G$ ^* Z8 A9 {( U0 t. [- p    }
( q! r' a$ R' S& `) u7 W$ C    if (y_can_step)
- j! p- {" ~; B# C    {
) S- D  z, W  g: m      y_counter += delta_steps.y;8 v% k8 F; n/ K, K$ F' O

) E" F! @8 |2 X# _, m- w      if (y_counter > 0)
( Q0 K) j, k4 U      {# Q$ R4 ^; b& U; l4 \; D. P
        do_step(Y_STEP_PIN);
  W' t' V  k, Z% }& T( z4 w0 _: n. c& I        y_counter -= max_delta;
" |2 ?& w* S+ [' p1 f# F& i
1 V4 g7 O  C$ I$ U" C* z        if (y_direction)
8 A9 S" m& S: i. {( d4 [        { current_steps.y++; y_pos++; }
: I9 \! h: q2 B0 W6 k1 J- h) e      
" ^+ A  D* h" D3 D        else4 e, f: \8 m0 N
        { current_steps.y--; y_pos--; }
" o4 l# l' x& }8 f2 h        
0 y. V, ]  I: F( l  T* _3 }7 S      }
* {' a6 A, L' Z8 g# V    }
5 c1 Q& m1 [% |& Q( A& M2 B/ V3 n& O# h6 |  d- {* {
    if (z_can_step)
3 @. j7 j2 s* J1 Y    {
% i# |. X* r2 J& ^. Q      z_counter += delta_steps.z;3 [- ~/ c3 V, T* U) Q1 t5 {
" ?4 M; u. J* s6 v, Z; N( F. o
      if (z_counter > 0)2 u8 V7 [* n9 I2 Q
      {% M  e0 c9 f9 T" {: P2 z
        do_step(Z_STEP_PIN);' X1 Y5 U0 q, b# L' ?0 y
        z_counter -= max_delta;/ F6 y- ?  N3 A( u5 `! z
2 o9 N# f5 r  X. p% {
        if (z_direction)' j. W3 d' ^% X" _# r* L0 i
        { current_steps.z++; z_pos++; }
+ K, V8 u  e5 K" a7 O: o% q( ]        
" V) K5 F& _) _+ C! |        else
1 V8 e3 X. x- i: s        { current_steps.z--; z_pos--; }
: o7 v& Z3 H( o2 ~( Y        * T4 N4 j! t, S9 Y5 m
      }
, t% |1 x6 j. o2 R1 R    }; j& F9 y3 C4 f, h! z* ~1 j
    + z- [5 F8 |3 X+ s2 S6 m
    if (u_can_step)1 y1 d) R4 d+ T# z; @, j
    {7 ^# c; ~( s1 x! a& k; J+ g* B$ k
      u_counter += delta_steps.u;2 L9 ]+ f* z6 G6 r' D& U6 w

, Z3 v2 b5 n, a* h" H8 _      if (u_counter > 0)7 U) W; A, F/ M. X; v3 A, w
      {2 c5 B; V6 j% ]8 @- Z. }; j  h
        do_step(U_STEP_PIN);& Z1 F$ U" ~# ^! j% E
        u_counter -= max_delta;
+ z. e, [0 Z5 l! b
7 M0 t0 q" T3 q0 X        if (u_direction), r( y$ e8 s1 e2 @7 |: ~4 s
        { current_steps.u++; u_pos++; }- `0 G& m8 ?1 x) y, S4 n4 e  ^
         
2 [" k2 p/ w' x; f        else/ o  H9 D# i! `# w+ ^
          { current_steps.u--; u_pos--; }
) l4 l# s: Q' m4 X: |; B" k         
; d1 O2 b, d0 J$ c3 y& ~      }8 F$ Y1 G0 z& R9 b7 p
    }
$ H% \4 _% s) Q% u9 y    //wait for next step.
; c; ]* D# _% L' ^- T" ?    if (milli_delay > 0){, u: K% c3 H( h) [( _' Y, ^! q7 p
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
  k- ~1 [: L+ g, U      delay(milli_delay);8 u) ]' b6 E, k: D- c
    }               
- u$ w6 z0 {4 `+ ^" c  G    else{% g. q1 D' n# ^4 Z+ x; O! q/ f; _  b
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
+ n. W6 H8 m  L' i/ g4 F( P      if(micro_delay>0)delayMicroseconds(micro_delay);4 [8 i, d8 q0 G& B! L
    }
! ^* R( P0 P6 _7 h* Q    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
/ `  q4 ]9 V6 p# N2 M( ~' v+ l3 w, l  }
! m+ p% z' l  o  g  while (x_can_step || y_can_step || z_can_step || u_can_step);
1 I% b" G. ^0 t! m  T3 t# K2 w$ L4 a0 n2 V+ r; q
. _- p$ F; b9 i7 ?3 ?7 ]% i# |
  //set our points to be the same
  c0 b) p. }/ _; O: Z  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
" E) ?# e8 g) c# R: B, Y! `  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;4 T) H$ D  e$ Z0 @: h6 d: q. N
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;. L$ p( _* O2 b3 l5 v$ b7 r/ P
  current_units.u = (float) u_pos / U_STEPS_PER_INCH;
, N+ W% m) a4 I/ R" \  
- T( Q( s- U( ^, f/ R2 O/ N  set_position(current_units.x, current_units.y, current_units.z, current_units.u );
0 N. g& z2 z6 r; N6 r5 }6 g" b8 Z; h
0 c% `5 }& K& o6 O( `1 S* _# g) E  long x_pos = 0; // x position in terms of absoloute motor stepps
, M2 g6 i$ m) ~% T2 C; V  long y_pos = 0; // y position in terms of absoloute motor stepps' ?& K4 H- }$ y6 T
  long z_pos = 0; // z position in terms of absoloute motor stepps
) \+ c$ b0 n' J# u: z3 y* w  long u_pos = 0; // u position in terms of absoloute motor stepps
: B" @  x! z0 X5 L- o2 S  calculate_deltas();
7 s0 D6 x' C# \; Z; k  k1 h  - N$ W5 t) ^! e0 G: C
}
- a2 j8 R* [! ^) k* x% V
2 ?$ z1 _: b: j; f) gbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)' y! p! u% V* J1 ]6 ]+ y3 R, K
{
# u7 U+ o& A5 e  //stop us if we're on target+ C9 }2 a0 S3 r& P, O
  if (target == current)6 x* U, O7 H% N' i. M. W* M" d
    return false;
# n1 r* I+ ^- ^) B  //stop us if we're at home and still going
0 T5 C8 _8 V# l& r0 `5 y0 H4 g5 {  else if (read_switch(min_pin) && !direction)/ }6 d+ Z& H1 p9 r5 C, r$ Y7 l
    return false;
1 D# I2 H; a2 b$ Q% g+ P, e  //stop us if we're at max and still going' u/ n5 ~+ @4 [' L5 g0 T
  else if (read_switch(max_pin) && direction), l) q& l7 ~4 N
    return false;9 F$ P/ i, o. k) @- `
- ]9 c0 m: s  m; ^
  //default to being able to step
4 D: H! ~5 Q" V9 l8 M$ j8 q  return true;
5 k6 V( ~- J  q2 t# _* a0 c}. g" e' @& e3 t( Q/ B
0 Y' W2 U# r" M1 A! S9 v: V. |% P
void do_step(byte step_pin)3 O: ~/ t( \0 M8 v, |% F
{
# ^) A; {! `* f) ]6 ^2 [" R6 \  digitalWrite(step_pin, HIGH);
" y0 J. `# D# ]# f9 r  //delayMicroseconds(1);0 D; E' D9 H: _( c
  digitalWrite(step_pin, LOW);
. n  |# A5 M& w7 @, c! v; q}
! c, t9 a+ C2 j2 Q0 j$ A; u
, G$ z! M9 }# M1 ]' z% n, ]& g7 hbool read_switch(byte pin)
0 N$ }- L: M4 C" N2 P2 f{
1 M% w$ z. C" C( H. b, w  //dual read as crude debounce
& g( y1 V& V+ w3 |  |; q4 [5 ?% U8 ~
  if ( SENSORS_INVERTING )* _5 D# [5 Z) I9 h1 O$ c
    return !digitalRead(pin) && !digitalRead(pin);0 t$ B/ G  l, h, Q9 H
  else9 Z: ^7 a3 _4 e; I( x( N
    return digitalRead(pin) && digitalRead(pin);( m( o6 ?8 E( }- K' z" f* a0 g
}( {3 b: T4 c* d

/ g+ a/ a/ x) m# e1 n* W8 Glong to_steps(float steps_per_unit, float units)& n/ M2 O! x1 N" U; b& @# l! D, T7 _
{
  c6 {3 X8 z- d* z1 G  return steps_per_unit * units;
" ~9 \. M7 K& K7 Q}
% a  P9 \6 }& [1 b- z  f$ J8 ?) \8 y4 p. D* O! Q
void set_target(float x, float y, float z, float u)
! W/ p4 H' o8 T5 E! O{
$ ~7 p, D7 ?# ?) W! [  target_units.x = x;! M+ y6 p; X: X/ q# X
  target_units.y = y;
7 ]* U+ S9 ?/ c" z  target_units.z = z;
3 Z$ b1 t0 |/ }6 s" C4 e  e  target_units.u = u;
8 J! ^8 e" L4 I& w  calculate_deltas();; L, ]! A& Y; F
}
, _" D$ b5 ~% ]# D% y" K# Y" y
/ ]! n# h/ S9 b8 c( w: l+ avoid set_position(float x, float y, float z, float u)8 t3 z- W2 x3 s2 O
{4 p: i- t! E' S& ^) H
  current_units.x = x;; {7 r, U$ r3 V, x8 |
  current_units.y = y;
$ F- e$ f# g+ [: d) y7 R" p  current_units.z = z;
2 ~- L& l1 v8 W& {( s% ?. \6 G7 C  current_units.u = u;
3 D. Z" ^# B4 ?) ~  calculate_deltas();
- S1 d4 g. g8 {}/ Q7 z3 m+ t% [# K

+ |' X2 p1 f$ Y2 a+ Rvoid calculate_deltas()! e1 `; @2 M0 o% r1 x
{
4 \1 G5 ]) ^5 z5 m8 C7 R  //figure our deltas.
# ]4 O- v4 [- L  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);
8 R0 U4 m! c+ ]  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);  n0 I. ]( k3 @0 X$ s
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);- i( J( h: S  l/ C
  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);
  \: J. j$ C) y6 y9 H2 X! L2 Y, O# |8 Q7 k  ?" i9 |" m
  //set our steps current, target, and delta% M2 L+ ~' S1 m+ Z/ T
  current_steps.x = to_steps(x_units, current_units.x);
% }7 O# |4 ^- z: M* i. M  current_steps.y = to_steps(y_units, current_units.y);
! G3 M' T4 |; a$ N- p9 a  current_steps.z = to_steps(z_units, current_units.z);
$ @: [6 @3 P  A. m9 U, V  current_steps.u = to_steps(u_units, current_units.u);
6 u* e7 z/ G# [- ~. S
2 ?9 y+ L* T! G+ A$ E  ]  target_steps.x = to_steps(x_units, target_units.x);3 `: v) q0 ?9 I) v/ z! f- R
  target_steps.y = to_steps(y_units, target_units.y);
/ ~8 U( M& O# A# y3 @1 l+ o  target_steps.z = to_steps(z_units, target_units.z);, n9 `+ ~% K$ I" q. q. N( v
  target_steps.u = to_steps(u_units, target_units.u);
% O8 e: `4 f* `. u5 [) R- Y: I5 A* d/ }3 H+ s* L  |) l- Q* r
  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);
: z1 i* q2 a9 C. N$ q5 A  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);
: a) m" Z8 `( {  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);
7 J& L  B/ A9 @2 T- p  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);' B9 X* I+ d- l3 x
9 |" w% `% l' ]/ l) l
6 Y; ?  K  z8 T& E, e, ]

: e- d1 S; V( Y/ i/ b; l/ u' S# a5 O8 {  //what is our direction
) M" C( g7 f8 ?* M  x_direction = (target_units.x >= current_units.x);
0 y) k/ M. K# X8 W( n3 A  y_direction = (target_units.y >= current_units.y);
, T3 n* D/ S# p  z_direction = (target_units.z >= current_units.z);
7 j: ]3 S! N. J# `+ ~  u_direction = (target_units.u >= current_units.u);- o! A8 P1 j3 ?* z# j1 a5 p

0 p" @8 ?% p( X" s) M5 H- @  n  //set our direction pins as well
5 ^% }) t& I; ?, C# l  digitalWrite(X_DIR_PIN,x_direction);* W3 a' b/ S; b' Y, s. G
  digitalWrite(Y_DIR_PIN,y_direction);0 t) {2 ]3 `4 \5 p- e' p* x
  digitalWrite(Z_DIR_PIN,z_direction);
2 D* P2 q0 v9 ?: p  digitalWrite(U_DIR_PIN,u_direction);
. t1 M2 n4 s6 _- ?2 |: Z+ v7 |! y, R
  //绘制LCD
. E! W  d. V% Q  D5 ?5 m; h  LCD_DRAW();& {. O6 E7 a1 X. l
( Z0 \  k& A6 Z$ m
}
" \! v4 Z+ L" g2 p( n& f) p
$ v$ n% k8 @7 N$ @% a5 q8 ?
. a) ]& T5 o, o9 B' L' r/ elong calculate_feedrate_delay(float feedrate)
% e. U3 c. w, Z) w3 L{
( L8 S# N+ {* |5 a+ Z  //how long is our line length?
- m7 [0 B) u! b  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);2 l4 r. q5 W, T6 C
  long master_steps = 0;% x. `+ e4 }' J# N& d# K

1 z* O' L* [+ b1 u  G& rmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
9 {% F) T/ Z- T3 V, h5 e, A" r- umaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
1 W) H0 d2 O7 Vmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
0 D  |% o* M% J% a2 z
" M7 E6 p& q/ A, m. k) u
0 y) m( k% J3 x& d
+ I% y6 n- Z4 t & k7 A; M) ~+ B% N! H
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.. S$ c. h: s* O
  //the formula has been condensed to save space.  here it is in english:5 z& ]0 L. z' K* O
  // distance / feedrate * 60000000.0 = move duration in microseconds
$ V; E7 g, w3 Q, N3 e  // move duration / master_steps = time between steps for master axis.( l9 M+ ~7 w2 y% F; g
return ((distance * 6000000.0) / feedrate) / master_steps;" h8 u7 _; l9 [, j+ A8 G
  M% u' r6 }) o2 S/ k
}/ j4 i8 ]' j1 B; ]5 X  ^' `
6 l+ ?8 C: Y5 S6 u6 x) {
long getMaxSpeed()' R# V0 y$ {' c5 @9 C( }% Q. s$ U& Q
{
0 b0 q& Q8 z- G7 z if (delta_steps.z > 0 || delta_steps.u > 0 )" \/ C) D* _; S
    return calculate_feedrate_delay(FAST_Z_FEEDRATE);9 ~0 l' S! B' a  e. N6 d) c
  else& p/ A* O: L% T# F8 D
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);3 D4 p$ \) z1 I, x& n
}5 f& R  M" C! r
( A# g9 v+ _3 v- g: H
void disable_steppers()
! r, p$ b( K2 L; L{* P$ `' y$ P% p1 j+ u& u; ?
  //enable our steppers
) x, b$ S' C0 c6 P: \' x# v: ], A  digitalWrite(X_ENABLE_PIN, LOW);9 \( p# L: x7 c( J
  digitalWrite(Y_ENABLE_PIN, LOW);( t- N2 b0 O5 z: t4 a% r
  digitalWrite(Z_ENABLE_PIN, LOW);& \4 b! D1 o7 e: p" w, H' N
  digitalWrite(U_ENABLE_PIN, LOW);
* P; Z5 G9 ]  Z9 ^1 u6 V}, V2 u0 J' s  i: `& i
. T0 Z! h: s, X) b

1 x- y: R, f; O) l' k' E* l4 L. ^" E//绘制LCD, F9 X: N) ~3 M0 u* n3 f0 }) L% G" W
//unsigned int DRAWCount=0; 3 f( S2 S, G" h* F: a; J/ U  l
void LCD_DRAW()
; [9 {1 B$ {7 @4 P* U7 O6 ^{
& c2 W: u- j! ^9 L; n4 @" t6 h- C    lcd.clear();
$ Q$ g, E0 b1 Y    lcd.setCursor(0, 0);0 }. {% N4 k0 E& T" i; n$ N
    lcd.print("    X=");/ \. q9 v5 M0 H+ ?# W- T8 B* {
    lcd.print(current_units.x);
: z0 Y3 W( i$ j: U( G- i    lcd.setCursor(0, 1);* N" O% P- a: V
    lcd.print("    Y=");4 q3 @( Q1 i- ^
    lcd.print(current_units.y);
7 n0 V' i8 [& a% [% ?- J6 d& D    lcd.setCursor(0, 2);% r+ E! _5 w% m- K* v6 r
    lcd.print("Z=");
: w! U6 L8 P. ]7 x; N0 q: ?    lcd.print(current_units.z);2 h5 y, G  m  I& F# P/ U0 D1 S
    lcd.setCursor(0, 3);9 [9 t7 |  D" ^( V- b! ^& I
    lcd.print("U=");
% y" E& u6 k# \4 p& N    lcd.print(current_units.u);
% g6 k8 d7 r& A+ i8 `; C) U  }& A0 g' g& v( Q8 h3 y0 x" ~: h2 ~
// else if (DRAWCount>=30)DRAWCount=0;
- J" [  p1 x" V" z$ X8 |1 @8 v//  else DRAWCount++;
8 m4 P! g9 M. Y& {) @0 H$ f: N void SPEEN()
( R/ r! z8 o  F" |1 T {
- Y2 @' H3 [4 B7 ?7 M5 h# X" e   delayMicroseconds(analogRead(SPEEN_CTL)+1);2 F) V: @8 X" ^  d
}
) g" o; |6 w4 M" A7 r3 ?7 k( \
- |6 P0 Y& E( m7 X* }# D; g
//delayMicroseconds(analogRead(SPEEN_CTL)+1);1 ]: k% u: H  n" h% R3 V
//if (digitalRead(BUTTON_SP_EN)) SPEEN();8 q! h2 ?' o3 t6 o5 ~( u! k, t

) z4 f% D/ P- m
8 ^/ l& x, j2 R/ h5 p( O
回复 支持 反对

使用道具 举报

 楼主| 发表于 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-11-24 09:29 , Processed in 0.073100 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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