std::vector of commands

Hello,

I wish to create a std::vector of orocos-commands, so that I can
successively execute all the commands in that vector from a periodic TC.

The updateHook() will execute/dispatch the first command, then
successive iterations will check if the command is done, and if it is
done, the next command will be executed. This will continue until all
the commands are executed.

All the commands in the vector may not have the same signature. So for
example, I can't define my vector as

std::vector< RTT::Command<bool(int,int)> > CmdVec;

because all the commands might not take 2 int's as arguments.

So how can I create a vector of commands having any signature? Is there
a better way to successively execute commands in other TC's, where the
next command is executed only after the previous one is done? I thought
maybe I can use

std::vector< RTT::DispatchInterface* > CmdVec;

but I am not at all sure that is the right approach.

Can I use RTT::CommandRepository instead of a std::vector? If yes, could
someone guide me on how to use it for my purpose? There doesn't seem to
be a way to check the number of commands in a RTT::CommandRepository and
execute them one after another.

Thanks in advance,
Sagar

std::vector of commands

On Wed, Oct 28, 2009 at 18:51, Sagar Behere <sagar [dot] behere [..] ...> wrote:
> Hello,
>
> I wish to create a std::vector of orocos-commands, so that I can
> successively execute all the commands in that vector from a periodic TC.

Makes sense.

>
> The updateHook() will execute/dispatch the first command, then
> successive iterations will check if the command is done, and if it is
> done, the next command will be executed. This will continue until all
> the commands are executed.
>
> All the commands in the vector may not have the same signature. So for
> example, I can't define my vector as
>
> std::vector< RTT::Command<bool(int,int)> > CmdVec;

Now it gets harder...

>
> because all the commands might not take 2 int's as arguments.
>
> So how can I create a vector of commands having any signature? Is there
> a better way to successively execute commands in other TC's, where the
> next command is executed only after the previous one is done? I thought
> maybe I can use
>
> std::vector< RTT::DispatchInterface* > CmdVec;
>
> but I am not at all sure that is the right approach.
>
> Can I use RTT::CommandRepository instead of a std::vector? If yes, could
> someone guide me on how to use it for my purpose? There doesn't seem to
> be a way to check the number of commands in a RTT::CommandRepository and
> execute them one after another.

CommandRepository is for RTT's use only. It's not designed to help users.

The question is not only how to store this in a std::vector, but also
how you will invoke each command and povide it's arguments. One way of
doing that is to use the CommandC object

You can use it like this:

&#10;int x,y;&#10;&#10;CommandC c1( peer-&gt;commands(), &quot;command1&quot;);&#10;c1.arg( x).arg(y); // Provide first and second argument to command1 by&#10;*reference*&#10;&#10;my_vect.push_back(c1); // this makes a copy of c1, so you can dispose c1&#10;CommandC c2( peer-&gt;commands(), &quot;command2&quot;);&#10;&#10;c2.argC( 3.0 ).argC(&quot;string&quot;); // provides args by *value* (copy is made).&#10;my_vect.push_back(c2); //  etc...&#10;&#10;//And finally in update hook, iterate over the vector and call each time:&#10;x =  -1; y = 5;&#10;it-&gt;execute();&#10;

which will: use the value given in argC() elements (C stands for
constant, or copy) OR read the variable given in the arg() elements.

Let us know if this works for you.

Peter

std::vector of commands

On Fri, Oct 30, 2009 at 1:43 PM, Peter Soetens <peter [..] ...>wrote:

> On Wed, Oct 28, 2009 at 18:51, Sagar Behere <sagar [dot] behere [..] ...>
> wrote:
> > Hello,
> >
> > I wish to create a std::vector of orocos-commands, so that I can
> > successively execute all the commands in that vector from a periodic TC.
>
> Makes sense.
>
> >
> > The updateHook() will execute/dispatch the first command, then
> > successive iterations will check if the command is done, and if it is
> > done, the next command will be executed. This will continue until all
> > the commands are executed.
> >
> > All the commands in the vector may not have the same signature. So for
> > example, I can't define my vector as
> >
> > std::vector< RTT::Command<bool(int,int)> > CmdVec;
>
> Now it gets harder...
>

One possible solution to your problem is to use a "generalized union", such
as what boost::variant implements [1]. It's basically a container for
heterogeneous objects, and you can use the visitor pattern to "do something"
to the contained data (boost's implementation already provides means to do
this).

Hope this helps, however, note that I am not familiar with the internal of
boost::variant, nor the details of your particular use case, so maybe
there's a gotcha somewhere that could make its use prohibitive to you.

[1] http://www.boost.org/doc/libs/1_40_0/doc/html/variant.html

Adolfo.

>
> >
> > because all the commands might not take 2 int's as arguments.
> >
> > So how can I create a vector of commands having any signature? Is there
> > a better way to successively execute commands in other TC's, where the
> > next command is executed only after the previous one is done? I thought
> > maybe I can use
> >
> > std::vector< RTT::DispatchInterface* > CmdVec;
> >
> > but I am not at all sure that is the right approach.
> >
> > Can I use RTT::CommandRepository instead of a std::vector? If yes, could
> > someone guide me on how to use it for my purpose? There doesn't seem to
> > be a way to check the number of commands in a RTT::CommandRepository and
> > execute them one after another.
>
> CommandRepository is for RTT's use only. It's not designed to help users.
>
> The question is not only how to store this in a std::vector, but also
> how you will invoke each command and povide it's arguments. One way of
> doing that is to use the CommandC object
>
> You can use it like this:
>
> &#10;&gt; int x,y;&#10;&gt;&#10;&gt; CommandC c1( peer-&gt;commands(), &quot;command1&quot;);&#10;&gt; c1.arg( x).arg(y); // Provide first and second argument to command1 by&#10;&gt; *reference*&#10;&gt;&#10;&gt; my_vect.push_back(c1); // this makes a copy of c1, so you can dispose c1&#10;&gt; CommandC c2( peer-&gt;commands(), &quot;command2&quot;);&#10;&gt;&#10;&gt; c2.argC( 3.0 ).argC(&quot;string&quot;); // provides args by *value* (copy is made).&#10;&gt; my_vect.push_back(c2); //  etc...&#10;&gt;&#10;&gt; //And finally in update hook, iterate over the vector and call each time:&#10;&gt; x =  -1; y = 5;&#10;&gt; it-&gt;execute();&#10;&gt;
>
> which will: use the value given in argC() elements (C stands for
> constant, or copy) OR read the variable given in the arg() elements.
>
> Let us know if this works for you.
>
> Peter
> --
> Orocos-Users mailing list
> Orocos-Users [..] ...
> http://lists.mech.kuleuven.be/mailman/listinfo/orocos-users
>