-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for Arduino boards (simple example and complete arduino l…
…ibrary) Add example for Adafruit FT232H breakout board (use python2.7) Update readme
- Loading branch information
1 parent
2699b45
commit d500ce1
Showing
12 changed files
with
603 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
17 changes: 17 additions & 0 deletions
17
Adafruit-FT232H-Breakout/magalpha_ft232h_simple_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
99 changes: 99 additions & 0 deletions
99
Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <MagAlpha.h> | ||
|
||
//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); | ||
} |
27 changes: 27 additions & 0 deletions
27
Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <MagAlpha.h> | ||
|
||
//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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=MagAlpha library | ||
version=1.0.0 | ||
author=Mathieu Kaelin, Monolithic Power Systems <sensors@monolithicpower.com> | ||
maintainer=Mathieu Kaelin <sensors@monolithicpower.com> | ||
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 |
Oops, something went wrong.