Displacement of the real situation

Hi all. I encountered strange behavior, I hope that someone can help))). I use a homemade driver to control the gyroscooter motor. Mosfets stp75nf75 and driver eg2133. Initially, I used resistors to open 20 ohm mosfets. This caused the mosfetes to heat up, now I replaced it with a nominal value of 10 ohms. The mosfets became colder, but under load the encoder began to skip steps)))))))) in the a / b / z mode))))). I brought back the 20th and the gaps disappeared. How can this be related? And I also tried to reduce the driver frequency to 15 kHz, then the skips were not so strong with 10 ohm resistors. microcontroller STM32F401ccu6

Are you using the STM32HWencoder for your ABZ encoder, or the interrupt-driven one?

In my experience, the interrupt driven code on the STM32 can only handle low speeds, especially if the encoder is high resolution (mine is 1000 pulses per rotation)

I recommend using the STM32HWencoder, Arduino-FOC-drivers/src/encoders/stm32hwencoder at master · simplefoc/Arduino-FOC-drivers · GitHub (install the simplefoc drivers library, updatedlast week)

I’m using this library. and the displacement of the position occurs when the rotor is held.

But if I return the 20 ohm resistors to open the MOSFETs, then the biases no longer occur

Then, sorry, can’t help more…

This sounds tricky…

One explanation could be that the high gate resistance slows things down so the signals are “gentle” enough and the sensor works well. But the MOSFET is switching too slowly, causing extra heat. One explanation can be that the dead-time is not sufficient for the slow switching speed. This could be solved maybe by increasing the dead-time (if you are using 6-PWM control, or a driver that lets you set the dead-time).

The low gate resistance causes less heat because the dead-time now works, but it is switching “more dynamically” causing some kind of voltage spike interfering with the sensor.
Perhaps this could be fixed by changing the PCB layout to better protect the sensor’s signals.

Another thing is to experiment with different gate resistances - have you tried 15Ω? You can also think about changing the design to use a diode to have different gate resistances for sink and source (discharge and charge).

I think to help you more we’d have to see the schematic and layout for the board, perhaps then people would have some more comments…

Hi. I really don’t have enough time for hobbies))). My scheme is very simple, as indicated in the datasheet. Today I used esp32 and no missing steps were noticed…

Don’t understand. Same code and driver. Everything works well on esp32, but there are problems with stm32. In fact, esp32 is also suitable for me. I just saw that the pwm/dir driver appeared. It suits me better than esp32 and analog reading. But unfortunately it didn’t work for me))))))

Maybe I’m using the wrong stm32 pins.

#include <SimpleFOC.h>
#include "SimpleFOCDrivers.h"
#include "encoders/stm32hwencoder/STM32HWEncoder.h"
#include "encoders/calibrated/CalibratedSensor.h"
#include "comms/stm32speeddir/STM32SpeedDirInput.h"

  STM32HWEncoder sensor = STM32HWEncoder(3000, PB6, PB7);
  STM32SpeedDirInput speed_dir = STM32SpeedDirInput(PB4, PB5);

  BLDCMotor motor = BLDCMotor(15, 1, 40, 0.01);
  BLDCDriver3PWM driver = BLDCDriver3PWM(PA1,PA2,PA5);

  CalibratedSensor sensor_calibrated = CalibratedSensor(sensor);
  float target = 0.0f;
void setup() {

  driver.pwm_frequency = 15000;
  driver.voltage_power_supply = 24;
  motor.voltage_sensor_align = 3;
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  motor.controller = MotionControlType::torque;

  // use monitoring with serial 
  // comment out if not needed

  sensor_calibrated.voltage_calibration = 2;

  speed_dir.min_speed = 0.0f;
  speed_dir.max_speed = 8.0f;
  speed_dir.min_pwm = 3.0f; 
  speed_dir.max_pwm = 97.0f;


void loop() {
  target = speed_dir.getTargetVelocity();

This is the code I used for the stm32f401. I also remembered that if I remove the lines associated with the serial port, the code does not work. It compiles, loads. But nothing happens when the power is applied. The engine remains switched off. And I can’t get any messages either. But if I add lines to enable the serial port, then everything works. Maybe it has something to do with it?

Hey @nikolaewich1988 ,

Sorry I’ve only had time to get back to you now.

The SpeedDirInput should be working, I am using it in another project.
Ive tested on STM32G0 but it should work the same on STM32F4, really.

The pins you’re using PB4, PB5 should be ok - they are on TIM3_CH1 and TIM3_CH2… But which pins are the motor PWM pins 15, 1 and 40? It’s not also on TIM3, right? Can you change it to use the STM32 pin ids?

For the PWM input the input has to be >0% and <100% duty cycle, otherwise the result can’t be measured correctly.

In terms of the Serial connection, this is strange. I think it is probably unrelated to the question of the SpeedDirInput?

Hello. The pwm pins are BLDCDriver3PWM driver = BLDCDriver3PWM(PA1,PA2,PA5);.

I couldn’t solve the problem and went back to esp32. Using the same engine driver. It works without any problems. I had to use adc/dir for management. I don’t know how this will affect the addition of a current sensor. But so far without it.

I also tried using stm32f401 angle mode. A common example from the library. And when I manually twisted the rotor beyond the retention boundary by the algorithm. Then there were also skipping steps. I’m not sure if these were exactly gaps, maybe they were false positives and he mistook them for encoder steps.

Gruß Iurii

Ok, those pins are all on TIM2, so that should be fine! Not sure what is up.

Let me know if you plan to try STM32 again and we can try to debug it…

But if at the moment ESP32 is working well for you, that’s good!

Hello. Okay, thank you for your help. There was also a problem in the serial port . If I remove the inclusion of the serial port from the code, then the engine does not start when power is applied.

// use monitoring with serial
// comment out if not needed

The code must have these two lines, otherwise nothing works. I also updated the board manager for stm32 yesterday. Maybe this will help. I’ll try again on stm32 and let you know.

But I liked how pwm/dir works. It’s a pity that esp32 doesn’t support pwm input. I tried different methods from the Internet, but everything works very unstable)))))))

Gruß Iurii