Browse Source

bugfixes and function to reverse direction

titi 5 years ago
parent
commit
e10e5901be
2 changed files with 30 additions and 15 deletions
  1. 21 10
      Atm_AccelStepper.cpp
  2. 9 5
      Atm_AccelStepper.h

+ 21 - 10
Atm_AccelStepper.cpp

@@ -9,7 +9,7 @@ Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
   const static state_t state_table[] PROGMEM = {
     /*                       ON_ENTER           ON_LOOP          ON_EXIT  EVT_DISABLE  EVT_ENABLE  EVT_ENABLED_TIMEOUT  EVT_MOVE  EVT_STOP  EVT_EMERGENCY_STOP  EVT_ON_LIMIT_LOW  EVT_ON_LIMIT_HIGH  EVT_ON_TARGET  EVT_HOMING_LOW  EVT_HOMING_HIGH  ELSE */
     /*    DISABLE */     ENT_DISABLED,              -1,              -1,         -1,    ENABLED,                  -1,   RUNNING,        -1,                -1,               -1,                -1,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
-    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,   RUNNING,      STOP,              STOP,               -1,                -1,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
+    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,   RUNNING,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
     /*     RUNNING */     ENT_RUNNING,      LP_RUNNING,              -1,    DISABLE,         -1,                  -1,   RUNNING,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,       ENABLED,            -1,              -1,   -1,
     /*        STOP */        ENT_STOP,         LP_STOP,              -1,    DISABLE,         -1,                  -1,   RUNNING,        -1,                -1,               -1,                -1,       ENABLED,            -1,              -1,   -1,
     /*  HOMING_LOW */  ENT_HOMING_LOW,   LP_HOMING_LOW,  EXT_HOMING_LOW,    DISABLE,         -1,                  -1,        -1,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,            -1,            -1,              -1,   -1,
@@ -117,7 +117,7 @@ void Atm_AccelStepper::action( int id ) {
       return;
     case ENT_RUNNING:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      //push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       return;
     case LP_RUNNING:
@@ -176,13 +176,13 @@ void Atm_AccelStepper::action( int id ) {
     case ENT_LIMIT_LOW:
       push( connectors, ON_ONLIMITLOW, 0, 0, 0 );
       //stop motor if going down, allow going up
-      if(_limitLow_Hard && (stepper->speed() < 0)) {trigger(EVT_EMERGENCY_STOP);}
+      if(_limitLow_Hard && (stepper->speed()<0) ) {trigger(EVT_EMERGENCY_STOP);}
       else{stepper_update(); trigger(EVT_MOVE);}
       return;
     case ENT_LIMIT_HIGH:
       push( connectors, ON_ONLIMITHIGH, 0, 1, 0 );
-      if(_limitHigh_Hard && (stepper->speed() > 0)) {trigger(EVT_EMERGENCY_STOP);}
-      else{stepper_update() ; trigger(EVT_MOVE);}
+      if(_limitHigh_Hard && (stepper->speed()>0)) {trigger(EVT_EMERGENCY_STOP);}
+      else{stepper_update(); trigger(EVT_MOVE);}
       return;
   }
 }
@@ -256,7 +256,7 @@ Atm_AccelStepper& Atm_AccelStepper::move( long int stepRel) {
   _targetStep   = _currentStep + stepRel;
   runMode = 0;
   //Serial.println(_targetStep);
-  stepper->move(_targetStep);
+  stepper->moveTo(_targetStep);
   enable();
   trigger( EVT_MOVE );
   return *this;
@@ -271,22 +271,31 @@ Atm_AccelStepper& Atm_AccelStepper::moveTo( long int stepAbs) {
   return *this;
 }
 
-Atm_AccelStepper& Atm_AccelStepper::rotate( long int speed) {
+Atm_AccelStepper& Atm_AccelStepper::rotate( long  int speed) {
   runMode = 1;
-  stepper->setSpeed(speed);
+  stepper->setSpeed( speed);
   enable();
   trigger( EVT_MOVE );
   return *this;
 }
 
+Atm_AccelStepper& Atm_AccelStepper::homing( bool direction ){
+  direction == 1 ? this->trigger(EVT_HOMING_HIGH) : this->trigger(EVT_HOMING_LOW);
+}
+
+// Atm_AccelStepper& Atm_AccelStepper::rotationReversed(bool reversed){
+//   _rotationReversed = reversed ? -1 : 1 ;
+// }
+
 Atm_AccelStepper& Atm_AccelStepper::setEnablePin( int enablePin ){
   _enablePin = enablePin ;
   pinMode(_enablePin, OUTPUT);
   return *this;
 }
 
-Atm_AccelStepper& Atm_AccelStepper::enableReversed( bool reverse ){
-  _enableReversed = reverse ;
+Atm_AccelStepper& Atm_AccelStepper::pinReversed( bool directionInvert,
+                              bool stepInvert, bool enableInvert){
+  stepper->setPinsInverted(directionInvert, stepInvert, enableInvert);
   return *this;
 }
 
@@ -301,6 +310,7 @@ Atm_AccelStepper& Atm_AccelStepper::limitLow_set(int mode,  int pin,  int revers
 
 Atm_AccelStepper& Atm_AccelStepper::limitLow_isHard(bool hardlimit){
   _limitLow_Hard = hardlimit;
+  return *this;
 }
 
 Atm_AccelStepper& Atm_AccelStepper::limitLow_setThresholds (int threshold_low, int threshold_high){
@@ -320,6 +330,7 @@ Atm_AccelStepper& Atm_AccelStepper::limitHigh_set(int mode,  int pin,  int rever
 
 Atm_AccelStepper& Atm_AccelStepper::limitHigh_isHard(bool hardlimit){
   _limitHigh_Hard = hardlimit;
+  return *this;
 }
 
 Atm_AccelStepper& Atm_AccelStepper::limitHigh_setThresholds (int threshold_low, int threshold_high){

+ 9 - 5
Atm_AccelStepper.h

@@ -49,15 +49,17 @@ class Atm_AccelStepper: public Machine {
   Atm_AccelStepper& setHomingSpeed( long int homingSpeed = 400);
   Atm_AccelStepper& setAcceleration( long int acc = 1000);
 
-  Atm_AccelStepper& move( long int stepRel );
-  Atm_AccelStepper& moveTo( long int stepAbs );
-  Atm_AccelStepper& rotate( long int speed );
+  Atm_AccelStepper& move(long int stepRel );
+  Atm_AccelStepper& moveTo(long int stepAbs );
+  Atm_AccelStepper& rotate(long int speed );
   Atm_AccelStepper& homing( bool direction );
   int runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
   Atm_AccelStepper& position_refresh( long int refresh_ms = 1000);
 
+  // Atm_AccelStepper& rotationReversed(bool reversed);
+
   Atm_AccelStepper& setEnablePin( int enablePin );
-  Atm_AccelStepper& enableReversed( bool reverse );
+  Atm_AccelStepper& pinReversed( bool directionInvert=false,  bool stepInvert=false, bool enableInvert=false );
   bool enabled ;
 
 
@@ -85,7 +87,9 @@ class Atm_AccelStepper: public Machine {
   long int _targetStep = 0;
   long int _maxStep ;
   atm_timer_millis position_timer ;
-  int POSITION_SEND_TIMER = 0 ;
+  int POSITION_SEND_TIMER = 50 ;
+
+  // bool _rotationReversed = 0 ;
 
   int _enablePin = -1;
   bool _enableReversed = 0 ;