Quadratic encoder not working

Hi, i tried to use the example from simple_foc to read my encoder output, but it doesn’t work. already had a look with the oscilloscope and the encoder signals look fine. i get the behaviour from the code that i would get if one of the two encoder pins wasn’t triggering. am i using unusable pins or should i look somewhere else? using a stm32F446RE nucleo, with channel A= PA13 and B=PA14, here is the code:

/**

  • Encoder example code
  • This is a code intended to test the encoder connections and to demonstrate the encoder setup.

*/

#include <SimpleFOC.h>

Encoder encoder = Encoder(PA13, PA14, 48);
// interrupt routine intialisation
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
int i=0;
void setup() {
// monitoring port
Serial.begin(115200);

// enable/disable quadrature mode
encoder.quadrature = Quadrature::ON;

// check if you need internal pullups
encoder.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
encoder.init();
// hardware interrupt enable
encoder.enableInterrupts(doA, doB);

Serial.println(“Encoder ready”);

_delay(1000);
}

void loop() {
// iterative function updating the sensor internal variables
// it is usually called in motor.loopFOC()
// not doing much for the encoder though
encoder.update();
// display the angle and the angular velocity to the terminal
if (i%100){
Serial.print(encoder.getAngle());
Serial.print(“\t”);
Serial.println(encoder.getVelocity());
i=0;}
i+=1;
}

HI @Florian_Dsnx , welcome to SimpleFOC!

Not sure what’s going in your case. In principle the code looks ok.

This main loop will run very fast, I would add a slight delay to it, perhaps 50us or 100us.

Have you checked whether these pins PA13, PA14 are used in some other way on the Nucleo board?

What pull-up values are you using, I note you’re using external pull-ups?

You could also try this encoder driver for the nucleo board: https://github.com/simplefoc/Arduino-FOC-drivers/tree/master/src/encoders/stm32hwencoder
It doesn’t use interrupts and therefore has much better performance. It will require you to use pins that connect to channels 1 & 2 of the same general purpose timer, however.

hi @runger thank you for your response, i had a look and it looks like PA13 and 14 are shared with swd signals on nucleo boards, maybe some other part of the simplefoc library is messing up the configuration. I’ll try with some delay and pins A0 and A1.

for the pullups i use 5k ohm, like recommended in the docs.

yes i found the hardware interrupt code yesterday, i will definitely try this out

1 Like