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