## Compatibility with Simulink- Download Aviation + Simulink project source code - 2.21 MB
- Download Aviation + Simulink + OpenGL project source code - 2.47 MB
- Download Aviation +_Simulink + WPF project source code - 2.45 MB
- Download Mechanical aggregate library source code - 932.54 KB
- Download additional files with containers and 3D shapes - 333.84 KB
- Download Sample of Simulink file import - 8.2 KB
- Download of identification of parameters of simulink component - 14.96 KB
- Download sample of spatial geometry of antenna - 2 KB
- Download sample of 2 channel follow-up system - 72.55 KB
- Download sample of 2 channel follow-up system + virtual reality - 81.58 KB
Other useful resources: ## 1. IntroductionUniversal science and engineering framework should be compatible with any third party science and engineering software. Compatibility with IronPython had been considered in "Universal Framework for Science and Engineering - Part 12: Grandiose Projects" article. This article is devoted to compatibility with Simulink. I do not have full compatibility with Simulink yet. Rather I have found answers of following two questions: - Could be *.mdl Simulink files easy exported by the framework;
- What advantages provides export of these files.
I have tried to develop adapter which is enable to import some Simulink files and I have found that compatibility problem is enough easy. Advantages of compatibility will be considered in this and following my artiles. Now I am continuing writing "Grandiose Projects 2. The incredible machine" article and I have found that the incredible machine without Simulink is not incredible machine. Later I will add Simulink samples to incredible machine. Since "Grandiose Projects 2. The incredible machine" arctile is overloaded I have decided to write new article. Both articles will written simultaneously. This article is devoted to compatibility with Simulink. My previous article will be complemented by Simulink applications. ## 2. BackgroundSimulink block schemes contain blocks with math transformations. These blocks are connected by lines. The meaning of lines is information flow. The framework has typed arrows. One type is consuming of information flow. Framework has another type of arrows such as mechanical connection, relative geometrical position, interaction with physical field, etc. So main idea of compatibility is translation of information flow lines to information consuming arrows. ## 3. OutlookOutllok of the idea will be considered by examples. Suppose that we have following Simulink scheme: This scheme is translated to following framework scheme: Arrows in second scheme have inverted direction and second scheme does not contain feedbak line. Feedback is implemented by another way. Let us compare elements of both schemes. The input element in first scheme is presented by following fragment of *.mdl file:
Corresponding element of framework has following properties: Here we have explicit formula parameters "Amplitude" and "Frequency" have been replaced by constants Negative term First window represents transformation function. Second window means that output of this block is send to ## 4. User interfacePresented above scheme is indeed invisible. We have special "Simulink" component with following properties: First tabpage enables us edit *.mdl file text. We also can download *.mdl file. Second tabpage represents Similink scheme. Third page enables us edit layout of public components. In result we have following component on desktop: ## 5. ImplementationImplementation is based on mapping between Simulink and Framework components. The Framework components are being stored in resources. Following picture presents resources which correspond to different Simulink blocks The Gain, Sin, Sum resources correspond to Simulink blocks with same types. The compilation procedure sets Framework components instead Simulink blocks. ## 6. CodeDom compilationCompatibility with Simulink can be reached by different ways. Here we will consider CodeDom compilation of Simulink files. CodeDom compilation had been already considered in my CodeProject article and arxiv article. Every Simulink file can be transformed to C# code. Let us provide an example of such transformation. Suppose that we have following Simulink scheme: This scheme corresponds to following code: using System; using System.Collections.Generic; using System.Text; using Simulink.CSharp.Library.Interfaces; using Simulink.CSharp.Library; namespace Calculation { public class Calculate : IStateCalculation { public void Update() { // First sum arrow_3 = +arrow_1 - arrow_2; // Mult arrow_4 = (System_0_const_K) * arrow_3; // Input of block 3 double block_3 = arrow_4 - ((1) * state[0]); derivation[0] = block_3; arrow_0 = +((5) * state[0]); double block_4 = arrow_0 - ((2) * state[1]); derivation[1] = block_4; arrow_2 = +((1) * state[1]); oldTime = time; } System.Double arrow_0; System.Double arrow_1; System.Double arrow_2; System.Double arrow_3; System.Double arrow_4; public double[] State { get { return state; } } public double[] Derivation { get { return derivation; } } double[] state = new double[2]; double[] derivation = new double[2]; double time; double oldTime; public double Time { get { return time; } set { time = value; } } double u = 0; double System_0_const_K = 0; public Dictionary<string, SetValue> Input { get { return input; } } Dictionary<string, SetValue> input; void SetBlock_0(object o) { arrow_1 = (System.Double)o; } public Dictionary<string, GetValue> Output { get { return output; } } Dictionary<string, GetValue> output; object GetBlock_0() { return arrow_2; } public Calculate() { input = new Dictionary<string, SetValue>(); input["Input"] = SetBlock_0; output = new Dictionary<string, GetValue>(); output["Scope"] = GetBlock_0; constants = new Dictionary<string, SetValue>(); constants["System_0_const_K"] = SetConst_0; } public void Reset() { arrow_0 = 0; arrow_1 = 0; arrow_2 = 0; arrow_3 = 0; arrow_4 = 0; } public Dictionary<string, SetValue> Constants { get { return constants; } } Dictionary<string, SetValue> constants; void SetConst_0(object o) { System_0_const_K = (double)o; } } } Let us comment this code. The code contains intrinsic calculation and external access members. ## 6.1 Intrinsic calculationFirst of all note that if blocks are connected by line than these blocks have common variables. For example the output variable of // First sum arrow_3 = +arrow_1 - arrow_2; // Mult arrow_4 = (System_0_const_K) * arrow_3; First operator corresponds to first Sum element of the scheme. The + and - signs correspond to List of Signs. This operator calculates ## 6.2 External accessExternal access is implemented by following interface. /// <summary> /// State calulation /// </summary> public interface IStateCalculation { /// <summary> /// Updates itself /// </summary> void Update(); /// <summary> /// Resets itself /// </summary> void Reset(); /// <summary> /// State /// </summary> double[] State { get; } /// <summary> /// Derivation /// </summary> double[] Derivation { get; } /// <summary> /// Input delegates /// </summary> Dictionary<string, SetValue> Input { get; } /// <summary> /// Output delegates /// </summary> Dictionary<string, GetValue> Output { get; } /// <summary> /// Constansts /// </summary> Dictionary<string, SetValue> Constants { get; } /// <summary> /// Time variable /// </summary> double Time { get; set; } } /// <summary> /// Sets value /// </summary> /// <param name="o">The value</param> public delegate void SetValue(object o); /// <summary> /// Gets valuye /// </summary> /// <returns>The value</returns> public delegate object GetValue(); The IStateCalculation calc = null; // Initialization of calc SetValue sv = calc.Input["Input"]; double a = 5; sv(a); In this sample we have set double value to input variable (the "Input" is variable name). Following code presents reading the value of the "Scope" output variable. GetValue gv = calc.Output["Scope"]; object o = gv(); It is worth to node that in general Simulink scheme is a system with internal state. The public double[] State { get { return state; } } public double[] Derivation { get { return derivation; } } double[] state = new double[2]; double[] derivation = new double[2]; The ## 7. ApplicationsEmulation of Simulink is not interesting as standalone. It is interesting as integrated to Framework. Such integration enable us use mechanics, field theory virtual reality and other useful features. ## 7.1 Identification of system parametersLet us consider once again following Simulink scheme: This scheme contains one constant. This constant is coefficient ofGain. We would like to define this constant. The Simulink scheme is included in following Framework scheme.
In this scheme the Simulink component is wrapper of Simulink scheme which uses runtime C# compilation. The icon means usage of Simulink wrapper with C# compilation. The Input component provides input for Simulink component. Properties of Input are presented below:
The t parameter of formula is time, other parameters are constants. So the Input provides harmonic signal. Properties of Simulink component are presented below:
These properties have following meaning. The Red curve corresponds to Let us explain meaning of components of this scheme. Meaning of This component accumulates signal. Step of accumulation is equal to 0.01. Number of steps is equal to 501. The This component calculates These properties have following meaning. Right and center pain mean that we would like approximate y - coordinates of ## 7.2 Interdisciplinary sample. Two channel follow system of radarTwo channel follow-up system of radar had been already considered in my article "Universal Framework for Science and Engineering - Part 3: Control systems. Processing of signals". This sample is more advanced and shows interdisciplinary character of the Framework. It includes following branches of science and engineering: - Spatial geometry;
- Field theory;
- Advanced mechanics;
- Control theory;
- Virtual reality.
## 7.2.1 Problem outlookOutlook of problem is schematically presented below: Radar antenna has two channel follow-up system with azimuth and elevation channels. Spatial geometry of this phenomenon is presented below: Input data is being obtained from signal reflection. Control system acts to radar. So radar follow up visibility line. Let us consider theoretical issues of the problem. ## 7.2.2 Theoretical issues## 7.2.2.1 Phase method of definintion of directionPhase method of definition of direction can be explained by following geometrical task. Suppose that we have equilateral spatial triangle A, B and C line L. Solution of this task is well known L is normal to triangle plane and intersects center of triangle. Similar method can be implemented if we use phase of signal instead distance. Now suppose that antenna have three inputs with different phases. Now suppose that radar antenna have three signals with different phases. Following-up system tries to equalize phases. It is clear that signal is not received in three geometric points. Physics of real radar is much more complicated. However in some cases three point model can be good approximation of physical phenomenon. We suppose in this sample there are three conventional points near antenna and signal contains three signals received from these points.
## 7.2.2.2 Advanced mechanicsIn general radar antenna could not be considered as mechanical absolutely rigid body. Therefore elastic oscillations of antenna will be considered here. The Framework contains aggregate designer with good support of elastic mechanical simulation. This aggregate designer is described in my previous article. Here I remind it. Why aggregate designer? Indeed mechanical equations are well known long time ago. But software development for simulation of complicated mechanical objects is not quite easy task. Aggregate designer make this task much easier. Let us consider mechanical model of spacecraft from models of its modules as example. Typical spacecraft module is schematically presented below: This module has own coordinates system OXYZ. Also it has places of connections. We can connect other modules to this module. Behavior of module is defined by following kinematic parameters: - Radius vector
*r*; - Velocity
*V*; - Orientation quaternion
*Q*; - Angular velocity .
But module is not rigid in general. And these parameters are not parameters of module. These parameters are rather parameters of one point of module. In this article we suppose that these parameters are parameters of origin of OXYX coordinat system. Since module is not rigid it has additional degress of freedom. These degrees of freedom can be interpreted as generalized coordinates i = 1,...,n). Instant state of module is defined by following parameters:I will call them state variables. Parameters: will be called accelerations. Mechanical equations define accelerations by state parameters. Accelerations near connection can be defined by following way:
where Both modules have equal acceleration near connection. First module acts to second one by force M. Similarly second module actcs to first one by force _{12}M and mechanical momentum _{21}F. Following equations:_{21}
F;_{21}
F; _{21}are well known. Mechanical equations of module can be represented by the following way:
In these expressions accelerations are independent variables. M) is force (mechanical momentum) of _{i}i- h connected module. Other vector and matrix parameters denend on state variables. Adding following evident expressions: results to linear by accelerations system of equations. This system enables us to find all accelerations. So we have mechanical equations. It is worth to note that this system is redundant. If /// <summary> /// Aggregable Mechanical Object /// </summary> public interface IAggregableMechanicalObject { /// <summary> /// Number of degrees of freedom /// </summary> int Dimension { get; } /// <summary> /// Number of connections /// </summary> int NumberOfConnections { get; } /// <summary> /// State of object /// </summary> double[] State { get; } /// <summary> /// Internal acceleration /// </summary> double[] InternalAcceleration { get; } /// <summary> /// State of connection /// x[0] - position, x[1] - quaternion, /// x[2] - linear velocity, x[3] - angular velocity /// </summary> /// <param name="numOfConnection">Number of connection</param> /// <returns>State of connection</returns> double[] this[int numOfConnection] { get; set; } /// <summary> /// Calculates transformation matrix from genrealized coordinates to /// acceleration of connection /// </summary> /// <param name="numOfConnection">Number of connection</param> /// <returns>The transformation matrix</returns> double[,] GetAccelerationMatrix(int numOfConnection); /// <summary> /// Gets matrix of forces /// </summary> /// <param name="numOfConnection">Number of connection</param> /// <returns>The matrix of forces</returns> double[,] GetForcesMatrix(int numOfConnection); /// <summary> /// Gets internal acceleration /// </summary> /// <param name="numOfConnection">Number of connection</param> /// <returns>Internal accceleration</returns> double[] GetInternalAcceleration(int numOfConnection); /// <summary> /// Gets connection force /// <summary> /// <param name="numOfConnection">Number of connection</param> /// <returns>Connection force</returns> double[] GetConnectionForce(int numOfConnection); /// <summary> /// Children objects /// </summary> Dictionary<IAggregableMechanicalObject, int[]> Children { get; } /// <summary> /// The is constant sign /// </summary> bool IsConstant { get; } /// <summary> /// Parent object /// </summary> IAggregableMechanicalObject Parent { get; set; } } I will consider samples of modules below. Here I describe this interface. Meaning of this interface members is presented in following table:
`IsConstant` flag. Following aggregate library contains a set of basic mechanical objects.
First library object represents rigid body mechanics. Equations of rigid body are well known and I will not present them here. Rigid body has variable number of connections. Its editor of properties is presented below: Number of connections their positions and orientations can be changed. Elastic console body is a mechanical system of infinite degrees of freedom. Usually math model of this object contains finite degrees of freedom with finite set of valuable harmonic oscillations. Every harmonic oscillation can be described by following second order system of ordinary differential equation:
Editor of properties of elastic body is presented below:
This editor enables us change number of harmonics and their numerical properties. Object of this type is used for simulation of antenna mechanics. ## 7.2.2.3 Declarative geometrical approachDirect or imperative approach in calculation uses explicit "expression" of phisical phenomenon such us "Evaluate following expression". This approach contains all disadvantages of imperative approach. Here we will use declarative approach which has following advantages: - Laconism;
- Good interoperability with other phenomena.
Let us provide declarative description. There are moved object, physical field of moved object which is naturally geometrically linked to moved object. There are elastic antenna which interacts with physical field. Interaction depends on relative position of moved object and antenna. However declarative approach enables us implicitly take to account this dependence. Engineer has a lot of problems which are besides explicit description of geometrical dependence on geometrical position. This task is also used in virtual reality. Virtual reality also implicitly takes to account geometrical positions. ## 7.2.2.4 Control theoryTypical control scheme of following system is PID control. Differentiation provides stability. Integration provides astatism. Simulink is used for simulation of PID controller. There are a lot of control laws implemented in Simulink. Now these control laws can be exported to the Framework.
## 7.2.3 Implementation## 7.2.3.1 Moved objectMoved object implemented by following diagram: The The The above picture has following meaning. Time dependencies ## 7.2.3.2 Physical field of moved objectSimulation of physical field is presented on following picture: Main object is Here These properties have following meaning. Relative coordinates ## 7.2.3.3 Model of receiverWe will use three point approximation model which is described in 7.2.2.1. This model is presented below: The model contains three virtual sensors Checked boxes correspond to following objects: **Coordinates;****Base;****S 1;****S 2;****S 3.**
The ## 7.2.3.4 Interaction with fieldModel of interaction with field is presented below: For interaction we simply connect virtual sensors ## 7.2.3.5 Mechanical model of antennaMechanical model of antenna is elastic body with 6 elastic harmonics. Properties of antenna are presened below: Atnenna is forced by mechanical momentum of control system. This momentum will be considered below. ## 7.2.3.6 Interaction of mechanics and receiver propertiesWe have noted that positions of conventional points depend on elastic oscillations. This dependence can be expressed by following way: where is shift of any coordinate of any conventional point, k - coefficients. Set of cooefficients (as whole) depends on number of conditional point and coordinate name (_{i}x,y,z). Implementation of this depenence is presented in following picture:
Let us explain meaning of comopnents. One of output parameter of Right part of picture means that The This feedback changes "constants" of ## 7.2.3.7 Follow-up algorithmOur system has two channels: - Elevation
- Azimuth
Antenna has three phase signals. We would like make two deviation signals from three phase signals. The geometry of signals is presented in following picture: Here The Right pane means that First picture represents logics of *.mdl file. Second picture represents constants and input variable. Here input variable is This picture has following meaning. Mechanical momentums M are equal to outputs of _{y}Channel 1 and Channel 2 respectively. Full simulation picture is presented below:Typical transition processes are presented below: First chart contains deviations and second chart components of angular velocity of antenna. This sample requires additional files. ## 7.2.3.8 Virtual realityWe have considered physical phenomena of two channel follow-up system. Now this physics will be used for virtual reality. We would like to present view of moved object though virtual videocamera which is installed on antenna. First of all we provide proper orientation of camera. The visibility axis of camera should be collinear to visibility axis to antenna. However visibility axis of antenna do not always canonically coincides with any mechanical axis of antenna. Additional frame will be introduced to provide proper orientation of camera. This frame is presented in following picture The frame is named This object has three public objects: - Base reference frame;
- Reference frame of moved object;
- Reference frame of installed camera installed on antenna.
Now we will construct following situation:
This diagram has following objects:
This sample requires additional files and "Aviation + Simulink + OpenGL" version of the framework. - Download additional files - 333.84 KB
- Download Aviation + Simulink + OpenGL project source code - 2.47 MB
- Download follow-up + virtual reality sample - 81.58 KB
To try this sample you need set parameters (Start time, Start step, Step, Step count and Pause) of virtial reality as it is presented in following picture Then the Start button should be clicked. More suitable values of these parameters are presented on following table:
## 7.2.4 WPF version of 3D visualizationThe framework had been founded in 2002. First virtual reality framework had been based on OpenGL. During writing of this article WPF visualization was developed. So it is reasonably supply this article with WPF visualization source code. You can download it. You can also download above situation. - Download Aviation_+ Simulink +_WPF project source code - 2.45 MB
- Download additional files for WPF version - 135.55 KB
- Download follow_up + virtual reality sample (WPF version) - 467.39 KB
WPF 3D performance substationally exceeds OpenGL one. So we need set foolowing parameters of animation:
## 7.2.5 ConclusionThis sample shows advantages of declarative approach. If we stated (relative) geometrical positions then we have no concern about lot of other details. Action of physical fields automatically takes to account positions and orientations. Control signal depends on relative orientations and positions of objects. Otherwise relative orientations and positions depend on control signal. Later motion simulation is used in virtual reality. Both virtual reality and simulation of physical phenomenon use reference frames. Virtual reality simulation becomes installation of virtual cameras and 3D models on reference frames provided by simulation of physical phenomena. ## 7.3 Stateflows. Automatic transmission controlSimulink supports stateflows. Now importance of stateflows in IT is evident. Moreover Stateflows are suppurted by some programming languages (C# for example). I think that best way of compatibility with Simulink stateflows is usage of Windows Workflow Foundation. Let us consider following example. Automatic transmission control is typical workflow problem. Full Simulink diagram of automatic transmission system is presened below: This scheme contains a catalogue of subschemes. The Framework wrapper of Simulink represents this catalogue by following way Left pane represents catalogue structure. Right pane represents subschemes. Logics of automation transmission control is described here. This logics contains state machines which are presented on following scheme: It is clear that following Windows Workflow Foundation diagrams represent in fact same state machines. So the natural way of compatibility of Simulink state machines and framework state machines is translation Simulink stateflow to Windows Workflow Foundation. ## WAIT## Points of interestsCompatibility with Simulink will provide a lot of prospects. Many engineering developments could be imported. ## HistoryI have decided to provide compatibility at November 2008. But I did not know good way of this idea. At June 2009 I have found a good way of idea implementation. During 10 days I have developed first version. History is not finished. This article is rather announcement. Many *.mdl files cannot be yet imported. This article and code will be extended. <input type="hidden" id="gwProxy" /><input type="hidden" id="jsProxy" önclick="jsCall();" /> |