Summary: A remote control for a slot car racing tracks; Unnecessarily complicated and inefficient; Ladies, Gentlemen and everyone else, start your engines.
However, simply creating an API that offers precise control over the device seemed way too straight forward - the controlling mechanism itself should add its challenges.
So I came up with the idea to control it with the network traffic itself.
The more traffic is sent to the remote control, the faster the car is supposed to go.[°Use more bandwidth!!!]
In order to build the remote control, the first step was to figure out how the slot car racing system works. So I disassembled it and took a look at its wiring.
The speed of the car is controlled by a variable resistor within the controller, which allows to control the voltage the car receives.
Pushing the controller further down lowers the resistance, which raises the voltage available to the car and makes is go faster.
A third wire is used to brake the car when the controller is in its neutral position by utilizing the current generated by the motor when the car is rolling out.
A look at wikipedia confirms this analysis. [^1]
Now that I understood the mechanism, the next step was to create a digital version of it, so that I could control the slot car racing track with a Raspberry Pi.
My first idea was to simply use a digital potentiometer.
However, searching for a suitable digipot, I quickly realized that there were no options with the desired dimensions of about 30 Ω available.
Therefore I built it myself using a few resistors, solid state relais and multiplexers.[°I'm sure there are simpler solutions for this, but, well, it worked for me.]
For the sake of simplicity,[°or maybe out of laziness, who knows? :)] I did not add the braking mechanism from the analog controller to my digital version.
With the hardware fully assembled and soldered, it was time to have a look at the software.
Python offers an easy way of implementing TCP servers.[°This project will use TCP. Even though the payload is immediately discarded, reliability is of the utmost importance and packet loss will not be accepted!!!]
By default, it only supports IPv4, but IPv6 support can be added by setting `address_family` to `AF_INET6`.
The server's logic itself is implemented in the method `handle()` of the class `SCRoIPRequestHandler`. While there is an active connection, it reads from the socket and sums up the amount of data that was received.