Skip to content

DongChen06/Python2Simulink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Python2Simulink

Robotics and Intelligent Vehicle Automation Lab (RIVAL)

  • 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 MATLAB Engine API for Python


Install the MATLAB Engine API follow the instruction Installation.

Applications


  1. 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.

output_example
Fig.1 Figure of regulation result

  1. 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,

output_example
Fig.2 simulink model

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.

output_example
Fig.3 Tracking result, x1(blue) and xd1(orange)

  1. PV Control

This model is based on the Tutorial here.

Gym-like Reinforcement Learning interface combined with Python and Matlab

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.

API


  • 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

Reference:


  1. link1
  2. Calling MATLAB from Python
  3. Troubleshoot MATLAB Errors in Python
  4. Call User Scripts and Functions from Python
  5. usage of eng.set_param

About

A bridge between Python and Matlab Simulink

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages