-
Notifications
You must be signed in to change notification settings - Fork 119
Example1
This is a simple example on how to create tasks in DUNE. There are three parts in in this exercise:
- Producer: will create a static variable "temperature" and set a value for it then it will be send as an IMC message.
- Consumer: will keep searching in the BUS for the variable "temperature" and print it on the screen.
- ini file: where we set the instructions for DUNE, for testing our tasks.
Assume DH> = $home/dune
First, we start by creating the simple HelloWorld task. The general structure of creating a task is
DH> python programs/scripts/dune-create-task.py DH> AuthorName NameOfTheTask
where, executing the python script 'dune-create-task.py' located in the DH>programs/scripts will create a task with task name "NameOfTheTask" in the Dune home directory (DH>). The AuthorName is to identify the author who is creating the task. The task will be created in the source directory DH>src/
For example, executing the following command
DH> python programs/scripts/dune-create-task.py . DuneAuthor HelloWorld
will generate the output
Created DUNE task in './src/HelloWorld'
Producer
Then we open the file in a text editor and copy the following code
```sh
#include <DUNE/DUNE.hpp>
namespace Workshop
{
namespace Producer
{
using DUNE_NAMESPACES;
struct Task: public DUNE::Tasks::Periodic
{
Task(const std::string& name, Tasks::Context& ctx):
DUNE::Tasks::Periodic(name, ctx)
{
}
void
onUpdateParameters(void)
{
}
void
onEntityReservation(void)
{
}
void
onEntityResolution(void)
{
}
void
onResourceAcquisition(void)
{
}
void
onResourceInitialization(void)
{
}
void
onResourceRelease(void)
{
}
void
task(void)
{
IMC::Temperature temperature;
temperature.setSourceEntity(getEntityId());
temperature.value = 45.0;
dispatch(temperature);
}
};
}
}
DUNE_TASK
In the same way, we create the task by
./dune-create-task -p ../dune/src -o 'Ricardo Martins' -t Workshop -n
Consumer
Again we copy the following code in a text editor
#include <DUNE/DUNE.hpp>
namespace Workshop
{
namespace Consumer
{
using DUNE_NAMESPACES;
struct Task: public DUNE::Tasks::Task
{
Task(const std::string& name, Tasks::Context& ctx):
DUNE::Tasks::Task(name, ctx)
{
bind<IMC::Temperature>(this);
}
void
onUpdateParameters(void)
{
}
void
onEntityReservation(void)
{
}
void
onEntityResolution(void)
{
}
void
onResourceAcquisition(void)
{
}
void
onResourceInitialization(void)
{
}
void
onResourceRelease(void)
{
}
void
consume(const IMC::Temperature* msg)
{
inf("temperature is %f", msg->value);
}
void
onMain(void)
{
while (!stopping())
{
waitForMessages(1.0);
}
}
};
}
}
DUNE_TASK
Because we changed the files, we have to rebuild the list of tasks before compiling
make rebuild_chache
Now, we can compile
make
Create the file ../dune/etc/development/workshop.ini which has the following lines
[Include ../common/transports.ini]
[Workshop.Producer]
Enabled = Always
Entity Label = Producer
[Workshop.Consumer]
Enabled = Always
Entity Label = Consumer
[Transports.Logging]
Enabled = Always
Entity Label = Logger
Transports = Temperature
then run
./dune –c development/workshop