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