From d500ce151f48ba45b702066a0efccf6029866bd3 Mon Sep 17 00:00:00 2001 From: Mathieu Kaelin Date: Sat, 4 Feb 2017 02:04:05 +0100 Subject: [PATCH] Add example for Arduino boards (simple example and complete arduino library) Add example for Adafruit FT232H breakout board (use python2.7) Update readme --- .../magalpha_ft232h_library.py | 83 +++++++++++++ .../magalpha_ft232h_simple_example.py | 17 +++ Arduino/MagAlpha-Library/README.md | 23 ++++ .../edit-magalpha-config.ino | 99 +++++++++++++++ .../read-magalpha-angle.ino | 27 +++++ Arduino/MagAlpha-Library/keywords.txt | 37 ++++++ Arduino/MagAlpha-Library/library.properties | 10 ++ Arduino/MagAlpha-Library/src/MagAlpha.cpp | 113 ++++++++++++++++++ Arduino/MagAlpha-Library/src/MagAlpha.h | 46 +++++++ .../magalpha-arduino-simple-example.ino | 96 +++++++++++++++ LICENSE | 21 ++++ README.md | 32 ++++- 12 files changed, 603 insertions(+), 1 deletion(-) create mode 100644 Adafruit-FT232H-Breakout/magalpha_ft232h_library.py create mode 100644 Adafruit-FT232H-Breakout/magalpha_ft232h_simple_example.py create mode 100644 Arduino/MagAlpha-Library/README.md create mode 100644 Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino create mode 100644 Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino create mode 100644 Arduino/MagAlpha-Library/keywords.txt create mode 100644 Arduino/MagAlpha-Library/library.properties create mode 100644 Arduino/MagAlpha-Library/src/MagAlpha.cpp create mode 100644 Arduino/MagAlpha-Library/src/MagAlpha.h create mode 100644 Arduino/magalpha-arduino-simple-example/magalpha-arduino-simple-example.ino create mode 100644 LICENSE diff --git a/Adafruit-FT232H-Breakout/magalpha_ft232h_library.py b/Adafruit-FT232H-Breakout/magalpha_ft232h_library.py new file mode 100644 index 0000000..b51aefb --- /dev/null +++ b/Adafruit-FT232H-Breakout/magalpha_ft232h_library.py @@ -0,0 +1,83 @@ +import Adafruit_GPIO as GPIO +import Adafruit_GPIO.FT232H as FT232H +import time + +class MagAlpha: + """"MagAlpha Communication Library for 3rd generation sensors (MA302, MA800, ...).""" + + def __init__(self): + FT232H.use_FT232H() + self.ft232h = FT232H.FT232H() + self.spiClockFreqInHz = 1000000 + self.chipSelectPinOnFt232h = 8 + self.spiMode = 0 + self.spi = FT232H.SPI(self.ft232h, cs=self.chipSelectPinOnFt232h, max_speed_hz=self.spiClockFreqInHz, mode=self.spiMode, bitorder=FT232H.MSBFIRST) + self.__rawToDegreeConvertionRatio = 360.0/65536.0 + + def readAngle(self, printEnabled=False): + """Return the angle [0-65535] (raw sensor output value).""" + response = self.spi.transfer([0x00, 0x00]) + angularPositionRaw = (response[0]<<8)+response[1] + if (printEnabled): + print 'Angular Position [raw] : {0}'.format(angularPositionRaw) + return angularPositionRaw + + def readAngleInDegree(self, printEnabled=False): + """Return the angle in degree [0-360] (raw sensor output converted in degree).""" + response = self.spi.transfer([0x00, 0x00]) + angularPositionRaw = (response[0]<<8)+response[1] + angularPositionDegree = float(angularPositionRaw)*self.__rawToDegreeConvertionRatio + if (printEnabled): + print 'Angular Position [degree] : {0}'.format(angularPositionDegree) + return angularPositionDegree + + def readAngleAdvanced(self, printEnabled=False): + """Return the angle in raw and degree format.""" + response = self.spi.transfer([0x00, 0x00]) + angularPositionRaw = (response[0]<<8)+response[1] + angularPositionDegree = float(angularPositionRaw)*self.__rawToDegreeConvertionRatio + if (printEnabled): + print 'Angular Position [raw] : {0} \t, [degree] : {1}'.format(angularPositionRaw, angularPositionDegree) + return angularPositionRaw, angularPositionDegree + + def readRegister(self, address, printEnabled=False): + """Return sensor register value.""" + command = 0b01000000 | (address & 0x1F) + self.spi.transfer([command, 0x00]) + response = self.spi.transfer([0x00, 0x00]) + registerValue = response[0] + if (printEnabled): + print 'Read Register [{0}] \t=\t{1}'.format(address, registerValue) + return registerValue + + def writeRegister(self, address, value, printEnabled=False): + """Return sensor written register value.""" + command = 0b10000000 | (address & 0x1F) + registerWriteValue = (value & 0xFF) + self.spi.transfer([command, registerWriteValue]) + #wait for 20ms + time.sleep(0.02) + response = self.spi.transfer([0x00, 0x00]) + registerReadValue = response[0] + if (printEnabled): + print 'Write Register [{0}] \t=\t{1}, \tReadback Value = {2}'.format(address, registerWriteValue, registerReadValue) + return registerReadValue + +if __name__ == "__main__": + magAlpha = MagAlpha() + for i in range(0, 10): + magAlpha.readRegister(i, True) + magAlpha.readRegister(0, True) + magAlpha.readRegister(1, True) + magAlpha.readAngle(True) + magAlpha.readAngleInDegree(True) + magAlpha.writeRegister(0, 0xFF, True) + magAlpha.writeRegister(1, 0x7F, True) + rawAngle = magAlpha.readAngle(True) + degreeAngle = magAlpha.readAngleInDegree(True) + print 'Test handle return value for readAngle and readAngleInDegree [raw] : {0} \t, [degree] : {1}'.format(rawAngle, degreeAngle) + rawAngle, degreeAngle = magAlpha.readAngleAdvanced(True) + print 'Test handle return value for readAngleAdvanced [raw] : {0} \t, [degree] : {1}'.format(rawAngle, degreeAngle) + for i in range(0, 20): + magAlpha.readAngle(True) + time.sleep(0.1) diff --git a/Adafruit-FT232H-Breakout/magalpha_ft232h_simple_example.py b/Adafruit-FT232H-Breakout/magalpha_ft232h_simple_example.py new file mode 100644 index 0000000..8c5b313 --- /dev/null +++ b/Adafruit-FT232H-Breakout/magalpha_ft232h_simple_example.py @@ -0,0 +1,17 @@ +from magalpha_ft232h_library import MagAlpha +import time + +# Create an instance of the MagAlpha Class +magAlpha = MagAlpha() + +#Example of register settings (some settings may not be available on every sensor model) +#Set zero setting to 0 (Reg 0 and 1) +magAlpha.writeRegister(0, 0) +magAlpha.writeRegister(1, 0) +#Set Rotation Direction to Clockwise by writting 0 to register 9 +magAlpha.writeRegister(9, 0) + +#Read raw angle value until the user press on Ctr+C (Keyboard interrupt) to exit the program +while True: + angle = magAlpha.readAngle(True) + time.sleep(0.1) diff --git a/Arduino/MagAlpha-Library/README.md b/Arduino/MagAlpha-Library/README.md new file mode 100644 index 0000000..b884392 --- /dev/null +++ b/Arduino/MagAlpha-Library/README.md @@ -0,0 +1,23 @@ +# MagAlpha library +Arduino library for the MPS MagAlpha magnetic angle sensor. + +Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the absolute angular position of a permanent magnet, typically a diametrically magnetized cylinder on the rotating shaft. + +For more information on the MagAlpha sensor family: +* [MagAlpha Product Overview](http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview) +* [MagAlpha Support Materials](http://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support) + +Check [Arduino SPI library reference page](https://www.arduino.cc/en/Reference/SPI) for the SPI signal connections. + +| Warning | +| ------- | +| Unlike most Arduino & Genuino boards, the MagAlpha runs at 3.3V. Even if the I/O can tolerate 5V, check that the voltage applied to VDD is at 3.3V. Applying a voltages higher than 3.3V to the VDD pin could damage the sensor.| + +Written by Mathieu Kaelin for Monolithic Power Systems. +MIT license, all text above must be included in any redistribution + +Place the MagAlpha library folder in your arduinosketchfolder/libraries/ folder. +You may need to create the libraries subfolder if its your first library. Restart the IDE. + +You can also check this tutorial on Arduino library installation: +* [All About Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use) diff --git a/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino b/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino new file mode 100644 index 0000000..efcd5b9 --- /dev/null +++ b/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino @@ -0,0 +1,99 @@ +#include + +//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections + +#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud) +#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz +#define SPI_CS_PIN 0 //SPI CS pin + +MagAlpha magAlpha; + +void setup() { + // put your setup code here, to run once: + //Set the SPI SCLK frequency, SPI Mode and CS pin + magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN); + //Set the Serial Communication used to report the angle + Serial.begin(UART_BAUDRATE); +} + +void loop() { + // put your main code here, to run repeatedly: + //======================================================================== + //Read the angle using different methods + //======================================================================== + uint16_t angle; + uint8_t angle8bit; + double angleInDegree; + + Serial.println("Read Angle using differents methods:"); + + //Read the angle (16-bit raw angle value) + angle = magAlpha.readAngle(); + Serial.print(" magAlpha.readAngle() = "); + Serial.println(angle, DEC); + + //Read the angle (16-bit raw angle value), equivalent to magAlpha.readAngle() function + angle = magAlpha.readAngle16(); + Serial.print(" magAlpha.readAngle16() = "); + Serial.println(angle, DEC); + + //Read the angle (8-bit raw angle value) + angle8bit = magAlpha.readAngle8(); + Serial.print(" magAlpha.readAngle8() = "); + Serial.println(angle, DEC); + + //Read the angle in degree (Read 16-bit raw angle value and then convert it to the 0-360 degree range) + angleInDegree = magAlpha.readAngleInDegree(); + Serial.print(" magAlpha.readAngleInDegree() = "); + Serial.println(angleInDegree, 3); + + //======================================================================== + //Read the zero settings in register 0 and 1 + //======================================================================== + uint8_t readbackRegister0Value, readbackRegister1Value; + //Read MagAlpha Gen3 Zero Settings (Registers 0 and 1 + readbackRegister0Value = magAlpha.readRegister(0); + readbackRegister1Value = magAlpha.readRegister(1); + Serial.println("Read Zero Setting:"); + Serial.print(" Read Register[0] = 0x"); + Serial.println(readbackRegister0Value, HEX); + Serial.print(" Read Register[1] = 0x"); + Serial.println(readbackRegister1Value, HEX); + + //======================================================================== + //Write MagAlpha Gen3 Zero Settings with value 0x7FFF (Registers 0 and 1) + //======================================================================== + readbackRegister0Value = magAlpha.writeRegister(0, 0xFF); + readbackRegister1Value = magAlpha.writeRegister(1, 0x7F); + Serial.println("Write Zero Setting:"); + Serial.print(" Write Register[0] = 0x"); + Serial.println(readbackRegister0Value, HEX); + Serial.print(" Write Register[1] = 0x"); + Serial.println(readbackRegister1Value, HEX); + if ((readbackRegister0Value == 0xFF) && (readbackRegister1Value == 0x7F)) + { + Serial.println(" Write Process Succeed"); + } + else + { + Serial.println(" Write Process Fail"); + } + + //======================================================================== + //Change MagAlpha Gen3 Rotation Direction (Register 9, bit 7) + //======================================================================== + uint8_t readbackRegister9Value; + //Read register 9 and toggle RD state + readbackRegister9Value = magAlpha.readRegister(9); + if ((readbackRegister9Value & 0x80) == 0){ + //Set RD to 1 + magAlpha.writeRegister(9, 0x80); + } + else{ + //Set RD to 0 + magAlpha.writeRegister(9, 0x00); + } + Serial.println("Write Rotation Direction Setting:"); + Serial.print(" Write Register[9] = 0x"); + Serial.println(readbackRegister9Value, HEX); +} diff --git a/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino b/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino new file mode 100644 index 0000000..f7e15cf --- /dev/null +++ b/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino @@ -0,0 +1,27 @@ +#include + +//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections + +#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud) +#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz +#define SPI_CS_PIN 0 //SPI CS pin + +MagAlpha magAlpha; + +void setup() { + // put your setup code here, to run once: + //Set the SPI SCLK frequency, SPI Mode and CS pin + magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN); + //Set the Serial Communication used to report the angle + Serial.begin(UART_BAUDRATE); +} + +void loop() { + // put your main code here, to run repeatedly: + uint16_t angle; + //Read the angle (16-bit raw angle value) + angle = magAlpha.readAngle(); + Serial.println(angle, DEC); + //Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read) + delay(25); +} diff --git a/Arduino/MagAlpha-Library/keywords.txt b/Arduino/MagAlpha-Library/keywords.txt new file mode 100644 index 0000000..8e9f59e --- /dev/null +++ b/Arduino/MagAlpha-Library/keywords.txt @@ -0,0 +1,37 @@ +####################################### +# Syntax Coloring Map For MagAlpha-Library +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +MagAlpha KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +end KEYWORD2 +readAngle KEYWORD2 +readAngle16 KEYWORD2 +readAngle8 KEYWORD2 +readAngleInDegree KEYWORD2 +readRegister KEYWORD2 +writeRegister KEYWORD2 +setSpiClockFrequency KEYWORD2 +setSpiDataMode KEYWORD2 +setSpiChipSelectPin KEYWORD2 +convertRawAngleToDegree KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### +magAlpha KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +MA_SPI_MODE_0 LITERAL1 +MA_SPI_MODE_3 LITERAL1 diff --git a/Arduino/MagAlpha-Library/library.properties b/Arduino/MagAlpha-Library/library.properties new file mode 100644 index 0000000..99e5d0b --- /dev/null +++ b/Arduino/MagAlpha-Library/library.properties @@ -0,0 +1,10 @@ +name=MagAlpha library +version=1.0.0 +author=Mathieu Kaelin, Monolithic Power Systems +maintainer=Mathieu Kaelin +sentence=Arduino library for the MPS MagAlpha magnetic angle sensor. +paragraph=Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the absolute angular position of a permanent magnet, typically a diametrically magnetized cylinder on the rotating shaft. +category=Sensors +url=https://github.com/monolithicpower/MagAlpha-Code-Samples +architectures=* +includes=SPI.h diff --git a/Arduino/MagAlpha-Library/src/MagAlpha.cpp b/Arduino/MagAlpha-Library/src/MagAlpha.cpp new file mode 100644 index 0000000..dfeec2d --- /dev/null +++ b/Arduino/MagAlpha-Library/src/MagAlpha.cpp @@ -0,0 +1,113 @@ +/*************************************************** + Arduino library for the MPS MagAlpha magnetic angle sensor + Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the + absolute angular position of a permanent magnet, typically a diametrically + magnetized cylinder on the rotating shaft. + ----> http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview + Written by Mathieu Kaelin for Monolithic Power Systems. + MIT license, all text above must be included in any redistribution +****************************************************/ + +#include "MagAlpha.h" + +//MagAlpha Read/Write Register Command +#define READ_REG_COMMAND (0b010 << 13) +#define WRITE_REG_COMMAND (0b100 << 13) + +MagAlpha::MagAlpha(){ +} + +void MagAlpha::begin(int32_t spiSclkFrequency, uint8_t spiMode, uint8_t spiChipSelectPin){ + setSpiChipSelectPin(spiChipSelectPin); + _speedMaximum = spiSclkFrequency; + _spiMode = spiMode; + SPI.begin(); + SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); +} + +void MagAlpha::begin(uint8_t spiChipSelectPin){ + setSpiChipSelectPin(spiChipSelectPin); + _speedMaximum = 10000000; + _spiMode = MA_SPI_MODE_3; + SPI.begin(); + SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); +} + +void MagAlpha::end(){ + SPI.end(); +} + +uint16_t MagAlpha::readAngle(){ + return readAngle16(); +} + +uint16_t MagAlpha::readAngle16(){ + uint16_t angle; + digitalWrite(_spiChipSelectPin, LOW); + angle = SPI.transfer16(0x0000); //Read 16-bit angle + //angle = SPI.transfer(0x00); //Read 8-bit angle + digitalWrite(_spiChipSelectPin, HIGH); + return angle; +} + +uint8_t MagAlpha::readAngle8(){ + uint8_t angle; + digitalWrite(_spiChipSelectPin, LOW); + angle = SPI.transfer(0x00); //Read 8-bit angle + digitalWrite(_spiChipSelectPin, HIGH); + return angle; +} + +double MagAlpha::readAngleInDegree(){ + uint16_t angle; + double angleInDegree; + angle = readAngle16(); + angleInDegree = (angle*360.0)/65536.0; + return angleInDegree; +} + +uint8_t MagAlpha::readRegister(uint8_t address){ + uint8_t readbackRegisterValue; + digitalWrite(_spiChipSelectPin, LOW); + SPI.transfer16(READ_REG_COMMAND | ((address & 0x1F) << 8) | 0x00); + digitalWrite(_spiChipSelectPin, HIGH); + digitalWrite(_spiChipSelectPin, LOW); + readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); + digitalWrite(_spiChipSelectPin, HIGH); + return readbackRegisterValue; +} + +uint8_t MagAlpha::writeRegister(uint8_t address, uint8_t value){ + uint8_t readbackRegisterValue; + digitalWrite(_spiChipSelectPin, LOW); + SPI.transfer16(WRITE_REG_COMMAND | ((address & 0x1F) << 8) | value); + digitalWrite(_spiChipSelectPin, HIGH); + delay(20); //Wait for 20ms + digitalWrite(_spiChipSelectPin, LOW); + readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); + digitalWrite(_spiChipSelectPin, HIGH); + //readbackRegisterValue should be equal to the written value + return readbackRegisterValue; +} + +void MagAlpha::setSpiClockFrequency(uint32_t speedMaximum){ + _speedMaximum = speedMaximum; + SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); +} + +void MagAlpha::setSpiDataMode(uint8_t spiMode){ + _spiMode = spiMode; + SPI.setDataMode(_spiMode); +} + +void MagAlpha::setSpiChipSelectPin(uint8_t spiChipSelectPin){ + _spiChipSelectPin = spiChipSelectPin; + pinMode(_spiChipSelectPin, OUTPUT); + digitalWrite(_spiChipSelectPin, HIGH); +} + +double MagAlpha::convertRawAngleToDegree(uint8_t rawAngleDataBitLength, uint16_t rawAngle){ + double angleInDegree; + angleInDegree = (rawAngle*360.0)/((double)pow(2, rawAngleDataBitLength)); + return angleInDegree; +} diff --git a/Arduino/MagAlpha-Library/src/MagAlpha.h b/Arduino/MagAlpha-Library/src/MagAlpha.h new file mode 100644 index 0000000..ab45dc5 --- /dev/null +++ b/Arduino/MagAlpha-Library/src/MagAlpha.h @@ -0,0 +1,46 @@ +/*************************************************** + Arduino library for the MPS MagAlpha magnetic angle sensor + Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the + absolute angular position of a permanent magnet, typically a diametrically + magnetized cylinder on the rotating shaft. + ----> http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview + Written by Mathieu Kaelin for Monolithic Power Systems. + MIT license, all text above must be included in any redistribution + ****************************************************/ + +#ifndef MAGALPHA_H +#define MAGALPHA_H + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif +#include + + //SPI Mode: MagAlpha Gen3 support SPI mode 3 and 0 [SPI_MODE3, SPI_MODE0] +#define MA_SPI_MODE_0 SPI_MODE0 +#define MA_SPI_MODE_3 SPI_MODE3 + +class MagAlpha { +public: + MagAlpha(); + void begin(int32_t spiSclkFrequency, uint8_t spiMode, uint8_t spiChipSelectPin); + void begin(uint8_t spiChipSelectPin); + void end(); + uint16_t readAngle(); + uint16_t readAngle16(); + uint8_t readAngle8(); + double readAngleInDegree(); + uint8_t readRegister(uint8_t address); + uint8_t writeRegister(uint8_t address, uint8_t value); + void setSpiClockFrequency(uint32_t speedMaximum); + void setSpiDataMode(uint8_t spiMode); + void setSpiChipSelectPin(uint8_t spiChipSelectPin); + double convertRawAngleToDegree(uint8_t rawAngleDataBitLength, uint16_t rawAngle); +private: + uint32_t _speedMaximum; + uint8_t _spiMode; + uint8_t _spiChipSelectPin; +}; +#endif //MAGALPHA_H diff --git a/Arduino/magalpha-arduino-simple-example/magalpha-arduino-simple-example.ino b/Arduino/magalpha-arduino-simple-example/magalpha-arduino-simple-example.ino new file mode 100644 index 0000000..661b89a --- /dev/null +++ b/Arduino/magalpha-arduino-simple-example/magalpha-arduino-simple-example.ino @@ -0,0 +1,96 @@ +#include + +//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections + +#define SPI_CS_PIN 0 //Connect Chip Select (CS) on pin +#define SPI_SCLK_FREQUENCY 10000000 //SPI Clock Frequency in Hz (MagAlpha support up to 25000000) +#define SPI_MODE SPI_MODE3 //SPI Mode: MagAlpha Gen3 support SPI mode 3 and 0 [SPI_MODE3, SPI_MODE0] +#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud) +#define READ_REG_COMMAND (0b010 << 13) +#define WRITE_REG_COMMAND (0b100 << 13) + +void setup() { + // put your setup code here, to run once: + pinMode(SPI_CS_PIN, OUTPUT); + digitalWrite(SPI_CS_PIN, HIGH); + Serial.begin(UART_BAUDRATE); + SPI.begin(); + SPI.beginTransaction(SPISettings(SPI_SCLK_FREQUENCY, MSBFIRST, SPI_MODE)); + + //Change the zero reference settings of the MagAlpha + //Read the initial zero settings + uint8_t readbackRegister0Value, readbackRegister1Value; + //Read MagAlpha Gen3 Zero Settings (Registers 0 and 1) + readbackRegister0Value = readRegister(0); + readbackRegister1Value = readRegister(1); + Serial.print("Read Register[0] = "); + Serial.println(readbackRegister0Value, DEC); + Serial.print("Read Register[1] = "); + Serial.println(readbackRegister1Value, DEC); + + //Write a new zero settings (0x7FF) + readbackRegister0Value = writeRegister(0, 0xFF); + readbackRegister1Value = writeRegister(1, 0x7F); + Serial.print("Write Register[0] = "); + Serial.println(readbackRegister0Value, DEC); + Serial.print("Write Register[1] = "); + Serial.println(readbackRegister1Value, DEC); + if ((readbackRegister0Value == 0xFF) && (readbackRegister1Value == 0x7F)) + { + Serial.println("Write Process Succeed"); + } + else + { + Serial.println("Write Process Fail"); + } +} + +void loop() { + // put your main code here, to run repeatedly: + double angleInDegree; + delay(25); + //Read angle in degree. To read the raw angle output from the sensor use readAngle(). + angleInDegree = readAngleInDegree(); + Serial.println(angleInDegree, 3); +} + +uint16_t readAngle(){ + uint16_t angle; + digitalWrite(SPI_CS_PIN, LOW); + angle = SPI.transfer16(0x0000); //Read 16-bit angle + //angle = SPI.transfer(0x00); //Read 8-bit angle + digitalWrite(SPI_CS_PIN, HIGH); + return angle; +} + +double readAngleInDegree(){ + uint16_t angle; + double angleInDegree; + angle = readAngle(); + angleInDegree = (angle*360.0)/65536.0; + return angleInDegree; +} + +uint8_t readRegister(uint8_t address){ + uint8_t readbackRegisterValue; + digitalWrite(SPI_CS_PIN, LOW); + SPI.transfer16(READ_REG_COMMAND | ((address & 0x1F) << 8) | 0x00); + digitalWrite(SPI_CS_PIN, HIGH); + digitalWrite(SPI_CS_PIN, LOW); + readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); + digitalWrite(SPI_CS_PIN, HIGH); + return readbackRegisterValue; +} + +uint8_t writeRegister(uint8_t address, uint8_t value){ + uint8_t readbackRegisterValue; + digitalWrite(SPI_CS_PIN, LOW); + SPI.transfer16(WRITE_REG_COMMAND | ((address & 0x1F) << 8) | value); + digitalWrite(SPI_CS_PIN, HIGH); + delay(20); //Wait for 20ms + digitalWrite(SPI_CS_PIN, LOW); + readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); + digitalWrite(SPI_CS_PIN, HIGH); + //readbackRegisterValue should be equal to the written value + return readbackRegisterValue; +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ae778b7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 MPS Tech Switzerland LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index eca6c4e..55dcb5a 100644 --- a/README.md +++ b/README.md @@ -1 +1,31 @@ -# MagAlpha-Code-Samples \ No newline at end of file +# MagAlpha-Code-Samples +This repository contains code samples for the MPS MagAlpha position sensor family. + +## License +All code samples are released under the MIT License. + +# Supported Hardware +We currently have code examples for the following hardware platforms: +* [Arduino](https://www.arduino.cc/) (works with the following 3.3V boards) + * [Arduino ZERO](https://www.arduino.cc/en/Main/ArduinoBoardZero) (use SPI connector and USB programming port) + * [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue) (use SPI connector and USB programming port) + * [Adafruit Feather M0 Basic Proto](https://www.adafruit.com/products/2772) (should also works with other feather M0 boards) + * [Arduino 101](https://www.arduino.cc/en/Main/ArduinoBoard101) (not tested) + * [Arduino MKRZero](https://www.arduino.cc/en/Main/ArduinoBoardMKRZero) (not tested) + +* [Adafruit FT232H Breakout](https://www.adafruit.com/products/2264) (use Python) + + +# About MagAlpha Sensor Familiy +MagAlpha sensor family is based on Hall devices that are directly integrated with the signal treatment. These sensors are extremely compact and can instantaneously detects and delivers the angle value in digital format. + +More information can be found on the [MPS website](https://www.monolithicpower.com/Products/Position-Sensors/Products-Overview) or directly on the [Position Sensors Design Support ](https://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support) page. +## Applications +### Potentiometer/Encoder Alternative +The MagAlpha sensor family offer a robust contactless angle encoder suitable for control buttons and knobs. The IC detects the absolute angular position of a permanent magnet, typically a diametrically magnetized cylinder attached to the rotor. The PWM digital output can be filtered to obtain an analog signal. + +### Motor Commutation +A MagAlpha sensor can replace a 3 Hall switch solution (UVW) or an incremental optical encoder (ABZ, ABI) for a fraction of the initial cost. Making it a perfect fit for Brushless DC motors (BLDC). Thanks to its extremely fast acquisition time, the MagAlpha can be used at speeds from 0 to 120'000 RPM. + +### Angle Measurement +The MagAlpha digital interface can be connected to any microcontroller having a SPI interface. It is extremely easy to monitor the angular position and configure sensor parameters such as the zero settings and the bias current trimming (BCT)