Fix short step pulses
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 20 Jan 2020 00:06:58 +0000 (16:06 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 20 Jan 2020 00:06:58 +0000 (16:06 -0800)
CHANGELOG.md
src/avr/src/config.h
src/avr/src/motor.c

index a495f4f343bf680dd8c02abe37fbc4bb0a0f7447..59708e6dd18f25158e8645c73247db3d9321f5ee 100644 (file)
@@ -17,6 +17,7 @@ Buildbotics CNC Controller Firmware Changelog
  - Raised default ``latch-backoff`` to 100mm and ``zero-backoff`` to 5mm.
  - Added ``max-deviation`` option.
  - Fixed problem with GCode boolean expression parsing.  #232.
+ - Ensure 2uS step pulse width.
 
 ## v0.4.11
  - Don't reset global offsets on M2.
index a224b4d793484587c481c30f18a7ace54f09c05b..39d83492c1040975f942dc7c8ba56c1573a7553b 100644 (file)
@@ -139,7 +139,7 @@ enum {
 #define STEP_TIMER_POLL          ((uint16_t)(STEP_TIMER_FREQ * 0.001)) // 1ms
 #define STEP_TIMER_ISR           TCC0_OVF_vect
 #define STEP_LOW_LEVEL_ISR       ADCB_CH0_vect
-#define STEP_PULSE_WIDTH         (F_CPU * 0.000002 / 2) // 2uS w/ clk/2
+#define STEP_PULSE_WIDTH         (F_CPU * 0.000002) // 2uS w/ clk/1
 #define SEGMENT_MS               4
 #define SEGMENT_TIME             (SEGMENT_MS / 60000.0) // mins
 
index cee1e9c8a1a67791c9fa3ec3f836b3abc960f47f..4cc5191d226fdcb15bf73f6d193bee30d4d9870a 100644 (file)
@@ -311,19 +311,19 @@ void motor_prep_move(int motor, float target) {
   const float seg_clocks = SEGMENT_TIME * (F_CPU * 60 / 2);
   float ticks_per_step = seg_clocks / steps;
 
-  // Limit clock if step rate is too fast
-  // We allow a slight fudge here (i.e. 1.9 instead 2) because the motor driver
-  // seems to be able to handle it and otherwise we could not actually hit
-  // an average rate of 250k usteps/sec.
-  if (ticks_per_step < STEP_PULSE_WIDTH * 1.9)
-    ticks_per_step = STEP_PULSE_WIDTH * 1.9; // Too fast
-
   // Use faster clock with faster step rates for increased resolution.
   if (ticks_per_step < 0x7fff) {
     ticks_per_step *= 2;
     m.clock = TC_CLKSEL_DIV1_gc;
 
-  } else m.clock = TC_CLKSEL_DIV2_gc;
+    // Limit clock if step rate is too fast
+    // We allow a slight fudge here (i.e. 1.9 instead 2) because the motor
+    // driver is able to handle it and otherwise we could not actually hit
+    // an average rate of 250k usteps/sec.
+    if (ticks_per_step < STEP_PULSE_WIDTH * 1.9)
+      ticks_per_step = STEP_PULSE_WIDTH * 1.9; // Too fast
+
+  } else m.clock = TC_CLKSEL_DIV2_gc; // NOTE, pulse width will be twice as long
 
   // Disable clock if too slow
   if (0xffff <= ticks_per_step) ticks_per_step = 0;