try to fix submodule
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
|
||||
#include "./MagneticSensorTLE5012B.h"
|
||||
|
||||
#if defined(_STM32_DEF_)
|
||||
|
||||
MagneticSensorTLE5012B::MagneticSensorTLE5012B(int data, int sck, int nCS) : TLE5012B(data, sck, nCS) { };
|
||||
MagneticSensorTLE5012B::~MagneticSensorTLE5012B(){ };
|
||||
|
||||
void MagneticSensorTLE5012B::init() {
|
||||
this->TLE5012B::init();
|
||||
this->Sensor::init();
|
||||
};
|
||||
|
||||
float MagneticSensorTLE5012B::getSensorAngle() {
|
||||
return getCurrentAngle();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
#ifndef __MAGNETIC_SENSOR_TLE5012B_H__
|
||||
#define __MAGNETIC_SENSOR_TLE5012B_H__
|
||||
|
||||
#include "./STM32TLE5012B.h"
|
||||
|
||||
#if defined(_STM32_DEF_)
|
||||
|
||||
#include "common/base_classes/Sensor.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class MagneticSensorTLE5012B : public Sensor, public TLE5012B {
|
||||
public:
|
||||
MagneticSensorTLE5012B(int data, int sck, int nCS);
|
||||
~MagneticSensorTLE5012B();
|
||||
virtual void init() override;
|
||||
virtual float getSensorAngle() override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
# SimpleFOC Driver for TLE5012B encoder for STM32 MCUs
|
||||
|
||||
This driver code is compatible with STM32 MCUs. Based loosely on code from [here](https://github.com/xerootg/btt-s42b-simplefoc), with many thanks to @xerootg
|
||||
|
||||
## Hardware setup
|
||||
|
||||
Connect the data pin of the sensor to the MOSI (COPI) line of the MCU. The SCLK and nCS lines are connected as normal.
|
||||
|
||||
## Software setup
|
||||
|
||||
Sample code:
|
||||
|
||||
```
|
||||
#include <Arduino.h>
|
||||
#include <SimpleFOC.h>
|
||||
#include <SimpleFOCDrivers.h>
|
||||
#include "encoders/tle5012b/MagneticSensorTLE5012B.h"
|
||||
|
||||
MagneticSensorTLE5012B sensor(PB15,PB13,PB12);
|
||||
|
||||
long ts;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) ;
|
||||
delay(2000);
|
||||
|
||||
Serial.println("Initializing sensor...");
|
||||
|
||||
sensor.init();
|
||||
|
||||
Serial.println("Sensor initialized.");
|
||||
|
||||
ts = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sensor.update();
|
||||
if (millis() - ts > 1000) {
|
||||
Serial.println(sensor.getAngle(), 3);
|
||||
ts = millis();
|
||||
}
|
||||
delay(1);
|
||||
}
|
||||
```
|
||||
|
||||
## Other MCUs
|
||||
|
||||
Infineon provides a driver library compatible with Atmel AVR (Arduino UNO, Nano, etc...) and Infineon XMC MCUs. If you have one of those MCUs, you can use this library in conjunction with the GenericSensor class in SimpleFOC.
|
||||
|
||||
```
|
||||
#include <SimpleFOC.h>
|
||||
#include <TLE5012-ino.hpp>
|
||||
|
||||
Tle5012Ino Tle5012Sensor = Tle5012Ino();
|
||||
errorTypes checkError = NO_ERROR;
|
||||
|
||||
float readMySensor(){
|
||||
// read my sensor
|
||||
// return the angle value in radians in between 0 and 2PI
|
||||
|
||||
Tle5012Sensor.getAngleValue(d);
|
||||
d = (d + 180);
|
||||
if ( d + 40.9 > 360 ) {
|
||||
d = (d + 40.9) - 360;
|
||||
}
|
||||
else
|
||||
d = d + 40.9;
|
||||
|
||||
d = d * 0.0174533;
|
||||
return d;
|
||||
}
|
||||
void initMySensor(){
|
||||
// do the init
|
||||
checkError = Tle5012Sensor.begin();
|
||||
}
|
||||
|
||||
// empty constructor
|
||||
GenericSensor sensor = GenericSensor(readMySensor, initMySensor);
|
||||
|
||||
void setup(){
|
||||
...
|
||||
//init sensor and use it further with foc...
|
||||
sensor.init();
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Other MCUs are currently not supported. The problem is that the sensor uses SPI in half-duplex mode, which is not supported by Arduino framework. So for each MCU type a custom driver has to be written to get the half-duplex SPI communication to work. There are currently no plans to support other MCUs on our side, but we will gladly accept pull requests :-)
|
||||
|
||||
If you can find other libraries for other MCUs, you can use the GenericSensor approach to integrate them.
|
||||
@@ -0,0 +1,159 @@
|
||||
|
||||
#include "STM32TLE5012B.h"
|
||||
|
||||
#if defined(_STM32_DEF_)
|
||||
|
||||
|
||||
#include "utility/spi_com.h"
|
||||
extern "C" uint32_t spi_getClkFreqInst(SPI_TypeDef *spi_inst);
|
||||
|
||||
|
||||
|
||||
TLE5012B::TLE5012B(int data, int sck, int nCS, uint32_t freq) {
|
||||
_data = data;
|
||||
_sck = sck;
|
||||
_nCS = nCS;
|
||||
_freq = freq;
|
||||
};
|
||||
|
||||
TLE5012B::~TLE5012B() {
|
||||
|
||||
};
|
||||
|
||||
|
||||
void TLE5012B::init() {
|
||||
pinMode(_nCS, OUTPUT);
|
||||
digitalWrite(_nCS, HIGH);
|
||||
|
||||
// initialize pins
|
||||
GPIO_InitTypeDef gpio;
|
||||
gpio.Pin = digitalPinToBitMask(_data);
|
||||
gpio.Mode = GPIO_MODE_AF_PP;
|
||||
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(digitalPinToPort(_data), &gpio);
|
||||
gpio.Pin = digitalPinToBitMask(_sck);
|
||||
gpio.Mode = GPIO_MODE_AF_PP;
|
||||
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(digitalPinToPort(_sck), &gpio);
|
||||
|
||||
SPI_TypeDef *spi_data = (SPI_TypeDef*)pinmap_peripheral(digitalPinToPinName(_data), PinMap_SPI_MOSI);
|
||||
SPI_TypeDef *spi_sclk = (SPI_TypeDef*)pinmap_peripheral(digitalPinToPinName(_sck), PinMap_SPI_SCLK);
|
||||
SPI_TypeDef *spi_inst = (SPI_TypeDef*)pinmap_merge_peripheral(spi_data, spi_sclk);
|
||||
|
||||
pinmap_pinout(digitalPinToPinName(_data), PinMap_SPI_MOSI);
|
||||
pinmap_pinout(digitalPinToPinName(_sck), PinMap_SPI_SCLK);
|
||||
|
||||
#if defined SPI1_BASE
|
||||
if (spi_inst == SPI1) {
|
||||
__HAL_RCC_SPI1_CLK_ENABLE();
|
||||
__HAL_RCC_SPI1_FORCE_RESET();
|
||||
__HAL_RCC_SPI1_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined SPI2_BASE
|
||||
if (spi_inst == SPI2) {
|
||||
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||
__HAL_RCC_SPI2_FORCE_RESET();
|
||||
__HAL_RCC_SPI2_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined SPI3_BASE
|
||||
if (spi_inst == SPI3) {
|
||||
__HAL_RCC_SPI3_CLK_ENABLE();
|
||||
__HAL_RCC_SPI3_FORCE_RESET();
|
||||
__HAL_RCC_SPI3_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined SPI4_BASE
|
||||
if (spi_inst == SPI4) {
|
||||
__HAL_RCC_SPI4_CLK_ENABLE();
|
||||
__HAL_RCC_SPI4_FORCE_RESET();
|
||||
__HAL_RCC_SPI4_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined SPI5_BASE
|
||||
if (spi_inst == SPI5) {
|
||||
__HAL_RCC_SPI5_CLK_ENABLE();
|
||||
__HAL_RCC_SPI5_FORCE_RESET();
|
||||
__HAL_RCC_SPI5_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined SPI6_BASE
|
||||
if (spi_inst == SPI6) {
|
||||
__HAL_RCC_SPI6_CLK_ENABLE();
|
||||
__HAL_RCC_SPI6_FORCE_RESET();
|
||||
__HAL_RCC_SPI6_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
_spi.Instance = spi_inst;
|
||||
_spi.Init.Direction = SPI_DIRECTION_1LINE;
|
||||
_spi.Init.Mode = SPI_MODE_MASTER;
|
||||
_spi.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
_spi.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
_spi.Init.CLKPhase = SPI_PHASE_2EDGE;
|
||||
_spi.Init.NSS = SPI_NSS_SOFT;
|
||||
_spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
_spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
_spi.Init.CRCPolynomial = 7;
|
||||
_spi.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
#if defined(SPI_NSS_PULSE_DISABLE)
|
||||
_spi.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
|
||||
#endif
|
||||
|
||||
uint32_t spi_freq = spi_getClkFreqInst(spi_inst);
|
||||
if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV2_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV4_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV8_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV16_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV32_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV64_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
|
||||
} else if (_freq >= (spi_freq / SPI_SPEED_CLOCK_DIV128_MHZ)) {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
|
||||
} else {
|
||||
_spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
|
||||
}
|
||||
|
||||
if (HAL_SPI_Init(&_spi) != HAL_OK) {
|
||||
// setup error
|
||||
Serial.println("TLE5012B setup error");
|
||||
}
|
||||
};
|
||||
|
||||
uint16_t TLE5012B::readRawAngle() {
|
||||
uint8_t data[4];
|
||||
readBytes(TLE5012B_ANGLE_REG, data, 2);
|
||||
return (((uint16_t)data[0] << 8) | data[1]) & 0x7FFF;
|
||||
};
|
||||
|
||||
|
||||
float TLE5012B::getCurrentAngle() {
|
||||
return ((float)readRawAngle())/TLE5012B_CPR * _2PI;
|
||||
}; // angle in radians, return current value
|
||||
|
||||
|
||||
void TLE5012B::readBytes(uint16_t reg, uint8_t *data, uint8_t len) {
|
||||
digitalWrite(_nCS, LOW);
|
||||
|
||||
reg |= TLE5012B_READ_REGISTER + (len>>1);
|
||||
uint8_t txbuffer[2] = { (uint8_t)(reg >> 8), (uint8_t)(reg & 0x00FF) };
|
||||
HAL_SPI_Transmit(&_spi, txbuffer, 2, 100); // TODO check return value for error, timeout
|
||||
//delayMicroseconds(1);
|
||||
HAL_SPI_Receive(&_spi, data, len + 2, 100);
|
||||
|
||||
digitalWrite(_nCS, HIGH);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef __TLE5012B_H__
|
||||
#define __TLE5012B_H__
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#if defined(_STM32_DEF_)
|
||||
|
||||
|
||||
#define _2PI 6.28318530718f
|
||||
#define TLE5012B_CPR 32768.0f
|
||||
|
||||
#define TLE5012B_READ_REGISTER 0x8000
|
||||
|
||||
enum TLE5012B_Register : uint16_t {
|
||||
TLE5012B_ANGLE_REG = 0x0020,
|
||||
TLE5012B_SPEED_REG = 0x0030
|
||||
};
|
||||
|
||||
|
||||
|
||||
class TLE5012B {
|
||||
public:
|
||||
TLE5012B(int data, int sck, int nCS, uint32_t freq = 1000000);
|
||||
~TLE5012B();
|
||||
virtual void init();
|
||||
|
||||
uint16_t readRawAngle();
|
||||
float getCurrentAngle(); // angle in radians, return current value
|
||||
private:
|
||||
|
||||
void readBytes(uint16_t reg, uint8_t *data, uint8_t len);
|
||||
|
||||
int _data;
|
||||
int _sck;
|
||||
int _nCS;
|
||||
uint32_t _freq;
|
||||
SPI_HandleTypeDef _spi;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user