grbl源代码nuts_bolts部分

转载]grbl源代码nuts_bolts部分

 (2017-03-19 17:29:43)

 

标签: 

 

分类: CNC

原文地址:grbl源代码nuts_bolts部分作者:

nuts_bolts文件一些变量声明和共享函数

#ifndef nuts_bolts_h

#define nuts_bolts_h

 

#define false 0

#define true 1

 

// Axis array index values. Must start with 0 and be continuous.轴数组索引值。从0开始,必须连续

#define N_AXIS 3 // 轴数组

#define X_AXIS 0 // Axis indexing value. 

#define Y_AXIS 1

#define Z_AXIS 2

// #define A_AXIS 3

 

// CoreXY motor assignments. DO NOT ALTER.

// NOTE: If the A and B motor axis bindings are changed, this effects the CoreXY equations.

// CoreXY电动机作业。不改变。

// 注意:如果A和B电机轴绑定是改变,这影响CoreXY方程。

 

#ifdef COREXY

 #define A_MOTOR X_AXIS // Must be X_AXIS

 #define B_MOTOR Y_AXIS // Must be Y_AXIS

#endif

 

// Conversions

#define MM_PER_INCH (25.40)

#define INCH_PER_MM (0.0393701)

#define TICKS_PER_MICROSECOND (F_CPU/1000000)  //滴答延时微秒

 

// Useful macros 宏命令

#define clear_vector(a) memset(a, 0, sizeof(a))  //用零清除整形数组a

#define clear_vector_float(a) memset(a, 0.0, sizeof(float)*N_AXIS)

// #define clear_vector_long(a) memset(a, 0.0, sizeof(long)*N_AXIS)

#define max(a,b) (((a) > (b)) ? (a) : (b)) //返回最大值

#define min(a,b) (((a) < (b)) ? (a) : (b)) //返回最小值

 

// Bit field and masking macros 有些字段和掩蔽宏

#define bit(n) (1 << n)

#define bit_true_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) |= (mask); SREG = sreg; }

#define bit_false_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) &= ~(mask); SREG = sreg; }

#define bit_toggle_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) ^= (mask); SREG = sreg; }

#define bit_true(x,mask) (x) |= (mask)

#define bit_false(x,mask) (x) &= ~(mask)

#define bit_istrue(x,mask) ((x & mask) != 0)

#define bit_isfalse(x,mask) ((x & mask) == 0)

 

// Read a floating point value from a string. Line points to the input buffer, char_counter 

// is the indexer pointing to the current character of the line, while float_ptr is 

// a pointer to the result variable. Returns true when it succeeds

//读一个浮点值的字符串。行指向输入缓冲区,char_counter

//索引器指向当前的角色,虽然float_ptr

//指针变量的结果。成功时返回true

 

uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr);

 

// Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms().

void delay_ms(uint16_t ms);

 

// Delays variable-defined microseconds. Compiler compatibility fix for _delay_us().

void delay_us(uint32_t us);

 

// Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking.

float hypot_f(float x, float y);   //计算直角三角形的斜边长

 

#endif

 

__________________________________________________________________________________________________________________

nuts_bolts.c

下面函数功能是根据line字符串读取float数值

#include "grbl.h"

 

 

#define MAX_INT_DIGITS 8 // Maximum number of digits in int32 (and float)

 

 

// Extracts a floating point value from a string. The following code is based loosely on

// the avr-libc strtod() function by Michael Stumpf and Dmitry Xmelkov and many freely

// available conversion method examples, but has been highly optimized for Grbl. For known

// CNC applications, the typical decimal value is expected to be in the range of E0 to E-4.

// Scientific notation is officially not supported by g-code, and the 'E' character may

// be a g-code word on some CNC systems. So, 'E' notation will not be recognized. 

// NOTE: Thanks to Radu-Eosif Mihailescu for identifying the issues with using strtod().

//从字符串中提取一个浮点值。下面的代码是基于松散

// avr-libc strtod()函数由迈克尔·Stumpf和德米特里?Xmelkov和许多自由

//可用转换方法的例子,但是对于Grbl一直高度优化。对已知的

//数控程序,典型的十进制值预计将在E0的军医。

//科学记数法官方不支持的刀位点,和“E”的角色

//是一个刀位点一些数控系统。所以,“E”符号将不会认可。

//注意:由于Radu-Eosif Mihailescu使用strtod识别问题()。

 

//下面的代码是松散地基于avr-libc strtod()函数由迈克尔·Stumpf和德米特里?Xmelkov和许多免费转换方法的例子,但是对于Grbl一直高度优化。

//对于已知的数控应用程序,典型的十进制值预计将在E0的军医。

//科学记数法官方不支持的刀位点,“E”字符可能是刀位点一些数控系统。

//所以,“E”符号将不会认可。

//注意:由于Radu-Eosif Mihailescu使用strtod识别问题()。

uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr)                  

{

  char *ptr = line + *char_counter; //指针line + char_counter值

  unsigned char c;

    

  // Grab first character and increment pointer. No spaces assumed in line.

  // 抓住第一个字符和增量指针。假定没有空格。

  c = *ptr++;

  

  // Capture initial positive/minus character

  // 获取初始正/负的性格

  bool isnegative = false;   //负数标志

  if (c == '-') {

    isnegative = true;

    c = *ptr++;

  } else if (c == '+') {

    c = *ptr++;

  }

  

  // Extract number into fast integer. Track decimal in terms of exponent value. 快速提取数字整数。跟踪十进制的指数的值。

  uint32_t intval = 0;     //转为整数 整数变量

  int8_t exp = 0;

  uint8_t ndigit = 0; //第几位

  bool isdecimal = false;

  while(1) {

    c -= '0';

    if (c <= 9) {

      ndigit++;

      if (ndigit <= MAX_INT_DIGITS) { //最大位数

        if (isdecimal) { exp--; }

        intval = (((intval << 2) + intval) << 1) + c; // intval*10 + c

      } else {

        if (!(isdecimal)) { exp++; }  // Drop overflow digits 溢出位下降

      }

    } else if (c == (('.'-'0') & 0xff)  &&  !(isdecimal)) {

      isdecimal = true;

    } else {

      break;

    }

    c = *ptr++;

  }

  

  // Return if no digits have been read.

  if (!ndigit) { return(false); };

  

  // Convert integer into floating point.将整数转换为浮点数。

  float fval;

  fval = (float)intval;

  

  // Apply decimal. Should perform no more than two floating point multiplications for the

  // expected range of E0 to E-4.

  //应用小数。执行不应超过两个浮点乘法

  // E0预期范围的军医。

  if (fval != 0) {

    while (exp <= -2) {

      fval *= 0.01; 

      exp += 2;

    }

    if (exp < 0) { 

      fval *= 0.1; 

    } else if (exp > 0) {

      do {

        fval *= 10.0;

      } while (--exp > 0);

    } 

  }

 

  // Assign floating point value with correct sign.   指定浮点值与正确的信号。 

  if (isnegative) {

    *float_ptr = -fval;

  } else {

    *float_ptr = fval;

  }

 

  *char_counter = ptr - line - 1; // Set char_counter to next statement

  

  return(true);

}

 

 

// Delays variable defined milliseconds. Compiler compatibility fix for _delay_ms(),

// which only accepts constants in future compiler releases.

//延迟变量定义毫秒。编译器兼容性解决_delay_ms(),

//在未来的编译器版本只接受常量。

 

void delay_ms(uint16_t ms) 

{

  while ( ms-- ) { _delay_ms(1); }

}

 

 

// Delays variable defined microseconds. Compiler compatibility fix for _delay_us(),

// which only accepts constants in future compiler releases. Written to perform more 

// efficiently with larger delays, as the counter adds parasitic time in each iteration.

//延迟变量定义微秒。编译器兼容性解决_delay_us(),

//在未来的编译器版本只接受常量。写入执行更

//有效地延迟较大,当计数器增加寄生时间在每个迭代中。

 

void delay_us(uint32_t us) 

{

  while (us) {

    if (us < 10) { 

      _delay_us(1);

      us--;

    } else if (us < 100) {

      _delay_us(10);

      us -= 10;

    } else if (us < 1000) {

      _delay_us(100);

      us -= 100;

    } else {

      _delay_ms(1);

      us -= 1000;

    }

  }

}

 

 

// Simple hypotenuse computation function.

//简单的斜边计算函数。

 

float hypot_f(float x, float y) { return(sqrt(x*x + y*y)); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值