adding virtuals to Actuator
[clinton/Smoothieware.git] / src / libs / actuators / StepperMotor.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).
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 #include "mri.h"
8 #include "libs/Kernel.h"
9 #include "libs/actuators/StepperMotor.h"
10 #include "libs/actuators/Actuator.h"
11 #include "MRI_Hooks.h"
12
13 // A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
14 // TODO : Abstract this into Actuator
15
16 StepperMotor::StepperMotor(Pin* step, Pin* dir, Pin* en) : step_pin(step), dir_pin(dir), en_pin(en) {
17 this->moving = false;
18 this->paused = false;
19 this->fx_counter = 0;
20 this->stepped = 0;
21 this->fx_ticks_per_step = 0;
22 this->steps_to_move = 0;
23 this->remove_from_active_list_next_reset = false;
24 this->is_move_finished = false;
25 this->signal_step = false;
26 this->step_signal_hook = new Hook();
27
28 set_high_on_debug(en->port_number, en->pin);
29 }
30
31 // This is called ( see the .h file, we had to put a part of things there for obscure inline reasons ) when a step has to be generated
32 // we also here check if the move is finished etc ...
33 void StepperMotor::step(){
34
35 // output to pins 37t
36 this->step_pin->set( 1 );
37 this->step_ticker->reset_step_pins = true;
38
39 // move counter back 11t
40 this->fx_counter -= this->fx_ticks_per_step;
41
42 // we have moved a step 9t
43 this->stepped++;
44
45 // Do we need to signal this step
46 if( this->stepped == this->signal_step_number && this->signal_step ){
47 this->step_signal_hook->call();
48 }
49
50 // Is this move finished ?
51 if( this->stepped == this->steps_to_move ){
52 // Mark it as finished, then StepTicker will call signal_mode_finished()
53 // This is so we don't call that before all the steps have been generated for this tick()
54 this->is_move_finished = true;
55 this->step_ticker->moves_finished = true;
56 }
57
58 }
59
60
61 // If the move is finished, the StepTicker will call this ( because we asked it to in tick() )
62 void StepperMotor::signal_move_finished(){
63
64 // work is done ! 8t
65 this->moving = false;
66 this->steps_to_move = 0;
67
68 // signal it to whatever cares 41t 411t
69 this->end_hook->call();
70
71 // We only need to do this if we were not instructed to move
72 if( this->moving == false ){
73 this->update_exit_tick();
74 }
75
76 this->is_move_finished = false;
77 }
78
79 // This is just a way not to check for ( !this->moving || this->paused || this->fx_ticks_per_step == 0 ) at every tick()
80 inline void StepperMotor::update_exit_tick(){
81 if( !this->moving || this->paused || this->steps_to_move == 0 ){
82 // We must exit tick() after setting the pins, no bresenham is done
83 //this->remove_from_active_list_next_reset = true;
84 this->step_ticker->remove_motor_from_active_list(this);
85 }else{
86 // We must do the bresenham in tick()
87 // We have to do this or there could be a bug where the removal still happens when it doesn't need to
88 this->step_ticker->add_motor_to_active_list(this);
89 }
90 }
91
92
93
94 // Instruct the StepperMotor to move a certain number of steps
95 void StepperMotor::move( bool direction, unsigned int steps ){
96 // We do not set the direction directly, we will set the pin just before the step pin on the next tick
97 this->dir_pin->set(direction);
98
99 // How many steps we have to move until the move is done
100 this->steps_to_move = steps;
101
102 // Zero our tool counters
103 this->fx_counter = 0; // Bresenheim counter
104 this->stepped = 0;
105
106 // Do not signal steps until we get instructed to
107 this->signal_step = false;
108
109 // Starting now we are moving
110 if( steps > 0 ){
111 this->moving = true;
112 }else{
113 this->moving = false;
114 }
115 this->update_exit_tick();
116
117 }
118
119 // Set the speed at which this steper moves
120 void StepperMotor::set_speed( double speed ){
121
122 if (speed < 20.0)
123 speed = 20.0;
124
125 // How many steps we must output per second
126 this->steps_per_second = speed;
127
128 // How many ticks ( base steps ) between each actual step at this speed, in fixed point 64
129 double ticks_per_step = (double)( (double)this->step_ticker->frequency / speed );
130 double double_fx_ticks_per_step = (double)(1<<8) * ( (double)(1<<8) * ticks_per_step ); // 8x8 because we had to do 16x16 because 32 did not work
131 this->fx_ticks_per_step = (uint32_t)( floor(double_fx_ticks_per_step) );
132
133 }
134
135 // Pause this stepper motor
136 void StepperMotor::pause(){
137 this->paused = true;
138 this->update_exit_tick();
139 }
140
141 // Unpause this stepper motor
142 void StepperMotor::unpause(){
143 this->paused = false;
144 this->update_exit_tick();
145 }
146
147