This is a sun tracking unit. I am using the AS5048B sensor mounted on a shaft which is driven by a stepper motor. I put the following test code together and found via a serial print that the angular values differ between "Serial.println(mysensor.angleR(U_DEG, false), DEC);" and assigning the "mysensor.angleR(U_DEG, false)" to a Double variable and then printing it.
Can anyone perhaps advise why the values differ?
#include <Wire.h> // Reference I2C bus library
#include <OneWire.h>
#include <ams_as5048b.h>
#define ONE_WIRE_BUS 4
//unit consts for AMS AS5048B sensor
#define U_RAW 1
#define U_TRN 2
#define U_DEG 3
#define U_RAD 4
#define U_GRAD 5
#define U_MOA 6
#define U_SOA 7
#define U_MILNATO 8
#define U_MILSE 9
#define U_MILRU 10
// Motor variables
int DirPin = 8; // Direction pin asigned to pin 8
int StepPin = 9; // Step pin asigned to pin 9
// Position sensor variables
AMS_AS5048B mysensor;
double EncoderAngle =0;
double deltaAngle = 0;
long steps = 0;
void setup() {
//Start serial
Serial.begin(9600);
while (!Serial) ; //wait until Serial ready
//init AMS_AS5048B object
mysensor.begin();
//consider the current position as zero
mysensor.setZeroReg();
motor_Init();
}
void loop() {
getAngle();
Serial.print("Angle degree : ");
Serial.println(mysensor.angleR(U_DEG, false), DEC);
Serial.print("EncoderAngle: ");
erial.println(EncoderAngle);
Serial.print("DeltaAngle: ");
Serial.println(mysensor.angleR(U_DEG, false)-EncoderAngle, DEC);
** Serial.print("DeltaAngle: ");**
Serial.println(mysensor.angleR(U_DEG, false)-EncoderAngle);
delay(2000);
MotorCW(steps);
steps = steps + 10;
delay(2000);
}
void motor_Init() {
pinMode(DirPin, OUTPUT);
pinMode(StepPin, OUTPUT);
Serial.print("MotorInit complete");
delay(1000);
}
void MotorCW(long movesteps) // Turns motor clock wise
{
pinMode(DirPin, OUTPUT); // Set motor pins to output
pinMode(StepPin, OUTPUT);
digitalWrite(DirPin,LOW); // CW Direction
for (long i=0; i<movesteps; i++) {
digitalWrite(StepPin,HIGH); // Send pulse for motor to trigger on
delay(10);
digitalWrite(StepPin,LOW); // Send pulse for motor to trigger off
delay(10);
}
}
void getAngle() {
// read sensor to get the encoder angle
EncoderAngle = mysensor.angleR(U_DEG, false);
delay(2000);
}