Last year at WWDC24, Apple announced official support for compiling code written in their Swift programming language for embedded devices (low-power systems without an operating system). This caught my attention, since I mostly regarded Swift as a very high-level language mostly used for writing iOS and Mac apps. I wondered if it was possible to get this kind of toolchain running on some weird devices, but the docs were severely lacking at the time and everything was very under-developed.
Last month, embedded Swift was brought up in a discussion again, and @lewis and I were interested in seeing if we could compile and run a Swift program natively on a VEX Brain for fun. At the time, Apple had just recently published documentation for compiling to ELF on embedded ARM using a custom linkerscript (which is what we were looking for), so Lewis started working on a proof of concept toolchain for all of this.
After setting up some basic plumbing in a C support library, we now have the first VEX program written in Swift!
As it turns out, Swift is a pretty standard compiled language that shares much more in common under the hood with C, C++, and Rust compared to a language like Java or Python. The key thing here is that Swift uses LLVM as its compiler backend and is able to link with C libraries and call C functions from directly within its language. Because of this, weâre able to compile Swift code to native ARM instructions that the Brain can immediately recognize and run.
Why?!
This was done for fun and pretty much no other reason other than âeh, we couldâ. Neither @lewis or I plan to seriously use this in competition, though after messing with it, Swift does seem like a pretty cool language to write with.
Source code?
Due to some potentially sensitive implementation details, I wonât be linking to any source code on this forum. I mostly just wanted to show this off because itâs a fun proof of concept and I find it neat that weâre able to run so many higher-level languages on this system.
I want to see more programming languages come to vex. JS is a good start as well as something like C# and C itself.
I personally use python myself as I can write different kinds of programs from ones to stop unnecessary processes on my pc to the one I use for my robot. Python was the first programming language I learned and I learned it before I even knew what vex was. But some other programming languages then the ones we have now would be cool.
I would like vex to provide something that can let you write programs in whatever coding language you want without them having to create something like a python module or something similar. That way teams have even more freedom on what they can use to program a bot.
Swift is actually something that I would not expect to be able to run on a v5 brain. I donât know what other programming languages supports ARM devices without an os but I would research more.
JS has actually been done before! RoboMatter used to offer a javascript API for the V5 before RMS shut down. Even though you technically could use it, I donât think you should mainly because JS can fall flat in very concurrent applications which is common in robotics. Also, while browser engines have put millions of dollars into optimizing JS engines on the web, the embedded scene is a different story and I canât say you can expect great performance there.
C# is an interesting story, because itâs very .NET-centric which isnât at all designed to run on embedded chips. nanoframework does exist and does indeed run on devices far less powerful than the brain, but it requires a complete port of the ChibiOS/RT kernel to run on top of, which would take a fair amount of effort to get working. Even if you did manage to do all that, I canât imagine it being a great experience to use.
C can (and does) run on the Brain natively. I can guarantee that if youâve ran a program on your robot before, some C code was involved somewhere to make that program work. Modern versions of PROS still maintain a fully usable C API alongside their C++ stuff, and I know of at least one high school team who used to program exclusively with the PROS C API.
I would like vex to provide something that can let you write programs in whatever coding language you want without them having to create something like a python module or something similar.
Generally getting a language to run on the brain is far easier than making it into an actually usable experience for programming a full robot. Thereâs a lot of work involved between a âhello worldâ and getting a functioning autonomous route. Itâs understandable that VEX would only officially offer support for C++ and Python, because it would take a pretty immense effort to maintain hardware APIs for five different languages.
I donât know what other programming languages supports ARM devices without an os but I would research more.
Languages that (to my knowledge) have ran on the Brain before:
C
C++
Python (via MicroPython)
MATLAB (running on top of a FreeRTOS C runtime)
Rust
Swift
Elixir (hosted by a PROS program)
Javascript (via QuickJS)
Typescript (transpiled to JS)
Java (via WASM)
Kotlin (via WASM)
Fortran (not sure about this one - though I think a VEXU team tried to get LAPACK running)
ARM Assembly (A friend of mine wrote a fully functional clawbot program using only ARM assembly while trying to learn assembly)
Languages that I think could realistically run on the Brain, but havenât been:
Lisp (there are a bunch of attempts to run various Lisps just about anywhere :p)
Theoretically any other language that can compile to WASM and produce a binary under roughly 2mb (there are probably a lot of these, multiple people have joked that Haskell could work here).