Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative currentStep for small changes in position #17

Merged
merged 2 commits into from Jul 3, 2020

Conversation

klaasdc
Copy link
Contributor

@klaasdc klaasdc commented Jul 2, 2020

I am using this library to drive an automotive gauge. Sometimes currentStep seems to ty to become negative (underflow), and the motor blocks until currentStep again enters the domain of valid step positions.

Simple test below.
During the test, the velocity variable vel is increased very quickly, so the destination step is reached but velocity did not ramp down quickly enough. This leads to the if (currentStep==targetStep && vel==0)-condition not triggering, which in turn leads to dir being -1 when currentStep reaches 0, inside the stepTo function.

Therefore it is better to limit vel to the delta to the target position.

#include <SwitecX12.h>

const int STEPS = 315 * 12;
const int A_STEP = 8;
const int A_DIR = 9;
const int RESET = 10;

SwitecX12 motor1(STEPS, A_STEP, A_DIR);

int state = 0;

void setup() {
  Serial.begin(9600);
  digitalWrite(RESET, HIGH);
  motor1.zero();
  motor1.setPosition(STEPS/2);
}

void loop() {
  if (state == 0){
    motor1.setPosition(0);
    Serial.println("go to 0");
    state = 1;
  } else if (state == 1){
    if (motor1.stopped){
      Serial.println("stopped1");
      state = 2;
    }
  } else if (state == 2){
    motor1.setPosition(500);
    Serial.println("go to 500");
    state = 3;
  } else if (state == 3){
    if (motor1.stopped){
      Serial.println("stopped2");
      state = 0;
    }
  }
  motor1.update();
}

Copy link
Collaborator

@guyc guyc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

@guyc guyc merged commit 67ace42 into clearwater:master Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants