Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / modules / tools / spindle / Spindle.h
CommitLineData
65d97468
PA
1/*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6*/
7
8#ifndef SPINDLE_MODULE_H
9#define SPINDLE_MODULE_H
10
11#include "libs/Module.h"
12#include <stdint.h>
13
14namespace mbed {
15 class PwmOut;
16 class InterruptIn;
17}
18
19// This module implements closed loop PID control for spindle RPM.
20class Spindle: public Module {
21 public:
22 Spindle();
23 virtual ~Spindle() {};
24 void on_module_loaded();
25
26
27 private:
28 void on_pin_rise();
29 void on_gcode_received(void *argument);
30 void on_gcode_execute(void *argument);
31 uint32_t on_update_speed(uint32_t dummy);
32
33 mbed::PwmOut *spindle_pin; // PWM output for spindle speed control
34 mbed::InterruptIn *feedback_pin; // Interrupt pin for measuring speed
35 bool output_inverted;
36
37 // Current values, updated at runtime
38 bool spindle_on;
39 float current_rpm;
40 float target_rpm;
41 float current_I_value;
42 float prev_error;
43 float current_pwm_value;
44 int time_since_update;
45 uint32_t last_irq;
46
47 // Values from config
48 float pulses_per_rev;
49 float control_P_term;
50 float control_I_term;
51 float control_D_term;
5a9f7a28 52 float smoothing_decay;
65d97468
PA
53
54 // These fields are updated by the interrupt
55 uint32_t last_edge; // Timestamp of last edge
56 volatile uint32_t last_time; // Time delay between last two edges
57 volatile uint32_t irq_count;
58};
59
60#endif
61