-
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 two parts in this exercise:
-
Create simple tasks to produce a variable (representing some sensor input), consume a variable (representing functions that use the sensor input), and binding these two (produce and consume) tasks for a meaningful interaction.
-
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.
and copy the following code
#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