Parcourir la source

reworked playCompensation to apply on enter running or homing

titi il y a 5 ans
Parent
commit
fac318465b
2 fichiers modifiés avec 16 ajouts et 67 suppressions
  1. 15 66
      Atm_AccelStepper.cpp
  2. 1 1
      Atm_AccelStepper.h

+ 15 - 66
Atm_AccelStepper.cpp

@@ -165,11 +165,11 @@ void Atm_AccelStepper::action( int id ) {
 
     case ENT_RUNNING:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      stepper->moveTo(_targetStep);
+      if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);}
+      if (runMode){stepper->setSpeed( _currentSpeed);}
+      else{stepper->moveTo(_targetStep);}
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
-      // stepper_update();
       return;
 
     case LP_RUNNING:
@@ -229,7 +229,8 @@ void Atm_AccelStepper::action( int id ) {
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
       push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       runMode = 1;
-      //_isHoming = 1 ;
+      _currentSpeed = -1*homing_speed;
+      if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);}
       stepper->setSpeed(-1*homing_speed);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
@@ -237,7 +238,6 @@ void Atm_AccelStepper::action( int id ) {
 
     case LP_HOMING_LOW:
       stepper_update();
-      // if (changed && (limitHigh_State||limitLow_State)){
         if(limitLow_State) {
           stepper->setCurrentPosition(0);
           _currentStep = 0;
@@ -251,28 +251,18 @@ void Atm_AccelStepper::action( int id ) {
           runMode = 0;
           trigger(EVT_EMERGENCY_STOP);
           push(connectors, ON_ONHOMINGLOW, 0,  _currentStep, homingLow_done);
-        }//Serial.println("homing low failed");}
+        }
       return;
 
     case EXT_HOMING_LOW:
-      // runMode = 0;
-      // //_isHoming = 0;
-      // if(last_trigger == EVT_ON_LIMIT_LOW) {
-      //   stepper->setCurrentPosition(0);
-      //   _currentStep = 0;
-      //   homingLow_done = 1 ;
-      // }
-      // else{homingLow_done = 0 ;};//Serial.println("homing low failed");}
-      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      // push(connectors, ON_ONHOMINGLOW, 0,  _currentStep, homingLow_done);
-      // trigger(EVT_EMERGENCY_STOP);
       return;
 
     case ENT_HOMING_HIGH:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
       push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       runMode = 1;
-      //_isHoming = 2 ;
+      _currentSpeed = homing_speed;
+      if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);}
       stepper->setSpeed(homing_speed);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
@@ -280,7 +270,6 @@ void Atm_AccelStepper::action( int id ) {
 
     case LP_HOMING_HIGH:
       stepper_update();
-      //if (changed && (limitHigh_State||limitLow_State)){
         if(limitHigh_State) {
           _maxStep = stepper->currentPosition();
           _currentStep = _maxStep;
@@ -289,33 +278,17 @@ void Atm_AccelStepper::action( int id ) {
           runMode = 0 ;
           trigger(EVT_EMERGENCY_STOP);
           push(connectors, ON_ONHOMINGHIGH, 0,  _currentStep, homingHigh_done);
-          //Serial.println("homing high done");
         }
         else if (changed && limitLow_State ){
           homingHigh_done = 0;
           runMode = 0 ;
           trigger(EVT_EMERGENCY_STOP);
           push(connectors, ON_ONHOMINGHIGH, 0,  _currentStep, homingHigh_done);
-        }//Serial.println("homing high failed");}
+        }
       return;
 
     case EXT_HOMING_HIGH:
-      //
-      // runMode = 0;
-      // //_isHoming = 0;
-      // if(last_trigger == EVT_ON_LIMIT_HIGH) {
-      //   _maxStep = stepper->currentPosition();
-      //   _currentStep = _maxStep;
-      //
-      //   homingHigh_done = 1;
-      //   //Serial.println("homing high done");
-      // }
-      // else{homingHigh_done = 0;};//Serial.println("homing high failed");}
-      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      // //_targetStep = _currentStep;
-      // push(connectors, ON_ONHOMINGHIGH, 0,  _currentStep, homingHigh_done);
-      // trigger(EVT_EMERGENCY_STOP);
-      // return;
+      return;
 
     case ENT_LIMIT_LOW:
       /*triggered by a change in limit state
@@ -445,7 +418,7 @@ Atm_AccelStepper& Atm_AccelStepper::setPlayCompensation(long int steps){
   playSteps = steps ;
 }
 
-bool Atm_AccelStepper::compensatePlay(){ //check if there is a change in direction,
+bool Atm_AccelStepper::compensatePlay(int speed){ //check if there is a change in direction,
 //use just after _targetStep update and before moving
   bool newDirection ;
   switch (runMode) {
@@ -453,14 +426,14 @@ bool Atm_AccelStepper::compensatePlay(){ //check if there is a change in directi
       newDirection = _targetStep > _currentStep;
       break;
     case 1: // speed mode
-      newDirection = stepper->speed()>0;
+      newDirection = speed>0;
       break;
   }
   if (direction != newDirection){
     long int compensation = newDirection ? -playSteps : playSteps ;
     setPosition(_currentStep + compensation);
     Serial.print("play compensation ");
-    Serial.println(_currentStep);
+    Serial.println(compensation);
     return 1;
     }
   else{return 0;}
@@ -490,10 +463,7 @@ Atm_AccelStepper& Atm_AccelStepper::position_refresh(long int refresh_ms){
 
 Atm_AccelStepper& Atm_AccelStepper::move( long int stepRel) {
   _targetStep   = _currentStep + stepRel;
-  if(playSteps){compensatePlay();}
   runMode = 0;
-  // _isHoming = 0;
-  //Serial.println(_targetStep);
   stepper->moveTo(_targetStep);
   enable();
   trigger( EVT_MOVE );
@@ -502,8 +472,6 @@ Atm_AccelStepper& Atm_AccelStepper::move( long int stepRel) {
 
 Atm_AccelStepper& Atm_AccelStepper::moveTo( long int stepAbs) {
   _targetStep   = stepAbs;
-  if(playSteps){compensatePlay();}
-  // _isHoming = 0 ;
   runMode = 0;
   stepper->moveTo(_targetStep);
   enable();
@@ -512,13 +480,9 @@ Atm_AccelStepper& Atm_AccelStepper::moveTo( long int stepAbs) {
 }
 
 Atm_AccelStepper& Atm_AccelStepper::movePercent( float percent) {
-  // Serial.println(percent/100.)
-
   _targetStep   = (float)_currentStep + percent/100.*float(_maxStep);
-  if(playSteps){compensatePlay();}
   runMode = 0;
-  // _isHoming = 0;
-  Serial.println(_targetStep);
+  // Serial.println(_targetStep);
   stepper->moveTo(_targetStep);
   enable();
   trigger( EVT_MOVE );
@@ -527,8 +491,6 @@ Atm_AccelStepper& Atm_AccelStepper::movePercent( float percent) {
 
 Atm_AccelStepper& Atm_AccelStepper::moveToPercent( float percent) {
   _targetStep   = (float) percent/100*float(_maxStep) ;
-  if(playSteps){compensatePlay();}
-  // _isHoming = 0
   runMode = 0;
   stepper->moveTo(_targetStep);
   enable();
@@ -538,10 +500,8 @@ Atm_AccelStepper& Atm_AccelStepper::moveToPercent( float percent) {
 
 Atm_AccelStepper& Atm_AccelStepper::rotate( long  int speed) {
   runMode = 1;
-  // _isHoming = 0 ;
+
   stepper->setSpeed( speed);
-  if(playSteps){compensatePlay();}
-  //stepper->runSpeed();
   enable();
   trigger( EVT_MOVE );
   return *this;
@@ -549,9 +509,7 @@ Atm_AccelStepper& Atm_AccelStepper::rotate( long  int speed) {
 
 Atm_AccelStepper& Atm_AccelStepper::homing( bool homingDirection ){
   enable();
-  // direction == 1 ? _isHoming = 2 : _isHoming = 1;
   homingDirection == 1 ? this->trigger(EVT_HOMING_HIGH) : this->trigger(EVT_HOMING_LOW);
-
   return *this;
 }
 
@@ -563,11 +521,6 @@ int Atm_AccelStepper::limitLow_avg(bool limistate){
   } else {
     limitLow_buf_head++;
   }
-  // for (int i =0; i<limit_buf_size; i++){
-  //   Serial.print(limitLow_state_buf[i]);
-  //   Serial.print(" ");
-  // }
-  // Serial.println();
   return limitLow_state_total > limit_buf_size / 2; //all values should agree
 }
 
@@ -579,13 +532,9 @@ int Atm_AccelStepper::limitHigh_avg(bool limistate){
   } else {
     limitHigh_buf_head++;
   }
-
   return limitHigh_state_total > limit_buf_size / 2; //all values should agree
 }
 
-// Atm_AccelStepper& Atm_AccelStepper::rotationReversed(bool reversed){
-//   _rotationReversed = reversed ? -1 : 1 ;
-// }
 
 Atm_AccelStepper& Atm_AccelStepper::setEnablePin( int enablePin ){
   _enablePin = enablePin ;

+ 1 - 1
Atm_AccelStepper.h

@@ -56,7 +56,7 @@ class Atm_AccelStepper: public Machine {
   Atm_AccelStepper& setAcceleration( long int acc);
   Atm_AccelStepper& setPosition( long int position);
   Atm_AccelStepper& setPlayCompensation(long int steps);
-  bool compensatePlay();
+  bool compensatePlay(int speed);
   long int getPosition();
   long int distanceToGo();
   bool isRunning();