Skip to content

Commit bd67bb9

Browse files
committed
added AS5145 SSI sensor
1 parent 0598eed commit bd67bb9

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ What's here? See the sections below. Each driver or function should come with it
1919
- [AS5048A SPI driver](src/encoders/as5048a/) - SPI driver for the AMS AS5048A absolute position magnetic rotary encoder IC.
2020
- [AS5047 SPI driver](src/encoders/as5047/) - SPI driver for the AMS AS5047P and AS5047D absolute position magnetic rotary encoder ICs.
2121
- [MA730 SPI driver](src/encoders/ma730/) - SPI driver for the MPS MagAlpha MA730 absolute position magnetic rotary encoder IC.
22+
- [MA730 SSI driver](src/encoders/ma730/) - SSI driver for the MPS MagAlpha MA730 absolute position magnetic rotary encoder IC.
23+
- [AS5145 SSI driver](src/encoders/as5145/) - SSI driver for the AMS AS5145 and AS5045 absolute position magnetic rotary encoder ICs.
2224
- [STM32 Hardware Encoder](src/encoders/stm32hwencoder/) - Hardware timer based encoder driver for ABI type quadrature encoders.
2325

2426
### Communications
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "./MagneticSensorAS5145.h"
2+
#include "common/foc_utils.h"
3+
#include "common/time_utils.h"
4+
5+
MagneticSensorAS5145::MagneticSensorAS5145(SPISettings settings) : settings(settings) {
6+
7+
}
8+
9+
10+
MagneticSensorAS5145::~MagneticSensorAS5145() {
11+
12+
}
13+
14+
void MagneticSensorAS5145::init(SPIClass* _spi) {
15+
this->spi=_spi;
16+
this->Sensor::init();
17+
}
18+
19+
// check 40us delay between each read?
20+
float MagneticSensorAS5145::getSensorAngle() {
21+
float angle_data = readRawAngleSSI();
22+
angle_data = ( (float)angle_data / AS5145_CPR ) * _2PI;
23+
// return the shaft angle
24+
return angle_data;
25+
}
26+
27+
28+
uint16_t MagneticSensorAS5145::readRawAngleSSI() {
29+
spi->beginTransaction(settings);
30+
uint16_t value = spi->transfer16(0x0000);
31+
//uint16_t parity = spi->transfer(0x00);
32+
spi->endTransaction();
33+
return (value>>3)&0x1FFF; // TODO this isn't what I expected from the datasheet... maybe there's a leading 0 bit?
34+
}; // 12bit angle value
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#ifndef __MAGNETIC_SENSOR_AS5145_H__
3+
#define __MAGNETIC_SENSOR_AS5145_H__
4+
5+
#include "SPI.h"
6+
#include "common/base_classes/Sensor.h"
7+
8+
9+
#define AS5145_BITORDER MSBFIRST
10+
#define AS5145_CPR 4096.0f
11+
#define _2PI 6.28318530718f
12+
13+
14+
static SPISettings AS5145SSISettings(1000000, AS5145_BITORDER, SPI_MODE2); // @suppress("Invalid arguments")
15+
16+
17+
class MagneticSensorAS5145 : public Sensor {
18+
public:
19+
MagneticSensorAS5145(SPISettings settings = AS5145SSISettings);
20+
virtual ~MagneticSensorAS5145();
21+
22+
virtual float getSensorAngle() override;
23+
24+
virtual void init(SPIClass* _spi = &SPI);
25+
26+
uint16_t readRawAngleSSI();
27+
28+
private:
29+
SPIClass* spi;
30+
SPISettings settings;
31+
};
32+
33+
34+
35+
#endif

src/encoders/as5145/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# AS5145 SimpleFOC driver
2+
3+
SSI protocol driver for the AMS AS5145 magnetic encoder. Any of the A, B or H variants should work. AS5045 encoders should also be supported.
4+
5+
Only angle reading is supported, might get to the status bits at a later time.
6+
The SSI protocol is "emulated" using the SPI peripheral.
7+
8+
Tested with AS5145A on STM32G491 so far.
9+
10+
## Hardware setup
11+
12+
Wire the sensor's data (DO) line to the MISO (CIPO) pin, nCS, SCK as normal. Leave the MOSI pin unconnected.
13+
14+
## Software setup
15+
16+
```
17+
#include <Arduino.h>
18+
#include <SimpleFOC.h>
19+
#include <SimpleFOCDrivers.h>
20+
#include "encoders/as5145/MagneticSensorAS5145.h"
21+
22+
MagneticSensorAS5145 sensor;
23+
SPIClass spi_ssi(PB15, PB14, PB13, PB12);
24+
25+
long ts;
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
while (!Serial) ;
30+
delay(2000);
31+
32+
Serial.println("Initializing sensor...");
33+
34+
spi_ssi.begin();
35+
sensor.init(&spi_ssi);
36+
37+
Serial.println("Sensor initialized.");
38+
39+
ts = millis();
40+
}
41+
42+
void loop() {
43+
sensor.update();
44+
if (millis() - ts > 1000) {
45+
Serial.println(sensor.getAngle(), 3);
46+
ts = millis();
47+
}
48+
delay(1);
49+
}
50+
```

0 commit comments

Comments
 (0)