Exploration of Airsim Using C and Rust in the Context of Safetycritical Systems
Total Page:16
File Type:pdf, Size:1020Kb
DEGREE PROJECT IN COMPUTER ENGINEERING, FIRST CYCLE, 15 CREDITS STOCKHOLM, SWEDEN 2018 Exploration of AirSim using C and Rust in the Context of SafetyCritical Systems DANIEL AROS BANDA JOEL WACHSLER KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE Exploration of AirSim using C and Rust in the context of Safety-Critical Systems JOEL WACHSLER DANIEL AROS BANDA Bachelor in Computer Science Date: June 20, 2018 Supervisor: George Ungureanu Examiner: Ingo Sander Swedish title: Utforskning av AirSim med hjälp av C och Rust inom ramen för Säkerhetskritiska System School of Electrical Engineering and Computer Science iii Abstract AirSim is a new simulator developed as a plugin for the Unreal En- gine, aiming to be a useful tool aiding the development and testing of algorithms for autonomous vehicles. Due to AirSim still being in its infancy there is little to none research available of its possibilities or detailed guidelines and tutorials on how to use its APIs. Rust is a new systems programming language with the purpose of being safe, practical and concurrent which through design choices can solve some of the major drawbacks of the C programming language such as memory leaks, thread management, and segmentation faults. Researching the features of AirSim and its different ways of communi- cating, we determine the possibility of implementing a custom flight controller in Rust and C able to control a drone in the simulator and evaluate the capabilities of Rust compared to C. This is conducted by reading available documentation for AirSim, studying the source code and learning about the communication protocols used by AirSim. This thesis results in an implementation of a custom flight controller in Rust and C that controls a drone in AirSim using a communication protocol named MAVLink which enables fine-grained control of the motors. The conclusion made about the comparison of Rust and C is that both languages were able to implement the safety-critical func- tionality of the flight controller and that Rust provided capabilities which could be useful when developing safety-critical systems. Keywords AirSim, Simulation, C, Rust, Safety-Critical Systems, Flight Controller, MAVLink iv Sammanfattning AirSim är en ny simulator utvecklad som ett plugin för Unreal Engi- ne, med målet att fungera som ett hjälpmedel inom utveckling och testning av algoritmer för autonoma fordon. På grund av att AirSim fortfarande är väldigt ungt finns väldigt lite forskning tillgänglig om dess möjligheter eller detaljerade riktlinjer och beskrivningar för an- vändningen av dess APIer. Rust är ett nytt programmeringsspråk med målet att vara säkert, prak- tiskt och parallellt vilket genom designval kan lösa några av de största problemen med programmeringsspråket C som till exempel minness- läckor, trådhantering och segmenteringsfel. Genom att undersöka funktionerna i AirSim och dess olika sätt att kommunicera, utforskar vi möjligheten av att utveckla en egen flyg- kontroller i Rust och C som kan styra en drönare i simulatorn och utvärdera Rust i förhållande till C. Detta genomförs genom att läsa tillgänglig dokumentation för AirSim, studera källkoden och lära oss de kommunikationsprotokoll som används av AirSim. Denna avhandling resulterar i implementationen av en egen flygkon- troller i Rust och C som styr en drönare i AirSim med kommunika- tionsprotokollet MAVLink, vilket möjliggör en noggrann kontroll av motorerna. Slutsatsen gällande Rust och C är att båda språken fun- gerade väl för implementationen av säkerhetsritiska funktioner i flyg- kontrollern samt att Rust erbjöd förmågor som kan visa sig vara an- vändbara vid utveckling av säkerhetskritiska system. Nyckelord AirSim, Simulering, C, Rust, Säkerhetskritiska System, Flygkontroller, MAVLink Contents 1 Introduction 1 1.1 Background . 1 1.2 Problem . 1 1.3 Purpose . 2 1.4 Goals . 2 1.5 Method . 3 1.6 Delimitations . 4 1.7 Document overview . 4 2 Background 6 2.1 AirSim . 6 2.1.1 AirSim architecture . 7 2.1.2 Flight controllers . 8 2.1.3 Vehicles . 9 2.2 Flight controller . 9 2.2.1 Aircraft principal axes . 10 2.2.2 Sensors . 10 2.2.3 PID controller . 11 2.3 RPC . 12 2.4 MAVLink . 12 2.5 Safety-critical systems . 13 2.6 The C programming language . 13 2.6.1 Memory management . 13 2.6.2 Dangling pointers . 14 2.7 The Rust programming language . 15 2.7.1 Ownership . 15 2.7.2 Borrowing . 16 2.7.3 Memory management . 16 v vi CONTENTS 3 AirSim 18 3.1 AirSim settings . 18 3.2 AirSim API using RPC . 19 3.3 AirSim API using MAVLink messages . 20 3.3.1 MAVLink messages used by AirSim . 21 3.4 The notion of time . 24 4 AirSim findings and controller implementation 25 4.1 System requirements . 25 4.1.1 Operating System and setup . 26 4.2 Communication . 26 4.2.1 RPC . 26 4.2.2 MAVLink . 27 4.2.3 Network communication . 30 4.2.4 Serial communication . 31 4.3 Enabling the MAVLink distance sensor . 31 4.4 Multiple vehicle simulation . 32 4.5 Custom flight controller . 33 4.5.1 Choosing API . 34 4.5.2 Architecture using MAVLink messages . 34 4.5.3 Implemented functionality . 36 4.6 Code examples . 38 5 Evaluation and conclusions 39 5.1 AirSim . 39 5.2 Rust and C in safety-critical systems . 40 5.3 Goals . 41 5.4 Future work . 43 5.4.1 Rust and C comparison . 43 5.4.2 Flight controller . 43 5.4.3 Multiple vehicle simulation . 43 5.4.4 AirSim . 44 Bibliography 45 List of Figures 2.1 Realistic graphics in AirSim provided by Unreal Engine . 7 2.2 Core components of the AirSim architecture . 8 2.3 The car vehicle in the default environment of AirSim . 9 2.4 The drone vehicle in the default environment of AirSim . 9 2.5 Principal axes of an aircraft . 10 2.6 Block diagram of a Proportional Integral Derivative (PID) controller with a feedback loop . 11 2.7 RPC procedure call overview . 12 2.8 MAVLink message transportation overview . 13 3.1 RPC takeoff procedure call example . 20 3.2 MAVLink message sensing and receiving in AirSim . 22 4.1 Spawning multiple vehicles in AirSim . 33 4.2 Flight controller (FC) functionality summary . 33 4.3 How simple flight, AirSim and Remote procedure call (RPC) are connected . 34 4.4 Custom flight controller architecture . 35 4.5 The drone hovering above the takeoff position using the custom FC . 36 vii Listings 2.1 Heap memory allocation in C . 14 2.2 Dangling pointer in C . 15 2.3 Ownership in Rust . 16 2.4 Borrowing in Rust . 16 2.5 Rust compiler preventing a dangling pointer . 17 2.6 Heap memory management in Rust . 17 3.1 AirSim settings file example . 19 3.2 Changing the port of the default Application Program- ming Interface (API) server . 19 3.3 AirSim settings to enable MAVLink communication . 21 3.4 AirSim settings to set the simulation clock to Scalable- Clock and change its speed to a factor 2 . 24 4.1 AirSim API communication in C++ . 27 4.2 MAVLink message parsing in C . 27 4.3 Example of decoding common MAVLink message sent by AirSim in C . 28 4.4 Receiving, parsing and decoding common MAVLink mes- sages sent by AirSim in Rust . 28 4.5 Actuator controls messaging in C . 29 4.6 Actuator control messaging in Rust . 30 4.7 AirSim settings for over the network control . 31 4.8 AirSim settings to enable serial communication . 31 viii List of Tables 3.1 MAVLink messages AirSim is listening for . 21 3.2 MAVLink messages sent by AirSim . 22 3.3 Fields of a HIL_ACTUATOR_CONTROLS message . 23 3.4 Fields of a HIL_SENSOR message . 23 ix Acronyms API Application Programming Interface. 2–5, 7, 8, 18–20, 25, 26, 29– 31, 40, 42, 44 CC Companion computer. 7 FC Flight controller. 1–4, 7–12, 18, 22, 24–27, 30, 37–40, 42–44 GC Garbage Collector. 14, 17 GCS Ground Control Station. 12 GPS Global Positioning System. 10–12, 27 IDE Integrated development environment. 41 IMU Inertial Measurement Unit. 10, 12 JSON JavaScript Object Notation. 18 MAVLink Micro Air Vehicle Link. 8, 12, 18–23, 26–36, 40, 42–44 PID Proportional Integral Derivative. 11, 27, 38, 43 RC Remote control. 7–9 RPC Remote procedure call. 8, 12, 19, 20, 26, 30, 31 segfault Segmentation fault. 14, 17, 39, 41 UART Universal asynchronous receiver-transmitter. 43 UDP User Datagram Protocol. 20, 31, 35, 43 x Chapter 1 Introduction Developing software systems where the consequences of failure may result in death, damage to the environment or serious financial loss, known as safety-critical systems [7], usually have a high cost of verifi- cation and validation [19]. Simulating these systems can reduce these costs by finding design flaws in the early stages of development and engineers being able to model the system without resorting to proto- typing [8]. 1.1 Background AirSim is a new open source simulator developed by Microsoft AI & Research [18]. The goal of this simulator is to be a useful tool in the de- velopment of autonomous vehicles and the gathering of training data for machine intelligence [15]. Connecting the simulator to a safety- critical system, in the form of a Flight controller (FC), developed in the C programming language and the new programming language Rust [9] the suitability for each of these languages in the context of safety-critical systems can be compared and evaluated without the us- age of a real drone. 1.2 Problem Due to the young age of AirSim, there is little to none research based upon the simulator, making it unclear of its potential and capabilities.