try to fix submodule

This commit is contained in:
2023-11-09 19:02:15 -05:00
parent c1d45aa443
commit deea94b076
366 changed files with 40228 additions and 2 deletions

View File

@@ -0,0 +1,350 @@
/*
* AS5047U.cpp
*
* Created on: 24 Feb 2022
* Author: runger
*/
#include "./AS5047U.h"
AS5047U::AS5047U(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
// nix
}
AS5047U::~AS5047U() {
}
void AS5047U::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0)
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
}
float AS5047U::getCurrentAngle(){
readCorrectedAngle();
return ((float)readCorrectedAngle())/(float)AS5047U_CPR * 2.0f * (float)PI;
}
float AS5047U::getFastAngle(){
return ((float)readCorrectedAngle())/(float)AS5047U_CPR * 2.0f * (float)PI;
}
uint16_t AS5047U::readRawAngle(){
uint16_t command = AS5047U_ANGLEUNC_REG | AS5047U_RW;
uint16_t lastresult = spi_transfer16(command)&AS5047U_RESULT_MASK;
return lastresult;
}
uint16_t AS5047U::readCorrectedAngle(){
uint16_t command = AS5047U_ANGLECOM_REG | AS5047U_RW;
uint16_t lastresult = spi_transfer16(command)&AS5047U_RESULT_MASK;
return lastresult;
}
uint16_t AS5047U::readMagnitude(){
uint16_t command = AS5047U_MAGNITUDE_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result;
}
uint16_t AS5047U::readVelocity(){
uint16_t command = AS5047U_VELOCITY_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result;
}
bool AS5047U::isErrorFlag(){
return errorflag;
}
bool AS5047U::isWarningFlag(){
return warningflag;
}
AS5047UError AS5047U::clearErrorFlag(){
uint16_t command = AS5047U_ERROR_REG | AS5047U_RW; // set r=1, result is 0x4001
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UError result;
result.reg = nop16();
return result;
}
AS5047USettings1 AS5047U::readSettings1(){
uint16_t command = AS5047U_SETTINGS1_REG | AS5047U_RW; // set r=1, result is 0xC018
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings1 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings1(AS5047USettings1 settings){
writeRegister24(AS5047U_SETTINGS1_REG, settings.reg);
}
AS5047USettings2 AS5047U::readSettings2(){
uint16_t command = AS5047U_SETTINGS2_REG | AS5047U_RW; // set r=1, result is 0x4019
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings2 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings2(AS5047USettings2 settings){
writeRegister24(AS5047U_SETTINGS2_REG, settings.reg);
}
AS5047USettings3 AS5047U::readSettings3(){
uint16_t command = AS5047U_SETTINGS3_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings3 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings3(AS5047USettings3 settings){
writeRegister24(AS5047U_SETTINGS3_REG, settings.reg);
}
AS5047UDiagnostics AS5047U::readDiagnostics(){
uint16_t command = AS5047U_DIAGNOSTICS_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UDiagnostics result = {
.reg = nop16()
};
return result;
}
uint8_t AS5047U::readAGC(){
uint16_t command = AS5047U_AGC_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result & 0x00FF;
};
uint8_t AS5047U::readECCCHK(){
uint16_t command = AS5047U_ECCCHK_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result & 0x007F;
};
AS5047UDisableSettings AS5047U::readDisableSettings(){
uint16_t command = AS5047U_DISABLE_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UDisableSettings result = {
.reg = nop16()
};
return result;
};
void AS5047U::writeDisableSettings(AS5047UDisableSettings settings){
writeRegister24(AS5047U_DISABLE_REG, settings.reg);
};
AS5047UECCSettings AS5047U::readECCSettings(){
uint16_t command = AS5047U_ECC_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UECCSettings result = {
.reg = nop16()
};
return result;
};
void AS5047U::writeECCSettings(AS5047UECCSettings settings){
writeRegister24(AS5047U_ECC_REG, settings.reg);
};
void AS5047U::enablePWM(bool enable, bool pwmOnWPin){
// AS5047UDisableSettings settings = readDisableSettings();
// if (settings.uvw_off==1) {
// settings.uvw_off = 0;
// writeDiableSettings(settings);
// }
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = pwmOnWPin?0:1;
settings2.pwm_on = enable;
writeSettings2(settings2);
}
void AS5047U::enableABI(bool enable){
AS5047UDisableSettings settings = readDisableSettings();
settings.abi_off = enable?0:1;
writeDisableSettings(settings);
delayMicroseconds(50);
if (enable) {
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = 0;
writeSettings2(settings2);
}
}
void AS5047U::enableUVW(bool enable){
AS5047UDisableSettings settings = readDisableSettings();
settings.uvw_off = enable?0:1;
writeDisableSettings(settings);
if (enable) {
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = 1;
writeSettings2(settings2);
}
}
uint16_t AS5047U::getZero(){
uint16_t command = AS5047U_ZPOSM_REG | AS5047U_RW;
spi_transfer16(command);
command = AS5047U_ZPOSL_REG | AS5047U_RW;
uint16_t result = spi_transfer16(command);
AS5047UZPosL posL = {
.reg = nop16()
};
return ((result&0x00FF)<<6) | posL.zposl;
}
uint16_t AS5047U::setZero(uint16_t value){
uint16_t command = AS5047U_ZPOSL_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UZPosL posL = {
.reg = nop16()
};
posL.zposl = value&0x003F;
writeRegister24(AS5047U_ZPOSL_REG, posL.reg);
writeRegister24(AS5047U_ZPOSM_REG, (value>>6)&0x00FF);
return getZero();
}
uint16_t AS5047U::nop16(){
uint16_t result = spi_transfer16(0xFFFF); // using 0xFFFF as nop instead of 0x0000, then next call to fastAngle will return an angle
return result&AS5047U_RESULT_MASK;
}
uint16_t AS5047U::spi_transfer16(uint16_t outdata) {
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
errorflag = ((result&AS5047U_ERROR)>0);
warningflag = ((result&AS5047U_WARNING)>0);
return result;
}
uint8_t AS5047U::calcCRC(uint16_t data){
uint8_t crc = 0xC4; // Initial value
for (int i = 0; i < 16; i++) {
if ((crc ^ data) & 0x8000) {
crc = (crc << 1) ^ 0x1D;
} else {
crc <<= 1;
}
data <<= 1;
}
return crc ^ 0xFF;
}
uint16_t AS5047U::writeRegister24(uint16_t reg, uint16_t data) {
uint8_t buff[3];
buff[0] = (reg>>8)&0x3F;
buff[1] = reg&0xFF;
buff[2] = calcCRC(reg);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
spi->transfer(buff, 3);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);
buff[0] = (data>>8)&0x3F;
buff[1] = data&0xFF;
buff[2] = calcCRC(data);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
spi->transfer(buff, 3);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);
delayMicroseconds(50);
return buff[0]<<8 | buff[1];
}

View File

@@ -0,0 +1,225 @@
/*
* AS5047.h
*
* Created on: 24 Feb 2022
* Author: runger
*/
#ifndef AS5047U_H_
#define AS5047U_H_
#include "Arduino.h"
#include "SPI.h"
union AS5047UDisableSettings {
struct {
uint16_t :9;
uint16_t filter_disable:1;
uint16_t :4;
uint16_t abi_off:1;
uint16_t uvw_off:1;
};
uint16_t reg;
};
union AS5047UDiagnostics {
struct {
uint16_t spi_cnt:2;
uint16_t :1;
uint16_t agc_finished:1;
uint16_t off_finished:1;
uint16_t sin_finished:1;
uint16_t cos_finished:1;
uint16_t maghalf_flag:1;
uint16_t comp_h:1;
uint16_t comp_l:1;
uint16_t cordic_ovf:1;
uint16_t loops_finished:1;
uint16_t vdd_mode:1;
};
uint16_t reg;
};
union AS5047UZPosM {
struct {
uint16_t zposm:8;
};
uint16_t reg;
};
union AS5047UZPosL {
struct {
uint16_t zposl:6;
uint16_t dia1_en:1; // automotive version only
uint16_t dia2_en:1; // automotive version only
};
uint16_t reg;
};
union AS5047USettings1 {
struct {
uint16_t k_max:3;
uint16_t k_min:3;
uint16_t dia3_en:1; // not applicable
uint16_t dia4_en:1; // not applicable
};
uint16_t reg;
};
union AS5047USettings2 {
struct {
uint16_t iwidth:1;
uint16_t noiseset:1;
uint16_t dir:1;
uint16_t uvw_abi:1;
uint16_t daecdis:1;
uint16_t abi_dec:1;
uint16_t data_select:1;
uint16_t pwm_on:1;
};
uint16_t reg;
};
union AS5047USettings3 {
struct {
uint16_t uvwpp:3;
uint16_t hys:2;
uint16_t abires:3;
};
uint16_t reg;
};
union AS5047UECCSettings {
struct {
uint16_t ecc_chsum:7;
uint16_t ecc_en:1;
};
uint16_t reg;
};
struct AS5047UError {
struct {
uint16_t cordic_ovf:1;
uint16_t off_notfinished:1;
uint16_t :1;
uint16_t wdtst:1;
uint16_t crc_error:1;
uint16_t cmd_error:1;
uint16_t frame_error:1;
uint16_t p2ram_error:1;
uint16_t p2ram_warning:1;
uint16_t maghalf:1;
uint16_t agc_warning:1;
};
uint16_t reg;
};
#define AS5047U_CPR 16384
#define AS5047U_ANGLECOM_REG 0x3FFF
#define AS5047U_ANGLEUNC_REG 0x3FFE
#define AS5047U_MAGNITUDE_REG 0x3FFD
#define AS5047U_VELOCITY_REG 0x3FFC
#define AS5047U_SIN_REG 0x3FFA
#define AS5047U_COS_REG 0x3FFB
#define AS5047U_AGC_REG 0x3FF9
#define AS5047U_DIAGNOSTICS_REG 0x3FF5
#define AS5047U_ERROR_REG 0x0001
#define AS5047U_PROG_REG 0x0003
#define AS5047U_ECCCHK_REG 0x00D1
#define AS5047U_DISABLE_REG 0x0015
#define AS5047U_ZPOSM_REG 0x0016
#define AS5047U_ZPOSL_REG 0x0017
#define AS5047U_SETTINGS1_REG 0x0018
#define AS5047U_SETTINGS2_REG 0x0019
#define AS5047U_SETTINGS3_REG 0x001A
#define AS5047U_ECC_REG 0x001B
#define AS5047U_WARNING 0x8000
#define AS5047U_ERROR 0x4000
#define AS5047U_RW 0x4000
#define AS5047U_ERRFLG 0xC000
#define AS5047U_RESULT_MASK 0x3FFF
#define AS5047U_BITORDER MSBFIRST
static SPISettings AS5047USPISettings(8000000, AS5047U_BITORDER, SPI_MODE1); // @suppress("Invalid arguments")
class AS5047U {
public:
AS5047U(SPISettings settings = AS5047USPISettings, int nCS = -1);
virtual ~AS5047U();
virtual void init(SPIClass* _spi = &SPI);
float getCurrentAngle(); // angle in radians, return current value
float getFastAngle(); // angle in radians, return last value and read new
uint16_t readRawAngle(); // 14bit angle value
uint16_t readCorrectedAngle(); // 14bit corrected angle value
uint16_t readMagnitude(); // 14bit magnitude value
uint16_t readVelocity(); // 14bit magnitude value
bool isErrorFlag();
bool isWarningFlag();
AS5047UError clearErrorFlag();
AS5047UDiagnostics readDiagnostics();
uint8_t readAGC();
uint8_t readECCCHK();
AS5047UDisableSettings readDisableSettings();
void writeDisableSettings(AS5047UDisableSettings settings);
AS5047USettings1 readSettings1();
void writeSettings1(AS5047USettings1 settings);
AS5047USettings2 readSettings2();
void writeSettings2(AS5047USettings2 settings);
AS5047USettings3 readSettings3();
void writeSettings3(AS5047USettings3 settings);
AS5047UECCSettings readECCSettings();
void writeECCSettings(AS5047UECCSettings settings);
void enablePWM(bool enable, bool pwmOnWPin = true);
void enableABI(bool enable);
void enableUVW(bool enable);
uint16_t setZero(uint16_t);
uint16_t getZero();
private:
uint16_t nop16();
uint16_t spi_transfer16(uint16_t outdata);
uint8_t calcCRC(uint16_t data);
uint16_t writeRegister24(uint16_t reg, uint16_t data);
SPIClass* spi;
SPISettings settings;
bool errorflag = false;
bool warningflag = false;
int nCS = -1;
};
#endif /* AS5047U_H_ */

View File

@@ -0,0 +1,29 @@
#include "./MagneticSensorAS5047U.h"
#include "common/foc_utils.h"
#include "common/time_utils.h"
MagneticSensorAS5047U::MagneticSensorAS5047U(int nCS, bool fastMode, SPISettings settings) : AS5047U(settings, nCS), fastMode(fastMode) {
}
MagneticSensorAS5047U::~MagneticSensorAS5047U(){
}
void MagneticSensorAS5047U::init(SPIClass* _spi) {
this->AS5047U::init(_spi);
this->Sensor::init();
}
float MagneticSensorAS5047U::getSensorAngle() {
float angle_data = readRawAngle();
if (!fastMode) // read again to ensure current value
angle_data = readRawAngle();
angle_data = ( angle_data / (float)AS5047U_CPR) * _2PI;
// return the shaft angle
return angle_data;
}

View File

@@ -0,0 +1,20 @@
#ifndef __MAGNETICSENSORAS5047U_H__
#define __MAGNETICSENSORAS5047U_H__
#include "common/base_classes/Sensor.h"
#include "./AS5047U.h"
class MagneticSensorAS5047U : public Sensor, public AS5047U {
public:
MagneticSensorAS5047U(int nCS = -1, bool fastMode = false, SPISettings settings = AS5047USPISettings);
virtual ~MagneticSensorAS5047U();
virtual float getSensorAngle() override;
virtual void init(SPIClass* _spi = &SPI);
private:
bool fastMode = false;
};
#endif

View File

@@ -0,0 +1,81 @@
# AS5047U SimpleFOC driver
While AS5047U absolute position magnetic rotary encoder is supported by the standard MagneticSensorSPI driver included in the base distribution, this AS5047U-specific driver includes some optimisations:
- access to the other registers of the AS5047U, including the magnitude value which can be used to check the magnet strength, the velocity register and the diagnostics register
- access to the error state of the sensor, and ability to clear errors
- it has a fastMode setting, in which the sensor is sent only 1 command per getAngle() call - the value returned will be from previous getAngle() invocation
## 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/as5047u/MagneticSensorAS5047U.h"
#define SENSOR1_CS 5 // some digital pin that you're using as the nCS pin
MagneticSensorAS5047U sensor1(SENSOR1_CS);
void setup() {
sensor1.init();
}
```
Set some options:
```c++
MagneticSensorAS5047U sensor1(SENSOR1_CS, true, 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();
// read the CORDIC magnitude value, a measure of the magnet field strength
float m1 = sensor1.readMagnitude();
// check for errors
if (sensor1.isErrorFlag()) {
AS5047UError error = sensor1.clearErrorFlag();
if (error.parityError) { // also error.framingError, error.commandInvalid
// etc...
}
}
// get diagnostics
AS5047Diagnostics diagnostics = sensor1.readDiagnostics();
```