Skip to content

Commit 427933e

Browse files
author
Richard Unger
committed
MT6701 SSI support
1 parent b139cf9 commit 427933e

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "./MagneticSensorMT6701SSI.h"
2+
#include "common/foc_utils.h"
3+
#include "common/time_utils.h"
4+
5+
MagneticSensorMT6701SSI::MagneticSensorMT6701SSI(int nCS, SPISettings settings) : settings(settings), nCS(nCS) {
6+
7+
}
8+
9+
10+
MagneticSensorMT6701SSI::~MagneticSensorMT6701SSI() {
11+
12+
}
13+
14+
void MagneticSensorMT6701SSI::init(SPIClass* _spi) {
15+
this->spi=_spi;
16+
this->Sensor::init();
17+
if (nCS >= 0) {
18+
pinMode(nCS, OUTPUT);
19+
digitalWrite(nCS, HIGH);
20+
}
21+
}
22+
23+
// check 40us delay between each read?
24+
float MagneticSensorMT6701SSI::getSensorAngle() {
25+
float angle_data = readRawAngleSSI();
26+
angle_data = ( angle_data / (float)MT6701_CPR ) * _2PI;
27+
// return the shaft angle
28+
return angle_data;
29+
}
30+
31+
32+
uint16_t MagneticSensorMT6701SSI::readRawAngleSSI() {
33+
if (nCS >= 0)
34+
digitalWrite(nCS, LOW);
35+
spi->beginTransaction(settings);
36+
uint16_t value = spi->transfer16(0x0000);
37+
spi->endTransaction();
38+
if (nCS >= 0)
39+
digitalWrite(nCS, HIGH);
40+
return (value>>2)&0x3FFF;
41+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef __MAGNETIC_SENSOR_MT6701_SSI_H__
2+
#define __MAGNETIC_SENSOR_MT6701_SSI_H__
3+
4+
#include "SPI.h"
5+
#include "common/base_classes/Sensor.h"
6+
7+
8+
#define MT6701_CPR 16384.0f
9+
10+
#define MT6701_BITORDER MSBFIRST
11+
12+
13+
// Use SPI mode 2, capture on falling edge. First bit is not valid data, so have to read 25 bits to get a full SSI frame.
14+
// SSI frame is 1 bit ignore, 14 bits angle, 4 bit status and 6 bit CRC.
15+
static SPISettings MT6701SSISettings(1000000, MT6701_BITORDER, SPI_MODE2); // @suppress("Invalid arguments")
16+
17+
18+
19+
class MagneticSensorMT6701SSI : public Sensor {
20+
public:
21+
MagneticSensorMT6701SSI(int nCS = -1, SPISettings settings = MT6701SSISettings);
22+
virtual ~MagneticSensorMT6701SSI();
23+
24+
virtual void init(SPIClass* _spi = &SPI);
25+
26+
float getSensorAngle() override; // angle in radians, return current value
27+
28+
protected:
29+
uint16_t readRawAngleSSI();
30+
31+
SPISettings settings;
32+
SPIClass* spi;
33+
int nCS = -1;
34+
};
35+
36+
37+
#endif

src/encoders/mt6701/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# MT6701 SimpleFOC driver
2+
3+
:warning: work in progress... SSI driver is working. I2C not yet complete.
4+
5+
Due to the peculiarities of its interfaces, this very versatile sensor is not directly supported by SimpleFOC's SPI or I2C magnetic sensor implementations. This folder contains dedicated SimpleFOC sensor drivers for the MT6701 for I2C and SSI.
6+
7+
Note: the ABZ, UVW and Analog outputs of this sensor are supported by the standard SimpleFOC encoder, hall-sensor or analog sensor classes respectively.
8+
9+
:warning: Note: the I2C output of this sensor is probably too slow for high performance motor control, but could be useful to program the sensor IC, and to read the absolute angle values for intializing ABZ or UVW modes.
10+
11+
## Hardware setup
12+
13+
For I2C, connect the sensor as normal for I2C, using the SDA and SCL lines.
14+
15+
:warning: Note: to program the sensor via I2C, it has to be operated at 5V.
16+
17+
For SSI, connect the CSN line to the nCS output of your MCU, the CLK line to the SCLK output of the MCU, and the DO line to the CIPO (MISO) input of the MCU.
18+
19+
In my tests, the sensor was not able to work correctly together with other SPI devices on the same bus, but your experience might differ from mine.
20+
21+
## Software setup
22+
23+
### SSI code sample
24+
25+
```c++
26+
#include "Arduino.h"
27+
#include "SPI.h"
28+
#include "SimpleFOC.h"
29+
#include "SimpleFOCDrivers.h"
30+
#include "encoders/MT6701/MagneticSensorMT6701SSI.h"
31+
32+
33+
#define SENSOR1_CS 5 // some digital pin that you're using as the nCS pin
34+
MagneticSensorMT6701SSI sensor1(SENSOR1_CS);
35+
36+
37+
void setup() {
38+
sensor1.init();
39+
}
40+
```
41+
42+
To use a custom SPI bus:
43+
44+
```c++
45+
void setup() {
46+
sensor1.init(&SPI2);
47+
}
48+
```
49+
50+
51+
52+
### I2C code sample
53+
54+
I2C usage is quite simple:
55+
56+
```c++
57+
#include "Arduino.h"
58+
#include "Wire.h"
59+
#include "SimpleFOC.h"
60+
#include "SimpleFOCDrivers.h"
61+
#include "encoders/MT6701/MagneticSensorMT6701I2C.h"
62+
63+
MagneticSensorMT6701I2C sensor1();
64+
65+
66+
void setup() {
67+
sensor1.init();
68+
}
69+
70+
```
71+
72+
If you've programmed a different I2C address or want to use a different I2C bus you can:
73+
74+
```c++
75+
#define I2C_ADDR 0x70
76+
MagneticSensorMT6701I2C sensor1(I2C_ADDR);
77+
78+
79+
void setup() {
80+
sensor1.init(&Wire2);
81+
}
82+
```

0 commit comments

Comments
 (0)