Top Banner
Real-time synchronous programing on Arduino Henry-Joseph Aud ´ eoud Student at Ensimag, Grenoble Supervised by: Verimag Synchrone, Grenoble Tutor: Florence Maraninchi February-May 2014 Introduction Arduinos platforms are open-source easily programmable embedded systems. Many sensors (temperature, luminosity sensor, ...) or actuators (led, motors ...) can be added to them. But, the grater the number of elements is, the harder it is to manage them all. The Lustre language is a synchronous language. It is designed to be used in embedded systems, in real-time situations. Defined in the Verimag laboratory in the 80’s, it is now used in the SCADE software, a Esterel technologies tool, by many companies as Airbus, Eurocopter, Schneider Electric or Eurostar. Lustre and Arduino work on the same structure: the infinite loop which is frequent in embedded systems. This loop works like that: 1 loop 2 get inputs 3 compute outputs from inputs and internal state 4 set outputs 5 end loop As systems are similar, it can be interesting to use Lustre into an Arduino program. With it, we will be able to use the power of a synchronous language into this platform. This can simplify the complexity of sensors management algorithms. But Lustre is not designed to be executed on an Arduino board, and the sensors interfaces could be difficult to adapt to Lustre. I worked, with the direction of Florence Maraninchi, my tutor, on finding a way to adapt them together. I realized 1
17

Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

Jul 11, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

Real-time synchronous programing on Arduino

Henry-Joseph AudeoudStudent at Ensimag, Grenoble

Supervised by: Verimag Synchrone, GrenobleTutor: Florence Maraninchi

February-May 2014

Introduction

Arduinos platforms are open-source easily programmable embedded systems. Manysensors (temperature, luminosity sensor, ...) or actuators (led, motors ...) can beadded to them. But, the grater the number of elements is, the harder it is to managethem all.

The Lustre language is a synchronous language. It is designed to be used inembedded systems, in real-time situations. Defined in the Verimag laboratory in the80’s, it is now used in the SCADE software, a Esterel technologies tool, by manycompanies as Airbus, Eurocopter, Schneider Electric or Eurostar.

Lustre and Arduino work on the same structure: the infinite loop which is frequentin embedded systems. This loop works like that:

1 loop2 get inputs3 compute outputs from inputs and i n t e r n a l s t a t e4 s e t outputs5 end loop

As systems are similar, it can be interesting to use Lustre into an Arduino program.With it, we will be able to use the power of a synchronous language into this platform.This can simplify the complexity of sensors management algorithms.

But Lustre is not designed to be executed on an Arduino board, and the sensorsinterfaces could be difficult to adapt to Lustre. I worked, with the direction ofFlorence Maraninchi, my tutor, on finding a way to adapt them together. I realized

1

Page 2: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

that Lustre is useful to simulate and test algorithms before putting them into anArduino program.

Contents

Introduction 1

1 Presentation of used tools 21.1 Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Lustre . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Advantages of thinking parallel 7

3 How to interface Lustre for Arduino 73.1 Compilation chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.2 Adaptation of lustre with some specific Arduino’s parts . . . . . . . . 9

3.2.1 A simple sensor . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2.2 A time greedy sensor . . . . . . . . . . . . . . . . . . . . . . . 113.2.3 The serial interface . . . . . . . . . . . . . . . . . . . . . . . . 14

4 Routing-level algorithm and simulation with Lustre 15

Conclusion 17

References 17

1 Presentation of used tools

1.1 Arduino

The Arduino project started in 2005, in Italy. One of the aims of this project is tocreate a light, cheap, and open-source single-board micro-controller. These boards,called Arduinos, are easily programmable, and have the possibility to manage manycomponents such as radio chips or sensors. This allow them to be used in many tasks.

An Arduino project, also called a sketch, uses the well-known C++ language.Instead of implementing the standard main function, an Arduino programmer hasto implement two methods: void setup(void) and void loop(void). The setup

method is executed once, at the beginning or when the board reset button is pushed.The loop method is repeated indefinitely. It implements the body of the infiniteloop present in embedded systems. All Arduino boards have a serial interface toallow them communicate with other devices. The functions print, read, and someothers of the object Serial allow a programmer to USE this interface. The serialmanagement hardware has a buffer, and the available function returns the numberof bytes present in this buffer.

Here is an example of a kind of hello world simple program:

2

Page 3: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

1 void setup ( ) {2 S e r i a l . begin ( 9600 ) ;3 }45 void loop ( ) {6 // Get m i l l i s e c ond s number s ince l a s t r e s e t7 int time = m i l l i s ( ) ;89 // Compute some r e s u l t s

10 int hours = ( time /1000/3600)%24;11 int minutes = ( time /1000/60)%60;12 int seconds = ( time /1000)%60;1314 // Print r e s u l t s .15 S e r i a l . p r i n t ( ( S t r ing ) ”Hel lo , world ! The time i s : ” + hours +16 ”h , ” + minutes + ”minutes and ” +17 seconds + ” seconds . ” ) ;18 S e r i a l . p r i n t l n ( ) ;19 }

In the setup method, we initialize serial communication with the function Serial.The loop method contain the three steps of the infinite loop: firstly, we get inputvalues, (here, the returned values of the millis function), secondly, we computesome values (here, the hours, minutes, and seconds values), and thirdly, we put thesevalues on output (on serial, so the output messages can be read on a computer forexample).

The Arduino board I have used is called Arduino Mega2560 [1]. This type ofboard has a 16MHz processor, a 256ko flash memory, and a 8 ko SRAM memory.It has 54 digital input/output pins to manage digital sensors, and 16 analog pinscoupled with 10-bits analog-to-digital converters to manage analog sensors. It is alsoable to support parts plugged on it, called shields, like the Wireless SD shield (seefigure 1), which allows Arduino board to manage a Xbee radio chip, and a micro-SDcard. I’ve used this Xbee radio chip to make the Arduinos communicate betweenthem.

1.2 Lustre

Lustre [2,3] is a dataflow programming language. Formalized in 1991, it is currentlyused in the industrial environment SCADE [4], developed by Esterel Technologies [5].It is designed to create parallel and real-time programs, mainly for embedded systems.

Lustre is based on blocs of code organized into nodes. A node uses read-onlyparameters and a set of equations, to compute returned values. These equations,like mathematical equations, have no order. When a programmer implements itsprogram, he can think of all nodes as if their executions were simultaneous. This canbe useful for some applications, particularly in embedded systems, where there aremany sensors and actuators, which need to be managed together.

Nodes are combined in dataflow diagrams to get a main node, representing thelustre program. It is then compiled by the lustre compiler, into C language. To useit, a programmer needs to do two things in a loop. Firstly, give inner parameters

3

Page 4: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

Figure 1: The Mega2560 Arduino board and its Wireless SD shield

to the lustre program via inner methods, and secondly, call the step method, whichcompute output values. The step function automatically gives to output functionsthe computed values.

main_node

Temp_converter

node

nodeSpeed

distance

time

celsius_temp fahrenheit_temp

speed

Figure 2: Diagram of the three nodes of the example

Let’s take an example. Let’s have two sensors, one measuring the temperature indegree Celsius, and the other measuring a distance traveled by an object. We wantthe temperature converted in degree Fahrenheit, and the speed of the object. Thistwo calculations are absolutely independent, so we will put them in two differentnodes. As figure 2 shows, we will need a third node to encapsulate the others. Hereis the Lustre code of a such program:

1 node Temp converter (2 c e l s i u s t emp : i n t ;3 ) r e tu rn s (4 t emp i s v a l i d : bool ;5 ( fahrenhe i t t emp : i n t ) when t emp i s v a l i d ;

4

Page 5: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

6 ) ;7 l e t8 fahrenhe i t t emp = ( ce l s i u s t emp ∗ 9 / 5 + 32) when t emp i s v a l i d ;9 t emp i s v a l i d = ( c e l s i u s t emp >= −273.15) ;

10 t e l .1112 node Speed (13 d i s t ance : f l o a t ;14 time : f l o a t ;15 ) r e tu rn s (16 speed : f l o a t ;17 ) ;18 var19 t r a v e l e d d i s t a n c e : f l o a t ;20 t ime l ap s e : f l o a t ;21 l e t22 t r a v e l e d d i s t a n c e = d i s t anc e − (0 −> pre ( d i s t anc e ) ) ;23 t ime l ap s e = time − (0 −> pre ( time ) ) ;24 speed = t r a v e l e d d i s t a n c e / t ime l ap s e ;25 t e l .

As we can see, the two nodes are absolutely independent, as if they were two differentprograms. In the first node, the program uses a clock with the keyword when (line5). Clock is a Boolean variable indicating when another variable is valid. Here, ifthe temperature is below −273.15◦C, it is not a valid temperature, so the Booleanvariable is false, and the fahrenheit_temp has no value. In the second node, we cannotice the special structure 0 -> pre(var) of line 22 and 23. This structure workslike a memory (as pre is a shortened of previous): pre(var) returns the previousvalue of var, and the 0 is the initial value of the memory.

Here is the code of the main node:

1 node main node (2 time : f l o a t ;3 d i s t ance : f l o a t ;4 c e l s i u s t emp : f l o a t ;5 r e tu rn s (6 speed : f l o a t ;7 t emp i s v a l i d : bool ;8 ( fahrenhe i t t emp : f l o a t ) when t emp i s v a l i d ;9 ) ;

10 l e t11 speed = Speed ( d i s tance , time ) ;12 ( t emp i s va l i d , f ahrenhe i t t emp ) = Temp converter ( c e l s i u s t emp ) ;13 t e l .

Then, we use the lustre compiler to compile it into C language. We get two files,main_node.c and main_node.h. In order to make the Lustre program works, wehave to call the functions defined in these files as follows:

1 #include ”main node . h”23 void main ( ) {4 // I n i t i a l i z a t i o n5 f loat ce l s iu s t emp , fahrenhe i t t emp ;6 f loat di s tance , speed , time ;

5

Page 6: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

7 struct main node ctx ∗ ctx = main node new ctx (NULL) ;8 main node rese t ( ctx ) ;9

10 // I n f i n i t e loop11 while ( t rue ) {12 // Set inpu t s13 c e l s i u s t emp = // . . . g e t va lue from f i r s t sensor14 d i s t ance = // . . . g e t va lue from second sensor15 ma in node I c e l s i u s t emp ( ctx , c e l s i u s t emp ) ;16 ma in node I d i s tance ( ctx , d i s t anc e ) ;17 main node I t ime ( ctx , ge t t ime ( ) ) ;1819 // Compute va l u e s20 main node step ( ctx ) ;21 }22 }

In this main function, we have three parts. The first is initialization. Indeed, Lustreprograms need a structure to store the value of the variables, and such structureis called the context. The lines 7-8 declare and initialize it. The second part isthe acquisition of input values, in the infinite loop. They are set into the contextvia the main_node_I_* methods (lines 15-17). The third part is line 20, wherewe let the lustre program compute the node output values. These values (speed,temp_is_valid and fahrenheit_temp) are given thanks to the output functions(main_node_O_*) that we have to implement:

1 void main node O speed (void∗ cdata , f loat speed ) {2 // . . . do something wi th the speed3 }45 void main node O fahrenheit temp (void∗ cdata , f loat temp) {6 // . . . do something wi th the conver ted temperature7 }89 void main node O temp is va l id (void∗ cdata , bool v a l i d ) {

10 // do noth ing11 }

In the two first functions, we get the computed value with the second parame-ter of the function. We can notice that the third function is not used. Indeed,as the value fahrenheit_temp is only valid when the Boolean signal is true, themain_node_O_fahrenheit_temp function is only called at these times. The indica-tion that the temperature is valid is implicitly known in the fact that this functionhave been called.

Lustre and Arduino are not originally designed to work together. However, itcould be interesting to use Lustre into Arduino programs, because it can simplifiesthe design of algorithms, allowing a programmer to build the different parts of it asif they were executed in parallel.

6

Page 7: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

2 Advantages of thinking parallel

Work with the infinite loop is a good way to manage sensors. But, it can throwproblems in some situations, when we work with particular sensors, or when thesensors number grows.

The first problem which can occur in building a such program is a problem oftiming. Indeed, just imagine that, in the previous example, the temperature sensortake one second to make a measurement. With the previous code, the line 13 hide acall to a kind of sleep function, that wait one second.

10 // I n f i n i t e loop11 while ( t rue ) {12 // Set inpu t s13 c e l s i u s t emp = // . . . i n v o l v e s a one second de lay14 d i s t ance = // . . .

This constraint directly affect the speed of all others processing, because the infiniteloop will not be executed more than one time every seconds. This cannot work if weneed to sample speed more frequently than one time per second to detect little speedvariations. More generally, the time it takes to execute the body of the loop once ishard to know, and, as it is the time that separates two readings of external physicalproperties, we have to know exactly the interval between two readings. And, anyway,it should not be too long. So, the first part of the loop, the one that gets the inputs,must level the sampling speed difference between all sensors. This can be done bycreating an algorithm that store the states of the sensors. Here, the temperaturesampling could be started, and start date could be kept into a variable. While thissampling is processing, we can work with the second sensor, until the temperaturemeasurement is ready.

The second problem is a consequence of this solution. Indeed, make an algorithmthat keep state of all sensors could be really difficult, and can lead to really complexcase analysis with a dozen of inputs. And it is where Lustre can really helps us,because it allows us to work with all sensors as if they were executed in parallel. TheLustre compiler has a static scheduler that organize tasks. This allow a programmerto think a Lustre program as a real dataflow program, without interferences betweendifferent nodes. In the diagram of figure 3, the nodes 1 and 2 have their execu-tion absolutely independent. The execution of the node 4 depends on the outputof the node 3, but not on the node itself, so their conception can be dissociated.When a programmer have to build a such program, it can make the different nodesindependently, and algorithm conception is really simplified.

Using Lustre on Arduino can allow a programmer to cut his algorithm into severalsimple parts, and reduce its complexity. A simplification that will save time, and thatwill make bug easier to find.

3 How to interface Lustre for Arduino

Let’s take an example, with three Arduinos, as shown in figure 4. The first has atemperature sensor plugged on it, the second has a moisture sensor, the third has a

7

Page 8: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

Node 2

Node 3 Node 4

Node 1

Figure 3: An example of independence between nodes

led allowing it to signal a too high temperature or humidity. Each Arduino is able tocommunicate with the Xbee technology. How can we use Lustre to make them worktogether?

Arduino Mega2560

Node: Thermometer

Thermosensor Moisture sensor

Arduino Mega2560

Node: Client

XBee Radio

XBee Radio

Node: Moisture

Arduino Mega2560

XBee Radio

LED

Figure 4: Combination of the three Arduinos used in the example

3.1 Compilation chain

Lustre is compatible with Arduino. Indeed, Lustre compiler produces C code, whichcan be understood by Arduino compiler, without any problems. Moreover, the struc-ture of Arduino programs is naturally composed of an infinite loop, where the Lustrestep function can be called. This makes that it was not so hard to adapt Lustre struc-ture in Arduino programs. Let’s study the compilation of one of the three programsbuild on one of the three Arduinos, for example, the node client, which managesthe led.

After compilation of the Lustre program, we get the two files client.c andclient.h. As in a C program, we include the header in the client.ino file, whichis the main file of an Arduino project. The Arduino IDE automatically detects .cppsfiles, compiles them, and includes them in the code uploaded on the board, so it issufficient to rename the client.c file in client.cpp.

Then, we need to create and initialize the Lustre context, and to implement thesetup and loop functions. That what does the following code:

8

Page 9: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

1 #include ” c l i e n t . h”2 struct c l i e n t c t x ∗ ctx = c l i e n t n ew c tx (NULL) ;34 void setup ( ) {5 c l i e n t r e s e t ( ctx ) ;6 }78 void loop ( ) {9 // Compute input va l u e s

10 time = m i l l i s ( ) ; // ge t time , in m i l l i s e c ond s11 // Set i t i n t o the con t ex t12 c l i e n t I m i l l i s ( ctx , time ) ;1314 // Do a s t ep15 c l i e n t s t e p ( ctx ) ;16 }

At line 2, we declare and create the Lustre context. At line 5, we reset this context.Indeed, we need to reset it every time we push on the reset button of the Arduinoboard. Then, in the loop function, we implement the body of the infinite loop. Theinput values are retrieved at line 10, and set into the context at line 12, thanks tothe client_I_* method. At line 15, we finally call the step Lustre method, whichlaunch the computation.

This method call output methods that we have to implement, as in a C program.Here, we have to send a computed value to the led with the analogWrite Arduinomethod, so the output function client_O_led_value can be implemented like this:

1 void c l i e n t O l e d v a l u e (void∗ cdata , int l e d va l u e ) {2 analogWrite ( l ed p in , l e d va l u e ) ; // sends va lue to the l e d3 }

Therewith, lustre program is melded with Arduino code, and Arduino IDE cansend a working program with embedded Lustre to the board.

The int or float Lustre casting operators need two others files (client_ext.cppand client_ext.h), which define the casting functions.

3.2 Adaptation of lustre with some specific Arduino’s parts

The compilation chain is not really hard to put in place. The main problem of usingArduino with Lustre, is to use adapt some complex sensors or actuators to the Lustrelanguage. Some of them have specific interface to get data, and Lustre code has tobe adapted to these specificities.

For the given example, we have mainly three points to consider: the moisturesensor, the temperature sensor, and the XBee radio chip, which can be seen as asensor and an actuator coupled together (for input and output data flows).

3.2.1 A simple sensor

The SEN92355P moisture sensor is an example of sensor, which is really simple tomanage with arduino, and easy to interface with lustre. It is able to get the moistureof a flower pot dirt, for example, as in figure 5. It is an analog component, so has to

9

Page 10: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

be plugged on an analog pin of the Arduino board. The value of the moisture canbe obtained with the analogRead Arduino function, which makes a 10-bits analog-to-digital conversion.

Figure 5: A SEN92355P in a flower pot

The first possibility to make it work with Lustre, is to make a measurement atevery cycle of the loop. In this case, utilization in Lustre is really simple. We candirectly use the moisture value, as it is always valid and up to date. The Lustre nodeis declared in this way:

1 node moisture (2 mo i s ture va lue : i n t ;3 ) r e tu rn s4 foobar : bool ; −− Lustre node must re turn something5 ) ;

The Arduino code only call two functions, moisture_I_moisture_value which setthe value into the context, and moisture_step which launches Lustre:

1 void loop ( ) {2 mo i s tu r e I mo i s tu r e va l u e ( ctx , analogRead ( SEN92355P pin ) ) ;3 mo i s tu r e s t ep ( ctx ) ;4 }

It only takes 100 microseconds to the function analogRead to make a conversion.But, for some reasons, either because 100 microseconds is too long for our usage, orbecause we need to save energy, the measurement could be done only when we needit. In this case, the node have to returns a Boolean flow, that will indicate if anupdate is needed or not. In response, it takes an input flow with its clock, becausethe moisture value will not be available all the time. The node external interface isshown in figure 6, and is declared in Lustre in this way:

1 node moisture (2 mo i s tu r e va l i d : bool ;3 ( mo i s ture va lue : i n t ) when mo i s t u r e va l u e va l i d ;4 ) r e tu rn s (5 update reques t : bool ;

10

Page 11: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

6 ) ;

node

Moisturemoisture_valid

moisture_value

update_request

Figure 6: External interface of the moisture node

The Arduino code is a little more complex than in the first version, as we need tostore the value of the update_request signal:

1 bool update was requested = f a l s e ;23 void loop ( ) {4 i f ( update was requested ) {5 mo i s t u r e I mo i s t u r e v a l i d ( ctx , t rue ) ;6 mo i s tu r e I mo i s tu r e va l u e ( ctx , analogRead ( SEN92355P pin ) ) ;7 } else {8 mo i s t u r e I mo i s t u r e v a l i d ( ctx , f a l s e ) ;9 }

10 update was requested = f a l s e ;11 mo i s tu r e s t ep ( ctx ) ;12 }1314 void mois ture O update request (void∗ cdata , bool update reques t ) {15 update was requested = update reques t ;16 }

In the output method, we store the update request. At lines 4-9, measurement isdone only if a request was made last cycle. By doing this, the node can manage thesensor, and get a value only when it needs it.

These two solutions show how this simple sensor can be used into a Lustre node.

3.2.2 A time greedy sensor

The DS18B20 thermosensor is a digital thermometer, which is a little more hard tointerface with Lustre. It can be used by an Arduino board and gives temperature atthe maximum precision of 0.0625 ◦C. It uses the one-wire protocol [6] to communicatewith the board micro controller. The Arduino’s OneWire library [7] allow us to workwith it. Here is the code to manage it:

1 #include <OneWire . h>2 OneWire ds (DS18B20 pin ) ;34 // S ta r t the temperature measurement5 ds . r e s e t ( ) ;6 ds . sk ip ( ) ;7 ds . wr i t e (0 x44 , 1 ) ;89 // Wait the measure i s done

11

Page 12: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

10 de lay ( 7 5 0 ) ;1112 // Ret r i eve the measured va lue13 ds . r e s e t ( ) ;14 ds . sk ip ( ) ;15 ds . wr i t e (0xBE) ;16 temperature = ( ds . read ( ) + ( ds . read ( ) << 8) ) ∗ 0 . 0 625 ;

As we can see at line 10, the DS18B20 sensor has the particularity to take a measurefor 750 milliseconds (with maximal precision), and store it into an internal registerbefore sending it thanks to the one-wire protocol to the board. The first part of thealgorithm (above the delay method) start the measurement, and the second part getthe value stored into the register.

measurement retrievingthe valuewith other sensors

Do something elsestopstart

measurement

time

Figure 7: Chronological diagram of the temperature sampling

We reasonably can’t let the board be blocked for 750 milliseconds, and doingnothing else within this time. A chronological diagram is given in figure 7, describ-ing what we need: work with others sensors between the start and the end of themeasurement. The Lustre node has to be able to start the measurement and get thevalue of the register with two different ways, to release the system during this time.So, the above algorithm has to be separated in two parts, corresponding to these twoactions. As shown in figure 8, the node temperature has two output signals. Thefirst starts the measurement, and the second get the value of it. As input parameters,the node should get the current time (because it has to wait for 750 milliseconds),and the value of the temperature and its clock (as the value is not available all thetime). The declaration of a such node is done in this way:

1 node temperature (2 m i l l i s : i n t ;3 t empera ture va l id : bool ;4 ( temperature va lue : i n t ) when tempera ture va l id ;5 ) r e tu rn s (6 s t a r t s amp l e : bool ;7 get sample : bool ;8 ) ;

In the Arduino code, as always, we need to set inputs into the Lustre context, andstart Lustre. The loop body is similar to the second version of the moisture sensornode:

1 bool t empe r a t u r e i s v a l i d = f a l s e ;2 f loat temperature va lue = 0 . 0 ;34 void loop ( ) {5 the rmomet e r I m i l l i s ( ctx , m i l l i s ( ) ) ;

12

Page 13: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

get_sample

start_sample

temperature_value

temperature_valid

millis

node

temperature

Figure 8: External interface of the temperature node

6 i f ( t empe r a t u r e i s v a l i d ) {7 thermometer I t emperature va l id ( ctx , t rue ) ;8 thermometer I temperature va lue ( ctx , temperature va lue ) ;9 } else {

10 thermometer I temperature va lue ( ctx , f a l s e ) ;11 }12 Thermometer server step ( ctx ) ;13 }

At lines 6-11, we conditionally set the value of the temperature, if it is valid. Thetwo variables describing the temperature declared at lines 1 and 2 have their valuesset into the output methods:

1 void thermometer O start sample (void∗ cdata , boo lean s t a r t ) {2 i f ( s t a r t ) {3 ds . r e s e t ( ) ;4 ds . sk ip ( ) ;5 ds . wr i t e (0 x44 , 1 ) ;6 }7 }89 void thermometer O get sample (void∗ cdata , boo lean get ) {

10 i f ( get ) {11 ds . r e s e t ( ) ;12 ds . sk ip ( ) ;13 ds . wr i t e (0xBE) ;14 t empe r a t u r e i s v a l i d = true ;15 temperature va lue = ( ds . read ( ) + ( ds . read ( ) << 8) ) ∗ 0 . 0 625 ;16 }17 }

In the first method, we only start the measurement. This is the first part of theoriginal algorithm. In the second method, we get the value stored in the sensorregister, and put it into the two variables that describe the temperature. Thesevalues will be put into the Lustre context at the next cycle of the loop.

By this way, we don’t use the delay method. Indeed, a 750 milliseconds timeris present into the Lustre node, that allow the Arduino board to work with othersensors during this time. And so, a time greedy sensor is used without interferenceswith others sensors.

13

Page 14: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

3.2.3 The serial interface

Arduino boards integrate a serial interface. This interface can be seen as a sensor andan actuator coupled together, for the input and output flows. By default, the data onthis interface is transmitted via USB to the computer. But, if a Xbee chip is pluggedon an Arduino board, an external switch allow to choose if the serial data flow issend via USB or via the radio channel. To make our three Arduinos communicatetogether, we have plugged an XBee chip on each Arduino.

The serial interface has a buffer, that store input data when they came. Thefunction Serial.available returns the number of bytes available in this buffer, andthe function Serial.read unstack the first byte of it. The function Serial.write

send values given in parameter on the serial interface.The radio input flow is irregular: it has input data only when another board sends

data, when the buffer is not empty, and output data when the program has to sendsomething. So, as for the previous nodes, we will use use clocks, for input flow. Theoutput flow will have to be managed exactly in the same way, but in the inverteddirection: the two variables are returned by the lustre program, instead of being givenas parameters. The figure 9 and the following code give the external interface of thenode client, which uses this radio:

node client (

radio_in_valid : bool;

(radio_in_value : int) when radio_in_valid;

) returns (

radio_out_valid : bool;

(radio_out_value : int) when radio_out_valid;

);

Radio

node

radio_out_valid

radio_out_value

radio_in_valid

radio_in_value

Figure 9: External interface of the radio node

If the serial buffer is not empty, the first byte has to be set into the context. Asprevious examples, we conditionally set the radio_in_value variable:

1 void loop ( ) {2 i f ( S e r i a l . a v a i l a b l e > 0) { // I f b u f f e r i s not empty3 c l i e n t I r a d i o i n v a l i d ( ctx , t rue ) ;4 c l i e n t I r a d i o i n v a l u e ( ctx , S e r i a l . read ( ) ) ;5 } else { // I f the b u f f e r i s empty6 c l i e n t I r a d i o i n v a l i d ( ctx , f a l s e ) ;7 }8 c l i e n t I s t e p ( ctx ) ;9 }

14

Page 15: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

There are two output functions that have to be implemented, as we have twooutput values. As the output method of the variable radio_out_value is only calledwhen there are something to transmit, we don’t need the client_O_radio_in_validmethod. Its body is empty. The other method has only to send data on the radiochannel, that is the serial interface:

1 void c l i e n t O r a d i o o u t v a l i d (void∗ cdata , boo lean b) {2 }34 void c l i e n t O r a d i o o u t v a l i d (void∗ cdata , int byte ) {5 S e r i a l . wr i t e ( byte ) ;6 }

4 Routing-level algorithm and simulation with Lus-

tre

In the previous example, we have some problems with the radio channel. Firstly, theclient node sends a temperature or a moisture request to the others boards to getthe corresponding values. But, the serial interface is not able to know from wherethe response come from. So, as described at left of the figure 10, when the client

node receives a value, it is not able to know if it is a temperature or a moisturemeasurement. The second problem is that the radio channel could lose packets.Imagine that a packet containing a temperature request is lost, as shown at right offigure 10. The sending node is waiting for the response, and the temperature node,which has not received the request, is waiting on it, so we have a dead-lock.

temperature request

moisturenode

temperaturenode

client

moisture request

50

22

nodeclient

node nodetemperature

temperature request

22

Dead−lock !moisture = ???

temperature = ???

packet loss

temperature request

Figure 10: Example of problems without a routing protocol

A routing simple algorithm can resolve these problems. The first one can beresolved by adding a one-byte header to each sent value, describing the data (m formoisture, t for temperature). To resolve the second problem, the node starts a timerwhen it sends its request. If no response came back, it performs a retransmission.The figure 11 shows a the two problems resolved by a such algorithm.

15

Page 16: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

temperature request

moisturenode

temperaturenode

client

moisture request

nodeclient

node nodetemperature

moisture = 50temperature = 22

temperature request

packet loss

m 50

t 22

t 22

retransmissiontemperature request

Figure 11: The two previous problems resolved by the routing protocol

This routing algorithm is easy to implement, but it is really complex to debug.Indeed, a program running on the Arduino platform is not fully observable. Thereare no debugger programs, like gdb on computer, that allow us to known the stateof the program. Moreover, to test it in extremes situations, we want to decide whichpacket is lost and which is not. The radio channel does not allow us to such anexperiment. Indeed, we can only reduce the strength of the signal (by increasing thedistance between Arduinos, or by putting a barrier between them), but we cannotdecide precisely if a packet succeeds to be transmitted or not.

Both parts (the thermometer, the moisture, and the client nodes) are written inlustre. And, as a Lustre node, they can be integrated into another node. Then, wecan create such a node, called simulator as it simulates the execution of the threeothers nodes. Add into it a fourth node, called channel, which aim would be todecide if the transmission of the packet was successful or not. Nodes are representedin figure 12. We don’t use the real radio of Arduino, but the fourth node simulate aradio channel that can lose packets, by either forwarding the packet or not. Then, wecan generate controlled packet losses, and demonstrate the strength of our algorithmto them.

simulated

radio

channeltemperature node

moisture node

client node

radio flows with losses

radio flows

simulator node

Figure 12: Nodes used in simulation (only radio data flows are represented)

The radio channel simulation node is built to lose some packets. If our algorithmis not solid to such losses, we would be able to produce the problem during simula-

16

Page 17: Real-time synchronous programing on Arduino · 1 Presentation of used tools 1.1 Arduino The Arduino project started in 2005, in Italy. One of the aims of this project is to create

tion, trace it and solve it, because of the fully-observability of Lustre. The Arduinoplatform is not as observable as Lustre, and the issue would be much more difficultto find on it.

Conclusion

Execute a Lustre program on the Arduino platform are not so difficult. InterfaceArduino’s sensors can be harder, depending on the interface of the sensor, but itgreatly simplify management algorithms conception. The arduino platform can workwith many sensors. But, after have studied some difficult sensors, it would not beso difficult to adapt other sensors to them. And use Lustre is really interesting withbig algorithms, because it can simulate and test them much more easily than if theywere directly coded into the Arduino native language.

Lustre is initially designed to work on centralized embedded systems. Here, wehave used Lustre as a distributed programming language, where communicationsbetween boards are effectuate on a radio channel. It is a new usage of Lustre, whichyields easily to this use. The algorithms are not huge, but Lustre may be able tomanage them, without many difficulties.

References

[1] Arduino mega2560 board description. http://arduino.cc/en/Main/

ArduinoBoardMega2560, 2013. [Online, accessed 6-May-2014].

[2] Lustre and synchrone, its conceptor team. [Online, accessed 16-May-2014].

[3] Halbwachs N.; Caspi P.; Raymond P.; Pilaud D. The synchronous data flowprogramming language lustre. Proceedings of the IEEE, 79(9):1305–1320, 1991.

[4] Scade suite. http://www.esterel-technologies.com/products/scade-suite.[Online, accessed 16-May-2014].

[5] Esterel website. http://www.esterel-technologies.com. [Onlinde, accessed16-May-2014].

[6] Robert D Lee. One-wire bus architecture, May 11 1993. US Patent 5,210,846.

[7] Onewire library. http://www.pjrc.com/teensy/td_libs_OneWire.html, 2007.[Online, accessed 10-May-2014].

17