try to fix submodule
This commit is contained in:
125
firmware/lib/Arduino-FOC-drivers/src/encoders/ma730/MA730.cpp
Normal file
125
firmware/lib/Arduino-FOC-drivers/src/encoders/ma730/MA730.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "MA730.h"
|
||||
|
||||
MA730::MA730(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
|
||||
|
||||
};
|
||||
MA730::~MA730() {
|
||||
|
||||
};
|
||||
|
||||
void MA730::init(SPIClass* _spi) {
|
||||
spi = _spi;
|
||||
if (nCS >= 0) {
|
||||
pinMode(nCS, OUTPUT);
|
||||
digitalWrite(nCS, HIGH);
|
||||
}
|
||||
};
|
||||
|
||||
float MA730::getCurrentAngle() {
|
||||
return (readRawAngle() * _2PI)/MA730_CPR;
|
||||
}; // angle in radians, return current value
|
||||
|
||||
uint16_t MA730::readRawAngle() {
|
||||
uint16_t angle = transfer16(0x0000);
|
||||
return angle;
|
||||
}; // 14bit angle value
|
||||
|
||||
uint16_t MA730::getZero() {
|
||||
uint16_t result = readRegister(MA730_REG_ZERO_POSITION_MSB)<<8;
|
||||
result |= readRegister(MA730_REG_ZERO_POSITION_LSB);
|
||||
return result;
|
||||
};
|
||||
uint8_t MA730::getBiasCurrentTrimming() {
|
||||
return readRegister(MA730_REG_BCT);
|
||||
};
|
||||
bool MA730::isBiasCurrrentTrimmingX() {
|
||||
return (readRegister(MA730_REG_ET) & 0x01)==0x01;
|
||||
};
|
||||
bool MA730::isBiasCurrrentTrimmingY() {
|
||||
return (readRegister(MA730_REG_ET) & 0x02)==0x02;
|
||||
};
|
||||
uint16_t MA730::getPulsesPerTurn() {
|
||||
uint16_t result = readRegister(MA730_REG_ILIP_PPT_LSB)>>6;
|
||||
result |= ((uint16_t)readRegister(MA730_REG_PPT_MSB))<<2;
|
||||
return result+1;
|
||||
};
|
||||
uint8_t MA730::getIndexLength() {
|
||||
return (readRegister(MA730_REG_ILIP_PPT_LSB)>>2)&0x0F;
|
||||
};
|
||||
uint8_t MA730::getRotationDirection() {
|
||||
return (readRegister(MA730_REG_RD)>>7);
|
||||
};
|
||||
uint8_t MA730::getFieldStrengthHighThreshold() {
|
||||
return (readRegister(MA730_REG_MGLT_MGHT)>>2)&0x07;
|
||||
};
|
||||
uint8_t MA730::getFieldStrengthLowThreshold() {
|
||||
return (readRegister(MA730_REG_MGLT_MGHT)>>5)&0x07;
|
||||
};
|
||||
FieldStrength MA730::getFieldStrength() {
|
||||
return (FieldStrength)(readRegister(MA730_REG_MGH_MGL)>>6);
|
||||
};
|
||||
|
||||
|
||||
|
||||
void MA730::setZero(uint16_t value) {
|
||||
writeRegister(MA730_REG_ZERO_POSITION_MSB, value>>8);
|
||||
writeRegister(MA730_REG_ZERO_POSITION_LSB, value&0x00FF);
|
||||
};
|
||||
void MA730::setBiasCurrentTrimming(uint8_t value) {
|
||||
writeRegister(MA730_REG_BCT, value);
|
||||
};
|
||||
void MA730::setBiasCurrrentTrimmingEnabled(bool Xenabled, bool Yenabled) {
|
||||
uint8_t val = Xenabled ? 0x01 : 0x00;
|
||||
val |= (Yenabled ? 0x02 : 0x00);
|
||||
writeRegister(MA730_REG_ET, val);
|
||||
};
|
||||
void MA730::setPulsesPerTurn(uint16_t value) {
|
||||
uint16_t pptVal = value - 1;
|
||||
writeRegister(MA730_REG_PPT_MSB, pptVal>>2);
|
||||
uint8_t val = readRegister(MA730_REG_ILIP_PPT_LSB);
|
||||
val &= 0x3F;
|
||||
val |= (pptVal&0x03)<<6;
|
||||
writeRegister(MA730_REG_ILIP_PPT_LSB, val);
|
||||
};
|
||||
void MA730::setIndexLength(uint8_t value) {
|
||||
uint8_t val = readRegister(MA730_REG_ILIP_PPT_LSB);
|
||||
val &= 0xC0;
|
||||
val |= ((value<<2)&0x3F);
|
||||
writeRegister(MA730_REG_ILIP_PPT_LSB, val);
|
||||
};
|
||||
void MA730::setRotationDirection(uint8_t value) {
|
||||
if (value==0)
|
||||
writeRegister(MA730_REG_RD, 0x00);
|
||||
else
|
||||
writeRegister(MA730_REG_RD, 0x80);
|
||||
};
|
||||
void MA730::setFieldStrengthThresholds(uint8_t high, uint8_t low) {
|
||||
uint8_t val = (low<<5) | (high<<2);
|
||||
writeRegister(MA730_REG_MGLT_MGHT, val);
|
||||
};
|
||||
|
||||
|
||||
uint16_t MA730::transfer16(uint16_t outValue) {
|
||||
if (nCS >= 0)
|
||||
digitalWrite(nCS, LOW);
|
||||
spi->beginTransaction(settings);
|
||||
uint16_t value = spi->transfer16(outValue);
|
||||
spi->endTransaction();
|
||||
if (nCS >= 0)
|
||||
digitalWrite(nCS, HIGH);
|
||||
return value;
|
||||
};
|
||||
uint8_t MA730::readRegister(uint8_t reg) {
|
||||
uint16_t cmd = 0x4000 | ((reg&0x001F)<<8);
|
||||
uint16_t value = transfer16(cmd);
|
||||
delayMicroseconds(1);
|
||||
value = transfer16(0x0000);
|
||||
return value>>8;
|
||||
};
|
||||
uint8_t MA730::writeRegister(uint8_t reg, uint8_t value) {
|
||||
uint16_t cmd = 0x8000 | ((reg&0x1F)<<8) | value;
|
||||
uint16_t result = transfer16(cmd);
|
||||
delay(20); // 20ms delay required
|
||||
result = transfer16(0x0000);
|
||||
return result>>8;
|
||||
};
|
||||
76
firmware/lib/Arduino-FOC-drivers/src/encoders/ma730/MA730.h
Normal file
76
firmware/lib/Arduino-FOC-drivers/src/encoders/ma730/MA730.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef __MA730_H__
|
||||
#define __MA730_H__
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SPI.h"
|
||||
|
||||
enum FieldStrength : uint8_t {
|
||||
FS_NORMAL = 0x00,
|
||||
FS_LOW = 0x01,
|
||||
FS_HIGH = 0x02,
|
||||
FS_ERR = 0x03 // impossible state
|
||||
};
|
||||
|
||||
|
||||
#define _2PI 6.28318530718f
|
||||
#define MA730_CPR 65536.0f
|
||||
|
||||
#define MA730_REG_ZERO_POSITION_LSB 0x00
|
||||
#define MA730_REG_ZERO_POSITION_MSB 0x01
|
||||
#define MA730_REG_BCT 0x02
|
||||
#define MA730_REG_ET 0x03
|
||||
#define MA730_REG_ILIP_PPT_LSB 0x04
|
||||
#define MA730_REG_PPT_MSB 0x05
|
||||
#define MA730_REG_MGLT_MGHT 0x06
|
||||
#define MA730_REG_RD 0x09
|
||||
#define MA730_REG_MGH_MGL 0x1B
|
||||
|
||||
#define MA730_BITORDER MSBFIRST
|
||||
|
||||
static SPISettings MA730SPISettings(1000000, MA730_BITORDER, SPI_MODE3); // @suppress("Invalid arguments")
|
||||
static SPISettings MA730SSISettings(4000000, MA730_BITORDER, SPI_MODE1); // @suppress("Invalid arguments")
|
||||
|
||||
|
||||
class MA730 {
|
||||
public:
|
||||
MA730(SPISettings settings = MA730SPISettings, int nCS = -1);
|
||||
virtual ~MA730();
|
||||
|
||||
virtual void init(SPIClass* _spi = &SPI);
|
||||
|
||||
float getCurrentAngle(); // angle in radians, return current value
|
||||
|
||||
uint16_t readRawAngle(); // 14bit angle value
|
||||
uint16_t readRawAngleSSI(); // 14bit angle value
|
||||
|
||||
uint16_t getZero();
|
||||
uint8_t getBiasCurrentTrimming();
|
||||
bool isBiasCurrrentTrimmingX();
|
||||
bool isBiasCurrrentTrimmingY();
|
||||
uint16_t getPulsesPerTurn();
|
||||
uint8_t getIndexLength();
|
||||
uint8_t getRotationDirection();
|
||||
uint8_t getFieldStrengthHighThreshold();
|
||||
uint8_t getFieldStrengthLowThreshold();
|
||||
FieldStrength getFieldStrength();
|
||||
|
||||
void setZero(uint16_t);
|
||||
void setBiasCurrentTrimming(uint8_t);
|
||||
void setBiasCurrrentTrimmingEnabled(bool Xenabled, bool Yenabled);
|
||||
void setPulsesPerTurn(uint16_t);
|
||||
void setIndexLength(uint8_t);
|
||||
void setRotationDirection(uint8_t);
|
||||
void setFieldStrengthThresholds(uint8_t high, uint8_t low);
|
||||
|
||||
private:
|
||||
SPIClass* spi;
|
||||
SPISettings settings;
|
||||
int nCS = -1;
|
||||
|
||||
uint16_t transfer16(uint16_t outValue);
|
||||
uint8_t readRegister(uint8_t reg);
|
||||
uint8_t writeRegister(uint8_t reg, uint8_t value);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "./MagneticSensorMA730.h"
|
||||
#include "common/foc_utils.h"
|
||||
#include "common/time_utils.h"
|
||||
|
||||
MagneticSensorMA730::MagneticSensorMA730(int nCS, SPISettings settings) : MA730(settings, nCS) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
MagneticSensorMA730::~MagneticSensorMA730(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MagneticSensorMA730::init(SPIClass* _spi) {
|
||||
this->MA730::init(_spi);
|
||||
this->Sensor::init();
|
||||
}
|
||||
|
||||
|
||||
float MagneticSensorMA730::getSensorAngle() {
|
||||
float angle_data = readRawAngle();
|
||||
angle_data = ( angle_data / (float)MA730_CPR) * _2PI;
|
||||
// return the shaft angle
|
||||
return angle_data;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __MAGNETIC_SENSOR_MA730_H__
|
||||
#define __MAGNETIC_SENSOR_MA730_H__
|
||||
|
||||
|
||||
#include "common/base_classes/Sensor.h"
|
||||
#include "./MA730.h"
|
||||
|
||||
class MagneticSensorMA730 : public Sensor, public MA730 {
|
||||
public:
|
||||
MagneticSensorMA730(int nCS = -1, SPISettings settings = MA730SPISettings);
|
||||
virtual ~MagneticSensorMA730();
|
||||
|
||||
virtual float getSensorAngle() override;
|
||||
|
||||
virtual void init(SPIClass* _spi = &SPI);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "./MagneticSensorMA730SSI.h"
|
||||
#include "common/foc_utils.h"
|
||||
#include "common/time_utils.h"
|
||||
|
||||
MagneticSensorMA730SSI::MagneticSensorMA730SSI(SPISettings settings) : settings(settings) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
MagneticSensorMA730SSI::~MagneticSensorMA730SSI() {
|
||||
|
||||
}
|
||||
|
||||
void MagneticSensorMA730SSI::init(SPIClass* _spi) {
|
||||
this->spi=_spi;
|
||||
this->Sensor::init();
|
||||
}
|
||||
|
||||
// check 40us delay between each read?
|
||||
float MagneticSensorMA730SSI::getSensorAngle() {
|
||||
float angle_data = readRawAngleSSI();
|
||||
angle_data = ( angle_data / (float)MA730_CPR ) * _2PI;
|
||||
// return the shaft angle
|
||||
return angle_data;
|
||||
}
|
||||
|
||||
|
||||
uint16_t MagneticSensorMA730SSI::readRawAngleSSI() {
|
||||
spi->beginTransaction(settings);
|
||||
uint16_t value = spi->transfer16(0x0000);
|
||||
//uint16_t parity = spi->transfer(0x00);
|
||||
spi->endTransaction();
|
||||
return (value<<1); //>>1)&0x3FFF;
|
||||
}; // 14bit angle value
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __MAGNETIC_SENSOR_MA730SSI_H__
|
||||
#define __MAGNETIC_SENSOR_MA730SSI_H__
|
||||
|
||||
|
||||
#include "common/base_classes/Sensor.h"
|
||||
#include "./MA730.h"
|
||||
|
||||
class MagneticSensorMA730SSI : public Sensor {
|
||||
public:
|
||||
MagneticSensorMA730SSI(SPISettings settings = MA730SSISettings);
|
||||
virtual ~MagneticSensorMA730SSI();
|
||||
|
||||
virtual float getSensorAngle() override;
|
||||
|
||||
virtual void init(SPIClass* _spi = &SPI);
|
||||
|
||||
uint16_t readRawAngleSSI();
|
||||
|
||||
private:
|
||||
SPIClass* spi;
|
||||
SPISettings settings;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,74 @@
|
||||
# MA730 SimpleFOC driver
|
||||
|
||||
While MA730 absolute position magnetic rotary encoder is supported by the standard MagneticSensorSPI driver included in the base distribution, this MA730-specific driver includes some optimisations:
|
||||
|
||||
- access to the other registers of the MA730
|
||||
- this driver directly reads the angle with one call to SPI
|
||||
- this will halve the number of 16-bit SPI transfers per simpleFOC loop iteration
|
||||
|
||||
|
||||
## Hardware setup
|
||||
|
||||
Connect as per normal for your SPI bus. No special hardware setup is needed to use this driver.
|
||||
|
||||
## Software setup
|
||||
|
||||
Its actually easier to use than the standard SPI sensor class, because it is less generic:
|
||||
|
||||
```c++
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "SPI.h"
|
||||
#include "SimpleFOC.h"
|
||||
#include "SimpleFOCDrivers.h"
|
||||
#include "encoders/ma730/MagneticSensorMA730.h"
|
||||
|
||||
#define SENSOR1_CS 5 // some digital pin that you're using as the nCS pin
|
||||
MagneticSensorMA730 sensor1(SENSOR1_CS);
|
||||
|
||||
|
||||
void setup() {
|
||||
sensor1.init();
|
||||
}
|
||||
```
|
||||
|
||||
Set some options:
|
||||
|
||||
```c++
|
||||
MagneticSensorMA730 sensor1(SENSOR1_CS, mySPISettings);
|
||||
```
|
||||
|
||||
Use another SPI bus:
|
||||
|
||||
```c++
|
||||
void setup() {
|
||||
sensor1.init(&SPI2);
|
||||
}
|
||||
```
|
||||
|
||||
Here's how you can use it:
|
||||
|
||||
```c++
|
||||
// update the sensor (only needed if using the sensor without a motor)
|
||||
sensor1.update();
|
||||
|
||||
// get the angle, in radians, including full rotations
|
||||
float a1 = sensor1.getAngle();
|
||||
|
||||
// get the velocity, in rad/s - note: you have to call getAngle() on a regular basis for it to work
|
||||
float v1 = sensor1.getVelocity();
|
||||
|
||||
// get the angle, in radians, no full rotations
|
||||
float a2 = sensor1.getCurrentAngle();
|
||||
|
||||
// get the raw 14 bit value
|
||||
uint16_t raw = sensor1.readRawAngle();
|
||||
|
||||
// get the field strength
|
||||
FieldStrength fs = sensor1.getFieldStrength();
|
||||
Serial.print("Field strength: ");
|
||||
Serial.println(fs);
|
||||
|
||||
// set pulses per turn for encoder mode
|
||||
sensor1.setPulsesPerTurn(999); // set to 999 if we want 1000 PPR == 4000 CPR
|
||||
```
|
||||
Reference in New Issue
Block a user