Michael Spencer / Smoothie

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StepperMotor.h Source File

StepperMotor.h

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #ifndef STEPPERMOTOR_H
00009 #define STEPPERMOTOR_H
00010 
00011 #include "libs/Hook.h"
00012 #include "Pin.h"
00013 
00014 class StepTicker;
00015 class Hook;
00016 
00017 class StepperMotor {
00018     public:
00019         StepperMotor();
00020         StepperMotor(Pin& step, Pin& dir, Pin& en);
00021 
00022         // Called a great many times per second, to step if we have to now
00023         inline void tick() {
00024             // increase the ( fixed point ) counter by one tick 11t
00025             fx_counter += (uint32_t)(1<<16);
00026 
00027             // if we are to step now 10t
00028             if (fx_counter >= fx_ticks_per_step)
00029                 step();
00030         };
00031 
00032         void step();
00033         inline void unstep() { step_pin.set(0); };
00034 
00035         inline void enable(bool state) { en_pin.set(!state); };
00036 
00037         void move_finished();
00038         void move( bool direction, unsigned int steps );
00039         void signal_move_finished();
00040         void set_speed( float speed );
00041         void update_exit_tick();
00042         void pause();
00043         void unpause();
00044 
00045         void change_steps_per_mm(float);
00046         void change_last_milestone(float);
00047 
00048         int  steps_to_target(float);
00049 
00050         template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
00051             Hook* hook = new Hook();
00052             hook->attach(optr, fptr);
00053             this->end_hook = hook;
00054         }
00055 
00056         template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
00057             this->step_signal_hook->attach(optr, fptr);
00058             this->signal_step_number = step;
00059             this->signal_step = true;
00060         }
00061 
00062         Hook* end_hook;
00063         Hook* step_signal_hook;
00064 
00065         bool signal_step;
00066         uint32_t signal_step_number;
00067 
00068         StepTicker* step_ticker;
00069         Pin step_pin;
00070         Pin dir_pin;
00071         Pin en_pin;
00072 
00073         float steps_per_second;
00074 
00075         volatile bool moving;
00076         bool paused;
00077 
00078         float steps_per_mm;
00079         float max_rate;
00080 
00081         int32_t last_milestone_steps;
00082         float   last_milestone_mm;
00083 
00084         uint32_t steps_to_move;
00085         uint32_t stepped;
00086         uint32_t fx_counter;
00087         uint32_t fx_ticks_per_step;
00088 
00089         bool     direction;
00090 
00091         //bool exit_tick;
00092         bool remove_from_active_list_next_reset;
00093 
00094         bool is_move_finished; // Whether the move just finished
00095 };
00096 
00097 #endif
00098