#include "InterruptIn.h"
#include "PwmOut.h"
#include "port_api.h"
+#include "us_ticker_api.h"
#define spindle_enable_checksum CHECKSUM("spindle_enable")
#define spindle_pwm_pin_checksum CHECKSUM("spindle_pwm_pin")
#define spindle_control_P_checksum CHECKSUM("spindle_control_P")
#define spindle_control_I_checksum CHECKSUM("spindle_control_I")
#define spindle_control_D_checksum CHECKSUM("spindle_control_D")
+#define spindle_control_smoothing_checksum CHECKSUM("spindle_control_smoothing")
#define UPDATE_FREQ 1000
control_I_term = THEKERNEL->config->value(spindle_control_I_checksum)->by_default(0.0001f)->as_number();
control_D_term = THEKERNEL->config->value(spindle_control_D_checksum)->by_default(0.0001f)->as_number();
+ // Smoothing value is low pass filter time constant in seconds.
+ float smoothing_time = THEKERNEL->config->value(spindle_control_smoothing_checksum)->by_default(0.1f)->as_number();
+ if (smoothing_time * UPDATE_FREQ < 1.0f)
+ smoothing_decay = 1.0f;
+ else
+ smoothing_decay = 1.0f / (UPDATE_FREQ * smoothing_time);
+
// Get the pin for hardware pwm
{
Pin *smoothie_pin = new Pin();
PinName pinname = port_pin((PortName)smoothie_pin->port_number, smoothie_pin->pin);
feedback_pin = new mbed::InterruptIn(pinname);
feedback_pin->rise(this, &Spindle::on_pin_rise);
+ NVIC_SetPriority(EINT3_IRQn, 16);
}
else
{
delete smoothie_pin;
}
- SysTick_Config(SYSTICK_MAXCOUNT, false);
-
THEKERNEL->slow_ticker->attach(UPDATE_FREQ, this, &Spindle::on_update_speed);
register_for_event(ON_GCODE_RECEIVED);
register_for_event(ON_GCODE_EXECUTE);
void Spindle::on_pin_rise()
{
- uint32_t timestamp = SYSTICK_MAXCOUNT - SysTick->VAL;
- last_time = (timestamp - last_edge) & SYSTICK_MAXCOUNT;
+ uint32_t timestamp = us_ticker_read();
+ last_time = timestamp - last_edge;
last_edge = timestamp;
irq_count++;
}
// Calculate current RPM
uint32_t t = last_time;
if (t == 0)
+ {
current_rpm = 0;
+ }
else
- current_rpm = SystemCoreClock * 60.0f / (t * pulses_per_rev);
+ {
+ float new_rpm = 1000000 * 60.0f / (t * pulses_per_rev);
+ current_rpm = smoothing_decay * new_rpm + (1.0f - smoothing_decay) * current_rpm;
+ }
if (spindle_on)
{