get rid of #define max and use stl::max
[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
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 void step();
24 inline void unstep() { step_pin.set(0); };
25
26 inline void enable(bool state) { en_pin.set(!state); };
27
28 bool is_moving() { return moving; }
29 void move_finished();
30 void move( bool direction, unsigned int steps, float initial_speed= -1.0F);
31 void signal_move_finished();
32 void set_speed( float speed );
33 void set_step_rate(float requested_rate, uint32_t block_steps_event_count);
34
35 void update_exit_tick();
36 void pause();
37 void unpause();
38
39 float get_steps_per_second() const { return steps_per_second; }
40 float get_steps_per_mm() const { return steps_per_mm; }
41 void change_steps_per_mm(float);
42 void change_last_milestone(float);
43 float get_last_milestone(void) const { return last_milestone_mm; }
44 float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
45 float get_max_rate(void) const { return max_rate; }
46 void set_max_rate(float mr) { max_rate= mr; }
47 float get_min_rate(void) const { return minimum_step_rate; }
48 void set_min_rate(float mr) { minimum_step_rate= mr; }
49
50 int steps_to_target(float);
51 uint32_t get_steps_to_move() const { return steps_to_move; }
52 uint32_t get_stepped() const { return stepped; }
53
54 template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
55 Hook* hook = new Hook();
56 hook->attach(optr, fptr);
57 this->end_hook = hook;
58 }
59
60 template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
61 this->step_signal_hook->attach(optr, fptr);
62 this->signal_step_number = step;
63 this->signal_step = true;
64 }
65
66 friend class StepTicker;
67 friend class Stepper;
68 friend class Planner;
69 friend class Robot;
70
71 private:
72 void init();
73
74 Hook* end_hook;
75 Hook* step_signal_hook;
76
77 uint32_t signal_step_number;
78
79 Pin step_pin;
80 Pin dir_pin;
81 Pin en_pin;
82
83 float steps_per_second;
84 float steps_per_mm;
85 float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
86 float minimum_step_rate; // this is the minimum step_rate in steps/sec for this motor for this block
87 static float default_minimum_actuator_rate;
88
89 volatile int32_t current_position_steps;
90 int32_t last_milestone_steps;
91 float last_milestone_mm;
92
93 uint32_t steps_to_move;
94 uint32_t stepped;
95
96 // set to 64 bit fixed point, 32:32 bits fractional
97 static const uint32_t fx_shift= 32;
98 static const uint64_t fx_increment= ((uint64_t)1<<fx_shift);
99 uint64_t fx_counter;
100 uint64_t fx_ticks_per_step;
101
102 struct {
103 bool direction:1;
104 bool is_move_finished:1; // Whether the move just finished
105 bool signal_step:1;
106 bool paused:1;
107 volatile bool moving:1;
108 };
109
110 // Called a great many times per second, to step if we have to now
111 inline void tick() {
112 // increase the ( 64 fixed point 32:32 ) counter by one tick 11t
113 fx_counter += fx_increment;
114
115 // if we are to step now 10t
116 if (fx_counter >= fx_ticks_per_step)
117 step();
118 };
119 };
120
121 #endif
122