Highspeed water drops photograhy – ‘Scripting’ an arduino microcontroller via dotnetcore

This is the first part of the software solution for the highspeed photography problem described here. What will get covered in this post will be how to get a script from your desktop translated to something which might be understood by an Arduino application.

As mentioned before, there will be a desktop app which will instruct an Arduino app to execute some operations:

Will focus now on the first step, building the list of instructions for Arduino.

What are the operations executed by the Arduino app which we want to orchestrate with our script? For start: Wait(time), TurnPinOn(pinNumber), TurnPinOff(pinNumber).
Can we represent these in way simple enough to be processed by the Arduino app with minimal effort? Well, yes. Assign an identifier (int) to each of these operations, use another int as the parameter of the operation, and keep an array of opId,param,opId,param,… as the list of operations to be executed. The Arduino app will iterate through the list and invoke a method with a parameter based on the current operation id.
So, the first requirement for the desktop app will be to translate a TurnPinOn(1); Wait(100); TurnPinOff(1); in something like [2,1,1,100,3,1].

From a c# implementation point of view, one approach for generating such a list would be to have the three methods adding their two ints in a list each time they are called.
We could ask dotnet to continuously watch and build the whole app when something got changed but that just takes maybe a bit too long and makes the whole process less responsive. Additionally, keeping the ‘script’ for arduino within the application makes things maybe a bit too muddy. But there is another option.
Besides occasionally tossing toasters out of an airlock, Roslyn brings another nice feature: the possibility of running scripts (generic c# scripts) in your application. With Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript we have a very convenient way of building up a c# script around the initial ‘script’ and translating it in that list of ints we need.

What does the desktop application at this stage: it monitors a file [1]It was wrong to add a GUI in the first implementation. There are enough text editors present on the machine running the desktop application., when the file is edited (and saved) it picks up the script from the file, wraps it in a predefined class, and executes the method generating the instructions for the arduino app. App can be found here and, its execution steps can be traced via the (I hope) explicit log entries.

Next: the arduino application and the communication between the two apps.

References   [ + ]

1. It was wrong to add a GUI in the first implementation. There are enough text editors present on the machine running the desktop application.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.