Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new sensors without breaking backwards compatibility #6

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ jobs:
platforms: |
- name: rp2040:rp2040
source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
- fqbn: rp2040:rp2040:rpipico2
platforms: |
- name: rp2040:rp2040
source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

steps:
- name: Checkout
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ For more information on the MagAlpha sensor family:
* [MagAlpha Support Materials](http://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support)

## Supported sensors
Supports all 3rd generation MagAlpha magnetic angle sensors from [Monolithic Power Systems](https://www.monolithicpower.com/).
Supports all 3rd, 4th, 6th, 7th, 8th generation MagAlpha magnetic angle sensors from [Monolithic Power Systems](https://www.monolithicpower.com/).

| Applications | Part Numbers |
| ------------| ------------ |
| Turning knob applications (potentiometer replacement) | MA800, MA820, MA850 |
| Rotary encoders (optical encoder replacement, Servo motors, ...) | MA702, MA704, MA710, MA730 |
| Position controlled motor drivers (FOC, ...) | MA302, MA310 |
| Position controlled motor drivers (FOC, ...) | MA302, MA310, MA600 |
| Motor commutation (hall switches replacement) | MA102 |


Expand All @@ -39,6 +39,7 @@ MIT license, all text above must be included in any redistribution.

### Serial communication
All MagAlpha have a SPI communication interface. Some sensors like the MA702 also have an additional SSI (2-wire) interface.
MagAlpha gen8 possess an I2C interface.

#### SPI (4-wire interface)
| Arduino | MagAlpha |
Expand All @@ -54,6 +55,12 @@ All MagAlpha have a SPI communication interface. Some sensors like the MA702 als
| MISO | SSD |
| SCK | SSCK |

### I2C (2-wire interface)
| Arduino | MagAlpha |
| -------- | -------- |
| SCL | SCL |
| SDA | SDA |

#### Arduino SPI pin mapping
| Arduino / Genuino Board | MOSI | MISO | SCK | CS | Voltage Level |
| ----------------------- | ---- | ---- | ---- | :---: | ------------- |
Expand Down
90 changes: 90 additions & 0 deletions examples/gen3/gen3.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <SPI.h>

#include <MagAlphaGen3.h>
#include <MagAlphaPartProperties.h>

#define SPI_CS_PIN (7)
#define SPI_SCLK_FREQUENCY (1000000)

MagAlphaGen3 magalpha;

double angle_real;
uint16_t zero, bct, angle_raw;
uint8_t register_value[2];
bool error;

void setup() {

magalpha.begin(SPI_SCLK_FREQUENCY, MagAlphaSPIMode::MODE_0, SPI_CS_PIN, &SPI);
// Other ways of use of begin() that will set default settings to the SPI peripheral
// magalpha.begin(SPI_CS_PIN, &SPI);

//magalpha.setSpiClockFrequency(SPI_SCLK_FREQUENCY);
//magalpha.setSpiDataMode(MagAlphaSPIMode::MODE0);
//magalpha.setSpiChipSelectPin(SPI_CS_PIN);
Serial.begin(115200);
while(!Serial);
}

void loop() {

// Read & Write registers
register_value[0] = magalpha.writeRegister(0, 0xAA);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.readRegister(0);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.writeRegister(0, 0x00);

// Read & Write registers in burst mode
register_value[0] = 0xAA;
register_value[1] = 0x55;
magalpha.writeRegisterBurst(0, register_value, 2);
magalpha.readRegisterBurst(0, register_value, 2);
for(uint8_t i = 0; i < sizeof(register_value); i++) {
Serial.print("Register ");
Serial.print(i);
Serial.print(" = ");
Serial.println(register_value[i], HEX);
}
register_value[0] = 0x00;
register_value[1] = 0x00;
magalpha.writeRegisterBurst(0, register_value, 2);

// Set & Get zero setting
zero = magalpha.setCurrentAngleAsZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);
magalpha.setZero(0x0000);
zero = magalpha.getZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);

// Set & Get BCT setting
magalpha.setBct(0xAA55);
bct = magalpha.getBct();
Serial.print("BCT = ");
Serial.println(bct, HEX);
magalpha.setBct(0x00);

// Read angle
angle_raw = magalpha.readAngleRaw8();
Serial.print("Angle raw (8 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw16();
Serial.print("Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw(&error);
if(error) {
Serial.println("An error occured during readAngleRaw");
} else {
Serial.print("readAngleRaw succeded, Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
}

angle_real = magalpha.readAngle();
Serial.print("Angle in degree = ");
Serial.println(angle_real, DEC);

// This delay has been added only for visual purpose of the terminal
delay(25);
}
108 changes: 108 additions & 0 deletions examples/gen4/gen4.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <SPI.h>

#include <MagAlphaGen4.h>
#include <MagAlphaPartProperties.h>

#define SPI_CS_PIN (7)
#define SPI_SCLK_FREQUENCY (1000000)

MagAlphaGen4 magalpha;

double angle_real;
uint16_t zero, bct, angle_raw;
uint8_t register_value[2];
bool error;

void setup() {

magalpha.begin(SPI_SCLK_FREQUENCY, MagAlphaSPIMode::MODE_0, SPI_CS_PIN, &SPI);
// Other ways of use of begin() that will set default settings to the SPI peripheral
// magalpha.begin(SPI_CS_PIN, &SPI);

//magalpha.setSpiClockFrequency(SPI_SCLK_FREQUENCY);
//magalpha.setSpiDataMode(MagAlphaSPIMode::MODE0);
//magalpha.setSpiChipSelectPin(SPI_CS_PIN);
Serial.begin(115200);
while(!Serial);
}

void loop() {

// Read & Write registers
register_value[0] = magalpha.writeRegister(0, 0xAA);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.readRegister(0);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.writeRegister(0, 0x00);

// Read & Write registers in burst mode
register_value[0] = 0xAA;
register_value[1] = 0x55;
magalpha.writeRegisterBurst(0, register_value, 2);
magalpha.readRegisterBurst(0, register_value, 2);
for(uint8_t i = 0; i < sizeof(register_value); i++) {
Serial.print("Register ");
Serial.print(i);
Serial.print(" = ");
Serial.println(register_value[i], HEX);
}
register_value[0] = 0x00;
register_value[1] = 0x00;
magalpha.writeRegisterBurst(0, register_value, 2);

// Set & Get zero setting
zero = magalpha.setCurrentAngleAsZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);
magalpha.setZero(0x0000);
zero = magalpha.getZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);

// Set & Get BCT setting
magalpha.setBct(0xAA55);
bct = magalpha.getBct();
Serial.print("BCT = ");
Serial.println(bct, HEX);
magalpha.setBct(0x00);

// Memory management
magalpha.writeRegister(0, 0xAA);
magalpha.storeRegister(0);
magalpha.writeRegister(0, 0x00);
magalpha.restoreAllRegisters();
if(magalpha.readRegister(0) != 0xAA) {
Serial.println("An error occured during restoreAllRegisters");
}
magalpha.writeRegister(0, 0x00);
magalpha.storeAllRegisters();
magalpha.restoreAllRegisters();
if(magalpha.readRegister(0) != 0x00) {
Serial.println("An error occured during storeAllRegisters");
}

// Clear error flags
magalpha.clearErrorFlags();

// Read angle
angle_raw = magalpha.readAngleRaw8();
Serial.print("Angle raw (8 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw16();
Serial.print("Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw(&error);
if(error) {
Serial.println("An error occured during readAngleRaw");
} else {
Serial.print("readAngleRaw succeded, Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
}

angle_real = magalpha.readAngle();
Serial.print("Angle in degree = ");
Serial.println(angle_real, DEC);

// This delay has been added only for visual purpose of the terminal
delay(25);
}
113 changes: 113 additions & 0 deletions examples/gen6/gen6.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <SPI.h>

#include <MagAlphaGen6.h>
#include <MagAlphaPartProperties.h>

#define SPI_CS_PIN (7)
#define SPI_SCLK_FREQUENCY (1000000)

MagAlphaGen6 magalpha;

double angle_real, speed;
uint16_t zero, bct, angle_raw, turn;
uint8_t register_value[2];
bool error;

void setup() {

magalpha.begin(SPI_SCLK_FREQUENCY, MagAlphaSPIMode::MODE_0, SPI_CS_PIN, &SPI);
// Other ways of use of begin() that will set default settings to the SPI peripheral
// magalpha.begin(SPI_CS_PIN, &SPI);

//magalpha.setSpiClockFrequency(SPI_SCLK_FREQUENCY);
//magalpha.setSpiDataMode(MagAlphaSPIMode::MODE0);
//magalpha.setSpiChipSelectPin(SPI_CS_PIN);
Serial.begin(115200);
while(!Serial);
}

void loop() {

// Read & Write registers
register_value[0] = magalpha.writeRegister(0, 0xAA);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.readRegister(0);
Serial.println(register_value[0], HEX);
register_value[0] = magalpha.writeRegister(0, 0x00);

// Read & Write registers in burst mode
register_value[0] = 0xAA;
register_value[1] = 0x55;
magalpha.writeRegisterBurst(0, register_value, 2);
magalpha.readRegisterBurst(0, register_value, 2);
for(uint8_t i = 0; i < sizeof(register_value); i++) {
Serial.print("Register ");
Serial.print(i);
Serial.print(" = ");
Serial.println(register_value[i], HEX);
}
register_value[0] = 0x00;
register_value[1] = 0x00;
magalpha.writeRegisterBurst(0, register_value, 2);

// Set & Get zero setting
zero = magalpha.setCurrentAngleAsZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);
magalpha.setZero(0x0000);
zero = magalpha.getZero();
Serial.print("Zero setting = ");
Serial.println(zero, HEX);

// Set & Get BCT setting
magalpha.setBct(0xAA55);
bct = magalpha.getBct();
Serial.print("BCT = ");
Serial.println(bct, HEX);
magalpha.setBct(0x00);

// Memory management
magalpha.writeRegister(0, 0xAA);
magalpha.storeRegisterBlock(0);
magalpha.writeRegister(0, 0x00);
magalpha.restoreAllRegisters();
if(magalpha.readRegister(0) != 0xAA) {
Serial.println("An error occured during restoreAllRegisters");
}
magalpha.writeRegister(0, 0x00);
magalpha.storeAllRegisters();
magalpha.restoreAllRegisters();
if(magalpha.readRegister(0) != 0x00) {
Serial.println("An error occured during storeAllRegisters");
}

// Clear error flags
magalpha.clearErrorFlags();

speed = magalpha.readSpeed();
Serial.println(speed);
turn = magalpha.readTurn();
Serial.println(turn);

// Read angle
angle_raw = magalpha.readAngleRaw8();
Serial.print("Angle raw (8 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw16();
Serial.print("Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
angle_raw = magalpha.readAngleRaw(&error);
if(error) {
Serial.println("An error occured during readAngleRaw");
} else {
Serial.print("readAngleRaw succeded, Angle raw (16 bits) = ");
Serial.println(angle_raw, HEX);
}

angle_real = magalpha.readAngle();
Serial.print("Angle in degree = ");
Serial.println(angle_real, DEC);

// This delay has been added only for visual purpose of the terminal
delay(25);
}
Loading
Loading