ESP32 simpleFOC and WiFi crashing

EDIT TLDR; Caused by analogRead being incompatible with ADC configuration for low side current sense.

I am attempting to put MQTT support together with simpleFOC. I can get either one to run but not both together. Has anyone done this successfully?

simpleFOC is running on the default core 0 arduino loop.
WiFi is running on a core 1 task.

I seem to have a problem around _driverSyncLowSide, _configure6PWM and wifiLowLevelInit. Specifically around getting the ADC running. I can get simpleFOC to run openloop without the ADC.

Have tried forcing the simpleFOC driver onto mcpwm slot 1 instead of default slot 0. Commenting out pin or timer config results in a different type of crash.

Some theories:

  • WiFi is using both pwm and I am out of luck.
  • WiFi and simpleFOC is using the single? high definition timer.
  • One of my PWM pins is clashing with something critical to WiFI.

Debugging the ESP32 stuff is a bit difficult since it is closed source at the point where it does the hardware config.


Hi @crashmatt ,

I haven’t tried MQTT, but WiFi + SimpleFOC is possible, at least I’ve done this in the past.

For PWM, you can re-assign the pins via the PinMux, so it pretty much doesn’t matter which pins you use, as long as it isn’t one of the (many) reserved ones.

For ADC, you have to use an ADC pin, and there are limitations in conjunction with WiFi, you can’t use one of the ADC units when WiFi is active.

I’d strongly advise to get it all running without current sensing first (set appropriate voltage limits, and/or use a higher-ohm motor for first tests), and then carefully add the current sensing, being sure to use only pins allowed at the same time as WiFi.

@crashmatt - I’ve issues with my ESP32 simpleFOC project as well when trying to use any ADC pin and analogRead(). @Antun_Skuric explained that with the latest updates to enable lowside current sensing, they’ve have to modify how analogRead() works. When i try to use analogRead(), my esp board crashes. Not sure if it’s related, but figured I’d share.

@runger The lowside is using ADC1 and WiFi should be on ADC2 (if I am correct)

@wrcman555 Thanks for sharing. Doubt I would have found this on my own.
Should be easy to test. Will add a command to analogRead from the other ADC and see if it crashes.

MQTT is an add-on to the WiFi connection. I am pretty sure it is the WiFi that is the problem. Should have been more accurate with the title.

@wrcman555 - Your theory seems to be confirmed. I put in a command to analogRead. Reading any of the ADC2 pins makes it crash. Looks like I am stuck with voltage estimation and no FOC.

Not sure if this will be of help, but my friend Ralf had a few issues to get bidirectional ESP-NOW working with my first implementations of low-side current sensing on the ESP32. The ADC1 gave some odd values sometimes when receiving ESP-NOW packets, he found some tricks to get it working reliably: dagor_actuator_control/firmware/a_DA024F120 at main · elggem/dagor_actuator_control · GitHub

This was never tested on the lates espressif-arduino release though.

@crashmatt Antun shared the following with me on discord:

Hey @benschwe,
We had to modify the analogRead in order to use low side current sensing. It was not an issue before but maybe with a new release of the ESP support package it doesn’t like it anymore.
What you can do is to use the our modified version of the analogRead() called adcRead(). The syntax is the same, this function is just much faster, which was needed for the low side sensing.

You should be able to replace your analogRead with adcRead directly, without any new includes.
If that does not work you can add include:
#include “current_sense/hardware_specific/esp32/esp32_adc_driver.h”

@wrcman555 Voltage mode is operating ok for now. I would still like to have a better current monitoring. Will give adcRead a go once I get a few other things in order.

@wrcman555 I tell a lie. adcRead still won’t work. The closed source WiFi library/binary blob is doing its own thing with the ADC . If I can’t change that then there will never be compatibility.