Merge remote-tracking branch 'upstream/edge' into upstreamedge
[clinton/Smoothieware.git] / src / libs / StepperMotor.h
CommitLineData
7b49793d 1/*
feb204be
AW
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.
7b49793d 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
feb204be
AW
6*/
7
a3bb687b 8#pragma once
feb204be 9
9e6014a6 10#include "Module.h"
5673fe39 11#include "Pin.h"
feb204be 12
9e6014a6 13class StepperMotor : public Module {
feb204be 14 public:
9c5fa39a 15 StepperMotor(Pin& step, Pin& dir, Pin& en);
3494f3d0 16 ~StepperMotor();
203213ae 17
d1d120e1 18 // called from step ticker ISR
c8bac202 19 inline bool step() { step_pin.set(1); current_position_steps += (direction?-1:1); return moving; }
d1d120e1 20 // called from unstep ISR
8b260c2c 21 inline void unstep() { step_pin.set(0); }
d1d120e1
JM
22 // called from step ticker ISR
23 inline void set_direction(bool f) { dir_pin.set(f); direction= f; }
9c5fa39a 24
203213ae 25 inline void enable(bool state) { en_pin.set(!state); };
9e6014a6 26 inline bool is_enabled() const { return !en_pin.get(); };
d1d120e1 27 inline bool is_moving() const { return moving; };
ad6a77d1 28 void start_moving() { moving= true; }
c8bac202 29 void stop_moving() { moving= false; }
9c5fa39a 30
58b69de8 31 void manual_step(bool dir);
9c5fa39a 32
eab91f11 33 bool which_direction() const { return direction; }
670fa10b 34
2fa50ca0 35 float get_steps_per_second() const { return steps_per_second; }
2fa50ca0 36 float get_steps_per_mm() const { return steps_per_mm; }
78d0e16a
MM
37 void change_steps_per_mm(float);
38 void change_last_milestone(float);
de2ee57c 39 void set_last_milestones(float, int32_t);
ad6a77d1 40 void update_last_milestones(float mm, int32_t steps);
90f70d46 41 float get_last_milestone(void) const { return last_milestone_mm; }
de2ee57c 42 int32_t get_last_milestone_steps(void) const { return last_milestone_steps; }
58c32991 43 float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
d1d120e1 44 uint32_t get_current_step(void) const { return current_position_steps; }
3494f3d0
JM
45 float get_max_rate(void) const { return max_rate; }
46 void set_max_rate(float mr) { max_rate= mr; }
29e809e0
JM
47 void set_acceleration(float a) { acceleration= a; }
48 float get_acceleration() const { return acceleration; }
49 bool is_selected() const { return selected; }
50 void set_selected(bool b) { selected= b; }
670fa10b 51
ad6a77d1 52 int32_t steps_to_target(float);
670fa10b 53
2fa50ca0
JM
54
55 private:
9e6014a6
JM
56 void on_halt(void *argument);
57 void on_enable(void *argument);
3494f3d0 58
1fce036c 59 int index;
670fa10b 60
9c5fa39a
MM
61 Pin step_pin;
62 Pin dir_pin;
63 Pin en_pin;
feb204be 64
1ad23cd3 65 float steps_per_second;
78d0e16a 66 float steps_per_mm;
3494f3d0 67 float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
29e809e0 68 float acceleration;
78d0e16a 69
58c32991 70 volatile int32_t current_position_steps;
78d0e16a
MM
71 int32_t last_milestone_steps;
72 float last_milestone_mm;
73
778093ce 74 volatile struct {
d1d120e1
JM
75 volatile bool direction:1;
76 volatile bool moving:1;
29e809e0 77 bool selected:1;
ec543686 78 };
feb204be
AW
79};
80