diff --git a/README.md b/README.md index 0ed3006..e29a19e 100644 --- a/README.md +++ b/README.md @@ -5,22 +5,24 @@ This branch serves only as a developmental platform for working on new ideas tha ------------ -Grbl is a no-compromise, high performance, low cost alternative to parallel-port-based motion control for CNC milling. It will run on a vanilla Arduino (Duemillanove/Uno) as long as it sports an Atmega 328. +Grbl is a no-compromise, high performance, low cost alternative to parallel-port-based motion control for CNC milling. It will run on a vanilla Arduino (Duemillanove/Uno) as long as it sports an Atmega 328. (Other AVR CPUs are unofficially supported as well.) -The controller is written in highly optimized C utilizing every clever feature of the AVR-chips to achieve precise timing and asynchronous operation. It is able to m aintain more than 30kHz of stable, jitter free control pulses. +The controller is written in highly optimized C utilizing every clever feature of the AVR-chips to achieve precise timing and asynchronous operation. It is able to maintain up to 30kHz of stable, jitter free control pulses. -It accepts standards-compliant G-code and has been tested with the output of several CAM tools with no problems. Arcs, circles and helical motion are fully supported, as well as, other basic functional g-code commands. Functions and variables are not currently supported, but may be included in future releases in a form of a pre-processor. +It accepts standards-compliant G-code and has been tested with the output of several CAM tools with no problems. Arcs, circles and helical motion are fully supported, as well as, other basic functional g-code commands. Although canned cycles, functions, and variables are not currently supported (may in the future), GUIs can be built or modified easily to translate to straight g-code for Grbl. Grbl includes full acceleration management with look ahead. That means the controller will look up to 18 motions into the future and plan its velocities ahead to deliver smooth acceleration and jerk-free cornering. ##Changelog for v0.9 from v0.8 - **ALPHA status: Under heavy development.** - New stepper algorithm: Based on an inverse time algorithm, but modified to ensure steps are executed exactly. This algorithm performs a constant timer tick and has a hard limit of 30kHz maximum step frequency. It is also highly tuneable and should be very easy to port to other microcontroller architectures. Overall, a much better, smoother stepper algorithm with the capability of very high speeds. - - Planner optimizations: Multiple changes to increase planner execution speed and removed redundant variables. + - Planner optimizations: Planning computations improved four-fold or more. Changes include streaming optimizations by ignoring already optimized blocks and removing redundant variables and computations and offloading them to the stepper algorithm on an ad-hoc basis. + - Planner stability: Previous Grbl planners have all had a corruption issue in rare circumstances that becomes particularly problematic at high step frequencies and when jogging. The new planner is robust and incorruptible, meaning that we can fearlessly drive Grbl to it's highest limits. Combined with the new stepper algorithm and planner optimizations, this means 5x to 10x performance increases in our testing! This is all achieved through the introduction of an intermediary step segment buffer that "checks-out" steps from the planner buffer in real-time. - Acceleration independence: Each axes may be defined with different acceleration parameters and Grbl will automagically calculate the maximum acceleration through a path depending on the direction traveled. This is very useful for machine that have very different axes properties, like the ShapeOko z-axis. - Maximum velocity independence: As with acceleration, the maximum velocity of individual axes may be defined. All seek/rapids motions will move at these maximum rates, but never exceed any one axes. So, when two or more axes move, the limiting axis will move at its maximum rate, while the other axes are scaled down. - Significantly improved arc performance: Arcs are now defined in terms of chordal tolerance, rather than segment length. Chordal tolerance will automatically scale all arc line segments depending on arc radius, such that the error does not exceed the tolerance value (default: 0.005 mm.) So, for larger radii arcs, Grbl can move faster through them, because the segments are always longer and the planner has more distance to plan with. - Soft limits: Checks if any motion command exceeds workspace limits. Alarms out when found. Another safety feature, but, unlike hard limits, position does not get lost, as it forces a feed hold before erroring out. + - Pin mapping: In an effort for Grbl to be compatible with other AVR architectures, such as the 1280 or 2560, a new pin_map.h configuration file has been created to allow Grbl to be compiled for them. This is currently user supported, so your mileage may vary. If you run across a bug, please let us know or better send us a fix! Thanks in advance! - New Grbl SIMULATOR by @jgeisler: A completely independent wrapper of the Grbl main source code that may be compiled as an executable on a computer. No Arduino required. Simply simulates the responses of Grbl as if it was on an Arduino. May be used for many things: checking out how Grbl works, pre-process moves for GUI graphics, debugging of new features, etc. Much left to do, but potentially very powerful, as the dummy AVR variables can be written to output anything you need. - Homing routine updated: Sets workspace volume in all negative space regardless of limit switch position. Common on pro CNCs. Also reduces soft limits CPU overhead. - Feedrate overrides: In the works, but planner has begun to be re-factored for this feature. diff --git a/archive/planner_dist.c b/archive/planner_dist.c deleted file mode 100644 index 0afa018..0000000 --- a/archive/planner_dist.c +++ /dev/null @@ -1,669 +0,0 @@ -/* - planner.c - buffers movement commands and manages the acceleration profile plan - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - Copyright (c) 2011 Jens Geisler - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -/* The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis. */ - -#include -#include -#include "planner.h" -#include "nuts_bolts.h" -#include "stepper.h" -#include "settings.h" -#include "config.h" -#include "protocol.h" - -#define SOME_LARGE_VALUE 1.0E+38 // Used by rapids and acceleration maximization calculations. Just needs - // to be larger than any feasible (mm/min)^2 or mm/sec^2 value. - -static plan_block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions -static volatile uint8_t block_buffer_tail; // Index of the block to process now -static uint8_t block_buffer_head; // Index of the next block to be pushed -static uint8_t next_buffer_head; // Index of the next buffer head -static uint8_t block_buffer_planned; // Index of the optimally planned block - -// Define planner variables -typedef struct { - int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate - // from g-code position for movements requiring multiple line motions, - // i.e. arcs, canned cycles, and backlash compensation. - float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment - float previous_nominal_speed_sqr; // Nominal speed of previous path line segment -} planner_t; -static planner_t pl; - - -// Returns the index of the next block in the ring buffer. Also called by stepper segment buffer. -// NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication. -uint8_t plan_next_block_index(uint8_t block_index) -{ - block_index++; - if (block_index == BLOCK_BUFFER_SIZE) { block_index = 0; } - return(block_index); -} - - -// Returns the index of the previous block in the ring buffer -static uint8_t plan_prev_block_index(uint8_t block_index) -{ - if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; } - block_index--; - return(block_index); -} - - -// Update the entry speed and millimeters remaining to execute for a partially completed block. Called only -// when the planner knows it will be changing the conditions of this block. -// TODO: Set up to be called from planner calculations. Need supporting code framework still, i.e. checking -// and executing this only when necessary, combine with the block_buffer_safe pointer. -// TODO: This is very similar to the planner reinitialize after a feed hold. Could make this do double duty. -void plan_update_partial_block(uint8_t block_index, float exit_speed_sqr) -{ -// TODO: Need to make a condition to check if we need make these calculations. We don't if nothing has -// been executed or placed into segment buffer. This happens with the first block upon startup or if -// the segment buffer is exactly in between two blocks. Just check if the step_events_remaining is equal -// the total step_event_count in the block. If so, we don't have to do anything. - - // !!! block index is the same as block_buffer_safe. - // See if we can reduce this down to just requesting the millimeters remaining.. - uint8_t is_decelerating; - float millimeters_remaining = 0.0; - st_fetch_partial_block_parameters(block_index, &millimeters_remaining, &is_decelerating); - - if (millimeters_remaining != 0.0) { - // Point to current block partially executed by stepper algorithm - plan_block_t *partial_block = plan_get_block_by_index(block_index); - - // Compute the midway speed of the partially completely block at the end of the segment buffer. - if (is_decelerating) { // Block is decelerating - partial_block->entry_speed_sqr = exit_speed_sqr - 2*partial_block->acceleration*millimeters_remaining; - } else { // Block is accelerating or cruising - partial_block->entry_speed_sqr += 2*partial_block->acceleration*(partial_block->millimeters-millimeters_remaining); - partial_block->entry_speed_sqr = min(partial_block->entry_speed_sqr, partial_block->nominal_speed_sqr); - } - - // Update only the relevant planner block information so the planner can plan correctly. - partial_block->millimeters = millimeters_remaining; - partial_block->max_entry_speed_sqr = partial_block->entry_speed_sqr; // Not sure if this needs to be updated. - } -} - - -/* PLANNER SPEED DEFINITION - +--------+ <- current->nominal_speed - / \ - current->entry_speed -> + \ - | + <- next->entry_speed (aka exit speed) - +-------------+ - time --> - - Recalculates the motion plan according to the following basic guidelines: - - 1. Go over every feasible block sequentially in reverse order and calculate the junction speeds - (i.e. current->entry_speed) such that: - a. No junction speed exceeds the pre-computed maximum junction speed limit or nominal speeds of - neighboring blocks. - b. A block entry speed cannot exceed one reverse-computed from its exit speed (next->entry_speed) - with a maximum allowable deceleration over the block travel distance. - c. The last (or newest appended) block is planned from a complete stop (an exit speed of zero). - 2. Go over every block in chronological (forward) order and dial down junction speed values if - a. The exit speed exceeds the one forward-computed from its entry speed with the maximum allowable - acceleration over the block travel distance. - - When these stages are complete, the planner will have maximized the velocity profiles throughout the all - of the planner blocks, where every block is operating at its maximum allowable acceleration limits. In - other words, for all of the blocks in the planner, the plan is optimal and no further speed improvements - are possible. If a new block is added to the buffer, the plan is recomputed according to the said - guidelines for a new optimal plan. - - To increase computational efficiency of these guidelines, a set of planner block pointers have been - created to indicate stop-compute points for when the planner guidelines cannot logically make any further - changes or improvements to the plan when in normal operation and new blocks are streamed and added to the - planner buffer. For example, if a subset of sequential blocks in the planner have been planned and are - bracketed by junction velocities at their maximums (or by the first planner block as well), no new block - added to the planner buffer will alter the velocity profiles within them. So we no longer have to compute - them. Or, if a set of sequential blocks from the first block in the planner (or a optimal stop-compute - point) are all accelerating, they are all optimal and can not be altered by a new block added to the - planner buffer, as this will only further increase the plan speed to chronological blocks until a maximum - junction velocity is reached. However, if the operational conditions of the plan changes from infrequently - used feed holds or feedrate overrides, the stop-compute pointers will be reset and the entire plan is - recomputed as stated in the general guidelines. - - Planner buffer index mapping: - - block_buffer_tail: Points to the beginning of the planner buffer. First to be executed or being executed. - - block_buffer_head: Points to the buffer block after the last block in the buffer. Used to indicate whether - the buffer is full or empty. As described for standard ring buffers, this block is always empty. - - next_buffer_head: Points to next planner buffer block after the buffer head block. When equal to the - buffer tail, this indicates the buffer is full. - - block_buffer_safe: Points to the first sequential planner block for which it is safe to recompute, which - is defined to be where the stepper's step segment buffer ends. This may or may not be the buffer tail, - since the step segment buffer queues steps which may have not finished executing and could span a few - blocks, if the block moves are very short. - - block_buffer_planned: Points to the first buffer block after the last optimally planned block for normal - streaming operating conditions. Use for planning optimizations by avoiding recomputing parts of the - planner buffer that don't change with the addition of a new block, as describe above. - - NOTE: All planner computations are performed in floating point to minimize numerical round-off errors. - When a planner block is executed, the floating point values are converted to fast integers by the stepper - algorithm segment buffer. See the stepper module for details. - - NOTE: Since the planner only computes on what's in the planner buffer, some motions with lots of short - line segments, like G2/3 arcs or complex curves, may seem to move slow. This is because there simply isn't - enough combined distance traveled in the entire buffer to accelerate up to the nominal speed and then - decelerate to a complete stop at the end of the buffer, as stated by the guidelines. If this happens and - becomes an annoyance, there are a few simple solutions: (1) Maximize the machine acceleration. The planner - will be able to compute higher velocity profiles within the same combined distance. (2) Maximize line - segment(s) distance per block to a desired tolerance. The more combined distance the planner has to use, - the faster it can go. (3) Maximize the planner buffer size. This also will increase the combined distance - for the planner to compute over. It also increases the number of computations the planner has to perform - to compute an optimal plan, so select carefully. The Arduino 328p memory is already maxed out, but future - ARM versions should have enough memory and speed for look-ahead blocks numbering up to a hundred or more. - -*/ -static void planner_recalculate() -{ - - // Initialize block index to the last block in the planner buffer. - uint8_t block_index = plan_prev_block_index(block_buffer_head); - - // Query stepper module for safe planner block index to recalculate to, which corresponds to the end - // of the step segment buffer. - uint8_t block_buffer_safe = st_get_prep_block_index(); - - // TODO: Make sure that we don't have to check for the block_buffer_tail condition, if the stepper module - // returns a NULL pointer or something. This could happen when the segment buffer is empty. Although, - // this call won't return a NULL, only an index.. I have to make sure that this index is synced with the - // planner at all times. - - // Recompute plan only when there is more than one planner block in the buffer. Can't do anything with one. - // NOTE: block_buffer_safe can be the last planner block if the segment buffer has completely queued up the - // remainder of the planner buffer. In this case, a new planner block will be treated as a single block. - if (block_index == block_buffer_safe) { // Also catches (head-1) = tail - - // Just set block_buffer_planned pointer. - block_buffer_planned = block_index; - - // TODO: Feedrate override of one block needs to update the partial block with an exit speed of zero. For - // a single added block and recalculate after a feed hold, we don't need to compute this, since we already - // know that the velocity starts and ends at zero. With an override, we can be traveling at some midblock - // rate, and we have to calculate the new velocity profile from it. - // plan_update_partial_block(block_index,0.0); - - } else { - - // TODO: If the nominal speeds change during a feedrate override, we need to recompute the max entry speeds for - // all junctions before proceeding. - - // Initialize planner buffer pointers and indexing. - plan_block_t *current = &block_buffer[block_index]; - - // Calculate maximum entry speed for last block in buffer, where the exit speed is always zero. - current->entry_speed_sqr = min( current->max_entry_speed_sqr, 2*current->acceleration*current->millimeters); - - // Reverse Pass: Coarsely maximize all possible deceleration curves back-planning from the last - // block in buffer. Cease planning when: (1) the last optimal planned pointer is reached. - // (2) the safe block pointer is reached, whereby the planned pointer is updated. - // NOTE: Forward pass will later refine and correct the reverse pass to create an optimal plan. - // NOTE: If the safe block is encountered before the planned block pointer, we know the safe block - // will be recomputed within the plan. So, we need to update it if it is partially completed. - float entry_speed_sqr; - plan_block_t *next; - block_index = plan_prev_block_index(block_index); - - if (block_index == block_buffer_safe) { // !! OR plan pointer? Yes I think so. - - // Only two plannable blocks in buffer. Compute previous block based on - // !!! May only work if a new block is being added. Not for an override. The exit speed isn't zero. - // !!! Need to make the current entry speed calculation after this. - plan_update_partial_block(block_index, 0.0); - block_buffer_planned = block_index; - - } else { - - // Three or more plan-able - while (block_index != block_buffer_planned) { - - next = current; - current = &block_buffer[block_index]; - - // Increment block index early to check if the safe block is before the current block. If encountered, - // this is an exit condition as we can't go further than this block in the reverse pass. - block_index = plan_prev_block_index(block_index); - if (block_index == block_buffer_safe) { - // Check if the safe block is partially completed. If so, update it before its exit speed - // (=current->entry speed) is over-written. - // TODO: The update breaks with feedrate overrides, because the replanning process no longer has - // the previous nominal speed to update this block with. There will need to be something along the - // lines of a nominal speed change check and send the correct value to this function. - plan_update_partial_block(block_index,current->entry_speed_sqr); - - // Set planned pointer at safe block and for loop exit after following computation is done. - block_buffer_planned = block_index; - } - - // Compute maximum entry speed decelerating over the current block from its exit 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; - } - } - } - - } - - // Forward Pass: Forward plan the acceleration curve from the planned pointer onward. - // Also scans for optimal plan breakpoints and appropriately updates the planned pointer. - 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]; - - // Any acceleration detected in the forward pass automatically moves the optimal planned - // pointer forward, since everything before this is all optimal. In other words, nothing - // can improve the plan from the buffer tail to the planned pointer by logic. - // TODO: Need to check if the planned flag logic is correct for all scenarios. It may not - // be for certain conditions. However, if the block reaches nominal speed, it can be a valid - // breakpoint substitute. - if (current->entry_speed_sqr < next->entry_speed_sqr) { - entry_speed_sqr = current->entry_speed_sqr + 2*current->acceleration*current->millimeters; - // If true, current block is full-acceleration and we can move the planned pointer forward. - if (entry_speed_sqr < next->entry_speed_sqr) { - next->entry_speed_sqr = entry_speed_sqr; // Always <= max_entry_speed_sqr. Backward pass sets this. - block_buffer_planned = block_index; // Set optimal plan pointer. - } - } - - // Any block set at its maximum entry speed also creates an optimal plan up to this - // point in the buffer. When the plan is bracketed by either the beginning of the - // buffer and a maximum entry speed or two maximum entry speeds, every block in between - // cannot logically be further improved. Hence, we don't have to recompute them anymore. - if (next->entry_speed_sqr == next->max_entry_speed_sqr) { - block_buffer_planned = block_index; // Set optimal plan pointer - } - - block_index = plan_next_block_index( block_index ); - } - - } - -} - - -void plan_reset_buffer() -{ - block_buffer_planned = block_buffer_tail; -} - - -void plan_init() -{ - block_buffer_tail = 0; - block_buffer_head = 0; // Empty = tail - next_buffer_head = 1; // plan_next_block_index(block_buffer_head) - plan_reset_buffer(); - memset(&pl, 0, sizeof(pl)); // Clear planner struct -} - - -void plan_discard_current_block() -{ - if (block_buffer_head != block_buffer_tail) { // Discard non-empty buffer. - block_buffer_tail = plan_next_block_index( block_buffer_tail ); - } -} - - -plan_block_t *plan_get_current_block() -{ - if (block_buffer_head == block_buffer_tail) { // Buffer empty - plan_reset_buffer(); - return(NULL); - } - return(&block_buffer[block_buffer_tail]); -} - - -plan_block_t *plan_get_block_by_index(uint8_t block_index) -{ - if (block_buffer_head == block_index) { return(NULL); } - return(&block_buffer[block_index]); -} - - -// Returns the availability status of the block ring buffer. True, if full. -uint8_t plan_check_full_buffer() -{ - if (block_buffer_tail == next_buffer_head) { return(true); } - return(false); -} - - -// Block until all buffered steps are executed or in a cycle state. Works with feed hold -// during a synchronize call, if it should happen. Also, waits for clean cycle end. -void plan_synchronize() -{ - while (plan_get_current_block() || sys.state == STATE_CYCLE) { - protocol_execute_runtime(); // Check and execute run-time commands - if (sys.abort) { return; } // Check for system abort - } -} - - -/* Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position - in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed - rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. - All position data passed to the planner must be in terms of machine position to keep the planner - independent of any coordinate system changes and offsets, which are handled by the g-code parser. - NOTE: Assumes buffer is available. Buffer checks are handled at a higher level by motion_control. - In other words, the buffer head is never equal to the buffer tail. Also the feed rate input value - is used in three ways: as a normal feed rate if invert_feed_rate is false, as inverse time if - invert_feed_rate is true, or as seek/rapids rate if the feed_rate value is negative (and - invert_feed_rate always false). */ -void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate) -{ - // Prepare and initialize new block - plan_block_t *block = &block_buffer[block_buffer_head]; - block->step_event_count = 0; - block->millimeters = 0; - block->direction_bits = 0; - block->acceleration = SOME_LARGE_VALUE; // Scaled down to maximum acceleration later - - // Compute and store initial move distance data. - // TODO: After this for-loop, we don't touch the stepper algorithm data. Might be a good idea - // to try to keep these types of things completely separate from the planner for portability. - int32_t target_steps[N_AXIS]; - float unit_vec[N_AXIS], delta_mm; - uint8_t idx; - for (idx=0; idxsteps[idx] = labs(target_steps[idx]-pl.position[idx]); - block->step_event_count = max(block->step_event_count, block->steps[idx]); - - // Compute individual axes distance for move and prep unit vector calculations. - // NOTE: Computes true distance from converted step values. - delta_mm = (target_steps[idx] - pl.position[idx])/settings.steps_per_mm[idx]; - unit_vec[idx] = delta_mm; // Store unit vector numerator. Denominator computed later. - - // Set direction bits. Bit enabled always means direction is negative. - if (delta_mm < 0 ) { block->direction_bits |= get_direction_mask(idx); } - - // Incrementally compute total move distance by Euclidean norm. First add square of each term. - block->millimeters += delta_mm*delta_mm; - } - block->millimeters = sqrt(block->millimeters); // Complete millimeters calculation with sqrt() - - // Bail if this is a zero-length block. Highly unlikely to occur. - if (block->step_event_count == 0) { return; } - - // Adjust feed_rate value to mm/min depending on type of rate input (normal, inverse time, or rapids) - // TODO: Need to distinguish a rapids vs feed move for overrides. Some flag of some sort. - if (feed_rate < 0) { feed_rate = SOME_LARGE_VALUE; } // Scaled down to absolute max/rapids rate later - else if (invert_feed_rate) { feed_rate = block->millimeters/feed_rate; } - - // Calculate the unit vector of the line move and the block maximum feed rate and acceleration scaled - // down such that no individual axes maximum values are exceeded with respect to the line direction. - // NOTE: This calculation assumes all axes are orthogonal (Cartesian) and works with ABC-axes, - // if they are also orthogonal/independent. Operates on the absolute value of the unit vector. - float inverse_unit_vec_value; - float inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple float divides - float junction_cos_theta = 0; - for (idx=0; idxacceleration = min(block->acceleration,settings.acceleration[idx]*inverse_unit_vec_value); - - // Incrementally compute cosine of angle between previous and current path. Cos(theta) of the junction - // between the current move and the previous move is simply the dot product of the two unit vectors, - // where prev_unit_vec is negative. Used later to compute maximum junction speed. - junction_cos_theta -= pl.previous_unit_vec[idx] * unit_vec[idx]; - } - } - - - // TODO: Need to check this method handling zero junction speeds when starting from rest. - if (block_buffer_head == block_buffer_tail) { - - // Initialize block entry speed as zero. Assume it will be starting from rest. Planner will correct this later. - block->entry_speed_sqr = 0.0; - block->max_junction_speed_sqr = 0.0; // Starting from rest. Enforce start from zero velocity. - - } else { - /* - Compute maximum allowable entry speed at junction by centripetal acceleration approximation. - Let a circle be tangent to both previous and current path line segments, where the junction - deviation is defined as the distance from the junction to the closest edge of the circle, - colinear with the circle center. The circular segment joining the two paths represents the - path of centripetal acceleration. Solve for max velocity based on max acceleration about the - radius of the circle, defined indirectly by junction deviation. This may be also viewed as - path width or max_jerk in the previous grbl version. This approach does not actually deviate - from path, but used as a robust way to compute cornering speeds, as it takes into account the - nonlinearities of both the junction angle and junction velocity. - - NOTE: If the junction deviation value is finite, Grbl executes the motions in an exact path - mode (G61). If the junction deviation value is zero, Grbl will execute the motion in an exact - stop mode (G61.1) manner. In the future, if continuous mode (G64) is desired, the math here - is exactly the same. Instead of motioning all the way to junction point, the machine will - just follow the arc circle defined here. The Arduino doesn't have the CPU cycles to perform - a continuous mode path, but ARM-based microcontrollers most certainly do. - - NOTE: The max junction speed is a fixed value, since machine acceleration limits cannot be - changed dynamically during operation nor can the line move geometry. This must be kept in - memory in the event of a feedrate override changing the nominal speeds of blocks, which can - change the overall maximum entry speed conditions of all blocks. - */ - // NOTE: Computed without any expensive trig, sin() or acos(), by trig half angle identity of cos(theta). - float sin_theta_d2 = sqrt(0.5*(1.0-junction_cos_theta)); // Trig half angle identity. Always positive. - - // TODO: Acceleration used in calculation needs to be limited by the minimum of the two junctions. - block->max_junction_speed_sqr = max( MINIMUM_JUNCTION_SPEED*MINIMUM_JUNCTION_SPEED, - (block->acceleration * settings.junction_deviation * sin_theta_d2)/(1.0-sin_theta_d2) ); - } - - // Store block nominal speed - block->nominal_speed_sqr = feed_rate*feed_rate; // (mm/min). Always > 0 - - // Compute the junction maximum entry based on the minimum of the junction speed and neighboring nominal speeds. - block->max_entry_speed_sqr = min(block->max_junction_speed_sqr, - min(block->nominal_speed_sqr,pl.previous_nominal_speed_sqr)); - - // Update previous path unit_vector and nominal speed (squared) - memcpy(pl.previous_unit_vec, unit_vec, sizeof(unit_vec)); // pl.previous_unit_vec[] = unit_vec[] - pl.previous_nominal_speed_sqr = block->nominal_speed_sqr; - - // Update planner position - memcpy(pl.position, target_steps, sizeof(target_steps)); // pl.position[] = target_steps[] - - // New block is all set. Update buffer head and next buffer head indices. - block_buffer_head = next_buffer_head; - next_buffer_head = plan_next_block_index(block_buffer_head); - - // Finish up by recalculating the plan with the new block. - planner_recalculate(); - -// int32_t blength = block_buffer_head - block_buffer_tail; -// if (blength < 0) { blength += BLOCK_BUFFER_SIZE; } -// printInteger(blength); - - -} - - -// Reset the planner position vectors. Called by the system abort/initialization routine. -void plan_sync_position() -{ - uint8_t idx; - for (idx=0; idx + - +--------+ <- nominal_speed /|\ - / \ / | \ - entry_speed -> + \ / | + <- next->entry_speed - | + <- next->entry_speed / | | - +-------------+ entry_speed -> +----+--+ - time --> ^ ^ ^ ^ - | | | | - decelerate distance decelerate distance - - Calculates the type of velocity profile for a given planner block and provides the deceleration - distance for the stepper algorithm to use to accurately trace the profile exactly. The planner - computes the entry and exit speeds of each block, but does not bother to determine the details of - the velocity profiles within them, as they aren't needed for computing an optimal plan. When the - stepper algorithm begins to execute a block, the block velocity profiles are computed ad hoc. - - Each block velocity profiles can be described as either a trapezoidal or a triangular shape. The - trapezoid occurs when the block reaches the nominal speed of the block and cruises for a period of - time. A triangle occurs when the nominal speed is not reached within the block. Both of these - velocity profiles may also be truncated on either end with no acceleration or deceleration ramps, - as they can be influenced by the conditions of neighboring blocks, where the acceleration ramps - are defined by constant acceleration equal to the maximum allowable acceleration of a block. - - Since the stepper algorithm already assumes to begin executing a planner block by accelerating - from the planner entry speed and cruise if the nominal speed is reached, we only need to know - when to begin deceleration to the end of the block. Hence, only the distance from the end of the - block to begin a deceleration ramp is computed for the stepper algorithm when requested. -*/ -float plan_calculate_velocity_profile(uint8_t block_index) -{ - plan_block_t *current_block = &block_buffer[block_index]; - - // Determine current block exit speed - float exit_speed_sqr = 0.0; // Initialize for end of planner buffer. Zero speed. - plan_block_t *next_block = plan_get_block_by_index(plan_next_block_index(block_index)); - if (next_block != NULL) { exit_speed_sqr = next_block->entry_speed_sqr; } // Exit speed is the entry speed of next buffer block - - // First determine intersection distance (in steps) from the exit point for a triangular profile. - // Computes: d_intersect = distance/2 + (v_entry^2-v_exit^2)/(4*acceleration) - float intersect_distance = 0.5*( current_block->millimeters + (current_block->entry_speed_sqr-exit_speed_sqr)/(2*current_block->acceleration) ); - - // Check if this is a pure acceleration block by a intersection distance less than zero. Also - // prevents signed and unsigned integer conversion errors. - if (intersect_distance > 0 ) { - float decelerate_distance; - // Determine deceleration distance (in steps) from nominal speed to exit speed for a trapezoidal profile. - // Value is never negative. Nominal speed is always greater than or equal to the exit speed. - // Computes: d_decelerate = (v_nominal^2 - v_exit^2)/(2*acceleration) - decelerate_distance = (current_block->nominal_speed_sqr - exit_speed_sqr)/(2*current_block->acceleration); - - // The lesser of the two triangle and trapezoid distances always defines the velocity profile. - if (decelerate_distance > intersect_distance) { decelerate_distance = intersect_distance; } - - // Finally, check if this is a pure deceleration block. - if (decelerate_distance > current_block->millimeters) { return(0.0); } - else { return( (current_block->millimeters-decelerate_distance) ); } - } - return( current_block->millimeters ); // No deceleration in velocity profile. -} - - -// Re-initialize buffer plan with a partially completed block, assumed to exist at the buffer tail. -// Called after a steppers have come to a complete stop for a feed hold and the cycle is stopped. -void plan_cycle_reinitialize(int32_t step_events_remaining) -{ - plan_block_t *block = &block_buffer[block_buffer_tail]; // Point to partially completed block - - // Only remaining millimeters and step_event_count need to be updated for planner recalculate. - // Other variables (step_x, step_y, step_z, rate_delta, etc.) all need to remain the same to - // ensure the original planned motion is resumed exactly. - block->millimeters = (block->millimeters*step_events_remaining)/block->step_event_count; - block->step_event_count = step_events_remaining; - - // Re-plan from a complete stop. Reset planner entry speeds and buffer planned pointer. - block->entry_speed_sqr = 0.0; - block->max_entry_speed_sqr = 0.0; - block_buffer_planned = block_buffer_tail; - planner_recalculate(); -} - - -/* -TODO: - When a feed hold or feedrate override is reduced, the velocity profile must execute a - deceleration over the existing plan. By logic, since the plan already decelerates to zero - at the end of the buffer, any replanned deceleration mid-way will never exceed this. It - will only asymptotically approach this in the worst case scenario. - - - For a feed hold, we simply need to plan and compute the stopping point within a block - when velocity decelerates to zero. We then can recompute the plan with the already - existing partial block planning code and set the system to a QUEUED state. - - When a feed hold is initiated, the main program should be able to continue doing what - it has been, i.e. arcs, parsing, but needs to be able to reinitialize the plan after - it has come to a stop. - - - For a feed rate override (reduce-only), we need to enforce a deceleration until we - intersect the reduced nominal speed of a block after it's been planned with the new - overrides and the newly planned block is accelerating or cruising only. If the new plan - block is decelerating at the intersection point, we keep decelerating until we find a - valid intersection point. Once we find this point, we can then resume onto the new plan, - but we may need to adjust the deceleration point in the intersection block since the - feedrate override could have intersected at an acceleration ramp. This would change the - acceleration ramp to a cruising, so the deceleration point will have changed, but the - plan will have not. It should still be valid for the rest of the buffer. Coding this - can get complicated, but it should be doable. One issue could be is in how to handle - scenarios when a user issues several feedrate overrides and inundates this code. Does - this method still work and is robust enough to compute all of this on the fly? This is - the critical question. However, we could block user input until the planner has time to - catch to solve this as well. - - - When the feed rate override increases, we don't have to do anything special. We just - replan the entire buffer with the new nominal speeds and adjust the maximum junction - speeds accordingly. - -void plan_compute_deceleration() { - -} - - -void plan_recompute_max_junction_velocity() { - // Assumes the nominal_speed_sqr values have been updated. May need to just multiply - // override values here. - // PROBLEM: Axes-limiting velocities get screwed up. May need to store an int8 value for the - // max override value possible for each block when the line is added. So the nominal_speed - // is computed with that ceiling, but still retained if the rates change again. - uint8_t block_index = block_buffer_tail; - plan_block_t *block = &block_buffer[block_index]; - pl.previous_nominal_speed_sqr = block->nominal_speed_sqr; - block_index = plan_next_block_index(block_index); - while (block_index != block_buffer_head) { - block = &block_buffer[block_index]; - block->max_entry_speed_sqr = min(block->max_junction_speed_sqr, - min(block->nominal_speed_sqr,pl.previous_nominal_speed_sqr)); - pl.previous_nominal_speed_sqr = block->nominal_speed_sqr; - block_index = plan_next_block_index(block_index); - } -} - -*/ diff --git a/archive/planner_time_archive.c b/archive/planner_time_archive.c deleted file mode 100644 index 9f584df..0000000 --- a/archive/planner_time_archive.c +++ /dev/null @@ -1,459 +0,0 @@ -/* - planner.c - buffers movement commands and manages the acceleration profile plan - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - Copyright (c) 2011 Jens Geisler - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -/* The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis. */ - -#include -#include -#include "planner.h" -#include "nuts_bolts.h" -#include "stepper.h" -#include "settings.h" -#include "config.h" -#include "protocol.h" - -#define SOME_LARGE_VALUE 1.0E+38 // Used by rapids and acceleration maximization calculations. Just needs - // to be larger than any feasible (mm/min)^2 or mm/sec^2 value. - -static plan_block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions -static uint8_t block_buffer_tail; // Index of the block to process now -static uint8_t block_buffer_head; // Index of the next block to be pushed -static uint8_t next_buffer_head; // Index of the next buffer head -static uint8_t block_buffer_planned; // Index of the optimally planned block - -// Define planner variables -typedef struct { - int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate - // from g-code position for movements requiring multiple line motions, - // i.e. arcs, canned cycles, and backlash compensation. - float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment - float previous_nominal_speed_sqr; // Nominal speed of previous path line segment -} planner_t; -static planner_t pl; - - -// Returns the index of the next block in the ring buffer. Also called by stepper segment buffer. -uint8_t plan_next_block_index(uint8_t block_index) -{ - block_index++; - if (block_index == BLOCK_BUFFER_SIZE) { block_index = 0; } - return(block_index); -} - - -// Returns the index of the previous block in the ring buffer -static uint8_t plan_prev_block_index(uint8_t block_index) -{ - if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; } - block_index--; - return(block_index); -} - - -/* PLANNER SPEED DEFINITION - +--------+ <- current->nominal_speed - / \ - current->entry_speed -> + \ - | + <- next->entry_speed (aka exit speed) - +-------------+ - time --> - - Recalculates the motion plan according to the following basic guidelines: - - 1. Go over every feasible block sequentially in reverse order and calculate the junction speeds - (i.e. current->entry_speed) such that: - a. No junction speed exceeds the pre-computed maximum junction speed limit or nominal speeds of - neighboring blocks. - b. A block entry speed cannot exceed one reverse-computed from its exit speed (next->entry_speed) - with a maximum allowable deceleration over the block travel distance. - c. The last (or newest appended) block is planned from a complete stop (an exit speed of zero). - 2. Go over every block in chronological (forward) order and dial down junction speed values if - a. The exit speed exceeds the one forward-computed from its entry speed with the maximum allowable - acceleration over the block travel distance. - - When these stages are complete, the planner will have maximized the velocity profiles throughout the all - of the planner blocks, where every block is operating at its maximum allowable acceleration limits. In - other words, for all of the blocks in the planner, the plan is optimal and no further speed improvements - are possible. If a new block is added to the buffer, the plan is recomputed according to the said - guidelines for a new optimal plan. - - To increase computational efficiency of these guidelines, a set of planner block pointers have been - created to indicate stop-compute points for when the planner guidelines cannot logically make any further - changes or improvements to the plan when in normal operation and new blocks are streamed and added to the - planner buffer. For example, if a subset of sequential blocks in the planner have been planned and are - bracketed by junction velocities at their maximums (or by the first planner block as well), no new block - added to the planner buffer will alter the velocity profiles within them. So we no longer have to compute - them. Or, if a set of sequential blocks from the first block in the planner (or a optimal stop-compute - point) are all accelerating, they are all optimal and can not be altered by a new block added to the - planner buffer, as this will only further increase the plan speed to chronological blocks until a maximum - junction velocity is reached. However, if the operational conditions of the plan changes from infrequently - used feed holds or feedrate overrides, the stop-compute pointers will be reset and the entire plan is - recomputed as stated in the general guidelines. - - Planner buffer index mapping: - - block_buffer_tail: Points to the beginning of the planner buffer. First to be executed or being executed. - - block_buffer_head: Points to the buffer block after the last block in the buffer. Used to indicate whether - the buffer is full or empty. As described for standard ring buffers, this block is always empty. - - next_buffer_head: Points to next planner buffer block after the buffer head block. When equal to the - buffer tail, this indicates the buffer is full. - - block_buffer_planned: Points to the first buffer block after the last optimally planned block for normal - streaming operating conditions. Use for planning optimizations by avoiding recomputing parts of the - planner buffer that don't change with the addition of a new block, as describe above. - - NOTE: Since the planner only computes on what's in the planner buffer, some motions with lots of short - line segments, like G2/3 arcs or complex curves, may seem to move slow. This is because there simply isn't - enough combined distance traveled in the entire buffer to accelerate up to the nominal speed and then - decelerate to a complete stop at the end of the buffer, as stated by the guidelines. If this happens and - becomes an annoyance, there are a few simple solutions: (1) Maximize the machine acceleration. The planner - will be able to compute higher velocity profiles within the same combined distance. (2) Maximize line - segment(s) distance per block to a desired tolerance. The more combined distance the planner has to use, - the faster it can go. (3) Maximize the planner buffer size. This also will increase the combined distance - for the planner to compute over. It also increases the number of computations the planner has to perform - to compute an optimal plan, so select carefully. The Arduino 328p memory is already maxed out, but future - ARM versions should have enough memory and speed for look-ahead blocks numbering up to a hundred or more. - -*/ -static void planner_recalculate() -{ - // Initialize block index to the last block in the planner buffer. - uint8_t block_index = plan_prev_block_index(block_buffer_head); - - // Recompute plan only when there is more than one planner block in the buffer. Can't do anything with one. - if (block_index == block_buffer_tail) { - // Just set block_buffer_planned pointer. - block_buffer_planned = block_buffer_tail; - return; - } - - // Initialize planner buffer pointers and indexing. - plan_block_t *current = &block_buffer[block_index]; - - // Calculate maximum entry speed for last block in buffer, where the exit speed is always zero. - current->entry_speed_sqr = min( current->max_entry_speed_sqr, 2*current->acceleration*current->millimeters); - - // Reverse Pass: Coarsely maximize all possible deceleration curves back-planning from the last - // block in buffer. Cease planning when: (1) the last optimal planned pointer is reached. - // (2) the safe block pointer is reached, whereby the planned pointer is updated. - // NOTE: Forward pass will later refine and correct the reverse pass to create an optimal plan. - // NOTE: If the safe block is encountered before the planned block pointer, we know the safe block - // will be recomputed within the plan. So, we need to update it if it is partially completed. - float entry_speed_sqr; - plan_block_t *next; - block_index = plan_prev_block_index(block_index); - if (block_index == block_buffer_tail) { // !! OR plan pointer? Yes I think so. - // Only two plannable blocks in buffer. Compute previous block based on - // !!! May only work if a new block is being added. Not for an override. The exit speed isn't zero. - // !!! Need to make the current entry speed calculation after this. - st_update_plan_block_parameters(); - block_buffer_planned = block_buffer_tail; - } else { - // Three or more plan-able blocks - while (block_index != block_buffer_planned) { - next = current; - current = &block_buffer[block_index]; - - // Increment block index early to check if the safe block is before the current block. If encountered, - // this is an exit condition as we can't go further than this block in the reverse pass. - block_index = plan_prev_block_index(block_index); - if (block_index == block_buffer_tail) { - // Check if the safe block is partially completed. If so, update it before its exit speed - // (=current->entry speed) is over-written. - // TODO: The update breaks with feedrate overrides, because the replanning process no longer has - // the previous nominal speed to update this block with. There will need to be something along the - // lines of a nominal speed change check and send the correct value to this function. - st_update_plan_block_parameters(); - - // Set planned pointer at safe block and for loop exit after following computation is done. - block_buffer_planned = block_buffer_tail; - } - - // Compute maximum entry speed decelerating over the current block from its exit 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; - } - } - } - } - - // Forward Pass: Forward plan the acceleration curve from the planned pointer onward. - // Also scans for optimal plan breakpoints and appropriately updates the planned pointer. - 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]; - - // Any acceleration detected in the forward pass automatically moves the optimal planned - // pointer forward, since everything before this is all optimal. In other words, nothing - // can improve the plan from the buffer tail to the planned pointer by logic. - // TODO: Need to check if the planned flag logic is correct for all scenarios. It may not - // be for certain conditions. However, if the block reaches nominal speed, it can be a valid - // breakpoint substitute. - if (current->entry_speed_sqr < next->entry_speed_sqr) { - entry_speed_sqr = current->entry_speed_sqr + 2*current->acceleration*current->millimeters; - // If true, current block is full-acceleration and we can move the planned pointer forward. - if (entry_speed_sqr < next->entry_speed_sqr) { - next->entry_speed_sqr = entry_speed_sqr; // Always <= max_entry_speed_sqr. Backward pass sets this. - block_buffer_planned = block_index; // Set optimal plan pointer. - } - } - - // Any block set at its maximum entry speed also creates an optimal plan up to this - // point in the buffer. When the plan is bracketed by either the beginning of the - // buffer and a maximum entry speed or two maximum entry speeds, every block in between - // cannot logically be further improved. Hence, we don't have to recompute them anymore. - if (next->entry_speed_sqr == next->max_entry_speed_sqr) { - block_buffer_planned = block_index; // Set optimal plan pointer - } - block_index = plan_next_block_index( block_index ); - } -} - - -void plan_init() -{ - memset(&pl, 0, sizeof(pl)); // Clear planner struct - block_buffer_tail = 0; - block_buffer_head = 0; // Empty = tail - next_buffer_head = 1; // plan_next_block_index(block_buffer_head) - block_buffer_planned = 0; // = block_buffer_tail; -} - - -void plan_discard_current_block() -{ - if (block_buffer_head != block_buffer_tail) { // Discard non-empty buffer. - block_buffer_tail = plan_next_block_index( block_buffer_tail ); - } -} - - -plan_block_t *plan_get_current_block() -{ - if (block_buffer_head == block_buffer_tail) { return(NULL); } // Buffer empty - return(&block_buffer[block_buffer_tail]); -} - - -float plan_get_exec_block_exit_speed() -{ - uint8_t block_index = plan_next_block_index(block_buffer_tail); - if (block_index == block_buffer_head) { return( 0.0 ); } - return( sqrt( block_buffer[block_index].entry_speed_sqr ) ); -} - - -// Returns the availability status of the block ring buffer. True, if full. -uint8_t plan_check_full_buffer() -{ - if (block_buffer_tail == next_buffer_head) { return(true); } - return(false); -} - - -// Block until all buffered steps are executed or in a cycle state. Works with feed hold -// during a synchronize call, if it should happen. Also, waits for clean cycle end. -void plan_synchronize() -{ - while (plan_get_current_block() || sys.state == STATE_CYCLE) { - protocol_execute_runtime(); // Check and execute run-time commands - if (sys.abort) { return; } // Check for system abort - } -} - - -/* Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position - in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed - rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. - All position data passed to the planner must be in terms of machine position to keep the planner - independent of any coordinate system changes and offsets, which are handled by the g-code parser. - NOTE: Assumes buffer is available. Buffer checks are handled at a higher level by motion_control. - In other words, the buffer head is never equal to the buffer tail. Also the feed rate input value - is used in three ways: as a normal feed rate if invert_feed_rate is false, as inverse time if - invert_feed_rate is true, or as seek/rapids rate if the feed_rate value is negative (and - invert_feed_rate always false). */ -void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate) -{ - // Prepare and initialize new block - plan_block_t *block = &block_buffer[block_buffer_head]; - block->step_event_count = 0; - block->millimeters = 0; - block->direction_bits = 0; - block->acceleration = SOME_LARGE_VALUE; // Scaled down to maximum acceleration later - - // Compute and store initial move distance data. - // TODO: After this for-loop, we don't touch the stepper algorithm data. Might be a good idea - // to try to keep these types of things completely separate from the planner for portability. - int32_t target_steps[N_AXIS]; - float unit_vec[N_AXIS], delta_mm; - uint8_t idx; - for (idx=0; idxsteps[idx] = labs(target_steps[idx]-pl.position[idx]); - block->step_event_count = max(block->step_event_count, block->steps[idx]); - - // Compute individual axes distance for move and prep unit vector calculations. - // NOTE: Computes true distance from converted step values. - delta_mm = (target_steps[idx] - pl.position[idx])/settings.steps_per_mm[idx]; - unit_vec[idx] = delta_mm; // Store unit vector numerator. Denominator computed later. - - // Set direction bits. Bit enabled always means direction is negative. - if (delta_mm < 0 ) { block->direction_bits |= get_direction_mask(idx); } - - // Incrementally compute total move distance by Euclidean norm. First add square of each term. - block->millimeters += delta_mm*delta_mm; - } - block->millimeters = sqrt(block->millimeters); // Complete millimeters calculation with sqrt() - - // Bail if this is a zero-length block. Highly unlikely to occur. - if (block->step_event_count == 0) { return; } - - // Adjust feed_rate value to mm/min depending on type of rate input (normal, inverse time, or rapids) - // TODO: Need to distinguish a rapids vs feed move for overrides. Some flag of some sort. - if (feed_rate < 0) { feed_rate = SOME_LARGE_VALUE; } // Scaled down to absolute max/rapids rate later - else if (invert_feed_rate) { feed_rate = block->millimeters/feed_rate; } - - // Calculate the unit vector of the line move and the block maximum feed rate and acceleration scaled - // down such that no individual axes maximum values are exceeded with respect to the line direction. - // NOTE: This calculation assumes all axes are orthogonal (Cartesian) and works with ABC-axes, - // if they are also orthogonal/independent. Operates on the absolute value of the unit vector. - float inverse_unit_vec_value; - float inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple float divides - float junction_cos_theta = 0; - for (idx=0; idxacceleration = min(block->acceleration,settings.acceleration[idx]*inverse_unit_vec_value); - - // Incrementally compute cosine of angle between previous and current path. Cos(theta) of the junction - // between the current move and the previous move is simply the dot product of the two unit vectors, - // where prev_unit_vec is negative. Used later to compute maximum junction speed. - junction_cos_theta -= pl.previous_unit_vec[idx] * unit_vec[idx]; - } - } - - - // TODO: Need to check this method handling zero junction speeds when starting from rest. - if (block_buffer_head == block_buffer_tail) { - - // Initialize block entry speed as zero. Assume it will be starting from rest. Planner will correct this later. - block->entry_speed_sqr = 0.0; - block->max_junction_speed_sqr = 0.0; // Starting from rest. Enforce start from zero velocity. - - } else { - /* - Compute maximum allowable entry speed at junction by centripetal acceleration approximation. - Let a circle be tangent to both previous and current path line segments, where the junction - deviation is defined as the distance from the junction to the closest edge of the circle, - colinear with the circle center. The circular segment joining the two paths represents the - path of centripetal acceleration. Solve for max velocity based on max acceleration about the - radius of the circle, defined indirectly by junction deviation. This may be also viewed as - path width or max_jerk in the previous grbl version. This approach does not actually deviate - from path, but used as a robust way to compute cornering speeds, as it takes into account the - nonlinearities of both the junction angle and junction velocity. - - NOTE: If the junction deviation value is finite, Grbl executes the motions in an exact path - mode (G61). If the junction deviation value is zero, Grbl will execute the motion in an exact - stop mode (G61.1) manner. In the future, if continuous mode (G64) is desired, the math here - is exactly the same. Instead of motioning all the way to junction point, the machine will - just follow the arc circle defined here. The Arduino doesn't have the CPU cycles to perform - a continuous mode path, but ARM-based microcontrollers most certainly do. - - NOTE: The max junction speed is a fixed value, since machine acceleration limits cannot be - changed dynamically during operation nor can the line move geometry. This must be kept in - memory in the event of a feedrate override changing the nominal speeds of blocks, which can - change the overall maximum entry speed conditions of all blocks. - */ - // NOTE: Computed without any expensive trig, sin() or acos(), by trig half angle identity of cos(theta). - float sin_theta_d2 = sqrt(0.5*(1.0-junction_cos_theta)); // Trig half angle identity. Always positive. - - // TODO: Acceleration used in calculation needs to be limited by the minimum of the two junctions. - block->max_junction_speed_sqr = max( MINIMUM_JUNCTION_SPEED*MINIMUM_JUNCTION_SPEED, - (block->acceleration * settings.junction_deviation * sin_theta_d2)/(1.0-sin_theta_d2) ); - } - - // Store block nominal speed - block->nominal_speed_sqr = feed_rate*feed_rate; // (mm/min). Always > 0 - - // Compute the junction maximum entry based on the minimum of the junction speed and neighboring nominal speeds. - block->max_entry_speed_sqr = min(block->max_junction_speed_sqr, - min(block->nominal_speed_sqr,pl.previous_nominal_speed_sqr)); - - // Update previous path unit_vector and nominal speed (squared) - memcpy(pl.previous_unit_vec, unit_vec, sizeof(unit_vec)); // pl.previous_unit_vec[] = unit_vec[] - pl.previous_nominal_speed_sqr = block->nominal_speed_sqr; - - // Update planner position - memcpy(pl.position, target_steps, sizeof(target_steps)); // pl.position[] = target_steps[] - - // New block is all set. Update buffer head and next buffer head indices. - block_buffer_head = next_buffer_head; - next_buffer_head = plan_next_block_index(block_buffer_head); - - // Finish up by recalculating the plan with the new block. - planner_recalculate(); - -// int32_t blength = block_buffer_head - block_buffer_tail; -// if (blength < 0) { blength += BLOCK_BUFFER_SIZE; } -// printInteger(blength); -} - - -// Reset the planner position vectors. Called by the system abort/initialization routine. -void plan_sync_position() -{ - uint8_t idx; - for (idx=0; idxmillimeters = (block->millimeters*step_events_remaining)/block->step_event_count; - block->step_event_count = step_events_remaining; - - // Re-plan from a complete stop. Reset planner entry speeds and buffer planned pointer. - block->entry_speed_sqr = 0.0; - block->max_entry_speed_sqr = 0.0; - block_buffer_planned = block_buffer_tail; - planner_recalculate(); -} diff --git a/archive/planner_v0_9.c b/archive/planner_v0_9.c deleted file mode 100644 index 1cf6fb4..0000000 --- a/archive/planner_v0_9.c +++ /dev/null @@ -1,476 +0,0 @@ -/* - planner.c - buffers movement commands and manages the acceleration profile plan - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - Copyright (c) 2011 Jens Geisler - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -/* The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis. */ - -#include -#include -#include "planner.h" -#include "nuts_bolts.h" -#include "stepper.h" -#include "settings.h" -#include "config.h" -#include "protocol.h" - -#define SOME_LARGE_VALUE 1.0E+38 // Used by rapids and acceleration maximization calculations. Just needs - // to be larger than any feasible (mm/min)^2 or mm/sec^2 value. - -static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions -static volatile uint8_t block_buffer_tail; // Index of the block to process now -static uint8_t block_buffer_head; // Index of the next block to be pushed -static uint8_t next_buffer_head; // Index of the next buffer head -static uint8_t block_buffer_planned; // Index of the optimally planned block - -// Define planner variables -typedef struct { - int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate - // from g-code position for movements requiring multiple line motions, - // i.e. arcs, canned cycles, and backlash compensation. - float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment - float previous_nominal_speed_sqr; // Nominal speed of previous path line segment - float last_target[N_AXIS]; // Target position of previous path line segment -} planner_t; -static planner_t pl; - - -// Returns the index of the next block in the ring buffer -// NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication. -static uint8_t next_block_index(uint8_t block_index) -{ - block_index++; - if (block_index == BLOCK_BUFFER_SIZE) { block_index = 0; } - return(block_index); -} - - -// Returns the index of the previous block in the ring buffer -static uint8_t prev_block_index(uint8_t block_index) -{ - if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; } - block_index--; - return(block_index); -} - - -/* STEPPER VELOCITY PROFILE DEFINITION - less than nominal rate-> + - +--------+ <- nominal_rate /|\ - / \ / | \ - initial_rate -> + \ / | + <- next->initial_rate - | + <- next->initial_rate / | | - +-------------+ initial_rate -> +----+--+ - time --> ^ ^ ^ ^ - | | | | - decelerate distance decelerate distance - - Calculates trapezoid parameters for stepper algorithm. Each block velocity profiles can be - described as either a trapezoidal or a triangular shape. The trapezoid occurs when the block - reaches the nominal speed of the block and cruises for a period of time. A triangle occurs - when the nominal speed is not reached within the block. Some other special cases exist, - such as pure ac/de-celeration velocity profiles from beginning to end or a trapezoid that - has no deceleration period when the next block resumes acceleration. - - The following function determines the type of velocity profile and stores the minimum required - information for the stepper algorithm to execute the calculated profiles. In this case, only - the new initial rate and n_steps until deceleration are computed, since the stepper algorithm - already handles acceleration and cruising and just needs to know when to start decelerating. -*/ -static void calculate_trapezoid_for_block(block_t *block, float entry_speed_sqr, float exit_speed_sqr) -{ - // Compute new initial rate for stepper algorithm - block->initial_rate = ceil(sqrt(entry_speed_sqr)*(INV_TIME_MULTIPLIER/(60*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - // TODO: Compute new nominal rate if a feedrate override occurs. - // block->nominal_rate = ceil(feed_rate*(RANADE_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - // Compute efficiency variable for following calculations. Removes a float divide and multiply. - // TODO: If memory allows, this can be kept in the block buffer since it doesn't change, even after feed holds. - float steps_per_mm_div_2_acc = block->step_event_count/(2*block->acceleration*block->millimeters); - - // First determine intersection distance (in steps) from the exit point for a triangular profile. - // Computes: steps_intersect = steps/mm * ( distance/2 + (v_entry^2-v_exit^2)/(4*acceleration) ) - int32_t intersect_distance = ceil( 0.5*(block->step_event_count + steps_per_mm_div_2_acc*(entry_speed_sqr-exit_speed_sqr)) ); - - // Check if this is a pure acceleration block by a intersection distance less than zero. Also - // prevents signed and unsigned integer conversion errors. - if (intersect_distance <= 0) { - block->decelerate_after = 0; - } else { - // Determine deceleration distance (in steps) from nominal speed to exit speed for a trapezoidal profile. - // Value is never negative. Nominal speed is always greater than or equal to the exit speed. - // Computes: steps_decelerate = steps/mm * ( (v_nominal^2 - v_exit^2)/(2*acceleration) ) - block->decelerate_after = ceil(steps_per_mm_div_2_acc * (block->nominal_speed_sqr - exit_speed_sqr)); - - // The lesser of the two triangle and trapezoid distances always defines the velocity profile. - if (block->decelerate_after > intersect_distance) { block->decelerate_after = intersect_distance; } - - // Finally, check if this is a pure deceleration block. - if (block->decelerate_after > block->step_event_count) { block->decelerate_after = block->step_event_count; } - } -} - - -/* PLANNER SPEED DEFINITION - +--------+ <- current->nominal_speed - / \ - current->entry_speed -> + \ - | + <- next->entry_speed - +-------------+ - time --> - - Recalculates the motion plan according to the following algorithm: - - 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_speed) - so that: - a. The junction speed is equal to or less than the maximum junction speed limit - b. No speed reduction within one block requires faster deceleration than the acceleration limits. - c. The last (or newest appended) block is planned from a complete stop. - 2. Go over every block in chronological (forward) order and dial down junction speed values if - a. The speed increase within one block would require faster acceleration than the acceleration limits. - - When these stages are complete, all blocks have a junction entry speed that will allow all speed changes - to be performed using the overall limiting acceleration value, and where no junction speed is greater - than the max limit. In other words, it just computed the fastest possible velocity profile through all - buffered blocks, where the final buffered block is planned to come to a full stop when the buffer is fully - executed. Finally it will: - - 3. Convert the plan to data that the stepper algorithm needs. Only block trapezoids adjacent to a - a planner-modified junction speed with be updated, the others are assumed ok as is. - - All planner computations(1)(2) are performed in floating point to minimize numerical round-off errors. Only - when planned values are converted to stepper rate parameters(3), these are integers. If another motion block - is added while executing, the planner will re-plan and update the stored optimal velocity profile as it goes. - - Conceptually, the planner works like blowing up a balloon, where the balloon is the velocity profile. It's - constrained by the speeds at the beginning and end of the buffer, along with the maximum junction speeds and - nominal speeds of each block. Once a plan is computed, or balloon filled, this is the optimal velocity profile - through all of the motions in the buffer. Whenever a new block is added, this changes some of the limiting - conditions, or how the balloon is filled, so it has to be re-calculated to get the new optimal velocity profile. - - Also, since the planner only computes on what's in the planner buffer, some motions with lots of short line - segments, like arcs, may seem to move slow. This is because there simply isn't enough combined distance traveled - in the entire buffer to accelerate up to the nominal speed and then decelerate to a stop at the end of the - buffer. There are a few simple solutions to this: (1) Maximize the machine acceleration. The planner will be - able to compute higher speed profiles within the same combined distance. (2) Increase line segment(s) distance. - The more combined distance the planner has to use, the faster it can go. (3) Increase the MINIMUM_PLANNER_SPEED. - Not recommended. This will change what speed the planner plans to at the end of the buffer. Can lead to lost - steps when coming to a stop. (4) [BEST] Increase the planner buffer size. The more combined distance, the - bigger the balloon, or faster it can go. But this is not possible for 328p Arduinos because its limited memory - is already maxed out. Future ARM versions should not have this issue, with look-ahead planner blocks numbering - up to a hundred or more. - - NOTE: Since this function is constantly re-calculating for every new incoming block, it must be as efficient - as possible. For example, in situations like arc generation or complex curves, the short, rapid line segments - can execute faster than new blocks can be added, and the planner buffer will then starve and empty, leading - to weird hiccup-like jerky motions. -*/ -static void planner_recalculate() -{ - // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated. - uint8_t block_index = block_buffer_head; - block_t *current = &block_buffer[block_index]; // Set as last/newest block in buffer - - // Determine safe point for which to plan to. - uint8_t block_buffer_safe = next_block_index( block_buffer_tail ); - - if (block_buffer_safe == next_buffer_head) { // Only one safe block in buffer to operate on - - block_buffer_planned = block_buffer_safe; - calculate_trapezoid_for_block(current, 0.0, MINIMUM_PLANNER_SPEED*MINIMUM_PLANNER_SPEED); - - } else { - - // TODO: need to account for the two block condition better. If the currently executing block - // is not safe, do we wait until its done? Can we treat the buffer head differently? - - // Calculate trapezoid for the last/newest block. - current->entry_speed_sqr = min( current->max_entry_speed_sqr, - MINIMUM_PLANNER_SPEED*MINIMUM_PLANNER_SPEED + 2*current->acceleration*current->millimeters); - calculate_trapezoid_for_block(current, current->entry_speed_sqr, MINIMUM_PLANNER_SPEED*MINIMUM_PLANNER_SPEED); - - - // Reverse Pass: Back plan the deceleration curve from the last block in buffer. Cease - // planning when: (1) the last optimal planned pointer is reached. (2) the safe block - // pointer is reached, whereby the planned pointer is updated. - float entry_speed_sqr; - block_t *next; - block_index = prev_block_index(block_index); - while (block_index != block_buffer_planned) { - next = current; - current = &block_buffer[block_index]; - - // Exit loop and update planned pointer when the tail/safe block is reached. - if (block_index == block_buffer_safe) { - block_buffer_planned = block_buffer_safe; - break; - } - - // Crudely maximize deceleration curve from the end of the non-optimally planned buffer to - // the optimal plan pointer. Forward pass will adjust and finish optimizing the plan. - 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; - } - } - block_index = prev_block_index(block_index); - } - - // Forward Pass: Forward plan the acceleration curve from the planned pointer onward. - // Also scans for optimal plan breakpoints and appropriately updates the planned pointer. - block_index = block_buffer_planned; // Begin at buffer planned pointer - next = &block_buffer[prev_block_index(block_buffer_planned)]; // Set up for while loop - while (block_index != next_buffer_head) { - current = next; - next = &block_buffer[block_index]; - - // Any acceleration detected in the forward pass automatically moves the optimal planned - // pointer forward, since everything before this is all optimal. In other words, nothing - // can improve the plan from the buffer tail to the planned pointer by logic. - if (current->entry_speed_sqr < next->entry_speed_sqr) { - block_buffer_planned = block_index; - 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; // Always <= max_entry_speed_sqr. Backward pass set this. - } - } - - // Any block set at its maximum entry speed also creates an optimal plan up to this - // point in the buffer. The optimally planned pointer is updated. - if (next->entry_speed_sqr == next->max_entry_speed_sqr) { - block_buffer_planned = block_index; - } - - // Automatically recalculate trapezoid for all buffer blocks from last plan's optimal planned - // pointer to the end of the buffer, except the last block. - calculate_trapezoid_for_block(current, current->entry_speed_sqr, next->entry_speed_sqr); - block_index = next_block_index( block_index ); - } - - } - -} - - -void plan_init() -{ - block_buffer_tail = block_buffer_head; - next_buffer_head = next_block_index(block_buffer_head); - block_buffer_planned = block_buffer_head; - memset(&pl, 0, sizeof(pl)); // Clear planner struct -} - - -inline void plan_discard_current_block() -{ - if (block_buffer_head != block_buffer_tail) { - block_buffer_tail = next_block_index( block_buffer_tail ); - } -} - - -inline block_t *plan_get_current_block() -{ - if (block_buffer_head == block_buffer_tail) { return(NULL); } - return(&block_buffer[block_buffer_tail]); -} - - -// Returns the availability status of the block ring buffer. True, if full. -uint8_t plan_check_full_buffer() -{ - if (block_buffer_tail == next_buffer_head) { return(true); } - return(false); -} - - -// Block until all buffered steps are executed or in a cycle state. Works with feed hold -// during a synchronize call, if it should happen. Also, waits for clean cycle end. -void plan_synchronize() -{ - while (plan_get_current_block() || sys.state == STATE_CYCLE) { - protocol_execute_runtime(); // Check and execute run-time commands - if (sys.abort) { return; } // Check for system abort - } -} - - -// Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position -// in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed -// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. -// All position data passed to the planner must be in terms of machine position to keep the planner -// independent of any coordinate system changes and offsets, which are handled by the g-code parser. -// NOTE: Assumes buffer is available. Buffer checks are handled at a higher level by motion_control. -// In other words, the buffer head is never equal to the buffer tail. Also the feed rate input value -// is used in three ways: as a normal feed rate if invert_feed_rate is false, as inverse time if -// invert_feed_rate is true, or as seek/rapids rate if the feed_rate value is negative (and -// invert_feed_rate always false). -void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate) -{ - // Prepare and initialize new block - block_t *block = &block_buffer[block_buffer_head]; - block->step_event_count = 0; - block->millimeters = 0; - block->direction_bits = 0; - block->acceleration = SOME_LARGE_VALUE; // Scaled down to maximum acceleration later - - // Compute and store initial move distance data. - int32_t target_steps[N_AXIS]; - float unit_vec[N_AXIS], delta_mm; - uint8_t idx; - for (idx=0; idxsteps[idx] = labs(target_steps[idx]-pl.position[idx]); - block->step_event_count = max(block->step_event_count, block->steps[idx]); - - // Compute individual axes distance for move and prep unit vector calculations. - delta_mm = target[idx] - pl.last_target[idx]; - unit_vec[idx] = delta_mm; // Store unit vector numerator. Denominator computed later. - - // Incrementally compute total move distance by Euclidean norm - block->millimeters += delta_mm*delta_mm; - - // Set direction bits. Bit enabled always means direction is negative. - if (delta_mm < 0 ) { block->direction_bits |= get_direction_mask(idx); } - } - block->millimeters = sqrt(block->millimeters); // Complete millimeters calculation - - // Bail if this is a zero-length block - if (block->step_event_count == 0) { return; } - - // Adjust feed_rate value to mm/min depending on type of rate input (normal, inverse time, or rapids) - // TODO: Need to distinguish a rapids vs feed move for overrides. Some flag of some sort. - if (feed_rate < 0) { feed_rate = SOME_LARGE_VALUE; } // Scaled down to absolute max/rapids rate later - else if (invert_feed_rate) { feed_rate = block->millimeters/feed_rate; } - - // Calculate the unit vector of the line move and the block maximum feed rate and acceleration limited - // by the maximum possible values. Block rapids rates are computed or feed rates are scaled down so - // they don't exceed the maximum axes velocities. The block acceleration is maximized based on direction - // and axes properties as well. - // NOTE: This calculation assumes all axes are orthogonal (Cartesian) and works with ABC-axes, - // if they are also orthogonal/independent. Operates on the absolute value of the unit vector. - float inverse_unit_vec_value; - float inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple float divides - float junction_cos_theta = 0; - for (idx=0; idxacceleration = min(block->acceleration,settings.acceleration[idx]*inverse_unit_vec_value); - - // Incrementally compute cosine of angle between previous and current path. Cos(theta) of the junction - // between the current move and the previous move is simply the dot product of the two unit vectors, - // where prev_unit_vec is negative. Used later to compute maximum junction speed. - junction_cos_theta -= pl.previous_unit_vec[idx] * unit_vec[idx]; - } - } - - /* Compute maximum allowable entry speed at junction by centripetal acceleration approximation. - Let a circle be tangent to both previous and current path line segments, where the junction - deviation is defined as the distance from the junction to the closest edge of the circle, - colinear with the circle center. The circular segment joining the two paths represents the - path of centripetal acceleration. Solve for max velocity based on max acceleration about the - radius of the circle, defined indirectly by junction deviation. This may be also viewed as - path width or max_jerk in the previous grbl version. This approach does not actually deviate - from path, but used as a robust way to compute cornering speeds, as it takes into account the - nonlinearities of both the junction angle and junction velocity. - NOTE: If the junction deviation value is finite, Grbl executes the motions in an exact path - mode (G61). If the junction deviation value is zero, Grbl will execute the motion in an exact - stop mode (G61.1) manner. In the future, if continuous mode (G64) is desired, the math here - is exactly the same. Instead of motioning all the way to junction point, the machine will - just follow the arc circle defined here. The Arduino doesn't have the CPU cycles to perform - a continuous mode path, but ARM-based microcontrollers most certainly do. - */ - // TODO: Acceleration need to be limited by the minimum of the two junctions. - // TODO: Need to setup a method to handle zero junction speeds when starting from rest. - if (block_buffer_head == block_buffer_tail) { - block->max_entry_speed_sqr = MINIMUM_PLANNER_SPEED*MINIMUM_PLANNER_SPEED; - } else { - // NOTE: Computed without any expensive trig, sin() or acos(), by trig half angle identity of cos(theta). - float sin_theta_d2 = sqrt(0.5*(1.0-junction_cos_theta)); // Trig half angle identity. Always positive. - block->max_entry_speed_sqr = (block->acceleration * settings.junction_deviation * sin_theta_d2)/(1.0-sin_theta_d2); - } - - // Store block nominal speed and rate - block->nominal_speed_sqr = feed_rate*feed_rate; // (mm/min)^2. Always > 0 - block->nominal_rate = ceil(feed_rate*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - // Compute and store acceleration and distance traveled per step event. - block->rate_delta = ceil(block->acceleration* - ((INV_TIME_MULTIPLIER/(60.0*60.0))/(ISR_TICKS_PER_SECOND*ACCELERATION_TICKS_PER_SECOND))); // (mult*mm/isr_tic/accel_tic) - block->d_next = ceil((block->millimeters*INV_TIME_MULTIPLIER)/block->step_event_count); // (mult*mm/step) - - // Update previous path unit_vector and nominal speed (squared) - memcpy(pl.previous_unit_vec, unit_vec, sizeof(unit_vec)); // pl.previous_unit_vec[] = unit_vec[] - pl.previous_nominal_speed_sqr = block->nominal_speed_sqr; - - // Update planner position - memcpy(pl.position, target_steps, sizeof(target_steps)); // pl.position[] = target_steps[] - memcpy(pl.last_target, target, sizeof(target)); // pl.last_target[] = target[] - - planner_recalculate(); - - // Update buffer head and next buffer head indices. - // NOTE: The buffer head update is atomic since it's one byte. Performed after the new plan - // calculations to help prevent overwriting scenarios with adding a new block to a low buffer. - block_buffer_head = next_buffer_head; - next_buffer_head = next_block_index(block_buffer_head); -} - - -// Reset the planner position vectors. Called by the system abort/initialization routine. -void plan_sync_position() -{ - uint8_t idx; - for (idx=0; idxmillimeters = (block->millimeters*step_events_remaining)/block->step_event_count; - block->step_event_count = step_events_remaining; - - // Re-plan from a complete stop. Reset planner entry speeds and buffer planned pointer. - block->entry_speed_sqr = 0.0; - block->max_entry_speed_sqr = MINIMUM_PLANNER_SPEED*MINIMUM_PLANNER_SPEED; - block_buffer_planned = block_buffer_tail; - planner_recalculate(); -} diff --git a/archive/planner_v0_9.h b/archive/planner_v0_9.h deleted file mode 100644 index 84ecc4b..0000000 --- a/archive/planner_v0_9.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - planner.h - buffers movement commands and manages the acceleration profile plan - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#ifndef planner_h -#define planner_h -#include "nuts_bolts.h" - -// The number of linear motions that can be in the plan at any give time -#ifndef BLOCK_BUFFER_SIZE - #define BLOCK_BUFFER_SIZE 17 -#endif - -// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in -// the source g-code and may never actually be reached if acceleration management is active. -typedef struct { - - // Fields used by the bresenham algorithm for tracing the line - uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) - uint32_t steps[N_AXIS]; // Step count along each axis - int32_t step_event_count; // The number of step events required to complete this block - - // Fields used by the motion planner to manage acceleration - float nominal_speed_sqr; // Axis-limit adjusted nominal speed for this block in (mm/min)^2 - float entry_speed_sqr; // Entry speed at previous-current block junction in (mm/min)^2 - float max_entry_speed_sqr; // Maximum allowable junction entry speed in (mm/min)^2 - float millimeters; // The total travel of this block in mm - float acceleration; // Axes-limit adjusted line acceleration in mm/min^2 - - // Settings for the trapezoid generator - uint32_t initial_rate; // The step rate at start of block - int32_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive) - uint32_t decelerate_after; // The index of the step event on which to start decelerating - uint32_t nominal_rate; // The nominal step rate for this block in step_events/minute - uint32_t d_next; // Scaled distance to next step - -} block_t; - -// Initialize the motion plan subsystem -void plan_init(); - -// Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position -// in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed -// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. -void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate); - -// Called when the current block is no longer needed. Discards the block and makes the memory -// availible for new blocks. -void plan_discard_current_block(); - -// Gets the current block. Returns NULL if buffer empty -block_t *plan_get_current_block(); - -// Reset the planner position vector (in steps) -void plan_sync_position(); - -// Reinitialize plan with a partially completed block -void plan_cycle_reinitialize(int32_t step_events_remaining); - -// Returns the status of the block ring buffer. True, if buffer is full. -uint8_t plan_check_full_buffer(); - -// Block until all buffered steps are executed -void plan_synchronize(); - -#endif diff --git a/archive/stepper_dist.c b/archive/stepper_dist.c deleted file mode 100644 index fe74e73..0000000 --- a/archive/stepper_dist.c +++ /dev/null @@ -1,706 +0,0 @@ -/* - stepper.c - stepper motor driver: executes motion plans using stepper motors - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" -#include "nuts_bolts.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) - -#define RAMP_NOOP_CRUISE 0 -#define RAMP_ACCEL 1 -#define RAMP_DECEL 2 - -#define LOAD_NOOP 0 -#define LOAD_SEGMENT 1 -#define LOAD_BLOCK 2 - -#define SEGMENT_NOOP 0 -#define SEGMENT_END_OF_BLOCK bit(0) -#define RAMP_CHANGE_ACCEL bit(1) -#define RAMP_CHANGE_DECEL bit(2) - -#define MINIMUM_STEPS_PER_SEGMENT 1 // Don't change - -#define SEGMENT_BUFFER_SIZE 6 - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - uint8_t segment_steps_remaining; // Steps remaining in line segment motion - - // Used by inverse time algorithm to track step rate - int32_t counter_dist; // Inverse time distance traveled since last step event - uint32_t ramp_rate; // Inverse time distance traveled per interrupt tick - uint32_t dist_per_tick; - - // Used by the stepper driver interrupt - uint8_t execute_step; // Flags step execution for each interrupt. - uint8_t step_pulse_time; // Step pulse reset time after step rise - uint8_t out_bits; // The next stepping-bits to be output - uint8_t load_flag; - - uint8_t counter_ramp; - uint8_t ramp_type; -} stepper_t; -static stepper_t st; - -// Stores stepper common data for executing steps in the segment buffer. Data can change mid-block when the -// planner updates the remaining block velocity profile with a more optimal plan or a feedrate override occurs. -// NOTE: Normally, this buffer is partially in-use, but, for the worst case scenario, it will never exceed -// the number of accessible stepper buffer segments (SEGMENT_BUFFER_SIZE-1). -typedef struct { - int32_t step_events_remaining; // Tracks step event count for the executing planner block - uint32_t dist_per_step; // Scaled distance to next step - uint32_t initial_rate; // Initialized step rate at re/start of a planner block - uint32_t nominal_rate; // The nominal step rate for this block in step_events/minute - uint32_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive) - uint32_t current_approx_rate; // Tracks the approximate segment rate to predict steps per segment to execute - int32_t decelerate_after; // Tracks when to initiate deceleration according to the planner block - float mm_per_step; -} st_data_t; -static st_data_t segment_data[SEGMENT_BUFFER_SIZE-1]; - -// Primary stepper segment ring buffer. Contains small, short line segments for the stepper algorithm to execute, -// which are "checked-out" incrementally from the first block in the planner buffer. Once "checked-out", the steps -// in the segments buffer cannot be modified by the planner, where the remaining planner block steps still can. -typedef struct { - uint8_t n_step; // Number of step events to be executed for this segment - uint8_t st_data_index; // Stepper buffer common data index. Uses this information to execute this segment. - uint8_t flag; // Stepper algorithm bit-flag for special execution conditions. -} st_segment_t; -static st_segment_t segment_buffer[SEGMENT_BUFFER_SIZE]; - -// Step segment ring buffer indices -static volatile uint8_t segment_buffer_tail; -static volatile uint8_t segment_buffer_head; -static uint8_t segment_next_head; - -static volatile uint8_t busy; // Used to avoid ISR nesting of the "Stepper Driver Interrupt". Should never occur though. -static plan_block_t *pl_current_block; // A pointer to the planner block currently being traced -static st_segment_t *st_current_segment; -static st_data_t *st_current_data; - -// Pointers for the step segment being prepped from the planner buffer. Accessed only by the -// main program. Pointers may be planning segments or planner blocks ahead of what being executed. -static plan_block_t *pl_prep_block; // Pointer to the planner block being prepped -static st_data_t *st_prep_data; // Pointer to the stepper common data being prepped -static uint8_t pl_prep_index; // Index of planner block being prepped -static uint8_t st_data_prep_index; // Index of stepper common data block being prepped -static uint8_t pl_partial_block_flag; // Flag indicating the planner has modified the prepped planner block - - -/* __________________________ - /| |\ _________________ ^ - / | | \ /| |\ | - / | | \ / | | \ s - / | | | | | \ p - / | | | | | \ e - +-----+------------------------+---+--+---------------+----+ e - | BLOCK 1 | BLOCK 2 | d - - time -----> - - The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta - until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after - after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as - +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. -*/ - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - // Enable stepper driver interrupt - st.execute_step = false; - st.load_flag = LOAD_BLOCK; - - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<n_step; - - // If the new segment starts a new planner block, initialize stepper variables and counters. - // NOTE: For new segments only, the step counters are not updated to ensure step phasing is continuous. - if (st.load_flag == LOAD_BLOCK) { - pl_current_block = plan_get_current_block(); // Should always be there. Stepper buffer handles this. - st_current_data = &segment_data[segment_buffer[segment_buffer_tail].st_data_index]; - - // Initialize direction bits for block. Set execute flag to set directions bits upon next ISR tick. - st.out_bits = pl_current_block->direction_bits ^ settings.invert_mask; - st.execute_step = true; - - // Initialize Bresenham line counters - st.counter_x = (pl_current_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - - // Initialize inverse time, step rate data, and acceleration ramp counters - st.counter_dist = st_current_data->dist_per_step; // dist_per_step always greater than ramp_rate. - st.ramp_rate = st_current_data->initial_rate; - st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; // Initialize ramp counter via midpoint rule - st.ramp_type = RAMP_NOOP_CRUISE; // Initialize as no ramp operation. Corrected later if necessary. - - // Ensure the initial step rate exceeds the MINIMUM_STEP_RATE. - if (st.ramp_rate < MINIMUM_STEP_RATE) { st.dist_per_tick = MINIMUM_STEP_RATE; } - else { st.dist_per_tick = st.ramp_rate; } - } - - // Check if ramp conditions have changed. If so, update ramp counters and control variables. - if ( st_current_segment->flag & (RAMP_CHANGE_DECEL | RAMP_CHANGE_ACCEL) ) { - /* Compute correct ramp count for a ramp change. Upon a switch from acceleration to deceleration, - or vice-versa, the new ramp count must be set to trigger the next acceleration tick equal to - the number of ramp ISR ticks counted since the last acceleration tick. This is ensures the - ramp is executed exactly as the plan dictates. Otherwise, when a ramp begins from a known - rate (nominal/cruise or initial), the ramp count must be set to ISR_TICKS_PER_ACCELERATION_TICK/2 - as mandated by the mid-point rule. For the latter conditions, the ramp count have been - initialized such that the following computation is still correct. */ - st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK-st.counter_ramp; - if ( st_current_segment->flag & RAMP_CHANGE_DECEL ) { st.ramp_type = RAMP_DECEL; } - else { st.ramp_type = RAMP_ACCEL; } - } - - st.load_flag = LOAD_NOOP; // Segment motion loaded. Set no-operation flag to skip during execution. - - } else { - // Can't discard planner block here if a feed hold stops in middle of block. - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - - } - - // Adjust inverse time counter for ac/de-celerations - if (st.ramp_type) { // Ignored when ramp type is RAMP_NOOP_CRUISE - st.counter_ramp--; // Tick acceleration ramp counter - if (st.counter_ramp == 0) { // Adjust step rate when its time - st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK; // Reload ramp counter - if (st.ramp_type == RAMP_ACCEL) { // Adjust velocity for acceleration - st.ramp_rate += st_current_data->rate_delta; - if (st.ramp_rate >= st_current_data->nominal_rate) { // Reached nominal rate. - st.ramp_rate = st_current_data->nominal_rate; // Set cruising velocity - st.ramp_type = RAMP_NOOP_CRUISE; // Set ramp flag to cruising - st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; // Re-initialize counter for next ramp change. - } - } else { // Adjust velocity for deceleration. - if (st.ramp_rate > st_current_data->rate_delta) { - st.ramp_rate -= st_current_data->rate_delta; - } else { // Moving near zero feed rate. Gracefully slow down. - st.ramp_rate >>= 1; // Integer divide by 2 until complete. Also prevents overflow. - } - } - // Adjust for minimum step rate, but retain operating ramp rate for accurate velocity tracing. - if (st.ramp_rate < MINIMUM_STEP_RATE) { st.dist_per_tick = MINIMUM_STEP_RATE; } - else { st.dist_per_tick = st.ramp_rate; } - } - } - - // Iterate inverse time counter. Triggers each Bresenham step event. - st.counter_dist -= st.dist_per_tick; - - // Execute Bresenham step event, when it's time to do so. - if (st.counter_dist < 0) { - st.counter_dist += st_current_data->dist_per_step; // Reload inverse time counter - - st.out_bits = pl_current_block->direction_bits; // Reset out_bits and reload direction bits - st.execute_step = true; - - // Execute step displacement profile by Bresenham line algorithm - st.counter_x -= pl_current_block->steps[X_AXIS]; - if (st.counter_x < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<flag & SEGMENT_END_OF_BLOCK) { - plan_discard_current_block(); - st.load_flag = LOAD_BLOCK; - } else { - st.load_flag = LOAD_SEGMENT; - } - - // Discard current segment by advancing buffer tail index - if ( ++segment_buffer_tail == SEGMENT_BUFFER_SIZE) { segment_buffer_tail = 0; } - } - - st.out_bits ^= settings.invert_mask; // Apply step port invert mask - } - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<step_events_remaining); -// st.ramp_type = RAMP_ACCEL; -// st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; -// st.ramp_rate = 0; -// sys.state = STATE_QUEUED; -// } else { -// sys.state = STATE_IDLE; -// } - sys.state = STATE_IDLE; - -} - - -/* Prepares step segment buffer. Continuously called from main program. - - The segment buffer is an intermediary buffer interface between the execution of steps - by the stepper algorithm and the velocity profiles generated by the planner. The stepper - algorithm only executes steps within the segment buffer and is filled by the main program - when steps are "checked-out" from the first block in the planner buffer. This keeps the - step execution and planning optimization processes atomic and protected from each other. - The number of steps "checked-out" from the planner buffer and the number of segments in - the segment buffer is sized and computed such that no operation in the main program takes - longer than the time it takes the stepper algorithm to empty it before refilling it. - Currently, the segment buffer conservatively holds roughly up to 40-60 msec of steps. - - NOTE: The segment buffer executes a set number of steps over an approximate time period. - If we try to execute over a fixed time period, it is difficult to guarantee or predict - how many steps will execute over it, especially when the step pulse phasing between the - neighboring segments must also be kept consistent. Meaning that, if the last segment step - pulses right before a segment end, the next segment must delay its first pulse so that the - step pulses are consistently spaced apart over time to keep the step pulse train nice and - smooth. Keeping track of phasing and ensuring that the exact number of steps are executed - as defined by the planner block, the related computational overhead can get quickly and - prohibitively expensive, especially in real-time. - Since the stepper algorithm automatically takes care of the step pulse phasing with - its ramp and inverse time counters by retaining the count remainders, we don't have to - explicitly and expensively track and synchronize the exact number of steps, time, and - phasing of steps. All we need to do is approximate the number of steps in each segment - such that the segment buffer has enough execution time for the main program to do what - it needs to do and refill it when it comes back. In other words, we just need to compute - a cheap approximation of the current velocity and the number of steps over it. -*/ - -/* - TODO: Figure out how to enforce a deceleration when a feedrate override is reduced. - The problem is that when an override is reduced, the planner may not plan back to - the current rate. Meaning that the velocity profiles for certain conditions no longer - are trapezoidal or triangular. For example, if the current block is cruising at a - nominal rate and the feedrate override is reduced, the new nominal rate will now be - lower. The velocity profile must first decelerate to the new nominal rate and then - follow on the new plan. So the remaining velocity profile will have a decelerate, - cruise, and another decelerate. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already - heavily decelerating throughout it, reducing the feedrate will not do much to it. So, - how do we determine when to resume the new plan? How many blocks do we have to wait - until the new plan intersects with the deceleration curve? One plus though, the - deceleration will never be more than the number of blocks in the entire planner buffer, - but it theoretically can be equal to it when all planner blocks are decelerating already. -*/ -void st_prep_buffer() -{ - if (sys.state == STATE_QUEUED) { return; } // Block until a motion state is issued - while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. - - // Initialize new segment - st_segment_t *prep_segment = &segment_buffer[segment_buffer_head]; - prep_segment->flag = SEGMENT_NOOP; - - // Determine if we need to load a new planner block. - if (pl_prep_block == NULL) { - pl_prep_block = plan_get_block_by_index(pl_prep_index); // Query planner for a queued block - if (pl_prep_block == NULL) { return; } // No planner blocks. Exit. - - -SPINDLE_ENABLE_PORT ^= 1<step_events_remaining = last_st_prep_data->step_events_remaining; - st_prep_data->rate_delta = last_st_prep_data->rate_delta; - st_prep_data->dist_per_step = last_st_prep_data->dist_per_step; - st_prep_data->nominal_rate = last_st_prep_data->nominal_rate; // TODO: Feedrate overrides recomputes this. - - st_prep_data->mm_per_step = last_st_prep_data->mm_per_step; - - pl_partial_block_flag = false; // Reset flag - - } else { - - // Prepare commonly shared planner block data for the ensuing segment buffer moves ad-hoc, since - // the planner buffer can dynamically change the velocity profile data as blocks are added. - st_prep_data = &segment_data[st_data_prep_index]; - - // Initialize Bresenham variables - st_prep_data->step_events_remaining = pl_prep_block->step_event_count; - - // Convert planner block velocity profile data to stepper rate and step distance data. - st_prep_data->nominal_rate = ceil(sqrt(pl_prep_block->nominal_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - st_prep_data->rate_delta = ceil(pl_prep_block->acceleration* - ((INV_TIME_MULTIPLIER/(60.0*60.0))/(ISR_TICKS_PER_SECOND*ACCELERATION_TICKS_PER_SECOND))); // (mult*mm/isr_tic/accel_tic) - st_prep_data->dist_per_step = ceil((pl_prep_block->millimeters*INV_TIME_MULTIPLIER)/pl_prep_block->step_event_count); // (mult*mm/step) - - // TODO: Check if we really need to store this. - st_prep_data->mm_per_step = pl_prep_block->millimeters/pl_prep_block->step_event_count; - - } - - // Convert planner entry speed to stepper initial rate. - st_prep_data->initial_rate = ceil(sqrt(pl_prep_block->entry_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - // TODO: Nominal rate changes with feedrate override. - // st_prep_data->nominal_rate = ceil(sqrt(pl_prep_block->nominal_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - st_prep_data->current_approx_rate = st_prep_data->initial_rate; - - // Calculate the planner block velocity profile type, determine deceleration point, and initial ramp. - float mm_decelerate_after = plan_calculate_velocity_profile(pl_prep_index); - st_prep_data->decelerate_after = ceil( mm_decelerate_after/st_prep_data->mm_per_step ); - if (st_prep_data->decelerate_after > 0) { // If 0, RAMP_CHANGE_DECEL flag is set later. - if (st_prep_data->initial_rate != st_prep_data->nominal_rate) { prep_segment->flag = RAMP_CHANGE_ACCEL; } - } - } - - // Set new segment to point to the current segment data block. - prep_segment->st_data_index = st_data_prep_index; - - // Approximate the velocity over the new segment using the already computed rate values. - // NOTE: This assumes that each segment will have an execution time roughly equal to every ACCELERATION_TICK. - // We do this to minimize memory and computational requirements. However, this could easily be replaced with - // a more exact approximation or have an user-defined time per segment, if CPU and memory overhead allows. - if (st_prep_data->decelerate_after <= 0) { - if (st_prep_data->decelerate_after == 0) { prep_segment->flag = RAMP_CHANGE_DECEL; } // Set segment deceleration flag - else { st_prep_data->current_approx_rate -= st_prep_data->rate_delta; } - if (st_prep_data->current_approx_rate < st_prep_data->rate_delta) { st_prep_data->current_approx_rate >>= 1; } - } else { - if (st_prep_data->current_approx_rate < st_prep_data->nominal_rate) { - st_prep_data->current_approx_rate += st_prep_data->rate_delta; - if (st_prep_data->current_approx_rate > st_prep_data->nominal_rate) { - st_prep_data->current_approx_rate = st_prep_data->nominal_rate; - } - } - } - - // TODO: Look into replacing the following dist_per_step divide with multiplying its inverse to save cycles. - - // Compute the number of steps in the prepped segment based on the approximate current rate. - // NOTE: The dist_per_step divide cancels out the INV_TIME_MULTIPLIER and converts the rate value to steps. - prep_segment->n_step = ceil(max(MINIMUM_STEP_RATE,st_prep_data->current_approx_rate)* - (ISR_TICKS_PER_SECOND/ACCELERATION_TICKS_PER_SECOND)/st_prep_data->dist_per_step); - // NOTE: Ensures it moves for very slow motions, but MINIMUM_STEP_RATE should always set this too. Perhaps - // a compile-time check to see if MINIMUM_STEP_RATE is set high enough is all that is needed. - prep_segment->n_step = max(prep_segment->n_step,MINIMUM_STEPS_PER_SEGMENT); - // NOTE: As long as the ACCELERATION_TICKS_PER_SECOND is valid, n_step should never exceed 255 and overflow. - // prep_segment->n_step = min(prep_segment->n_step,MAXIMUM_STEPS_PER_BLOCK); // Prevent unsigned int8 overflow. - - // Check if n_step exceeds steps remaining in planner block. If so, truncate. - if (prep_segment->n_step > st_prep_data->step_events_remaining) { - prep_segment->n_step = st_prep_data->step_events_remaining; - } - - // Check if n_step crosses decelerate point in block. If so, truncate to ensure the deceleration - // ramp counters are set correctly during execution. - if (st_prep_data->decelerate_after > 0) { - if (prep_segment->n_step > st_prep_data->decelerate_after) { - prep_segment->n_step = st_prep_data->decelerate_after; - } - } - - // Update stepper common data variables. - st_prep_data->decelerate_after -= prep_segment->n_step; - st_prep_data->step_events_remaining -= prep_segment->n_step; - - // Check for end of planner block - if ( st_prep_data->step_events_remaining == 0 ) { - - // TODO: When a feed hold ends, the step_events_remaining will also be zero, even though a block - // have partially been completed. We need to flag the stepper algorithm to indicate a stepper shutdown - // when complete, but not remove the planner block unless it truly is the end of the block (rare). - - // Set EOB bitflag so stepper algorithm discards the planner block after this segment completes. - prep_segment->flag |= SEGMENT_END_OF_BLOCK; - // Move planner pointer to next block and flag to load a new block for the next segment. - pl_prep_index = plan_next_block_index(pl_prep_index); - pl_prep_block = NULL; - } - - // New step segment completed. Increment segment buffer indices. - segment_buffer_head = segment_next_head; - if ( ++segment_next_head == SEGMENT_BUFFER_SIZE ) { segment_next_head = 0; } - -// long a = prep_segment->n_step; -// printInteger(a); -// printString(" "); -SPINDLE_ENABLE_PORT ^= 1<step_events_remaining*st_prep_data->mm_per_step; - if (st_prep_data->decelerate_after > 0) { *is_decelerating = false; } - else { *is_decelerating = true; } - - // Flag for new prep_block when st_prep_buffer() is called after the planner recomputes. - pl_partial_block_flag = true; - pl_prep_block = NULL; - } - return; -} diff --git a/archive/stepper_old.c b/archive/stepper_old.c deleted file mode 100644 index d702eba..0000000 --- a/archive/stepper_old.c +++ /dev/null @@ -1,746 +0,0 @@ -/* - stepper.c - stepper motor driver: executes motion plans using stepper motors - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" -#include "nuts_bolts.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) - -#define RAMP_NOOP_CRUISE 0 -#define RAMP_ACCEL 1 -#define RAMP_DECEL 2 - -#define LOAD_NOOP 0 -#define LOAD_SEGMENT 1 -#define LOAD_BLOCK 2 - -#define SEGMENT_NOOP 0 -#define SEGMENT_END_OF_BLOCK bit(0) -#define SEGMENT_ACCEL bit(1) -#define SEGMENT_DECEL bit(2) - -#define MINIMUM_STEPS_PER_SEGMENT 1 // Don't change - -#define SEGMENT_BUFFER_SIZE 6 - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - uint8_t segment_steps_remaining; // Steps remaining in line segment motion - - // Used by inverse time algorithm to track step rate - int32_t counter_d; // Inverse time distance traveled since last step event - uint32_t delta_d; // Inverse time distance traveled per interrupt tick - uint32_t d_per_tick; - - // Used by the stepper driver interrupt - uint8_t execute_step; // Flags step execution for each interrupt. - uint8_t step_pulse_time; // Step pulse reset time after step rise - uint8_t out_bits; // The next stepping-bits to be output - uint8_t load_flag; - - uint8_t ramp_count; - uint8_t ramp_type; -} stepper_t; -static stepper_t st; - -// Stores stepper common data for executing steps in the segment buffer. Data can change mid-block when the -// planner updates the remaining block velocity profile with a more optimal plan or a feedrate override occurs. -// NOTE: Normally, this buffer is partially in-use, but, for the worst case scenario, it will never exceed -// the number of accessible stepper buffer segments (SEGMENT_BUFFER_SIZE-1). -typedef struct { - int32_t step_events_remaining; // Tracks step event count for the executing planner block - uint32_t d_next; // Scaled distance to next step - uint32_t initial_rate; // Initialized step rate at re/start of a planner block - uint32_t nominal_rate; // The nominal step rate for this block in step_events/minute - uint32_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive) - uint32_t current_approx_rate; // Tracks the approximate segment rate to predict steps per segment to execute - int32_t decelerate_after; // Tracks when to initiate deceleration according to the planner block - float mm_per_step; -} st_data_t; -static st_data_t segment_data[SEGMENT_BUFFER_SIZE-1]; - -// Primary stepper segment ring buffer. Contains small, short line segments for the stepper algorithm to execute, -// which are "checked-out" incrementally from the first block in the planner buffer. Once "checked-out", the steps -// in the segments buffer cannot be modified by the planner, where the remaining planner block steps still can. -typedef struct { - uint8_t n_step; // Number of step events to be executed for this segment - uint8_t st_data_index; // Stepper buffer common data index. Uses this information to execute this segment. - uint8_t flag; // Stepper algorithm bit-flag for special execution conditions. -} st_segment_t; -static st_segment_t segment_buffer[SEGMENT_BUFFER_SIZE]; - -// Step segment ring buffer indices -static volatile uint8_t segment_buffer_tail; -static volatile uint8_t segment_buffer_head; -static uint8_t segment_next_head; - -static volatile uint8_t busy; // Used to avoid ISR nesting of the "Stepper Driver Interrupt". Should never occur though. -static plan_block_t *pl_current_block; // A pointer to the planner block currently being traced -static st_segment_t *st_current_segment; -static st_data_t *st_current_data; - -// Pointers for the step segment being prepped from the planner buffer. Accessed only by the -// main program. Pointers may be planning segments or planner blocks ahead of what being executed. -static plan_block_t *pl_prep_block; // Pointer to the planner block being prepped -static st_data_t *st_prep_data; // Pointer to the stepper common data being prepped -static uint8_t pl_prep_index; // Index of planner block being prepped -static uint8_t st_data_prep_index; // Index of stepper common data block being prepped -static uint8_t pl_partial_block_flag; // Flag indicating the planner has modified the prepped planner block - - -/* __________________________ - /| |\ _________________ ^ - / | | \ /| |\ | - / | | \ / | | \ s - / | | | | | \ p - / | | | | | \ e - +-----+------------------------+---+--+---------------+----+ e - | BLOCK 1 | BLOCK 2 | d - - time -----> - - The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta - until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after - after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as - +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. -*/ - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - // Enable stepper driver interrupt - st.execute_step = false; - st.load_flag = LOAD_BLOCK; - - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<n_step; - - // Check if the counters need to be reset for a new planner block - if (st.load_flag == LOAD_BLOCK) { - pl_current_block = plan_get_current_block(); // Should always be there. Stepper buffer handles this. - st_current_data = &segment_data[segment_buffer[segment_buffer_tail].st_data_index]; //st_current_segment->st_data_index]; - - // Initialize direction bits for block - st.out_bits = pl_current_block->direction_bits ^ settings.invert_mask; - st.execute_step = true; // Set flag to set direction bits upon next ISR tick. - - // Initialize Bresenham line counters - st.counter_x = (pl_current_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - - // Initialize inverse time and step rate counter data - st.counter_d = st_current_data->d_next; // d_next always greater than delta_d. - if (st.delta_d < MINIMUM_STEP_RATE) { st.d_per_tick = MINIMUM_STEP_RATE; } - else { st.d_per_tick = st.delta_d; } - - // During feed hold, do not update rate, ramp type, or ramp counters. Keep decelerating. -// if (sys.state == STATE_CYCLE) { - st.delta_d = st_current_data->initial_rate; - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; // Initialize ramp counter via midpoint rule - st.ramp_type = RAMP_NOOP_CRUISE; // Initialize as no ramp operation. Corrected later if necessary. -// } - - } - - // Acceleration and cruise handled by ramping. Just check if deceleration needs to begin. - if ( st_current_segment->flag & (SEGMENT_DECEL | SEGMENT_ACCEL) ) { - /* Compute correct ramp count for a ramp change. Upon a switch from acceleration to deceleration, - or vice-versa, the new ramp count must be set to trigger the next acceleration tick equal to - the number of ramp ISR ticks counted since the last acceleration tick. This is ensures the - ramp is executed exactly as the plan dictates. Otherwise, when a ramp begins from a known - rate (nominal/cruise or initial), the ramp count must be set to ISR_TICKS_PER_ACCELERATION_TICK/2 - as mandated by the mid-point rule. For these conditions, the ramp count have been initialized - such that the following computation is still correct. */ - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK-st.ramp_count; - if ( st_current_segment->flag & SEGMENT_DECEL ) { st.ramp_type = RAMP_DECEL; } - else { st.ramp_type = RAMP_ACCEL; } - } - - st.load_flag = LOAD_NOOP; // Segment motion loaded. Set no-operation flag to skip during execution. - - } else { - // Can't discard planner block here if a feed hold stops in middle of block. - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - - } - - // Adjust inverse time counter for ac/de-celerations - // NOTE: Accelerations are handled by the stepper algorithm as it's thought to be more computationally - // efficient on the Arduino AVR. This could may not be true with higher ISR frequencies or faster CPUs. - if (st.ramp_type) { // Ignored when ramp type is NOOP_CRUISE - st.ramp_count--; // Tick acceleration ramp counter - if (st.ramp_count == 0) { // Adjust step rate when its time - if (st.ramp_type == RAMP_ACCEL) { // Adjust velocity for acceleration - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK; // Reload ramp counter - st.delta_d += st_current_data->rate_delta; - if (st.delta_d >= st_current_data->nominal_rate) { // Reached nominal rate. - st.delta_d = st_current_data->nominal_rate; // Set cruising velocity - st.ramp_type = RAMP_NOOP_CRUISE; // Set ramp flag to cruising - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; // Re-initialize counter for next ramp. - } - } else { // Adjust velocity for deceleration. - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK; // Reload ramp counter - if (st.delta_d > st_current_data->rate_delta) { - st.delta_d -= st_current_data->rate_delta; - } else { // Moving near zero feed rate. Gracefully slow down. - st.delta_d >>= 1; // Integer divide by 2 until complete. Also prevents overflow. - - // TODO: Check for and handle feed hold exit? At this point, machine is stopped. - // - Set system flag to recompute plan and reset segment buffer. - // - Segment steps in buffer needs to be returned to planner correctly. - // busy = false; - // return; - - } - } - // Finalize adjusted step rate. Ensure minimum. - if (st.delta_d < MINIMUM_STEP_RATE) { st.d_per_tick = MINIMUM_STEP_RATE; } - else { st.d_per_tick = st.delta_d; } - } - } - - // Iterate inverse time counter. Triggers each Bresenham step event. - st.counter_d -= st.d_per_tick; - - // Execute Bresenham step event, when it's time to do so. - if (st.counter_d < 0) { - st.counter_d += st_current_data->d_next; // Reload inverse time counter - - st.out_bits = pl_current_block->direction_bits; // Reset out_bits and reload direction bits - st.execute_step = true; - - // Execute step displacement profile by Bresenham line algorithm - st.counter_x -= pl_current_block->steps[X_AXIS]; - if (st.counter_x < 0) { - st.out_bits |= (1<step_event_count; - // st.steps_x++; - if (st.out_bits & (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - st.out_bits |= (1<step_event_count; - // st.steps_y++; - if (st.out_bits & (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - st.out_bits |= (1<step_event_count; - // st.steps_z++; - if (st.out_bits & (1< 0) { - if (st.out_bits & (1< 0) { - if (st.out_bits & (1< 0) { - if (st.out_bits & (1<flag & SEGMENT_END_OF_BLOCK) { - plan_discard_current_block(); - st.load_flag = LOAD_BLOCK; - } else { - st.load_flag = LOAD_SEGMENT; - } - - // Discard current segment by advancing buffer tail index - if ( ++segment_buffer_tail == SEGMENT_BUFFER_SIZE) { segment_buffer_tail = 0; } - - } - - st.out_bits ^= settings.invert_mask; // Apply step port invert mask - } - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<step_events_remaining); -// st.ramp_type = RAMP_ACCEL; -// st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; -// st.delta_d = 0; -// sys.state = STATE_QUEUED; -// } else { -// sys.state = STATE_IDLE; -// } - sys.state = STATE_IDLE; - -} - - -/* Prepares step segment buffer. Continuously called from main program. - - The segment buffer is an intermediary buffer interface between the execution of steps - by the stepper algorithm and the velocity profiles generated by the planner. The stepper - algorithm only executes steps within the segment buffer and is filled by the main program - when steps are "checked-out" from the first block in the planner buffer. This keeps the - step execution and planning optimization processes atomic and protected from each other. - The number of steps "checked-out" from the planner buffer and the number of segments in - the segment buffer is sized and computed such that no operation in the main program takes - longer than the time it takes the stepper algorithm to empty it before refilling it. - Currently, the segment buffer conservatively holds roughly up to 40-60 msec of steps. - - NOTE: The segment buffer executes a set number of steps over an approximate time period. - If we try to execute over a set time period, it is difficult to guarantee or predict how - many steps will execute over it, especially when the step pulse phasing between the - neighboring segments are kept consistent. Meaning that, if the last segment step pulses - right before its end, the next segment must delay its first pulse so that the step pulses - are consistently spaced apart over time to keep the step pulse train nice and smooth. - Keeping track of phasing and ensuring that the exact number of steps are executed as - defined by the planner block, the related computational overhead gets quickly and - prohibitively expensive, especially in real-time. - Since the stepper algorithm automatically takes care of the step pulse phasing with - its ramp and inverse time counters, we don't have to explicitly and expensively track the - exact number of steps, time, or phasing of steps. All we need to do is approximate - the number of steps in each segment such that the segment buffer has enough execution time - for the main program to do what it needs to do and refill it when it has time. In other - words, we just need to compute a cheap approximation of the current velocity and the - number of steps over it. -*/ - -/* - TODO: Figure out how to enforce a deceleration when a feedrate override is reduced. - The problem is that when an override is reduced, the planner may not plan back to - the current rate. Meaning that the velocity profiles for certain conditions no longer - are trapezoidal or triangular. For example, if the current block is cruising at a - nominal rate and the feedrate override is reduced, the new nominal rate will now be - lower. The velocity profile must first decelerate to the new nominal rate and then - follow on the new plan. So the remaining velocity profile will have a decelerate, - cruise, and another decelerate. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already - heavily decelerating throughout it, reducing the feedrate will not do much to it. So, - how do we determine when to resume the new plan? How many blocks do we have to wait - until the new plan intersects with the deceleration curve? One plus though, the - deceleration will never be more than the number of blocks in the entire planner buffer, - but it theoretically can be equal to it when all planner blocks are decelerating already. -*/ -void st_prep_buffer() -{ - while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. - - // Initialize new segment - st_segment_t *prep_segment = &segment_buffer[segment_buffer_head]; - prep_segment->flag = SEGMENT_NOOP; - - // Determine if we need to load a new planner block. - if (pl_prep_block == NULL) { - pl_prep_block = plan_get_block_by_index(pl_prep_index); // Query planner for a queued block - if (pl_prep_block == NULL) { return; } // No planner blocks. Exit. - - // Increment stepper common data index - if ( ++st_data_prep_index == (SEGMENT_BUFFER_SIZE-1) ) { st_data_prep_index = 0; } - - // Check if the planner has re-computed this block mid-execution. If so, push the previous segment - // data. Otherwise, prepare a new segment data for the new planner block. - if (pl_partial_block_flag) { - - // Prepare new shared segment block data and copy the relevant last segment block data. - st_data_t *last_st_prep_data; - last_st_prep_data = st_prep_data; - st_prep_data = &segment_data[st_data_prep_index]; - - st_prep_data->step_events_remaining = last_st_prep_data->step_events_remaining; - st_prep_data->rate_delta = last_st_prep_data->rate_delta; - st_prep_data->d_next = last_st_prep_data->d_next; - st_prep_data->nominal_rate = last_st_prep_data->nominal_rate; // TODO: Feedrate overrides recomputes this. - - st_prep_data->mm_per_step = last_st_prep_data->mm_per_step; - - pl_partial_block_flag = false; // Reset flag - - } else { - - // Prepare commonly shared planner block data for the ensuing segment buffer moves ad-hoc, since - // the planner buffer can dynamically change the velocity profile data as blocks are added. - st_prep_data = &segment_data[st_data_prep_index]; - - // Initialize Bresenham variables - st_prep_data->step_events_remaining = pl_prep_block->step_event_count; - - // Convert planner block velocity profile data to stepper rate and step distance data. - st_prep_data->nominal_rate = ceil(sqrt(pl_prep_block->nominal_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - st_prep_data->rate_delta = ceil(pl_prep_block->acceleration* - ((INV_TIME_MULTIPLIER/(60.0*60.0))/(ISR_TICKS_PER_SECOND*ACCELERATION_TICKS_PER_SECOND))); // (mult*mm/isr_tic/accel_tic) - st_prep_data->d_next = ceil((pl_prep_block->millimeters*INV_TIME_MULTIPLIER)/pl_prep_block->step_event_count); // (mult*mm/step) - - // TODO: Check if we really need to store this. - st_prep_data->mm_per_step = pl_prep_block->millimeters/pl_prep_block->step_event_count; - - } - - // Convert planner entry speed to stepper initial rate. - st_prep_data->initial_rate = ceil(sqrt(pl_prep_block->entry_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - // TODO: Nominal rate changes with feedrate override. - // st_prep_data->nominal_rate = ceil(sqrt(pl_prep_block->nominal_speed_sqr)*(INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))); // (mult*mm/isr_tic) - - st_prep_data->current_approx_rate = st_prep_data->initial_rate; - - // Calculate the planner block velocity profile type, determine deceleration point, and initial ramp. - float mm_decelerate_after = plan_calculate_velocity_profile(pl_prep_index); - st_prep_data->decelerate_after = ceil( mm_decelerate_after/st_prep_data->mm_per_step ); - if (st_prep_data->decelerate_after > 0) { // If 0, SEGMENT_DECEL flag is set later. - if (st_prep_data->initial_rate != st_prep_data->nominal_rate) { prep_segment->flag = SEGMENT_ACCEL; } - } - } - - // Set new segment to point to the current segment data block. - prep_segment->st_data_index = st_data_prep_index; - - // Approximate the velocity over the new segment using the already computed rate values. - // NOTE: This assumes that each segment will have an execution time roughly equal to every ACCELERATION_TICK. - // We do this to minimize memory and computational requirements. However, this could easily be replaced with - // a more exact approximation or have a unique time per segment, if CPU and memory overhead allows. - if (st_prep_data->decelerate_after <= 0) { - if (st_prep_data->decelerate_after == 0) { prep_segment->flag = SEGMENT_DECEL; } // Set segment deceleration flag - else { st_prep_data->current_approx_rate -= st_prep_data->rate_delta; } - if (st_prep_data->current_approx_rate < st_prep_data->rate_delta) { st_prep_data->current_approx_rate >>= 1; } - } else { - if (st_prep_data->current_approx_rate < st_prep_data->nominal_rate) { - st_prep_data->current_approx_rate += st_prep_data->rate_delta; - if (st_prep_data->current_approx_rate > st_prep_data->nominal_rate) { - st_prep_data->current_approx_rate = st_prep_data->nominal_rate; - } - } - } - - // Compute the number of steps in the prepped segment based on the approximate current rate. - // NOTE: The d_next divide cancels out the INV_TIME_MULTIPLIER and converts the rate value to steps. - prep_segment->n_step = ceil(max(MINIMUM_STEP_RATE,st_prep_data->current_approx_rate)* - (ISR_TICKS_PER_SECOND/ACCELERATION_TICKS_PER_SECOND)/st_prep_data->d_next); - // NOTE: Ensures it moves for very slow motions, but MINIMUM_STEP_RATE should always set this too. Perhaps - // a compile-time check to see if MINIMUM_STEP_RATE is set high enough is all that is needed. - prep_segment->n_step = max(prep_segment->n_step,MINIMUM_STEPS_PER_SEGMENT); - // NOTE: As long as the ACCELERATION_TICKS_PER_SECOND is valid, n_step should never exceed 255 and overflow. - // prep_segment->n_step = min(prep_segment->n_step,MAXIMUM_STEPS_PER_BLOCK); // Prevent unsigned int8 overflow. - - // Check if n_step exceeds steps remaining in planner block. If so, truncate. - if (prep_segment->n_step > st_prep_data->step_events_remaining) { - prep_segment->n_step = st_prep_data->step_events_remaining; - } - - // Check if n_step crosses decelerate point in block. If so, truncate to ensure the deceleration - // ramp counters are set correctly during execution. - if (st_prep_data->decelerate_after > 0) { - if (prep_segment->n_step > st_prep_data->decelerate_after) { - prep_segment->n_step = st_prep_data->decelerate_after; - } - } - - // Update stepper common data variables. - st_prep_data->decelerate_after -= prep_segment->n_step; - st_prep_data->step_events_remaining -= prep_segment->n_step; - - // Check for end of planner block - if ( st_prep_data->step_events_remaining == 0 ) { - // Set EOB bitflag so stepper algorithm discards the planner block after this segment completes. - prep_segment->flag |= SEGMENT_END_OF_BLOCK; - // Move planner pointer to next block and flag to load a new block for the next segment. - pl_prep_index = plan_next_block_index(pl_prep_index); - pl_prep_block = NULL; - } - - // New step segment completed. Increment segment buffer indices. - segment_buffer_head = segment_next_head; - if ( ++segment_next_head == SEGMENT_BUFFER_SIZE ) { segment_next_head = 0; } - -// long a = prep_segment->n_step; -// printInteger(a); -// printString(" "); - - } -} - -uint8_t st_get_prep_block_index() -{ -// Returns only the index but doesn't state if the block has been partially executed. How do we simply check for this? - return(pl_prep_index); -} - -void st_fetch_partial_block_parameters(uint8_t block_index, float *millimeters_remaining, uint8_t *is_decelerating) -{ - // if called, can we assume that this always changes and needs to be updated? if so, then - // we can perform all of the segment buffer setup tasks here to make sure the next time - // the segments are loaded, the st_data buffer is updated correctly. - // !!! Make sure that this is always pointing to the correct st_prep_data block. - - // When a mid-block acceleration occurs, we have to make sure the ramp counters are updated - // correctly, much in the same fashion as the deceleration counters. Need to think about this - // make sure this is right, but i'm pretty sure it is. - - // TODO: NULL means that the segment buffer has just completed a planner block. Clean up! - if (pl_prep_block != NULL) { - *millimeters_remaining = st_prep_data->step_events_remaining*st_prep_data->mm_per_step; - if (st_prep_data->decelerate_after > 0) { *is_decelerating = false; } - else { *is_decelerating = true; } - - // Flag for new prep_block when st_prep_buffer() is called after the planner recomputes. - pl_partial_block_flag = true; - pl_prep_block = NULL; - } - return; -} diff --git a/archive/stepper_time.c b/archive/stepper_time.c deleted file mode 100644 index 4b73098..0000000 --- a/archive/stepper_time.c +++ /dev/null @@ -1,775 +0,0 @@ -/* - stepper.c - stepper motor driver: executes motion plans using stepper motors - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" -#include "nuts_bolts.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) - -#define RAMP_NOOP_CRUISE 0 -#define RAMP_ACCEL 1 -#define RAMP_DECEL 2 - -#define LOAD_NOOP 0 -#define LOAD_SEGMENT 1 -#define LOAD_BLOCK 2 - -#define SEGMENT_NOOP 0 -#define SEGMENT_END_OF_BLOCK bit(0) -#define RAMP_CHANGE_ACCEL bit(1) -#define RAMP_CHANGE_DECEL bit(2) - -#define MINIMUM_STEPS_PER_SEGMENT 1 // Don't change - -#define SEGMENT_BUFFER_SIZE 6 - -#define DT_SEGMENT ACCELERATION_TICKS_PER_SECOND/ISR_TICKS_PER_SECOND - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - - // Used by inverse time algorithm to track step rate - int32_t counter_dist; // Inverse time distance traveled since last step event - - uint8_t step_count; // Steps remaining in line segment motion - uint8_t phase_count; // Phase ticks remaining after line segment steps complete - - // Used by the stepper driver interrupt - uint8_t execute_step; // Flags step execution for each interrupt. - uint8_t step_pulse_time; // Step pulse reset time after step rise - uint8_t out_bits; // The next stepping-bits to be output - uint8_t load_flag; -} stepper_t; -static stepper_t st; - -// Stores stepper common data for executing steps in the segment buffer. Data can change mid-block when the -// planner updates the remaining block velocity profile with a more optimal plan or a feedrate override occurs. -// NOTE: Normally, this buffer is partially in-use, but, for the worst case scenario, it will never exceed -// the number of accessible stepper buffer segments (SEGMENT_BUFFER_SIZE-1). -typedef struct { - uint32_t dist_per_step; - float step_events_remaining; // Tracks step event count for the executing planner block - float accelerate_until; - float decelerate_after; - float current_rate; - float maximum_rate; - float exit_rate; - - float acceleration; - float step_per_mm; -} st_data_t; -static st_data_t segment_data[SEGMENT_BUFFER_SIZE-1]; - -// Primary stepper segment ring buffer. Contains small, short line segments for the stepper algorithm to execute, -// which are "checked-out" incrementally from the first block in the planner buffer. Once "checked-out", the steps -// in the segments buffer cannot be modified by the planner, where the remaining planner block steps still can. -typedef struct { - uint8_t n_step; // Number of step events to be executed for this segment - uint8_t n_phase_tick; - uint32_t dist_per_tick; - uint8_t st_data_index; // Stepper buffer common data index. Uses this information to execute this segment. - uint8_t flag; // Stepper algorithm bit-flag for special execution conditions. -} st_segment_t; -static st_segment_t segment_buffer[SEGMENT_BUFFER_SIZE]; - -// Step segment ring buffer indices -static volatile uint8_t segment_buffer_tail; -static volatile uint8_t segment_buffer_head; -static uint8_t segment_next_head; - -static volatile uint8_t busy; // Used to avoid ISR nesting of the "Stepper Driver Interrupt". Should never occur though. -static plan_block_t *pl_current_block; // A pointer to the planner block currently being traced -static st_segment_t *st_current_segment; -static st_data_t *st_current_data; - -// Pointers for the step segment being prepped from the planner buffer. Accessed only by the -// main program. Pointers may be planning segments or planner blocks ahead of what being executed. -static plan_block_t *pl_prep_block; // Pointer to the planner block being prepped -static st_data_t *st_prep_data; // Pointer to the stepper common data being prepped -static uint8_t pl_prep_index; // Index of planner block being prepped -static uint8_t st_data_prep_index; // Index of stepper common data block being prepped -static uint8_t pl_partial_block_flag; // Flag indicating the planner has modified the prepped planner block - - -/* __________________________ - /| |\ _________________ ^ - / | | \ /| |\ | - / | | \ / | | \ s - / | | | | | \ p - / | | | | | \ e - +-----+------------------------+---+--+---------------+----+ e - | BLOCK 1 | BLOCK 2 | d - - time -----> - - The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta - until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after - after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as - +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. -*/ - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - // Enable stepper driver interrupt - st.execute_step = false; - st.load_flag = LOAD_BLOCK; - - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<n_step; - - // If the new segment starts a new planner block, initialize stepper variables and counters. - // NOTE: For new segments only, the step counters are not updated to ensure step phasing is continuous. - if (st.load_flag == LOAD_BLOCK) { - pl_current_block = plan_get_current_block(); // Should always be there. Stepper buffer handles this. - st_current_data = &segment_data[segment_buffer[segment_buffer_tail].st_data_index]; - - // Initialize direction bits for block. Set execute flag to set directions bits upon next ISR tick. - st.out_bits = pl_current_block->direction_bits ^ settings.invert_mask; - st.execute_step = true; - - // Initialize Bresenham line counters - st.counter_x = (pl_current_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - - // Initialize inverse time, step rate data, and acceleration ramp counters - st.counter_dist = st_current_data->dist_per_step; // dist_per_step always greater than dist_per_tick. - } - - st.load_flag = LOAD_NOOP; // Segment motion loaded. Set no-operation flag to skip during execution. - - } else { - // Can't discard planner block here if a feed hold stops in middle of block. - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - - } - - // Iterate inverse time counter. Triggers each Bresenham step event. - st.counter_dist -= st_current_segment->dist_per_tick; - - // Execute Bresenham step event, when it's time to do so. - if (st.counter_dist < 0) { - if (st.step_count > 0) { // Block phase correction from executing step. - st.counter_dist += st_current_data->dist_per_step; // Reload inverse time counter - - st.out_bits = pl_current_block->direction_bits; // Reset out_bits and reload direction bits - st.execute_step = true; - - // Execute step displacement profile by Bresenham line algorithm - st.counter_x -= pl_current_block->steps[X_AXIS]; - if (st.counter_x < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<flag & SEGMENT_END_OF_BLOCK) { - plan_discard_current_block(); - st.load_flag = LOAD_BLOCK; - } else { - st.load_flag = LOAD_SEGMENT; - } - - // Discard current segment by advancing buffer tail index - if ( ++segment_buffer_tail == SEGMENT_BUFFER_SIZE) { segment_buffer_tail = 0; } - } - st.phase_count--; - } - - - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<step_events_remaining); -// st.ramp_type = RAMP_ACCEL; -// st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; -// st.ramp_rate = 0; -// sys.state = STATE_QUEUED; -// } else { -// sys.state = STATE_IDLE; -// } - sys.state = STATE_IDLE; - -} - - -/* Prepares step segment buffer. Continuously called from main program. - - The segment buffer is an intermediary buffer interface between the execution of steps - by the stepper algorithm and the velocity profiles generated by the planner. The stepper - algorithm only executes steps within the segment buffer and is filled by the main program - when steps are "checked-out" from the first block in the planner buffer. This keeps the - step execution and planning optimization processes atomic and protected from each other. - The number of steps "checked-out" from the planner buffer and the number of segments in - the segment buffer is sized and computed such that no operation in the main program takes - longer than the time it takes the stepper algorithm to empty it before refilling it. - Currently, the segment buffer conservatively holds roughly up to 40-60 msec of steps. - - NOTE: The segment buffer executes a set number of steps over an approximate time period. - If we try to execute over a fixed time period, it is difficult to guarantee or predict - how many steps will execute over it, especially when the step pulse phasing between the - neighboring segments must also be kept consistent. Meaning that, if the last segment step - pulses right before a segment end, the next segment must delay its first pulse so that the - step pulses are consistently spaced apart over time to keep the step pulse train nice and - smooth. Keeping track of phasing and ensuring that the exact number of steps are executed - as defined by the planner block, the related computational overhead can get quickly and - prohibitively expensive, especially in real-time. - Since the stepper algorithm automatically takes care of the step pulse phasing with - its ramp and inverse time counters by retaining the count remainders, we don't have to - explicitly and expensively track and synchronize the exact number of steps, time, and - phasing of steps. All we need to do is approximate the number of steps in each segment - such that the segment buffer has enough execution time for the main program to do what - it needs to do and refill it when it comes back. In other words, we just need to compute - a cheap approximation of the current velocity and the number of steps over it. -*/ - -/* - TODO: Figure out how to enforce a deceleration when a feedrate override is reduced. - The problem is that when an override is reduced, the planner may not plan back to - the current rate. Meaning that the velocity profiles for certain conditions no longer - are trapezoidal or triangular. For example, if the current block is cruising at a - nominal rate and the feedrate override is reduced, the new nominal rate will now be - lower. The velocity profile must first decelerate to the new nominal rate and then - follow on the new plan. So the remaining velocity profile will have a decelerate, - cruise, and another decelerate. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already - heavily decelerating throughout it, reducing the feedrate will not do much to it. So, - how do we determine when to resume the new plan? How many blocks do we have to wait - until the new plan intersects with the deceleration curve? One plus though, the - deceleration will never be more than the number of blocks in the entire planner buffer, - but it theoretically can be equal to it when all planner blocks are decelerating already. -*/ -void st_prep_buffer() -{ - if (sys.state == STATE_QUEUED) { return; } // Block until a motion state is issued - while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. - - // Initialize new segment - st_segment_t *prep_segment = &segment_buffer[segment_buffer_head]; - prep_segment->flag = SEGMENT_NOOP; - - // ----------------------------------------------------------------------------------- - // Determine if we need to load a new planner block. If so, prepare step data. - if (pl_prep_block == NULL) { - pl_prep_block = plan_get_block_by_index(pl_prep_index); // Query planner for a queued block - if (pl_prep_block == NULL) { return; } // No planner blocks. Exit. - SPINDLE_ENABLE_PORT ^= 1<step_events_remaining = last_st_prep_data->step_events_remaining; - st_prep_data->dist_per_step = last_st_prep_data->dist_per_step; - st_prep_data->step_per_mm = last_st_prep_data->step_per_mm; - st_prep_data->acceleration = last_st_prep_data->acceleration; - - pl_partial_block_flag = false; // Reset flag - - } else { - - // Prepare commonly shared planner block data for the ensuing segment buffer moves ad-hoc, since - // the planner buffer can dynamically change the velocity profile data as blocks are added. - st_prep_data = &segment_data[st_data_prep_index]; - - // Initialize planner block step data - st_prep_data->step_events_remaining = pl_prep_block->step_event_count; - st_prep_data->step_per_mm = pl_prep_block->step_event_count/pl_prep_block->millimeters; - st_prep_data->dist_per_step = ceil(INV_TIME_MULTIPLIER/st_prep_data->step_per_mm); // (mult*mm/step) - st_prep_data->acceleration = st_prep_data->step_per_mm*pl_prep_block->acceleration; - - } - - // Convert planner entry speed to stepper initial rate. - st_prep_data->current_rate = st_prep_data->step_per_mm*sqrt(pl_prep_block->entry_speed_sqr); - - // Determine current block exit speed - plan_block_t *pl_next_block = plan_get_block_by_index(plan_next_block_index(pl_prep_index)); - float exit_speed_sqr; - if (pl_next_block != NULL) { - exit_speed_sqr = pl_next_block->entry_speed_sqr; - st_prep_data->exit_rate = st_prep_data->step_per_mm*sqrt(exit_speed_sqr); - } else { - exit_speed_sqr = 0.0; // End of planner buffer. Zero speed. - st_prep_data->exit_rate = 0.0; - } - - // Determine velocity profile based on the 7 possible types: Cruise-only, cruise-deceleration, - // acceleration-cruise, acceleration-only, deceleration-only, trapezoid, and triangle. - st_prep_data->accelerate_until = pl_prep_block->millimeters; - if (pl_prep_block->entry_speed_sqr == pl_prep_block->nominal_speed_sqr) { - st_prep_data->maximum_rate = sqrt(pl_prep_block->nominal_speed_sqr); - st_prep_data->accelerate_until = pl_prep_block->millimeters; - if (exit_speed_sqr == pl_prep_block->nominal_speed_sqr) { // Cruise-only type - st_prep_data->decelerate_after = 0.0; - } else { // Cruise-deceleration type - st_prep_data->decelerate_after = (pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(2*pl_prep_block->acceleration); - } - } else if (exit_speed_sqr == pl_prep_block->nominal_speed_sqr) { - // Acceleration-cruise type - st_prep_data->maximum_rate = sqrt(pl_prep_block->nominal_speed_sqr); - st_prep_data->decelerate_after = 0.0; - st_prep_data->accelerate_until -= (pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(2*pl_prep_block->acceleration); - } else { - float intersection_dist = 0.5*( pl_prep_block->millimeters + (pl_prep_block->entry_speed_sqr - - exit_speed_sqr)/(2*pl_prep_block->acceleration) ); - if (intersection_dist > 0.0) { - if (intersection_dist < pl_prep_block->millimeters) { // Either trapezoid or triangle types - st_prep_data->decelerate_after = (pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(2*pl_prep_block->acceleration); - if (st_prep_data->decelerate_after < intersection_dist) { // Trapezoid type - st_prep_data->maximum_rate = sqrt(pl_prep_block->nominal_speed_sqr); - st_prep_data->accelerate_until -= (pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(2*pl_prep_block->acceleration); - } else { // Triangle type - st_prep_data->decelerate_after = intersection_dist; - st_prep_data->maximum_rate = sqrt(2*pl_prep_block->acceleration*st_prep_data->decelerate_after+exit_speed_sqr); - st_prep_data->accelerate_until -= st_prep_data->decelerate_after; - } - } else { // Deceleration-only type - st_prep_data->maximum_rate = sqrt(pl_prep_block->entry_speed_sqr); - st_prep_data->decelerate_after = pl_prep_block->millimeters; - } - } else { // Acceleration-only type - st_prep_data->maximum_rate = sqrt(exit_speed_sqr); - st_prep_data->decelerate_after = 0.0; - st_prep_data->accelerate_until = 0.0; - } - } - - // Determine block velocity profile parameters -// st_prep_data->accelerate_until = (pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(2*pl_prep_block->acceleration); -// st_prep_data->decelerate_after = (pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(2*pl_prep_block->acceleration); -// -// // Determine if velocity profile is a triangle or trapezoid. -// if (pl_prep_block->millimeters < st_prep_data->accelerate_until+st_prep_data->decelerate_after) { -// st_prep_data->decelerate_after = 0.5*( pl_prep_block->millimeters + (pl_prep_block->entry_speed_sqr -// - exit_speed_sqr)/(2*pl_prep_block->acceleration) ); -// st_prep_data->accelerate_until = pl_prep_block->millimeters-st_prep_data->decelerate_after; -// st_prep_data->maximum_speed = sqrt(2*pl_prep_block->acceleration*st_prep_data->decelerate_after+exit_speed_sqr); -// } else { -// st_prep_data->accelerate_until = pl_prep_block->millimeters-st_prep_data->accelerate_until; -// st_prep_data->maximum_speed = sqrt(pl_prep_block->nominal_speed_sqr); -// } - - // Convert velocity profile parameters in terms of steps. - st_prep_data->maximum_rate *= st_prep_data->step_per_mm; - st_prep_data->accelerate_until *= st_prep_data->step_per_mm; - st_prep_data->decelerate_after *= st_prep_data->step_per_mm; - - } - - // Set new segment to point to the current segment data block. - prep_segment->st_data_index = st_data_prep_index; - - // ----------------------------------------------------------------------------------- - // Initialize segment execute distance. Attempt to create a full segment over DT_SEGMENT. - // NOTE: Computed in terms of steps and seconds to prevent numerical round-off issues. - - float steps_remaining = st_prep_data->step_events_remaining; - float dt = DT_SEGMENT; - if (steps_remaining > st_prep_data->accelerate_until) { // Acceleration ramp - steps_remaining -= st_prep_data->current_rate*DT_SEGMENT - + st_prep_data->acceleration*(0.5*DT_SEGMENT*DT_SEGMENT); - if (steps_remaining < st_prep_data->accelerate_until) { // **Incomplete** Acceleration ramp end. - // Acceleration-cruise, acceleration-deceleration ramp junction, or end of block - steps_remaining = st_prep_data->accelerate_until; - dt = 2*(st_prep_data->step_events_remaining-steps_remaining)/ - (st_prep_data->current_rate+st_prep_data->maximum_rate); - st_prep_data->current_rate = st_prep_data->maximum_rate; - } else { // **Complete** Acceleration only. - st_prep_data->current_rate += st_prep_data->acceleration*DT_SEGMENT; - } - } else if (steps_remaining <= st_prep_data->decelerate_after) { // Deceleration ramp - steps_remaining -= st_prep_data->current_rate*DT_SEGMENT - - st_prep_data->acceleration*(0.5*DT_SEGMENT*DT_SEGMENT); - if (steps_remaining > 0) { // **Complete** Deceleration only. - st_prep_data->current_rate -= st_prep_data->acceleration*DT_SEGMENT; - } else { // **Complete* End of block. - dt = 2*st_prep_data->step_events_remaining/(st_prep_data->current_rate+st_prep_data->exit_rate); - steps_remaining = 0; - // st_prep_data->current_speed = st_prep_data->exit_speed; - } - } else { // Cruising profile - steps_remaining -= st_prep_data->maximum_rate*DT_SEGMENT; - if (steps_remaining < st_prep_data->decelerate_after) { // **Incomplete** End of cruise. - steps_remaining = st_prep_data->decelerate_after; - dt = (st_prep_data->step_events_remaining-steps_remaining)/st_prep_data->maximum_rate; - } // Otherwise **Complete** Cruising only. - } - - // ----------------------------------------------------------------------------------- - // If segment is incomplete, attempt to fill the remainder. - // NOTE: Segment remainder always spans a cruise and/or a deceleration ramp. - - if (dt < DT_SEGMENT) { - if (steps_remaining > 0) { // Skip if end of block. - float last_steps_remaining; - - // Fill incomplete segment with an acceleration junction. - if (steps_remaining > st_prep_data->decelerate_after) { // Cruising profile - last_steps_remaining = steps_remaining; - steps_remaining -= st_prep_data->current_rate*(DT_SEGMENT-dt); - if (steps_remaining < st_prep_data->decelerate_after) { // **Incomplete** - steps_remaining = st_prep_data->decelerate_after; - dt += (last_steps_remaining-steps_remaining)/st_prep_data->maximum_rate; - // current_speed = maximum_speed; - } else { // **Complete** Segment filled. - dt = DT_SEGMENT; - } - } - - // Fill incomplete segment with a deceleration junction. - if (steps_remaining > 0) { - if (steps_remaining <= st_prep_data->decelerate_after) { // Deceleration ramp - last_steps_remaining = steps_remaining; - float dt_remainder = DT_SEGMENT-dt; - steps_remaining -= dt_remainder*(st_prep_data->current_rate - - 0.5*st_prep_data->acceleration*dt_remainder); - if (steps_remaining > 0) { // **Complete** Segment filled. - st_prep_data->current_rate -= st_prep_data->acceleration*dt_remainder; - dt = DT_SEGMENT; - } else { // **Complete** End of block. - steps_remaining = 0; - dt += (2*last_steps_remaining/(st_prep_data->current_rate+st_prep_data->exit_rate)); - // st_prep_data->current_speed = st_prep_data->exit_speed; - } - } - } - - } - } - - // ----------------------------------------------------------------------------------- - // Compute segment step rate, steps to execute, and step phase correction parameters. - // NOTE: - - // !!! PROBLEM. Step events remaining in floating point can limit the number of steps - // we can accurately track, since floats have 8 significant digits. However, this only - // becomes a problem if there are more than 10,000,000, which translates to a CNC machine - // with 800 step/mm and 10 meters of axis travel. - - prep_segment->dist_per_tick = ceil((st_prep_data->step_events_remaining-steps_remaining) - /dt*(INV_TIME_MULTIPLIER/ISR_TICKS_PER_SECOND)); // (mult*mm/isr_tic) - - if (steps_remaining > 0) { - - // Compute number of steps to execute and segment step phase correction. - prep_segment->n_step = ceil(st_prep_data->step_events_remaining)-ceil(steps_remaining); - prep_segment->n_phase_tick = ceil((ceil(steps_remaining)-steps_remaining)*st_prep_data->dist_per_step); - - } else { // End of block. Finish it out. - - // Set to execute the remaining steps and no phase correction upon finishing the block. - prep_segment->n_step = ceil(st_prep_data->step_events_remaining); - prep_segment->n_phase_tick = 0; - - // Move planner pointer to next block and flag to load a new block for the next segment. - pl_prep_index = plan_next_block_index(pl_prep_index); - pl_prep_block = NULL; - prep_segment->flag |= SEGMENT_END_OF_BLOCK; - } - - // Update step execution variables - st_prep_data->step_events_remaining = steps_remaining; - - // Ensure the initial step rate exceeds the MINIMUM_STEP_RATE. - // TODO: Use config.h error checking to do this. Otherwise, counters get screwy. - - // New step segment initialization completed. Increment segment buffer indices. - segment_buffer_head = segment_next_head; - if ( ++segment_next_head == SEGMENT_BUFFER_SIZE ) { segment_next_head = 0; } - SPINDLE_ENABLE_PORT ^= 1<step_events_remaining/st_prep_data->step_per_mm; - if (st_prep_data->step_events_remaining < st_prep_data->decelerate_after) { *is_decelerating = true; } - else { *is_decelerating = false; } - - // Flag for new prep_block when st_prep_buffer() is called after the planner recomputes. - pl_partial_block_flag = true; - pl_prep_block = NULL; - } - return; -} diff --git a/archive/stepper_time_archive.c b/archive/stepper_time_archive.c deleted file mode 100644 index dac918f..0000000 --- a/archive/stepper_time_archive.c +++ /dev/null @@ -1,823 +0,0 @@ -/* - stepper.c - stepper motor driver: executes motion plans using stepper motors - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" -#include "nuts_bolts.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) - -#define RAMP_ACCEL 0 -#define RAMP_CRUISE 1 -#define RAMP_DECEL 2 - -#define LOAD_NOOP 0 -#define LOAD_SEGMENT 1 -#define LOAD_BLOCK 2 - -#define SEGMENT_NOOP 0 -#define SEGMENT_END_OF_BLOCK bit(0) -#define RAMP_CHANGE_ACCEL bit(1) -#define RAMP_CHANGE_DECEL bit(2) - -#define SEGMENT_BUFFER_SIZE 6 - -#define DT_SEGMENT (1.0/(ACCELERATION_TICKS_PER_SECOND*60.0)) - -// Stores the planner block Bresenham algorithm execution data for the segments in the segment -// buffer. Normally, this buffer is partially in-use, but, for the worst case scenario, it will -// never exceed the number of accessible stepper buffer segments (SEGMENT_BUFFER_SIZE-1). -// NOTE: This data is copied from the prepped planner blocks so that the planner blocks may be -// discarded when entirely consumed and completed by the segment buffer. -typedef struct { - uint8_t direction_bits; - int32_t steps[N_AXIS]; - int32_t step_event_count; -} st_block_t; -static st_block_t st_block_buffer[SEGMENT_BUFFER_SIZE-1]; -// TODO: Directly adjust this parameters to stop motion of individual axes for the homing cycle. -// But this may require this to be volatile if it is controlled by an interrupt. - -// Primary stepper segment ring buffer. Contains small, short line segments for the stepper -// algorithm to execute, which are "checked-out" incrementally from the first block in the -// planner buffer. Once "checked-out", the steps in the segments buffer cannot be modified by -// the planner, where the remaining planner block steps still can. -typedef struct { - uint8_t n_step; // Number of step events to be executed for this segment - uint8_t st_block_index; // Stepper block data index. Uses this information to execute this segment. - int32_t phase_dist; - int32_t dist_per_tick; -} segment_t; -static segment_t segment_buffer[SEGMENT_BUFFER_SIZE]; - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - - // Used by inverse time algorithm to track step rate - int32_t counter_dist; // Inverse time distance traveled since last step event - - // Used by the stepper driver interrupt - uint8_t execute_step; // Flags step execution for each interrupt. - uint8_t step_pulse_time; // Step pulse reset time after step rise - uint8_t out_bits; // The next stepping-bits to be output - - uint8_t step_count; // Steps remaining in line segment motion - uint8_t exec_block_index; // Tracks the current st_block index. Change indicates new block. - st_block_t *exec_block; // Pointer to the block data for the segment being executed - segment_t *exec_segment; // Pointer to the segment being executed -} stepper_t; -static stepper_t st; - -// Step segment ring buffer indices -static volatile uint8_t segment_buffer_tail; -static volatile uint8_t segment_buffer_head; -static uint8_t segment_next_head; - -static volatile uint8_t busy; // Used to avoid ISR nesting of the "Stepper Driver Interrupt". Should never occur though. - -// Pointers for the step segment being prepped from the planner buffer. Accessed only by the -// main program. Pointers may be planning segments or planner blocks ahead of what being executed. -static plan_block_t *pl_block; // Pointer to the planner block being prepped -static st_block_t *st_prep_block; // Pointer to the stepper block data being prepped - -typedef struct { - uint8_t st_block_index; // Index of stepper common data block being prepped - uint8_t partial_block_flag; // Flag indicating the planner has modified the prepped planner block - - float step_per_mm; - float step_events_remaining; // Tracks step event count for the executing planner block -// int32_t step_events_remaining; - float step_remainder; - - uint8_t ramp_type; - float current_speed; - float maximum_speed; - float exit_speed; - float accelerate_until; - float decelerate_after; -} st_prep_t; -static st_prep_t prep; - - -/* __________________________ - /| |\ _________________ ^ - / | | \ /| |\ | - / | | \ / | | \ s - / | | | | | \ p - / | | | | | \ e - +-----+------------------------+---+--+---------------+----+ e - | BLOCK 1 | BLOCK 2 | d - - time -----> - - The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta - until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after - after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as - +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. -*/ - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - - // Enable stepper driver interrupt - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<n_step; - - // If the new segment starts a new planner block, initialize stepper variables and counters. - // NOTE: When the segment data index changes, this indicates a new planner block. - if ( st.exec_block_index != st.exec_segment->st_block_index ) { - st.exec_block_index = st.exec_segment->st_block_index; - st.exec_block = &st_block_buffer[st.exec_block_index]; - - // Initialize direction bits for block. Set execute flag to set directions bits upon next ISR tick. - st.out_bits = st.exec_block->direction_bits ^ settings.invert_mask; - st.execute_step = true; - - // Initialize Bresenham line counters - st.counter_x = (st.exec_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - - // Initialize inverse time, step rate data, and acceleration ramp counters - st.counter_dist = INV_TIME_MULTIPLIER; // dist_per_step always greater than dist_per_tick. - } - - } else { - // Segment buffer empty. Shutdown. - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - - } - - // Iterate inverse time counter. Triggers each Bresenham step event. - st.counter_dist -= st.exec_segment->dist_per_tick; - - // Execute Bresenham step event, when it's time to do so. - if (st.counter_dist < 0) { - if (st.step_count != 0) { // Block phase correction from executing step. - st.counter_dist += INV_TIME_MULTIPLIER; // Reload inverse time counter - st.step_count--; // Decrement step events count - - // Execute step displacement profile by Bresenham line algorithm - st.execute_step = true; - st.out_bits = st.exec_block->direction_bits; // Reset out_bits and reload direction bits - st.counter_x -= st.exec_block->steps[X_AXIS]; - if (st.counter_x < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<phase_dist > st.counter_dist) { - // Segment is complete. Discard current segment and advance segment indexing. - st.exec_segment = NULL; - if ( ++segment_buffer_tail == SEGMENT_BUFFER_SIZE) { segment_buffer_tail = 0; } - } - } - - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<step_events_remaining); -// st.ramp_type = RAMP_ACCEL; -// st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; -// st.ramp_rate = 0; -// sys.state = STATE_QUEUED; -// } else { -// sys.state = STATE_IDLE; -// } - sys.state = STATE_IDLE; - -} - - -/* Prepares step segment buffer. Continuously called from main program. - - The segment buffer is an intermediary buffer interface between the execution of steps - by the stepper algorithm and the velocity profiles generated by the planner. The stepper - algorithm only executes steps within the segment buffer and is filled by the main program - when steps are "checked-out" from the first block in the planner buffer. This keeps the - step execution and planning optimization processes atomic and protected from each other. - The number of steps "checked-out" from the planner buffer and the number of segments in - the segment buffer is sized and computed such that no operation in the main program takes - longer than the time it takes the stepper algorithm to empty it before refilling it. - Currently, the segment buffer conservatively holds roughly up to 40-60 msec of steps. - - NOTE: The segment buffer executes a set number of steps over an approximate time period. - If we try to execute over a fixed time period, it is difficult to guarantee or predict - how many steps will execute over it, especially when the step pulse phasing between the - neighboring segments must also be kept consistent. Meaning that, if the last segment step - pulses right before a segment end, the next segment must delay its first pulse so that the - step pulses are consistently spaced apart over time to keep the step pulse train nice and - smooth. Keeping track of phasing and ensuring that the exact number of steps are executed - as defined by the planner block, the related computational overhead can get quickly and - prohibitively expensive, especially in real-time. - Since the stepper algorithm automatically takes care of the step pulse phasing with - its ramp and inverse time counters by retaining the count remainders, we don't have to - explicitly and expensively track and synchronize the exact number of steps, time, and - phasing of steps. All we need to do is approximate the number of steps in each segment - such that the segment buffer has enough execution time for the main program to do what - it needs to do and refill it when it comes back. In other words, we just need to compute - a cheap approximation of the current velocity and the number of steps over it. -*/ - -/* - TODO: Figure out how to enforce a deceleration when a feedrate override is reduced. - The problem is that when an override is reduced, the planner may not plan back to - the current rate. Meaning that the velocity profiles for certain conditions no longer - are trapezoidal or triangular. For example, if the current block is cruising at a - nominal rate and the feedrate override is reduced, the new nominal rate will now be - lower. The velocity profile must first decelerate to the new nominal rate and then - follow on the new plan. So the remaining velocity profile will have a decelerate, - cruise, and another decelerate. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already - heavily decelerating throughout it, reducing the feedrate will not do much to it. So, - how do we determine when to resume the new plan? How many blocks do we have to wait - until the new plan intersects with the deceleration curve? One plus though, the - deceleration will never be more than the number of blocks in the entire planner buffer, - but it theoretically can be equal to it when all planner blocks are decelerating already. -*/ -void st_prep_buffer() -{ - if (sys.state == STATE_QUEUED) { return; } // Block until a motion state is issued - while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. - - - // ----------------------------------------------------------------------------------- - // Determine if we need to load a new planner block. If so, prepare step data. - if (pl_block == NULL) { - pl_block = plan_get_current_block(); // Query planner for a queued block - if (pl_block == NULL) { return; } // No planner blocks. Exit. - -// SPINDLE_ENABLE_PORT ^= 1<steps[X_AXIS] = pl_block->steps[X_AXIS]; - st_prep_block->steps[Y_AXIS] = pl_block->steps[Y_AXIS]; - st_prep_block->steps[Z_AXIS] = pl_block->steps[Z_AXIS]; - st_prep_block->direction_bits = pl_block->direction_bits; - st_prep_block->step_event_count = pl_block->step_event_count; - - // Initialize planner block step count, unit distance data, and remainder tracker. - prep.step_per_mm = ((float)st_prep_block->step_event_count)/pl_block->millimeters; - prep.step_events_remaining = st_prep_block->step_event_count; - prep.step_remainder = 0.0; - } - - // Compute the prepped planner block velocity profile to be traced by stepper algorithm. - prep.current_speed = sqrt(pl_block->entry_speed_sqr); - prep.exit_speed = plan_get_exec_block_exit_speed(); - - // Determine velocity profile based on the 7 possible types: Cruise-only, cruise-deceleration, - // acceleration-cruise, acceleration-only, deceleration-only, full-trapezoid, and triangle. - prep.ramp_type = RAMP_ACCEL; - float exit_speed_sqr = prep.exit_speed*prep.exit_speed; - float inv_2_accel = 0.5/pl_block->acceleration; - float intersection_dist = - 0.5*(pl_block->millimeters+inv_2_accel*(pl_block->entry_speed_sqr-exit_speed_sqr)); - if (intersection_dist > 0.0) { - if (intersection_dist < pl_block->millimeters) { // Either trapezoid or triangle types - // NOTE: For acceleration-cruise trapezoid, following calculation will be 0.0. - prep.decelerate_after = inv_2_accel*(pl_block->nominal_speed_sqr-exit_speed_sqr); - if (prep.decelerate_after < intersection_dist) { // Trapezoid type - prep.maximum_speed = sqrt(pl_block->nominal_speed_sqr); - if (pl_block->entry_speed_sqr == pl_block->nominal_speed_sqr) { - // Cruise-deceleration or cruise-only type. - prep.ramp_type = RAMP_CRUISE; - prep.accelerate_until = pl_block->millimeters; - } else { - // Full-trapezoid or acceleration-cruise types - prep.accelerate_until = - pl_block->millimeters-inv_2_accel*(pl_block->nominal_speed_sqr-pl_block->entry_speed_sqr); - } - } else { // Triangle type - prep.accelerate_until = intersection_dist; - prep.decelerate_after = intersection_dist; - prep.maximum_speed = sqrt(2.0*pl_block->acceleration*intersection_dist+exit_speed_sqr); - } - } else { // Deceleration-only type - prep.ramp_type = RAMP_DECEL; - prep.maximum_speed = prep.current_speed; - prep.accelerate_until = pl_block->millimeters; - prep.decelerate_after = pl_block->millimeters; - } - } else { // Acceleration-only type - prep.maximum_speed = prep.exit_speed; - prep.accelerate_until = 0.0; - prep.decelerate_after = 0.0; - } - - } - - // Initialize new segment - segment_t *prep_segment = &segment_buffer[segment_buffer_head]; - - // Set new segment to point to the current segment data block. - prep_segment->st_block_index = prep.st_block_index; - - /* ----------------------------------------------------------------------------------- - Compute the average velocity of this new segment by determining the total distance - traveled over the segment time DT_SEGMENT. This section attempts to create a full - segment based on the current ramp conditions. If the segment is incomplete and - terminates upon a ramp change, the next section will attempt to fill the remaining - segment execution time. However, if an incomplete segment terminates at the end of - the planner block, the segment execution time is less than DT_SEGMENT and the new - segment will execute over this truncated execution time. - */ - float dt = 0.0; - float mm_remaining = pl_block->millimeters; - float dt_var = DT_SEGMENT; - float mm_var; - do { - switch (prep.ramp_type) { - case RAMP_ACCEL: - // NOTE: Acceleration ramp always computes during first loop only. - mm_remaining -= DT_SEGMENT*(prep.current_speed + pl_block->acceleration*(0.5*DT_SEGMENT)); - if (mm_remaining < prep.accelerate_until) { // End of acceleration ramp. - // Acceleration-cruise, acceleration-deceleration ramp junction, or end of block. - mm_remaining = prep.accelerate_until; // NOTE: 0.0 at EOB - dt_var = 2.0*(pl_block->millimeters-mm_remaining)/(prep.current_speed+prep.maximum_speed); - if (mm_remaining == prep.decelerate_after) { prep.ramp_type = RAMP_DECEL; } - else { prep.ramp_type = RAMP_CRUISE; } - prep.current_speed = prep.maximum_speed; - } else { // Acceleration only. - prep.current_speed += pl_block->acceleration*dt_var; - } - break; - case RAMP_CRUISE: - // NOTE: mm_var used to retain the last mm_remaining for incomplete segment dt_var calculations. - mm_var = mm_remaining - prep.maximum_speed*dt_var; - if (mm_var < prep.decelerate_after) { // End of cruise. - // Cruise-deceleration junction or end of block. - dt_var = (mm_remaining - prep.decelerate_after)/prep.maximum_speed; - mm_remaining = prep.decelerate_after; // NOTE: 0.0 at EOB - prep.ramp_type = RAMP_DECEL; - } else { // Cruising only. - mm_remaining = mm_var; - } - break; - default: // case RAMP_DECEL: - // NOTE: mm_var used to catch negative decelerate distance values near zero speed. - mm_var = dt_var*(prep.current_speed - 0.5*pl_block->acceleration*dt_var); - if ((mm_var > 0.0) && (mm_var < pl_block->millimeters)) { // Deceleration only. - prep.current_speed -= pl_block->acceleration*dt_var; - // Check for near-zero speed and prevent divide by zero in rare scenarios. - if (prep.current_speed <= prep.exit_speed) { mm_remaining = 0.0; } - else { mm_remaining -= mm_var; } - } else { // End of block. - dt_var = 2.0*mm_remaining/(prep.current_speed+prep.exit_speed); - mm_remaining = 0.0; - // prep.current_speed = prep.exit_speed; - } - } - dt += dt_var; - if (dt < DT_SEGMENT) { dt_var = DT_SEGMENT - dt; } // **Incomplete** At ramp junction. - else { break; } // **Complete** Exit loop. Segment execution time maxed. - } while ( mm_remaining > 0.0 ); // **Complete** Exit loop. End of planner block. - - /* - float mm_remaining; - float dt = DT_SEGMENT; - if (pl_block->millimeters > prep.accelerate_until) { // [Acceleration Ramp] - mm_remaining = pl_block->millimeters - DT_SEGMENT*(prep.current_speed + pl_block->acceleration*(0.5*DT_SEGMENT)); - if (mm_remaining < prep.accelerate_until) { // **Incomplete** Acceleration ramp end. - // Acceleration-cruise, acceleration-deceleration ramp junction, or end of block. - mm_remaining = prep.accelerate_until; // NOTE: 0.0 at EOB - dt = 2.0*(pl_block->millimeters-mm_remaining)/(prep.current_speed+prep.maximum_speed); - prep.current_speed = prep.maximum_speed; - } else { // **Complete** Acceleration only. - prep.current_speed += pl_block->acceleration*DT_SEGMENT; - prep.current_speed = min(prep.maximum_speed,prep.current_speed); - } - } else if (pl_block->millimeters > prep.decelerate_after) { // [No Ramp. Cruising] - mm_remaining = pl_block->millimeters - prep.maximum_speed*DT_SEGMENT; - if (mm_remaining < prep.decelerate_after) { // **Incomplete** End of cruise. - // Cruise-deceleration junction or end of block. - mm_remaining = prep.decelerate_after; // NOTE: 0.0 at EOB - dt = (pl_block->millimeters-mm_remaining)/prep.maximum_speed; - } // Otherwise **Complete** Cruising only. - } else { // [Deceleration Ramp] - mm_remaining = DT_SEGMENT*(prep.current_speed - 0.5*pl_block->acceleration*DT_SEGMENT); - if ((mm_remaining > 0.0) && (mm_remaining < pl_block->millimeters)) { // **Complete** Deceleration only. - prep.current_speed -= pl_block->acceleration*DT_SEGMENT; - if (prep.current_speed <= prep.exit_speed) { // Round off error fix. Prevents divide by zero. - mm_remaining = 0.0; - } else { - mm_remaining = pl_block->millimeters - mm_remaining; - } - } else { // **Complete** End of block. - mm_remaining = 0.0; - dt = 2.0*pl_block->millimeters/(prep.current_speed+prep.exit_speed); - // prep.current_speed = prep.exit_speed; - } - } - - - /* ----------------------------------------------------------------------------------- - If segment is incomplete, attempt to fill the remaining segment execution time. - NOTE: Segment remainder always spans a cruise and/or a deceleration ramp. - - float partial_mm, dt_remainder; - if ((dt < DT_SEGMENT) && (mm_remaining > 0.0)) { - dt_remainder = DT_SEGMENT-dt; - - // Attempt to fill incomplete segment with cruising profile. - if (mm_remaining > prep.decelerate_after) { // Cruising profile - partial_mm = mm_remaining - prep.current_speed*dt_remainder; - if (partial_mm < prep.decelerate_after) { // **Incomplete** - dt += (mm_remaining-prep.decelerate_after)/prep.maximum_speed; - mm_remaining = prep.decelerate_after; - // current_speed = maximum_speed; - } else { // **Complete** Segment filled. - mm_remaining = partial_mm; - dt = DT_SEGMENT; - } - } - - // Attempt to fill incomplete segment with deceleration ramp. - if ((dt < DT_SEGMENT) && (mm_remaining > 0.0)) { - if (mm_remaining <= prep.decelerate_after) { // Deceleration ramp - dt_remainder = DT_SEGMENT-dt; - partial_mm = dt_remainder*(prep.current_speed-0.5*pl_block->acceleration*dt_remainder); - if ((partial_mm > 0.0) && (mm_remaining > partial_mm)) { // **Complete** Segment filled. - prep.current_speed -= pl_block->acceleration*dt_remainder; - if (prep.current_speed <= prep.exit_speed) { - mm_remaining = 0.0; - - } else { - mm_remaining -= partial_mm; - dt = DT_SEGMENT; - } - } else { // **Complete** End of block. - dt += (2.0*mm_remaining/(prep.current_speed+prep.exit_speed)); - mm_remaining = 0.0; - // prep.current_speed = prep.exit_speed; - } - } - } - } - */ -// printString(" Z"); -// printFloat(dt*(60.0*1000.0)); -// printString(" "); -// printFloat(mm_remaining); -// printString(" "); -// printFloat(prep.current_speed); -// printString("Z "); - - /* ----------------------------------------------------------------------------------- - Compute segment step rate, steps to execute, and step phase correction parameters. - */ -// float step_events; -// if (mm_remaining > 0.0) { -// step_events = prep.step_per_mm*(pl_block->millimeters - mm_remaining); // Convert mm to steps -// prep_segment->n_step = floor(step_events + prep.step_remainder); -// if (prep_segment->n_step > prep.step_events_remaining) { // Prevent round-off overshoot -// prep_segment->n_step = prep.step_events_remaining; -// } -// } else { // Ensure all remaining steps are executed -// step_events = prep.step_per_mm*pl_block->millimeters; -// prep_segment->n_step = prep.step_events_remaining; -// } -// prep.step_events_remaining -= prep_segment->n_step; -// -// // Compute segment rate. -// prep_segment->dist_per_tick = -// ceil( (INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND)) * (step_events/dt) ); // (mult*step/isr_tic) -// -// if (prep.step_events_remaining > 0) { -// // Compute step phase distance and update segment continuation parameters. -// prep.step_remainder += step_events - prep_segment->n_step; -// prep_segment->phase_dist = ceil(INV_TIME_MULTIPLIER-INV_TIME_MULTIPLIER*prep.step_remainder); -// pl_block->millimeters = mm_remaining; -// pl_block->entry_speed_sqr = prep.current_speed*prep.current_speed; -// -// } else { // End of block. Finish it out. -// // The planner block is complete. All steps are set to be executed in the segment buffer. -// // Move planner pointer to next block and flag to load a new block for the next segment. -// prep_segment->phase_dist = INV_TIME_MULTIPLIER; -// pl_block = NULL; -// plan_discard_current_block(); -// } - - if (mm_remaining > 0.0) { - - float steps_remaining = prep.step_per_mm*mm_remaining; - prep_segment->dist_per_tick = ceil( (INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))* - ((prep.step_events_remaining-steps_remaining)/dt) ); // (mult*step/isr_tic) - - // Compute number of steps to execute and segment step phase correction. - prep_segment->n_step = ceil(prep.step_events_remaining)-ceil(steps_remaining); - prep_segment->phase_dist = ceil(INV_TIME_MULTIPLIER*(1.0-ceil(steps_remaining)+steps_remaining)); - - // Update step execution variables - prep.step_events_remaining = steps_remaining; - pl_block->millimeters = mm_remaining; - pl_block->entry_speed_sqr = prep.current_speed*prep.current_speed; - - } else { // End of block. Finish it out. - - prep_segment->dist_per_tick = ceil( (INV_TIME_MULTIPLIER/(60.0*ISR_TICKS_PER_SECOND))* - prep.step_events_remaining/dt ); // (mult*step/isr_tic) - prep_segment->phase_dist = INV_TIME_MULTIPLIER; - - // Set to execute the remaining steps and no phase correction upon finishing the block. - prep_segment->n_step = ceil(prep.step_events_remaining); - - - // NOTE: Not required. Planner will ignore this block as it is now complete. - // prep.step_events_remaining = 0.0; - // pl_block->millimeters = 0.0; - - // The planner block is complete. All steps are set to be executed in the segment buffer. - // Move planner pointer to next block and flag to load a new block for the next segment. - pl_block = NULL; - plan_discard_current_block(); - } - -// long a = prep_segment->n_step; -// printInteger(a); -// printString(" "); -// a = prep_segment->phase_dist; -// printInteger(prep_segment->dist_per_tick); -// printString(" "); -// printFloat(prep.step_events_remaining); -// printString(" "); -// printFloat(pl_block->millimeters); -// printString(" "); - - - // !!! PROBLEM. Step events remaining in floating point can limit the number of steps - // we can accurately track, since floats have ~7.2 significant digits. However, this only - // becomes a problem if there are more than 1,000,000, which translates to a CNC machine - // with 200 step/mm and 5 meters of axis travel. Possible but unlikely. Could have more - // issues with user setting up their machine with too high of steps. - - // TODO: dist_per_tick must be less than INV_TIME_MULTIPLIER. A check can be made to - // make this a hard limit. Need to make sure this doesn't affect the velocity profiles.. - // it shouldn't. The same could said for the minimum allowable step rate too. This should - // not affect the tracing of the profiles either. - - // Ensure the initial step rate exceeds the MINIMUM_STEP_RATE. - // TODO: Use config.h error checking to do this. Otherwise, counters get screwy. - - // New step segment initialization completed. Increment segment buffer indices. - segment_buffer_head = segment_next_head; - if ( ++segment_next_head == SEGMENT_BUFFER_SIZE ) { segment_next_head = 0; } - -int32_t blength = segment_buffer_head - segment_buffer_tail; -if (blength < 0) { blength += SEGMENT_BUFFER_SIZE; } -printInteger(blength); -// SPINDLE_ENABLE_PORT ^= 1<. -*/ - -/* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith - and Philipp Tiefenbacher. */ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) -#define CRUISE_RAMP 0 -#define ACCEL_RAMP 1 -#define DECEL_RAMP 2 - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - int32_t event_count; // Total event count. Retained for feed holds. - int32_t step_events_remaining; // Steps remaining in motion - - // Used by Pramod Ranade inverse time algorithm - int32_t delta_d; // Ranade distance traveled per interrupt tick - int32_t d_counter; // Ranade distance traveled since last step event - uint8_t ramp_count; // Acceleration interrupt tick counter. - uint8_t ramp_type; // Ramp type variable. - uint8_t execute_step; // Flags step execution for each interrupt. - -} stepper_t; -static stepper_t st; -static block_t *current_block; // A pointer to the block currently being traced - -// Used by the stepper driver interrupt -static uint8_t step_pulse_time; // Step pulse reset time after step rise -static uint8_t out_bits; // The next stepping-bits to be output - -// NOTE: If the main interrupt is guaranteed to be complete before the next interrupt, then -// this blocking variable is no longer needed. Only here for safety reasons. -static volatile uint8_t busy; // True when "Stepper Driver Interrupt" is being serviced. Used to avoid retriggering that handler. - -// __________________________ -// /| |\ _________________ ^ -// / | | \ /| |\ | -// / | | \ / | | \ s -// / | | | | | \ p -// / | | | | | \ e -// +-----+------------------------+---+--+---------------+----+ e -// | BLOCK 1 | BLOCK 2 | d -// -// time -----> -// -// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta -// until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after -// after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as -// +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. - - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - // Enable stepper driver interrupt - st.execute_step = false; - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<direction_bits ^ settings.invert_mask; - st.execute_step = true; // Set flag to set direction bits. - - // Initialize Bresenham variables - st.counter_x = (current_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - st.event_count = current_block->step_event_count; - st.step_events_remaining = st.event_count; - - // During feed hold, do not update Ranade counter, rate, or ramp type. Keep decelerating. - if (sys.state == STATE_CYCLE) { - // Initialize Ranade variables - st.d_counter = current_block->d_next; - st.delta_d = current_block->initial_rate; - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; - - // Initialize ramp type. - if (st.step_events_remaining == current_block->decelerate_after) { st.ramp_type = DECEL_RAMP; } - else if (st.delta_d == current_block->nominal_rate) { st.ramp_type = CRUISE_RAMP; } - else { st.ramp_type = ACCEL_RAMP; } - } - - } else { - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - } - - // Adjust inverse time counter for ac/de-celerations - if (st.ramp_type) { - // Tick acceleration ramp counter - st.ramp_count--; - if (st.ramp_count == 0) { - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK; // Reload ramp counter - if (st.ramp_type == ACCEL_RAMP) { // Adjust velocity for acceleration - st.delta_d += current_block->rate_delta; - if (st.delta_d >= current_block->nominal_rate) { // Reached cruise state. - st.ramp_type = CRUISE_RAMP; - st.delta_d = current_block->nominal_rate; // Set cruise velocity - } - } else if (st.ramp_type == DECEL_RAMP) { // Adjust velocity for deceleration - if (st.delta_d > current_block->rate_delta) { - st.delta_d -= current_block->rate_delta; - } else { - st.delta_d >>= 1; // Integer divide by 2 until complete. Also prevents overflow. - } - } - } - } - - // Iterate Pramod Ranade inverse time counter. Triggers each Bresenham step event. - if (st.delta_d < MINIMUM_STEP_RATE) { st.d_counter -= MINIMUM_STEP_RATE; } - else { st.d_counter -= st.delta_d; } - - // Execute Bresenham step event, when it's time to do so. - if (st.d_counter < 0) { - st.d_counter += current_block->d_next; - - // Check for feed hold state and execute accordingly. - if (sys.state == STATE_HOLD) { - if (st.ramp_type != DECEL_RAMP) { - st.ramp_type = DECEL_RAMP; - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; - } - if (st.delta_d <= current_block->rate_delta) { - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); - return; - } - } - - // TODO: Vary Bresenham resolution for smoother motions or enable faster step rates (>20kHz). - - out_bits = current_block->direction_bits; // Reset out_bits and reload direction bits - st.execute_step = true; - - // Execute step displacement profile by Bresenham line algorithm - st.counter_x -= current_block->steps[X_AXIS]; - if (st.counter_x < 0) { - out_bits |= (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - out_bits |= (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - out_bits |= (1<decelerate_after) { - st.ramp_type = DECEL_RAMP; - if (st.step_events_remaining == current_block->decelerate_after) { - if (st.delta_d == current_block->nominal_rate) { - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK/2; // Set ramp counter for trapezoid - } else { - st.ramp_count = ISR_TICKS_PER_ACCELERATION_TICK-st.ramp_count; // Set ramp counter for triangle - } - } - } - } - } else { - // If current block is finished, reset pointer - current_block = NULL; - plan_discard_current_block(); - } - - out_bits ^= settings.invert_mask; // Apply step port invert mask - } - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<acceleration = min(block->acceleration,settings.acceleration[idx]*inverse_unit_vec_value); // Incrementally compute cosine of angle between previous and current path. Cos(theta) of the junction @@ -417,7 +417,7 @@ void plan_sync_position() void plan_cycle_reinitialize() { // Re-plan from a complete stop. Reset planner entry speeds and buffer planned pointer. -// st_update_plan_block_parameters(); + st_update_plan_block_parameters(); block_buffer_planned = block_buffer_tail; planner_recalculate(); } diff --git a/planner.h b/planner.h index a1335aa..dc96830 100644 --- a/planner.h +++ b/planner.h @@ -28,25 +28,24 @@ #define BLOCK_BUFFER_SIZE 18 #endif -// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in -// the source g-code and may never actually be reached if acceleration management is active. +// This struct stores a linear movement of a g-code block motion with its critical "nominal" values +// are as specified in the source g-code. typedef struct { - // Fields used by the bresenham algorithm for tracing the line - // NOTE: Do not change any of these values once set. The stepper algorithm uses them to execute the block correctly. - uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) - int32_t steps[N_AXIS]; // Step count along each axis - int32_t step_event_count; // The maximum step axis count and number of steps required to complete this block. + // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values. + uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) + int32_t steps[N_AXIS]; // Step count along each axis + int32_t step_event_count; // The maximum step axis count and number of steps required to complete this block. // Fields used by the motion planner to manage acceleration - float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2 - float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and - // neighboring nominal speeds with overrides in (mm/min)^2 - float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2 - float nominal_speed_sqr; // Axis-limit adjusted nominal speed for this block in (mm/min)^2 - float acceleration; // Axis-limit adjusted line acceleration in (mm/min^2) - float millimeters; // The remaining distance for this block to be executed in (mm) - // uint8_t max_override; // Maximum override value based on axis speed limits + float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2 + float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and + // neighboring nominal speeds with overrides in (mm/min)^2 + float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2 + float nominal_speed_sqr; // Axis-limit adjusted nominal speed for this block in (mm/min)^2 + float acceleration; // Axis-limit adjusted line acceleration in (mm/min^2) + float millimeters; // The remaining distance for this block to be executed in (mm) + // uint8_t max_override; // Maximum override value based on axis speed limits } plan_block_t; diff --git a/report.c b/report.c index e71ac54..101736a 100644 --- a/report.c +++ b/report.c @@ -151,10 +151,10 @@ void report_grbl_settings() { printPgmString(PSTR("$0=")); printFloat(settings.steps_per_mm[X_AXIS]); printPgmString(PSTR(" (x, step/mm)\r\n$1=")); printFloat(settings.steps_per_mm[Y_AXIS]); printPgmString(PSTR(" (y, step/mm)\r\n$2=")); printFloat(settings.steps_per_mm[Z_AXIS]); - printPgmString(PSTR(" (z, step/mm)\r\n$3=")); printFloat(settings.max_velocity[X_AXIS]); - printPgmString(PSTR(" (x v_max, mm/min)\r\n$4=")); printFloat(settings.max_velocity[Y_AXIS]); - printPgmString(PSTR(" (y v_max, mm/min)\r\n$5=")); printFloat(settings.max_velocity[Z_AXIS]); - printPgmString(PSTR(" (z v_max, mm/min)\r\n$6=")); printFloat(settings.acceleration[X_AXIS]/(60*60)); // Convert from mm/min^2 for human readability + printPgmString(PSTR(" (z, step/mm)\r\n$3=")); printFloat(settings.max_rate[X_AXIS]); + printPgmString(PSTR(" (x max rate, mm/min)\r\n$4=")); printFloat(settings.max_rate[Y_AXIS]); + printPgmString(PSTR(" (y max rate, mm/min)\r\n$5=")); printFloat(settings.max_rate[Z_AXIS]); + printPgmString(PSTR(" (z max rate, mm/min)\r\n$6=")); printFloat(settings.acceleration[X_AXIS]/(60*60)); // Convert from mm/min^2 for human readability printPgmString(PSTR(" (x accel, mm/sec^2)\r\n$7=")); printFloat(settings.acceleration[Y_AXIS]/(60*60)); // Convert from mm/min^2 for human readability printPgmString(PSTR(" (y accel, mm/sec^2)\r\n$8=")); printFloat(settings.acceleration[Z_AXIS]/(60*60)); // Convert from mm/min^2 for human readability printPgmString(PSTR(" (z accel, mm/sec^2)\r\n$9=")); printFloat(-settings.max_travel[X_AXIS]); // Grbl internally store this as negative. diff --git a/serial.c b/serial.c index d3325c7..c07e64b 100644 --- a/serial.c +++ b/serial.c @@ -30,7 +30,7 @@ uint8_t rx_buffer[RX_BUFFER_SIZE]; uint8_t rx_buffer_head = 0; -uint8_t rx_buffer_tail = 0; +volatile uint8_t rx_buffer_tail = 0; uint8_t tx_buffer[TX_BUFFER_SIZE]; uint8_t tx_buffer_head = 0; @@ -93,8 +93,7 @@ void serial_write(uint8_t data) { // Data Register Empty Interrupt handler ISR(SERIAL_UDRE) { - // Temporary tx_buffer_tail (to optimize for volatile) - uint8_t tail = tx_buffer_tail; + uint8_t tail = tx_buffer_tail; // Temporary tx_buffer_tail (to optimize for volatile) #ifdef ENABLE_XONXOFF if (flow_ctrl == SEND_XOFF) { @@ -122,12 +121,15 @@ ISR(SERIAL_UDRE) uint8_t serial_read() { - if (rx_buffer_head == rx_buffer_tail) { + uint8_t tail = rx_buffer_tail; // Temporary rx_buffer_tail (to optimize for volatile) + if (rx_buffer_head == tail) { return SERIAL_NO_DATA; } else { - uint8_t data = rx_buffer[rx_buffer_tail]; - rx_buffer_tail++; - if (rx_buffer_tail == RX_BUFFER_SIZE) { rx_buffer_tail = 0; } + uint8_t data = rx_buffer[tail]; + + tail++; + if (tail == RX_BUFFER_SIZE) { tail = 0; } + rx_buffer_tail = tail; #ifdef ENABLE_XONXOFF if ((get_rx_buffer_count() < RX_BUFFER_LOW) && flow_ctrl == XOFF_SENT) { diff --git a/settings.c b/settings.c index 8f628a5..0ffcc76 100644 --- a/settings.c +++ b/settings.c @@ -73,12 +73,12 @@ void settings_reset(bool reset_all) { settings.steps_per_mm[Z_AXIS] = DEFAULT_Z_STEPS_PER_MM; settings.pulse_microseconds = DEFAULT_STEP_PULSE_MICROSECONDS; settings.default_feed_rate = DEFAULT_FEEDRATE; - settings.max_velocity[X_AXIS] = DEFAULT_RAPID_FEEDRATE; - settings.max_velocity[Y_AXIS] = DEFAULT_RAPID_FEEDRATE; - settings.max_velocity[Z_AXIS] = DEFAULT_RAPID_FEEDRATE; - settings.acceleration[X_AXIS] = DEFAULT_ACCELERATION; - settings.acceleration[Y_AXIS] = DEFAULT_ACCELERATION; - settings.acceleration[Z_AXIS] = DEFAULT_ACCELERATION; + settings.max_rate[X_AXIS] = DEFAULT_X_MAX_RATE; + settings.max_rate[Y_AXIS] = DEFAULT_Y_MAX_RATE; + settings.max_rate[Z_AXIS] = DEFAULT_Z_MAX_RATE; + settings.acceleration[X_AXIS] = DEFAULT_X_ACCELERATION; + settings.acceleration[Y_AXIS] = DEFAULT_Y_ACCELERATION; + settings.acceleration[Z_AXIS] = DEFAULT_Z_ACCELERATION; settings.arc_tolerance = DEFAULT_ARC_TOLERANCE; settings.invert_mask = DEFAULT_STEPPING_INVERT_MASK; settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION; @@ -92,8 +92,8 @@ void settings_reset(bool reset_all) { if (DEFAULT_HARD_LIMIT_ENABLE) { settings.flags |= BITFLAG_HARD_LIMIT_ENABLE; } if (DEFAULT_HOMING_ENABLE) { settings.flags |= BITFLAG_HOMING_ENABLE; } settings.homing_dir_mask = DEFAULT_HOMING_DIR_MASK; - settings.homing_feed_rate = DEFAULT_HOMING_FEEDRATE; - settings.homing_seek_rate = DEFAULT_HOMING_RAPID_FEEDRATE; + settings.homing_feed_rate = DEFAULT_HOMING_FEED_RATE; + settings.homing_seek_rate = DEFAULT_HOMING_SEEK_RATE; settings.homing_debounce_delay = DEFAULT_HOMING_DEBOUNCE_DELAY; settings.homing_pulloff = DEFAULT_HOMING_PULLOFF; settings.stepper_idle_lock_time = DEFAULT_STEPPER_IDLE_LOCK_TIME; @@ -163,9 +163,9 @@ uint8_t settings_store_global_setting(int parameter, float value) { case 0: case 1: case 2: if (value <= 0.0) { return(STATUS_SETTING_VALUE_NEG); } settings.steps_per_mm[parameter] = value; break; - case 3: settings.max_velocity[X_AXIS] = value; break; - case 4: settings.max_velocity[Y_AXIS] = value; break; - case 5: settings.max_velocity[Z_AXIS] = value; break; + case 3: settings.max_rate[X_AXIS] = value; break; + case 4: settings.max_rate[Y_AXIS] = value; break; + case 5: settings.max_rate[Z_AXIS] = value; break; case 6: settings.acceleration[X_AXIS] = value*60*60; break; // Convert to mm/min^2 for grbl internal use. case 7: settings.acceleration[Y_AXIS] = value*60*60; break; // Convert to mm/min^2 for grbl internal use. case 8: settings.acceleration[Z_AXIS] = value*60*60; break; // Convert to mm/min^2 for grbl internal use. diff --git a/settings.h b/settings.h index 3369845..6b7d36f 100644 --- a/settings.h +++ b/settings.h @@ -25,11 +25,11 @@ #include #include "nuts_bolts.h" -#define GRBL_VERSION "0.9a" +#define GRBL_VERSION "0.9b" // Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl // when firmware is upgraded. Always stored in byte 0 of eeprom -#define SETTINGS_VERSION 53 +#define SETTINGS_VERSION 55 // Define bit flag masks for the boolean settings in settings.flag. #define BITFLAG_REPORT_INCHES bit(0) @@ -58,24 +58,22 @@ // Global persistent settings (Stored from byte EEPROM_ADDR_GLOBAL onwards) typedef struct { float steps_per_mm[N_AXIS]; - uint8_t microsteps; + float max_rate[N_AXIS]; + float acceleration[N_AXIS]; + float max_travel[N_AXIS]; uint8_t pulse_microseconds; float default_feed_rate; - float default_seek_rate; uint8_t invert_mask; - float arc_tolerance; - float acceleration[N_AXIS]; + uint8_t stepper_idle_lock_time; // If max value 255, steppers do not disable. float junction_deviation; + float arc_tolerance; + uint8_t decimal_places; uint8_t flags; // Contains default boolean settings uint8_t homing_dir_mask; float homing_feed_rate; float homing_seek_rate; uint16_t homing_debounce_delay; float homing_pulloff; - uint8_t stepper_idle_lock_time; // If max value 255, steppers do not disable. - uint8_t decimal_places; - float max_velocity[N_AXIS]; - float max_travel[N_AXIS]; // uint8_t status_report_mask; // Mask to indicate desired report data. } settings_t; extern settings_t settings; diff --git a/stepper.c b/stepper.c index 771480d..c2813a9 100644 --- a/stepper.c +++ b/stepper.c @@ -105,10 +105,9 @@ typedef struct { float step_per_mm; // Current planner block step/millimeter conversion scalar float steps_remaining; - int32_t step_events_remaining; // Tracks step event count for the executing planner block uint8_t ramp_type; // Current segment ramp state - float millimeters_remaining; + float mm_eob; float current_speed; // Current speed at the end of the segment buffer (mm/min) float maximum_speed; // Maximum speed of executing block. Not always nominal speed. (mm/min) float exit_speed; // Exit speed of executing block (mm/min) @@ -217,15 +216,14 @@ void st_go_idle() stepper pins appropriately. It is supported by The Stepper Port Reset Interrupt which it uses to reset the stepper port after each pulse. The bresenham line tracer algorithm controls all three stepper outputs simultaneously with these two interrupts. + + NOTE: This interrupt must be as efficient as possible and complete before the next ISR tick, + which for Grbl is 33.3usec at a 30kHz ISR rate. Oscilloscope measured time in ISR is 5usec + typical and 25usec maximum, well below requirement. */ -/* TODO: - - Measured time in ISR. Typical and worst-case. Roughly 5usec min to 25usec max. Good enough. - There are no major changes to the base operations of this ISR with the new segment buffer. - - Determine if placing the position counters elsewhere (or change them to 8-bit variables that - are added to the system position counters at the end of a segment) frees up cycles. - - Create NOTE: to describe that the total time in this ISR must be less than the ISR frequency - in its worst case scenario. -*/ +// TODO: Replace direct updating of the int32 position counters in the ISR somehow. Perhaps use smaller +// int8 variables and update position counters only when a segment completes. This can get complicated +// with probing and homing cycles that require true real-time positions. ISR(TIMER2_COMPA_vect) { // SPINDLE_ENABLE_PORT ^= 1<millimeters = prep.millimeters_remaining; pl_block->entry_speed_sqr = prep.current_speed*prep.current_speed; // Update entry speed. pl_block = NULL; // Flag st_prep_segment() to load new velocity profile. } @@ -456,32 +453,12 @@ void st_update_plan_block_parameters() accounted for. This allows the stepper algorithm to run at very high step rates without losing steps. */ -/* - TODO: With feedrate overrides, increases to the override value will not significantly - change the planner and stepper current operation. When the value increases, we simply - need to recompute the block plan with new nominal speeds and maximum junction velocities. - However with a decreasing feedrate override, this gets a little tricky. The current block - plan is optimal, so if we try to reduce the feed rates, it may be impossible to create - a feasible plan at its current operating speed and decelerate down to zero at the end of - the buffer. We first have to enforce a deceleration to meet and intersect with the reduced - feedrate override plan. For example, if the current block is cruising at a nominal rate - and the feedrate override is reduced, the new nominal rate will now be lower. The velocity - profile must first decelerate to the new nominal rate and then follow on the new plan. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already heavily - decelerating throughout it, reducing the feedrate override will not do much to it. So, - how do we determine when to resume the new plan? One solution is to tie into the feed hold - handling code to enforce a deceleration, but check when the current speed is less than or - equal to the block maximum speed and is in an acceleration or cruising ramp. At this - point, we know that we can recompute the block velocity profile to meet and continue onto - the new block plan. -*/ void st_prep_buffer() { while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. if (sys.state == STATE_QUEUED) { return; } // Block until a motion state is issued - // Determine if we need to load a new planner block. If so, prepare step data. + // Determine if we need to load a new planner block or if the block remainder is replanned. if (pl_block == NULL) { pl_block = plan_get_current_block(); // Query planner for a queued block if (pl_block == NULL) { return; } // No planner blocks. Exit. @@ -504,10 +481,8 @@ void st_prep_buffer() st_prep_block->step_event_count = pl_block->step_event_count; // Initialize segment buffer data for generating the segments. - prep.step_events_remaining = st_prep_block->step_event_count; prep.steps_remaining = st_prep_block->step_event_count; - prep.millimeters_remaining = pl_block->millimeters; - prep.step_per_mm = prep.steps_remaining/prep.millimeters_remaining; + prep.step_per_mm = prep.steps_remaining/pl_block->millimeters; if (sys.state == STATE_HOLD) { prep.current_speed = prep.exit_speed; @@ -516,28 +491,30 @@ void st_prep_buffer() else { prep.current_speed = sqrt(pl_block->entry_speed_sqr); } } + prep.mm_eob = 0.0; + float inv_2_accel = 0.5/pl_block->acceleration; if (sys.state == STATE_HOLD) { - // Compute velocity profile parameters for a feed hold in-progress. + // Compute velocity profile parameters for a feed hold in-progress. This profile overrides + // the planner block profile, enforcing a deceleration to zero speed. prep.ramp_type = RAMP_DECEL; float decel_dist = inv_2_accel*pl_block->entry_speed_sqr; - if (decel_dist < prep.millimeters_remaining) { + if (decel_dist < pl_block->millimeters) { prep.exit_speed = 0.0; - prep.steps_remaining = prep.step_per_mm*decel_dist; - prep.millimeters_remaining = decel_dist; + prep.mm_eob = pl_block->millimeters-decel_dist; } else { - prep.exit_speed = sqrt(pl_block->entry_speed_sqr-2*pl_block->acceleration*prep.millimeters_remaining); + prep.exit_speed = sqrt(pl_block->entry_speed_sqr-2*pl_block->acceleration*pl_block->millimeters); } } else { // Compute velocity profile parameters of the prepped planner block. prep.ramp_type = RAMP_ACCEL; // Initialize as acceleration ramp. - prep.accelerate_until = prep.millimeters_remaining; + prep.accelerate_until = pl_block->millimeters; prep.exit_speed = plan_get_exec_block_exit_speed(); float exit_speed_sqr = prep.exit_speed*prep.exit_speed; float intersect_distance = - 0.5*(prep.millimeters_remaining+inv_2_accel*(pl_block->entry_speed_sqr-exit_speed_sqr)); + 0.5*(pl_block->millimeters+inv_2_accel*(pl_block->entry_speed_sqr-exit_speed_sqr)); if (intersect_distance > 0.0) { - if (intersect_distance < prep.millimeters_remaining) { // Either trapezoid or triangle types + if (intersect_distance < pl_block->millimeters) { // Either trapezoid or triangle types // NOTE: For acceleration-cruise and cruise-only types, following calculation will be 0.0. prep.decelerate_after = inv_2_accel*(pl_block->nominal_speed_sqr-exit_speed_sqr); if (prep.decelerate_after < intersect_distance) { // Trapezoid type @@ -556,16 +533,15 @@ void st_prep_buffer() } } else { // Deceleration-only type prep.ramp_type = RAMP_DECEL; - prep.decelerate_after = prep.millimeters_remaining; + // prep.decelerate_after = pl_block->millimeters; prep.maximum_speed = prep.current_speed; } } else { // Acceleration-only type prep.accelerate_until = 0.0; - prep.decelerate_after = 0.0; + // prep.decelerate_after = 0.0; prep.maximum_speed = prep.exit_speed; } - } - + } } // Initialize new segment @@ -584,18 +560,18 @@ void st_prep_buffer() considered completed despite having a truncated execution time less than DT_SEGMENT. */ float dt = 0.0; - float mm_remaining = prep.millimeters_remaining; + float mm_remaining = pl_block->millimeters; float time_var = DT_SEGMENT; // Time worker variable float mm_var; // mm distance worker variable do { switch (prep.ramp_type) { case RAMP_ACCEL: - // NOTE: Acceleration ramp always computes during first loop only. + // NOTE: Acceleration ramp only computes during first do-while loop. mm_remaining -= DT_SEGMENT*(prep.current_speed + pl_block->acceleration*(0.5*DT_SEGMENT)); if (mm_remaining < prep.accelerate_until) { // End of acceleration ramp. // Acceleration-cruise, acceleration-deceleration ramp junction, or end of block. mm_remaining = prep.accelerate_until; // NOTE: 0.0 at EOB - time_var = 2.0*(prep.millimeters_remaining-mm_remaining)/(prep.current_speed+prep.maximum_speed); + time_var = 2.0*(pl_block->millimeters-mm_remaining)/(prep.current_speed+prep.maximum_speed); if (mm_remaining == prep.decelerate_after) { prep.ramp_type = RAMP_DECEL; } else { prep.ramp_type = RAMP_CRUISE; } prep.current_speed = prep.maximum_speed; @@ -618,21 +594,21 @@ void st_prep_buffer() default: // case RAMP_DECEL: // NOTE: mm_var used to catch negative decelerate distance values near zero speed. mm_var = time_var*(prep.current_speed - 0.5*pl_block->acceleration*time_var); - if ((mm_var > 0.0) && (mm_var < mm_remaining)) { // Deceleration only. + if ((mm_var > prep.mm_eob) && (mm_var < mm_remaining)) { // Deceleration only. prep.current_speed -= pl_block->acceleration*time_var; // Check for near-zero speed and prevent divide by zero in rare scenarios. if (prep.current_speed > prep.exit_speed) { mm_remaining -= mm_var; } - else { mm_remaining = 0.0; } // NOTE: Force EOB for now. May or may not be needed. + else { mm_remaining = prep.mm_eob; } // NOTE: Force EOB for now. May or may not be needed. } else { // End of block. - time_var = 2.0*mm_remaining/(prep.current_speed+prep.exit_speed); - mm_remaining = 0.0; + time_var = 2.0*(mm_remaining-prep.mm_eob)/(prep.current_speed+prep.exit_speed); + mm_remaining = prep.mm_eob; // prep.current_speed = prep.exit_speed; // !! May be needed for feed hold reinitialization. } } dt += time_var; // Add computed ramp time to total segment time. if (dt < DT_SEGMENT) { time_var = DT_SEGMENT - dt; } // **Incomplete** At ramp junction. else { break; } // **Complete** Exit loop. Segment execution time maxed. - } while ( mm_remaining > 0.0 ); // **Complete** Exit loop. End of planner block. + } while (mm_remaining > prep.mm_eob); // **Complete** Exit loop. End of planner block. /* ----------------------------------------------------------------------------------- Compute segment step rate, steps to execute, and step phase correction parameters. @@ -654,38 +630,37 @@ void st_prep_buffer() prep_segment->phase_dist = ceil(INV_TIME_MULTIPLIER*(ceil(steps_remaining)-steps_remaining)); prep_segment->n_step = ceil(prep.steps_remaining)-ceil(steps_remaining); - // Update step execution variables - prep.step_events_remaining -= prep_segment->n_step; - prep.millimeters_remaining = mm_remaining; - prep.steps_remaining = steps_remaining; + // Update step execution variables. + if (mm_remaining == prep.mm_eob) { + // NOTE: Currently only feed holds qualify for this scenario. May change with overrides. + prep.current_speed = 0.0; + prep.steps_remaining = ceil(steps_remaining); + pl_block->millimeters = prep.steps_remaining/prep.step_per_mm; + plan_cycle_reinitialize(); + sys.state = STATE_QUEUED; // End cycle. + } else { + pl_block->millimeters = mm_remaining; + prep.steps_remaining = steps_remaining; + } } else { // End of block. // Set to execute the remaining steps and no phase correction upon finishing the block. prep_segment->dist_per_tick = ceil( prep.steps_remaining*time_var ); // (mult*step/isr_tic) prep_segment->phase_dist = 0; prep_segment->n_step = ceil(prep.steps_remaining); + + // The planner block is complete. All steps are set to be executed in the segment buffer. + // TODO: Broken with feed holds. Need to recalculate the planner buffer at this time. + pl_block = NULL; + plan_discard_current_block(); - prep.step_events_remaining -= prep_segment->n_step; - if (prep.step_events_remaining > 0) { - sys.state = STATE_QUEUED; - pl_block->entry_speed_sqr = 0.0; - prep.current_speed = 0.0; - prep.steps_remaining = prep.step_events_remaining; - pl_block->millimeters = prep.steps_remaining/prep.step_per_mm; - prep.millimeters_remaining = pl_block->millimeters; - pl_block = NULL; - prep.flag_partial_block = true; - plan_cycle_reinitialize(); - } else { - // The planner block is complete. All steps are set to be executed in the segment buffer. - // TODO: Ignore this for feed holds. Need to recalculate the planner buffer at this time. - pl_block = NULL; - plan_discard_current_block(); + if (sys.state == STATE_HOLD) { + if (prep.current_speed == 0.0) { + plan_cycle_reinitialize(); + sys.state = STATE_QUEUED; + } } - } - - // New step segment initialization completed. Increment segment buffer indices. segment_buffer_head = segment_next_head; @@ -696,3 +671,24 @@ void st_prep_buffer() // printInteger(blength); } } + +/* + TODO: With feedrate overrides, increases to the override value will not significantly + change the planner and stepper current operation. When the value increases, we simply + need to recompute the block plan with new nominal speeds and maximum junction velocities. + However with a decreasing feedrate override, this gets a little tricky. The current block + plan is optimal, so if we try to reduce the feed rates, it may be impossible to create + a feasible plan at its current operating speed and decelerate down to zero at the end of + the buffer. We first have to enforce a deceleration to meet and intersect with the reduced + feedrate override plan. For example, if the current block is cruising at a nominal rate + and the feedrate override is reduced, the new nominal rate will now be lower. The velocity + profile must first decelerate to the new nominal rate and then follow on the new plan. + Another issue is whether or not a feedrate override reduction causes a deceleration + that acts over several planner blocks. For example, say that the plan is already heavily + decelerating throughout it, reducing the feedrate override will not do much to it. So, + how do we determine when to resume the new plan? One solution is to tie into the feed hold + handling code to enforce a deceleration, but check when the current speed is less than or + equal to the block maximum speed and is in an acceleration or cruising ramp. At this + point, we know that we can recompute the block velocity profile to meet and continue onto + the new block plan. +*/ diff --git a/stepper_test.c b/stepper_test.c deleted file mode 100644 index fea6315..0000000 --- a/stepper_test.c +++ /dev/null @@ -1,788 +0,0 @@ -/* - stepper.c - stepper motor driver: executes motion plans using stepper motors - Part of Grbl - - Copyright (c) 2011-2013 Sungeun K. Jeon - Copyright (c) 2009-2011 Simen Svale Skogsrud - - Grbl is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl. If not, see . -*/ - -#include -#include "stepper.h" -#include "config.h" -#include "settings.h" -#include "planner.h" -#include "nuts_bolts.h" - -// Some useful constants -#define TICKS_PER_MICROSECOND (F_CPU/1000000) - -#define RAMP_NOOP_CRUISE 0 -#define RAMP_ACCEL 1 -#define RAMP_DECEL 2 - -#define LOAD_NOOP 0 -#define LOAD_SEGMENT 1 -#define LOAD_BLOCK 2 - -#define SEGMENT_NOOP 0 -#define SEGMENT_END_OF_BLOCK bit(0) -#define RAMP_CHANGE_ACCEL bit(1) -#define RAMP_CHANGE_DECEL bit(2) - -#define MINIMUM_STEPS_PER_SEGMENT 1 // Don't change - -#define SEGMENT_BUFFER_SIZE 6 - -#define DT_SEGMENT (1/ACCELERATION_TICKS_PER_SECOND) - -// Stepper state variable. Contains running data and trapezoid variables. -typedef struct { - // Used by the bresenham line algorithm - int32_t counter_x, // Counter variables for the bresenham line tracer - counter_y, - counter_z; - - // Used by inverse time algorithm to track step rate - int32_t counter_dist; // Inverse time distance traveled since last step event - - uint8_t step_count; // Steps remaining in line segment motion - uint8_t phase_count; // Phase ticks remaining after line segment steps complete - - // Used by the stepper driver interrupt - uint8_t execute_step; // Flags step execution for each interrupt. - uint8_t step_pulse_time; // Step pulse reset time after step rise - uint8_t out_bits; // The next stepping-bits to be output - uint8_t load_flag; -} stepper_t; -static stepper_t st; - -// Stores stepper common data for executing steps in the segment buffer. Data can change mid-block when the -// planner updates the remaining block velocity profile with a more optimal plan or a feedrate override occurs. -// NOTE: Normally, this buffer is partially in-use, but, for the worst case scenario, it will never exceed -// the number of accessible stepper buffer segments (SEGMENT_BUFFER_SIZE-1). -typedef struct { - - // TODO: Retain step[N_AXIS], step_event_count, and direction byte here, so that we can throw - // away the planner block when the segment prep is complete. - - float step_events_remaining; // Tracks step event count for the executing planner block - - // Planner block velocity profile parameters used to trace and execute steps.; - float accelerate_until; - float decelerate_after; - float current_speed; - float maximum_speed; - float exit_speed; -} st_data_t; -static st_data_t segment_data[SEGMENT_BUFFER_SIZE-1]; - -// Primary stepper segment ring buffer. Contains small, short line segments for the stepper algorithm to execute, -// which are "checked-out" incrementally from the first block in the planner buffer. Once "checked-out", the steps -// in the segments buffer cannot be modified by the planner, where the remaining planner block steps still can. -typedef struct { - uint8_t n_step; // Number of step events to be executed for this segment - uint8_t n_phase_tick; - uint32_t dist_per_tick; - uint8_t st_data_index; // Stepper buffer common data index. Uses this information to execute this segment. - uint8_t flag; // Stepper algorithm bit-flag for special execution conditions. -} st_segment_t; -static st_segment_t segment_buffer[SEGMENT_BUFFER_SIZE]; - -// Step segment ring buffer indices -static volatile uint8_t segment_buffer_tail; -static volatile uint8_t segment_buffer_head; -static uint8_t segment_next_head; - -static volatile uint8_t busy; // Used to avoid ISR nesting of the "Stepper Driver Interrupt". Should never occur though. -static plan_block_t *pl_current_block; // A pointer to the planner block currently being traced -static st_segment_t *st_current_segment; -static st_data_t *st_current_data; - -// Pointers for the step segment being prepped from the planner buffer. Accessed only by the -// main program. Pointers may be planning segments or planner blocks ahead of what being executed. -static plan_block_t *pl_prep_block; // Pointer to the planner block being prepped -static st_data_t *st_prep_data; // Pointer to the stepper common data being prepped -static uint8_t pl_prep_index; // Index of planner block being prepped -static uint8_t st_data_prep_index; // Index of stepper common data block being prepped -static uint8_t pl_partial_block_flag; // Flag indicating the planner has modified the prepped planner block - -static float st_prep_step_per_mm; - -// TODO: All this stuff only needs to be retained for the prepped planner block. Once changed -// or complete, we do not need this information anymore. Duh! -// typedef struct { -// float step_events_remaining; // Tracks step event count for the executing planner block -// float accelerate_until; -// float decelerate_after; -// float current_speed; -// float maximum_speed; -// float exit_speed; -// float step_per_mm; -// } st_prep_data_t; - -/* __________________________ - /| |\ _________________ ^ - / | | \ /| |\ | - / | | \ / | | \ s - / | | | | | \ p - / | | | | | \ e - +-----+------------------------+---+--+---------------+----+ e - | BLOCK 1 | BLOCK 2 | d - - time -----> - - The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta - until reaching cruising speed block->nominal_rate, and/or until step_events_remaining reaches block->decelerate_after - after which it decelerates until the block is completed. The driver uses constant acceleration, which is applied as - +/- block->rate_delta velocity increments by the midpoint rule at each ACCELERATION_TICKS_PER_SECOND. -*/ - -// Stepper state initialization. Cycle should only start if the st.cycle_start flag is -// enabled. Startup init and limits call this function but shouldn't start the cycle. -void st_wake_up() -{ - // Enable steppers by resetting the stepper disable port - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { - STEPPERS_DISABLE_PORT |= (1<> 3); - // Enable stepper driver interrupt - st.execute_step = false; - st.load_flag = LOAD_BLOCK; - - TCNT2 = 0; // Clear Timer2 - TIMSK2 |= (1<n_step; - - // If the new segment starts a new planner block, initialize stepper variables and counters. - // NOTE: For new segments only, the step counters are not updated to ensure step phasing is continuous. - if (st.load_flag == LOAD_BLOCK) { - pl_current_block = plan_get_current_block(); // Should always be there. Stepper buffer handles this. - st_current_data = &segment_data[segment_buffer[segment_buffer_tail].st_data_index]; - - // Initialize direction bits for block. Set execute flag to set directions bits upon next ISR tick. - st.out_bits = pl_current_block->direction_bits ^ settings.invert_mask; - st.execute_step = true; - - // Initialize Bresenham line counters - st.counter_x = (pl_current_block->step_event_count >> 1); - st.counter_y = st.counter_x; - st.counter_z = st.counter_x; - - // Initialize inverse time, step rate data, and acceleration ramp counters - st.counter_dist = INV_TIME_MULTIPLIER; // dist_per_step always greater than dist_per_tick. - } - - st.load_flag = LOAD_NOOP; // Segment motion loaded. Set no-operation flag to skip during execution. - - } else { - // Can't discard planner block here if a feed hold stops in middle of block. - st_go_idle(); - bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end - return; // Nothing to do but exit. - } - - } - - // Iterate inverse time counter. Triggers each Bresenham step event. - st.counter_dist -= st_current_segment->dist_per_tick; - - // Execute Bresenham step event, when it's time to do so. - if (st.counter_dist < 0) { - if (st.step_count > 0) { // Block phase correction from executing step. - st.counter_dist += INV_TIME_MULTIPLIER; // Reload inverse time counter - - st.out_bits = pl_current_block->direction_bits; // Reset out_bits and reload direction bits - st.execute_step = true; - - // Execute step displacement profile by Bresenham line algorithm - st.counter_x -= pl_current_block->steps[X_AXIS]; - if (st.counter_x < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Y_AXIS]; - if (st.counter_y < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<steps[Z_AXIS]; - if (st.counter_z < 0) { - st.out_bits |= (1<step_event_count; - if (st.out_bits & (1<flag & SEGMENT_END_OF_BLOCK) { - plan_discard_current_block(); - st.load_flag = LOAD_BLOCK; - } else { - st.load_flag = LOAD_SEGMENT; - } - - // Discard current segment by advancing buffer tail index - if ( ++segment_buffer_tail == SEGMENT_BUFFER_SIZE) { segment_buffer_tail = 0; } - } - st.phase_count--; - } - - - busy = false; -// SPINDLE_ENABLE_PORT ^= 1<step_events_remaining); -// st.ramp_type = RAMP_ACCEL; -// st.counter_ramp = ISR_TICKS_PER_ACCELERATION_TICK/2; -// st.ramp_rate = 0; -// sys.state = STATE_QUEUED; -// } else { -// sys.state = STATE_IDLE; -// } - sys.state = STATE_IDLE; - -} - - -/* Prepares step segment buffer. Continuously called from main program. - - The segment buffer is an intermediary buffer interface between the execution of steps - by the stepper algorithm and the velocity profiles generated by the planner. The stepper - algorithm only executes steps within the segment buffer and is filled by the main program - when steps are "checked-out" from the first block in the planner buffer. This keeps the - step execution and planning optimization processes atomic and protected from each other. - The number of steps "checked-out" from the planner buffer and the number of segments in - the segment buffer is sized and computed such that no operation in the main program takes - longer than the time it takes the stepper algorithm to empty it before refilling it. - Currently, the segment buffer conservatively holds roughly up to 40-60 msec of steps. - - NOTE: The segment buffer executes a set number of steps over an approximate time period. - If we try to execute over a fixed time period, it is difficult to guarantee or predict - how many steps will execute over it, especially when the step pulse phasing between the - neighboring segments must also be kept consistent. Meaning that, if the last segment step - pulses right before a segment end, the next segment must delay its first pulse so that the - step pulses are consistently spaced apart over time to keep the step pulse train nice and - smooth. Keeping track of phasing and ensuring that the exact number of steps are executed - as defined by the planner block, the related computational overhead can get quickly and - prohibitively expensive, especially in real-time. - Since the stepper algorithm automatically takes care of the step pulse phasing with - its ramp and inverse time counters by retaining the count remainders, we don't have to - explicitly and expensively track and synchronize the exact number of steps, time, and - phasing of steps. All we need to do is approximate the number of steps in each segment - such that the segment buffer has enough execution time for the main program to do what - it needs to do and refill it when it comes back. In other words, we just need to compute - a cheap approximation of the current velocity and the number of steps over it. -*/ - -/* - TODO: Figure out how to enforce a deceleration when a feedrate override is reduced. - The problem is that when an override is reduced, the planner may not plan back to - the current rate. Meaning that the velocity profiles for certain conditions no longer - are trapezoidal or triangular. For example, if the current block is cruising at a - nominal rate and the feedrate override is reduced, the new nominal rate will now be - lower. The velocity profile must first decelerate to the new nominal rate and then - follow on the new plan. So the remaining velocity profile will have a decelerate, - cruise, and another decelerate. - Another issue is whether or not a feedrate override reduction causes a deceleration - that acts over several planner blocks. For example, say that the plan is already - heavily decelerating throughout it, reducing the feedrate will not do much to it. So, - how do we determine when to resume the new plan? How many blocks do we have to wait - until the new plan intersects with the deceleration curve? One plus though, the - deceleration will never be more than the number of blocks in the entire planner buffer, - but it theoretically can be equal to it when all planner blocks are decelerating already. -*/ -void st_prep_buffer() -{ - if (sys.state == STATE_QUEUED) { return; } // Block until a motion state is issued - while (segment_buffer_tail != segment_next_head) { // Check if we need to fill the buffer. - - // Initialize new segment - st_segment_t *prep_segment = &segment_buffer[segment_buffer_head]; - prep_segment->flag = SEGMENT_NOOP; - - // ----------------------------------------------------------------------------------- - // Determine if we need to load a new planner block. If so, prepare step data. - if (pl_prep_block == NULL) { - pl_prep_block = plan_get_block_by_index(pl_prep_index); // Query planner for a queued block - if (pl_prep_block == NULL) { return; } // No planner blocks. Exit. - - // Increment stepper common data index - if ( ++st_data_prep_index == (SEGMENT_BUFFER_SIZE-1) ) { st_data_prep_index = 0; } - - // Check if the planner has re-computed this block mid-execution. If so, push the previous - // segment data. Otherwise, prepare a new segment data for the new planner block. - if (pl_partial_block_flag) { - - // Prepare new shared segment block data and copy the relevant last segment block data. - st_data_t *last_st_prep_data; - last_st_prep_data = st_prep_data; - st_prep_data = &segment_data[st_data_prep_index]; - - st_prep_data->step_events_remaining = last_st_prep_data->step_events_remaining; - - pl_partial_block_flag = false; // Reset flag - - } else { - - // Prepare commonly shared planner block data for the ensuing segment buffer moves ad-hoc, since - // the planner buffer can dynamically change the velocity profile data as blocks are added. - st_prep_data = &segment_data[st_data_prep_index]; - - st_prep_step_per_mm = pl_prep_block->step_event_count/pl_prep_block->millimeters; - - // Initialize planner block step data - st_prep_data->step_events_remaining = pl_prep_block->step_event_count; - - } - - - st_prep_data->current_speed = sqrt(pl_prep_block->entry_speed_sqr); - - // Determine current block exit speed - plan_block_t *pl_next_block = plan_get_block_by_index(plan_next_block_index(pl_prep_index)); - float exit_speed_sqr; - if (pl_next_block == NULL) { - exit_speed_sqr = 0.0; - st_prep_data->exit_speed = 0.0; - } else { - exit_speed_sqr = pl_next_block->entry_speed_sqr; - st_prep_data->exit_speed = sqrt(exit_speed_sqr); - } - -// st_prep_data->accelerate_until = 0.5*(pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(pl_prep_block->acceleration); -// st_prep_data->decelerate_after = 0.5*(pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(pl_prep_block->acceleration); -// if (pl_prep_block->millimeters < st_prep_data->accelerate_until+st_prep_data->decelerate_after) { -// st_prep_data->decelerate_after = 0.5*( pl_prep_block->millimeters + 0.5*(pl_prep_block->entry_speed_sqr -// - exit_speed_sqr)/(pl_prep_block->acceleration) ); -// st_prep_data->accelerate_until = pl_prep_block->millimeters-st_prep_data->decelerate_after; -// st_prep_data->maximum_speed = sqrt(2*pl_prep_block->acceleration*st_prep_data->decelerate_after+exit_speed_sqr); -// } else { -// st_prep_data->accelerate_until = pl_prep_block->millimeters-st_prep_data->accelerate_until; -// st_prep_data->maximum_speed = sqrt(pl_prep_block->nominal_speed_sqr); -// } - - // Determine velocity profile based on the 7 possible types: Cruise-only, cruise-deceleration, - // acceleration-cruise, acceleration-only, deceleration-only, trapezoid, and triangle. - st_prep_data->accelerate_until = pl_prep_block->millimeters; - if (pl_prep_block->entry_speed_sqr == pl_prep_block->nominal_speed_sqr) { - st_prep_data->maximum_speed = sqrt(pl_prep_block->nominal_speed_sqr); - if (exit_speed_sqr == pl_prep_block->nominal_speed_sqr) { // Cruise-only type - st_prep_data->decelerate_after = 0.0; - } else { // Cruise-deceleration type - st_prep_data->decelerate_after = 0.5*(pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(pl_prep_block->acceleration); - } - } else if (exit_speed_sqr == pl_prep_block->nominal_speed_sqr) { - // Acceleration-cruise type - st_prep_data->maximum_speed = sqrt(pl_prep_block->nominal_speed_sqr); - st_prep_data->decelerate_after = 0.0; - st_prep_data->accelerate_until -= 0.5*(pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(pl_prep_block->acceleration); - } else { - float intersection_dist = 0.5*( pl_prep_block->millimeters + 0.5*(pl_prep_block->entry_speed_sqr - - exit_speed_sqr)/(pl_prep_block->acceleration) ); - if (intersection_dist > 0.0) { - if (intersection_dist < pl_prep_block->millimeters) { // Either trapezoid or triangle types - st_prep_data->decelerate_after = 0.5*(pl_prep_block->nominal_speed_sqr-exit_speed_sqr)/(pl_prep_block->acceleration); - if (st_prep_data->decelerate_after < intersection_dist) { // Trapezoid type - st_prep_data->maximum_speed = sqrt(pl_prep_block->nominal_speed_sqr); - st_prep_data->accelerate_until -= 0.5*(pl_prep_block->nominal_speed_sqr-pl_prep_block->entry_speed_sqr)/(pl_prep_block->acceleration); - } else { // Triangle type - st_prep_data->decelerate_after = intersection_dist; - st_prep_data->maximum_speed = sqrt(2*pl_prep_block->acceleration*st_prep_data->decelerate_after+exit_speed_sqr); - st_prep_data->accelerate_until -= st_prep_data->decelerate_after; - } - } else { // Deceleration-only type - st_prep_data->maximum_speed = st_prep_data->current_speed; - st_prep_data->decelerate_after = pl_prep_block->millimeters; - } - } else { // Acceleration-only type - st_prep_data->maximum_speed = st_prep_data->exit_speed; - st_prep_data->decelerate_after = 0.0; - st_prep_data->accelerate_until = 0.0; - } - } - } - - // Set new segment to point to the current segment data block. - prep_segment->st_data_index = st_data_prep_index; - - // ----------------------------------------------------------------------------------- - // Initialize segment execute distance. Attempt to create a full segment over DT_SEGMENT. - - float mm_remaining = pl_prep_block->millimeters; - float dt = DT_SEGMENT; - if (mm_remaining > st_prep_data->accelerate_until) { // Acceleration ramp - mm_remaining -= (st_prep_data->current_speed*DT_SEGMENT - + pl_prep_block->acceleration*(0.5*DT_SEGMENT*DT_SEGMENT)); - if (mm_remaining < st_prep_data->accelerate_until) { // **Incomplete** Acceleration ramp end. - // Acceleration-cruise, acceleration-deceleration ramp junction, or end of block. - mm_remaining = st_prep_data->accelerate_until; // NOTE: 0.0 at EOB - dt = 2*(pl_prep_block->millimeters-mm_remaining)/ - (st_prep_data->current_speed+st_prep_data->maximum_speed); - st_prep_data->current_speed = st_prep_data->maximum_speed; - } else { // **Complete** Acceleration only. - st_prep_data->current_speed += pl_prep_block->acceleration*DT_SEGMENT; - } - } else if (mm_remaining <= st_prep_data->decelerate_after) { // Deceleration ramp - mm_remaining -= (st_prep_data->current_speed*DT_SEGMENT - - pl_prep_block->acceleration*(0.5*DT_SEGMENT*DT_SEGMENT)); - if (mm_remaining > 0.0) { // **Complete** Deceleration only. - st_prep_data->current_speed -= pl_prep_block->acceleration*DT_SEGMENT; - } else { // **Complete* End of block. - dt = 2*pl_prep_block->millimeters/(st_prep_data->current_speed+st_prep_data->exit_speed); - mm_remaining = 0.0; - // st_prep_data->current_speed = st_prep_data->exit_speed; - } - } else { // Cruising profile - mm_remaining -= st_prep_data->maximum_speed*DT_SEGMENT; - if (mm_remaining < st_prep_data->decelerate_after) { // **Incomplete** End of cruise. - // Cruise-deceleration junction or end of block. - mm_remaining = st_prep_data->decelerate_after; // NOTE: 0.0 at EOB - dt = (pl_prep_block->millimeters-mm_remaining)/st_prep_data->maximum_speed; - } // Otherwise **Complete** Cruising only. - } - - // ----------------------------------------------------------------------------------- - // If segment is incomplete, attempt to fill the remainder. - // NOTE: Segment remainder always spans a cruise and/or a deceleration ramp. - - if (dt < DT_SEGMENT) { - if (mm_remaining > 0.0) { // Skip if end of block. - float last_mm_remaining; - float dt_remainder; - - // Fill incomplete segment with an acceleration junction. - if (mm_remaining > st_prep_data->decelerate_after) { // Cruising profile - last_mm_remaining = mm_remaining; - dt_remainder = DT_SEGMENT-dt; - mm_remaining -= st_prep_data->current_speed*dt_remainder; - if (mm_remaining < st_prep_data->decelerate_after) { // **Incomplete** - mm_remaining = st_prep_data->decelerate_after; - dt += (last_mm_remaining-mm_remaining)/st_prep_data->maximum_speed; - // current_speed = maximum_speed; - } else { // **Complete** Segment filled. - dt = DT_SEGMENT; - } - } - - // Fill incomplete segment with a deceleration junction. - if (mm_remaining > 0.0) { - if (mm_remaining <= st_prep_data->decelerate_after) { // Deceleration ramp - last_mm_remaining = mm_remaining; - dt_remainder = DT_SEGMENT-dt; - mm_remaining -= (dt_remainder*(st_prep_data->current_speed - - 0.5*pl_prep_block->acceleration*dt_remainder)); - if (mm_remaining > 0.0) { // **Complete** Segment filled. - st_prep_data->current_speed -= pl_prep_block->acceleration*dt_remainder; - dt = DT_SEGMENT; - } else { // **Complete** End of block. - mm_remaining = 0.0; - dt += (2*last_mm_remaining/(st_prep_data->current_speed+st_prep_data->exit_speed)); - // st_prep_data->current_speed = st_prep_data->exit_speed; - } - } - } - - } - } - - // ----------------------------------------------------------------------------------- - // Compute segment step rate, steps to execute, and step phase correction parameters. - - // Convert segment distance in terms of steps. -// float dist_travel = pl_prep_block->millimeters; -// if (mm_remaining > 0.0) { dist_travel -= mm_remaining; } - - - if (mm_remaining > 0.0) { - - float steps_remaining = st_prep_step_per_mm*mm_remaining; - prep_segment->dist_per_tick = ceil( (INV_TIME_MULTIPLIER/ISR_TICKS_PER_SECOND)* - (st_prep_data->step_events_remaining-steps_remaining)/dt ); // (mult*step/isr_tic) - - // Compute number of steps to execute and segment step phase correction. - prep_segment->n_step = ceil(st_prep_data->step_events_remaining)-ceil(steps_remaining); - prep_segment->n_phase_tick = ceil(INV_TIME_MULTIPLIER*(ceil(steps_remaining)-steps_remaining)/prep_segment->dist_per_tick); - - // Update step execution variables - st_prep_data->step_events_remaining = steps_remaining; - pl_prep_block->millimeters = mm_remaining; - - } else { // End of block. Finish it out. - - prep_segment->dist_per_tick = ceil( (INV_TIME_MULTIPLIER/ISR_TICKS_PER_SECOND)* - st_prep_data->step_events_remaining/dt ); // (mult*step/isr_tic) - - // Set to execute the remaining steps and no phase correction upon finishing the block. - prep_segment->n_step = ceil(st_prep_data->step_events_remaining); - prep_segment->n_phase_tick = 0; - - // NOTE: Not required. Planner will ignore this block as it is now complete. - // st_prep_data->step_events_remaining = 0.0; - // pl_prep_block->millimeters = 0.0; - - // Move planner pointer to next block and flag to load a new block for the next segment. - pl_prep_index = plan_next_block_index(pl_prep_index); - pl_prep_block = NULL; - prep_segment->flag |= SEGMENT_END_OF_BLOCK; - - } - - // !!! PROBLEM. Step events remaining in floating point can limit the number of steps - // we can accurately track, since floats have ~7.2 significant digits. However, this only - // becomes a problem if there are more than 1,000,000, which translates to a CNC machine - // with 200 step/mm and 5 meters of axis travel. Possible but unlikely. Could have more - // issues with user setting up their machine with too high of steps. - - // TODO: dist_per_tick must be less than INV_TIME_MULTIPLIER. A check can be made to - // make this a hard limit. Need to make sure this doesn't affect the velocity profiles.. - // it shouldn't. The same could said for the minimum allowable step rate too. This should - // not affect the tracing of the profiles either. - - // Ensure the initial step rate exceeds the MINIMUM_STEP_RATE. - // TODO: Use config.h error checking to do this. Otherwise, counters get screwy. - - // New step segment initialization completed. Increment segment buffer indices. - segment_buffer_head = segment_next_head; - if ( ++segment_next_head == SEGMENT_BUFFER_SIZE ) { segment_next_head = 0; } - - SPINDLE_ENABLE_PORT ^= 1<entry_speed_sqr = st_prep_data->current_speed*st_prep_data->current_speed; - // pl_partial_block->max_entry_speed_sqr = pl_partial_block->entry_speed_sqr; // Not sure if this needs to be updated. - - // Flag for new prep_block when st_prep_buffer() is called after the planner recomputes. - pl_partial_block_flag = true; - pl_prep_block = NULL; - } - return; -}