MIRA
|
Previous: Tutorial: Creating a Unit that Publishes Data | Next: Tutorial: Using Parameters and Properties |
---|
This tutorial assumes that you have created a domain and that you know how to create, compile and launch a unit as described in the previous tutorials. To run the Unit from this tutorial you need the "FloatPublisher" Unit of the previous tutorial. If you did not complete that tutorial, you can download the code of that Unit here.
In this tutorial we will get to know MicroUnits, a special type of Units that are reactive only and therefore have no worker thread to perform cyclic operations. The MicroUnits concept is suitable for Units that react on incoming data only. In this tutorial, we will create such a MicroUnit, that subscribes on the data that is produced by our "FloatPublisher". Our new Unit will compute the moving average on that data within a certain window. This mean value will be published again to be used by other units.
First, we create the MicroUnit using the mirawizard similar to the previous tutorial. This time we select "MicroUnit" as component type. Let's call our MicroUnit "MeanCalculator" and place it into the "tutorials" namespace within our Tutorials domain. After you have created the MicroUnit you should have a source file that contains something like:
In order to receive the data from our "FloatPublisher" that it writes to the "FloatChannel", we need to subscribe on that channel. Again, we use the initialize() function to do that because this function is called after the unit was created and before it gets started.
Similar to publishing a channel, we need to specify the data type of the channel and its name that we want to subscribe. Moreover, we can specify a callback method that is called whenever new data becomes available.
We also need to add the onNewData callback method to our class:
This function will automatically be called when new data is published to the channel, with the read-locked data of the latest update as parameter.
Now we can calculate the mean value. We will do this by using a queue collecting up to 10 values. When we have reached 10 values, we will throw away the oldest and calculate the mean.
For demonstration purposes, we write out the computed average on the console.
Now that we have used this naive but simple way for calculating the moving average, we can publish it in a channel for all the other modules interested in the mean of the 10 last float values. As we have learned in the previous tutorial, we can do this by adding the following lines:
Now every time we have computed a new mean, we will write it to the channel "MeanChannel".
Instead of
we could use the much shorter
However, please note that this should only be used for data where copying is cheap, since the post() method will result in copying of the data, while the ChannelWrite version can be used without additional unnecessary data copying.
We are already done, and the code of our Unit should look like this.
Now we can compile and test it. The mirawizard has already added the following lines to our domain's CMakeLists.txt, that build a shared library for our Unit:
As in the previous tutorial, we do not need to make any changes here and can build the unit by typing:
within our project's root directory.
To start our unit, we have to adapt our configuration XML file. Therefore, we open the file domains/Tutorials/etc/Tutorials.xml
and modify it as follows:
The second <unit>-line now additionally starts our new "MeanCalculator" Unit beside our "FloatProducer"
Now we can start that configuration file using the mira tool, and should see the outputs of our two Units:
As you can see, whenever the "FloatProducer" generates a value, the "MeanCalculator" is activated by its onNewData() callback and computes a new mean value.
Next: Tutorial: Getting in touch with miracenter |
---|