ZoetropeLabs / AS5048A-Arduino

A simple SPI library to interface with Austria Microsystem's AS5048A angle sensor
MIT License
39 stars 28 forks source link

Can't get this library to work #10

Open MahOraibi opened 3 years ago

MahOraibi commented 3 years ago

Hello ZoetropeLabs,

I tried using this library with my as5048a, and normally if anything doesn't work properly I would assume it was my mistake at some point. However, I tried the same connection with another code for as5048a and it works perfectly, but I cannot use that code because it's too long and complicated unlike the library.

What is currently happening is:

all these issues didn't occur using the other code.

Here's my code and I really hope you can help me resolve these issues:

#include <SPI.h>
/*PINS
   Arduino SPI pins
   MOSI = 11, MISO = 12, SCK = 13, CS = 10
   STM32 SPI pins
   MOSI = PA7, MISO = PA6, SCK = PA5, CS = PA4
*/
#include "FastAccelStepper.h"
#include <TMCStepper.h>
#include <TMCStepper_UTILITY.h>
#include <AS5048A.h>

#define dirPinStepper 6
#define enablePinStepper 5
#define stepPinStepper 7
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define DRIVER_ADDRESS2 0b01 // TMC2209 Driver address according to MS1 and MS2
#define xJoy A0
#define yJoy A1
#define preset_btn 4
#define RSENSE 0.11f // Match to your driver
TMC2209Stepper fDriver(&SERIAL_PORT, RSENSE, DRIVER_ADDRESS);
TMC2209Stepper zDriver(&SERIAL_PORT, RSENSE, DRIVER_ADDRESS2);
bool btnFlag;
volatile long currentPos;
long distance;
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;

AS5048A angleSensor(53);
  float val;
 int new_degree = 400;

void setup()
{
  SPI.begin();
  Serial.begin(19200);
  angleSensor.begin();
  pinMode(preset_btn, INPUT);
  digitalWrite(preset_btn, LOW);
  //Checking the initial angle

  SERIAL_PORT.begin(115200);      // HW UART drivers
  //fDriver.beginSerial(115200);
  fDriver.begin();                 //  SPI: Init CS pins and possible SW SPI pins
  // UART: Init SW UART (if selected) with default 115200 baudrate
  fDriver.toff(2);                 // Enables driver in software
  fDriver.rms_current(600);        // Set motor RMS current
  fDriver.microsteps(16);
  fDriver.en_spreadCycle(false);   // Toggle spreadCycle on TMC2208/2209/2224
  fDriver.pwm_autoscale(true);

  Serial.println("Starting");
  Serial.flush();
  engine.init();
  stepper = engine.stepperConnectToPin(stepPinStepper);
  if (stepper) {
    stepper->setDirectionPin(dirPinStepper);
    stepper->setEnablePin(enablePinStepper, true);
    stepper->enableOutputs();
    stepper->setAutoEnable(false);
    stepper->setSpeedInUs(250);  // the parameter is us/step !!!
    stepper->setAcceleration(3000);
  } else {
    Serial.println("Stepper Not initialized!");
    delay(1000);
  }
}

void loop()
{
  joystick ();
  //callPreset();
  val = angleSensor.getRotationInDegrees();
Serial.println(val);  
delay(10);
   /* while (Serial.available() == 0)   
    { //Wait for user input  
      }  
    String data = Serial.readString();
new_degree= data.toInt();
Serial.println(new_degree);
   Serial.println(val);*/
}

void joystick () {

  if (analogRead(xJoy) > 600) {
      stepper->runForward();
  } else if (analogRead(xJoy) < 450) {
     stepper->runBackward();
  } else {
    stepper->stopMove();
  }

}

void callPreset() {
  float deltaAngle = new_degree - (int)val;
  long deltaSteps = (long)(deltaAngle * 8.888);
  if (deltaSteps != 0) {
    stepper->move(deltaSteps); // move the same number of steps measured
    Serial.print("new position is");
    Serial.println(val);
    while (stepper->isRunning()){
      delay(2);
    }
  }    
}
benhowes commented 3 years ago

This library is no longer maintained, I don't think I can really provide you any help given it's been about 6 years since I last used it! I can see you use getRotationInDegrees, which doesn't seem to be part of the library code?