Creating a new command

Introduction

Command (dynamicgraph::command::Command) are objects that encapsulate an action to be performed on an entity.

In this page, we define a new command that will call method InvertedPendulum::incr. The source code is in src/command-increment.hh.

Implementation

We first define the new class by deriving dynamicgraph::command::Command:

namespace dynamicgraph {
namespace tutorial {
namespace command {
class Increment : public Command
{

The constructor takes

We then define the action of the command in virtual method doExecute. We need to get a reference to the object on which the command will act. Note that we can straightfowardly statically cast in InvertedPendulum the Entity object returned by method owner:

virtual Value doExecute()
{
Entity& entity = owner();
InvertedPendulum& ip = static_cast<InvertedPendulum&>(entity);

We then get the parameters as a vector of dynamicgraph::command::Value objects and cast them into the appropriate types specified at construction:

std::vector<Value> values = getParameterValues();
double timeStep = values[0].value();

Finally, we execute the action and return a value of the appropriate type, here the command return no value:

ip.incr(timeStep);
return Value();
}


dynamic-graph-tutorial
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:37:04