aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/stepcompress.c
Commit message (Collapse)AuthorAgeFilesLines
* stepcompress: Fix proactive queue flushing on move with 64K+ stepsKevin O'Connor2017-11-291-16/+21
| | | | | | | | | | | | | | | Commit e05c6354 changed the internal step compress queue from 64bit integers to 32bit integers. However, that commit broke the proactive flushing of moves that could produce more than 64K steps. This could lead to large memory allocations and cpu slow downs on printers that had a very large Z axis - possibly leading to a "Timer too close" mcu shutdown. Correct the code so that it properly generates a 64bit flush clock. Also, be sure to only expand / memmove the queue when there is no room for a new element at the end. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Implement print time to clock conversion in C codeKevin O'Connor2017-09-191-30/+51
| | | | | | | Implement the conversion from print_time to the local mcu's clock within the C code. This simplifies the python code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Return number of steps traveled from stepcompress_push()Kevin O'Connor2017-09-131-2/+2
| | | | | | | Return the same information from stepcompress_push() that is returned from stepcompress_push_const() and stpcompress_push_delta(). Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Minor performance tweaks for rpiKevin O'Connor2017-08-311-3/+2
| | | | | | | | Invert the if conditional in queue_append() and change the order of reads in minmax_point() - gcc on the rpi seems to do generate better code this way. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Use addition instead of multiplication on queue addKevin O'Connor2017-08-311-10/+12
| | | | | | Replace multiplication with addition where possible. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompreses: Change the step queue to use 32bit integersKevin O'Connor2017-08-311-37/+57
| | | | | | | | | | | | | The RaspberryPi processor implements 'double to int32' conversions much faster than 'double to int64' conversions. Rework the code so that steps stored in the queue are always a small offset from the last sent step time. (In the unlikely event that a step is far from the last step time, then the queue is flushed to maintain this invariant.) This simplifies the step compression code as well - it no longer needs to check for integer overflows. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Refactor queue insertion to use a cursorKevin O'Connor2017-08-311-97/+105
| | | | | | | | Create an insertion "cursor" for adding items to the step compression queue. This makes the calling code simpler and it makes it easier to update the queue memory management in the future. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Simplify delta Z only move calculationsKevin O'Connor2017-04-201-9/+9
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Add comment on common suffixes and unitsKevin O'Connor2017-04-071-0/+8
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Modify check_expand() into check_push()Kevin O'Connor2017-04-071-42/+41
| | | | | | | | | | Add the new item at the same time as checking if there is space in the queue. Also, update the default optimization level of c_helper.so to O2 to improve the compiled code layout. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Remove step_dist from stepcompress_push_delta()Kevin O'Connor2017-04-071-23/+18
| | | | | | | Pass the step direction explicitly to the low-level delta kinematic C code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* delta: Do reverse direction checking in C codeKevin O'Connor2017-04-071-3/+35
| | | | | | | | Calculate where a tower must reverse direction during a move in the C code instead of the delta.py kinematic code. This simplifies the python code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* delta: Make it clear that a "virtual tower" is createdKevin O'Connor2017-04-071-17/+18
| | | | | | | | | | | The delta code calculates a "virtual tower" along the line of movement. Rework the variable names and comments to make it clear that this is occurring. It is not necessary to pass the start_pos variable to the C code as it is simple to update the start_pos at the start of each movement. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Merge stepcompress_delta_const and stepcompress_delta_accelKevin O'Connor2017-04-071-85/+50
| | | | | | | | It's not necessary to have separate C delta kinematic functions for constant acceleration and constant velocity as constant velocity can be obtained by using a constant acceleration of zero. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Pass delta velocity and acceleration directly to C codeKevin O'Connor2017-04-071-23/+38
| | | | | | | | Update the C delta kinematic code to take velocity and acceleration directly in step distances and clock ticks. This simplifies the mcu.py python code as it only needs to do unit and axis conversion. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Merge stepcompress_push_accel() and stepcompress_push_const()Kevin O'Connor2017-04-071-60/+35
| | | | | | | | It's not necessary to have separate C functions for constant acceleration and constant velocity as constant velocity can be obtained by using a constant acceleration of zero. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Pass constant velocity and acceleration directly to C codeKevin O'Connor2017-04-071-15/+19
| | | | | | | | Update the C code to take velocity and acceleration directly in step distances and clock ticks. This simplifies the mcu.py python code as it only needs to do unit and axis conversion. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Move stepcompress_push_* functions to their own sectionKevin O'Connor2017-04-071-53/+58
| | | | | | This is only code movement; no code changes. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Always return 0 on negative number in safe_sqrt()Kevin O'Connor2017-03-221-5/+5
| | | | | | | | | | | | sqrt() of a negative number in the C code returns NaN. This value results in behavior that is difficult to debug. Always return 0.0 instead as this results in better behavior that is easier to track down. Also, on some code paths, safe_sqrt is called on numbers that are multiplied by very large amounts (eg, 16000000**2) and thus distinguishing between large and small negative numbers is difficult. For now, report in the log if the value is below -0.001. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Improve check_line() error messagesKevin O'Connor2017-03-161-8/+13
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Propagate errors back to python codeKevin O'Connor2017-02-061-56/+107
| | | | | | | Propagate error codes back to the python code and raise an exception on an error. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Flush periodically if adding more than 64K steps in a moveKevin O'Connor2016-12-311-26/+50
| | | | | | | | | | It's possible for a printer with very fine resolution to require a large number of steps for a homing operation. Instead of storing all of those steps in memory, periodically flush the queue should more than 64K steps be present. This keeps a reasonable limit on the amount of ram needed to store steps. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Using normal message priority system during homingKevin O'Connor2016-12-301-3/+14
| | | | | | | | | | | | | | The endstop homing system requires all queue_step commands be in the MCU's 'move queue' before endstop checking starts. Use the normal message priority system to request that stepper queue_step commands are received prior to the start of the end_stop_home command. This simplifies the code and removes the need for special serial queue flushing. This also fixes a bug in homing operations that take longer than 2^31 clock ticks. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Rework addfactor integer overflow checkKevin O'Connor2016-12-261-11/+7
| | | | | | | | Revert 4a16053c and avoid integer overflows in the addfactor calculation by exiting the loop early if count > 0x200. This provides stronger protection against overflows. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Fix integer overflow leading to infinite loopKevin O'Connor2016-12-241-4/+11
| | | | | | | | | Commit a217c0f3 changed the way the "addfactor" was calculated. Unfortunately, it was possible for the updated method to cause an integer overflow and have a negative addfactor. Fix this by explicitly casting the addfactor calculation to uint32_t. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Increase check on max count to 10000000Kevin O'Connor2016-12-231-4/+4
| | | | | | | | Some motors have very small step distances and they can generate over a million steps during a homing operation. Increase the maximum count to ten million to avoid triggering the internal sanity check. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Prefer greater interval if all else equalKevin O'Connor2016-12-191-1/+2
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Move check for add=0 sequences out of loopKevin O'Connor2016-12-191-1/+9
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Favor higher add values when bisectingKevin O'Connor2016-12-191-1/+1
| | | | | | | | Instead of splitting the available "add range" in half, try for add values closer to the higher end of the range. This heuristic seems to result in better choices. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Use inclusive range on min/maxaddKevin O'Connor2016-12-191-11/+11
| | | | | | | Change the min/maxadd variables to use an inclusive range instead of exclusive. This better matches min/maxinterval. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Be consistent with "point" vs "nextpoint"Kevin O'Connor2016-12-191-33/+35
| | | | | | | | | | | | Make it clear which variables refer to the best verified point found so far, and which variables deal with the next (not yet verified) point. Also, remove checked_count as bestcount serves the same purpose. Also, allow minmax_point to be inlined. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Don't warn on multi-step interval=0 sequencesKevin O'Connor2016-12-141-1/+2
| | | | | | | It is possible to get a valid multi-step sequence with an interval of zero if the add is non-zero. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Encourage add=0 in compress_bisect_add()Kevin O'Connor2016-12-061-2/+2
| | | | | | | Only use a non-zero 'add' if it actually extends the number of steps the command covers. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Optimize push_delta_const() for common XY or Z only movesKevin O'Connor2016-12-051-6/+25
| | | | | | | | | Most moves are on the XY plane - avoid a few multiplications in the inner loop in this case. When there is a Z move, it is almost always entirely a Z move - avoid the sqrt() call in the inner loop in this case. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* delta: Rework delta math to avoid using inv_movexy_rKevin O'Connor2016-12-051-18/+22
| | | | | | | | | | | | | | | Taking the inverse of the XY move distance can lead to extremely large values when the XY distance is very small. This can lead to saturation of the double precision variables and incorrect results. Rework the delta kinematic math to avoid using this inverse. Pass the closestxy_d value directly to the C functions so that the C code can calculate its intermediate constants. After this change the move_z special case is no longer necessary as the regular delta functions now work with movexy_r=0 and movez_r=1. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* pyhelper: Add ability to route error messages to python loggingKevin O'Connor2016-11-301-22/+20
| | | | | | | Instead of writing error messages to stderr, route them into the python code and use the standard python logging system. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* mcu: Be careful to free memory allocated in C codeKevin O'Connor2016-11-301-0/+23
| | | | | | Free steppersync, stepcompress, and commandqueue objects. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Don't bother loop unrolling in push_sqrt()Kevin O'Connor2016-11-141-10/+5
| | | | | | | It's not necessary to have two loops - checking if factor is greater than zero in the loop is fine. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Optimize safe_sqrt() codeKevin O'Connor2016-11-141-3/+8
| | | | | | Optimize the code by putting the uncommon case out-of-line. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* delta: Initial support for linear delta kinematicsKevin O'Connor2016-11-141-0/+73
| | | | | | This adds support for delta based robots. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Do all step rounding in C codeKevin O'Connor2016-11-131-28/+62
| | | | | | | | | | | | | | | | | Commits f0cefebf and 8f331f08 changed the way the code determined what steps to take on fractional steps. Unfortunately, it was possible in some situations for the C code to round differently from the python code which could result in warnings and lost steps. Change the code so that all fractional step handling is done in the C code. Implementing the step rounding logic in one location avoids any conflicts. In order to efficiently handle the step rounding in the C code, the C code has also been extended to directly send the "set_next_step_dir" command. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Minor code movementKevin O'Connor2016-11-121-41/+41
| | | | | | | Move flush() and check_expand() code to a new location so that the stepcompress_push_X() functions can flush. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Do 32bit integer overflow checks internally in C codeKevin O'Connor2016-11-021-10/+31
| | | | | | | | Update the stepcompress C code to check for integer overflow so that the python code does not need to. The new checks also handle the possibility of a single move lasting long enough to cause an overflow. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Invert the meaning of the min_clock flagKevin O'Connor2016-11-021-12/+10
| | | | | | | | Use a non-zero qm->min_clock value to indicate that the command uses the move queue and to also store the clock of when that move queue item will be released. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Store step times using 64bit integersKevin O'Connor2016-11-011-18/+14
| | | | Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Check for small negative numbers on sqrt() callsKevin O'Connor2016-10-101-2/+13
| | | | | | | | | It's theoretically possible for floating point truncation to cause a math formula to return a small negative number instead of 0. If sqrt() is called on this small negative number it could cause a crash. Check for this case and return 0. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Eliminate possible infinite loop in compress_bisect_add()Kevin O'Connor2016-10-101-1/+1
| | | | | | | | | Commit 47f30331 converted compress_bisect_add() to use "best reach" instead of "best add". However, that change caused compress_bisect_add() to behave poorly when passed in invalid data (negative times). Change the code to better handle invalid data. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Compress for greatest "reach" instead of greatest "add"Kevin O'Connor2016-09-241-21/+23
| | | | | | | | | Search for the maximum reachable value instead of the maximum "add". The maximum reachable value encompasses both the maximum count and a closest step time to the last requested step time. This allows for more "add=0" sequences to be generated which the mcu can optimize for. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Fix error causing queue to not be expanded in expand_queue()Kevin O'Connor2016-07-161-2/+2
| | | | | | | | The test to check if the queue only needed to be moved was not correct and it could lead to a segfault if clean_queue() was called instead of actually increasing the queue size. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
* stepcompress: Check for invalid count in step_factor and step_sqrtKevin O'Connor2016-07-161-2/+13
| | | | | | | Check for an invalid count and report an error if found. This prevents some segfaults when count goes negative. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>