Go Down

Topic: AS5048a and Steppermotor /SPI problem (Read 157 times) previous topic - next topic

RWTH_MASCHI

Hey guys,

I got some ams rotary encoders and some Nema 17 and I am trying to build my own closed-loop servo. I am using the AS5048a Library by by ZoetropeLabs:

https://github.com/ZoetropeLabs/AS5048A-Arduino

and the common AccelStepper Library.

Both on its own works fine, I can read the sensordata via SPI and control the motors without problems, yet if I try to read sensordata and control a motor in the same code, the motor won't work.

The motor just buzzes and vibrates- like when you do too many serial prints. i dont rly know why this happens and need some advice to solve my problem.

Greetings RWTH_MASCHI

outsider

How many steps per revolution for the motor? For the encoder? Post your code.
HowToPostCode

RWTH_MASCHI

here it is:

Code: [Select]
#include <AS5048A.h>
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 11, 10); // Step Dir

//define encoder
AS5048A angleSensor (9);

int currentStep;
int lastStep;
int angle;

boolean inDegrees = true; //set to Mode to "degrees" and not to "Step".. For "Step" set inDegrees=false
int winkelTeilung; //200Steps per revolution or 360 degrees
int fullRotationCounter = 0;


void setup()
{
  Serial.begin(9600);
  angleSensor.init(); //initialize the absolute rotary encoder
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(1000);
  stepper.moveTo(10000);
}
void loop()
{

  long data = (int)angleSensor2.getRawRotation(); //get the 14bit position as int
 

  if (inDegrees == true) {
   
    currentStep = (int)data * 0.02197265625; //converts 14bit int/per revolution to 360deg/rev
    winkelTeilung = 360;

  } else {
   
    currentStep = (int)data * 0.0122070703125; //converts 14bit int/revolution to 200steps/rev
    winkelTeilung = 200;
  }

  //if the currentstep value overflows add 1 rotation and substract on if it goes backwards
  if (currentStep >= 0 && currentStep <= 10 && lastStep <= winkelTeilung - 1 && lastStep >= winkelTeilung - 10 ) {
    fullRotationCounter++;
  }
  if (lastStep >= 0 && lastStep <= 10 && currentStep <= winkelTeilung - 1 && currentStep >= winkelTeilung - 10) {
    fullRotationCounter--;
  }

  //get the total value of rotation
  angle = currentStep + (fullRotationCounter * winkelTeilung);

  Serial.print("Got rotation of: ");
  Serial.println(angle);

  lastStep = currentStep;

//just to Test if the steppermotor runs
  stepper.moveTo(10000);
  stepper.run();
}

Go Up