Grbl代码分析 Planner_recalculate()

该博客详细分析了Grbl控制系统中的Planner_recalculate()函数,该函数用于优化步进电机的运动规划,通过反向和正向通道计算每个块的最大入口速度,确保加速度和移动距离的合理搭配,以实现最佳的运动路径规划。

static void planner_recalculate()

{

  //读前一个块数据

  uint8_t block_index = plan_prev_block_index(block_buffer_head);

  // 如果前一个块已经优化了返回

  if (block_index == block_buffer_planned) { return; }

  /*粗略地最大化所有可能的加速曲线,从缓冲区的最后一个区块开始反向规划。当达到最后一个最优计划或尾部指针时,停止计划。注意:正向通道以后将完善和修正反向通道,以创建一个最佳计划。*/ 

 float entry_speed_sqr;

  plan_block_t *next;

  plan_block_t *current = &block_buffer[block_index];

  /*认为块最后的退出速度为0,按照块的加速度和移动距离计算出最大速度,这个速度和这块的进入速度比较,取最小值*/

  current->entry_speed_sqr = min( current->max_entry_speed_sqr, 2*current->acceleration*current->millimeters);

/*读取前一个块数据。后面的循环程序是从后往前计算每个块的进入速度,即计算块是不

是减速,如果是减速(小于max_entry_speed_sqr),设置进入速度= entry_speed_sqr,如果大于max_entry_speed_sqr,表示这一段不是全程减速*/

  block_index = plan_prev_block_index(block_index);

  if (block_index == block_buffer_planned) { //如果这个块已经优化了

// 如果这个块是block_buffer_tail,更新step buffer

if (block_index == block_buffer_tail) { st_update_plan_block_parameters(); }

  } else {

    while (block_index != block_buffer_planned) {//只要没有优化,循环计算速度

      next = current;

      current = &block_buffer[block_index];

      block_index = plan_prev_block_index(block_index);

      // 如果这个块是block_buffer_tail,更新step buffer

      if (block_index == block_buffer_tail) { st_update_plan_block_parameters(); }

speed.

      if (current->entry_speed_sqr != current->max_entry_speed_sqr) {

        entry_speed_sqr = next->entry_speed_sqr + 2*current->acceleration*current->millimeters;

        if (entry_speed_sqr < current->max_entry_speed_sqr) {

          current->entry_speed_sqr = entry_speed_sqr;

        } else {

          current->entry_speed_sqr = current->max_entry_speed_sqr;

        }

      }

    }

  }

  /*向前推算优化速度。从几经优化的块出发,按照加速过程计算,如果推算的进入速度小于next->entry_speed_sqr,设置进入速度= entry_speed_sqr。置优化*/

.  next = &block_buffer[block_buffer_planned]; // Begin at buffer planned pointer

  block_index = plan_next_block_index(block_buffer_planned);

  while (block_index != block_buffer_head) {

    current = next;

    next = &block_buffer[block_index];

    if (current->entry_speed_sqr < next->entry_speed_sqr) {

      entry_speed_sqr = current->entry_speed_sqr + 2*current->acceleration*current->millimeters;

      if (entry_speed_sqr < next->entry_speed_sqr) {

        next->entry_speed_sqr = entry_speed_sqr;

        block_buffer_planned = block_index;

      }

}

//如果进入速度已经是max_entry_speed_sqr,没有优化的空间了,置优化标志

if (next->entry_speed_sqr == next->max_entry_speed_sqr) { block_buffer_planned = block_index; }

    block_index = plan_next_block_index( block_index );

  }

}

ain()主函数首先执行下面初始化函数 serial_init(); // Setup serial baud rate and interrupts settings_init(); // Load Grbl settings from EEPROM stepper_init(); // 配置步进方向和中断定时器 system_init(); // 配置引脚分配别针和pin-change中断 memset(&sys, 0, sizeof(system_t)); // Clear all system variables sys.abort = true; // Set abort to complete initialization 完成初始化设置中止 sei(); // Enable interrupts #ifdef HOMING_INIT_LOCK //宏运算(settings.flags & (1 << 4)) != 0结果flags等于执行sys.state = STATE_ALARM //系统状态赋值为报警状态 if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; } #endif _____________________________________________________________________________________________________________________________________ 接下来是一些主要初始化循环 for(;;) { serial_reset_read_buffer(); // Clear serial read buffer gc_init(); // Set g-code parser to default state spindle_init(); //主轴 coolant_init(); //冷却液 limits_init(); //极限开关 probe_init(); //探测 plan_reset(); // Clear block buffer and planner variables 清晰块缓冲区和规划师变量 st_reset(); // Clear stepper subsystem variables. 清晰的步进系统变量。 // Sync cleared gcode and planner positions to current system position. 同步清除gcode和策划师职位当前系统位置。 plan_sync_position(); gc_sync_position(); // Reset system variables. sys.abort = false; //系统中止标志 sys_rt_exec_state = 0; //系统执行标志状态变量状态位清零。 sys_rt_exec_alarm = 0; //系统执行警报标志变量清零。 sys.suspend = false; //系统暂停标志位,取消,和安全的门。 sys.soft_limit = false; //系统限制标志状态机复位。(布尔) protocol_main_loop(); //主协议循环 } // ___________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值