Coder Social home page Coder Social logo

sosandroid / ams_as5048b Goto Github PK

View Code? Open in Web Editor NEW
48.0 13.0 28.0 40 KB

Arduino Lib for AMS AS5048B I2C - 14-bit magnetic rotary position sensor

License: Other

C++ 100.00%
i2c arduino rotaryencoder i2c-sensors arduino-library ams as5048 position pwm wind-vane xplane angle degrees radian-grade magnetic

ams_as5048b's Introduction

Arduino library for AS5048B from AMS

AS5048B is a 14-bit magnetic rotary position sensor with digital angle (I2C) and PWM output. This library deals only with the I2C channel.

AS5048B's AMS page.

v1.0 - First release
v1.0.1 - Typo to allow compiling on Codebender.cc (Math.h vs math.h) + Wind vane example modification to comply with the Timer.h lib used by them
v1.0.2 - ams_as5048b.cpp - fix setZeroReg() issue raised by @MechatronicsWorkman
v1.0.3 - Small bug fix and improvement by @DavidHowlett
v1.0.4 - Implemented OTP register burning by @brentyi
v1.0.5 - Optional parameters fix

Features

  • Manage zero position
  • Counts CW or CCW
  • Reads Auto Gain & Diagnostics registers
  • Reads 14 bits magnitude
  • Reads 14 bits angle with various units output (raw, turn, degree, radian, grade, minute of arc, second of arc, Nato mil, Russian mil, Swedish mil)
  • Computes an angular exponential moving average
  • Reads exponential moving average angle and outputting with various units
  • Resets Exp moving Avg
  • OTP setting
  • OTP programming sequence

Code examples

  • Single angle reading, outputs 2 units
  • Angular exponential moving average reading, outputs read angle and average
  • Wind vane, outputs azimuth and compass direction - This one as a special #define for Codebender.cc support
  • Dial reading for X-Plane
  • Slave address programming

Not available yet features

  • PWM reading
  • Debug

Testing

  • Tested against AS5048B's official adapter board
  • Tested on Arduino Mega with Arduino IDE 1.0.5 && Codebender.cc
  • Tested on Arduino Uno with Arduino IDE 1.6.9
  • Tested on Arduino Nano with Arduino IDE 1.6.9
  • Tested on Teensy++ 2.0 with Arduino IDE 1.6.9
  • Please comment about other devices

ams_as5048b's People

Contributors

brentyi avatar davidhowlett avatar derpicknicker1 avatar gseqbe avatar nfloxs avatar sosandroid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ams_as5048b's Issues

Arduino Due

valori encoder arduino due
Hi, I tried the library on the UNO and it works perfectly, but when I switch to the DUE I get values(in degrees) only between 270 and 355. I've used the same wiring I used on arduino UNO so I suppose the issue is on the DUE. Does anyone know how to solve the problem ? Thank you in advice!

Zero position seems to not be permanent.

I'm using this library with both a Teensy 3.5 and an Arduino Micro.

Is the setZeroReg() method setting permanently the zero position?
In my case the setting is not permanent: if I write the current angle to the zeroReg by calling setZeroReg() and then I read it by calling zeroRegR(), the setting is held by the sensor just in the same session. If I power off both the micro-controller and the sensor and I do not recall setZeroReg(), form zeroRegR() I read just 0, and not the setting saved before.

Page 28 of the data sheet mention that in order to burn the following procedure is needed:

  1. Write 0 into OTP zero position register to clear
  2. Read angle information
  3. Write previous read angle position into OTP zero position register
    Now the zero position is set.
    If you want to burn it to the OTP register send:
  4. Set the Programming Enable bit in the OTP control register
  5. Set the Burn bit to start the automatic programming procedure
  6. Read angle information (equals to 0)
  7. Set the Verify bit to load the OTP data again into the internal registers
  8. Read angle information (equals to 0)

The setZeroReg() seems to me to execute only the first three steps, or am I wrong?

Thanks!

Issue burning the Zero OTP

I am using the AS5048B magnetic encoder in my project. I am facing the problem that once I run the code temporarily zero position gets set but when I restart the system it forgets the zero position.

I am calling "setZeroReg" first than "doProgZero" function. I have also tried calling both functions multiple times.

I am using the encoder in 5V mode and the correct capacitors are already hooked.

Random input from sensor

Problem:
Sensor angle degree readout constantly fluctuates between roughly +/-13 degrees ....even though complete idle/resting !?
And Impossible to gain any degree values over 90 ?? (Rollover to Zero at about 90 degree)

Any help on this would be tremendously appreciated, as stuck in development for the moment !

Sidenotes:
No closeby stray magnetic fields exists & no metal parts near sensor.
Radially polarized magnet used (came with the AMS board)

Setup:

  • DUE (pins: SDA/20, SCL/21).
  • Functionally Non-modified/original "read_simple_angle" Sketch (enclosed).
  • 5V & 3.3V on AS5048B shorted (Feed 5V from DUE).
  • 2.2uF cap over VCC/Gnd.
  • Both SDA+SCL pulled up by each 15k (even though already done by DUE. Also tested without but no difference)

**** (original) Sketch used
#include <ams_as5048b.h>
#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

AMS_AS5048B mysensor;

void setup() {
Serial.begin(9600);
while (!Serial) ;
mysensor.begin();
mysensor.setZeroReg();
}

void loop() {
Serial.print("Angle sensor raw : ");
Serial.println(mysensor.angleR(U_RAW, true), DEC);
Serial.print("Angle degree : ");
Serial.println(mysensor.angleR(U_DEG, false), DEC);
delay(2000);
}

Sample monitor readout_1:
raw : 2250.0000000000 degree : 49.4384765625
raw : 2570.0000000000 degree : 56.4697265625
raw : 2314.0000000000 degree : 50.8447265625
raw : 2378.0000000000 degree : 52.2509765625
raw : 2634.0000000000 degree : 57.8759765625
raw : 2314.0000000000 degree : 50.8447265625
raw : 2122.0000000000 degree : 46.6259765625
raw : 2442.0000000000 degree : 53.6572265625
raw : 2122.0000000000 degree : 46.6259765625
raw : 2314.0000000000 degree : 50.8447265625
raw : 2378.0000000000 degree : 52.2509765625
raw : 2122.0000000000 degree : 46.6259765625
raw : 2250.0000000000 degree : 49.4384765625
raw : 1994.0000000000 degree : 43.8134765625
raw : 2442.0000000000 degree : 53.6572265625

Sample monitor readout_2:
raw : 158.0000000000 degree : 3.4716796875
raw : 478.0000000000 degree : 10.5029296875
raw : 414.0000000000 degree : 9.0966796875
raw : 158.0000000000 degree : 3.4716796875
raw : 4062.0000000000 degree : 89.2529296875
raw : 414.0000000000 degree : 9.0966796875
raw : 606.0000000000 degree : 13.3154296875
raw : 350.0000000000 degree : 7.6904296875
raw : 286.0000000000 degree : 6.2841796875
raw : 542.0000000000 degree : 11.9091796875
raw : 350.0000000000 degree : 7.6904296875
raw : 30.0000000000 degree : 0.6591796875
raw : 414.0000000000 degree : 9.0966796875
raw : 606.0000000000 degree : 13.3154296875
raw : 286.0000000000 degree : 6.2841796875
raw : 286.0000000000 degree : 6.2841796875
raw : 414.0000000000 degree : 9.0966796875
raw : 4062.0000000000 degree : 89.2529296875
raw : 606.0000000000 degree : 13.3154296875
raw : 286.0000000000 degree : 6.2841796875

Arduino Due fix

I've added a fix to the library to work the the Due.

#17

Could someone with an UNO also try this?

Correct Connexion Arduino

Hello guys ๐Ÿ‘ ,
Thanks for your code,
I want to use it in my project but I didn't find any guide of connexion online for the AS5048B, can you share the connexion you use to test your code please ?
Thanks,
WilliamH07

Arduino Uno P

Hi there, I was wondering if anyone can tell me what I am doing wrong.

I have a AS5048B connected to my Arduino Uno by doing the following:

AS5048B Arduino uno

SDA ------------------ > A4
SCL --------------------> A5

I notice that there are hardware pin such as A1 and A2 located on the AS5048B ... However, I do not know where to put these pins on, furthermore not knowing if I am required to us these pins....

When I go onto my Serial Monitor all I am getting is the following:

Angle degree : I2C error: 2
359.9780273437

Any help will be appreciated
Thanks,
Jess

Could you please add AS5047P SPI support or Macros?

Hi sosandroid,

Sir, would you have time to investigate how many functions could be reused in AS5047P to your library. They have almost identical registers i think, but minor different hardware capability setups.

Zero position did not reset

I was trying to use this code on an Arduino Uno and ran into problems. The read angle code seems to work as expected however the reset zero position did not work for me. After reading the AS5048B data sheet it mentioned to clear the zero position register first before writing a new angle to it. Thus I made the following changes which seem to work.

void AMS_AS5048B::setZeroReg(void) {

//I ADDED THIS
AMS_AS5048B::writeReg(AS5048B_ZEROMSB_REG, (uint8_t) 0x00);
AMS_AS5048B::writeReg(AS5048B_ZEROLSB_REG, (uint8_t) 0x00);
//I ADDED THIS

uint16_t zero = AMS_AS5048B::readReg16(AS5048B_ANGLMSB_REG);
AMS_AS5048B::zeroRegW(zero);
return;
}

Just thought I would pass on this information to anyone else using this code.

Wrong I2C address in sample programs

In the program_address and read_simple_angle sample Arduino files, the encoder object is constructed with address 0x40. This is inconsistent with the addressed used to program the chip, which is actually constructed with address 0x44. The binary value set is 0b1000100 which is correctly commented. However, that's 0x44, not 0x40. Interestingly, a user will be able to use the program_address.ino file to successfully read the chip after programming it (although the read_simple_angle file will not work because the incorrect address is accessed). This is likely due to the encoder object's address being changed in software when the addressRegW method is run, thus allowing it to correctly read the angle later in the void loop().

I2c address constraint ?

In the program_adress.ino file you are using adress 0x01.
When i go to the official adress lis of the i2c organisation. https://i2cdevices.org/addresses , i see that the low addreses are reserved.

Did you had problems with theses the low addresses ?

I would like to use the 0x50 to 0x56 addresses could i expect any issues ?

I tried to program 0x50 and had a few problems.
For some reason it seems that the adress did not change.
- When i scan my i2c devices, it is not detected but
- Fortunately, i am still able to read the angle with the simple read program.
- If i read the adress register 255 is returned.
( it is the same value that i have on ea second board where i did not change the address yet )

            - I have found the AMS i2c programming notice, and since i am using a 3.3volt board i think that the address simply did
               not program because 5v is required or we must add a capacitor when we have 3.3 volt only.
               ( both conditions that i did  not respected because i was not aware of this constraint)
                  https://ams.com/documents/20143/36005/AS5048_AN000386_1-00.pdf

Should i retry with a simple uno board that i have in hand?

simple question i2c error 2

Using the device with arduino uno set up with i2c connections, I only see the error message: Angle sensor raw : I2C error: 2
Is there another setting that should be used?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.