Skip to content

Commit 0476f73

Browse files
author
Richard Unger
committed
add code example to MT6835 readme
1 parent 51e5254 commit 0476f73

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/encoders/mt6835/README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# MT6835 SimpleFOC driver
22

3-
:warning: untested
4-
53
Driver for the MagnTek MT6835 precision magnetic rotary encoder.
64

75
This sensor features support for up to 21 bit resolution (!) and speeds up to 120,000RPM. While its full precision requires calibration using an external calibration system, it is impressively precise even in uncalibrated state, and it offers an internal self-calibration mode whose precision is somewhere between the other two.
@@ -17,12 +15,50 @@ Connect the sensor to an SPI bus on your MCU. Pay attention to the voltage level
1715
Usage example:
1816

1917
```c++
20-
#include "Arduino.h"
21-
#include "Wire.h"
18+
#include <Arduino.h>
19+
2220
#include "SimpleFOC.h"
2321
#include "SimpleFOCDrivers.h"
24-
#include "encoders/MT6835/MagneticSensorMT6835.h"
2522

26-
// TODO code example
23+
#include "encoders/mt6835/MagneticSensorMT6835.h"
24+
25+
#define SENSOR_nCS PB6
26+
27+
SPISettings myMT6835SPISettings(1000000, MT6835_BITORDER, SPI_MODE3);
28+
MagneticSensorMT6835 sensor = MagneticSensorMT6835(SENSOR_nCS, myMT6835SPISettings);
29+
30+
long ts;
31+
32+
void setup() {
33+
sensor.init();
34+
ts = millis();
35+
}
36+
37+
void loop() {
38+
sensor.update();
39+
long now = millis();
40+
if (now - ts > 1000) {
41+
ts = now;
42+
SimpleFOCDebug::print("A: ");
43+
SimpleFOCDebug::print(sensor.getAngle());
44+
SimpleFOCDebug::print(" V: ");
45+
SimpleFOCDebug::println(sensor.getVelocity());
46+
}
47+
delay(10);
48+
}
49+
50+
```
51+
52+
Set the zero position:
53+
54+
```c++
55+
uint16_t pos = sensor.getZeroPosition(); // current value
56+
sensor.setZeroFromCurrentPosition(); // set zero to current position
57+
```
58+
59+
Set the ABZ resolution (needed if you want to use ABZ as the default is 1):
60+
61+
```c++
62+
sensor.setABZResolution(2048);
2763
```
2864

0 commit comments

Comments
 (0)