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/>.
8 #include "libs/Kernel.h"
9 #include "StepperMotor.h"
11 StepperMotor::StepperMotor(){
15 this->fx_ticks_per_step
= 0;
16 this->steps_to_move
= 0;
17 this->direction_bit
= 0;
21 StepperMotor::StepperMotor(Pin
* step
, Pin
* dir
, Pin
* en
) : step_pin(step
), dir_pin(dir
), en_pin(en
) {
25 this->fx_ticks_per_step
= 0;
26 this->steps_to_move
= 0;
27 this->direction_bit
= 0;
31 // Called a great many times per second, to step if we have to now
32 bool StepperMotor::tick(){
35 this->dir_pin
->set( this->direction_bit
);
36 this->step_pin
->set( this->step_bit
);
38 // ignore inactive steppers 13t
39 if( !this->moving
|| this->paused
|| this->fx_ticks_per_step
== 0 ){ this->step_bit
= false; return false; }
41 // increase the ( fixed point ) counter by one tick 11t
42 this->fx_counter
+= (uint64_t)((uint64_t)1<<32);
44 // if we are to step now 10t
45 if( this->fx_counter
>= this->fx_ticks_per_step
){
47 // move counter back 11t
48 this->fx_counter
-= this->fx_ticks_per_step
;
50 // we must step, actual output is done at the beginning of this function 8t
53 // we have moved a step 9t
56 // is this move finished ? 11t
57 if( this->moving
&& this->stepped
== this->steps_to_move
){
62 // signal it to whatever cares 41t 411t
63 this->end_hook
->call();
76 // Instruct the StepperMotor to move a certain number of steps
77 void StepperMotor::move( bool direction
, unsigned int steps
){
79 //printf("stepper move %p moving %u steps\r\n", this, steps);
81 // We do not set the direction directly, we will set the pin just before the step pin on the next tick
82 this->direction_bit
= direction
;
84 // How many steps we have to move until the move is done
85 this->steps_to_move
= steps
;
87 // Zero our tool counters
88 this->fx_counter
= 0; // Bresenheim counter
91 // Starting now we are moving
92 if( steps
> 0 ){ this->moving
= true; }else{ this->moving
= false; }
96 // Set the speed at which this steper moves
97 void StepperMotor::set_speed( double speed
){
100 this->steps_per_second
= 0;
101 this->fx_ticks_per_step
= 1>>63;
105 // How many steps we must output per second
106 this->steps_per_second
= speed
;
108 // How many ticks ( base steps ) between each actual step at this speed, in fixed point 64
109 double ticks_per_step
= (double)( (double)this->step_ticker
->frequency
/ speed
);
110 double double_fx_ticks_per_step
= (double)(1<<16) * ( (double)(1<<16) * ticks_per_step
);
111 this->fx_ticks_per_step
= (uint64_t)( floor(double_fx_ticks_per_step
) );
113 // printf("speed: %f frequency: %f ticks_per_step: %f double_fx_ticks_per_step: %f floor: %f fx_ticks_per_step: %f \r\n", speed, (double)this->step_ticker->frequency, ticks_per_step, double_fx_ticks_per_step, floor(double_fx_ticks_per_step), (double)this->fx_ticks_per_step );