From ed04bd02e27dabccf114ee4c10a88865fd1ec369 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Mon, 5 Jul 2021 20:31:08 +0200 Subject: [PATCH] PulseGen/speed_tables: cleanup constants --- src/config/config.h | 10 +++++++- src/config/todo.h | 14 ----------- src/hal/cpu.h | 5 ++++ src/modules/pulse_gen.cpp | 25 +++++++++---------- src/modules/pulse_gen.h | 8 ++---- src/modules/speed_table.h | 13 +++++++--- .../unit/modules/pulse_gen/test_pulse_gen.cpp | 3 +-- 7 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 src/config/todo.h diff --git a/src/config/config.h b/src/config/config.h index c396067..b5c139c 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -1,7 +1,6 @@ #pragma once #include #include "axis.h" -#include "todo.h" /// Wrangler for assorted compile-time configuration and constants. namespace config { @@ -33,6 +32,15 @@ static constexpr const uint8_t buttonsADCIndex = 0; ///< ADC index of buttons in /// microstepping interval. static constexpr uint8_t uStepMaxRes = 32; +/// Do not plan moves equal or shorter than the requested steps +static constexpr uint8_t dropSegments = 0; + +/// Max step frequency 40KHz +static constexpr uint16_t maxStepFrequency = 40000; + +/// Minimum stepping rate 120Hz +static constexpr uint16_t minStepRate = 120; + /// Idler configuration static constexpr AxisConfig idler = { .dirOn = true, diff --git a/src/config/todo.h b/src/config/todo.h deleted file mode 100644 index 3331837..0000000 --- a/src/config/todo.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#ifndef __AVR__ - #define F_CPU 16000000 -#endif - -// Max step frequency 40KHz -#define MAX_STEP_FREQUENCY 40000 - -// Minimum stepper rate 120Hz. -#define MINIMAL_STEP_RATE 120 - -// Step frequency divider (influences the speed tables!) -#define STEP_TIMER_DIVIDER 8 diff --git a/src/hal/cpu.h b/src/hal/cpu.h index 9775408..a1713a1 100644 --- a/src/hal/cpu.h +++ b/src/hal/cpu.h @@ -5,6 +5,11 @@ namespace hal { namespace cpu { +#ifndef F_CPU + /// Main clock frequency + #define F_CPU (16000000ul) +#endif + /// CPU init routines (not really necessary for the AVR) void Init(); diff --git a/src/modules/pulse_gen.cpp b/src/modules/pulse_gen.cpp index 500c5a8..0c94e35 100644 --- a/src/modules/pulse_gen.cpp +++ b/src/modules/pulse_gen.cpp @@ -9,16 +9,15 @@ using modules::speed_table::calc_timer; namespace modules { namespace pulse_gen { -PulseGen::PulseGen() { - // Some initial values +PulseGen::PulseGen(steps_t max_jerk, steps_t acceleration) { + // Axis status position = 0; - acceleration = 1200; + this->max_jerk = max_jerk; + this->acceleration = acceleration; + + // Block buffer block_buffer_head = block_buffer_tail = 0; current_block = nullptr; - - // TODO: configuration constants - dropsegments = 5; - max_jerk = 100; } void PulseGen::CalculateTrapezoid(block_t *block, steps_t entry_speed, steps_t exit_speed) { @@ -28,12 +27,12 @@ void PulseGen::CalculateTrapezoid(block_t *block, steps_t entry_speed, steps_t e rate_t final_rate = exit_speed; // Limit minimal step rate (Otherwise the timer will overflow.) - if (initial_rate < MINIMAL_STEP_RATE) - initial_rate = MINIMAL_STEP_RATE; + if (initial_rate < config::minStepRate) + initial_rate = config::minStepRate; if (initial_rate > block->nominal_rate) initial_rate = block->nominal_rate; - if (final_rate < MINIMAL_STEP_RATE) - final_rate = MINIMAL_STEP_RATE; + if (final_rate < config::minStepRate) + final_rate = config::minStepRate; if (final_rate > block->nominal_rate) final_rate = block->nominal_rate; @@ -100,7 +99,7 @@ void PulseGen::Move(pos_t target, steps_t feed_rate) { block->steps = abs(target - position); // Bail if this is a zero-length block - if (block->steps <= dropsegments) + if (block->steps <= config::dropSegments) return; // Direction and speed for this block @@ -109,7 +108,7 @@ void PulseGen::Move(pos_t target, steps_t feed_rate) { // Acceleration of the segment, in steps/sec^2 block->acceleration = acceleration; - block->acceleration_rate = block->acceleration * (rate_t)((float)F_CPU / (F_CPU / STEP_TIMER_DIVIDER)); + block->acceleration_rate = block->acceleration * (rate_t)((float)F_CPU / (F_CPU / speed_table::cpuFrequencyDivider)); // Perform the trapezoid calculations CalculateTrapezoid(block, max_jerk, max_jerk); diff --git a/src/modules/pulse_gen.h b/src/modules/pulse_gen.h index 11149d1..5485dec 100644 --- a/src/modules/pulse_gen.h +++ b/src/modules/pulse_gen.h @@ -15,7 +15,7 @@ typedef int32_t pos_t; ///< Axis position (signed) class PulseGen { public: - PulseGen(); + PulseGen(steps_t max_jerk, steps_t acceleration); /// @returns the acceleration for the axis steps_t Acceleration() const { return acceleration; }; @@ -59,11 +59,6 @@ private: rate_t acceleration; ///< acceleration steps/sec^2 }; - //{ units constants - steps_t max_jerk; - steps_t dropsegments; // segments are dropped if lower than that - //} - // Block buffer parameters block_t block_buffer[2]; block_t *current_block; @@ -72,6 +67,7 @@ private: // Axis data pos_t position; ///< Current axis position + steps_t max_jerk; ///< Axis jerk (could be constant) steps_t acceleration; ///< Current axis acceleration // Step parameters diff --git a/src/modules/speed_table.h b/src/modules/speed_table.h index 667c0a2..6c88cf1 100644 --- a/src/modules/speed_table.h +++ b/src/modules/speed_table.h @@ -1,6 +1,7 @@ #pragma once #include "../config/config.h" #include "../hal/progmem.h" +#include "../hal/cpu.h" #include "math.h" namespace modules { @@ -10,6 +11,11 @@ namespace speed_table { typedef uint16_t st_timer_t; +/// CPU timer frequency divider required for the speed tables +static constexpr uint8_t cpuFrequencyDivider = 8; +static_assert(F_CPU / cpuFrequencyDivider == 2000000, + "speed tables not compatible for the requested frequency"); + /// Lookup table for rates equal or higher than 8*256 extern const st_timer_t speed_table_fast[256][2] PROGMEM; @@ -18,8 +24,8 @@ extern const st_timer_t speed_table_slow[256][2] PROGMEM; /// Calculate the next timer interval and steps according to current step rate static inline st_timer_t calc_timer(st_timer_t step_rate, uint8_t &step_loops) { - if (step_rate > MAX_STEP_FREQUENCY) - step_rate = MAX_STEP_FREQUENCY; + if (step_rate > config::maxStepFrequency) + step_rate = config::maxStepFrequency; if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times step_rate = (step_rate >> 2) & 0x3fff; step_loops = 4; @@ -51,8 +57,9 @@ static inline st_timer_t calc_timer(st_timer_t step_rate, uint8_t &step_loops) { timer -= ((pm::read_word(table_address + 1) * (uint8_t)(step_rate & 0x0007)) >> 3); } if (timer < 100) { + // 20kHz this should never happen timer = 100; - } // 20kHz this should never happen + } return timer; } diff --git a/tests/unit/modules/pulse_gen/test_pulse_gen.cpp b/tests/unit/modules/pulse_gen/test_pulse_gen.cpp index 152f77c..194c685 100644 --- a/tests/unit/modules/pulse_gen/test_pulse_gen.cpp +++ b/tests/unit/modules/pulse_gen/test_pulse_gen.cpp @@ -18,8 +18,7 @@ TEST_CASE("pulse_gen::basic", "[pulse_gen]") { }; for (int accel = 100; accel <= 5000; accel *= 2) { - PulseGen pg; - pg.SetAcceleration(accel); + PulseGen pg(10, accel); pg.Move(100000, 10000); unsigned long ts = 0;