Added support for 256 microstepping, Smoother operation at 250k step rate by doubling...
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 1 Sep 2018 22:20:50 +0000 (15:20 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 1 Sep 2018 22:31:25 +0000 (15:31 -0700)
CHANGELOG.md
src/avr/src/config.h
src/avr/src/motor.c
src/resources/config-template.json

index f0c8e0832e957b69fae8ef4e60e4face0e61f1b0..a0ec682d4d850f0a12a60e97b24dc5e3f52e11aa 100644 (file)
@@ -3,6 +3,8 @@ Buildbotics CNC Controller Firmware Changelog
 
 ## v0.3.29
  - Increased display precision of position and motor config.
+ - Added support for 256 microstepping.
+ - Smoother operation at 250k step rate by doubling clock as needed.
 
 ## v0.3.28
  - Show step rate on motor configuration page.
index 18d7348067e8f0183152009657fbb9c27570c8f1..97570c2bfd344c626f1c44ca68479cc73e9f888a 100644 (file)
@@ -169,7 +169,7 @@ enum {
                                   DRV8711_DRIVE_TDRIVEN_250 | \
                                   DRV8711_DRIVE_OCPDEG_1    | \
                                   DRV8711_DRIVE_OCPTH_250)
-#define DRV8711_TORQUE            DRV8711_TORQUE_SMPLTH_200
+#define DRV8711_TORQUE            DRV8711_TORQUE_SMPLTH_50
 #define DRV8711_CTRL             (DRV8711_CTRL_ISGAIN_10 | \
                                   DRV8711_CTRL_DTIME_450 | \
                                   DRV8711_CTRL_EXSTALL_bm)
index 832c94fe6516a43b0c88dc97de171bee4f663683..b8617c097b0d94cf4da7f0773099f6a119f765ea 100644 (file)
@@ -78,6 +78,7 @@ typedef struct {
 
   // Move prep
   bool prepped;
+  uint8_t clock;
   uint16_t timer_period;
   bool negative;
   int32_t position;
@@ -280,10 +281,10 @@ void motor_load_move(int motor) {
   // interrupting the clock mid step or causing counter wrap around.
 
   // Set clock and period
-  m->timer->CTRLA = TC_CLKSEL_DIV2_gc; // Start clock
+  m->timer->CTRLA  = m->clock;         // Start clock
   m->timer->PERBUF = m->timer_period;  // Set next frequency
   m->last_negative = m->negative;
-  m->commanded = m->position;
+  m->commanded     = m->position;
 }
 
 
@@ -314,20 +315,28 @@ void motor_prep_move(int motor, float target) {
   m->negative = steps < 0;
   if (m->negative) steps = -steps;
 
-  // We use clock / 2
-  float seg_clocks = SEGMENT_TIME * (F_CPU * 60 / 2);
-  float ticks_per_step = round(seg_clocks / steps);
+  // Start with clock / 2
+  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, disable if too slow
+  // 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)
-    m->timer_period = STEP_PULSE_WIDTH * 1.9;             // Too fast
-  else if (0xffff <= ticks_per_step) m->timer_period = 0; // Too slow
-  else m->timer_period = ticks_per_step;                  // Just right
+    ticks_per_step = STEP_PULSE_WIDTH * 1.9; // Too fast
 
-  if (!steps) m->timer_period = 0;
+  // 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;
+
+  // Disable clock if too slow
+  if (0xffff <= ticks_per_step) ticks_per_step = 0;
+
+  m->timer_period = steps ? round(ticks_per_step) : 0;
 
   // Power motor
   switch (m->power_mode) {
index 77e2f0cb4e986354848db5e70472b4a69076db94..34f32082bfee72fe60837be0b79e0ca82e55e515 100644 (file)
@@ -58,7 +58,7 @@
         },
         "microsteps": {
           "type": "int",
-          "values": [1, 2, 4, 8, 16, 32, 64, 128],
+          "values": [1, 2, 4, 8, 16, 32, 64, 128, 256],
           "unit": "per full step",
           "default": 32,
           "code": "mi"