Getting closed loop working on stepper


This is my first attempt at using SimpleFOC. I’ve built a custom board for controlling a single stepper, similar in form to the mechaduino (entire thing mounts to the back of a NEMA17).

The proc is an ESP32S2, I’m using the bare chip, not a module. Tested and working, no solder bridges or open connections.
The encoder is a TLE5012B, over SPI. Used the GenericSensor to interface with a custom angle read function. SPI is on 10MHz, the angle read output of SimpleFOC is indeed going from 0 to ~6.2 rad for ~1 manual rotation of the motor shaft.

The driver is 2 x TB67H451, quite similar to the popular A4950, with a 0E15 shunt to give approx 2A/phase max current… The VREF pins are driven by the PWM(30kHz) with a 4K7/0.1uF combo to give a fairly reasonable analog reference. (The pins are actually on the DAC out, one idea is to later use the DAC directly- but unsure about the 8 bit resolution (Although the PWM probably isn’t that precise either. But I’d rather not deal with replacing the PWM with the DAC part when everything else is also unknown)

The Problem- the motor turns on VelocityOpenloop. It appears to randomly switch directions, after a couple of rotations. This would be at a constant velocity setpoint. The speed in both directions is not the same either. It appears to be a bit more stable at higher speeds.

I do not know how to proceed from here. I have SimpleFOC Studio running as well, and the Torque/Velocity control simply results in the motor shaft vibrating in one place.

Could someone provide a roadmap in what I should check and in what order (and how) I should tune it? I require both Velocity as well as Angle control.

Thank you for reading through this long-ish post. All help is much appreciated!

Dear @rm2488 , welcome to SimpleFOC!

Personally, I don’t use steppers much, and so I don’t have a test-setup for this ready… but I can see what I can do in terms of helping.

First thing I would ask is if you have the possibility to try the code from our dev branch. My impression is that there have been some similar issues, and these may have been fixed since the last release version.
It would also enable you to quickly test new fixes we might come up with.

I also note the following issue reported recently regarding steppers: Stepper voltage bemf with negative velocity · Issue #219 · simplefoc/Arduino-FOC · GitHub

This sounds a bit similar to your problem?

Thank you runger.

I will have a look at the dev branch, but I am under the impression that the release branch is stable, at least stable enough for what I am going for. I have a suspicion that the choice of processor may have something to do with the glitches I am facing- possibly a timing issue.

Issue 219 is similar, but perhaps not applicable yet. I have not defined a KV value, since I have never managed to get the motor turning in Torque mode. Also, perhaps I don’t need to set either KV or motor resistance since the drivers are working in constant-current mode. I have set a resistance of 1.5E, while the actual resistance is 1.8. 1.5 gives me the correct correlation between the voltage output and current, and I’d rather work with current setpoints.

I am at a loss where to start though. Should I start with d and q axis? Or is there something else I should implement or verify?

EDIT: I missed mentioning that the motor is failing the PP check. It is a 1.8 degree stepper, so I have set pole pairs = 50. Changing the align voltage produces different PP estimations, from ~80 to 120. Could this be a clue?