Engee documentation
Notebook

Code generation for Arduino (flashing LED)

The Arduino platform is today the most common platform for educational robotics, automation of simple processes. It is used for prototyping in many application areas.

In this example we will show how to generate Arduino programming code from a graphical model in Engee, using one of the simplest tasks in circuit design. Let's create a model that will blink the "on-board" LED of the Arduino platform with some specified frequency.

Introduction

The solution to the problem of transferring a model to a hardware platform, outlined in this example, can be broken down into several steps:

  1. Creating or modifying the Engee model 2 Describe the Model-Arduino interface
  2. Downloading the folder with the project and uploading it to the platform via Arduino IDE

In the model-centric design process, we try to focus all our efforts on the first stage - modelling the algorithm that will be transferred to Arduino. The interface customisation stage requires very little intervention, and code generation and uploading to the target platform, although not automated, does not involve any complex operations.

Platform preparation

For this example we only need an Arduino-compatible platform (Uno, Leonardo, Iskra and others) and a suitable USB cord.

You will also need to install Arduino IDE on your computer, find and install additional drivers (if necessary) and connect the existing board via USB port.

image_2.png

Finding and installing drivers to connect to external hardware usually takes some effort. The driver above should allow you to work with the Arduino Uno, Arduino Leonardo and many other similar boards such as the Iskra Neo. However, in case of any difficulties, we recommend that you consult the help articles or the Engee community for assistance.

Model description

We will generate the code from the model blink.engee.

At each step, it performs the calculations described therein and returns two values:

  1. out_LED_BUILTIN - LED state (initially equal to 1, changing to "opposite" with each cycle),
  2. param_WAIT_MS - the constantly returned value 500 (the number of milliseconds between two calls to our model code).

image_2.png

One of the conditions for code generation is that the model use a constant-step solver.

It is also worth keeping in mind that it is not technically possible (or even advisable) to generate code from all blocks. The list of allowed blocks is still very large, but it is always useful to check the Engee code generator documentation._

Code generation

Get the code for this model by clicking in the top right corner of the Engee workspace (you will then need to move the blink_code folder inside the sketch_blink_custom folder ).

In this demonstration, we propose to do this with the following command:

In [ ]:
engee.generate_code( "$(@__DIR__)/blink.engee",
                     "$(@__DIR__)/sketch_blink_custom/blink_code" )
Out[0]:
"Created directory - /user/start/examples/codegen/arduino_blink/sketch_blink_custom/blink_code"

As a result, in the directory sketch_blink_custom we will find the folder blink_code, in which we will find .c and .h files with the code of our system.

Project files

There is only one model in our project. From the generated files we need only files with the code of this model: source code blink.c and header file blink.h.

It is worth paying attention to the structure of files and directories of the project:

image_2.png

  • file sketch_blink_custom.ino contains description of the Model-Arduino interface,
  • the sketch_blink_custom level directory must be named the same as the *.ino file (Arduino IDE requirements),
  • the directory blink_code contains files with generated code, which we will include in the file *.ino.

Model-Arduino interface

The interface between the ports of our model and the input points to the different Arduino functions is described in the file sketch_blink_custom.ino. We suggest you to open it directly in Engee and study its contents.

This code implements an infinite loop that runs the generated code of the model. output values are placed in the structure blink_Y, after which each value is passed by us to the required function from the standard Arduino library.

Transferring the model to Arduino

It remains to package the catalogue with the demonstration into an archive and download it from the Engee file manager to your local computer. Archiving and downloading can be done with a single context menu action.

image_2.png

After unpacking the archive you will find the file sketch_blink_custom.ino, which can be opened in Arduino IDE.

image_2.png

You have a ready project opened in the Arduino IDE, which you just need to send to the target platform using the "Upload" button.

image.png

A typical result of running the example is shown in the following video:

20240126_122454_2_30fps.GIF

Conclusion

Working with low-level code usually requires some technical skills and additional manipulations.

In addition to developing the model you will need, at a minimum:

  • create an interface between the Engee model and Arduino functions,
  • download the project and run it on a local, target platform.

More complex problems may arise when implementing individual application tasks - for example, matching the speed of the model to the clock speed of the Arduino processor.

However, your optimised workflow, built in Engee and suited to your needs, will save a lot of effort in a complex project by automating many routine activities:

  • synchronising project code to match mathematical models,
  • creating visual documentation - in Engee you can automatically, using commands, add schematic photos and output graphs to scripts and generate a report in HTML or PDF,
  • and greatly simplify semi-natural testing.

Blocks used in example