How to use Simlink PID tools to implement a controller in matlab - matlab

I was wondering if it is possible to use simlink's PID architecture in matlab to actually control hardware - rather than modelling it.
I have been playing around a little with the PID tuning in matlab - and have worked out the correct gains for my system (or at least good enough).
Now I want to implement the actual control loop in matlab using the real hardware. In this case I am tying to stabilise a laser using a measurement from a wavemeter - although from the point of view of matlab essentially I have two functions - one which returns the current wavelength, and another which alters the wavelength with an input from 0-100.
How do I get the fancy PID objects to work in a loop where for example I input data each loop, and get the required output to stabilise things given my gains - or should I just hardcode the equations in?
I have tried looking through some of the simlink examples - but there doesn't seem to me much actual implementation - mostly just modelling.
Thanks in advance for any advice.

MATLAB (without Simulink, or with Simulink in purely a simulation mode) is not a real-time environment. Trying to use it to control hardware in real-time won't work unless you have very slow sample rates.
If you do have slow sample rates, then you'd connect to your hardware to MATLAB using device drivers such as those in the Data Acquisition Toolbox
You haven't found any info on real-time implementation in the Simulink documentation because from Simulink the mechanism for real-time implementation is via Simulink Coder. You would need to use it in conjunction with a real-time environment such as Simulink Desktop Real-Time (if your sample rates are relatively slow), Simulink Real-Time, or one of many other 3rd party real-time form factors.

Related

Simscape, COMSOL compatibility in simulating electromagnetic range of inductive charging

I am currently trying to use Simscape to design and simulate the electrical circuit and COMSOL Multiphysics to simulate the electromagnetic interaction between coils. What I'm not certain of is whether or not we can successfully link the two software packages nicely via MATLAB or not. They both have MATLAB support though.
I am also researching whether ANSYS suite. There may be some software we can use that would take the place of one or both of the previous software.
Several years ago I had to make a connection between COMSOL and Matlab as well. This is called a Matlab Comsol LiveLink.
However, I think (not sure), that the electrical circuit should be modeled in COMSOL. Using the LiveLink you can set parameters in COMSOL using Matlab and extract the updated calculation. To my knowledge you cannot input a model, do calculations, and then extract the results.
You can look in the users guide, maybe it says something about this
Some other links which may be useful:
LiveLink™ for MATLAB®
Integrate COMSOL Multiphysics® with
MATLAB® Scripting

Data from LabVIEW to Matlab for processing

I want to make a biometric identification system of the ECG/EKG.
Provided that Matlab does not perform Data Acquisition in Real Time (for monitoring), is there any way to make the monitoring and data acquisition in LabVIEW and then work simultaneously with Matlab for signal processing?
You could just get a matlab compatible daq and run everything in matlab. http://www.mathworks.com/products/daq/
You can indeed do some data acquisition with LabView and work simultaneously with Matlab for signal processing by calling the Matlab script node, which executes some Matlab code during vi execution.
You may have some performance issues, though, because both Labview and Matlab have to run on your machine simultaneously.
Question:
is there any way to make the monitoring and data acquisition on
LabView and then work simultaneously with Matlab for signal processing
Answers:
LabVIEW has "MathScript" node which is basic MatLab built into
an add-on. It is not the MatLab toolboxes. It runs native MatLab
code. It also runs slightly faster LabVIEW updates to the code. If
your code runs there, then LabVIEW will pass data natively
to your code. This box does not have direct MatLab toolbox access, so if
you use any special calls then that can cause a problem.
If you have MatLab on the box, then you can call the external MatLab
function/code using mathscript (link), and the MatLab will run
the function.
Clarification:
Real time just means "bounded time" (link), not "instant". If your idea of bounds are loose enough then many systems can work for them. You do not state it in your question - but what do you consider acceptable response time?
I've worked a lot with LabVIEW and Matlab. Personally, I would not use the Math Scripting node and would opt for using the Matlab Automation Server. You can call Matlab from LabVIEW using the ActiveX palette in LabVIEW (See Functions>>Connectivity>>ActiveX>>Automation Open) A couple reasons why I'd go for ActiveX and NOT the MathScript node:
The Math Script node does not allow you to change code dynamically. You must hardcode your data into the Math Script node and any future changes would require a change to LabVIEW's G code and therefore a recompile of your EXE
The Math Script node does not support all functions when compiled to an executable. Most notably graphing functions. See the help file here to read more on this.
Calling Matlab from ActiveX is going to give you a lot more flexibility in regards to how data is passed and processed.

Performance improvement of simulink coder over accelerated models

I wonder if there's some experience out there, how much performance can be gained from using the Simulink Coder to generate models over running the model in (rapid) accelerator mode?
For instance, how does it compare to the quite dramatic improvements from not-accelerated to accelerated models?
I know this is kind of vague and probably depends on the specifics of a model, but I'd be happy with examplary numbers.
I don't know if you will really find speed improvement anecdotes that helpful, as your results are going to be highly dependent on your own implementation.
That said, the following reference on How Acceleration Modes Work might be useful to you.
As described in this link...
The Accelerator mode generates and links code into a
C-MEX S-function. Simulink uses this acceleration target code to
perform the simulation...
...the model methods are separate from the Simulink
software and are part of the Acceleration target code. A C-MEX
S-function API communicates with the Simulink software, and a MEX API
communicates with MATLAB. The target code executes in the same process
as MATLAB and Simulink.
The link goes on to describe Rapid Acceleration, as such:
The Rapid Accelerator mode creates a Rapid Accelerator standalone
executable from your model. This executable includes the solver and
model methods, but it resides outside of MATLAB and Simulink. It uses
External mode (see Host/Target Communication) to communicate with
Simulink.
(Note that the actual link has some nice diagrams that help to illustrate this architecture).
I think that there are a few important things to takeaway from these descriptions. First, in Accelerator Mode, code is only generated for the model methods; however, in Rapid Accelerator Mode, code is also generated for the solver. In either case there will be some overhead due to communication between the generated code and Simulink (though in general that overhead would be greater in non-rapid accelerator mode).
Now, whether you are going to see significant differences between these acceleration modes and full-on code generation will most likely depend on how you would be using the generated code. You had stated that your application is intended to essentially analyze/visualize the outputs of simulink models. Do you intend for you application, itself, to be a Simulink model? If so, I imagine your workflow would be that you generate code for the "plug-in" model and then incorporate this into your "parent" model as an s-function. Based on the descriptions given above, my gut feeling is that you would not see much of a performance improvement compared to accelerator mode.
However, if your application is to be implemented outside of Simulink, then code generation would definitely be the way to go. In this case, you've completely eliminated the overhead of communicating with Simulink. In this workflow, you could, for example, generate a stand-alone executable from the "plug-in" model, execute that model to get the outputs, and then simply load those outputs for analysis and visualization into your application. Your application could be written in any language you want. In fact, you could even still write your application in MATLAB -- the key point, though, being that your application runs once the stand-alone executable (generated from the "plug-in" model) has finished executing, rather than interfacing with the plug-in model while it is executing.

What's the difference between stateflow and simulink?

As far as I know, stateflow and simulink are often used at the same time, and are both environments developed by MathWorks, who make Matlab. May I know what's the difference between them?
Simulink is largely a controls oriented solution. It graphically depicts math like products, sums, integrals, etc. However, it's conditional logic facility is lacking. Any kind of if construct quickly becomes terse and unmanageable in my opinion. I've seen many models, and there is a clear line here where Simulink should end and Stateflow should start.
Stateflow deals extremely well with logic and, of course, state machines. Now with the addition of the Simulink Function blocks within Stateflow, we have a powerful combination to allow the state machine in Stateflow direct the rest of the program.
As far as functionality goes, they are both functionally complete, meaning anything you can code in C, you can code in Simulink or Stateflow. However, I would not recommend coding a PID loop in Stateflow, but it's possible. You could also easily create a state machine in Simulink, but I'd advise against it.
As far as code generation; in the beginning of the meld, the Stateflow and Simulink had separate code generators that were sewed together with more Simulink generated C code at code generation time. Then came CGIR (Code Generation Intermediary Representation), which unifies the code generator between Stateflow and Simulink. It came around 2007, and has continued to deliver substantial increases in performance. Generated code has increased in on target performance to a point where companies can use the code in their embedded systems and actually get a performance benefit rather than take a small hit. Also, the time to generate the code has also decreased substantially. CGIR is a replacement for the Target Language Compiler, however, never fear, TLC API will still be available, perhaps forever for those who have developed massive libraries of proprietary code generation libraries.
Hope this helps, let me know if something sounds fishy or if I need to clarify.
Quoting a Stateflow Webinar:
Simulink is used to respond to continuous changes in dynamic changes.
Stateflow is used to respond to instantaneous changes in dynamic changes.
Real-world systems have to respond to both continuous and instantaneous changes.
Use both Simulink and Stateflow so that you can use the right tool for the right job. Examples: Suspensions dynamics of a car are modelled with Simulink whereas, the gear transmission is modelled with Stateflow.
Learn more about Stateflow in general at:
http://www.mathworks.com/products/stateflow/examples.html
Stateflow has been updated for making it very easy to create state machines and flow charts in R2012b.
The major updates include a new graphical editor, state transition tables, MATLAB as the action language and an integrated debugger. Find short videos for these features and how they can be used at:
http://www.mathworks.com/products/stateflow/whatsnew.html
best,
Siddharth
I'm also currently involved with both simulink and stateflow. Till now I did everything in Simulink but once you have to implement logic( if, case) your model becomes visually difficult to be analysed after coding. But I think theoretically you can do everything also just in Simulink (correct me if I'm wrong).
The answer of macduff explains very good the differences.
Regards,
GR
I have worked on both on simulink and state flow environment both are Matlab tool. When u want to design a reactive system or event driven system at that time we should prefer stateflow instead of simulink.
because we can control an event in stateflow in better way compare to simulink .though debugging is easy in simulink but readability and code generation are easy in stateflow.
Complex state machine: Stateflow
Complex mathematical process: Simulink blocks
Others: both of Stateflow and Simulink are fine
Another major benefit of Stateflow is the integration and usage of external C code. This can be done by selecting the Action Language C within the Chart settings.
As commonly known the user can include external Code within the "Simulation Target" options. While a normal Simulink model need small workarounds to access the external C code (ceval(), Simulink.Parameter for globals etc.), Stateflow can directly access functions, defines, etc.
Problems like array of buses/structs can also be avoided by coding these arrays in external code.
For a lot of use cases I love the flexibility to directly interact with C code within Stateflow.

MATLAB vs Simulink in digital image processing?

Is there anything I can do with code in MATLAB for digital image processing that I can't do with Simulink's digital image processing toolbox or vice versa?
What difference is there between the two?
At the code level, there's not much difference between MATLAB and Simulink. Simulink provides an environment for constructing programs at a higher level of abstraction than text; the relationships that hold between code blocks is represented as arrows that pass between flowchart objects. You can then visualize the system as it operates at runtime, as data objects are passed visibly between the flowchart blocks. Simulink also lets you alter a part of the program in real time, so that you can see the effects of the local change without having to stop everything else.
Given how much image processing work is done in block-level dataflow languages such as LabVIEW, I would recommend starting with Simulink. Most industrial or creative image processing (like Apple's Shake) is done via a node-based interface. That's not to say that you couldn't use MATLAB code as well; it's just a bit more obtuse (this is a good example of a powerful library that was made with MATLAB code).