Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | planner.h - buffers movement commands and manages the acceleration profile plan |
| 3 | Part of Grbl |
| 4 | |
| 5 | Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC |
| 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud |
| 7 | |
| 8 | Grbl is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | Grbl is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #ifndef planner_h |
| 23 | #define planner_h |
| 24 | |
| 25 | |
| 26 | // The number of linear motions that can be in the plan at any give time |
| 27 | #ifndef BLOCK_BUFFER_SIZE |
| 28 | #ifdef USE_LINE_NUMBERS |
| 29 | #define BLOCK_BUFFER_SIZE 15 |
| 30 | #else |
| 31 | #define BLOCK_BUFFER_SIZE 16 |
| 32 | #endif |
| 33 | #endif |
| 34 | |
| 35 | // Returned status message from planner. |
| 36 | #define PLAN_OK true |
| 37 | #define PLAN_EMPTY_BLOCK false |
| 38 | |
| 39 | // Define planner data condition flags. Used to denote running conditions of a block. |
| 40 | #define PL_COND_FLAG_RAPID_MOTION bit(0) |
| 41 | #define PL_COND_FLAG_SYSTEM_MOTION bit(1) // Single motion. Circumvents planner state. Used by home/park. |
| 42 | #define PL_COND_FLAG_NO_FEED_OVERRIDE bit(2) // Motion does not honor feed override. |
| 43 | #define PL_COND_FLAG_INVERSE_TIME bit(3) // Interprets feed rate value as inverse time when set. |
| 44 | #define PL_COND_FLAG_SPINDLE_CW bit(4) |
| 45 | #define PL_COND_FLAG_SPINDLE_CCW bit(5) |
| 46 | #define PL_COND_FLAG_COOLANT_FLOOD bit(6) |
| 47 | #define PL_COND_FLAG_COOLANT_MIST bit(7) |
| 48 | #define PL_COND_MOTION_MASK (PL_COND_FLAG_RAPID_MOTION|PL_COND_FLAG_SYSTEM_MOTION|PL_COND_FLAG_NO_FEED_OVERRIDE) |
| 49 | #define PL_COND_SPINDLE_MASK (PL_COND_FLAG_SPINDLE_CW|PL_COND_FLAG_SPINDLE_CCW) |
| 50 | #define PL_COND_ACCESSORY_MASK (PL_COND_FLAG_SPINDLE_CW|PL_COND_FLAG_SPINDLE_CCW|PL_COND_FLAG_COOLANT_FLOOD|PL_COND_FLAG_COOLANT_MIST) |
| 51 | |
| 52 | |
| 53 | // This struct stores a linear movement of a g-code block motion with its critical "nominal" values |
| 54 | // are as specified in the source g-code. |
| 55 | typedef struct { |
| 56 | // Fields used by the bresenham algorithm for tracing the line |
| 57 | // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values. |
| 58 | uint32_t steps[N_AXIS]; // Step count along each axis |
| 59 | uint32_t step_event_count; // The maximum step axis count and number of steps required to complete this block. |
| 60 | uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) |
| 61 | |
| 62 | // Block condition data to ensure correct execution depending on states and overrides. |
| 63 | uint8_t condition; // Block bitflag variable defining block run conditions. Copied from pl_line_data. |
| 64 | #ifdef USE_LINE_NUMBERS |
| 65 | int32_t line_number; // Block line number for real-time reporting. Copied from pl_line_data. |
| 66 | #endif |
| 67 | |
| 68 | // Fields used by the motion planner to manage acceleration. Some of these values may be updated |
| 69 | // by the stepper module during execution of special motion cases for replanning purposes. |
| 70 | float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2 |
| 71 | float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and |
| 72 | // neighboring nominal speeds with overrides in (mm/min)^2 |
| 73 | float acceleration; // Axis-limit adjusted line acceleration in (mm/min^2). Does not change. |
| 74 | float millimeters; // The remaining distance for this block to be executed in (mm). |
| 75 | // NOTE: This value may be altered by stepper algorithm during execution. |
| 76 | |
| 77 | // Stored rate limiting data used by planner when changes occur. |
| 78 | float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2 |
| 79 | float rapid_rate; // Axis-limit adjusted maximum rate for this block direction in (mm/min) |
| 80 | float programmed_rate; // Programmed rate of this block (mm/min). |
| 81 | |
| 82 | #ifdef VARIABLE_SPINDLE |
| 83 | // Stored spindle speed data used by spindle overrides and resuming methods. |
| 84 | float spindle_speed; // Block spindle speed. Copied from pl_line_data. |
| 85 | #endif |
| 86 | } plan_block_t; |
| 87 | |
| 88 | |
| 89 | // Planner data prototype. Must be used when passing new motions to the planner. |
| 90 | typedef struct { |
| 91 | float feed_rate; // Desired feed rate for line motion. Value is ignored, if rapid motion. |
| 92 | float spindle_speed; // Desired spindle speed through line motion. |
| 93 | uint8_t condition; // Bitflag variable to indicate planner conditions. See defines above. |
| 94 | #ifdef USE_LINE_NUMBERS |
| 95 | int32_t line_number; // Desired line number to report when executing. |
| 96 | #endif |
| 97 | } plan_line_data_t; |
| 98 | |
| 99 | |
| 100 | // Initialize and reset the motion plan subsystem |
| 101 | void plan_reset(); // Reset all |
| 102 | void plan_reset_buffer(); // Reset buffer only. |
| 103 | |
| 104 | // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position |
| 105 | // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed |
| 106 | // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. |
| 107 | uint8_t plan_buffer_line(float *target, plan_line_data_t *pl_data); |
| 108 | |
| 109 | // Called when the current block is no longer needed. Discards the block and makes the memory |
| 110 | // availible for new blocks. |
| 111 | void plan_discard_current_block(); |
| 112 | |
| 113 | // Gets the planner block for the special system motion cases. (Parking/Homing) |
| 114 | plan_block_t *plan_get_system_motion_block(); |
| 115 | |
| 116 | // Gets the current block. Returns NULL if buffer empty |
| 117 | plan_block_t *plan_get_current_block(); |
| 118 | |
| 119 | // Called periodically by step segment buffer. Mostly used internally by planner. |
| 120 | uint8_t plan_next_block_index(uint8_t block_index); |
| 121 | |
| 122 | // Called by step segment buffer when computing executing block velocity profile. |
| 123 | float plan_get_exec_block_exit_speed_sqr(); |
| 124 | |
| 125 | // Called by main program during planner calculations and step segment buffer during initialization. |
| 126 | float plan_compute_profile_nominal_speed(plan_block_t *block); |
| 127 | |
| 128 | // Re-calculates buffered motions profile parameters upon a motion-based override change. |
| 129 | void plan_update_velocity_profile_parameters(); |
| 130 | |
| 131 | // Reset the planner position vector (in steps) |
| 132 | void plan_sync_position(); |
| 133 | |
| 134 | // Reinitialize plan with a partially completed block |
| 135 | void plan_cycle_reinitialize(); |
| 136 | |
| 137 | // Returns the number of available blocks are in the planner buffer. |
| 138 | uint8_t plan_get_block_buffer_available(); |
| 139 | |
| 140 | // Returns the number of active blocks are in the planner buffer. |
| 141 | // NOTE: Deprecated. Not used unless classic status reports are enabled in config.h |
| 142 | uint8_t plan_get_block_buffer_count(); |
| 143 | |
| 144 | // Returns the status of the block ring buffer. True, if buffer is full. |
| 145 | uint8_t plan_check_full_buffer(); |
| 146 | |
| 147 | void plan_get_planner_mpos(float *target); |
| 148 | |
| 149 | |
| 150 | #endif |