From 805d37f77b25fba70c05518cda4edd1c349b7f04 Mon Sep 17 00:00:00 2001
From: Erik van der Zalm <erik@vdzalm.eu>
Date: Tue, 15 Nov 2011 18:14:00 +0100
Subject: [PATCH] Fixed some small planner bugs

---
 Marlin/Configuration.h |  5 +++--
 Marlin/planner.cpp     | 15 ++++++++++-----
 Marlin/planner.h       | 20 ++++++++++----------
 Marlin/stepper.cpp     |  2 +-
 4 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 135b61e0135..3b0cfacfebc 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -11,7 +11,8 @@
 
 // Frequency limit
 // See nophead's blog for more info
-#define XY_FREQUENCY_LIMIT  15
+// Not working OK
+//#define XY_FREQUENCY_LIMIT  15
 
 // Minimum planner junction speed. Sets the default minimum speed the planner plans for at the end
 // of the buffer and all stops. This should not be much greater than zero and should only be changed
@@ -201,7 +202,7 @@ const bool ENDSTOPS_INVERTING = true; // set to true to invert the logic of the
 
 #define AXIS_RELATIVE_MODES {false, false, false, false}
 
-#define MAX_STEP_FREQUENCY 40000L // Max step frequency for Ultimaker (5000 pps / half step)
+#define MAX_STEP_FREQUENCY 40000 // Max step frequency for Ultimaker (5000 pps / half step)
 
 // default settings 
 
diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp
index a3dde6c3121..c27d5860176 100644
--- a/Marlin/planner.cpp
+++ b/Marlin/planner.cpp
@@ -72,7 +72,7 @@
 unsigned long minsegmenttime;
 float max_feedrate[4]; // set the max speeds
 float axis_steps_per_unit[4];
-long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
+unsigned long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
 float minimumfeedrate;
 float acceleration;         // Normal acceleration mm/s^2  THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX
 float retract_acceleration; //  mm/s^2   filament pull-pack and push-forward  while standing still in the other axis M204 TXXXX
@@ -153,8 +153,8 @@ inline float intersection_distance(float initial_rate, float final_rate, float a
 // Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
 
 void calculate_trapezoid_for_block(block_t *block, float entry_factor, float exit_factor) {
-  long initial_rate = ceil(block->nominal_rate*entry_factor); // (step/min)
-  long final_rate = ceil(block->nominal_rate*exit_factor); // (step/min)
+  unsigned long initial_rate = ceil(block->nominal_rate*entry_factor); // (step/min)
+  unsigned long final_rate = ceil(block->nominal_rate*exit_factor); // (step/min)
 
   // Limit minimal step rate (Otherwise the timer will overflow.)
   if(initial_rate <120) {initial_rate=120; }
@@ -570,7 +570,7 @@ void plan_buffer_line(const float &x, const float &y, const float &z, const floa
   long max_x_segment_time = max(x_segment_time[0], max(x_segment_time[1], x_segment_time[2]));
   long max_y_segment_time = max(y_segment_time[0], max(y_segment_time[1], y_segment_time[2]));
   long min_xy_segment_time =min(max_x_segment_time, max_y_segment_time);
-  if(min_xy_segment_time < MAX_FREQ_TIME) speed_factor = min(speed_factor, (float)min_xy_segment_time / (float)MAX_FREQ_TIME);
+  if(min_xy_segment_time < MAX_FREQ_TIME) speed_factor = min(speed_factor, speed_factor * (float)min_xy_segment_time / (float)MAX_FREQ_TIME);
 #endif
 
   // Correct the speed  
@@ -579,7 +579,12 @@ void plan_buffer_line(const float &x, const float &y, const float &z, const floa
     for(int i=0; i < 4; i++) {
     if(abs(current_speed[i]) > max_feedrate[i])
       speed_factor = min(speed_factor, max_feedrate[i] / abs(current_speed[i]));
-//      Serial.print("current_speed"); Serial.print(i); Serial.print(" : "); Serial.println(current_speed[i]);
+ /*     
+      if(speed_factor < 0.1) {
+        Serial.print("speed factor : "); Serial.println(speed_factor);
+        Serial.print("current_speed"); Serial.print(i); Serial.print(" : "); Serial.println(current_speed[i]);
+      }
+ */
   }
     for(unsigned char i=0; i < 4; i++) {
       current_speed[i] *= speed_factor;
diff --git a/Marlin/planner.h b/Marlin/planner.h
index f5c01ea2668..be1587d6b80 100644
--- a/Marlin/planner.h
+++ b/Marlin/planner.h
@@ -32,9 +32,9 @@ typedef struct {
   // Fields used by the bresenham algorithm for tracing the line
   long steps_x, steps_y, steps_z, steps_e;  // Step count along each axis
   long step_event_count;                    // The number of step events required to complete this block
-  volatile long accelerate_until;           // The index of the step event on which to stop acceleration
-  volatile long decelerate_after;           // The index of the step event on which to start decelerating
-  volatile long acceleration_rate;          // The acceleration rate used for acceleration calculation
+  long accelerate_until;                    // The index of the step event on which to stop acceleration
+  long decelerate_after;                    // The index of the step event on which to start decelerating
+  long acceleration_rate;                   // The acceleration rate used for acceleration calculation
   unsigned char direction_bits;             // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
   #ifdef ADVANCE
 //    long advance_rate;
@@ -50,14 +50,14 @@ typedef struct {
   float max_entry_speed;                             // Maximum allowable junction entry speed in mm/min
   float millimeters;                                 // The total travel of this block in mm
   float acceleration;                                // acceleration mm/sec^2
-  unsigned char recalculate_flag;                          // Planner flag to recalculate trapezoids on entry junction
-  unsigned char nominal_length_flag;                       // Planner flag for nominal speed always reached
+  unsigned char recalculate_flag;                    // Planner flag to recalculate trapezoids on entry junction
+  unsigned char nominal_length_flag;                 // Planner flag for nominal speed always reached
 
   // Settings for the trapezoid generator
-  long nominal_rate;                                 // The nominal step rate for this block in step_events/sec 
-  volatile long initial_rate;                        // The jerk-adjusted step rate at start of block  
-  volatile long final_rate;                          // The minimal rate at exit
-  long acceleration_st;                              // acceleration steps/sec^2
+  unsigned long nominal_rate;                        // The nominal step rate for this block in step_events/sec 
+  unsigned long initial_rate;                        // The jerk-adjusted step rate at start of block  
+  unsigned long final_rate;                          // The minimal rate at exit
+  unsigned long acceleration_st;                     // acceleration steps/sec^2
   volatile char busy;
 } block_t;
 
@@ -84,7 +84,7 @@ void check_axes_activity();
 extern unsigned long minsegmenttime;
 extern float max_feedrate[4]; // set the max speeds
 extern float axis_steps_per_unit[4];
-extern long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
+extern unsigned long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
 extern float minimumfeedrate;
 extern float acceleration;         // Normal acceleration mm/s^2  THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX
 extern float retract_acceleration; //  mm/s^2   filament pull-pack and push-forward  while standing still in the other axis M204 TXXXX
diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp
index 96a43ed7ed3..2e232201bf3 100644
--- a/Marlin/stepper.cpp
+++ b/Marlin/stepper.cpp
@@ -253,7 +253,7 @@ inline unsigned short calc_timer(unsigned short step_rate) {
     timer = (unsigned short)pgm_read_word_near(table_address);
     timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
   }
-  if(timer < 100) timer = 100; //(20kHz this should never happen)
+  if(timer < 100) { timer = 100; Serial.print("Steprate to high : "); Serial.println(step_rate); }//(20kHz this should never happen)
   return timer;
 }