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