Wrong velocity from a MT6835 with the Encoder class

I’m using 3 methods to read from a MT6835 magnetic encoder:

  • SPI
  • AB with interrupt (using the `Encoder` class)
  • AB with hardware counter (using the `ESP32HWEncoder` class)

The three methods give the same angle measurements. However, when it comes to velocity computation, the Encoder class gives aberrant values:

---------
SPI      - Ang: -0.364593 Vel: 0.0203945
AB (int) - Ang: 0.365375 Vel: -0.180679
AB (hw)  - Ang: 0.365375 Vel: -0.0204003
---------
SPI      - Ang: -0.344439 Vel: 0.0201351
AB (int) - Ang: 0.345242 Vel: -2.26831
AB (hw)  - Ang: 0.345242 Vel: -0.0201141
---------
SPI      - Ang: -0.132929 Vel: 0.211293
AB (int) - Ang: 0.133744 Vel: -2.26831
AB (hw)  - Ang: 0.133744 Vel: -0.21128
---------
SPI      - Ang: -0.361214 Vel: -0.228054
AB (int) - Ang: 0.362019 Vel: 0
AB (hw)  - Ang: 0.362019 Vel: 0.228045
---------
SPI      - Ang: -0.361214 Vel: 0
AB (int) - Ang: 0.362019 Vel: 0
AB (hw)  - Ang: 0.362019 Vel: 0
---------

I tried to reduce the CPR, or to slow down the loop, but the issue persists.

Here is my code:

/*

Read values from MT6835 encoder using SPI, AB/interrupt and AB_hardware counter

See:
https://github.com/simplefoc/Arduino-FOC-drivers/tree/master/src/encoders/mt6835
https://github.com/simplefoc/Arduino-FOC-drivers/tree/master/src/encoders/esp32hwencoder

*/

#include "SimpleFOC.h"
#include "encoders/esp32hwencoder/ESP32HWEncoder.h"
#include "encoders/mt6835/MagneticSensorMT6835.h"

//===========================================================================
// Encoder SPI communication
//===========================================================================

// SPI pins
#define PIN_SCK 18
#define PIN_MISO 17
#define PIN_MOSI 16
#define PIN_CS 40

SPIClass spi(HSPI);
MagneticSensorMT6835 sensor(PIN_CS);

//===========================================================================
// Encoder AB communication (through interrupt)
//===========================================================================

#define PIN_ENC_Y_A 44
#define PIN_ENC_Y_B 48

constexpr uint16_t AB_RESOLUTION = 16384;  // Pulse-per-revolution, max 16384

Encoder encoder(PIN_ENC_Y_A, PIN_ENC_Y_B, AB_RESOLUTION);

void doA() { encoder.handleA(); }
void doB() { encoder.handleB(); }

//===========================================================================
// Encoder AB communication (using esp32 harware counter)
//===========================================================================

ESP32HWEncoder encoder2(PIN_ENC_Y_A, PIN_ENC_Y_B, AB_RESOLUTION);

//===========================================================================
//===========================================================================
//===========================================================================

void setup() {
  delay(4000);
  printf("\n\n--- MT6835 Test ---\n");

  // Initialize SPI
  if (!spi.begin(PIN_SCK, PIN_MISO, PIN_MOSI)) {
    printf("SPI bus not initialized, exiting...\n");
    while (1);
  }

  // Initialize the SPI sensor
  sensor.init(&spi);

  // Check default settings to see if the sensor is initialized correctly
  if (sensor.getBandwidth() != 5 || sensor.getHysteresis() != 7) {
    printf("MT6835 not initialized correctly, exiting...\n");
    while (1);
  }

  // Reset the SPI sensor to zero in order to match the AB values
  const bool res = sensor.setZeroFromCurrentPosition();
  assert(res);

  // Use SPI to set the AB resolution
  sensor.setABZResolution(AB_RESOLUTION - 1);
  assert(sensor.getABZResolution() == AB_RESOLUTION - 1);

  // AB setup (interrupt)
  encoder.quadrature = Quadrature::ON;
  encoder.pullup = Pullup::USE_INTERN;
  encoder.init();
  encoder.enableInterrupts(doA, doB);

  // AB setup (harware counter)
  encoder2.pullup = Pullup::USE_INTERN;
  encoder2.init();
}

//===========================================================================
//===========================================================================
//===========================================================================

void loop() {
  sensor.update();
  encoder.update();
  encoder2.update();

  static auto ts = millis();
  const auto now = millis();
  if (now - ts > 1000) {
    ts = now;
    printf("---------\n");
    printf("SPI      - Ang: %g Vel: %g\n", sensor.getAngle(),
        sensor.getVelocity());
    printf("AB (int) - Ang: %g Vel: %g\n", encoder.getAngle(),
        encoder.getVelocity());
    printf("AB (hw)  - Ang: %g Vel: %g\n", encoder2.getAngle(),
        encoder2.getVelocity());
  }
}

//===========================================================================

(I know encoder and encoder2somehow walk all over each other, but removing one or the other gives the same results)

Am I missing something?

2 Answers

2

The SPI and the HWEncoder seem to have the opposite direction (maybe fixed by swapping A and B) but pretty much exactly the same values.

interrupt encoder is off in terms of velocity, I would say it’s because it’s a lot less accurate on the time dimension which introduces errors…

I would say it’s because it’s a lot less accurate on the time dimension which introduces errors…

Thanks @runger . I’m not sure, as the speed is sometimes off by 3 orders of magnitude. Anyway, let’s close this. If there’s a bug, others will run into it sooner or later.