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

dynamically change setTargetAbs() while motor is moving #59

Closed
mickeyvanolst opened this issue Nov 12, 2019 · 2 comments
Closed

dynamically change setTargetAbs() while motor is moving #59

mickeyvanolst opened this issue Nov 12, 2019 · 2 comments

Comments

@mickeyvanolst
Copy link

mickeyvanolst commented Nov 12, 2019

I want to use the motor in a way that it constantly receives new target data and that it simply tries to follow the target position. When using the StepControl class I have an easy way to move to an absolute position, but it doesn't allow for a graceful response when giving a new position while it is in the process of going somewhere. RotateControl does allow for dynamic shifts in direction with overrideSpeed(), though then I have to rebuilt the libraries ability to keep track of where it is on my side. Is there any way I could still use StepControl but allow for dynamically changing the absolute target?

This is my current test sketch, it works, but I have the feeling this could be done in a better way.

#include "Arduino.h"
#include "TeensyStep.h"

constexpr unsigned motorStpPerRev = 200 * 16;            // e.g. fullstep/rev * microstepping
constexpr unsigned motorAcceleration = 15000;

float targetMotorSpeed, oldMotorSpeed;

int motorTargetPosition = 0;
int motorTargetDirection = 2; // 0 == up 1 == down 2 == none
int prevMotorPosition = 0;

constexpr unsigned maxSpeed = 1000;

RotateControl controller;

Stepper motor(4, 5);

void setup() {
  Serial.begin(9600);
  while(!Serial) {
    ;
  }
  motor.setMaxSpeed(1);   // -> parameter of overrideSpeed equals real speed
  motor.setAcceleration(motorAcceleration);
  
  // startup controller
  controller.rotateAsync(motor);
  controller.overrideSpeed(0.0f);

//  setMotorTarget(-4000);
  
}

void setMotorTarget(int t) {
  motorTargetPosition = t;
  if(t > motor.getPosition()) {
    motorTargetDirection = 0;
  } else if(t < motor.getPosition()){
    motorTargetDirection = 1;
  } else {
    motorTargetDirection = 2; //none
  }
}

void loop() {
  int p = motor.getPosition();
  int pp = prevMotorPosition;
  int t = motorTargetPosition;

  if(motorTargetDirection == 0) {
    // we go up
    setMotorSpeed(maxSpeed);//up
    if(p > t) motorTargetDirection = 2;
  } else if(motorTargetDirection == 1){
    // we go down
    setMotorSpeed(-maxSpeed);//down
    if(p < t) motorTargetDirection = 2;  
 } else {
    // nowhere
    setMotorSpeed(0);
  }

  prevMotorPosition = p;
  updateSpeeds();

  int sinT = sin(millis()*0.0001)*9000; // send it a changing target (sine wave for testing)
  setMotorTarget(sinT);
  
}

void setMotorSpeed(float rpm)
{
    targetMotorSpeed = rpm / 60.0f * motorStpPerRev;
}

void updateSpeeds()
{   
    if(targetMotorSpeed != oldMotorSpeed)  // if target speed changed -> update all
    {
        oldMotorSpeed = targetMotorSpeed;
        controller.overrideSpeed(targetMotorSpeed);
    }
}

inline int getCurMotorSpeed() { return controller.isRunning() ? controller.getCurrentSpeed() : 0; }
@luni64
Copy link
Owner

luni64 commented Nov 12, 2019

You currently can not change the target while the motor is moving. but there is a simple way to do this. Here some more information and a test sketch: #58 (comment)

(You need to take the code fromthe DevTimer branch for this to work).

Edit:

...though then I have to rebuilt the libraries ability to keep track of where it is on my side. Is there any way I could still use StepControl but allow for dynamically changing the absolute target?

BTW: It keeps track of the position whichever controller you use. The current position is handled by the Stepper class not the controlller. You can also switch controllers if needed. Just make sure that you don't have 2 controllers driving the same motor at the same time...

@mickeyvanolst
Copy link
Author

Luni thank you so much!

Your example code from the other thread was a perfect solution :-)

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

No branches or pull requests

2 participants