Micropython port

Hello,

I have searched the forum but couldn’t find anything in relation to porting the code to Micropython. Are there any plans ( would it be possible) to port the code to micropython?
MicroPython has a Numpy like array manipulation library called ulab. It will probably be possible to use it to port the code. Any thoughts on that matter? Any suggestions? Maybe someone already done that ?

Thanks to the fact that SimpleFOC uses Arduino, you can run on it a wide variety of MCUs. I don’t see any benefit for porting the code to µPython. What benefit would be obtained by porting to µPython?

I’m not an expert and I may be wrong but I think it’s not technically feasible, since Python is an interpreted language whose performance is very much lower compared to C ++

I’ve thought about it :slight_smile:

MicroPython is becoming pretty popular for its ease of use and low barrier to entry for programming MCUs.

Thing is, its 100x slower than any C based solution, and SimpleFOC needs speed! The speed at which the main loop runs the loopFOC() function directly impacts the performance you can achieve - there’s no scope for jitter, breaks or timeouts there. Running it 100x slower - no chance of it working.

So porting SimpleFOC to python is a no-go, but creating API bindings so the C version of SimpleFOC can be called from MicroPython would be possible.
This would be possible on MCUs with enough power to run multiple threads. Then SimpleFOC could run in a high priority thread, while micropython could run in parallel at lower priority, and control the set-points via the API.

The thing is, there are hardly any MCUs with sufficient power to leave you with a lot of spare processing time to do a lot on the micropython side. Anything big you do there would directly impact the motor performance.

So on the whole I just don’t see the micropython and SimpleFOC on one MCU approach as a “winning” one. I think a approach where you let the SimpleFOC MCU control the motors, and add a second MCU or compact PC (RaspiPi, BeagleBone) which controls the first via CAN, I2C or similar will be much more flexible and perform much better.

I agree, calling the C from Micropython would be much faster. In relation to the MCU, what about esp32? Dual-core running at 240 MHz. So 1 core can be dedicated to SimpleFOC the other all the rest?

Update: Nevermind … For the moment Micropython runs on core1 only. So it won’t be possible until they sort the issue …

Certainly, ESP32 would be an example of a powerful MCU. Also the STM32H series and the Raspi RP2040… all these would be capable of doing it in the way I described above. If you have 2 cores like the RP2040 or the ESP32, then it’s even more likely you’ll get acceptable performance.

But really, where is the advantage? Getting the 2-core multi-threaded micropython setup (with a custom library not normally part of any MicroPython build) all set up will be a “big job”, and no longer accessible to beginner level users (whom micropython is aimed at). And if you’re a pro user, then why bother with a complex setup that won’t perform as well as just doing it all in C++?

I’m honestly asking - I don’t have any problem with micropython, and would be in favour of supporting it if it worked well and it is something people wanted and would benefit from… but at the moment I don’t quite see it as working out that way?

@exider, you seem to be in favour of it - what would be, from your point of view - the advantage or use-case that makes you want it?

People who prefer Python and use boards running Micropython obviously like the easy of use and flexibility the language provide. Form me, the main benefit would be that I won’t need to cut the project between 2 MCUs one running Micropython and second Arduino. This will add a layer of additional components and wiring…

An api that can be called from Micopython would be great!

1 Like

You are right. I think it is possible to port the code to Micropython. Are you familiar with Micropython?

I am not a developer, but I use micropython freely …

There are some math libraries available for Micropython:

Analytics

  • uMath - Computer Algebra for microcontrollers.
  • micropython-ulab - A numpy-like fast vector module for MicroPython.
  • micropython-fourier - Fast Fourier transform in MicroPython’s inline ARM assembler.
  • ulinalg - Small size matrix handling module with a few linear algebra operations specifically for MicroPython (Python3).
  • micropython-mtx - Fast Matrix Multiplication and Linear Solver on MicroPython.
  • micropython-vec - Vector Operations on MicroPython.

On the ESP32, MicroPython runs as a single task under FreeRTOS. The motor control could be run in another, high-priority FreeRTOS task, and commanded from MicroPython.

I don’t know whether running it on the second ESP32 core would be a good thing.

There would be effort involved in porting the Arduino-specific library functions to the ESP-IDF API, and in making the outer loop into its own task.

However, since the ESP32 SimpleFOC code already uses the ESP-IDF libraries for (at least) PWM control, this may not be too difficult.

2 Likes

(post deleted by author)