Unity oyun motoru ile simülatör tasarımı
Simulator design with Unity game engine
Yazar: CİHAD DOĞAN
Danışman: PROF. DR. AFİFE LEYLA GÖREN
İstanbul Teknik Üniversitesi / Fen Bilimleri Enstitüsü / Kontrol ve Otomasyon Mühendisliği Ana Bilim Dalı / Kontrol ve Otomasyon Mühendisliği Bilim Dalı
Derece: Yüksek Lisans, 2019
Konu: Bilgisayar Mühendisliği Bilimleri-Bilgisayar ve Kontrol = Computer Engineering and Computer Science and Control ; Elektrik ve Elektronik Mühendisliği = Electrical and Electronics Engineering

Özet:
Günümüzde bilgisayar simülasyonları birçok mühendislik projesinin yanında ekonomi, biyoloji, psikoloji ve tıp gibi alanlarda da aktif olarak kullanılmaktadır. Geliştirilen bilgisayar simülatörleri sayesinde birçok deney, test ve eğitim ucuz, hızlı ve herhangi bir insan hayatını tehlikeye atmadan gerçekleştirilmektedir. Bilgisayar simülatörleri çeşitli oyun motorları kullanılarak veya çok daha düşük seviye olan grafik ve fizik kütüphaneleri kullanılarak geliştirilebilir. Bu tez çalışmasında kontrol ve otomasyon mühendisliği eğitiminde kullanılan çeşitli deney setlerinin Unity oyun motoru kullanılarak simülatörlerinin oluşturulması amaçlanmıştır. Unity oyun motoru Unity Technologies firması tarafından geliştirilen bilgisayar, konsollar ve mobil platformlar için oyun ve simülatör geliştirilmesini sağlayan çapraz bir oyun motorudur. Unity oyun motoru kendi web adresinden  ücretsiz olarak edinilebilir. Yazılım dili olarak C# kullanan motor Windows ortamında çalışabilmektedir. 
Çalışmanın ilk aşamasında Unity oyun motorunun yapısı ve kullanımı incelenmiştir. Motorun içinde kullanılan fizik, grafik gibi bileşenler tek tek incelenerek motorun çalışma mantığı öğrenilmiştir. Daha sonra oyun motorunun gerçek dünyadaki fizik kanunlarına olan yakınlığını ölçmek için çeşitli fizik deneyleri yapılmıştır. Bu deneyler ile yer çekimi, eylemsizlik, Newton kanunları ve sürtünme gibi en temel fizik kanunlarının Unity oyun motorunda doğru şekilde uygulandığı görülmüştür. 
Çalışmanın ikinci aşamasında geliştirilecek olan simülatörlerin Simulink programından kontrolünün sağlanması amaçlanmıştır. Bu amaçla TCP ve UDP haberleşme protokolleri irdelenmiştir. Bu karşılaştırmanın ve yapılan testlerin sonucunda daha hafif ve daha hızlı olması sebebiyle  UDP protokolünün kullanılmasına karar verilmiştir. Bu aşamanın sonunda hem Simulink hemde Unity oyun motoru tarafına gerekli yazılımlar yazılarak geliştirilecek olan simülatörlerin UDP protokolü ile Simulink ortamından kontrolleri sağlanmıştır. Yazılan yazılım çeşitli hız testlerine tabi tutulmuş ve isterlerin karşılandığına emin olunmuştur.  
Çalışmanın üçüncü ve son aşamasında ise top ve plaka sistemi, ters sarkaç sistemi , mobil robot sistemi ve örnek bir vinç sisteminin Unity oyun motoru kullanılarak simülatörü tasarlanması amaçlanmıştır. Tüm simülatörlerin tasarımında benzer yazılım mimarisi, grafik arayüzü ve test yöntemleri kullanılmıştır. Öncelikle sistemlerin gerçek deney setleri incelenmiş ve sistemler alt mekanik bileşenlerine ayrılmıştır. Bu alt bileşenler tek tek Unity oyun motorunda gerçeklenerek sistemlerin bilgisayar simülatörü oluşturulmuştur. Daha sonra sistemlerin doğrusal olmayan modelleri  Euler-Lagrange yöntemiyle elde edilmiştir. Elde edilen doğrusal olmayan modelin Simulink programında simülasyon modeli oluşturulmuştur. Son olarak doğrusal olmayan model ve geliştirilen simülatörlerin aynı sistem girişleri için sistem çıkışları karşılaştırılmıştır. Bu karşılaştırmalar sonucunda geliştirilen simülatörlerin gerçekçiliği irdelenmiş ve çeşitli yorumlar yapılmıştır.

Abstract:
Nowadays, computer simulations are actively used in many engineering projects as well as in economics, biology, psychology and medicine. Thanks to computer simulators, many experiments, tests and training are carried out cheaply, quickly and without endangering any human life. Computer simulations can be developed using game engines or directly using low level graphics and physics libraries. This thesis study is intended to create simulators using Unity game engine of various experiment sets used in control and automation engineering training. 
Unity is a cross-platform game engine released in June 2005 by Unity Technologies to develop video games and simulations for PCs, consoles, mobile devices. Unity enables the development of both 2D and 3D video games and simulations with the libraries and tools it contains. It is used in many game and simulation projects thanks to its easy usage and cross-platform structure. Various examples of simulators developed with Unity can be found in [2], [3] and [4]. Unity uses the high level Microsoft C # language as script language. As a graphics engine, Unity uses the DirectX library developed by Microsoft for Windows and Xbox platforms, and OpenGL open source graphics library for mobile devices. Nvidia Physx is used as a physics engine. Unity can be downloaded for free from its Internet address and can be used with Windows, MacOS and Linux operating systems.
In the second stage of the study, the main graphics and phsyics libraries of Unity game engine has been examined. Physx is a physics engine that was developed in 2004 by the company NovodeX at the ETH Zurich University. The physics engine was purchased in 2008 by NVIDIA, one of the leading graphic processor companies. It is currently being developed as an open source under NVIDIA. The most current version is Physx 4.0, which was released in December 2018, but is currently using version 3.4.1 in the Unity game engine. DirectX is a collection of APIs released by Microsoft in 1994 for multimedia processing on their platforms. DirectX also includes Direct2D, Direct3D and DirectWrite. Through these libraries, complex vector operations, 2D/3D dimensional graphics operations can be performed and computer graphics are obtained as a result of these operations. The most recent version is DirectX 12 released in October 2018. In the Unity game engine DirectX 12 or DirectX 11 can be used. OpenGL (Open Graphics Library) is an open-source graphics library published in 1992. OenGL is supported by different hardware manufacturers and platforms like Windows, Linux, MacOS. The most current version of OpenGL is released on 4.6 July 2017. In the Unity game engine, any version from OpenGL 3.5 to OpenGL 4.5 can be used.
In the third stage, it's aimed that developing a method to provide control of simulators that developed in Unity from. Simulink is a model-based design environment that frequently used in the education and application of many engineering disciplines. Because of this intense usage, an intermediate communication software has been written to control the simulation in Unity environment by Simulink. For this purpose, the TCP and UDP communication protocols are examined. TCP is a communication protocol thar created to send data in advanced computer networks without loss. It is ensured that the data sent in the TCP protocol arrives client at the other end. With this feature, TCP protocol is preferred for systems that need to communicate with high accuracy and reliability. UDP is a protocol that created for fast and efficient data communication in computer networks. There is no flow or error control in the UDP protocol. Therefore, it is not guaranteed that the sent data will reach the other end. However, in addition to this disadvantage, the UDP protocol can be implemented and operated faster than the TCP protocol. As a result of this comparison it was decided to use the UDP protocol which lighter and faster. At the end of this stage necessary softwares are written on Simulink and Unity to provide a communication and control between Simulink and Unity. with UDP protocol. Communication software has been subjected to various speed tests and it has been assured that the requirements have been met. 
In the fourth and final stage, developing computer simulators for different dynamical systems is aimed. For this purpose Ball and Plate system is selected first. Ball and Plate simulator system is developed. Ball and Plate system is frequently used system in control research. The main purpose of this system is to control the position of the ball that placed on an angularly movable plate or to follow a desired path on the plate. Because, it is used frequently in control applications, this system is implemented in physics engine and it is aimed to compare the performance of physics engine with real world physics and create an experiment environment. At first, system is divided into sub-mechanical components. Than these sub-components are created Unity game engine one by one to create complete system in engine. For testing accuracy of simulator, nonlinear models of the systems are obtained by Euler-Lagrange method. Simulation of nonlinear model mathematical model is created in Simulin. The system outputs for system same inputs are compared. As a result of these comparisons, the realism of the simulators is discussed and various interpretations were made. Inverse pendulum system was developed as second simulator system.  The inverse pendulum system is one of the most classical control problem. The main purpose of this system is to control the angle of the pendulum connected to a vehicle moving linearly in a single axis.Like ball and plate system,  inverse pendulum system is also selected because of it is wide in control applications. Similar methodologies are used for the developing and testing simulator of inverse pendulum system with ball and plate system. As a result of tests and comparions, the realism of the inverse pendulum simulator is discussed and various interpretations were made.A gantry crane system is developed as third simulator. Cranes are defined as construction machines used for lifting and transporting large and heavy objects where manpower is not sufficient. Generally speaking, although the basic mechanics of the cranes are the same, the structures may vary according to the areas where they are used. In this section, a simulator and experimental set of a traveling bridge crane system has been developed in Unity environment. Similar methodologies are used for the developing and testing simulator of gantry crane simulator system with ball and plate and inverse pendulum systems. As a result of tests and comparions, the realism of the simulator is discussed and various interpretations were made. Lastly a mobile robot simulator is developed. Mobile robots are used in many robotics and control projects. Especially autonomous mobile robots moving as swarm is one of most common topic nowadays. In this simulator, it is aimed to create a simulator environment in which one or more mobile robots are controlled.
In this study, 4 different simulators of 4 different systems were developed by using Unity game engine. In order to use these simulators with other tools and programs like Simulink, a communication interface software have been written with UDP communication protocol. Nonlinear model of each system is obtained and simulations of these models are created in Simulink program. Then, for same system inputs, the outputs of nonlinear models and simulators were compared. As a result of these comparisons, it was investigated whether the simulators act in accordance with the phsyic laws in the real world. Simulators and nonlinear model simulations were found to give similar outputs for same inputs.So we can say that simulators In future, the first thing to do is to test the simulators with different system values.aAter obtaining sufficient data, the simulators should be tested with the actual models of the systems.In future studies, simulators of more complex systems like cars, drones, trains,and aircrafts can be developed in Unity game engine. Simulators of such systems can be made. In this way, it is possible to use these systems which are expensive and dangerous in real world experiment sets in academic studies.