Merge remote-tracking branch 'upstream/edge' into upstream-master
[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
8#ifndef STEPPERMOTOR_H
9#define STEPPERMOTOR_H
10
feb204be 11#include "libs/Hook.h"
5673fe39 12#include "Pin.h"
cb2e6bc6 13#include <atomic>
398e9edc 14#include <functional>
feb204be
AW
15
16class StepTicker;
5673fe39 17class Hook;
feb204be 18
670fa10b 19class StepperMotor {
feb204be 20 public:
670fa10b 21 StepperMotor();
9c5fa39a 22 StepperMotor(Pin& step, Pin& dir, Pin& en);
3494f3d0 23 ~StepperMotor();
203213ae
MM
24
25 void step();
26 inline void unstep() { step_pin.set(0); };
9c5fa39a 27
203213ae 28 inline void enable(bool state) { en_pin.set(!state); };
9c5fa39a 29
eab91f11
JM
30 bool is_moving() const { return moving; }
31 bool which_direction() const { return direction; }
670fa10b 32 void move_finished();
c6796a29 33 StepperMotor* move( bool direction, unsigned int steps, float initial_speed= -1.0F);
bd0f7508 34 void signal_move_finished();
c6796a29
JM
35 StepperMotor* set_speed( float speed );
36 void set_moved_last_block(bool flg) { last_step_tick_valid= flg; }
672298b2 37 void update_exit_tick();
670fa10b 38
2fa50ca0 39 float get_steps_per_second() const { return steps_per_second; }
2fa50ca0 40 float get_steps_per_mm() const { return steps_per_mm; }
78d0e16a
MM
41 void change_steps_per_mm(float);
42 void change_last_milestone(float);
90f70d46 43 float get_last_milestone(void) const { return last_milestone_mm; }
58c32991 44 float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
3494f3d0
JM
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; }
670fa10b 49
78d0e16a 50 int steps_to_target(float);
2fa50ca0
JM
51 uint32_t get_steps_to_move() const { return steps_to_move; }
52 uint32_t get_stepped() const { return stepped; }
670fa10b
L
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
2fa50ca0
JM
60 friend class StepTicker;
61 friend class Stepper;
62 friend class Planner;
63 friend class Robot;
64
65 private:
728477c4 66 void init();
3494f3d0 67
1fce036c 68 int index;
a157d099 69 Hook* end_hook;
670fa10b 70
9c5fa39a
MM
71 Pin step_pin;
72 Pin dir_pin;
73 Pin en_pin;
feb204be 74
1ad23cd3 75 float steps_per_second;
78d0e16a 76 float steps_per_mm;
3494f3d0
JM
77 float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
78 float minimum_step_rate; // this is the minimum step_rate in steps/sec for this motor for this block
9502f9d5 79 static float default_minimum_actuator_rate;
78d0e16a 80
58c32991 81 volatile int32_t current_position_steps;
78d0e16a
MM
82 int32_t last_milestone_steps;
83 float last_milestone_mm;
84
670fa10b
L
85 uint32_t steps_to_move;
86 uint32_t stepped;
c6796a29 87 uint32_t last_step_tick;
398e9edc 88 uint32_t signal_step;
3eadcfee 89
cb2e6bc6
JM
90 // set to 32 bit fixed point, 18:14 bits fractional
91 static const uint32_t fx_shift= 14;
c6796a29 92 static const uint32_t fx_increment= ((uint32_t)1<<fx_shift);
cb2e6bc6
JM
93 uint32_t fx_counter;
94 uint32_t fx_ticks_per_step;
df6a30f2 95
728477c4
JM
96 struct {
97 bool direction:1;
f095cddd 98 volatile bool is_move_finished:1; // Whether the move just finished
728477c4 99 volatile bool moving:1;
c6796a29 100 bool last_step_tick_valid:1; // set if the last step tick time is valid (ie the motor moved last block)
728477c4 101 };
ec543686
JM
102
103 // Called a great many times per second, to step if we have to now
aed1f6ca 104 inline bool tick() {
cb2e6bc6 105 // increase the ( 32 fixed point 18:14 ) counter by one tick 11t
3eadcfee 106 fx_counter += fx_increment;
ec543686 107
cb2e6bc6 108 // if we are to step now
aed1f6ca 109 if (fx_counter >= fx_ticks_per_step){
ec543686 110 step();
aed1f6ca
JM
111 return true;
112 }
113 return false;
ec543686 114 };
feb204be
AW
115};
116
feb204be
AW
117#endif
118