Modelica Assignment
|
Practical Information
GoalsIn this assignment, you will use the Modelica Language to first create a model of a gantry system and then create a controller for optimal control of the system. You will learn:
OpenModelicaFor the tutorial on TBD, you should make sure you have OMEdit installed in your laptops. You should also have downloaded this zip-file which contains an example model of cooling of an object based on Newton's law of cooling and corresponding Python code. You will use OpenModelica to perform all the tasks. You can install the latest stable version of OpenModelica on your systems by following the directions by going to the OpenModelica website > Download > Windows/Linux/Mac (based on your OS). Choose the 'Official Release' package. Installing OpenModelica installs a number of applications in your system: OMEditThe OpenModelica Connection Editor (OMEdit) is the standard graphical user interface that is used for graphical Modelica model creation and editing. We will use OMEdit for all the tasks. Launch instructions and other features of OMEdit are available on the OMEdit website. OMEdit has 4 different views but we are only interested in the Modelling and Plotting views. We will primarily use OMEdit to design the model. Information about the other OpenModelica applications are just given for information. Modelica Standard LibraryA major advantage of using Modelica is the availability of a wide-variety of Modelica libraries, making it easier to re-use blocks and classes from these libraries. For the assignment, you should ensure, once you have installed OpenModelica, that the Modelica Standard Library (MSL) package is also correctly downloaded, and automatically loaded when you start OMEdit. To do so, go to File > Manage Libraries > Install Library > for Name select Modelica with Version 4.0.0 > OK. The Modelica Standard Library should be visible when you restart OMEdit. OMNotebookOMNotebook is a notebook-like tool to learn the usage of OpenModelica and construct more-readable documentation which incorporates code-windows in between text. Think of Jupyter Notebooks. OMShellOMShell is an interactive session handler that parses and interprets commands and Modelica expressions for evaluation, simulation, plotting, etc. The session handler also contains simple history facilities, and completion of file names and certain identifiers in commands. We will not use OMShell because we want to learn and visualize graphically the models that we are building. Hence we will only use OMEdit. We use Python because we can then directly use the data obtained from simulation for analysis through a number of data analysis packages available in Python. One thing to note is that a Modelica model needs to be compiled only once. Once compiled, the parameters can be changed in subsequent executions/simulation runs without having to compile the model again. This is advantageous since compilation can prove to be a significant overhead if we are optimising for a number of different parameters and ranges (read: the number of simulation runs could go into the thousands or more, as I'm doing in my research, but that is the beauty of it). BasicsWhile the Modelica language and its capabilities of object-oriented acausal modeling of the multi-physics have already been described in the lectures, what follows is a 'how-to' to get started building your own Modelica models for the assignment using OMEdit. On the left of the OMEdit window, is the Libraries pane which displays all the libraries that have been loaded in the current Modelica session. Modelica models are organised in Packages which can contain other packages and Modelica classes. Create a Modelica PackageFigure 1: Example package creation. Go to File > New > New Modelica Class. In the Create New Modelica Class dialog box, enter a valid package name for example, example_package. In Specialization, select Package. This is also shown in Fig.1. Click OK. You should now see your Modelica package in the Libraries class. You should save it before proceeding further by clicking on the corresponding icon in the menu bar or going to File > Save. If you have already created and saved a package, you can load it in the current session by going to File > Open Model/Library Files, and choosing the corresponding file in the browsing window. Create your first Modelica ModelRight click on your Modelica package in the Libraries window > in the drop-down menu select New Modelica Class. In the create New Modelica Class dialog box, enter a valid class name, for example, example_model. In Specialization, select Model. Make sure that for Insert in class, the textbox specifies the Modelica package within which you want to create your Modelica model. Click OK. You have created your first Modelica model! You can edit the model from within the package or by opening the model. Ideally you should edit the model directly, since then you can visualize it if in the form of blocks and connectors. You cannot visualise a package since it just contains other modelica classes with no physical connections between them. The modelica classes are just organised that way since they may be related. For example, the Modelica package contains sub-packages like Electrical (for electrical components), etc. Modelica classes can extend other Modelica classes, i.e. inherit the declarations from the extended class. They may also simply re-use components defined in other Modelica classes. Build/Edit your Modelica ModelDouble click on the Modelica model you want to edit. This takes you to the Modeling view. In the top right corner of the Modeling view are 3 buttons to toggle between different editing interfaces. The 2nd button is to open the graphical editor and the 3rd button is to open the textual editor. Figure 2: Buttons to change editor views. The Icon view is only used to arrange connector points for a block. The graphical editor can only be used in case the model consists of blocks with causal/acausal connections between them. For the 1st and 2nd tasks of the assignment, you will not use the graphical editor, since we are just creating a model based on Ordinary Differential Equations which have no graphical concrete syntax (within OpenModelica at least). In the text editing pane, you can now define your modelica model in-between the model and its corresponding end declarations. Look at the Modelica By Example website for simple introductory examples of Modelica models based on simple Ordinary Differential Equations of physics. If you are making a component-based model. You can add components to the model by dragging them from the Libraries pane onto the graphical editor. You will be prompted to give the component a unique name. You can draw connections between the interfaces of components by holding the left mouse button while simultaneously moving it from one interface ot the other. This highly intuitive. You can modify parameters of the components by double-clicking on them, which will open the Element Parameters box. Simulate your Modelica Model in OMEditFigure 3: 'Check Model' and 'Simulation' buttons in OMEdit. Once you have defined a complete and correct Modelica model, you can now simulate it. To do so, you should click on the S icon in the menu bar. This opens the Simulation Setup dialog box. You can set simulation parameters in this dialog box. Make sure that you have verified the simulation start time, stop time, step-size/number of intervals, solver method (DASSL is preferred), and tolerance (the default 1e-6 is low enough). You can also set the output format to output the data to a file. However, we do not need to do so, since we will later perform simulations through Python where we will handle the simulation output directly. This is just for visualization and initial validation of the model. Click OK to begin simulation. Note: You can also 'check' the model by clicking on the tick-mark icon in the menu bar. The output from this can be observed in the message browser. The message browser will tell you if something is missing in the model, or if there are inconsistencies in the model. Visualize simulation traceClicking OK in the Simulation Setup ideally takes you to the Plotting view. If not, you can go to the plotting view through the corresponding tab in the bottom-right corner. On the right you will see the variable browser. You can select any variable/s to plot them in the pane and observe their values over the simulation. The values indicated in the variables pane is the value of that variable at the end of the simulation. Parametrizing simulation quantities in OMEditTo define a parameter you simply have to use the 'parameter' keyword followed by its type, name and default value in this syntax: parameter <type> <name> = <default value> ;If you want to parametrize a quantity which is a parameter of a block in your model. Simply double click on the block in the graphical editor and change the parameter value to the parameter name. This value need not be a numeric quantity but it can be a reference to the name of the parameter within the model, or a function of the parameter as well. Once you hve changed the value in the block, go to the textual editor of your model (not of the block!), and use the same syntax as above to define a parameter. Note that the name of this parameter should correspond to the name of the parameter you have defined in the value of the block. Look at the following image for an example: Figure 4: Parametrizing the values of the block-parameters of the blocks in your model. Multiple simulations and analyses of your Modelica modelThe example Python file contained in the zip file describes how to use Python to simulate and visualize data from an example Modelica model which will be introduced in the tutorial as well. We will perform multiple simulations by generating the executable through OMEdit. You should make sure that the executable is generated and not deleted in the right directory by:
To generate these files, once the settings mentioned above are correctly configured, you should simulate the model in OMEdit at least once. To re-simulate your model, but not from OMEdit, you can simply use the terminal command to execute the executable which is in a sub-directory of the working directory as described above. This sub-directory is named 'package_name.model_name' and the executable in this sub-directory is executed with the command './model_name'. For the example, it will be './NewtonCooling'. Note that you should change the current directory of the terminal session to the example/NewtonCooling/ directory before doing so. This cannot be done from within the example/ directory. The simulation results will be stored in the same directory as the executable. By default it is a MAT-file, and you should try to use the same because it is the most efficient (compared to CSV, etc.). At the end of each simulation you should read the MAT-file, and record the variables that you are interested in before performing the next simulation, because this file will be overwritten during re-simulation. You can read the MAT-files generated by OpenModelica using the read_mat_file() function in the example code! To re-simulate the model, but with different parameters, you can use command line as well. You simply have to structure your command using the -override flag, as the following './model_name -override parameter1=value, parameter2=value ..'.This is also described and demonstrated in the example code. To implement a loop over the parameter values, you will have to write a Python script. You can execute shell commands using the os package as demonstrated in the example. Take special note that os.changeDir function is used to change the working directory of the virtual shell to the results directory. Assignment Overview
This assignment consists of four main parts:
Each part is aligned with one of the learning goals Problem StatementA gantry system is an indispensable element of an efficient sea-port. It is used to move containers on and off docked ships. When a ship docks at the port, the following steps happen in order:
Have a look at this video. It vividly demonstrates the steps above performed by gantry cranes to completely automate the loading/unloading of ships docked at the Port of Singapore.
Traditionally, these gantry systems are operated by skilled technicians with years of practice. However, as you may have seen in every other domain, there is a push to automate the operation of gantry cranes supposedly to increase efficiency compared to human labor. As can be deduced from the list of steps above, the operation of a gantry crane involves many steps, each of which has complexities of its own. In this assignment, we will only be focusing on step 5, which is, the movement of the trolley on its tracks (remember that this is not the same as the tracks for the whole gantry system!). In essence, we would like to design a suitable controller such that the position of the trolley is moved based on a desired set-point; movement of the trolley is only possible in one direction, i.e. perpendicular to the ship (if viewed from above). The complexity in this step comes from the fact that the trolley is loaded with the suspended container, giving the container a pendulum-like motion when the trolley is moved. For obvious reasons of safety and efficiency, it is desirable that the container/pendulum sways as little as possible. This means we need to design a controller that controls the movement of the trolley, while minimizing the angle of sway of the container. The obvious method is to move the trolley infinitesimally slowly, but this is not efficient w.r.t time required to execute the operation. Plant Model CreationFig.7 describes the mechanics of the trolley-pendulum system and the associated forces. We will consider that the length of the pendulum rope is fixed and not variable. We will consider damping in the movement of the trolley on the tracks, and also damping in the swinging of the pendulum (primarily due to friction). As described earlier, we are interested in the position of the trolley $x(t)$, and the angle of the pendulum $\theta(t)$ at any given time t. The control signal applied to move the trolley is $u(t)$ .Figure 7: Illustration of simplified kinematics of trolley and pendulum system. To obtain the equations of motion in the forms of ODEs that can be used by Modelica to numerically solve the positions of the trolley and pendulum, we need to compute the Lagrangian of this kinematic system, and use it to derive the ODEs. Fear not! The task has already been performed by our colleague Joost Mertens from the CoSys Lab (where they have built a scaled model of the gantry system that we are studying in this assignment), and they have provided us with the solved ODEs that can be plugged into Modelica. They are: $$ \frac{d}{dt} x(t) = v(t) $$ $$ \frac{d}{dt} \theta(t) = w(t) $$ $$ \frac{d}{dt} v(t) = \frac{ r \cdot \left(d_{c} \cdot v{\left(t \right)} - m \cdot \left(g \cdot \sin{\left(\theta{\left(t \right)} \right)}\cdot \cos{\left(\theta{\left(t \right)} \right)} + r \cdot \sin{\left(\theta{\left(t \right)} \right)}\cdot\left(w{\left(t \right)}\right)^{2}\right) - u{\left(t \right)}\right) - {\left( d_{p} \cdot \cos{\left(\theta{\left(t \right)} \right)} \cdot w{\left(t \right)}\right)}}{- r \cdot \left(M + m \cdot \sin^{2}{\left(\theta{\left(t \right)} \right)}\right)} $$ $$ \frac{d}{d t} w{\left(t \right)} = \frac{{\left(d_{p} \cdot w{\left(t \right)} \cdot {\left(m + M \right)}\right)} + {\left(m^{2} \cdot r^{2} \cdot \sin{\left(\theta{\left(t \right)} \right)} \cdot \cos{\left(\theta{\left(t \right)} \right)} \left(w{\left(t \right)}\right)^{2}\right)} + m \cdot r \cdot \left({\left(g \cdot \sin{\left(\theta{\left(t \right)} \right)} \cdot {\left(m + M \right)}\right)} + {\left(\cos{\left(\theta{\left(t \right)} \right)} \cdot {\left(u{\left(t \right)} - d_{c}\cdot v{\left(t \right)} \right)}\right)}\right)}{(m \cdot r^2)\cdot {\left(- M - {\left( m \cdot \sin^{2}{\left(\theta{\left(t \right)} \right)}\right)}\right)}} $$ Where:
TasksYou are the engineer and the designer/scientist/client has given you the information above. Your task is to model the gantry system using the information given above, in Modelica. Special care should be taken to parametrize the model correctly.
BEWARE! Often customer requirements will be in multiple units as is done above (for example metres and kilometres). It is your task to unify and express them all in a single unitary system so that your solution is numerically correct. The recommended way is to use SI units in all cases. BONUS: for explicitly specifying the units of all the variables and parameters in your Modelica model by reusing definitions from the MSL. BONUS: for explicitly describing each parameter and variable with comments in your Modelica code. Note: There are many assumptions in our model, for example, the rigidity of the rope, the point-estimation of masses, drag friction, etc.. Plant Model CalibrationYou have successfully created an abstract model of the gantry, but how will you decide the values of the parameters so that your model truly represents reality? This is done through parameter calibration. The idea is that, we try (combinations of) different parameters, and compare the output of the simulation of the Modelica model, with real-life gantry data. The parameters, which give the least error between the two datas are said to be the best fit, and are chosen accordingly. An exhaustive way of finding optimal parameter values (note that in each experiment, there is one parameter) is to loop over possible values for the parameters to find the one that gives the closest match mentioned above. This is known as a parameter sweep. Because we designed the gantry, we already know that:
Hence, we need to find the damping coefficients $d_p$ and $d_c$. In our case, we can simplify the estimation of these parameters, by performing two different experiments, where the effect associated with one of the coefficients are nullified and hence the parameters can be estimated independently. Experiment 1In the first experiment, you will 'lock' the pendulum. In real life, this would be performed by setting $r=0$ and $m=0$ by 'reeling in' the pendulum rope with no container attached to it.
Experiment 2In the second experiment, you will 'lock' the trolley's movement. This is also how it will be performed in real-life:
TasksThe main purpose of this part of the assignment is finding the damping coefficients $d_p$ and $d_c$ of the gantry system, based on experimental data (i.e., observations/measurements on the real system). You will have to repeat the following steps for each parameter that needs to be estimated.
BEWARE! The csv data is at a time-step of 0.004 seconds, with start time at 0 seconds and stop time at 20 seconds (5001 data-points). Controller Model CreationA PID (Proportional-Integral-Derivative) controller is widely used in practice to drive the output of the plant towards a set-point. This set-point could change in time. We will however, consider the set-point is constant. In our example, the set-point represents the desired lateral position of the gantry trolley/cart. The control output from the PID controller, i.e. $u(t)$ drives the plant / process. The plant produces an output i.e. $x(t)$. The difference between the measured output $x(t)$ and the desired output $r(t)$ (a.k.a the set-point) is called the error $e(t)$. This error value is processed in the PID controller in three different ways as shown in the figure: Figure 8: PID controller (from Wikipedia).
Hence, the output from the PID controller $u(t)$ is given by: $$u(t) = (K_p \cdot e(t)) + (K_i \cdot \int_0^t e(t) dt) + (K_d \cdot \dfrac{d e(t)}{dt}) $$The gains $K_p$, $K_i$, and $K_d$ modulate the behavior of the PID controller. You may read more about a PID controller online. The essential point is that three signals are summed, which forms the PID control output $u(t)$. We will also be exploring how the three quantities affect the behavior of the PID control through the tasks. So, to sum up, in our case:
Hence the equation for implementing the control is: $$ e(t) = r - x(t) $$ coupled with the equation above.Note: In our simple case, the set-point is constant; we only want to test the trolley's movement from point a to point b. However, realistically, the set-point changes with time, often discontinuously. TasksYou modeled equations using a textual language in the first task. In this task, you will model equations using block diagram representations in Modelica. Sub-task 1: Creating a Block from the plant model
Note: The above is a description of using your gantry plant model as a block, but the same principles apply for any other plant model that you have defined textually and want to use as a causal block. There are different kinds of connectors available in the MSL for causal and a-causal quantities. One can add as many meaningful interfaces as they like. Note: The 'extension' of Icons.Block is only a cosmetic change about the appearance of the block in the next sub-task, and is not functional in any way. Sub-task 2: Creating the PID controller block
Note: You have not yet created the whole PID control loop yet! That is the next step. Sub-task 3: Creating the PID control loop
Note: Students often forget to include the analysis of varying the three control parameters in their report. Avoid making the same mistake! Examples on how to compose blocks in Modelica is described on this site. These example blocks are available in the Modelica library. Note: Do not model any part of the control loop using textual equations. In this part of the assignment you should use the graphical view to model your equations. Only use the textual syntax to define the parameters and interfaces of your model. Controller Model TuningSimilar to the calibration of the plant, the controller also needs to be tuned for the most ideal solution. However, we do not directly have reference data to compare the simulation output to. Instead, the designer (you) should come up with a cost function to ascertain if the plant behaves as required. TasksSub-task 1: Design of the cost function
Sub-task 2: Perform simulations and calculate costs
Congratulations! You have successfully modeled a physical system and a controller for it in Modelica! Note: This is a very naive controller not fit for use in production. Look at this video. It demonstrates an automated gantry crane. Notice the irregular movement of the trolley, and how the gantry moves the container in 2 'swoops' ensuring maximum stability while being efficient. Modern controller are very complex, with features like state-estimation that makes the controller aware of the physics of the system at a more fine-grained level. Practical Issues
|
Maintained by Hans Vangheluwe. | Last Modified: 2023/11/03 17:30:23. |