Merge branch 'edge' into accel
[clinton/Smoothieware.git] / src / modules / robot / Stepper.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) with additions from Sungeun K. Jeon (https://github.com/chamnit/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 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "Stepper.h"
11 #include "Planner.h"
12 #include "Conveyor.h"
13 #include <vector>
14 using namespace std;
15 #include "libs/nuts_bolts.h"
16 #include "libs/Hook.h"
17 #include <mri.h>
18
19
20 Stepper* stepper;
21 uint32_t previous_step_count;
22 uint32_t skipped_speed_updates;
23 uint32_t speed_ticks_counter;
24
25
26 Stepper::Stepper(){
27 this->current_block = NULL;
28 this->paused = false;
29 this->trapezoid_generator_busy = false;
30 this->force_speed_update = false;
31 skipped_speed_updates = 0;
32 }
33
34 //Called when the module has just been loaded
35 void Stepper::on_module_loaded(){
36 stepper = this;
37 register_for_event(ON_CONFIG_RELOAD);
38 this->register_for_event(ON_BLOCK_BEGIN);
39 this->register_for_event(ON_BLOCK_END);
40 this->register_for_event(ON_GCODE_EXECUTE);
41 this->register_for_event(ON_PLAY);
42 this->register_for_event(ON_PAUSE);
43
44 // Get onfiguration
45 this->on_config_reload(this);
46
47 // Acceleration ticker
48 this->acceleration_tick_hook = this->kernel->slow_ticker->attach( this->acceleration_ticks_per_second, this, &Stepper::trapezoid_generator_tick );
49
50 // Attach to the end_of_move stepper event
51 this->kernel->robot->alpha_stepper_motor->attach(this, &Stepper::stepper_motor_finished_move );
52 this->kernel->robot->beta_stepper_motor->attach( this, &Stepper::stepper_motor_finished_move );
53 this->kernel->robot->gamma_stepper_motor->attach(this, &Stepper::stepper_motor_finished_move );
54 }
55
56 // Get configuration from the config file
57 void Stepper::on_config_reload(void* argument){
58
59 this->acceleration_ticks_per_second = this->kernel->config->value(acceleration_ticks_per_second_checksum)->by_default(100 )->as_number();
60 this->minimum_steps_per_minute = this->kernel->config->value(minimum_steps_per_minute_checksum )->by_default(3000 )->as_number();
61
62 // Steppers start off by default
63 this->turn_enable_pins_off();
64 }
65
66 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
67 void Stepper::on_pause(void* argument){
68 this->paused = true;
69 this->kernel->robot->alpha_stepper_motor->pause();
70 this->kernel->robot->beta_stepper_motor->pause();
71 this->kernel->robot->gamma_stepper_motor->pause();
72 }
73
74 // When the play/pause button is set to play, or a module calls the ON_PLAY event
75 void Stepper::on_play(void* argument){
76 // TODO: Re-compute the whole queue for a cold-start
77 this->paused = false;
78 this->kernel->robot->alpha_stepper_motor->unpause();
79 this->kernel->robot->beta_stepper_motor->unpause();
80 this->kernel->robot->gamma_stepper_motor->unpause();
81 }
82
83
84 void Stepper::on_gcode_execute(void* argument){
85 Gcode* gcode = static_cast<Gcode*>(argument);
86
87 if( gcode->has_m){
88 if( gcode->m == 17 ){
89 this->turn_enable_pins_on();
90 }
91 if( gcode->m == 84 || gcode->m == 18 ){
92 this->turn_enable_pins_off();
93 }
94 }
95 }
96
97 void Stepper::turn_enable_pins_on(){
98 this->kernel->robot->alpha_en_pin.set(0);
99 this->kernel->robot->beta_en_pin.set(0);
100 this->kernel->robot->gamma_en_pin.set(0);
101 this->enable_pins_status = true;
102 }
103
104 void Stepper::turn_enable_pins_off(){
105 this->kernel->robot->alpha_en_pin.set(1);
106 this->kernel->robot->beta_en_pin.set(1);
107 this->kernel->robot->gamma_en_pin.set(1);
108 this->enable_pins_status = false;
109 }
110
111 // A new block is popped from the queue
112 void Stepper::on_block_begin(void* argument){
113 Block* block = static_cast<Block*>(argument);
114
115 // The stepper does not care about 0-blocks
116 if( block->millimeters == 0.0 ){ return; }
117
118 // Mark the new block as of interrest to us
119 if( block->steps[ALPHA_STEPPER] > 0 || block->steps[BETA_STEPPER] > 0 || block->steps[GAMMA_STEPPER] > 0 ){
120 block->take();
121 }else{
122 return;
123 }
124
125 // We can't move with the enable pins off
126 if( this->enable_pins_status == false ){
127 this->turn_enable_pins_on();
128 }
129
130 // Setup : instruct stepper motors to move
131 if( block->steps[ALPHA_STEPPER] > 0 ){ this->kernel->robot->alpha_stepper_motor->move( ( block->direction_bits >> 0 ) & 1 , block->steps[ALPHA_STEPPER] ); }
132 if( block->steps[BETA_STEPPER ] > 0 ){ this->kernel->robot->beta_stepper_motor->move( ( block->direction_bits >> 1 ) & 1 , block->steps[BETA_STEPPER ] ); }
133 if( block->steps[GAMMA_STEPPER] > 0 ){ this->kernel->robot->gamma_stepper_motor->move( ( block->direction_bits >> 2 ) & 1 , block->steps[GAMMA_STEPPER] ); }
134
135 this->current_block = block;
136
137 // Setup acceleration for this block
138 this->trapezoid_generator_reset();
139
140 // Find the stepper with the more steps, it's the one the speed calculations will want to follow
141 this->main_stepper = this->kernel->robot->alpha_stepper_motor;
142 if( this->kernel->robot->beta_stepper_motor->steps_to_move > this->main_stepper->steps_to_move ){ this->main_stepper = this->kernel->robot->beta_stepper_motor; }
143 if( this->kernel->robot->gamma_stepper_motor->steps_to_move > this->main_stepper->steps_to_move ){ this->main_stepper = this->kernel->robot->gamma_stepper_motor; }
144
145
146 this->trapezoid_generator_tick(0);
147
148 // Synchronise the acceleration curve with the stepping
149 this->synchronize_acceleration(0);
150
151 }
152
153 // Current block is discarded
154 void Stepper::on_block_end(void* argument){
155 this->current_block = NULL; //stfu !
156 }
157
158 //#pragma GCC push_options
159 //#pragma GCC optimize ("O0")
160
161 // When a stepper motor has finished it's assigned movement
162 uint32_t Stepper::stepper_motor_finished_move(uint32_t dummy){
163
164 // We care only if none is still moving
165 if( this->kernel->robot->alpha_stepper_motor->moving || this->kernel->robot->beta_stepper_motor->moving || this->kernel->robot->gamma_stepper_motor->moving ){ return 0; }
166
167 // This block is finished, release it
168 if( this->current_block != NULL ){
169 this->current_block->release();
170 }
171
172 return 0;
173 }
174
175
176 // This is called ACCELERATION_TICKS_PER_SECOND times per second by the step_event
177 // interrupt. It can be assumed that the trapezoid-generator-parameters and the
178 // current_block stays untouched by outside handlers for the duration of this function call.
179 uint32_t Stepper::trapezoid_generator_tick( uint32_t dummy ) {
180
181 LPC_GPIO1->FIOSET = 1<<31;
182
183 if(this->current_block && !this->paused && this->main_stepper->moving ) {
184 uint32_t current_steps_completed = this->main_stepper->stepped;
185
186 if( this->force_speed_update ){
187 this->force_speed_update = false;
188 this->set_step_events_per_minute(this->trapezoid_adjusted_rate);
189 LPC_GPIO1->FIOCLR = 1<<31;
190 return 0;
191 }
192
193 if(current_steps_completed <= this->current_block->accelerate_until + 1) {
194 this->trapezoid_adjusted_rate += this->current_block->rate_delta;
195
196 if (this->trapezoid_adjusted_rate > this->current_block->nominal_rate ) {
197 this->trapezoid_adjusted_rate = this->current_block->nominal_rate;
198 }
199 this->set_step_events_per_minute(this->trapezoid_adjusted_rate);
200 }else if (current_steps_completed > this->current_block->decelerate_after) {
201 // NOTE: We will only reduce speed if the result will be > 0. This catches small
202 // rounding errors that might leave steps hanging after the last trapezoid tick.
203 if(this->trapezoid_adjusted_rate > this->current_block->rate_delta * 1.5) {
204 this->trapezoid_adjusted_rate -= this->current_block->rate_delta;
205 }else{
206 this->trapezoid_adjusted_rate = this->current_block->rate_delta * 1.5;
207 }
208 if(this->trapezoid_adjusted_rate < this->current_block->final_rate ) {
209 this->trapezoid_adjusted_rate = this->current_block->final_rate;
210 }
211 this->set_step_events_per_minute(this->trapezoid_adjusted_rate);
212 }else {
213 // Make sure we cruise at exactly nominal rate
214 if (this->trapezoid_adjusted_rate != this->current_block->nominal_rate) {
215 this->trapezoid_adjusted_rate = this->current_block->nominal_rate;
216 this->set_step_events_per_minute(this->trapezoid_adjusted_rate);
217 }
218 }
219 }
220
221 LPC_GPIO1->FIOCLR = 1<<31;
222 return 0;
223 }
224
225
226
227 // Initializes the trapezoid generator from the current block. Called whenever a new
228 // block begins.
229 inline void Stepper::trapezoid_generator_reset(){
230 this->trapezoid_adjusted_rate = this->current_block->initial_rate;
231 this->force_speed_update = true;
232 this->trapezoid_tick_cycle_counter = 0;
233 previous_step_count = 0;
234 skipped_speed_updates = 0;
235 speed_ticks_counter = 0;
236 }
237
238 // Update the speed for all steppers
239 void Stepper::set_step_events_per_minute( double steps_per_minute ){
240
241 // We do not step slower than this
242 //steps_per_minute = max(steps_per_minute, this->minimum_steps_per_minute);
243 if( steps_per_minute < this->minimum_steps_per_minute ){
244 steps_per_minute = this->minimum_steps_per_minute;
245 }
246
247
248 // Instruct the stepper motors
249 if( this->kernel->robot->alpha_stepper_motor->moving ){ this->kernel->robot->alpha_stepper_motor->set_speed( (steps_per_minute/60L) * ( (double)this->current_block->steps[ALPHA_STEPPER] / (double)this->current_block->steps_event_count ) ); }
250 if( this->kernel->robot->beta_stepper_motor->moving ){ this->kernel->robot->beta_stepper_motor->set_speed( (steps_per_minute/60L) * ( (double)this->current_block->steps[BETA_STEPPER ] / (double)this->current_block->steps_event_count ) ); }
251 if( this->kernel->robot->gamma_stepper_motor->moving ){ this->kernel->robot->gamma_stepper_motor->set_speed( (steps_per_minute/60L) * ( (double)this->current_block->steps[GAMMA_STEPPER] / (double)this->current_block->steps_event_count ) ); }
252
253 this->kernel->call_event(ON_SPEED_CHANGE, this);
254
255 }
256
257 // This function has the role of making sure acceleration and deceleration curves have their
258 // rythm synchronized. The accel/decel must start at the same moment as the speed update routine
259 // This is caller in "step just occured" or "block just began" ( step Timer ) context, so we need to be fast.
260 // All we do is reset the other timer so that it does what we want
261 uint32_t Stepper::synchronize_acceleration(uint32_t dummy){
262
263 LPC_GPIO1->FIODIR |= 1<<21;
264 LPC_GPIO1->FIOSET = 1<<21;
265
266
267 // No move was done, this is called from on_block_begin
268 // This means we setup the accel timer in a way where it gets called right after
269 // we exit this step interrupt, and so that it is then in synch with
270 if( this->main_stepper->stepped == 0 ){
271 // Whatever happens, we must call the accel interrupt asap
272 // Because it will set the initial rate
273 // We also want to synchronize in case we start accelerating or decelerating now
274
275 // Accel interrupt must happen asap
276 NVIC_SetPendingIRQ(TIMER2_IRQn);
277 // Synchronize both counters
278 LPC_TIM2->TC = LPC_TIM0->TC;
279
280 // If we start decelerating after this, we must ask the actuator to warn us
281 // so we can do what we do in the "else" bellow
282 if( this->current_block->decelerate_after > 0 && this->current_block->decelerate_after < this->main_stepper->steps_to_move ){
283 this->main_stepper->attach_signal_step(this->current_block->decelerate_after, this, &Stepper::synchronize_acceleration);
284 }
285 }else{
286 // If we are called not at the first steps, this means we are beginning deceleration
287 NVIC_SetPendingIRQ(TIMER2_IRQn);
288 // Synchronize both counters
289 LPC_TIM2->TC = LPC_TIM0->TC;
290 }
291
292 LPC_GPIO1->FIOCLR = 1<<21;
293
294 return 0;
295 }
296
297
298 //#pragma GCC pop_options