Skip to content

Commit d500ce1

Browse files
committed
Add example for Arduino boards (simple example and complete arduino library)
Add example for Adafruit FT232H breakout board (use python2.7) Update readme
1 parent 2699b45 commit d500ce1

File tree

12 files changed

+603
-1
lines changed

12 files changed

+603
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Adafruit_GPIO as GPIO
2+
import Adafruit_GPIO.FT232H as FT232H
3+
import time
4+
5+
class MagAlpha:
6+
""""MagAlpha Communication Library for 3rd generation sensors (MA302, MA800, ...)."""
7+
8+
def __init__(self):
9+
FT232H.use_FT232H()
10+
self.ft232h = FT232H.FT232H()
11+
self.spiClockFreqInHz = 1000000
12+
self.chipSelectPinOnFt232h = 8
13+
self.spiMode = 0
14+
self.spi = FT232H.SPI(self.ft232h, cs=self.chipSelectPinOnFt232h, max_speed_hz=self.spiClockFreqInHz, mode=self.spiMode, bitorder=FT232H.MSBFIRST)
15+
self.__rawToDegreeConvertionRatio = 360.0/65536.0
16+
17+
def readAngle(self, printEnabled=False):
18+
"""Return the angle [0-65535] (raw sensor output value)."""
19+
response = self.spi.transfer([0x00, 0x00])
20+
angularPositionRaw = (response[0]<<8)+response[1]
21+
if (printEnabled):
22+
print 'Angular Position [raw] : {0}'.format(angularPositionRaw)
23+
return angularPositionRaw
24+
25+
def readAngleInDegree(self, printEnabled=False):
26+
"""Return the angle in degree [0-360] (raw sensor output converted in degree)."""
27+
response = self.spi.transfer([0x00, 0x00])
28+
angularPositionRaw = (response[0]<<8)+response[1]
29+
angularPositionDegree = float(angularPositionRaw)*self.__rawToDegreeConvertionRatio
30+
if (printEnabled):
31+
print 'Angular Position [degree] : {0}'.format(angularPositionDegree)
32+
return angularPositionDegree
33+
34+
def readAngleAdvanced(self, printEnabled=False):
35+
"""Return the angle in raw and degree format."""
36+
response = self.spi.transfer([0x00, 0x00])
37+
angularPositionRaw = (response[0]<<8)+response[1]
38+
angularPositionDegree = float(angularPositionRaw)*self.__rawToDegreeConvertionRatio
39+
if (printEnabled):
40+
print 'Angular Position [raw] : {0} \t, [degree] : {1}'.format(angularPositionRaw, angularPositionDegree)
41+
return angularPositionRaw, angularPositionDegree
42+
43+
def readRegister(self, address, printEnabled=False):
44+
"""Return sensor register value."""
45+
command = 0b01000000 | (address & 0x1F)
46+
self.spi.transfer([command, 0x00])
47+
response = self.spi.transfer([0x00, 0x00])
48+
registerValue = response[0]
49+
if (printEnabled):
50+
print 'Read Register [{0}] \t=\t{1}'.format(address, registerValue)
51+
return registerValue
52+
53+
def writeRegister(self, address, value, printEnabled=False):
54+
"""Return sensor written register value."""
55+
command = 0b10000000 | (address & 0x1F)
56+
registerWriteValue = (value & 0xFF)
57+
self.spi.transfer([command, registerWriteValue])
58+
#wait for 20ms
59+
time.sleep(0.02)
60+
response = self.spi.transfer([0x00, 0x00])
61+
registerReadValue = response[0]
62+
if (printEnabled):
63+
print 'Write Register [{0}] \t=\t{1}, \tReadback Value = {2}'.format(address, registerWriteValue, registerReadValue)
64+
return registerReadValue
65+
66+
if __name__ == "__main__":
67+
magAlpha = MagAlpha()
68+
for i in range(0, 10):
69+
magAlpha.readRegister(i, True)
70+
magAlpha.readRegister(0, True)
71+
magAlpha.readRegister(1, True)
72+
magAlpha.readAngle(True)
73+
magAlpha.readAngleInDegree(True)
74+
magAlpha.writeRegister(0, 0xFF, True)
75+
magAlpha.writeRegister(1, 0x7F, True)
76+
rawAngle = magAlpha.readAngle(True)
77+
degreeAngle = magAlpha.readAngleInDegree(True)
78+
print 'Test handle return value for readAngle and readAngleInDegree [raw] : {0} \t, [degree] : {1}'.format(rawAngle, degreeAngle)
79+
rawAngle, degreeAngle = magAlpha.readAngleAdvanced(True)
80+
print 'Test handle return value for readAngleAdvanced [raw] : {0} \t, [degree] : {1}'.format(rawAngle, degreeAngle)
81+
for i in range(0, 20):
82+
magAlpha.readAngle(True)
83+
time.sleep(0.1)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from magalpha_ft232h_library import MagAlpha
2+
import time
3+
4+
# Create an instance of the MagAlpha Class
5+
magAlpha = MagAlpha()
6+
7+
#Example of register settings (some settings may not be available on every sensor model)
8+
#Set zero setting to 0 (Reg 0 and 1)
9+
magAlpha.writeRegister(0, 0)
10+
magAlpha.writeRegister(1, 0)
11+
#Set Rotation Direction to Clockwise by writting 0 to register 9
12+
magAlpha.writeRegister(9, 0)
13+
14+
#Read raw angle value until the user press on Ctr+C (Keyboard interrupt) to exit the program
15+
while True:
16+
angle = magAlpha.readAngle(True)
17+
time.sleep(0.1)

Arduino/MagAlpha-Library/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# MagAlpha library
2+
Arduino library for the MPS MagAlpha magnetic angle sensor.
3+
4+
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.
5+
6+
For more information on the MagAlpha sensor family:
7+
* [MagAlpha Product Overview](http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview)
8+
* [MagAlpha Support Materials](http://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support)
9+
10+
Check [Arduino SPI library reference page](https://www.arduino.cc/en/Reference/SPI) for the SPI signal connections.
11+
12+
| Warning |
13+
| ------- |
14+
| 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.|
15+
16+
Written by Mathieu Kaelin for Monolithic Power Systems.
17+
MIT license, all text above must be included in any redistribution
18+
19+
Place the MagAlpha library folder in your arduinosketchfolder/libraries/ folder.
20+
You may need to create the libraries subfolder if its your first library. Restart the IDE.
21+
22+
You can also check this tutorial on Arduino library installation:
23+
* [All About Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
5+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
6+
#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz
7+
#define SPI_CS_PIN 0 //SPI CS pin
8+
9+
MagAlpha magAlpha;
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
//Set the SPI SCLK frequency, SPI Mode and CS pin
14+
magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN);
15+
//Set the Serial Communication used to report the angle
16+
Serial.begin(UART_BAUDRATE);
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
//========================================================================
22+
//Read the angle using different methods
23+
//========================================================================
24+
uint16_t angle;
25+
uint8_t angle8bit;
26+
double angleInDegree;
27+
28+
Serial.println("Read Angle using differents methods:");
29+
30+
//Read the angle (16-bit raw angle value)
31+
angle = magAlpha.readAngle();
32+
Serial.print(" magAlpha.readAngle() = ");
33+
Serial.println(angle, DEC);
34+
35+
//Read the angle (16-bit raw angle value), equivalent to magAlpha.readAngle() function
36+
angle = magAlpha.readAngle16();
37+
Serial.print(" magAlpha.readAngle16() = ");
38+
Serial.println(angle, DEC);
39+
40+
//Read the angle (8-bit raw angle value)
41+
angle8bit = magAlpha.readAngle8();
42+
Serial.print(" magAlpha.readAngle8() = ");
43+
Serial.println(angle, DEC);
44+
45+
//Read the angle in degree (Read 16-bit raw angle value and then convert it to the 0-360 degree range)
46+
angleInDegree = magAlpha.readAngleInDegree();
47+
Serial.print(" magAlpha.readAngleInDegree() = ");
48+
Serial.println(angleInDegree, 3);
49+
50+
//========================================================================
51+
//Read the zero settings in register 0 and 1
52+
//========================================================================
53+
uint8_t readbackRegister0Value, readbackRegister1Value;
54+
//Read MagAlpha Gen3 Zero Settings (Registers 0 and 1
55+
readbackRegister0Value = magAlpha.readRegister(0);
56+
readbackRegister1Value = magAlpha.readRegister(1);
57+
Serial.println("Read Zero Setting:");
58+
Serial.print(" Read Register[0] = 0x");
59+
Serial.println(readbackRegister0Value, HEX);
60+
Serial.print(" Read Register[1] = 0x");
61+
Serial.println(readbackRegister1Value, HEX);
62+
63+
//========================================================================
64+
//Write MagAlpha Gen3 Zero Settings with value 0x7FFF (Registers 0 and 1)
65+
//========================================================================
66+
readbackRegister0Value = magAlpha.writeRegister(0, 0xFF);
67+
readbackRegister1Value = magAlpha.writeRegister(1, 0x7F);
68+
Serial.println("Write Zero Setting:");
69+
Serial.print(" Write Register[0] = 0x");
70+
Serial.println(readbackRegister0Value, HEX);
71+
Serial.print(" Write Register[1] = 0x");
72+
Serial.println(readbackRegister1Value, HEX);
73+
if ((readbackRegister0Value == 0xFF) && (readbackRegister1Value == 0x7F))
74+
{
75+
Serial.println(" Write Process Succeed");
76+
}
77+
else
78+
{
79+
Serial.println(" Write Process Fail");
80+
}
81+
82+
//========================================================================
83+
//Change MagAlpha Gen3 Rotation Direction (Register 9, bit 7)
84+
//========================================================================
85+
uint8_t readbackRegister9Value;
86+
//Read register 9 and toggle RD state
87+
readbackRegister9Value = magAlpha.readRegister(9);
88+
if ((readbackRegister9Value & 0x80) == 0){
89+
//Set RD to 1
90+
magAlpha.writeRegister(9, 0x80);
91+
}
92+
else{
93+
//Set RD to 0
94+
magAlpha.writeRegister(9, 0x00);
95+
}
96+
Serial.println("Write Rotation Direction Setting:");
97+
Serial.print(" Write Register[9] = 0x");
98+
Serial.println(readbackRegister9Value, HEX);
99+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
5+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
6+
#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz
7+
#define SPI_CS_PIN 0 //SPI CS pin
8+
9+
MagAlpha magAlpha;
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
//Set the SPI SCLK frequency, SPI Mode and CS pin
14+
magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN);
15+
//Set the Serial Communication used to report the angle
16+
Serial.begin(UART_BAUDRATE);
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
uint16_t angle;
22+
//Read the angle (16-bit raw angle value)
23+
angle = magAlpha.readAngle();
24+
Serial.println(angle, DEC);
25+
//Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read)
26+
delay(25);
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#######################################
2+
# Syntax Coloring Map For MagAlpha-Library
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
MagAlpha KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
end KEYWORD2
17+
readAngle KEYWORD2
18+
readAngle16 KEYWORD2
19+
readAngle8 KEYWORD2
20+
readAngleInDegree KEYWORD2
21+
readRegister KEYWORD2
22+
writeRegister KEYWORD2
23+
setSpiClockFrequency KEYWORD2
24+
setSpiDataMode KEYWORD2
25+
setSpiChipSelectPin KEYWORD2
26+
convertRawAngleToDegree KEYWORD2
27+
28+
#######################################
29+
# Instances (KEYWORD2)
30+
#######################################
31+
magAlpha KEYWORD2
32+
33+
#######################################
34+
# Constants (LITERAL1)
35+
#######################################
36+
MA_SPI_MODE_0 LITERAL1
37+
MA_SPI_MODE_3 LITERAL1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=MagAlpha library
2+
version=1.0.0
3+
author=Mathieu Kaelin, Monolithic Power Systems <sensors@monolithicpower.com>
4+
maintainer=Mathieu Kaelin <sensors@monolithicpower.com>
5+
sentence=Arduino library for the MPS MagAlpha magnetic angle sensor.
6+
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.
7+
category=Sensors
8+
url=https://github.com/monolithicpower/MagAlpha-Code-Samples
9+
architectures=*
10+
includes=SPI.h

0 commit comments

Comments
 (0)