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:
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.
#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();
}