Matlab model of simplefoc controller

Hello everyone,

I would like to know if someone has modelled a MatLab model of a controller based on simpleFOC?

I’ve found some example of BLDC Control from MatLab, but I’m a bit lost with it and I don’t succeed to match the model with the simplefoc library and controller.

My final goal is to be able to tune PID parameter more easly for speed and position control, thanks to the matlab model.

Thanks !

Hi @Jimenez_Lucas!

Welcome to the forum :slight_smile:

I personally do not have a Matlab model of a controller based on SimpleFOC, maybe others on the forum have tried it!

I only made simple simulations of simple DC motors in Simulink. Do you also have acces to Simulink? If so, do you also have access to the Simulink Control Design toolkit? This will make it a lot easier! I do not know how you got your licence but the Matlab license of my university also offers Simulink and the control design toolkit.

I found this on the web which might be useful for you!

Good morning,
I wish to thank you for your amazing work.
Is there the possibility to include SimpleFOC in a Simulink model like a block? I mainly use the embedded coder to compile my models for Arduino and ESP32 boards and I would like to try SimpleFOC.
I think that S-Function would require deep changes in the repo.

Thank you for the support,

Hi and welcome, @albertoc ,

Currently there is no such support. It certainly would be cool, but since Simulink/Matlab is a licensed product with no free version, AFAICT, the audience would be limited to Matlab license holders. Certainly I assume there would be some those among our ranks, but I’m not one of them…

Perhaps supporting something like Xcos (which is free and open source) or Blockly would open the solution to more people?

Maybe sci cosim/ octave? :thinking:

@runger thank you for your support.
For sure the Simulink integration will help all the student which have the license included with the university subscription (for example I am a Mechanical engineering student and I am not able to write my own FOC yet).
But I think it is not necessary. Do you think that separating the control part is possibile?
I can imagine a c++ function which takes as input all the sensors (motor and inverter parameters, position, current, voltage, …) and outputs the duty cycle.
I would leave all the pwm management which is characteristic of the microcontroller to the user (for instance, esp32 has a specific pheripheral available with simulink).

The c++ function can be integrated in almost every environment (Simulink, octave, amesim,…) without specializing for the specific one. The simulink coder will do the effort to compile and run the model in every compatible micro, expanding the possible application of the project.

Do you think it is a good idea?

At the moment I am developing an high power (20-50kw) pmsm control with traction control strategies (developed with Simulink). I’m facing mainly two ways to proceed:

  • integrating simplefoc into the ecu using a single micro
  • dedicating a bluepill for motor control which takes as input the request from the ecu (>2 micros on the board). But with this choice, the user looses the capability to do MiL testing and to exploit the PID optimizator.

If you agree, I would happy to put my effort to bring simplefoc into a Simulink open source implementation but I’m lacking on the hardware abstraction process from the repo code.


Hi Alberto,

I really don’t know enough about how Simulink and Matlab code generation work to be able to say if this can work or not…

In general terms, the code is already split in the way you want.The BLDCMotor and FOCMotor classes contain code which run the FOC algorithm and motion control, and output “phase voltages”.

Other parts of the code, the PWM drivers, are hardware specific and translate the phase voltages into PWM output on the various hardware architectures.

But the execution is very time-critical. You should aim for at least 1kHz iteration speed on the control loop, including reading the sensor, computing the FOC and the motion control. 10kHz is better if you want good performance.
So the whole code generation would have to assemble to final code in such a way that there are low latencies and everything executes quickly. It won’t work in MicroPython, for example, that’s just too slow.

I think it would be a very interesting project, and worthwhile, but I think it would amount to a large refactoring of the SimpleFOC codebase, and I am doubtful that these code-bases could be kept unified in the same repository…

if you choose to pursue it, please do keep us updated, I am sure people would be interested!

Thanks again @runger.
I’m pretty optimistic about the code execution speed (simulink c coder is the instrument that is used to build and flash the engine control units). The mechanism is completely different from micropython (which should be an interpreted language): here the simulink file is translated into c or c++ or assembler code (that you can see and modify). Micros company generally provide a simulink support package that is used to characterize the model according to the cpu architecture.
In this way you can achieve MHz tasks on the most powerful micro (ignition controller for instance).

I’m not confident enough on the software modifications but I will try for sure and I’ll refer to the forum :rofl: starting from the classes you said.

Thank you and congratulation for the amazing project.


1 Like


There are various blocksets in Simulink which include field oriented control. SimpleFOC is propably quite similar from the theory point of you. Check the documentation, for example (BLDC motor is same as PMSM):

Thank you @Paul for the support but the aim of the simulink porting is different. I would like to simulate the SimpleFOC implementation to tune the parameters and to verify the match with the inverter with the software I will flash later (Model-in-the-Loop testing).

Regarding the Simulink implementation I tried with S-Functions or Simulink c++ import wizard, but I’m not able to deal with the “Arduino.h” inclusion in every source file.
Would you like to give me some suggestions on how to extract the logic functions from the hardware-dependend operations?

Thank you in advance.

Basically, start with the functions loopFOC() and move() of BLDCMotor.cpp, and work your way “outwards” from there. Those functions handle the algorithm for FOC and motion control.

Hi @albertoc,

I also have an interest in porting SimpleFOC to Simulink. Have you had any progress with it?

Thank you in advance.