Periodic Activity

Hi all, I have an periodic activity running on my app. and I'd like to
change it's period during execution time in order to set an PWM on my
board. Is it possible?

Thanks,
Breno

Periodic Activity

On Fri, Nov 27, 2009 at 14:16, <breno [..] ...> wrote:
> Hi all, I have an periodic activity running on my app. and I'd like to
> change it's period during execution time in order to set an PWM on my
> board. Is it possible?

Hi Breno,

The best way to do this is to use an RTT::Activity, which can change
its frequency during run-time (RTT 1.10). Unfortunately (as discussed
earlier on this list), you need to change the frequency of the
underlying thread, instead of on the activity object itself. From
within a TaskContext, write:

&#10;this-&gt;getActivity()-&gt;thread()-&gt;setPeriod( new_period_in_sec );&#10;

Would that work for you ?

Peter

Periodic Activity

> On Fri, Nov 27, 2009 at 14:16, <breno [..] ...> wrote:
>> Hi all, I have an periodic activity running on my app. and I'd like to
>> change it's period during execution time in order to set an PWM on my
>> board. Is it possible?
>
> Hi Breno,
>
> The best way to do this is to use an RTT::Activity, which can change
> its frequency during run-time (RTT 1.10). Unfortunately (as discussed
> earlier on this list), you need to change the frequency of the
> underlying thread, instead of on the activity object itself. From
> within a TaskContext, write:
>
> &#10;&gt; this-&gt;getActivity()-&gt;thread()-&gt;setPeriod( new_period_in_sec );&#10;&gt;
>
> Would that work for you ?
>
> Peter
>

Hi, thanks for your support!!!

I did an simple example in order to test some commands from API.

I'm posting my code below, I hope you get it through. If you have any
suggestion to do it in a better way let me know, please.

CODE START:

class AtividadePeriodica
: public Activity
{
protected:

public :AtividadePeriodica()
: Activity(1,5)
{
}

void initialized() {
// Your algorithm for periodic execution goes inhere
log(Info) << "Initialized" <<endlog();
}
void step() {
// Your algorithm for periodic execution goes inhere
log(Info) << "Step" <<endlog();
}

};

class World
: public RTT::TaskContext
{
protected:
Method<void(double)> Changer;

void changeperiod(double period)
{
activity->setPeriod(period);
}

private:
AtividadePeriodica * activity;

public:World(std::string name)
: TaskContext(name, PreOperational),
Changer("Change", &World::changeperiod, this)
{

activity = new AtividadePeriodica();

assert( Changer.ready() );

this->methods()->addMethod(&Changer,"Method","Period","New Period");
}

bool configureHook()
{
Logger::In in("World");
// Lookup the Hello component.

//activity->setActivity( new Activity( 5, 5 ));
activity->start();

return true;

}
void updateHook()
{
activity->stop();

}

};

CODE FINISH

So then, when i call the method Change from World Browser I can change the
period. I realized also that it takes some noticeable time to get it done,
I mean, to change the period effectively.

I'm using RTT 1.10.0

Case you have any sgg,

Thanks

Breno

Periodic Activity

On Mon, Nov 30, 2009 at 19:26, <breno [..] ...> wrote:
>> On Fri, Nov 27, 2009 at 14:16,  <breno [..] ...> wrote:
>>> Hi all, I have an periodic activity running on my app. and I'd like to
>>> change it's period during execution time in order to set an PWM on my
>>> board. Is it possible?
>>
>> Hi Breno,
>>
>> The best way to do this is to use an RTT::Activity, which can change
>> its frequency during run-time (RTT 1.10). Unfortunately (as discussed
>> earlier on this list), you need to change the frequency of the
>> underlying thread, instead of on the activity object itself. From
>> within a TaskContext, write:
>>
>> &#10;&gt;&gt; this-&gt;getActivity()-&gt;thread()-&gt;setPeriod( new_period_in_sec );&#10;&gt;&gt;
>>
>> Would that work for you ?
>>
>> Peter
>>
>
>
> Hi, thanks for your support!!!
>
> I did an simple example in order to test some commands from API.
>
> I'm posting my code below, I hope you get it through. If you have any
> suggestion to do it in a better way let me know, please.

The code below is not what we had in mind. Most users don't override Activity or
implement ActivityInterface. All your code goes into updateHook() etc.
It *is* possible
as you write it, but it beats the purpose of having updateHook().
Also, the stop() in updateHook()
is very suspect, I would at least expect it in stopHook().

What you had to do was:

&#10;class MyTask : public TaskContext&#10;{&#10;protected:&#10;        Method&lt;void(double)&gt; Changer;&#10;&#10;        void changeperiod(double period)&#10;                 {&#10;                         this-&gt;getActivity()-&gt;thread()-&gt;setPeriod(period);&#10;                 }&#10;public:&#10;     MyTask() : TaskContext(&quot;MyTask&quot;),&#10;        Changer(&quot;Change&quot;, &amp;World::changeperiod, this)&#10;    {&#10;           this-&gt;methods()-&gt;addMethod(&amp;Changer,&quot;Method&quot;,&quot;Period&quot;,&quot;New Period&quot;);&#10;           this-&gt;setActivity( new Activity(ORO_SCHED_RT,5) ); // priority=5&#10;    }&#10;&#10;    void updateHook()&#10;    {&#10;                // your code for periodic execution goes here.&#10;    }&#10;};&#10;&#10;

On some operating systems, the new period is only picked up after the
sleeping is done. So you get most of the time a delay of 1 'old'
sample. We probably can improve this in the fosi/Thread.cpp
implementation.

Peter

Periodic Activity

> On Mon, Nov 30, 2009 at 19:26, <breno [..] ...> wrote:
>>> On Fri, Nov 27, 2009 at 14:16,  <breno [..] ...> wrote:
>>>> Hi all, I have an periodic activity running on my app. and I'd like to
>>>> change it's period during execution time in order to set an PWM on my
>>>> board. Is it possible?
>>>
>>> Hi Breno,
>>>
>>> The best way to do this is to use an RTT::Activity, which can change
>>> its frequency during run-time (RTT 1.10). Unfortunately (as discussed
>>> earlier on this list), you need to change the frequency of the
>>> underlying thread, instead of on the activity object itself. From
>>> within a TaskContext, write:
>>>
>>> &#10;&gt;&gt;&gt; this-&gt;getActivity()-&gt;thread()-&gt;setPeriod( new_period_in_sec );&#10;&gt;&gt;&gt;
>>>
>>> Would that work for you ?
>>>
>>> Peter
>>>
>>
>>
>> Hi, thanks for your support!!!
>>
>> I did an simple example in order to test some commands from API.
>>
>> I'm posting my code below, I hope you get it through. If you have any
>> suggestion to do it in a better way let me know, please.
>
> The code below is not what we had in mind. Most users don't override
> Activity or
> implement ActivityInterface. All your code goes into updateHook() etc.
> It *is* possible
> as you write it, but it beats the purpose of having updateHook().
> Also, the stop() in updateHook()
> is very suspect, I would at least expect it in stopHook().
>
> What you had to do was:
>
> &#10;&gt; class MyTask : public TaskContext&#10;&gt; {&#10;&gt; protected:&#10;&gt;         Method&lt;void(double)&gt; Changer;&#10;&gt;&#10;&gt;         void changeperiod(double period)&#10;&gt;                  {&#10;&gt;                          this-&gt;getActivity()-&gt;thread()-&gt;setPeriod(period);&#10;&gt;                  }&#10;&gt; public:&#10;&gt;      MyTask() : TaskContext(&quot;MyTask&quot;),&#10;&gt;         Changer(&quot;Change&quot;, &amp;World::changeperiod, this)&#10;&gt;     {&#10;&gt;            this-&gt;methods()-&gt;addMethod(&amp;Changer,&quot;Method&quot;,&quot;Period&quot;,&quot;New&#10;&gt; Period&quot;);&#10;&gt;            this-&gt;setActivity( new Activity(ORO_SCHED_RT,5) ); //&#10;&gt; priority=5&#10;&gt;     }&#10;&gt;&#10;&gt;     void updateHook()&#10;&gt;     {&#10;&gt;                 // your code for periodic execution goes here.&#10;&gt;     }&#10;&gt; };&#10;&gt;&#10;&gt;
>
> On some operating systems, the new period is only picked up after the
> sleeping is done. So you get most of the time a delay of 1 'old'
> sample. We probably can improve this in the fosi/Thread.cpp
> implementation.
>
> Peter
>
Absolutely perfect!!! Thanks a lot for all observations!

Breno