MEGA 2560 - Odd Timer Behavior first loop and intermittent thereafter

For the sake of those who may find this thread during their research, I'm going to continue updating this thread as I work through the problem and find the ultimate solution.

I've found the root cause of the issue, and it is related to rounding/truncation when working with floats/doubles which ultimately require an int type result (specifically uint16_t, as used by my OCR array). My speeds are in mm/s, so I have a variable of type double which represents the mm per step of the drive (taking into account steps per revolution, any microstepping, and pulley size on the motor). With my motors EDIT: and pulleys, this particular value with those units is 0.04 mm/step, thus requiring the float/double data type, unless I carry a different unit throughout the calculations.

To highlight the root cause of the problem, I created a table of values including multiple pairs of columns, one for step count and the other for the accelDecelIndex value I am using to chose the OCR array value to assign to OCRnA. The pairs of Step and A/D Index columns, from left to right, represent speeds 60 mm/s, 70 mm/s, 80 mm/s, and 90 mm/s, respectively. The step number which is highlighted in green is the last possible step for an OCR update during ramp up (it's the last step in the acceleration phase).

     60mm/s            70mm/s           80mm/s            90mm/s

The speeds 70mm/s and 90mm/s highlight the problem perfectly. Before the last acceleration phase step, the A/D Index goes outside of bounds (in my instance the last array index is 20).

Along with the truncation/rounding issues associated with mixed type math as mentioned above, I am using a fixed size array for my OCRnA values, which can create additional truncation/rounding issues in certain cases. I've attempted mitigating this by using the round() function from "math.h", but it of course has it's own associated errors, and thus will not fully resolve the problem.

Here are the formulas I'm using in my code, in a more human-readable format for convenience. Let me know if anyone would like me to associate the data types I'm using for each variable in the image, to better allow analysis and troubleshooting. For now, I've left it off to avoid duplication of information already in this thread, and to also avoid cluttering the image of the formulas used.

EDIT 2: The above formulas are all derived from linear kinematics equations, which can be found in any physics book, or alternatively, the "AVR446: Linear speed control of stepper motor" document published by Atmel (google search will easily yield the full PDF).