Move most handling of steps from arm_solution,Robot,Planner to StepperMotor
[clinton/Smoothieware.git] / src / libs / StepperMotor.cpp
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 #include "mri.h"
8 #include "libs/Kernel.h"
9 #include "StepperMotor.h"
10 #include "MRI_Hooks.h"
11
12 // A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
13 // TODO : Abstract this into Actuator
14
15 StepperMotor::StepperMotor(){
16 this->moving = false;
17 this->paused = false;
18 this->fx_counter = 0;
19 this->stepped = 0;
20 this->fx_ticks_per_step = 0;
21 this->steps_to_move = 0;
22 this->remove_from_active_list_next_reset = false;
23 this->is_move_finished = false;
24 this->signal_step = false;
25 this->step_signal_hook = new Hook();
26
27 last_milestone_steps = 0;
28 last_milestone_mm = 0.0F;
29 }
30
31 StepperMotor::StepperMotor(Pin* step, Pin* dir, Pin* en) : step_pin(step), dir_pin(dir), en_pin(en) {
32 this->moving = false;
33 this->paused = false;
34 this->fx_counter = 0;
35 this->stepped = 0;
36 this->fx_ticks_per_step = 0;
37 this->steps_to_move = 0;
38 this->remove_from_active_list_next_reset = false;
39 this->is_move_finished = false;
40 this->signal_step = false;
41 this->step_signal_hook = new Hook();
42
43 set_high_on_debug(en->port_number, en->pin);
44
45 last_milestone_steps = 0;
46 last_milestone_mm = 0.0F;
47 }
48
49 // This is called ( see the .h file, we had to put a part of things there for obscure inline reasons ) when a step has to be generated
50 // we also here check if the move is finished etc ...
51 void StepperMotor::step(){
52
53 // output to pins 37t
54 this->step_pin->set( 1 );
55 this->step_ticker->reset_step_pins = true;
56
57 // move counter back 11t
58 this->fx_counter -= this->fx_ticks_per_step;
59
60 // we have moved a step 9t
61 this->stepped++;
62
63 // Do we need to signal this step
64 if( this->stepped == this->signal_step_number && this->signal_step ){
65 this->step_signal_hook->call();
66 }
67
68 // Is this move finished ?
69 if( this->stepped == this->steps_to_move ){
70 // Mark it as finished, then StepTicker will call signal_mode_finished()
71 // This is so we don't call that before all the steps have been generated for this tick()
72 this->is_move_finished = true;
73 this->step_ticker->moves_finished = true;
74 }
75
76 }
77
78
79 // If the move is finished, the StepTicker will call this ( because we asked it to in tick() )
80 void StepperMotor::signal_move_finished(){
81
82 // work is done ! 8t
83 this->moving = false;
84 this->steps_to_move = 0;
85
86 // signal it to whatever cares 41t 411t
87 this->end_hook->call();
88
89 // We only need to do this if we were not instructed to move
90 if( this->moving == false ){
91 this->update_exit_tick();
92 }
93
94 this->is_move_finished = false;
95 }
96
97 // This is just a way not to check for ( !this->moving || this->paused || this->fx_ticks_per_step == 0 ) at every tick()
98 inline void StepperMotor::update_exit_tick(){
99 if( !this->moving || this->paused || this->steps_to_move == 0 ){
100 // We must exit tick() after setting the pins, no bresenham is done
101 //this->remove_from_active_list_next_reset = true;
102 this->step_ticker->remove_motor_from_active_list(this);
103 }else{
104 // We must do the bresenham in tick()
105 // We have to do this or there could be a bug where the removal still happens when it doesn't need to
106 this->step_ticker->add_motor_to_active_list(this);
107 }
108 }
109
110
111
112 // Instruct the StepperMotor to move a certain number of steps
113 void StepperMotor::move( bool direction, unsigned int steps ){
114 // We do not set the direction directly, we will set the pin just before the step pin on the next tick
115 this->dir_pin->set(direction);
116
117 // How many steps we have to move until the move is done
118 this->steps_to_move = steps;
119
120 // Zero our tool counters
121 this->fx_counter = 0; // Bresenheim counter
122 this->stepped = 0;
123
124 // Do not signal steps until we get instructed to
125 this->signal_step = false;
126
127 // Starting now we are moving
128 if( steps > 0 ){
129 this->moving = true;
130 }else{
131 this->moving = false;
132 }
133 this->update_exit_tick();
134
135 }
136
137 // Set the speed at which this steper moves
138 void StepperMotor::set_speed( float speed ){
139
140 if (speed < 20.0)
141 speed = 20.0;
142
143 // How many steps we must output per second
144 this->steps_per_second = speed;
145
146 // How many ticks ( base steps ) between each actual step at this speed, in fixed point 64
147 float ticks_per_step = (float)( (float)this->step_ticker->frequency / speed );
148 float double_fx_ticks_per_step = (float)(1<<8) * ( (float)(1<<8) * ticks_per_step ); // 8x8 because we had to do 16x16 because 32 did not work
149 this->fx_ticks_per_step = (uint32_t)( floor(double_fx_ticks_per_step) );
150
151 }
152
153 // Pause this stepper motor
154 void StepperMotor::pause(){
155 this->paused = true;
156 this->update_exit_tick();
157 }
158
159 // Unpause this stepper motor
160 void StepperMotor::unpause(){
161 this->paused = false;
162 this->update_exit_tick();
163 }
164
165
166 void StepperMotor::change_steps_per_mm(float new_steps)
167 {
168 steps_per_mm = new_steps;
169 last_milestone_steps = lround(last_milestone_mm * steps_per_mm);
170 }
171
172 void StepperMotor::change_last_milestone(float new_milestone)
173 {
174 last_milestone_mm = new_milestone;
175 last_milestone_steps = lround(last_milestone_mm * steps_per_mm);
176 }
177
178 int StepperMotor::steps_to_target(float target)
179 {
180 int target_steps = target * steps_per_mm;
181 return target_steps - last_milestone_steps;
182 }