# Python-Simulink
Run your Simulink models & libraries in Python.
## Motivation
1. Running software-in-the-loop tests with Simulink becomes time consuming with Matlab & Simulink overhead. There are ways to reduce it (Model reference, etc) however nothing has shown to be as fast as a precompiled shared library.
2. Python has a very mature set of tools and packages to automate testing.
3. Testing can be distributed to machines without Matlab/Simulink licenses.
4. There are more and more Python developers. Being able to hand off a Simulink Library for further use there is a good feature.
## Use cases
1. Use Python and it's ecosystem to run complex Simulink models.
2. Use Python & `pytest` to run Software-in-the-Loop (SIL) tests on Simulink subsystems.
3. Give Simulink algorithms to developers without Matlab/Simulink licenses to use.
4. Use GitHub/GitLab actions and Python to automate testing in the cloud.
5. Start a programming language war at your company.
## Disclaimer
This repository is a set of instructions, with examples, on how to create a Pythonic wrapper for Simulink models. It **is not** a turnkey Python module to do this:
```python
import simulinkdll
simulinkdll.run("my_model.slx")
do_stuff()
```
For a given library or model configuring the Python should only need done when the Simulink Parameters/Signals change. The end developer will then be able do a `import simulink_model`, but it takes development time.
### High level instructions.
1. [Create a shared library in Simulink.](https://www.mathworks.com/help/ecoder/ug/creating-and-using-host-based-shared-libraries.html)
2. Create Python representations of all items in the header file.
3. Open the shared library (`.dll`, `.so`) in Python and run the model.
# Examples
### [Simple DLL Export](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example1/dllModel.ipynb)
- For demonstrating minimal dll functionality and the steps required to run a model in Python.
- Demonstrate implementions of `SimulinkGlobal` vs `ExportedGlobal` in `Simulink.Parameter` and `Simulink.Signal` variables.
![](Example1/dllModel.png)
### [Discrete Transfer Function](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example2/discrete_tf-python_class.ipynb)
![](Example2/discrete_tf.png)
A simple discrete transfer function. Compiled with a 1st order low pass filter.
There are two example notebooks for Example 2.
1. [Simple Example](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example2/discrete_tf.ipynb) - A simple low-level ctypes wrapper.
2. [Pythonic Example](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example2/discrete_tf-python_class.ipynb) - Use Python syntactic sugar to create a high level [TransferTF python](https://github.com/dapperfu/python_SimulinkDLL/blob/master/Example2/discretetf.py) class to interact with the model. Adds datalogging and pandas integration.
- Example 2 also contains sample `pytest` tests in the [`tests`](https://github.com/dapperfu/python_SimulinkDLL/tree/master/Example2/tests) directory. This demonstrates how you can use `pytest` to test Simulink models. Sample test results are shown shown in [Example2/test_results.md.](https://github.com/dapperfu/python_SimulinkDLL/blob/master/Example2/test_results.md)
- Tests can be run on [GitHub actions](https://github.com/features/actions) as well.
This is an example badge: [![Simulink DLL Test](https://github.com/dapperfu/python_SimulinkDLL/actions/workflows/blank.yml/badge.svg)](https://github.com/dapperfu/python_SimulinkDLL/actions/workflows/blank.yml)
![](GitHub_Actions.png)
### [Bouncing Ball](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example3/bouncing_ball.ipynb)
Adapted from [Mathworks's Simulation of a Bouncing Ball](https://www.mathworks.com/help/simulink/slref/simulation-of-a-bouncing-ball.html)
![](Example3/bouncing_ball.png)
Running a Simulation in Simulink also has some overhead. By compiling the model to a shared library and executing it, this overhead is eliminated.
`bouncing_ball_benchmark.m` benchmarks the model by testing increasingly smaller time steps. The model was then compiled and tested in Python and the corresponding times are recorded below.
| Time Step | Simulink Duration (s) | Python Duration (s) |
| --------- | --------------------- | ------------------- |
| 1e-4 | 0.5905 | 0.06 |
| 1e-5 | 1.0461 | 0.61 |
| 1e-6 | 8.1991 | 6.08 |
| 1e-7 | 78.9901 | 60.18 |
# Jenkins Build Automation
This project also serves as a proof of concept for using [CI/CD devops techniques](https://www.atlassian.com/continuous-delivery/principles/continuous-integration-vs-delivery-vs-deployment) with Simulink Models. There is a [`Jenkinsfile` ](Jenkinsfile) that will build each of the examples and archive the artifacts:
- shared library (`.dll`)
- header files (`.h`)
Jenkins Pipeline screenshot:
![Jenkins pipeline screenshot](jenkins_pipeline.png)
Jenkins Artifacts screenshot:
![Jenkins artifacts](jenkins_artifacts.png)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
仿真-使用共享库使用Python运行Simulink仿真模型-附项目源码+例程-优质项目实战.zip (140个子文件)
bouncing_ball.bat 363B
discrete_tf.bat 359B
dllModel.bat 353B
bouncing_ball.c 5KB
dllModel.c 3KB
ert_main.c 3KB
ert_main.c 3KB
ert_main.c 3KB
discrete_tf.c 3KB
dllModel_data.c 701B
rtwreport.css 6KB
coder_app.css 4KB
bouncing_ball.def 210B
dllModel.def 173B
discrete_tf.def 169B
bouncing_ball_win64.dll 48KB
dllModel_win64.dll 48KB
discrete_tf_win64.dll 47KB
codedescriptor.dmr 409KB
codedescriptor.dmr 376KB
codedescriptor.dmr 375KB
spinner.gif 4KB
rtwtypes.h 5KB
rtwtypes.h 5KB
rtwtypes.h 5KB
dllModel.h 4KB
bouncing_ball.h 4KB
discrete_tf.h 4KB
dllModel_private.h 3KB
dllModel_types.h 854B
discrete_tf_types.h 827B
bouncing_ball_types.h 784B
discrete_tf_private.h 756B
bouncing_ball_private.h 706B
rtwtypes_h.html 27KB
bouncing_ball_c.html 25KB
bouncing_ball_h.html 19KB
ert_main_c.html 14KB
bouncing_ball_trace.html 11KB
bouncing_ball_contents.html 8KB
bouncing_ball_interface.html 6KB
bouncing_ball_types_h.html 4KB
bouncing_ball_private_h.html 4KB
bouncing_ball_coderassumptions.html 4KB
navToolbar.html 3KB
bouncing_ball_survey.html 3KB
bouncing_ball_subsystems.html 3KB
rtwmsg.html 3KB
bouncing_ball_codegen_rpt.html 2KB
inspect.html 866B
bouncing_ball_metrics.html 747B
bouncing_ball_replacements.html 652B
nav.html 319B
discrete_tf-python_class.ipynb 173KB
discrete_tf.ipynb 157KB
bouncing_ball.ipynb 93KB
dllModel.ipynb 9KB
Jenkinsfile 695B
rtwhilite.js 71KB
coder_app.js 19KB
search.js 17KB
rtwshrink.js 6KB
define.js 5KB
rtwreport_utils.js 3KB
bouncing_ball_traceInfo.js 3KB
traceInfo_flag.js 2KB
dllModel_win64.lib 8KB
bouncing_ball_win64.lib 8KB
discrete_tf_win64.lib 7KB
bouncing_ball_benchmark.m 834B
dllModel_init.m 808B
discrete_tf_init.m 720B
bouncing_ball_init.m 593B
bouncing_ball_build.m 366B
discrete_tf_build.m 351B
dllModel_build.m 294B
README.md 5KB
README.md 1KB
README.md 1022B
test_results.md 364B
README.md 251B
discrete_tf.mdl 30KB
dllModel.mk 14KB
bouncing_ball.mk 14KB
discrete_tf.mk 12KB
bouncing_ball.obj 2KB
dllModel.obj 2KB
discrete_tf.obj 2KB
dllModel_data.obj 495B
jenkins_artifacts.png 66KB
GitHub_Actions.png 42KB
discrete_tf_23_1.png 28KB
bouncing_ball.png 26KB
sldemo_bounceExample_02.png 26KB
discrete_tf_24_1.png 24KB
discrete_tf_step.png 22KB
step_response.png 22KB
dllModel_Solver.png 20KB
dllModel.png 20KB
bouncing_ball_8_1.png 18KB
共 140 条
- 1
- 2
资源评论
极智视界
- 粉丝: 3w+
- 资源: 1755
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功