reset position after test raw.
[clinton/Smoothieware.git] / src / libs / StepperMotor.cpp
index 1aa5907..236ce25 100644 (file)
 #include "StepTicker.h"
 
 #include <math.h>
-
-// in steps/sec the default minimum speed (was 20steps/sec hardcoded)
-float StepperMotor::default_minimum_actuator_rate= 20.0F;
-
-// A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
-
-StepperMotor::StepperMotor()
-{
-    init();
-}
+#include "mbed.h"
 
 StepperMotor::StepperMotor(Pin &step, Pin &dir, Pin &en) : step_pin(step), dir_pin(dir), en_pin(en)
 {
-    init();
-    enable(false);
     set_high_on_debug(en.port_number, en.pin);
-}
-
-StepperMotor::~StepperMotor()
-{
-}
-
-void StepperMotor::init()
-{
-    // register this motor with the step ticker, and get its index in that array and bit position
-    this->index= THEKERNEL->step_ticker->register_motor(this);
-    this->moving = false;
-    this->stepped = 0;
-    this->is_move_finished = false;
-    this->force_finish= false;
 
     steps_per_mm         = 1.0F;
     max_rate             = 50.0F;
@@ -48,19 +23,35 @@ void StepperMotor::init()
     last_milestone_steps = 0;
     last_milestone_mm    = 0.0F;
     current_position_steps= 0;
+    enable(false);
+    moving= false;
+    acceleration= NAN;
+    selected= true;
+
+    this->set_direction(false);
+
+    this->register_for_event(ON_HALT);
+    this->register_for_event(ON_ENABLE);
 }
 
-// // Instruct the StepperMotor to move a certain number of steps
-// StepperMotor* StepperMotor::move( bool direction, unsigned int steps, float initial_speed)
-// {
-//     set_direction(direction);
-//     this->direction = direction;
-//     this->force_finish= false;
+StepperMotor::~StepperMotor()
+{
+    THEKERNEL->unregister_for_event(ON_HALT, this);
+    THEKERNEL->unregister_for_event(ON_ENABLE, this);
+}
 
-//     // Zero our tool counters
-//     this->stepped = 0;
-//     return this;
-// }
+void StepperMotor::on_halt(void *argument)
+{
+    if(argument == nullptr) {
+        enable(false);
+        moving= false;
+    }
+}
+
+void StepperMotor::on_enable(void *argument)
+{
+    enable(argument != nullptr);
+}
 
 void StepperMotor::change_steps_per_mm(float new_steps)
 {
@@ -76,8 +67,45 @@ void StepperMotor::change_last_milestone(float new_milestone)
     current_position_steps = last_milestone_steps;
 }
 
-int  StepperMotor::steps_to_target(float target)
+void StepperMotor::set_last_milestones(float mm, int32_t steps)
+{
+    last_milestone_mm= mm;
+    last_milestone_steps= steps;
+    current_position_steps= last_milestone_steps;
+}
+
+void StepperMotor::update_last_milestones(float mm, int32_t steps)
+{
+    last_milestone_steps += steps;
+    last_milestone_mm = mm;
+}
+
+int32_t StepperMotor::steps_to_target(float target)
 {
-    int target_steps = lroundf(target * steps_per_mm);
+    int32_t target_steps = lroundf(target * steps_per_mm);
     return target_steps - last_milestone_steps;
 }
+
+// Does a manual step pulse, used for direct encoder control of a stepper
+// NOTE this is experimental and may change and/or be reomved in the future, it is an unsupported feature.
+// use at your own risk
+void StepperMotor::manual_step(bool dir)
+{
+    if(!is_enabled()) enable(true);
+
+    // set direction if needed
+    if(this->direction != dir) {
+        this->direction= dir;
+        this->dir_pin.set(dir);
+        wait_us(1);
+    }
+
+    // pulse step pin
+    this->step_pin.set(1);
+    wait_us(3);
+    this->step_pin.set(0);
+
+
+    // keep track of actuators actual position in steps
+    this->current_position_steps += (dir ? -1 : 1);
+}