Coupled Physics and FPGA Simulation

An open source controller development workflow using OpenModellica for physics simulation and Icarus verilog for controller simulation.

Similar projects worth following
This is the general recipe I use for coupled simulations with cocotb, icarus verilog and OpenModelica during design, debug and testing of automatic controllers running on FPGA's. I've had good success with this coupling strategy in one project so far in a very hardcoded manner and as I continue to use it in other projects I will try to identify the canonical chunks of code, isolate them and release them in a repository.

When I'm developing a control system, first I model the system to be controlled in Modelica.  Modelica is is a specialized language for simulation of physical systems.  OpenModellica is a great open source modelica tool and there are commercial tools as well.  The Modelica language specification includes an external interface that's essential for this project.  If you new to Modellica I highly recommend Modellica by Example by Dr. Michael Tiller.  The full text is available for free online here.

Next I model the controller.  At this point I'm trying to figure out the general form of the control loop, if the desired control of the system is even possible ect.  Many controllers can be modeled in the Modelica language or even using blocks in the modelica standard library.   The modelica language is an equation based language and it's optimized for modelling physical systems but it's very capable of modelling the behavior of the controller as well.  I have modeled fairly complex controllers using the modelica language.  It's also possible to use modelica external functions to augment or fully implement the controller model at this stage.

When it's time to prototype the controller in hardware, assuming the control algorithm to be implemented is to run on a FPGA, I implement the controller in my hardware description language of choice, verilog.  (If you're looking for a way to learn verilog I highly recommend  Once the controller is implemented or even not all the way but implemented enough to be at the point where's is helpful to have some physics to interact with, it's time to do the coupling.

Icarus verilog is an open source verilog simulation tool.  Cocotb uses Icarus as a back end (and can use other simuation tools as well...).  As stated on it's project website, 'Cocotb is a coroutine based cosimulation library for writing VHDL and Verilog testbenches in Python.'  Cocotb gives you a nice enviromnent where you can interact with the verilog controller and also communicate to the modellica simulation.

Basically you need a modellica tool (for example OpenModellica), Cocotb and a simulation backend (Icarus verilog for example).  On the modelica side you create an external object that will handle sending and receiving data to and from cocotb and create a block to be inserted into the system model that replaces the modelica version of the controller.  Beej's Guide to Network Programming will get you up to speed on to make the modelica external object and cocotb talk over sockets.  There is a client and a server.  In the past I have made the cocotb part the server and the modelica simulation part the client but in the future I think I will do it the other way because generally the cocotb needs be restarted to make changes more often than the modelica simulation.  Doing it this way it will be possible to make shut down the controller simulation and make changes while the modelica simulation stays running.

The sequence of computations and communications are illustrated here:

This coupling method is pretty good for controllers that sample data from sensors.  The main limitation of this coupling method is that during each time the hdl simulation (icarus and cocotb) runs it must be known when sensor data must be acquired from the modelica model next.  Consider the example where the controller triggers an analog to digital conversion.  The time of the ADC conversion is determined by the controller logic so the controller simulation can run up until the trigger and then switch to the physics simulation which will then run up until the time of the ACD conversion.  All good here :) Now consider an analog signal determined by some physics in the modelica model connected to a comparitor in turn connected to a pin on the FPGA.  This is an input to the controller that can't be...

Read more »

  • Interface generator released on GitHub

    Adam Lange11/20/2018 at 19:28 0 comments

    I used the coupling strategy once so far.  It worked quite well.  The first time through I hand coded everything.  For the second time through, I'm making a code generator.  At the time of writing, the generator is mostly done.  The generator outputs a python module to be integrated in a cocotb test bench and a modelica model.  I'll be trying to use it soon.

    Although it's almost certainly full of bugs, I have the generator up on GitHub:

View project log

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates