Hopsan
Introduction Writing Custom Components

The majority of the Hopsan component code is contained within the HopsanCore. When a new component is created, only the parts which make the new component unique needs to be specified. All other functionality is inherited from the base implementation in the HopsanCore. The code that is written for the new components must be compiled into a shared object file .dll (Windows), .so (UNIX) or .dynlib (MacOS) file. This file will contain the runtime executable code for the new components. While simulating, Hopsan will call functions in this file when the component is created, initialized, finalized and when it takes a simulation step.

The compiled library file can include one or several new components. It must also include two mandatory functions used by the HopsanCore to get information about and to register the contents of the library once it is loaded. A description of the library files is given in The Component Library Files

Prerequisites

  • Basic knowledge of C++
  • Basic knowledge of TLM modeling (unless you are writing a Signal component)
  • Development tools
    • Compiler, MinGW32 or MinGW64 (MS Windows), GNU GCC (Linux) VisualC++ (Windows) is needed if you want to compile a model export to Matlab/Simulink.
    • Qt Creator (IDE) and the Qt libraries (if you are writing libraries manually based on the Example component library) (The examples use Qt project files that must be processed by qmake to generate Makefiles, but you may use any code editor that you like) If you use another IDE or development environment you must write the Makefiles yourself (if you choose this approach we assume that you know how).

See the page_setupbuildenvironment or How to setup the component library development environment page for instructions on how to set up your development environment.

The Example Component Library

The documentation examples are based on the exampleComponentLibrary that is included in each Hopsan release. You can find it in your Hopsan installation directory, under componentLibraries/exampleComponentLib. These documentation examples contain subsets of the original files, you should also look at the original files for their full content and additional comments. When creating a new component library, you can use the example files as a "getting started" template.

Tools

generateLibraryFiles.py:
If you end up building large component libraries, then this python script may come in handy. It can be used to automatically generate header and source include files as well as Qt project include files for the entire library tree recursively. See the defualtComponentLibrary that is included in each Hopsan release for an example.