|  | 
| 1 |  | -# dllModel - Python Runner Notebook | 
| 2 |  | - | 
| 3 |  | - | 
| 4 |  | - | 
| 5 |  | -# Python Setup | 
| 6 |  | - | 
| 7 |  | -This is the boilerplate setup needed before running the model. For readability the [`rtwtypes.h`](https://github.com/dapperfu/python_SimulinkDLL/blob/master/Example1/rtwtypes.py) is in a separate file. | 
| 8 |  | - | 
| 9 |  | - | 
| 10 |  | -```python | 
| 11 |  | -from rtwtypes import * | 
| 12 |  | -``` | 
| 13 |  | - | 
| 14 |  | -### ```dllModel.h``` | 
| 15 |  | - | 
| 16 |  | - | 
| 17 |  | -```python | 
| 18 |  | -class B_dllModel_T(ctypes.Structure): | 
| 19 |  | - _fields_ = [ | 
| 20 |  | - ("SimulationSignal1", real_T), | 
| 21 |  | - ("SignalOut3", real32_T), | 
| 22 |  | - ("SignalOut2", uint16_T), | 
| 23 |  | - | 
| 24 |  | - ] | 
| 25 |  | -class ExtU_dllModel_T(ctypes.Structure): | 
| 26 |  | - _fields_ = [ | 
| 27 |  | - ("SignalIin2", uint16_T) | 
| 28 |  | - ] | 
| 29 |  | -class ExtY_dllModel_T(ctypes.Structure): | 
| 30 |  | - _fields_ = [ | 
| 31 |  | - ("OutputPort2", real32_T) | 
| 32 |  | - ] | 
| 33 |  | -class P_dllModel_T(ctypes.Structure): | 
| 34 |  | - _fields_ = [ | 
| 35 |  | - ("K2", uint16_T) | 
| 36 |  | - ] | 
| 37 |  | - | 
| 38 |  | -class Timing(ctypes.Structure): | 
| 39 |  | - _fields_ = [ | 
| 40 |  | - ("clockTick0", uint32_T), | 
| 41 |  | - ("clockTickH0", uint32_T), | 
| 42 |  | - ] | 
| 43 |  | -class tag_RTM_dllModel_T(ctypes.Structure): | 
| 44 |  | - | 
| 45 |  | - _fields_ = [ | 
| 46 |  | - ("errorStatus", ctypes.c_char_p), | 
| 47 |  | - ("Timing", Timing), | 
| 48 |  | - ] | 
| 49 |  | -``` | 
| 50 |  | - | 
| 51 |  | - | 
| 52 |  | -```python | 
| 53 |  | -import os | 
| 54 |  | -dll_path = os.path.abspath('dllModel_win64.dll') | 
| 55 |  | -dll = ctypes.windll.LoadLibrary(dll_path) | 
| 56 |  | -``` | 
| 57 |  | - | 
| 58 |  | - | 
| 59 |  | -```python | 
| 60 |  | -# Block parameters (default storage) | 
| 61 |  | -dllModel_P = P_dllModel_T.in_dll(dll, 'dllModel_P') | 
| 62 |  | -# Block signals (default storage) | 
| 63 |  | -dllModel_B = B_dllModel_T.in_dll(dll, 'dllModel_B') | 
| 64 |  | -# External inputs (root inport signals with default storage) | 
| 65 |  | -dllModel_U = ExtU_dllModel_T.in_dll(dll, 'dllModel_U') | 
| 66 |  | -# External outputs (root outports fed by signals with default storage) | 
| 67 |  | -dllModel_Y = ExtY_dllModel_T.in_dll(dll, 'dllModel_Y') | 
| 68 |  | -``` | 
| 69 |  | - | 
| 70 |  | - | 
| 71 |  | -```python | 
| 72 |  | -""" | 
| 73 |  | - * Exported Global Signals | 
| 74 |  | - * | 
| 75 |  | - * Note: Exported global signals are block signals with an exported global | 
| 76 |  | - * storage class designation. Code generation will declare the memory for | 
| 77 |  | - * these signals and export their symbols. | 
| 78 |  | - """ | 
| 79 |  | -SignalIn = real32_T.in_dll(dll, 'SignalIn') | 
| 80 |  | -SimulationSignal2 = real_T.in_dll(dll, 'SimulationSignal2') | 
| 81 |  | -SignalOut = real32_T.in_dll(dll, 'SignalOut') | 
| 82 |  | -""" | 
| 83 |  | - * Exported Global Parameters | 
| 84 |  | - * | 
| 85 |  | - * Note: Exported global parameters are tunable parameters with an exported | 
| 86 |  | - * global storage class designation. Code generation will declare the memory for | 
| 87 |  | - * these parameters and exports their symbols. | 
| 88 |  | -""" | 
| 89 |  | -K = real32_T.in_dll(dll, 'K') | 
| 90 |  | - | 
| 91 |  | -# Model entry point functions | 
| 92 |  | -dllModel_initialize = dll.dllModel_initialize | 
| 93 |  | -dllModel_step = dll.dllModel_step | 
| 94 |  | -dllModel_terminate = dll.dllModel_terminate | 
| 95 |  | - | 
| 96 |  | -# Real-time Model object | 
| 97 |  | -dllModel_M = ctypes.POINTER(tag_RTM_dllModel_T).in_dll(dll, 'dllModel_M') | 
| 98 |  | -``` | 
| 99 |  | - | 
| 100 |  | -# Running The Model. | 
| 101 |  | - | 
| 102 |  | -Before running the model you will need to run the model init function. | 
| 103 |  | - | 
| 104 |  | - | 
| 105 |  | -```python | 
| 106 |  | -dllModel_initialize(); | 
| 107 |  | -``` | 
| 108 |  | - | 
| 109 |  | -Take an initial step into the model, function returns the current step number. | 
| 110 |  | - | 
| 111 |  | - | 
| 112 |  | -```python | 
| 113 |  | -dllModel_step() | 
| 114 |  | -``` | 
| 115 |  | - | 
| 116 |  | - | 
| 117 |  | - | 
| 118 |  | - | 
| 119 |  | - 1 | 
| 120 |  | - | 
| 121 |  | - | 
| 122 |  | - | 
| 123 |  | -Inspect the simulation time, both in the Block signal structure and in the global variable. | 
| 124 |  | - | 
| 125 |  | - | 
| 126 |  | -```python | 
| 127 |  | -[dllModel_B.SimulationSignal1, SimulationSignal2] | 
| 128 |  | -``` | 
| 129 |  | - | 
| 130 |  | - | 
| 131 |  | - | 
| 132 |  | - | 
| 133 |  | - [0.0, c_double(0.0)] | 
| 134 |  | - | 
| 135 |  | - | 
| 136 |  | - | 
| 137 |  | -Take another step and inspect the simulation time again. | 
| 138 |  | - | 
| 139 |  | -0.1s matches the discrete step size specified in the model. | 
| 140 |  | - | 
| 141 |  | - | 
| 142 |  | - | 
| 143 |  | - | 
| 144 |  | - | 
| 145 |  | -```python | 
| 146 |  | -dllModel_step() | 
| 147 |  | -[dllModel_B.SimulationSignal1, SimulationSignal2] | 
| 148 |  | -``` | 
| 149 |  | - | 
|  | 1 | +### Example 1: [dllModel.ipynb](https://nbviewer.jupyter.org/github/dapperfu/python_SimulinkDLL/blob/master/Example1/dllModel.ipynb) | 
| 150 | 2 | 
 | 
| 151 |  | - | 
| 152 |  | - | 
| 153 |  | - [0.30000000000000004, c_double(0.30000000000000004)] | 
| 154 |  | - | 
| 155 |  | - | 
| 156 |  | - | 
| 157 |  | -### Manipulating Signals | 
| 158 |  | - | 
| 159 |  | -& Reading Outputs | 
|  | 3 | +For demonstrating minimal dll functionality and the steps required to run a model in Python. | 
| 160 | 4 | 
 | 
| 161 | 5 |  | 
| 162 | 6 | 
 | 
| 163 |  | - | 
| 164 |  | -```python | 
| 165 |  | -SignalIn.value=float(2) | 
| 166 |  | -SignalOut | 
| 167 |  | -``` | 
| 168 |  | - | 
| 169 |  | - | 
| 170 |  | - | 
| 171 |  | - | 
| 172 |  | - c_float(2.0) | 
| 173 |  | - | 
| 174 |  | - | 
| 175 |  | - | 
| 176 |  | - | 
| 177 |  | -```python | 
| 178 |  | -dllModel_step() | 
| 179 |  | -SignalOut | 
| 180 |  | -``` | 
| 181 |  | - | 
| 182 |  | - | 
| 183 |  | - | 
| 184 |  | - | 
| 185 |  | - c_float(2.0) | 
| 186 |  | - | 
| 187 |  | - | 
| 188 |  | - | 
| 189 |  | - | 
| 190 |  | -```python | 
| 191 |  | -[dllModel_B.SignalOut2, dllModel_B.SignalOut3, SignalOut] | 
| 192 |  | -``` | 
| 193 |  | - | 
| 194 |  | - | 
| 195 |  | - | 
| 196 |  | - | 
| 197 |  | - [2, 4.0, c_float(2.0)] | 
| 198 |  | - | 
| 199 |  | - | 
| 200 |  | - | 
| 201 |  | - | 
| 202 |  | -```python | 
| 203 |  | -dllModel_U.SignalIin2=1 | 
| 204 |  | -``` | 
| 205 |  | - | 
| 206 |  | - | 
| 207 |  | -```python | 
| 208 |  | -dllModel_step() | 
| 209 |  | -[dllModel_B.SignalOut2, dllModel_B.SignalOut3, SignalOut] | 
| 210 |  | -``` | 
| 211 |  | - | 
| 212 |  | - | 
| 213 |  | - | 
| 214 |  | - | 
| 215 |  | - [2, 4.0, c_float(2.0)] | 
| 216 |  | - | 
| 217 |  | - | 
| 218 |  | - | 
| 219 |  | - | 
| 220 |  | -```python | 
| 221 |  | -dllModel_M.contents.Timing.clockTick0 | 
| 222 |  | -``` | 
| 223 |  | - | 
| 224 |  | - | 
| 225 |  | - | 
| 226 |  | - | 
| 227 |  | - 6 | 
| 228 |  | - | 
| 229 |  | - | 
| 230 |  | - | 
| 231 |  | - | 
| 232 |  | -```python | 
| 233 |  | -step = dllModel_step() | 
| 234 |  | -assert dllModel_M.contents.Timing.clockTick0==step | 
| 235 |  | -``` | 
0 commit comments