- Built by Dong Chen
- Started on Jan.11, 2020
A bridge between Python and Simulink. This file aims to build a bridge between Python and Simulink. At each time step, the python script will send a command (input) to the simulink model, then the simulink model executes for one step and then returns the results to Python scripts for the decision usage.
Install the MATLAB Engine API follow the instruction Installation.
- plant example
In this example, we build a PI controller to regulate a secord-order system to a reference value (10 here). The Python script compute the control input and sends then value to the Simulink model. Then Simulink model runs for one step and returns the output value to the python script.
Fig.1 Figure of regulation result
- tracking example
In this example, the controller tries to control the variables x1 and x2 to a regulate track xd1 and xd2. There is also a code-like model and controller built by Matlab code.
The simulink model looks like this,
where the u1 and u2 are control input from the Python script. The state variables x1 and x2 will be sent to the python script.
Fig.3 Tracking result, x1(blue) and xd1(orange)
- PV Control
This model is based on the Tutorial here.
The Gym-like environment file is located in IQL_conventional folder:
- env folder contains the environment file.
- config folder contains the system setting for RL model and environment.
- agent folder contains the RL model and policies.
- start the engine and connect to Matlab; Load the model:
matlab.engine.start_matlab()
self.eng.eval("model = '{}'".format(self.modelName),nargout=0)
self.eng.eval("load_system(model)",nargout=0)
-
eng.eval('out.output')
-
eng.workspace()
-
self.eng.set_param('{}/u'.format(self.modelName),'value',str(u),nargout=0): set the control input u.
-
Start Simulation and then Instantly pause and read output.
eng.set_param(self.modelName,'SimulationCommand','start','SimulationCommand','pause',nargout=0)
out = self.eng.workspace['out']
self.eng.eval('out.output'), self.eng.eval('out.tout')
- set_param
learn how to set parameters of blocks check here