get direct step running from encoder
[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 #ifndef STEPPERMOTOR_H
9 #define STEPPERMOTOR_H
10
11 #include "libs/Hook.h"
12 #include "Pin.h"
13 #include <atomic>
14 #include <functional>
15
16 class StepTicker;
17 class Hook;
18
19 class StepperMotor {
20 public:
21 StepperMotor();
22 StepperMotor(Pin& step, Pin& dir, Pin& en);
23 ~StepperMotor();
24
25 void step();
26 inline void unstep() { step_pin.set(0); };
27 void manual_step(bool dir);
28
29 inline void enable(bool state) { enabled= state; en_pin.set(!state); };
30
31 bool is_moving() const { return moving; }
32 bool which_direction() const { return direction; }
33 void move_finished();
34 StepperMotor* move( bool direction, unsigned int steps, float initial_speed= -1.0F);
35 void signal_move_finished();
36 StepperMotor* set_speed( float speed );
37 void set_moved_last_block(bool flg) { last_step_tick_valid= flg; }
38 void update_exit_tick();
39
40 float get_steps_per_second() const { return steps_per_second; }
41 float get_steps_per_mm() const { return steps_per_mm; }
42 void change_steps_per_mm(float);
43 void change_last_milestone(float);
44 float get_last_milestone(void) const { return last_milestone_mm; }
45 float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
46 float get_max_rate(void) const { return max_rate; }
47 void set_max_rate(float mr) { max_rate= mr; }
48 float get_min_rate(void) const { return minimum_step_rate; }
49 void set_min_rate(float mr) { minimum_step_rate= mr; }
50
51 int steps_to_target(float);
52 uint32_t get_steps_to_move() const { return steps_to_move; }
53 uint32_t get_stepped() const { return stepped; }
54 void force_finish_move() { force_finish= true; }
55
56 template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
57 Hook* hook = new Hook();
58 hook->attach(optr, fptr);
59 this->end_hook = hook;
60 }
61
62 friend class StepTicker;
63 friend class Stepper;
64 friend class Planner;
65 friend class Robot;
66
67 private:
68 void init();
69
70 int index;
71 Hook* end_hook;
72
73 Pin step_pin;
74 Pin dir_pin;
75 Pin en_pin;
76
77 float steps_per_second;
78 float steps_per_mm;
79 float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
80 float minimum_step_rate; // this is the minimum step_rate in steps/sec for this motor for this block
81 static float default_minimum_actuator_rate;
82
83 volatile int32_t current_position_steps;
84 int32_t last_milestone_steps;
85 float last_milestone_mm;
86
87 uint32_t steps_to_move;
88 uint32_t stepped;
89 uint32_t last_step_tick;
90 uint32_t signal_step;
91
92 // set to 32 bit fixed point, 18:14 bits fractional
93 static const uint32_t fx_shift= 14;
94 static const uint32_t fx_increment= ((uint32_t)1<<fx_shift);
95 uint32_t fx_counter;
96 uint32_t fx_ticks_per_step;
97
98 volatile struct {
99 volatile bool is_move_finished:1; // Whether the move just finished
100 volatile bool moving:1;
101 volatile bool force_finish:1; // set to force a move to finish early
102 bool direction:1;
103 bool last_step_tick_valid:1; // set if the last step tick time is valid (ie the motor moved last block)
104 bool enabled:1;
105 };
106
107 // Called a great many times per second, to step if we have to now
108 inline bool tick() {
109 // increase the ( 32 fixed point 18:14 ) counter by one tick 11t
110 fx_counter += fx_increment;
111
112 // if we are to step now
113 if (fx_counter >= fx_ticks_per_step){
114 step();
115 return true;
116 }
117 return false;
118 };
119 };
120
121 #endif
122