找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 24511|回复: 14

运动控制入门篇GCode_Interpreter

[复制链接]
发表于 2014-5-10 09:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xmdesign 于 2014-5-10 09:42 编辑 & P- w5 o$ x# D2 C* F% o6 w& ]8 u
% A+ M' A4 n4 s3 J8 V# M
首先声明
! ^, s2 e+ }4 q1 G5 j/ E我不是专家,业余捣鼓
6 g% U( k4 h2 O* _+ j源代码首先来自网络: ~; Z+ r' l) P' h
开发平台Arduino及Maple
$ X* q) g$ J/ X: y/ M  K2 yhttp://www.arduino.cc/$ @$ P+ i/ f2 N* H& ], u' U
http://leaflabs.com/devices/
  E, ?' s! V3 {国内活跃社区
3 i# J" h  I% }2 s1 }) thttp://www.geek-workshop.com/forum.php 8 U& \6 P; |8 q' H

  [3 C( ]. k* o8 G竟然缺少积累,首先寻找最大的开放式开源平台,这样可以积累全球范围的创客和DIY者的脑力活动经验
& W. Z4 z- W( X- z; e  N  D来入门了解熟悉思路架构等- C( [8 r1 U+ [7 Y! ?' d+ ~/ ]

1 d, c& G) O# n# |我只捣鼓了下8位机Arduino 移植到32位机Maple 方面一点点事情,
! X2 _8 e0 ^% S, g许多功能还木完成,不过作为低档次得应用可能还有可能, a9 a0 V( G" d3 D( {0 y0 v* g) K
0 t& }( o. O* ~8 F
我自己也是个半桶水,再乐意玩的起码自学能力还是要有点吧' R  z4 \  K9 O& M
7 V; F5 y1 |& B
拒绝 所有的求
# s* }1 ]& |) Q' A* C1 F0 l/ V求人不如求自己 不然木玩1 r9 T% }8 {/ _: }
, Y7 e' j- E. @2 z: }# S! t
高手绕道 谢谢!1 @+ k: K6 H" o6 x0 e" I( E
0 H7 m& U* K9 ?! `+ ~
回复

使用道具 举报

 楼主| 发表于 2014-5-10 09:48:37 | 显示全部楼层
本帖最后由 xmdesign 于 2014-5-10 09:52 编辑 : S5 |  z4 m5 P
! O9 O7 X4 V4 U, V' {
GCode_Interpreter是比较容易懂的,木那些寄存器等虾米开始不容易懂的东东0 {1 S. _/ h5 y3 i5 U
贴代码先% O' b& O0 |% f3 Z9 n: t4 W
直接Maple的,某宝许多超便宜哈2 I# N2 a4 \4 y4 n
晕,我怎么上不了RAR?
: u" U$ t* E% {$ V4 J( A想玩的留下 e妹吧* M; _; p& {3 k* e- ?5 d1 M7 ^
第一个妹麻烦传第二个妹哈
% f7 r/ o& E6 }我平常杂事多* d' U4 y: B  r/ y3 s/ D5 O
谁放网盘上再,麻烦开放下
 楼主| 发表于 2014-5-10 09:52:53 | 显示全部楼层
// Arduino G-code Interpreter for Rep Rap- a/ s# Z; I9 t" V
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
6 n1 I; N1 g' z3 d// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)9 h2 ^6 w7 K' l' H
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
% A+ Q/ ~, }9 e& \& N// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
+ i/ ~8 J  z) B' M) ~( L$ ?. a7 z4 E! q( C' ^! P; @- g4 f
// Arduino G-code Interpreter for Macro / Micro photography
) D, Y2 ~$ C& Z  z// v1.4 by Gene Cooper - modified and setup controls for macro / micro photography (gene@fourchambers.org)
" Z" G- [" [: f//modified by YaoHan China 2010.2.15
+ C' S# V, Q4 z8 G  G$ F//modified by JiWei China 2010.2.22 (jwdesign@163.com,QQ:75990175)6 f! k0 V& h2 `% v2 _1 k
#include <stdlib.h>5 _# h; m0 \1 o0 P# n% z2 X
#include <LiquidCrystal.h>! x+ I; a, ?- F5 N7 q9 y

- i+ t$ g) x; B& ~3 L/ k0 o& U//启动(高电平有效)/暂停(低电平有效)  默认高电平. m7 b8 U( y! D" B" {/ s
#define BUTTON_CTL 35
+ p1 M5 ^" G$ ]1 ^7 A
' \; \! x# i: N8 j4 B//点动模式键(高电平有效)  默认低电平
. a" Z$ L. P7 y& Z. [9 v& |* p#define BUTTON_SS 36! x: M7 j9 a! {3 t
1 V& T' c/ b0 k, ?8 Z! N
//点动键
# J0 t% p; f. S7 I8 U& F6 K1 p3 E#define BUTTON_MAN 152 v8 J5 a$ A* \( k% x6 h: J1 q1 z
) I+ q9 \9 L0 k7 A
//电位器速度控制
! ~' V7 k9 G0 L5 F' n6 d& l. c#define SPEEN_CTL 16& @( Z: O* Q5 A( A  l

! z8 s# n/ f4 ?' s& `7 k/ L% h//手动调速使能
- V. U1 Y5 k2 G$ g- c/ I#define BUTTON_SP_EN 17
9 J. k$ ]5 w# z. c/ M
- p4 H& O+ d, Q/ h% N//LCD 类型 1604或16025 m; W) v+ V) U! s
#define LCD_TYPE 1604
: G) q9 k! b" ~' X) f: A& Q//LCD 引脚定义
$ _! z) _5 X6 h3 S; w: m2 \3 ~: V#define LCD_RS 23
: f# L, I/ O& d7 z& f: N#define LCD_EN 24
: [" {# k  l4 O" K#define LCD_D4 254 h( Z9 N& p8 o9 f: ^5 G) Z+ m
#define LCD_D5 268 C6 t1 ^- n; B5 ~" y
#define LCD_D6 27
+ f& }( n. W" o#define LCD_D7 28+ }% G- W3 p% P& z1 Z# H9 T
! y( z+ t3 t1 T/ W0 p
//命令字符串
2 [5 E5 F6 `8 P8 a- n( s' o7 b- Z#define COMMAND_SIZE 128
0 n8 y* f! p9 lchar word_old[COMMAND_SIZE];3 V. \1 Y& \+ N( ?3 ~0 \
byte serial_count=0;
$ G  t7 G( c5 M! T  bint no_data = 0;
/ a' E0 p% @' s5 a//LCD 引脚链接 配置
: `: `- V; M6 b. Y- I4 _8 ]' K+ X7 ]LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);, z4 v7 h+ _" ~- {7 G# t0 Z
//停止控制,高电平有效) O) a; q1 F- C* [: z! t
#define BUTTON_STOP 30; l9 I* y4 @9 T% G$ U
int stop_flag=0;
. ]8 v6 i" B' g
/ O+ n; O) h0 O  \//暂停
" q& z4 U  Z0 {+ `) m0 gvoid pause(){  r# N3 R9 {* R# Q, B3 S1 E4 ?) _8 B3 g
  while(!digitalRead(BUTTON_CTL));
/ S+ k5 C2 J& C3 i5 Y8 k! y2 [2 ^}
7 R$ T1 ], v+ T" h. L$ g8 @
; |# X0 i9 M( x! Z. P9 V' ?void stopper(){
3 b. C$ L+ S; C) H1 S  delayMicroseconds(10);- S+ s( z" n( Z( l
  stop_flag = digitalRead(BUTTON_STOP);) Z$ B$ ~3 Z+ |7 s  L% J
}: h; r0 W8 n9 k. C* r0 e3 ~

" j' U0 {  F" J7 S3 E+ H  Ovoid setup()
3 ~! E& f! b* b" E, R, U2 a{3 J" c0 ~, O/ m# y
  //show start
: m  z8 ^( [0 ^- `  SerialUSB.println("start");
) r7 @- G2 W$ g: ]4 I
( F6 p" y3 h0 J4 Z4 K  //启动lcd
2 |9 O6 A+ F& z3 f- V. r    lcd.begin(16,4);
8 {1 ^1 A( X0 y& K& D9 N- k& V    lcd.setCursor(0, 0);  Q+ m7 N1 z. [" J3 b
    lcd.print("hello, world!");, Y: [" \0 x: l" z1 ~

" b( \/ |  E0 G4 k2 H  //初始化控制引脚及引脚功能
& Z  B/ f% V( ~6 o$ @& p3 l  pinMode(BUTTON_CTL,INPUT_PULLUP);
6 a% w3 j& q" W- j  pinMode(BUTTON_SS,INPUT_PULLDOWN);
) R! @8 x) P( [% G6 U" @* p  pinMode(BUTTON_STOP,INPUT_PULLDOWN);" m$ ]% K  f" E, L5 F" i8 f
  pinMode(BUTTON_MAN,INPUT_ANALOG);  W& ?/ c9 f. r6 h! S6 d
" _5 P+ [0 w7 q. V) M( ?7 ^; H
  //控制引脚的中断
( q9 x* I2 N) |+ B" w8 h% B  attachInterrupt(BUTTON_CTL,pause,FALLING);//暂停的中断
+ ~* _, g! q4 Z" ]- H% H6 D; }7 @  attachInterrupt(BUTTON_STOP,stopper,CHANGE);, u$ E9 R" m; @# @2 y1 t$ u% d( n* R

- o' m2 i+ C9 n, |  //other init
) h1 Y' i& @, |" y+ ?1 |" B  init_process_string();5 f- l' D' u) T3 B
  init_steppers();
+ o* S  ?. e7 r) @8 P) Z  init_camera();
- ^& ]2 v) A: h" a5 \- \* c
0 Q& b$ J& P* ]5 o}' ~3 e, E3 Q5 Z& `6 f9 R- S5 X

  L# R3 m, D! @3 K/ jvoid loop()
( z7 d5 r* l& v  Q0 ]8 f& i7 ^- l{  I/ L5 o5 L- J9 u$ ]. T- d# P
  char c;* p3 F6 z& g7 t; O* V) `

) F) Y; v* t# L& ]/ D9 R  //读取输入的字符" j, e+ b# Z5 T# \
  if ((SerialUSB.available() > 0) && (!stop_flag))- n; J$ h1 n) _2 c  R
  {
& V5 ?# f- g  ]4 f! P7 ^    c = SerialUSB.read();
: r7 S. R7 D; z- B4 m( {+ F    no_data = 0;* L  Q/ {( {& c3 u8 l
7 U: D9 s1 @0 o2 Z8 v  v) {6 n
    //换行符代表一个命令的结束
* @  h, x& h. q: o" [    if (c != '\n'). Z+ i9 J& h7 I  B$ D
    {9 ?. V/ p6 \3 h6 }: G" d9 ^: @
      word_old[serial_count] = c;6 L- L( r* c& X8 C6 o
      serial_count++;
6 a2 W; {8 M& _/ N, h1 u1 l3 Z4 x' I: u
    }
  a9 ]$ I3 e/ u( S0 @  }) {8 S7 X) ]: T# V7 s& [5 }$ X/ ^  {
  //标记没有数据输入% m8 M) P$ d8 \% L2 C" M: s3 `% i
  else
/ j3 P  U1 R. G7 @& o0 J, Z  {5 \. q- m& E, l7 H8 K5 E
    no_data++;1 |/ G0 b( B) _/ i
    delayMicroseconds(100);- @8 `& f, z# a9 l' Y4 d
  }- D) }: Y1 G% A$ n/ i

$ M* N) K) q# h( c  //if theres a pause or we got a real command, do it, |5 k" L* P; r) L
  if ((serial_count && (c == '\n' || no_data > 100)) && (!stop_flag))
6 Z" b/ O# f+ N2 U; f8 F  {
  A- n4 W0 b4 N# v% D/ m5 i% E
3 {  [. x4 S8 F3 \5 i& C% C2 a    //处理命令& u9 z, G6 h9 S$ d
    process_string(word_old, serial_count);
4 f1 ]: H) I, N/ |* K7 o4 r7 [0 f: g
0 C. v% v3 ?: f) `    //清除命令2 y+ W, I6 C1 i6 g% U' q
    init_process_string();) {# F: ^% X5 u/ }
  }. r7 I- F9 L9 f! z3 S9 R6 H

; m- ^( v7 B$ Q  //如果没有数据关闭电机
7 b7 E, o* Q* z2 R  if (no_data > 1000)9 k% f* y- Y" h. N5 ?9 @3 g, [
    disable_steppers();
8 c6 k% H  F1 s3 q' ]5 N7 L6 Q/ R$ p2 E# l( [# h
  //如果ss键按下进入点动模式
. v# L' Y/ q& P0 D  E' e# a  //if(digitalRead(BUTTON_SS)) ss();
8 R8 S7 S7 e; q. [; V  run_a();
0 Z! ~1 w1 t9 W* C1 o+ @}" y6 c; w, d; }5 z
* b; f# {$ n/ Z) F) l
//点动模式. A! E2 h: e7 A6 s% U1 g% [' z
void ss(){
; o) L% t( `, L* D4 o, M5 ]2 R; Q1 T( ^* f" Y4 G
  delay(1);; c$ f) W* u! P4 V: Q1 r% U
  if(!digitalRead(BUTTON_SS))return;
* e  {0 c( F( ^1 X, Z$ f; w  if(!digitalRead(BUTTON_SS))return;/ }% _" Q# v: J* v. z: R7 R5 w
  //init_process_string();1 G( p* O( c: B; L0 y& P
  //init_steppers();7 p% e! `6 B2 t# @; f
  //init_camera();, f$ i+ E& T/ r3 `( [/ R' w1 _1 h
9 [) ^# W$ S4 [! s* @# l
// SerialUSB.println("Step By Step Mode");" A5 l" Z5 F) ^' M" u( p# P
//#if (LCD_TYPE == 1604 )3 ^7 ?* p9 g1 Y) A
  //当LCD为1604时显示要处理的命令3 i% B! G/ ^% l( v$ S8 l: w
   lcd.setCursor(0, 0);* M: H9 L' x) ^$ w7 U
   lcd.print("Step By Step Mode");0 B" {$ }* y, t5 Q" k8 L+ A, Y
//#endif8 b& y3 m9 R: {( l  ^* S* T
  process_string("G1 F5000",8);
3 r; t/ j2 H' o5 U. P8 J' I  process_string("G91",8);. |. p* S6 g4 o7 ~
  //init_process_string();% s1 M% n- S1 y" c6 {
8 E$ D% H6 m( g: H* _; W
  while(digitalRead(BUTTON_SS)){
. c7 ?/ e4 o  I  Q7 z    int i=0;
2 Q0 K7 z4 Y5 U5 N6 T    i=analogRead(BUTTON_MAN)>>9;  `8 c9 C8 X" |4 O3 g0 ^
    //if (i==0){break;}
. U6 M# z4 r' K: P   //SerialUSB.println(i);$ h% x* u9 `/ P0 v
   //delay(1000);   ( Y7 R  G2 E& l7 S& r# Z

* y# V! X1 z1 d. l7 J2 c7 J, H   if(i==2){
: ]# Z7 r: A  q0 v      set_target(1000.0, 0.0, 0.0, 0.0);+ r$ N; s( s( [5 Y  U
      dda_move(getMaxSpeed());+ h2 B! o" s8 T4 i" Z# z; i
      if(stop_flag) return;1 w$ w" e# B6 L
      //process_string("X0.01",5);1 t3 O3 g8 r/ V  g2 y
      //init_process_string();
" x) F4 i6 x$ i; Y    }
) d) d2 o4 W; r( n  X7 a9 r9 O    else
% _0 _0 k0 V7 U1 L: R    if(i==5){
, m# r) b0 T& k7 K- Z& g! E$ K      set_target(-1000.0, 0.0, 0.0, 0.0);
/ |4 ?+ a: D5 V      dda_move(getMaxSpeed());
5 l( n) y6 d# a& D1 l$ V8 V      if(stop_flag) return;3 z/ M# ?. p- d: e- @& A+ L% }: e. y& Z+ p
      //process_string("X-0.01",6);
4 P3 T$ K5 J$ H- q. ^      //init_process_string();7 i; n! Y% r; a, G0 s. N% l
    }
, L& p$ s9 n6 P& @8 _  H/ g     
. V+ F7 F, ?2 s6 }
- Q$ z2 W: e( C4 b  c! `  }1 r0 k( e, O3 B, T
1 c' N. _& j0 b9 x
  //init_steppers();; |/ B; p7 f% s% e) ~1 ~+ N
  //init_camera();
& x1 X5 a6 q5 C8 r( D+ |# S   // SerialUSB.println("Return To Normal Mode");
0 x. Y5 U! Q; I* v9 c3 p     process_string("G1",8);
4 j: Q4 Q& G4 c# y3 S$ [  // process_string("G91",8);
8 v" p5 T3 ~$ Q! o: F+ u    init_process_string();6 F' L7 y2 k* @9 q
//#if (LCD_TYPE == 1604 )9 R8 B; y7 T. q4 J, x
  //当LCD为1604时显示要处理的命令
6 A* r  }7 R' g' @$ h' ?  // lcd.setCursor(0, 4);
" e. ~0 [( N: a2 |  // lcd.print("Return To Normal Mode");& R$ W! y0 z+ D# f: ]2 W% b& ?
//#endif
$ L% l9 L, x4 V: o' [' D}
7 }/ y' W  P( H# r# J3 Q) {2 z$ s- g% l: a' B* u3 s  w: g
void run_a()
0 Y& {; l) ]& K( k: E/ C+ Y8 {* |{
# H9 t, Q7 S3 _" n* [  //delay(1);
/ n# L' N# t& ~, ~. k$ N) c  //if(digitalRead(14)== HIGH)return;6 T9 y$ G, H4 P
  //if(digitalRead(14)== HIGH)return;
# u2 h& u6 O, G6 M  //process_string("G1 F2000",8);
& x) x5 _! W1 |' O$ z* L //process_string("G92",8);' w9 {+ b+ b4 q4 p9 w5 e- ]- ~
process_string("G90",8);/ @$ m* }5 M. [2 M4 d
process_string("X120 F10",5);3 |. O* ]+ E- M" h/ x6 n( n4 @
process_string("G4P2000",8);
- i, d: J5 b2 C& l) d* ?( A2 k' v process_string("X10 F10",5);
& c( n  |! m) }; M8 |7 X7 e2 w// process_string("M101",8);
0 k! R9 q5 J, h* H5 }' O// process_string("X-50",5);& @5 \, E5 L. _: d/ T" P, ?$ N, m
dda_move(getMaxSpeed());8 q9 J3 ]. H- _
init_process_string();
& S) e8 @) h, Z5 D( k# z
' O6 ^8 t/ ?8 s8 |+ b5 p}
: u: O9 p9 Q# A9 ]8 ^0 l* r4 a
 楼主| 发表于 2014-5-10 09:53:25 | 显示全部楼层

5 K2 c* q1 t2 W3 Z  Y
3 s4 t6 ]7 R/ p# _// 定义机器参数( k0 a5 Z, k/ g0 i1 t+ Y! P. G
#define X_STEPS_PER_INCH 400
* a. U& ]! f1 J* T  ]8 |#define X_STEPS_PER_MM   16.0/ j2 R, y3 k1 [) E7 J
#define X_MOTOR_STEPS    200
) l4 g6 R. {2 S3 ]) v2 j  t
3 M$ l* y$ ?7 A, B! ]+ @8 i#define Y_STEPS_PER_INCH 400.0
6 f+ J4 T" h/ C! H$ q#define Y_STEPS_PER_MM   16.0) q6 Y! Z6 z" M( h- o
#define Y_MOTOR_STEPS    200$ B  ?4 j- s. y# \$ M' b0 T

, `) D6 b: P. O& Q* Y#define Z_STEPS_PER_INCH 400.0
% X  S! ]; ?% d( Z) L3 _9 Q#define Z_STEPS_PER_MM   16.0
5 m1 r) J+ Q+ X2 y, I8 D#define Z_MOTOR_STEPS    200
+ q9 ~3 D5 h2 W* r5 Q$ x
: @! I' o. Y& x4 P) I! t#define U_STEPS_PER_INCH 400.0
# Q4 b1 |2 X, A! D& Z#define U_STEPS_PER_MM   16.03 r* V3 _, T9 x- w+ f
#define U_MOTOR_STEPS    200
! S5 s( B  u! A& E1 T6 C9 `3 k- L! B
//最大进给率* [+ t4 X$ S: Q1 C9 l
#define FAST_XY_FEEDRATE 1500.0
' t' `) o/ u+ `2 U$ [5 C#define FAST_Z_FEEDRATE  1500.0* V  t& _8 [' }
#define FAST_U_FEEDRATE  1500.0
8 u& Q, ]5 p7 a( a# G4 R// Units in curve section
+ o+ z+ a4 s' A3 _+ J, I" F! L#define CURVE_SECTION_INCHES 0.019685
3 C  ^# [8 J8 b% M# x3 r#define CURVE_SECTION_MM 0.5& B/ r! g( m" P8 c$ a: _+ r7 S
5 b* S/ b0 L8 O
1 a. n' n: z. P% H6 D
// Set to one if sensor outputs inverting (ie: 1 means open, 0 means closed)
  k6 O8 N' c! u# s7 N( Z// RepRap opto endstops are *not* inverting.8 A* W9 c! j% O; v4 O
#define SENSORS_INVERTING 1
2 X2 ^3 v2 W. l5 W% ^) r& U  A7 E2 P6 w7 h
/****************************************************************************************
$ \+ R- E" V- {6 \: A0 |# ?' _ * digital i/o pin assignment
' r2 K, S1 o' l3 R: n9 G  c *2 A7 @% C5 S; _3 o0 S# t
* this uses the undocumented feature of Arduino - pins 14-19 correspond to analog 0-5. N, T( |# r* j- V# l9 i. x
****************************************************************************************/
! o- V0 W9 y" r  b$ v5 o5 ?; e0 ~; r" T
//camera shutter and control pins* O5 O0 o- R: _+ o' u$ z
#define CAM_SHUTTER_PIN1 29' m7 z: j1 E5 r6 _" t# }
#define CAM_SHUTTER_PIN2 30
" W  _$ n2 o8 R, L3 [: K//#define CAM_AUX_PIN1 31 // analog 0
2 C6 ]5 A6 ^" S5 J; [. J//#define CAM_AUX_PIN2 32 // analog 1
3 Q7 g0 V7 M- [% f; o$ m  F8 T7 g//#define CAM_AUX_PIN3 33 // analog 2) L7 r, |: q* l" f! F& @
//#define CAM_AUX_PIN4 34 // analog 35 U, U+ g& y) _& n% B2 q4 y

# q7 ]  T/ @( s// stepper driver pins
* _. R; q: l% K. h' O#define X_STEP_PIN 7
4 ?/ v" I( J9 u5 x( x#define X_DIR_PIN 86 d0 k" h3 w4 @3 N. p% Y. x6 K0 [/ Y
#define X_ENABLE_PIN 19
' a' c; \: s( L8 \" F  T. m- {: i6 e" N
#define Y_STEP_PIN 9
4 H5 Q# Y  D% ?% V8 ~#define Y_DIR_PIN 10; a! s- P& j$ o
#define Y_ENABLE_PIN 19* Y/ q3 |0 \7 ]8 b6 |1 s) g

: J. B* W7 W7 [& y" M#define Z_STEP_PIN 11- b; m- g  ^) e4 g5 E  q% i% X# c- n
#define Z_DIR_PIN 12, ~" A) g$ I) w) |" Z
#define Z_ENABLE_PIN 19
( s9 p2 p' T* q3 ]7 x. P% m2 W/ h) @0 r; f2 _  ~. i8 H
#define U_STEP_PIN 13) V  @! X5 _/ Q' E
#define U_DIR_PIN 14
+ F6 Z8 c2 U; L# v8 W#define U_ENABLE_PIN 19& d7 q# {6 I- i/ |
) W* G* y) o( b+ O2 Z
// limits not used right now4 n! g# S( ~% \0 T
#define X_MIN_PIN 14
; |& e. [& c! t& [+ M- N5 S#define X_MAX_PIN 146 w0 v* |$ ]* ~$ ^% @8 r
#define Y_MIN_PIN 14+ R" |! t; O6 ?3 x* S: C% d
#define Y_MAX_PIN 14" v7 |& j& z. {$ M& U
#define Z_MIN_PIN 14( @  v2 Q+ x3 V% T2 s% P
#define Z_MAX_PIN 14
; U% L* g7 w$ z2 @2 ~* M#define U_MIN_PIN 146 e$ Z) F, D4 ?
#define U_MAX_PIN 144 k! {% x; K) p" M4 N4 c% {
 楼主| 发表于 2014-5-10 09:54:26 | 显示全部楼层
void init_camera()/ T) u" m" P  |9 g, U6 @, U
{
: R+ h" T3 B. H  W7 [7 u  pinMode(CAM_SHUTTER_PIN1, OUTPUT);- _5 M9 q: G/ y. I
  pinMode(CAM_SHUTTER_PIN2, OUTPUT);
+ c2 }. n" T5 z8 ?2 @! }, X4 S  Y  //pinMode(CAM_AUX_PIN1, OUTPUT); // analog 0* _% J, N/ Z! x. B
// pinMode(CAM_AUX_PIN2, OUTPUT); // analog 1
1 l( I4 c) s6 u5 R // pinMode(CAM_AUX_PIN3, OUTPUT); // analog 2
: q. X7 s7 S" `  s- K% z5 S# D$ S // pinMode(CAM_AUX_PIN4, OUTPUT); // analog 3( g* I  S9 |: f7 J; T
}  J7 ]# j) A. a2 f1 Y" O9 G

5 E0 v' z" D; E$ Hvoid camera_shutter1()
+ |) E" `/ {* h; r{+ Q1 `& u. D( Y% ~6 P+ b$ ~
  // fire the camera shutter via relay...1/4 sec hold time
6 H- E0 `0 ~9 a6 U  digitalWrite(CAM_SHUTTER_PIN1, HIGH);
9 p; u. h, {, g2 F8 D3 m  delay(250);
) n$ j7 D" Y* }( O  digitalWrite(CAM_SHUTTER_PIN1, LOW);
" u4 ^! K& _) R6 L* B# S" @  z( N+ A; _; E8 e4 f/ T
}5 N  J$ h1 z/ M+ e- O, x
, r6 o; F8 @' P0 m3 B, ?- T
void camera_shutter2()
+ Z9 P$ L* z$ z{
5 q9 g" R& \# h; z1 l& C3 f  // fire the camera shutter via relay...1/4 sec hold time
. E& `4 g% W# e  digitalWrite(CAM_SHUTTER_PIN2, HIGH);
2 f+ x  q& [; x1 F8 u) q5 Y3 j6 e  delay(250);! t: x! T. w  b3 u: z; A9 J) r
  digitalWrite(CAM_SHUTTER_PIN2, LOW);( ~" T: W  [% [6 U8 j2 g

; b0 l: C; A+ M/ y. Z, N}! w8 V2 E1 q1 a5 D, u  s/ o" n- {7 Q
/*
2 K  Q: M& `5 p6 B5 I5 G3 Q8 Wvoid camera_aux1_on()% u% M2 s1 y, [" s; j+ C
{6 n: T9 U) `1 Y" ]8 H' Y
  // turn aux relay 1 on& x2 o' {7 [. K& z% }
  digitalWrite(CAM_AUX_PIN1, HIGH);; j. m3 J8 J, F1 D) J+ i* p3 Q: y
}5 }0 V! L& H) v- E6 W
" \, k( q+ n4 e; u5 s
void camera_aux1_off()
5 Z5 T; }0 }( Q) L{8 }" w+ y: Q9 J; S
  // turn aux relay 1 off0 w) }* J( ~  n' Y. E$ S2 P
  digitalWrite(CAM_AUX_PIN1, LOW);
, q1 }% S6 c& [0 y: {+ ]$ }7 _}
, c) j; G+ k' l4 L
3 j& d* V& L- P) s; @void camera_aux2_on()
1 U; v' g# e. x# I5 |1 g{
4 m. o# d) A" G  // turn aux relay 2 on
# _1 @1 g* D2 `! n  digitalWrite(CAM_AUX_PIN2, HIGH);
! j1 D1 s5 i* t. b}; _# {" U& w% e3 Z' @4 d

3 |. _7 r* H* \# b  Jvoid camera_aux2_off()
2 V7 O. M4 m  c! Y" n5 a{
, C! D- D. g/ @$ Y  // turn aux relay 2 off
) p6 E) M" o) g. B* z1 U# W  digitalWrite(CAM_AUX_PIN2, LOW);, `5 j- ]) q4 p
}4 e/ U' i7 z1 }

3 R: r- c6 m' wvoid camera_aux3_on()
0 l9 `! o+ ]) v! `{3 ~7 T2 b$ c% ]
  // turn aux relay 3 on$ X9 H8 F' y0 g' E9 r2 h9 [, o
  digitalWrite(CAM_AUX_PIN3, HIGH);
- s% v/ l$ I( B, ]}8 r+ P! o6 T6 y; }- H
' h% ]* I9 h; X2 f
void camera_aux3_off()
) G' S0 |" |, x6 @0 Z8 K{$ d3 ?  ]2 c* n0 ~4 k  z+ J4 r
  // turn aux relay 3 off
/ I% D' M* @3 b# p  digitalWrite(CAM_AUX_PIN3, LOW);
/ N" C, x- Y0 D2 s}
4 Z/ D' d3 P. _4 s" k& m' F: w% i0 R  }/ f/ E
void camera_aux4_on()
# n& }  b5 I# I% ]' D$ l& U, K{7 w- C+ U) M- B8 v/ Y
  // turn aux relay 4 on* `7 q, c- y9 @. ?5 r! O
  digitalWrite(CAM_AUX_PIN4, HIGH);
4 U4 t, P1 ^8 l9 u}
( V$ }# q' u, Q# L; }* F# g; d
5 _4 `- X# t% Evoid camera_aux4_off()
6 o1 u% F9 W0 D/ z/ D+ n{
% |/ D* k- [& i0 s  // turn aux relay 4 off
/ L8 I5 ~5 O0 M' B$ R( z* r  digitalWrite(CAM_AUX_PIN4, LOW);
+ @8 H* @. a! P- M3 S}
发表于 2014-5-10 09:54:28 | 显示全部楼层
楼主推荐的网址不错
( V. B# Z; V4 P5 ]& `+ s请问楼主是玩什么的,用乐高玩具吗?
 楼主| 发表于 2014-5-10 09:55:02 | 显示全部楼层
// our point structure to make things nice.. t1 k* O2 `3 C
struct LongPoint {
- @7 ~' e- B% ]# V$ y0 e  long x;1 o& E" o' @6 z0 l4 `# i
  long y;
8 V! z% R: p3 V8 a8 X; M  Z  long z;
* i7 V# K- `& b' J) u; }' m  long u;
0 P" q) D( t8 C3 n6 G};
1 h9 k' A7 f% [3 a0 n, L
! T: t7 I! g: v0 Istruct FloatPoint {
# t8 E$ ^: h; e# f# B  float x;
1 v; w# M6 y6 s# ~/ _  float y;% S# g1 g# O$ h, m) P- Y5 r) f
  float z;3 n3 u/ U) q8 h% i) t: e4 ?/ [
  float u;
3 f9 `& h0 y4 F2 f! @: f};
& ~! R! K0 t/ M4 g7 b8 g1 r' D& T8 I; J1 v% U
FloatPoint current_units;/ W) u; V8 b& f( j/ E$ {8 o) A
FloatPoint target_units;  U! T; P$ S2 J3 o) S
FloatPoint delta_units;2 a" e& x- l) f* m

0 }% n7 u1 X& s# zFloatPoint current_steps;  p, m0 z2 W2 R& ]" e
FloatPoint target_steps;
& `$ S1 f' E0 ~0 o5 fFloatPoint delta_steps;1 b% Z9 @  {& Z* J6 `2 j- [0 ?# J
  v; O7 P! G( I% ?0 g" z
boolean abs_mode = false;   //0 = 增量位置模式; 1 = 绝对位置模式1 Y8 v' i- P' A' ]7 n! u! m! ~

) P9 _% r$ w; P% X( ^% I//default to inches for units% z. F: v& V3 D4 x
float x_units = X_STEPS_PER_INCH;
6 F+ R7 u- _& A) Ffloat y_units = Y_STEPS_PER_INCH;  i8 y& |! Q: d
float z_units = Z_STEPS_PER_INCH;8 ~% g% q3 |, j8 n4 G
float u_units = U_STEPS_PER_INCH;& [8 q" T8 q/ z% L* D
float curve_section = CURVE_SECTION_INCHES;5 M' k4 g. L- T6 C: o
, z; a  T$ P$ |% K4 e2 t+ E
//our direction vars
; Z- W4 j+ C* ~- x, t' X& F( t5 L; b5 ?byte x_direction = 1;
% X: S. @3 J4 K+ Z4 l/ m# [1 @2 ?byte y_direction = 1;7 K" L' [! ~# Q1 O1 A, a- Z- Y
byte z_direction = 1;3 w8 U, n$ n9 _! ^! b' g4 T
byte u_direction = 1;6 w% W; l( }. C9 x0 l+ g' Z
3 O' t0 G  o. j: l1 H
//初始化字符串处理: Q' }2 M3 ~$ j. o' ~/ j
void init_process_string()
9 X$ W/ m" p$ W3 ~{
6 P7 o0 g- v+ r) M# W, R* E  T  //init our command
5 b) I, n) e" T& w! g& @0 Y  for (byte i=0; i<COMMAND_SIZE; i++)
) F8 _" f! Y% y    word_old[i] = 0;
6 ?, g8 V0 `, `. f; f6 q  serial_count = 0;( v% v$ D* X( f+ |
}
$ ?/ U& t: a8 _6 s9 J% \  j8 Y4 F, @+ M
; Y' [7 J. u+ d+ p3 D//our feedrate variables.
" D7 p/ E6 _0 w& X  ~  Gfloat feedrate = 0.0;, i) |0 u8 M/ ]  i- G
long feedrate_micros = 0;) e8 j3 g7 z/ Z7 G8 ^5 A

5 X$ r5 E. V+ y& Y* Z//读取并执行命令+ x7 o4 F7 I+ ]# o- X# B
void process_string(char instruction[], int size), U; v4 P+ |+ Q6 y" B
{; s& Y3 p  J: l- T
  //the character / means delete block... used for comments and stuff.+ x2 \9 T6 _$ x8 r1 s% W
  if (instruction[0] == '/')
7 J9 ?" e$ r; `6 [& E! s% A" x% l  {/ h: [( E# k* d& t& }; y) g% i) C- F
    // SerialUSB.print("ok");) n/ N: L/ r1 d5 E  ?
    // SerialUSB.print(byte(78));: U7 ]; j- h6 E( m0 `/ A
    return;8 h# Q6 _8 \" s1 q. J
  }8 |% I4 ?- q! ?0 l* [& B
  //init baby!  ?2 n1 h# d8 x8 X) w9 Z
  FloatPoint fp;; S  m: A! z. F, O
  fp.x = 0.0;
, g5 Q8 F* b  `( g% ^+ N  fp.y = 0.0;" C( C, d) h" ]* J
  fp.z = 0.0;, f" J( \* g% H$ w/ s
  fp.u = 0.0;3 l! N/ x" `  V. E
$ K  c" ^9 _4 ~, C
  byte code = 0;; I5 H9 H6 `% c' `! p- S$ l

. @% B7 `) a" ]% }5 D1 d  //显示在处理的命令
( v4 i5 o) [6 W8 A. b1 G#if (LCD_TYPE == 1604 )% O: X7 ~0 c. y- H
  // lcd.setCursor(0, 4);
# z. ~) y7 @. V8 `) P) @  // lcd.print(word_old);) k# A4 M' o) ~& ]$ Q' i3 X5 ]7 w% k
#endif% u4 o, `- w) r  F6 l$ J" u
  SerialUSB.println();7 e; |$ ^( N$ I! a; V
  SerialUSB.print(instruction);
8 R/ p5 J& S* `, y  SerialUSB.print("\t");" W; m" o2 K2 g- r) D

, o& R7 t( P  m1 K5 {5 n! o  //what line are we at?
9 C) W5 e; Z# a) d, e* p: U8 }. b  //        long line = -1;
7 P1 z+ [8 y8 F9 r  //        if (has_command('N', instruction, size))
- D) o5 Y! `8 E  //                line = (long)search_string('N', instruction, size);
0 Z  b2 m+ [$ z7 }0 v/ h' w) z. z1 D$ l3 {# q
  /*5 L0 S  H) O! j, o7 c9 z& g* Z/ ~
        Serial.print("line: ");
' D+ C6 t0 K5 K* u/ E  _9 b           Serial.println(line);
1 z0 f, P% Q" A' C+ [           Serial.println(instruction);4 |3 g$ m8 T# Y; h' c; N) a- r. o
   */. j. W- \. P" M6 a% ~& `: V2 p
  //判断是否读取了个 G代码?+ d. o! d! B) l- O9 ^* B; X
  if (
4 }% ~: K( ?) h: |" y5 U* \    has_command('G', instruction, size) ||
; Y4 O) O7 @, n" E- I0 J# r    has_command('X', instruction, size) ||
1 A8 y2 t' W8 ?# K# F    has_command('Y', instruction, size) ||
9 O$ P: ?; k/ `3 ]9 b1 ]( p    has_command('Z', instruction, size) ||2 ^0 b: w, o! ?: m
    has_command('U', instruction, size)
3 ]8 ~, V: l, m9 t. M( o9 {  Y6 V    )
- k; I- m# F1 x% f  {( }! m0 r2 _, F  T
    //which one?* J1 U4 |' r& C5 W5 K# w
    code = (int)search_string('G', instruction, size);: L2 b+ S( \5 F7 s6 Z
    // Get co-ordinates if required by the code type given
3 T8 ?- f" C  }4 K    switch (code)6 l; ^3 U+ T  a1 h6 f' `' P4 j
    {
- l+ w" @+ l. _5 ^& ~% r7 U0 f    case 0:
0 B6 ~1 y  J; |0 V: e  l, k2 f' }    case 1:
* n# [2 ?6 @2 {9 P! i4 Y3 P- _" D    case 2:9 \+ s: U. o# ~  k6 W0 y
    case 3:* ]9 g) G& t! ~. M0 g
      if(abs_mode)  y/ F1 V0 V+ m* [+ \6 U: K
      {. L, F* F  Z. l0 g5 C* D
        //we do it like this to save time. makes curves better.
2 i* T0 \* ~# _: a        //eg. if only x and y are specified, we dont have to waste time looking up z.* S: T5 ]. v2 W$ T" O% h+ U
        if (has_command('X', instruction, size))
: d4 ^3 R7 m1 m: l          fp.x = search_string('X', instruction, size);$ S* z2 C8 ~: c' ^2 y' A. O
        else
) J; Y6 J7 L; r" r1 n# y* G1 Q% {+ d          fp.x = current_units.x;
+ J! b$ `: |9 O# v: F+ m0 _5 C& Z- x, T" b; _
        if (has_command('Y', instruction, size))
( A  d1 c, r+ \) ~          fp.y = search_string('Y', instruction, size);
: r2 r) S; D$ g+ T9 `) m        else
9 O0 l4 H& t) ]$ t          fp.y = current_units.y;8 C  w4 _% w3 I3 R" r( `+ i- |: S
# h6 N9 u# [! y; K' w) j/ x. H
        if (has_command('Z', instruction, size))1 k. t, h2 B  [" `" e3 U
          fp.z = search_string('Z', instruction, size);% i7 d3 e5 `) W' j! U
        else8 B0 C& n7 s% f5 ]5 d. X2 y( V
          fp.z = current_units.z;* }- ^8 w5 y+ t9 M" P. _4 \
          : Z# i! u, Q. y$ c" b6 m6 v0 C4 ]
        if (has_command('U', instruction, size))
5 y* A- x' F4 c: ]4 Q& `          fp.u = search_string('U', instruction, size);
8 h8 s" t. ^- W- B+ l        else" h6 {/ a8 |' Q5 c! \  Q* q6 T, s
          fp.u = current_units.u;
0 A: e3 B3 J6 `- s. Z0 f7 D5 |      }# i' R4 o- P2 W% p
      else
8 _  |1 O  R! _$ B      {. x$ H' l' g  v3 s2 r7 f7 v
        fp.x = search_string('X', instruction, size) + current_units.x;$ @; W& p- v, c( k+ X0 }9 U8 _
        fp.y = search_string('Y', instruction, size) + current_units.y;9 V6 j/ \2 I7 q5 C/ Y
        fp.z = search_string('Z', instruction, size) + current_units.z;$ A: P2 M) ^& N  j/ X0 O% G
        fp.u = search_string('U', instruction, size) + current_units.u;
. r7 [8 V. t  C7 w+ |. i: H1 M      }1 C+ F* [1 m( j2 o3 w
      break;, O* s, I. a0 A- b0 L+ k; f: V2 D+ C
    }
2 y7 l) l  L% P    //do something!! N+ X7 ^. A- }! z, `' o% A
    switch (code)
# B' a- X# E: A1 p! B    {
5 k% D7 a$ v, C! P4 ^      //Rapid Positioning
/ _+ f" x/ a6 Z5 c  H8 w# {8 p0 ~# F      //Linear Interpolation
  v1 A2 r* U, a/ {# j  \" z1 B/ n      //these are basically the same thing.
* f' Q* P4 n9 u. N    case 0:
) J; w  ^) v( {8 q4 T( H7 D    case 1:
6 _  x: B4 q! h      //set our target.
: f2 O3 w) V* c: y      set_target(fp.x, fp.y, fp.z, fp.u);
; f- _  Q7 _6 P1 \3 f4 |+ r- d; [6 D      //set_targeta( fp.a);
# b, h2 ?! W4 v% K      //do we have a set speed?
/ w7 w' h+ v% x      if (has_command('G', instruction, size))
- V: K+ y9 Q& Q% e- F6 N      {
( O% ?) ?, L" k        //adjust if we have a specific feedrate.
$ `+ |7 ~2 ]' w% R5 \        if (code == 1)
7 }9 [: \0 f  q/ d- K7 q        {
' c/ ?% V+ i/ ^) T) o& S          //how fast do we move?: H. O9 i# }' n' [; G
          feedrate = search_string('F', instruction, size);4 o4 Z5 B- b# i7 u8 D) U# X. U
          if (feedrate > 0)" t7 [5 }% H- |# N6 G! E
            feedrate_micros = calculate_feedrate_delay(feedrate);/ o; I8 w% T  H$ }0 j
          //nope, no feedrate9 U; z: t$ i, Z' T9 j
          else
. N% e+ ?5 G- V3 D9 V8 z            feedrate_micros = getMaxSpeed();- K3 Y0 o* v" W( u  g
        }8 h7 E! P7 _1 O
        //use our max for normal moves.) L1 V* k/ m# W% `, ?8 G$ M
        else2 d1 a" [# t: k+ X: y+ c
          feedrate_micros = getMaxSpeed();
8 e- c1 y2 F* n/ g' x0 K; m! n      }
! n5 b3 G4 U) s8 A7 o& F& ~# B      //nope, just coordinates!, F' Z3 ^( l6 C* r  X
      else
  ^. p' k5 J2 H: V      {) _" b# J/ z0 M% c. N
        //do we have a feedrate yet?
6 q( Z& ]% ^; D, Z; b' w        if (feedrate > 0)  S# M# ^3 u9 C" Y% T8 D* k
          feedrate_micros = calculate_feedrate_delay(feedrate);
1 b0 m+ D" H# h% w( ]$ ?        //nope, no feedrate3 H2 d7 u" i3 `/ @. B/ f
        else
7 k& K9 ?& [: T$ A2 ]5 }          feedrate_micros = getMaxSpeed();
; y9 {  Y0 L8 a, R+ f- r& ^      }
+ D4 z& u( T/ l$ y4 r# }; h) m8 ?$ |% E4 z: }, B
      //finally move.
4 J. P1 G" }. {" T& C/ J- V: y      dda_move(feedrate_micros);2 P7 [: g" l# N; t
      if(stop_flag) return;
1 F. x, R$ b, T      break;" o5 H8 U7 _3 D1 E
' C9 d$ s, Y! B
      //Clockwise arc/ ^. c& C; B/ _" s5 g, U+ Y4 c
    case 2:
8 K/ l4 t+ R$ I, ?! r' J- h      //Counterclockwise arc; C3 E  T7 Z0 }
    case 3:- U2 W8 @9 {! y" ^
      FloatPoint cent;) s5 j/ L  i: W0 |! B1 @: p9 |
      // Centre coordinates are always relative
# @/ }; s* g7 u1 j      cent.x = search_string('I', instruction, size) + current_units.x;) }! m+ _6 k' x
      cent.y = search_string('J', instruction, size) + current_units.y;- |# k* @+ g) I1 f- K+ W. g5 O
      float angleA, angleB, angle, radius, length, aX, aY, bX, bY;! f8 m) o  J' s& m& I% T

4 k) E, v% `% P7 [9 ]      aX = (current_units.x - cent.x);
$ X. S) d; V5 |* m1 e9 r      aY = (current_units.y - cent.y);
* q! N0 \( u* y$ f+ R- U7 @      bX = (fp.x - cent.x);
- D6 \& S$ k; [$ U+ K; P1 D* Q      bY = (fp.y - cent.y);0 r1 B9 ?- [3 ?5 ~
9 D" }. g, R. h6 W! v( p
      if (code == 2) { // Clockwise
# J7 S% M& o4 n$ X! z/ t/ R# g        angleA = atan2(bY, bX);7 {/ o2 F- W/ Y$ P
        angleB = atan2(aY, aX);. a8 F9 L8 Q0 E
      } + q* P7 \) @$ d5 t% l) O
      else { // Counterclockwise! r, U' A8 P4 a: C/ R# O
        angleA = atan2(aY, aX);. i8 ]6 _( u- Y) C# ?) V! B' s* k
        angleB = atan2(bY, bX);) N/ m( a: j" C5 b0 x
      }9 v" V/ e: o. ~% I8 D
      // Make sure angleB is always greater than angleA
, e; Y; ~) K8 Y& _- h  M1 ~      // and if not add 2PI so that it is (this also takes1 h+ |7 [* v2 l
      // care of the special case of angleA == angleB,! C2 B) `7 m5 Z5 K
      // ie we want a complete circle)8 C8 K3 N! [- @; M
      if (angleB <= angleA) angleB += 2 * M_PI;# }7 ]9 d3 h2 G+ d2 ~* s
      angle = angleB - angleA;0 o! l5 {& Q# s& `+ u
$ E1 W: H) E2 c4 l6 R  g
      radius = sqrt(aX * aX + aY * aY);  X2 ?/ R2 L5 w6 B9 `6 e" n+ H- {. i
      length = radius * angle;4 L, T8 Z4 w* @  m
      int steps, s, step;0 U& z5 Q. [- q* ~2 ~) L
      steps = (int) ceil(length / curve_section);
7 {* Z4 {8 e9 a4 V! q- z! k
0 L! H0 l# J$ h0 X# f      FloatPoint newPoint;$ F1 U2 i' s4 ^2 ?( H4 B, r
      for (s = 1; s <= steps; s++) {
; |& b) G" E: d( O6 ?! D1 U        step = (code == 3) ? s : steps - s; // Work backwards for CW
" s1 E. r0 d/ j( }3 C        newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
+ {' H) C$ h0 Y! G        newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));& Y! e8 G4 j* i- ^- L
        set_target(newPoint.x, newPoint.y, fp.z, fp.u);
+ z+ k5 n- k, H& E
# O! g8 f8 F) `4 }$ H6 ]& k        // Need to calculate rate for each section of curve: j, O) E- ^9 N7 X" K: i" X& y$ K
        if (feedrate > 0)
/ @" Z! Y5 U& _( |! A0 {" v9 J( Z          feedrate_micros = calculate_feedrate_delay(feedrate);3 V3 S: j* n4 W4 X
        else# c+ a4 E; J9 W3 s, k" A, v4 i) J
          feedrate_micros = getMaxSpeed();5 Z8 q2 T1 ?+ c; A" R
: T" |* T) v' N5 g! U1 q8 i
        // Make step! n: f# |& V0 j8 P+ r
        dda_move(feedrate_micros);
" _0 T$ l3 |5 z3 l        if(stop_flag) return;
# P, Q9 X' H( g3 D9 X# ?      }
3 P! {" A6 c' H7 b9 `" T  R0 Z. _
6 v7 S# |# J1 l4 q      break;
: z! S' J# s, [/ {) h  E8 [+ t1 O, ~1 o/ Z' h7 T
      //Dwell+ r3 p, x- ~6 B3 s; U5 q8 n
    case 4:& B2 N2 J* b+ T. f/ ~* g* P9 T
      delay((int)search_string('P', instruction, size));
$ b. U9 o8 a& N6 R0 r/ x$ ?3 [. J      break;
/ ^7 G5 _2 e" N5 D8 T; M9 Z+ X3 U$ A
      //Inches for Units( L8 K- G4 b: i8 Z, A
    case 20:9 X/ ^3 @. w% @8 M
      x_units = X_STEPS_PER_INCH;0 s5 q0 O! g7 H0 L
      y_units = Y_STEPS_PER_INCH;
3 |4 G$ c  U  O; V  J7 W: J% H      z_units = Z_STEPS_PER_INCH;' W; _3 C( r4 F; o% H( l
      u_units = U_STEPS_PER_INCH;
" ], X1 {/ Y0 h  u9 x      curve_section = CURVE_SECTION_INCHES;
! ?2 n7 I$ X: x3 G      calculate_deltas();' U& r" D2 o8 P/ g/ V
      break;( r  ^" t5 j6 [* m4 O5 X

/ j# y2 d% a" L" Z- ]; t) X& h) u      //mm for Units3 W4 \$ O" u+ A4 E# [# q
    case 21:& [* |) x/ l% X$ t, l4 {
      x_units = X_STEPS_PER_MM;$ w! l4 _: R8 [: X. _# i( h
      y_units = Y_STEPS_PER_MM;2 t  Q: w. R' m6 V$ R
      z_units = Z_STEPS_PER_MM;
$ K6 `; ?7 }5 L+ Z. |7 I      u_units = U_STEPS_PER_MM;
6 `7 M6 S6 Q) B      curve_section = CURVE_SECTION_MM;! `7 l, q- L- U, K- q
      calculate_deltas();
4 l& j: F8 W9 e4 Q      break;
) I$ S) {6 c) ?3 U) a' K1 g
4 M' K, Q, f' r" z      //go home.
: ~- L" n9 Y. u8 D* q    case 28:+ Q- T9 N3 ^/ F/ P5 d
      set_target(0.0, 0.0, 0.0, 0.0);
3 H/ D' n4 X+ W  d* C. ?$ Y      dda_move(getMaxSpeed());$ c- \3 q) @8 e- }3 f  \7 N' k( `
      if(stop_flag) return;3 F+ [+ e; g2 e; k, k
      break;$ q2 W7 f8 i  H

: p. \8 ^/ k! G0 h0 ~; m) M7 w      //go home via an intermediate point.
7 ^/ |/ b' W7 X8 Z    case 30:
, X( Y# P9 R& G  B9 H$ W5 `' o      fp.x = search_string('X', instruction, size);
  a4 U  ^% ?. [) d) J, `      fp.y = search_string('Y', instruction, size);# y4 O6 `& [7 o# ?6 L7 @
      fp.z = search_string('Z', instruction, size);
% v. _4 v; d$ L$ B# P6 n, H8 t9 T# K      fp.u = search_string('U', instruction, size);
. G. Y; j& q* j      //set our target.0 U3 |, Y0 r% ^9 M. T- S, Z
      if(abs_mode)
# J# U: f2 n* {* u      {8 P& ^0 @  k' G6 f( E
        if (!has_command('X', instruction, size))# h" g+ f) F5 N  ~- j: I; K) r
          fp.x = current_units.x;
9 }0 b' E3 q) l/ s" u% U( d7 @        if (!has_command('Y', instruction, size))
0 i2 ]5 d: G% l& \          fp.y = current_units.y;
4 E5 s. F& U! m" z6 T5 i* M        if (!has_command('Z', instruction, size))
8 V* t3 M  C8 j/ B          fp.z = current_units.z;
  p, S/ }' A  X0 i3 V4 Q* `. n        if (!has_command('U', instruction, size))
% B: r& `( d1 u          fp.u = current_units.u;
) }( D4 q5 z* T0 q% e        set_target(fp.x, fp.y, fp.z, fp.u);' q( h; c" ]' Z8 j3 s
        2 k/ |5 w  ^% z0 o2 Y3 D, _
      }
2 O% P! s% y7 P0 v' n' F      else
# s4 A6 _! F1 z+ _% V5 x! Y        set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
! b0 v; w- ~0 m) d7 b       # n, ^8 F2 Y( r$ S; c
      //go there.
5 D9 \. x1 W4 p8 m      dda_move(getMaxSpeed());
. V# X; e, e& i& x; K' g* o      if(stop_flag) return;. `6 P4 {; D6 E- G
; L; ^" R9 Q, X+ b! q
      //go home.* R' x* v7 }  Q, }5 p5 r
      set_target(0.0, 0.0, 0.0, 0.0 );, H4 J$ H9 M1 E4 M3 K
     
3 T4 V: [6 K) X3 W  P      dda_move(getMaxSpeed());
0 o5 A( i# p3 M      if(stop_flag) return;
1 u  @% B0 r+ T; E4 y1 T      break;' _, G% j: F: h

6 p( X9 A. J1 u: B# [5 k. X      //Absolute Positioning% X; [$ G- l! A9 n' B8 l
    case 90:$ ?+ ^" e: {5 T. j' b
      abs_mode = true;
4 m6 q; }: ~. C) t5 v7 A  {      break;9 _. R4 c3 N) f6 l" d! O# l2 E$ ^
; S0 D* q2 i! l: ^% K- D
      //Incremental Positioning
( |, k* G' ]  N7 X# X    case 91:" r5 y5 e. k" t; P9 p! R  s1 z
      abs_mode = false;
' W2 u4 `1 S, ]9 p8 m& d/ j      break;9 P: w% m. }& b: i
, E/ S& _& h" W- s% [
      //Set as home
5 N. d; Y5 R1 {& ~2 u, N) \4 c    case 92:
9 q; v: M  a" j8 Z. U/ n     
& j  A* g" E. |3 c) T6 X) p. V8 S      set_position(0.0, 0.0, 0.0, 0.0 );
  C. f6 j. _$ ~& P9 E) x  D1 z      
: ?# ~5 h- |+ c/ W      break;& u6 ~2 M" N5 s. O/ x& k

3 D: r! {* p+ q$ ?4 G      /*" Z, `' y" X4 s0 `- g
                        //Inverse Time Feed Mode
& l5 F) x) f" z0 T                               case 93:
- l9 V1 q8 y7 F* t  X7 ]2 P       ' Z' }( X: p6 I1 q$ J9 H+ P
                               break;  //TODO: add this
" [7 Q9 e- ^7 c4 W* G       ! ~2 _3 _( ~2 T1 n4 F. t! N
                               //Feed per Minute Mode
7 W" E3 S! Y9 u  f1 Z2 n                               case 94:+ ]) X# @- B) b% E: e6 i, Q
       ) L: V; V+ L% @, O4 P
                               break;  //TODO: add this; K  {, P* Z6 J0 u4 M) S
       */
$ k0 I: C( o9 f3 }: S* W0 J
' Z  L- t/ h4 M( S% P3 S    default:
3 B+ v" n* J; [' j      SerialUSB.print("huh? G");+ @- o  Z; p# Y$ C" z
      SerialUSB.println(code,DEC);! T4 f8 h# t3 o$ e9 d: |) j
    }
8 B& S" B& j& j" V8 e! P  }
9 C* }! Z, j% z3 d0 V/ S# i  J
: v# \& y4 A4 Q- [3 G  //find us an m code.' r) [+ ]$ L- ^3 g2 q
  if (has_command('M', instruction, size))
1 w1 \  K( [, i# |( i4 s8 {6 I& p  {+ _7 ~5 ?; k8 e! j
    code = search_string('M', instruction, size);
; c8 [" H$ W# d  [    switch (code)' J; z: O. y* `9 R1 M: W1 ]
    {
, B$ M% Y* Z7 c6 d! A& |      //TODO: this is a bug because search_string returns 0.  gotta fix that.5 S  Z2 y6 D8 \2 |* j" u/ N& g
    case 0:
+ u# d8 Y% y, i# v7 S      true;6 y- y& E: t0 N# S" _2 T
      break;: S# q$ K2 H) y
& \' Q+ O$ }: _" n& X: d- r
    case 100:: }0 }# Z  o- [$ P# M9 j, `
      break;
( C: l* j& e# K6 |
: a4 w$ @, f. P) ~$ T5 H* i      // fire camera relay
5 h0 F% N! j; z! K7 I1 L    case 101:9 M6 O/ v* Z3 m: A5 Y2 L
      camera_shutter1();( l0 ]" @  d. v3 r
      break;( u# ?0 I2 T5 h$ z) W% I  e" a

9 _5 E+ s! M: k      // fire camera relay2
: _: @' l: D) a3 v    case 102:% O" ~, Z8 i  M7 K
      camera_shutter2();
8 c: {5 k0 ]* H      break;) K, ]) h4 j) P- s
/*
5 }. ?! F. z+ ?      // turn aux 1 relay on$ K% E7 |( e% `% L  \5 |% c- t
    case 103:
! u$ n- \% Q" I3 q: @" N      camera_aux1_on();
1 N- L3 t9 g3 s. x! A7 f2 S3 F      break;
5 a" @8 E9 o6 \6 o& V9 Y
& V8 Y  k( e* t6 Q7 R  Q      // turn aux 1 relay off
" t4 y( P6 `" t    case 104:
2 k3 [7 |7 l4 E0 O      camera_aux1_off();4 f( @% E" Y! w. o, n; U
      break;) J- L. t  j4 o5 q, A

9 G+ y' k3 F4 P9 f4 x* H      // turn aux 2 relay on
  F$ J6 {) D  O2 _    case 105:) b5 G/ C% |5 {, k4 `, z
      camera_aux2_on();# p$ J! ?# \* W1 V
      break;' J$ Y  J1 c2 }$ ]7 A$ u% u

, Z0 M& F% U4 R      // turn aux 2 relay off
2 L  F/ U  d7 H8 t3 ]    case 106:
- k7 X; E0 M6 ?1 |% t* ^      camera_aux2_off();1 t- ~6 {! j& r! S+ S  N
      break;
+ P1 g% i1 o+ z2 M# H7 D- h* B1 k8 b3 A
      // turn aux 3 relay on
: G. }9 L( \' e0 Z    case 107:
( |$ ?' `, T. p7 K, |; \      camera_aux3_on();
8 D  a! _0 V) |4 L3 F! V7 |      break;
! V- D; ^( X8 e' V
  w- z( [) M# a8 D      // turn aux 3 relay off
; i9 ]7 v% v" P; I- B) E0 o3 h    case 108:$ O, v" p! s: ]5 }2 m
      camera_aux3_off();8 j/ K3 H8 O$ F1 ?) m+ u( u4 i' s
      break;7 O- x8 n5 r% S) I- Z
1 E+ n( I  U4 v2 G8 s
      // turn aux 4 relay on3 M* n  X' K0 ~& }' H. e
    case 109:
% U" q6 y, |( m8 e. J( M2 W      camera_aux4_on();
. @, F& x) z( D1 D) J) Z3 _      break;
% h- a. n) f7 o  S. S/ e. m9 ^6 S* g3 D1 V0 P$ `8 D
      // turn aux 4 relay off4 n# y# r) E0 m5 W/ \& P
    case 110:
2 u7 ~0 b9 m6 q' @* E& g  R      camera_aux4_off();
- e' o# P  Y# H9 W8 y      break;1 {% F+ H# k; p* a
*/
1 _# O  [+ r$ _9 z4 B    default:
6 c  f) h0 S: e$ y
6 _  z5 E+ o. s6 o      SerialUSB.print("Huh? M");
2 _1 G% X3 H6 D1 c$ A1 @9 N      SerialUSB.println(code);6 w+ V: b$ y' z$ {
    }# R1 D! L' d# L8 b. m
  }
& P/ L! y9 l, n# d
* k1 m: z. H" q( D, @. |* E: i  //tell our host we're done.
5 k  p6 `" K" k1 e  SerialUSB.print(byte(78));. a+ g4 P( g& c* {% e

3 s, Q8 I; x$ P1 T4 \& B4 }}" C9 a& b  L' T; M5 E+ }7 L

$ d  n# y! d& k# K9 b$ J0 S//look for the number that appears after the char key and return it
! e0 ]3 s4 m6 Hdouble search_string(char key, char instruction[], int string_size)
3 a( S; U/ Z+ `, w1 Q+ E/ F! A2 R6 A: K{
6 M5 F! l) p& c  char temp[10] = "         ";9 y6 c2 r' b4 @, @1 Q; m2 [
  for (byte i=0; i<string_size; i++)
/ O. h" j, [9 E4 b6 B  {$ K7 t9 ~& C0 j0 d. t% c
    if (instruction[i] == key)
6 ]1 |# p6 s" o: Y    {" Z  d! S& k  r
      i++;      
- y# p+ r2 S. \' B      int k = 0;
! t( ~' r; |* r0 x/ \& o6 w* j      while (i < string_size && k < 10)
; {( V, g+ N; H, R# K8 e8 z5 P      {
1 e0 N6 l  |* V& l        if (instruction[i] == 0 || instruction[i] == ' ')
# v9 D% t, g9 a          break;
' N1 C3 O# v7 ^, M* Q& N0 |7 V
1 b5 U  b/ n. ^        temp[k] = instruction[i];
$ K& w7 k1 R. G8 ]: V        i++;- \" h" j4 M# M& j( V: w
        k++;% I+ g" a( P% ?' K6 S
      }0 \3 M' Q! k, Y
      return strtod(temp, NULL);& H3 f# }) p5 C. ]: U! I
    }
! W9 G& r2 s* \( K. f  }: y+ [6 D/ B6 I) V1 s/ P* \
' U3 _9 F0 n/ {1 h! i
  return 0;
1 j4 Z7 l/ r1 }0 T; c}" q: j* F6 N& l) j7 E
3 f. E0 }* o! L% ^7 Y& U( v
//look for the command if it exists.
% I% u. i! Q% ~  p/ ubool has_command(char key, char instruction[], int string_size)0 I+ \' y6 q$ O( o! B: O( g
{* T/ G0 b% ?4 N
  for (byte i=0; i<string_size; i++)
3 o3 _7 t# i( g. z4 W  {
; _: c; s/ L- W1 M. B0 u$ N% a& p    if (instruction[i] == key){
  G# N) a: S; a$ ?
& K0 k! Y& W. m5 H+ N9 {& {; A      return true;9 B: s5 e5 R- q+ Q1 l; G3 J
    }# f' A+ Q4 v" R* h* r# ]+ o$ D
  }
* I0 w6 x3 f5 [( T1 @; M9 f3 @8 `9 X" d* s# E- A
  return false;
# T" |1 o5 ?1 C3 F}3 E2 V, u" @# g1 R9 S% Z

% @6 m/ x: p; X6 \3 |0 p; |# i5 B1 y( `0 g4 d

! h+ v2 c, E5 c0 Q
 楼主| 发表于 2014-5-10 09:55:37 | 显示全部楼层
4 f; P$ |- [  Z2 m' f3 u0 ^7 Y4 d
//init our variables' {* }2 b9 |7 f7 X4 {
long max_delta;
; K3 g" O0 ]4 Q- U( W+ g6 Y) m. E8 Mlong x_counter;
+ R8 h/ ^0 Q8 f$ Y+ P3 z6 h' nlong y_counter;9 L3 n& J/ Q& b% V, j' F3 w% O! K) A
long z_counter;$ ], A8 L- [$ ^' s/ i, n9 [
long u_counter;$ N- K$ `- M3 K4 f: t$ m$ x
long x_pos = 0; // x position in terms of absoloute motor stepps
. b6 T0 G) {8 f) Q0 @2 Jlong y_pos = 0; // y position in terms of absoloute motor stepps
# H& F+ j: l2 w& plong z_pos = 0; // z position in terms of absoloute motor stepps! \, s6 d- Q6 r  W: q* N
long u_pos = 0; // U position in terms of absoloute motor stepps
$ C& d, d$ Y! h9 G. i- J0 w, m* a, O0 V$ |" g1 Z+ H& c0 S
bool x_can_step;
  \$ e1 m: M7 c" b( Hbool y_can_step;
: O; L; e2 Q+ b: z' _bool z_can_step;
$ o% Y8 q/ W. C0 m; s8 k; Q2 qbool u_can_step;- K  ^2 t' ?/ {" \) T; `
int milli_delay;9 C/ u, F7 i7 \% T/ m
& m/ }, u- a( V
void init_steppers()
2 D. y  O9 p! @  h{7 s0 d5 T0 M- {% n% x
  //turn them off to start.
2 W2 f  T) b* M5 E  disable_steppers();
+ N* t; ]6 i( u+ i# }# `5 y: g
9 ^3 ]: S4 B' u& _8 i# o" W  //init our points.
( E9 G7 p  q6 D; P+ \2 f, p$ F  current_units.x = 0.0;7 J* H0 @" ^: o- T1 y! x) ]2 i
  current_units.y = 0.0;
4 Q8 G1 d* @8 e  L  l, a- B  current_units.z = 0.0;
) u( W' ]4 p6 n: d  current_units.u = 0.0;
5 Q# s, E. g. `# e7 X1 u  target_units.x = 0.0;" j! }3 v2 G0 R! }. R
  target_units.y = 0.0;
; t" I9 E1 r7 o9 ^  target_units.z = 0.0;; @, ?; B% X; E( |; H
  target_units.u = 0.0;# W% y) N, m7 q7 t! y, u3 N
  ' M% t8 a+ }8 @- ?
. J: v9 v# ^: Z# E8 M* M2 m
  pinMode(X_STEP_PIN, OUTPUT);) b! L8 w) C2 j$ ^* c
  pinMode(X_DIR_PIN, OUTPUT);6 {& V( H2 f/ \7 H+ M: c, T
  pinMode(X_ENABLE_PIN, OUTPUT);
" n! |6 j$ |6 l: u+ n( u- o  pinMode(X_MIN_PIN, INPUT);1 b& ^: d2 Z' o* @' O6 z
  pinMode(X_MAX_PIN, INPUT);
) h* A$ G2 W6 S* S" p) r- s& Z, @/ L5 Z7 V  Q+ ]% ~
  pinMode(Y_STEP_PIN, OUTPUT);$ O+ |# T0 M/ p/ H2 M, Q/ Q
  pinMode(Y_DIR_PIN, OUTPUT);4 k4 E- U0 o9 g1 M- W
  pinMode(Y_ENABLE_PIN, OUTPUT);" B( q& `# ^) g. v8 V
  pinMode(Y_MIN_PIN, INPUT);
8 {$ |) F. h8 o! i& k2 O  pinMode(Y_MAX_PIN, INPUT);( `/ o! d3 N2 e: A# G: a- t

5 I. s1 E2 |$ ]6 C& @  z3 V  pinMode(Z_STEP_PIN, OUTPUT);
& F. a# A: e# F  pinMode(Z_DIR_PIN, OUTPUT);( X; Q8 n6 C! @6 g8 ^5 }, s
  pinMode(Z_ENABLE_PIN, OUTPUT);, ~6 O0 x3 ?: e+ K' o( i$ C7 k7 r
  pinMode(Z_MIN_PIN, INPUT);
) L+ l1 N1 [4 f. L0 W) Z  v  pinMode(Z_MAX_PIN, INPUT);9 o, u' j9 K% D% u5 p
8 p: N; H; V* ]* b$ }3 e9 v
  pinMode(U_STEP_PIN, OUTPUT);
/ \/ m  Y! \8 C: a9 _' E  pinMode(U_DIR_PIN, OUTPUT);& A  g4 ]& t* Q* n& b
  pinMode(U_ENABLE_PIN, OUTPUT);, J( O& g' b& x; R0 C
  pinMode(U_MIN_PIN, INPUT);
( V: {2 o: A7 s; `1 F, H$ S' N( j  pinMode(U_MAX_PIN, INPUT);0 _( o* ^$ U6 Y# A- p
  //figure our stuff.7 y2 o/ E' Z* `$ Y
  calculate_deltas();
( P6 b; X# J  a" K}
* Y; [5 w8 e7 H7 `/ Z& e+ ~+ F( L8 c1 ?4 c" X* k* {9 J
void dda_move(long micro_delay)
% m$ M" c/ N1 @1 J3 u& t: e{5 {' |! e9 \: {" W& o( ]
  //enable our steppers7 Y3 {. _( j, {0 b, L
  digitalWrite(X_ENABLE_PIN, HIGH);
5 K  G& s& N+ |  ~2 k  digitalWrite(Y_ENABLE_PIN, HIGH);+ e& H' C3 {' i# j4 T! h+ R2 m
  digitalWrite(Z_ENABLE_PIN, HIGH);
* x7 X! u; G, N/ r" n/ M2 X  digitalWrite(U_ENABLE_PIN, HIGH);
4 m& e: g: M9 D: @8 f. H8 q  //figure out our deltas6 ^( p. ^" \% T' ^4 x( v
  max_delta = max(delta_steps.x, delta_steps.y);' I$ c( j; n( p
  max_delta = max(delta_steps.z, max_delta);
; i' n7 e7 c9 r8 n: |! g# F0 d) T  max_delta = max(delta_steps.u, max_delta);
6 M1 Z2 z' q) o9 F" L& k  //init stuff.
# R( g% x7 O4 I. t- A* K* Y  long x_counter = -max_delta/2;" x2 p: y; v2 p- W' Q$ j
  long y_counter = -max_delta/2;) ]2 b  ?7 z) `2 P& v' A" G/ X
  long z_counter = -max_delta/2;2 J9 `& P6 G2 f
  long u_counter = -max_delta/2;
9 P) P: ?8 Y& h! Y7 r: C2 w
# f. W0 Y% D7 T  N& z2 @  //our step flags! J- B2 N0 B, ]; \
  bool x_can_step = 0;3 ?; Y* O! E! Y6 ?8 d7 s0 S
  bool y_can_step = 0;/ W- K- N% N; m" t5 h, X- n0 t
  bool z_can_step = 0;
; r3 m' Q% t8 d- w, L! ?8 v  bool u_can_step = 0;0 Q7 H2 i/ P) j! y4 L
( q: P" A: K% P0 F
  if (micro_delay >= 16383)9 _1 ~' A0 Z! {2 i& w: C* L
    milli_delay = micro_delay / 1000;
2 \' k% K4 w, z- i( ~. l  else! h( I7 u; h, ^( T5 U$ U
    milli_delay = 0;5 n: o) S7 q: `* p8 ]8 R

$ I. Q& t7 }) y( I6 r1 `
4 C9 m$ h  L8 j$ U+ s% T. e4 ~& [  //do our DDA line!
$ m5 c8 L8 a# E8 L) ^
+ l- N  j. X! X9 t8 t: N. A8 o5 \  do/ A' x* `3 C, K8 W0 ~+ Z
  {. e; B. a( }7 t- Z+ `
    if(( digitalRead(BUTTON_SS)?analogRead(BUTTON_MAN)>>9==0:0) || stop_flag) break;
' B4 Z# v, t" H# |! J' Y9 W    x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, x_direction);
0 Y( @) A5 N- f, \. I9 E! o7 V" c0 E    y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, current_steps.y, target_steps.y, y_direction);. ]1 U  m0 h' U' i, c- u' l0 C
    z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, current_steps.z, target_steps.z, z_direction);
% j; X! n. C9 h1 x; Y    u_can_step = can_step(U_MIN_PIN, U_MAX_PIN, current_steps.u, target_steps.u, u_direction);  N8 d1 l/ s) n, a( R
    if (x_can_step)
5 \2 p6 a$ q  ~$ S# \3 W7 ?    {
5 `4 C- m/ c& P. e      x_counter += delta_steps.x;' O5 ~: p' I# _% Q
6 Y8 I& w' ]# h8 m( \  m
      if (x_counter > 0)
: r* n  l7 J( _. U$ t' N) w! J  w      {" s( D' @8 ~8 R
        do_step(X_STEP_PIN);
' a  U5 k" p  k# L        x_counter -= max_delta;1 j1 C. K5 r" ]- j" J' u" m7 M
        if (x_direction)6 l2 m+ M3 Q4 Y3 j1 X0 r
         { current_steps.x++; x_pos++; }! e/ C& H& ~0 E( C1 P# \
          ; _4 P1 d  y  V! s- Y8 b+ C
        else: W( u; Q! g( N2 N# G) K
          { current_steps.x--; x_pos--; }/ z3 T5 B- J1 x& H/ p5 N
         
+ [5 k( ?" U3 ]( z: B% Y      }
8 H% x8 g, ~+ h    }/ M9 S  B  M4 _1 ~, m
    if (y_can_step)
! n+ j9 n) R0 ^$ B& W9 p    {0 }+ f8 E* q# |$ `+ |" f. t
      y_counter += delta_steps.y;
: ^; {# ~0 U, |% m" d) l1 f( h" Y3 a0 l" G* Q" t8 Q1 `3 L/ B
      if (y_counter > 0)4 Y  e3 i- c$ m' W; ?7 M) a
      {( v: d$ B$ Z1 R: }
        do_step(Y_STEP_PIN);
- y& Z2 Z" p% t! D1 I        y_counter -= max_delta;
0 I" |- L. B* y& V2 _0 Q" V. h7 p- D/ ?9 y
        if (y_direction)5 M: j. b) h! X, u5 ]5 N
        { current_steps.y++; y_pos++; }/ L) c, D7 C0 h6 r( X! t3 l. _, H
      
: Y  E: k5 `' c2 b        else9 g: D4 l5 ?1 f9 T; c- l1 ~' z% D9 r
        { current_steps.y--; y_pos--; }
: c3 Z( B, ~1 ]' V        8 p  s6 Z" T: u6 J. Q
      }
7 G# e  o, L9 p, e3 H2 u    }
  D, G9 w1 b0 b) Y  G/ r0 \% R' |3 ]# a5 v; ~+ W
    if (z_can_step)
& `  v' b2 D, G+ P& p1 d+ x    {1 I4 q/ N! R! v
      z_counter += delta_steps.z;
- s1 p2 _% s: ^! N  P
3 g; h6 F7 i' z+ y; P      if (z_counter > 0)
3 Z) L" G0 ^6 a% ]! A- }# o6 O- X      {
9 n. a8 ^" A/ j/ M+ j: U" H        do_step(Z_STEP_PIN);- w" V0 ^* T- ?1 v( g
        z_counter -= max_delta;
. y$ U" N: d8 Q1 x0 r, _$ U* Y. D3 n" [+ }7 ^
        if (z_direction)
. r2 W; d+ P) E) W; H# u, d; t, j        { current_steps.z++; z_pos++; }
/ J: u' c' H( Z- Q  n' o        0 c! [6 M+ ~% N6 y5 r8 @
        else
7 W& ?+ _- e1 P; ]        { current_steps.z--; z_pos--; }
/ c* s: X2 y* L7 [  z2 m9 O" Q/ j        & N* A# B; ^! |$ W( d( G6 L
      }
$ g* P# j% n. t6 _    }
1 m" S, d+ j8 c. h' p6 S- `    $ l% u) E' m( t/ B! x) u( V! z
    if (u_can_step)% m5 r( x3 W3 s/ r& b3 q! ~  {1 S9 S
    {
. L. o5 X5 k; W  U8 g      u_counter += delta_steps.u;
6 P7 z9 A- W' `/ w
& w* h; y/ J) ?8 E3 Z      if (u_counter > 0)
5 e8 e0 z2 L7 K8 X3 _4 V4 n& T  r      {
; m5 O3 I1 `& M- ]) W        do_step(U_STEP_PIN);: a. H, e( F4 y* [
        u_counter -= max_delta;
2 \7 ?" \, b3 F! V
: _5 `1 I: G* ^1 q% W" ~' T9 D, `        if (u_direction)/ c5 {( s" {5 t) O6 P  J
        { current_steps.u++; u_pos++; }: [" Q, w1 K3 m8 o
         
7 j( y4 z/ a! l) L4 B( W, z        else
' j' k0 B  I4 C& W, R          { current_steps.u--; u_pos--; }
9 T, z9 n, v& T# _! F         + q# ]; I$ ]3 i& M# a1 @
      }
8 r" s! f  M1 I7 `+ J    }' s. s7 u& J6 W+ V
    //wait for next step.
1 m( \: \. }, z, K    if (milli_delay > 0){5 _$ J4 D% `3 D/ x
      //if (digitalRead(BUTTON_SP_EN)) SPEEN();
$ b/ ~# U# ?4 I4 _9 y# M1 b' R      delay(milli_delay);
  G4 _! a3 G  g: A3 ~4 A& v# R9 Q    }                1 e' p% b& O) [+ f# f$ O4 d
    else{
9 g/ Q( T7 b: a      //if (digitalRead(BUTTON_SP_EN)) SPEEN();( Y% n4 ~/ h6 n( U  n6 j
      if(micro_delay>0)delayMicroseconds(micro_delay);
) ~# s7 |! O& i$ A6 F4 i    }
2 B! h5 F4 h( f    //if(x_can_step%40 || y_can_step%40 || z_can_step%40);
/ O5 d9 w* x! S0 G, k) Y# A' u  }
$ u# A9 _5 T' H+ s7 E9 o/ m  while (x_can_step || y_can_step || z_can_step || u_can_step);
7 [$ l6 A% H/ e& ?! `; j: r) |! @6 c6 }' O
9 h7 o" u. ]! d* j
  //set our points to be the same2 N$ X; b# Y1 [3 f0 S
  current_units.x = (float) x_pos / X_STEPS_PER_INCH;
3 ^; l& f$ _% U! _* U3 e: R  current_units.y = (float) y_pos / Y_STEPS_PER_INCH;% E7 x, @" w' G3 `
  current_units.z = (float) z_pos / Z_STEPS_PER_INCH;4 _2 D. }: F& H9 D* p! ~
  current_units.u = (float) u_pos / U_STEPS_PER_INCH;% ?# v: }5 d* E9 ?+ F! t  T
  5 G! I. Y6 O' d% @3 c
  set_position(current_units.x, current_units.y, current_units.z, current_units.u );$ l2 k2 J# N7 {) P5 F$ W
4 f# R$ F, |3 a5 R6 S9 w  j- N
  long x_pos = 0; // x position in terms of absoloute motor stepps
; N( i% Y' W, A* o! s  long y_pos = 0; // y position in terms of absoloute motor stepps5 E# S5 `3 \. a" y! ~1 D* l
  long z_pos = 0; // z position in terms of absoloute motor stepps2 @4 {/ s$ G# O9 i1 _: n
  long u_pos = 0; // u position in terms of absoloute motor stepps3 u! N1 V9 O9 b
  calculate_deltas();6 }. a1 p& O9 r1 \& J- Z' `
  2 ?. v0 O  R3 t# s: ^
}  @  Z$ I. ?( p6 ~/ X7 J" V

5 b) S1 W4 E' O) ~9 _% F5 d9 qbool can_step(byte min_pin, byte max_pin, long current, long target, byte direction)
8 m) }( e5 a& _# J( ~8 T& H{( [  f$ c8 e% n* b* V1 d
  //stop us if we're on target
; T7 `  @/ S- H1 H5 X" t  if (target == current)
9 _2 @: [+ K+ I8 c3 j# m- n    return false;+ J' F; K; P/ ~' h9 t  O4 {
  //stop us if we're at home and still going
( O! L* y* Z. d6 |  else if (read_switch(min_pin) && !direction)  u9 u- W2 Z/ R% i: [, D8 r
    return false;
9 S" X$ M! ~! f" }, `  //stop us if we're at max and still going0 W; s# [2 C" M& c5 T& M0 g
  else if (read_switch(max_pin) && direction)
& y4 ?: H6 k4 g" \" z- ^1 m5 S( _    return false;
1 S0 d+ @% c/ \8 n: P
9 P, h! Q% M4 U2 J1 j, M. [- w  //default to being able to step
6 o7 _# b% V( m0 k: o3 L  return true;5 i1 p  Q- R3 j& l! O+ J9 T
}0 P3 e3 @7 z& m" y/ A  _4 Q& V

, s  [: {* r9 n9 c1 tvoid do_step(byte step_pin)
  q: e4 K6 S: b- j" i) P8 j0 a" O{
- e  f* N) Q. v  x+ J  digitalWrite(step_pin, HIGH);
6 y. B! ^; n/ M/ V  //delayMicroseconds(1);9 y7 ?1 N6 ^! L/ C$ f- C# f
  digitalWrite(step_pin, LOW);& X& X/ U+ u) i8 B0 |8 F* p: H
}
* i2 @3 x5 D0 G* C( }/ u6 n) n/ M! @# R
bool read_switch(byte pin)
! {- z. ~; \3 q  h: z4 ]$ L/ q' m2 f, u$ H{
( r2 `  j. h9 ^. ?2 `& e$ k  //dual read as crude debounce
! n, b6 B4 }1 k8 X+ c3 J( L( c* N' `: n2 o
  if ( SENSORS_INVERTING )
: k( ~1 b- P5 X( ~$ A1 W7 ]0 ]$ C0 p    return !digitalRead(pin) && !digitalRead(pin);# x' U8 ~4 a9 P8 ~1 X
  else. A5 t3 b4 K7 v- S* X' w- P. j2 t
    return digitalRead(pin) && digitalRead(pin);
5 G: W# ]- e5 L}
5 e, y/ \- t/ b7 N
; L; d, @  F0 T0 L; s- a# Y2 ^long to_steps(float steps_per_unit, float units)
0 D2 S+ e4 W/ H! v{
2 b  X0 S/ W5 {1 W  return steps_per_unit * units;. `5 H, A& {4 ]5 ^! l
}
  }+ \% D5 m4 _) J7 t6 ]4 {6 ]" Y
) D$ Z1 h0 ~- @# Tvoid set_target(float x, float y, float z, float u)
9 x$ E. g$ S1 G/ }$ u{
% E' ~) }& }* W/ Z  target_units.x = x;3 V1 \: h3 j7 s! p3 `
  target_units.y = y;
9 O# E) ~9 x( b0 x  target_units.z = z;
+ j5 u% M$ v: a) J+ [7 t: R4 J" d  target_units.u = u;) r. @0 @1 b' j0 w7 |, C: \
  calculate_deltas();
8 I) {4 ?9 B/ `( r}( J$ ?9 ?7 `$ y  s2 ]8 N5 }
. u7 x' H" {" q/ W5 O6 b' L
void set_position(float x, float y, float z, float u)) j' q6 x4 p* i; m, A
{
' X: y+ a% x4 f1 c3 g  current_units.x = x;. @, u8 I4 d- E1 ~7 x; \
  current_units.y = y;) V1 }' P  p) L
  current_units.z = z;2 X! }1 b4 h4 b! D! t
  current_units.u = u;
4 D  V$ l* T: l  calculate_deltas();
) D1 p; h7 G3 P9 v) O* L}
, o. g/ i. f; _2 U4 ]/ G" {; J, K$ G9 ?& y" ]
void calculate_deltas()4 h8 r$ B3 Z  t, R5 L1 |2 Q
{
& x6 O+ i3 \- T0 W- [  //figure our deltas.7 J" v% ?" r% b, V% D% Q, Q
  delta_units.x = (target_units.x >= current_units.x) ? (target_units.x - current_units.x) : (current_units.x - target_units.x);( U7 n* j% A" G4 E' z4 n1 q9 [
  delta_units.y = (target_units.y >= current_units.y) ? (target_units.y - current_units.y) : (current_units.y - target_units.y);4 G0 G1 Z: f  d- S" E
  delta_units.z = (target_units.z >= current_units.z) ? (target_units.z - current_units.z) : (current_units.z - target_units.z);
+ r7 g5 A( b0 I8 x: C" ?  delta_units.u = (target_units.u >= current_units.u) ? (target_units.u - current_units.u) : (current_units.u - target_units.u);  e' n1 J  N- E  c' u6 Z
. B+ \9 w% ], c3 o
  //set our steps current, target, and delta& R' G( u- r( F+ u1 ]1 ?% B/ b0 d
  current_steps.x = to_steps(x_units, current_units.x);% g! ]4 Y* |2 ]" v* H5 r
  current_steps.y = to_steps(y_units, current_units.y);/ _( _/ G0 \6 Z% g, X
  current_steps.z = to_steps(z_units, current_units.z);
3 g+ d- o2 b/ C0 R- ?  current_steps.u = to_steps(u_units, current_units.u);1 i! P# d+ g! u3 c9 M" A7 D% k
, m+ `* G1 \2 `; T
  target_steps.x = to_steps(x_units, target_units.x);9 s2 \0 Q7 e# Y6 e8 q
  target_steps.y = to_steps(y_units, target_units.y);, O- Z+ f$ ~$ c# U/ k/ ^
  target_steps.z = to_steps(z_units, target_units.z);
# e3 N$ P0 ^5 ~  target_steps.u = to_steps(u_units, target_units.u);6 R1 G, N+ @4 \  P

4 s$ h' F  d$ `  delta_steps.x = (target_steps.x >= current_steps.x) ? (target_steps.x - current_steps.x) : (current_steps.x - target_steps.x);3 a4 ?1 P/ N  ^1 z" ]7 o
  delta_steps.y = (target_steps.y >= current_steps.y) ? (target_steps.y - current_steps.y) : (current_steps.y - target_steps.y);& ?0 e+ v- m- P$ D& r4 e3 g9 c
  delta_steps.z = (target_steps.z >= current_steps.z) ? (target_steps.z - current_steps.z) : (current_steps.z - target_steps.z);$ M- K6 `5 ^8 ~3 a3 Y6 ]1 t
  delta_steps.u = (target_steps.u >= current_steps.u) ? (target_steps.u - current_steps.u) : (current_steps.u - target_steps.u);* |+ P6 ?: C- c/ r: g+ M; J

  h$ \+ U& y1 _9 F. i# k, L* G9 v2 B* y$ x

4 @8 B6 N' l; Y1 B; |  //what is our direction
- A2 B- ^/ b- h$ o- n  x_direction = (target_units.x >= current_units.x);  {  D* I/ d! G* `* l* {" F
  y_direction = (target_units.y >= current_units.y);* j% ?7 s) R2 ^0 j% I) J: f. A
  z_direction = (target_units.z >= current_units.z);0 b. T' i; h4 V- d
  u_direction = (target_units.u >= current_units.u);
, e5 G) S/ ]( R; W6 e
; B: {2 Z! p  K- X% Z  //set our direction pins as well
$ j" z4 N; m2 W  U. o" r/ s  digitalWrite(X_DIR_PIN,x_direction);1 W7 M! D) T4 h( U8 U6 q2 `2 W3 b
  digitalWrite(Y_DIR_PIN,y_direction);
/ @' g& h! G* ~" _# n  digitalWrite(Z_DIR_PIN,z_direction);. T+ v, _) T- \% M
  digitalWrite(U_DIR_PIN,u_direction);
/ e7 d& }4 |" i6 p, w( m& L1 j8 L- Z% Z# ^) t, s# U
  //绘制LCD; A3 C6 A& c& p
  LCD_DRAW();
* [6 h3 Y0 t2 d* n! o
& B- ^# s4 M) ~}# E8 n3 s4 c" l
7 u! u" H: I) c# _2 N1 z
$ `; k8 a% C4 ]) J) j( s& b
long calculate_feedrate_delay(float feedrate), P% H7 R5 x- O( B# N/ Q7 {
{5 J! y( a7 c7 `* |$ @. y
  //how long is our line length?
4 f1 {# s* }/ k; c' i( {  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);+ r; z8 K+ n& f/ e9 L* c' u4 q
  long master_steps = 0;4 f" h5 {( k. K4 ~5 D5 R! m7 Z% C: _4 k- D

& u7 Z0 Q- d; f8 f( hmaster_steps=(delta_steps.x > delta_steps.y)?delta_steps.x:delta_steps.y;
9 s1 p& V& F, h2 q' W" E  r  Qmaster_steps=(delta_steps.z>master_steps)?delta_steps.z:master_steps;
3 e. j, H1 E# x, G9 mmaster_steps=(delta_steps.u>master_steps)?delta_steps.u:master_steps;
2 R! L5 W$ B, ^* N& W- ?) B$ @& Z+ e3 |; T# E% S

8 r' m3 {; Z, O: h5 f1 o* |
* W: C% u6 U* ]$ h" ^7 e8 x8 {4 G ! f0 _0 ^" o$ i0 k
  //calculate delay between steps in microseconds.  this is sort of tricky, but not too bad.
+ P3 E  N( O8 L! @6 Z  //the formula has been condensed to save space.  here it is in english:/ [& O% W$ R! f' j
  // distance / feedrate * 60000000.0 = move duration in microseconds6 V; v, ~0 x: p8 S9 [
  // move duration / master_steps = time between steps for master axis.- t" E! Y, g6 K' F! s! l# Y
return ((distance * 6000000.0) / feedrate) / master_steps;1 d0 H* u8 e" v$ r

9 C# B. w9 h; c" Y4 S9 f}' m. h7 s. H5 U  r

& o3 O3 O2 P  n& G$ ]9 X6 ]& Mlong getMaxSpeed()
9 L/ K4 H2 r& C& E8 o+ e) [{
, L% c4 E/ e5 T' o4 j3 b3 | if (delta_steps.z > 0 || delta_steps.u > 0 )
  J! w; q; a; r! J5 c    return calculate_feedrate_delay(FAST_Z_FEEDRATE);
9 x6 o% C$ I% b, z! {; a  else: _# a6 q  Q" h
    return calculate_feedrate_delay(FAST_XY_FEEDRATE);
! p5 F$ q: l# c}& }& X' k' i1 d1 M. ^! S" w
$ d! G$ Z3 J; q  `2 l
void disable_steppers()
9 w6 ?2 H5 n, m' j{
: C  G0 d/ r, K- U  //enable our steppers  T" \7 A* F" M1 ~: g
  digitalWrite(X_ENABLE_PIN, LOW);: c. A9 A3 I6 Y9 D
  digitalWrite(Y_ENABLE_PIN, LOW);
5 M: l6 i  S# T  digitalWrite(Z_ENABLE_PIN, LOW);- S- v1 Q; i# J0 s% x
  digitalWrite(U_ENABLE_PIN, LOW);
; R: }* G" V* u( i7 \, P+ ]}
6 g' G" O' m. J! p2 [/ m; `
" P. x) ]0 |# {4 K" I3 ^
, ^/ j- u( s: B! O//绘制LCD
+ }8 R% {! J; |# E0 W//unsigned int DRAWCount=0;
6 o+ d8 D7 m3 avoid LCD_DRAW()' U( B/ g( Y$ K2 c# z$ b
{0 J% d3 Z6 Q1 v- ?% k
    lcd.clear(); ) f/ q# q7 G6 U3 G: c
    lcd.setCursor(0, 0);, F% S2 F; S2 z  C: M% a& V4 T
    lcd.print("    X=");
5 A+ P. I! k) F8 x$ y: ?0 I) K    lcd.print(current_units.x);
/ A6 }4 J, B- w8 k, k( g4 {5 p- X    lcd.setCursor(0, 1);
$ _: M7 j& J7 C! k    lcd.print("    Y=");
# g2 M; ~$ ~& @; N    lcd.print(current_units.y);3 `4 t% W* @* [# w8 o1 G$ d
    lcd.setCursor(0, 2);' r+ S& m5 I: B" b+ d/ `
    lcd.print("Z=");* k# Z: `* D9 S! \5 A+ ]
    lcd.print(current_units.z);
' y# k2 j5 b7 d. ~/ I3 M) P, z; [# }    lcd.setCursor(0, 3);
% f& g7 {4 O7 J% j1 r1 C    lcd.print("U=");
  c1 V! d  j" V5 F. _! z    lcd.print(current_units.u);7 B# ]' ?9 M7 L! G, E, U
  }4 ?! |! P2 P4 D5 h
// else if (DRAWCount>=30)DRAWCount=0;" P( d4 Q& K. u7 x( Y4 N6 h
//  else DRAWCount++;4 I1 m+ O# Z" [2 ~  N
void SPEEN()# Y1 g2 g0 l. T" m6 X* n( N( o
{5 A- z! A& r: N# p6 E1 r7 i- y
   delayMicroseconds(analogRead(SPEEN_CTL)+1);
, J% G- M& E2 y4 |3 `9 K" e }
1 S) y, t6 X; d1 q1 g
/ m$ [1 g- X0 ?7 o, i! F8 c8 d5 F
//delayMicroseconds(analogRead(SPEEN_CTL)+1);, _6 i! A* q( m, `$ W) T  d( L0 X
//if (digitalRead(BUTTON_SP_EN)) SPEEN();' Y: e3 I# V" f$ T* B7 }

7 A6 u+ ], Y& j* F2 u9 d* b+ t6 z, f5 d$ 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, 2025-8-19 01:05 , Processed in 0.078513 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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