Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: TeensyStep - changing target while running

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    1

    TeensyStep - changing target while running

    Hi,
    i am trying to change the target using setTargetAbs and then tell the controller to change controller.moveAsync(motor).
    when i do this the motor stops and starts again with acceleration.
    i would like it to keep running and go to the new target.
    if the target is the other way i want the motor to slowdown then change direction and accelerate again.
    is there someting i did not find. or is it not teensystep?

    Thanks in advance

  2. #2
    Junior Member
    Join Date
    Jul 2015
    Posts
    9
    Same question here, I've just made a post in the TeensyStep repo as well

    https://github.com/luni64/TeensyStep/issues/59

  3. #3
    Junior Member
    Join Date
    Jul 2015
    Posts
    9
    This is the current test I've written keeping track of the position myself using RotateControl instead of StepperControl. It works, but I'm not really happy with it.

    Code:
    #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; }

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    Answered on GitHub...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •