Async CurrentSense and SPI magneto reading for SAMD21

Hello folks,

I just created a MR for a major effort I spent the last couple of weeks to acheive.

It allows to sample 3 ADC pins and an SPI magneto at each PWM TOP event. It also uses _micros() to timestamp readings.

I spent significant time merging the dev branch back into my fork.

I think we should put more effort avoid duplication. functions defined in generic_mcu should not be cut & pasted to mcu_specific cpp files. in fact, we should maybe use OO programming there.

I only request one relatively minor API change only used with the LowsideCurrentSense class

I’ll eventually provide a .ino with a working example setup.

1 Like

I’m just looking it over! Awesome work!!!

One thing I would like to understand: why do you want to sync the sensor readings to the PWM events? I fully understand the need to do this for the ADCs in low side sensing, but where is the advantage to reading the sensor in sync with the PWM?
I also understand the potential benefits of reading the sensor asynchronously to the main loop code, but the linking to the PWM, that I don’t get…

The main loop uses 1 sensor reading per iteration (with the new sensor API). On SAMD21 you can expect about 2-3kHz on the main loop - on the other hand your PWM is going at 24kHz - 8-10x faster. So you’d be doing 9 unused sensor reads for each main loop iteration?

Also, generally speaking, the slower these readings, the more accurate they are (depends on the sensor, yes, but in general if you give them more time they can do more filtering and be more accurate).
On the other hand, you want as little lag between reading the sensor and using the data as possible, since the communication and the internal processing of the sensor already add lag.

I’m a bit confused I guess about the goal of the PWM-linked sensor reading…

Don’t worry, it is totally optional. It is basically for logging/studying purpose I will send all current sense and sensors readings on the pipe and study everyting. I want readings aligned temporarily. Also, the reasonning is that reading beign aligned would help taking the right decision.

On SAMD21 you can expect about 2-3kHz on the main loop

Paul Gould achieved 10khz with the samd21. No floats. With the samd51 I have M4F and its FPU.

Also, if one wants to substract back emf from current sense, one wants to know the rotor’s position at the moment of current sense…

What makes you think the magnetic sensor’s precision is affected by the read rate?

I think we’re only polling a register which is updated by an independent process.

I guess that’s true, although I have to think about how this is best done…

You’re right, I take this back… I think this depends on the sensor, and for the AS sensors this isn’t so clear.

I think it is definately possible, but at least at the moment that’s not what SimpleFOC aims for. You get that kind of speed by optimising for a specific architecture and use-case, and SimpleFOC aims to be general, for example as a learning tool and starting point for those wishing to engage in their own optimisation journeys.

I doubt very much that we could get 10kHz main loop performance on all our architectures and with all our use cases from the same code-base, while also keeping the code clear and understandable…

Well, on a M4F MCU running at 120Mhz… 10khz might me easy to acheive (12K MCU clock cycles/loop), without any hw-specific/clunky optimizations.

I’m chose this library because of the nice code readability efforts, and the excellent documentation efforts.

I’m sure we can find a way to make this library both simple and powerful.

Agree! Also on ESP32 and the faster STM32s… I just doubt it for SAMD21, unless you optimize a lot