Collision Prediction with Native Kotlin

November 4, 2022

Kotlin is now the preferred language for Android development. The primary design is to compile bytecode for the Java virtual machine, however, it also allows programs to be compiled to Javascript or native binaries. The native binaries are compiled with LLVM and allow Kotlin to be used on iOS platforms. Since most phones and embedded devices are now using chips with the ARM instruction set, it could be possible to code embedded software in Kotlin and compile to a native binary which could be run on the device. In order for this to be a feasible strategy, the Kotlin native binaries have to have similar runtime to C/C++ code which is already being used on those devices. However, if embedded Kotlin is efficient enough, it could simplify embedded development by allowing the usage of higher level languages.

In order to benchmark the performance of Kotlin on embedded hardware, algorithms need to be written in both C and Kotlin. The collision prediction algorithm from [Tome’s Device to Vehicle program] “D2V” is a natural choice since it is a C library with a Java native interface. The algorithm can easily be re-written in pure Kotlin, and then compared to C runtime on a PC.

The collision prediction simulation reads a CSV file containing personal safety messages (PSMs). This makes it trivial to benchmark the two versions of the algorithm. The main library is mostly based on vector math and trigonometry, which makes it straightforward to translate to  Kotlin. The setup of the native Kotlin build was more complicated than expected. While the collision prediction library was successfully re-written, it was only compiled as JVM bytecode. The next step would be to complete the benchmarking by compiling the algorithm to native binaries and testing on long CSV files containing PSMs. The remaining work is enough for another lab day project.