Difference between revisions of "HANtune/MATLAB Interface"

From OpenMBD
Jump to: navigation, search
(Reading data from a running Simulink model)
(Reading data from a running Simulink model)
Line 37: Line 37:
 
updateLayout()
 
updateLayout()
 
print 'DAQ list created with ' + str(daqSize) + ' items'</nowiki>
 
print 'DAQ list created with ' + str(daqSize) + ' items'</nowiki>
 +
 +
====Reading====
 +
 +
<nowiki>#Start the simulation
 +
engine.eval("set_param('" + modelName +"','SimulationCommand','start');")
 +
print 'Simulation started...'</nowiki>
 +
 +
 +
<nowiki>#Read data from the model in Simulink and add it to the signals in HANtune
 +
while (True):
 +
data = engine.feval('getDaqListData', modelName, daqList, daqSize)
 +
if not data:
 +
break
 +
 +
i = 0
 +
for value in data:
 +
signals[i].setValueAndTime(value, 0)
 +
i += 1
 +
 +
print 'Simulation completed!'</nowiki>
  
 
==Reading data from a running Simulink model==
 
==Reading data from a running Simulink model==

Revision as of 19:00, 21 October 2019

HANtune can be interfaced with MATLAB® (and by extension Simulink®). We can do this by using the MATLAB engine and calling it from a script.

Note: You will need to have installed a version of MATLAB® with the Java MATLAB engine.

Connecting to the MATLAB Engine

Reading data from a running Simulink model

Set your model

#Change these to the name and location of your model
modelName = 'MyModel'
modelPath = 'C:\Users\Michiel Klifman\Desktop'


#Add folders to MATLAB path
from java.lang import System
scriptPath = System.getProperty("user.dir") + "\scripts\examples\MATLAB_Interface"
engine.eval("addpath('" + scriptPath + "')")
engine.eval("addpath('" + modelPath + "')")

Creating a daq list

#Create a daqlist from signals in the model
result = engine.feval(2, 'createDaqList', modelName, 'signals')
daqItemNames = result[0]
daqList = result[1]
daqSize = len(daqList)
#Create a signal for each item in the daq list
signals = []
removeAllSignals()
for name in daqItemNames:
	signal = createSignal(name)
	signals.append(signal)
updateLayout()
print 'DAQ list created with ' + str(daqSize) + ' items'

Reading

#Start the simulation
engine.eval("set_param('" + modelName +"','SimulationCommand','start');")
print 'Simulation started...'


#Read data from the model in Simulink and add it to the signals in HANtune
while (True):
	data = engine.feval('getDaqListData', modelName, daqList, daqSize)
	if not data:
		break

	i = 0
	for value in data:
		signals[i].setValueAndTime(value, 0)
		i += 1

print 'Simulation completed!'

Reading data from a running Simulink model