38a315f21189558be31122259040cb4784f30c62
[clinton/Smoothieware.git] / src / libs / StepperMotor.h
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 #pragma once
9
10 #include "Pin.h"
11 #include <atomic>
12 #include <functional>
13
14 class StepTicker;
15 class Hook;
16
17 class StepperMotor {
18 public:
19 StepperMotor();
20 StepperMotor(Pin& step, Pin& dir, Pin& en);
21 ~StepperMotor();
22
23 inline void step() { current_position_steps += (direction?-1:1); step_pin.set( 1 ); }
24 inline void unstep() { step_pin.set(0); }
25 inline void set_direction(bool f) { direction= f; dir_pin.set(f); }
26
27 inline void enable(bool state) { en_pin.set(!state); };
28
29 bool which_direction() const { return direction; }
30
31 float get_steps_per_second() const { return steps_per_second; }
32 float get_steps_per_mm() const { return steps_per_mm; }
33 void change_steps_per_mm(float);
34 void change_last_milestone(float);
35 float get_last_milestone(void) const { return last_milestone_mm; }
36 float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
37 float get_max_rate(void) const { return max_rate; }
38 void set_max_rate(float mr) { max_rate= mr; }
39
40 int steps_to_target(float);
41
42 friend class StepTicker;
43 friend class Stepper;
44 friend class Planner;
45 friend class Robot;
46
47 private:
48 void init();
49
50 int index;
51
52 Pin step_pin;
53 Pin dir_pin;
54 Pin en_pin;
55
56 float steps_per_second;
57 float steps_per_mm;
58 float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
59 float minimum_step_rate; // this is the minimum step_rate in steps/sec for this motor for this block
60 static float default_minimum_actuator_rate;
61
62 volatile int32_t current_position_steps;
63 int32_t last_milestone_steps;
64 float last_milestone_mm;
65
66 volatile struct {
67 bool direction:1;
68 };
69 };
70