Inital rewrite of stepticker and associated code to implement accleration per tick.
[clinton/Smoothieware.git] / src / libs / StepperMotor.h
index 6e654d7..44efa6a 100644 (file)
@@ -8,8 +8,9 @@
 #ifndef STEPPERMOTOR_H
 #define STEPPERMOTOR_H
 
-#include "libs/Hook.h"
 #include "Pin.h"
+#include <atomic>
+#include <functional>
 
 class StepTicker;
 class Hook;
@@ -18,80 +19,64 @@ class StepperMotor {
     public:
         StepperMotor();
         StepperMotor(Pin& step, Pin& dir, Pin& en);
+        ~StepperMotor();
 
-        // Called a great many times per second, to step if we have to now
-        inline void tick() {
-            // increase the ( fixed point ) counter by one tick 11t
-            fx_counter += (uint32_t)(1<<16);
-
-            // if we are to step now 10t
-            if (fx_counter >= fx_ticks_per_step)
-                step();
-        };
-
-        void step();
-        inline void unstep() { step_pin.set(0); };
+        inline void step() { current_position_steps += (direction?-1:1); step_pin.set( 1 ); }
+        inline void unstep() { step_pin.set(0); }
+        inline void set_direction(bool f) { direction= f; dir_pin.set(f); }
 
         inline void enable(bool state) { en_pin.set(!state); };
 
-        void move_finished();
-        void move( bool direction, unsigned int steps );
-        void signal_move_finished();
-        void set_speed( float speed );
-        void update_exit_tick();
-        void pause();
-        void unpause();
+        bool is_moving() const { return moving; }
+        bool which_direction() const { return direction; }
+//        void move_finished();
+//        StepperMotor* move( bool direction, unsigned int steps, float initial_speed= -1.0F);
+//        StepperMotor* set_speed( float speed );
 
+        float get_steps_per_second()  const { return steps_per_second; }
+        float get_steps_per_mm()  const { return steps_per_mm; }
         void change_steps_per_mm(float);
         void change_last_milestone(float);
+        float get_last_milestone(void) const { return last_milestone_mm; }
+        float get_current_position(void) const { return (float)current_position_steps/steps_per_mm; }
+        float get_max_rate(void) const { return max_rate; }
+        void set_max_rate(float mr) { max_rate= mr; }
 
         int  steps_to_target(float);
+        uint32_t get_stepped() const { return stepped; }
+        void force_finish_move() { force_finish= true; }
 
-        template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
-            Hook* hook = new Hook();
-            hook->attach(optr, fptr);
-            this->end_hook = hook;
-        }
+        friend class StepTicker;
+        friend class Stepper;
+        friend class Planner;
+        friend class Robot;
 
-        template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
-            this->step_signal_hook->attach(optr, fptr);
-            this->signal_step_number = step;
-            this->signal_step = true;
-        }
+    private:
+        void init();
 
-        Hook* end_hook;
-        Hook* step_signal_hook;
+        int index;
 
-        bool signal_step;
-        uint32_t signal_step_number;
-
-        StepTicker* step_ticker;
         Pin step_pin;
         Pin dir_pin;
         Pin en_pin;
 
         float steps_per_second;
-
-        volatile bool moving;
-        bool paused;
-
         float steps_per_mm;
-        float max_rate;
+        float max_rate; // this is not really rate it is in mm/sec, misnamed used in Robot and Extruder
+        float minimum_step_rate; // this is the minimum step_rate in steps/sec for this motor for this block
+        static float default_minimum_actuator_rate;
 
+        volatile int32_t current_position_steps;
         int32_t last_milestone_steps;
         float   last_milestone_mm;
 
-        uint32_t steps_to_move;
-        uint32_t stepped;
-        uint32_t fx_counter;
-        uint32_t fx_ticks_per_step;
-
-        bool     direction;
-
-        //bool exit_tick;
-        bool remove_from_active_list_next_reset;
-
-        bool is_move_finished; // Whether the move just finished
+        uint32_t stepped; // TBD may not be needed
+        volatile struct {
+            volatile bool is_move_finished:1; // Whether the move just finished
+            volatile bool moving:1;
+            volatile bool force_finish:1; // set to force a move to finish early
+            bool direction:1;
+        };
 };
 
 #endif