Code Monkey home page Code Monkey logo

as5047p-driver's Introduction

AS5047P Driver

GitHub GitHub release (latest by date) PlatformIO Registry

A platform independent library for ams AS5047P rotary position sensor/magnetic encoder.

  • src/: Main code.
  • examples/: Some examples.
  • LICENSE
  • README.md (this file)

Usage

Install

There are 3 ways to install:

  1. Go to VS Code > PIO Home > Libraries and search "as5047-driver", press the "Add to Project" button.

  2. Add lib_deps = ziteh/as5047-driver@^2.0.0 in platformio.ini file, for example:

[env:nucleo_f446re]
platform = ststm32
board = nucleo_f446re
framework = stm32cube
lib_deps = ziteh/as5047-driver@^2.0.0
  1. PIO CLI: pio pkg install --library "ziteh/as5047-driver@^2.0.0".

Writing the Code

Include

#include <as5047p.h>

The following functions that must be implemented in the user file (e.g. main.c), function names are free:

void as5047p_spi_send(uint16_t data);
uint16_t as5047p_spi_read(void);
void as5047p_spi_select(void);
void as5047p_spi_deselect(void);
void as5047p_delay(void)

Take STM32 HAL for example:

void as5047p_spi_send(uint16_t data)
{
  HAL_SPI_Transmit(&hspi1, (uint8_t *)&data, 1, HAL_MAX_DELAY);
}

uint16_t as5047p_spi_read(void)
{
  uint16_t data = 0;
  HAL_SPI_Receive(&hspi1, (uint8_t *)&data, 1, HAL_MAX_DELAY);
  return data;
}

void as5047p_spi_select(void)
{
  HAL_GPIO_WritePin(AS5047P_SS_GPIO_Port, AS5047P_SS_Pin, GPIO_PIN_RESET);
}

void as5047p_spi_deselect(void)
{
  HAL_GPIO_WritePin(AS5047P_SS_GPIO_Port, AS5047P_SS_Pin, GPIO_PIN_SET);
}

// For 't_CSn': High time of CSn between two transmissions, >350 ns.
void as5047p_delay(void)
{
  HAL_Delay(1);
}

Make handle and init:

as5047p_handle_t as5047p;
as5047p_make_handle(&as5047p_spi_send,
                    &as5047p_spi_read,
                    &as5047p_spi_select,
                    &as5047p_spi_deselect,
                    &as5047p_delay,
                    &as5047p);

// Configure SETTINGS1 and SETTINGS2.
as5047p_config(&as5047p, 0b00100101, 0b00000000); 

// Set specify position as zero.
as5047p_set_zero(&as5047p, 0);

Read angle in degree:

float angle_deg;
int8_t error = as5047p_get_angle(&as5047p, without_daec, &angle_deg);

if (error == 0)
{
    printf("Angle: %f\r\n", angle_deg);
}

Read raw position:

uint16_t position;
int8_t error = as5047p_get_position(&as5047p, without_daec, &position);

if (error == 0)
{
    printf("Raw: %5i\r\n", position);
}

Examples

My blog (Chinese) has more detailed descriptions.

AS5047P SPI Interface

  • SPI Mode = 1.
    • CPOL = 0: Clock is low when idle.
    • CPHA = 1: Data is sampled on the second edge (i.e. falling edge).
  • CS (chip select) pin active low.
  • Data size (data frame format) is 16-bit.
  • Bit order is MSB first.
  • Max clock rates up to 10 MHz.
  • Only supports slave operation mode.

as5047p-driver's People

Contributors

ziteh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

as5047p-driver's Issues

Angle read between 180 and 360 degree

Reading the angle from the sensor give an angle between 180 and 360 degree. The MSB of raw position seems to be read as 1 even on the 0-180 sector adding 180 degree to the mesure.

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.