TMCStepper icon indicating copy to clipboard operation
TMCStepper copied to clipboard

XACTUAL and VACTUAL always zero on TMC5160

Open maceiras opened this issue 3 years ago • 0 comments

Hello,

I am using a TMC5160-BOB Breakout boards to control my stepper motor(17HS08-1004S). The breakout boards is configured to use the internal ramp generator.

Currently, I am trying to use my motor in velocity mode, I manage to make my motor move at a given velocity, but I am not able to read feedbacks from the driver, XACTUAL and VACTUAL registry are always zero!

I know that this place may not be the best one to ask such question but I struggled for a while and would love to have a possible explanation.

Here is part of the code I use to get my problem:

long speedFromHz(float speedHz) { return (long)(speedHz / ((float)12000000 / (float)(1ul << 24)) * (float)256); }
float speedToHz(long speedInternal) { return ((float)speedInternal * (float)12000000 / (float)(1ul << 24) / (float)256); }

class Motor{
  public:
    Motor(const uint16_t& pin, const bool& inverted=false):stepper_(TMC5160Stepper(pin,0.075)),pin_(pin),inverted_(inverted){
    }

    void begin(){
      stepper_.begin();
      stepper_.rms_current(700);
      stepper_.en_pwm_mode(true);
      stepper_.microsteps(256);
      stepper_.AMAX(1000);
      stepper_.RAMPMODE(1);
    }

    void setSpeed(const int& speed){
      bool rot_dir = speed < 0 ? true : false;
      if(inverted_){
        rot_dir = !rot_dir;
      }

      stepper_.shaft(rot_dir);
      stepper_.VMAX(speedFromHz(abs(speed)));
    }

    int32_t getPosition(){
      int32_t pos;
      pos = stepper_.XACTUAL();
      return pos/256.0;
    }
    
    float getVelocity(){
      uint32_t vel;
      vel = stepper_.VACTUAL();

      if(bitRead(vel,23)){
        vel |= 0xFF000000;
      }
      
      return speedToHz(vel);
    }
    
  private:
 
    TMC5160Stepper stepper_;
    bool inverted_;
    const uint16_t pin_;
};

Motor test(csPin,false);

void setup() {
  pinMode(22, OUTPUT);
  digitalWrite(22, LOW);  // Active low
  
  SPI.begin();
  Serial.begin(115200);
  test.begin();
}

Thanks a lot for your help on this!

maceiras avatar Oct 19 '22 18:10 maceiras