Giter Site home page Giter Site logo

addingding / as5050a Goto Github PK

View Code? Open in Web Editor NEW

This project forked from castrofernando/as5050a

0.0 0.0 0.0 23 KB

Library based on arduino framework to comunicate with a SPI magnetic rotary encoder low power AS5050A.

License: GNU Lesser General Public License v2.1

C++ 100.00%

as5050a's Introduction

AS5050A

Description

Library based on arduino framework to comunicate with a SPI magnetic rotary encoder low power AS5050A.

Tested hardwares

ESP32-WROOM

How to use

  1. Include de library AS5050A.h
  2. Create a AS5050A object: AS5050A encoder(SS,MOSI,MISO,SCK,1000000); ; -> Define here the SPI chip pinout and the SPI SPEED. The sensor SPI support rates up to 10MHz.
  3. Use method begin() to initialize the device.

Methods available

Method Description
void begin() Initialize the spi sensor.
void setStartPosition() define a offset for readValue / read angle / totalAngle. Set a home position to the device (value 0)
int32_t readValue() read scale from 0 to 1023 (10 bits)
int16_t getAngle() convert value read from 0° up to 360°
int8_t getRotations() get rotation number (negative and positive to inform the direction)
int32_t getAbsoluteAngle() get absolute angle based on rotations (negative and positive to indicate the direction);
void setAlarmPoint(int32_t absoluteAngle) define a alarm point to be triggered by absoluteAngle. To this works, should read the value and absolute angle with some often (1ms? / 10ms?)
void alarmEvent(alarmCallback alarm_callback) Alarm callback event. Based on the alarm points set, it will trigger an alarm when between +-5 value set.
  • To define max alarm points, please change the define on .h file -> #define MAX_ALARM_POINT . Default is 10.
  • To change the interval alarm value, please change the define on .h file -> #define ROUND_POINT_ERROR . Default is 5.

Example


#include <Arduino.h>
#include <AS5050A.h>

//SPI PINOUT - ESP32-WROOM

#define MISO   (gpio_num_t)19
#define MOSI   (gpio_num_t)23
#define SCK    (gpio_num_t)18
#define SS     (gpio_num_t)5

//Define encoder and spi pins
AS5050A encoder(SS,MOSI,MISO,SCK,1000000); 

//Initialize hardware
void initHardware(){
  pinMode(SS,OUTPUT);
  pinMode(MISO,INPUT);
  pinMode(MOSI,OUTPUT);
  pinMode(SCK,OUTPUT);
  digitalWrite(SS,HIGH);

  Serial.begin(9600);
}

//Alarm callback
void alarmEventCallback(int32_t alarm){
    Serial.print("ALARM CALLBACK: ");
    Serial.println(alarm);
}

void setup(){
    initHardware();
    encoder.begin();
    encoder.setAlarmPoint(450);		//set and event when absolute event between 445 and 455 (+- 5 -> see as5050a.h file)
    encoder.setAlarmPoint(-350);	//set and event when absolute event between -355 and -345 (+- 5 -> see as5050a.h file)
    encoder.alarmEvent(&alarmEventCallback); //pass a function address to be called by alarm event callback.
}

void loop(){
      delay(1);
      Serial.print("Value: ");
      Serial.print(encoder.readValue(),DEC); //need to be call to all others methods work.
      Serial.print("\t Angle: ");
      Serial.print(encoder.getAngle(),DEC);
      Serial.print("\t Rotations: ");
      Serial.print(encoder.getRotations(),DEC);
      Serial.print("\t Absolute Angle: ");
      Serial.println(encoder.getAbsoluteAngle(),DEC); //need to call to be able treat the alarm event.
}

as5050a's People

Contributors

castrofernando avatar

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.