Try uncomment:
// encoder._pinA = PB_6;
// encoder._pinB = PB_7_ALT1;
And check your timer pin map
Try uncomment:
// encoder._pinA = PB_6;
// encoder._pinB = PB_7_ALT1;
And check your timer pin map
You need to uncomment these 2 lines, otherwise the initialization fails
// encoder._pinA = PB_6;
// encoder._pinB = PB_7_ALT1;
And edit the right configuration files… You can see the working code in this thread https://community.simplefoc.com/t/encoder-skips-pulses/2884/38
Hi @robca
I appreciate your quick response and help.
I tried your suggestion from the other page, but it’s still the same.
The setup sequence doesn’t even trigger. Which makes me think it’s not even passing through the encoder.init() function.
I feel like I’m missing a small setting somewhere.
This is how I have currently
#include <SimpleFOC.h>
#include "SimpleFOCDrivers.h"
#include "encoders/stm32hwencoder/STM32HWEncoder.h"
BLDCMotor motor = BLDCMotor(21);
BLDCDriver6PWM driver = BLDCDriver6PWM(A_PHASE_UH, A_PHASE_UL, A_PHASE_VH, A_PHASE_VL, A_PHASE_WH, A_PHASE_WL);
LowsideCurrentSense currentSense = LowsideCurrentSense(0.003f, -64.0f/7.0f, A_OP1_OUT, A_OP2_OUT, A_OP3_OUT);
// Encoder encoder = Encoder(A_HALL1, A_HALL2, 1000);
STM32HWEncoder encoder = STM32HWEncoder(1000, PB6, PB7);
float targetSpeed = 0;
Commander command = Commander(Serial);
void doMotor(char* cmd) { command.motor(&motor, cmd); }
void setup() {
// init magnetic sensor hardware
encoder._pinA = PB_6;
encoder._pinB = PB_7_ALT1;
encoder.init();
motor.linkSensor(&encoder);
Please read thru the whole thread and pay attention to “edit the right files”
https://community.simplefoc.com/t/encoder-skips-pulses/2884/19?u=robca
you need to uncomment the PB6 and PB_7_ALT1 pins in variants/STM32G4xx/G431C(6-8-B)U_G441CBU/PeripheralPins_B_G431B_ESC1.c
The board variant is only partially defined, and most pins are commented out. For the timer used in the HW encoder, you need PB6 and PB7_ALT1 to be available. Not PB7, that one uses the wrong timer
Hi @robca
Thank you for pointing it out. I had missed that part.
I tried changing it, didn’t make a difference for some reason.
This is what I did in the PeripheralPins_B_G431B_ESC1.c file
But, I’m doing it with a tired mind, lol. Will take a break and come back and look at the post.
Again, truly appreciate your help with this.
Sorry you are having problems…
Let me cut and paste snippets from my working code. Please note that I’m using the latest versions of STM32duino and latest SimpleFOC from the Dev branch, because @runger fixed the HW encoder code after problems were reported (Arduino-FOC-drivers/src/encoders/stm32hwencoder at dev · simplefoc/Arduino-FOC-drivers · GitHub). The code in master doesn’t work.
#include <SimpleFOC.h>
#include "encoders/stm32hwencoder/STM32HWEncoder.h"
STM32HWEncoder encoder = STM32HWEncoder(1024, PB6, PB_7_ALT1);
void setup() {
// init magnetic sensor hardware
encoder._pinA = PB_6;
encoder._pinB = PB_7_ALT1;
encoder.init();
Assuming you edited the right file (i.e. the one used by the compiler), your PB6 and PB7_ALT1 look right now.
If none of this works (check you are using the dev version of the encoder), your next best option is to put a breakpoint in the init function of the HW encoder and verify that InstanceA is the right timer (0x40000800)
Just to let you know I have released the Drivers Library yesterday, and version 1.0.4 can now be “officially” used, together with either the SimpleFOC dev branch, or the SimpleFOC release 2.3.0.
So you no longer need the dev branches to use the new STM32HWEncoder. However all the other comments regarding the PinMap still apply - that’s unfortunately part of the Arduino board files and out of scope for the SimpleFOC library.
I am testing with my new motor, which is similar to my old one, and I was able to tune the current q and d PID’s such that the clicking does not happen. I also noticed a difference when I disabled the motor.monitor() line. I’ve practically been able to remove the cogging through tuning the I parameter of the velocity PID. The only problem I’m having now is the motor will sometimes oscillate when its target angle is 0, 2pi, etc. The I term of the velocity PID is 35.0, and I did find that lowering it to around 10 gets rid of the oscillation, but it also undoes the correction for cogging. Is there any reason why this would happen only at 0?
Hi @mizzi_labs another issue you may be facing is platformIO is using wrong library version, even though you have changed it in the platformio.ini it doesn’t update and keeps compiling with old version. This happens to me quite often and I have todo a clean all first and then recompile, this will reinstall the library dependencies for the project.
Thanks for this folks. My setup works much better and can get higher speeds with the hw encoder code!
Hi @robca ,
It started working for me this morning.
I was making a silly mistake of not including the header files (was too smart to not respect them ):
#include "Arduino.h"
#include "Wire.h"
#include "SPI.h"
Truly appreciate your and everyone’s help in getting this to run.