load a lua script in a cpp component

Dear all,

Until now, I was specifying the behaviour of my components in the .cpp files via the "hook" (configureHook, updateHook,...) functions. But now, I would like to split the structure (still in .cpp files) and the behaviour by using scripts (.lua files for the moment) for the latter. How could I do this? I know it's possible to load a script in a component by using the following command :

this->getProvider<Scripting>("scripting")->loadPrograms("my_script.ops");

but it's very blur for me and all my attempts to exploit this way have failed. By the way, the best for me would be to connect the script and the component directly in the XML mapping file, if it makes sense.

Thank you in advance for your help.

Best regards,

Benoit

load a lua script in a cpp component

Hi Benoit,

On Thu, Feb 02, 2012 at 11:12:36PM +0000, goepfert [dot] benoit [..] ... wrote:
>
> Until now, I was specifying the behaviour of my components in the .cpp files
> via the "hook" (configureHook, updateHook,...) functions. But now, I would
> like to split the structure (still in .cpp files) and the behaviour by using
> scripts (.lua files for the moment) for the latter. How could I do this? I
> know it's possible to load a script in a component by using the following
> command :
>
> this->getProvider("scripting")->loadPrograms("my_script.ops");

This is the way to load an script written in the Orocos scripting
language. To load a Lua script in C++ component you have to use the
LuaService instead. The basic steps are:

1. Call the deployer "loadService" operation to load the "Lua" service
into the component in question

2. Call the exec_file operation of the service to execute a file in
the service (componentA:provides("Lua"):exec_file("scriptA.lua")

See this section in the Cookbook for more details:
http://www.orocos.org/wiki/orocos/toolchain/luacookbook#toc42

You can then install an ExecutionEngine Hook to be triggered at the
same frequency than the host component.

> but it's very blur for me and all my attempts to exploit this way have
> failed.
> By the way, the best for me would be to connect the script and the component
> directly in the XML mapping file, if it makes sense.

The XML doesn't support the specifics of Lua components and likely
never will. I am currently working on a "next generation" deployment
DSL to support advanced deployment that will cover this use case, but
it might take a few weeks still.

Meanwhile your only option are deployment scripts.

Best regards
Markus

load a lua script in a cpp component

2012/2/14 <goepfert [dot] benoit [..] ...>

> I've added the method loadService in my TaskContext.cpp but then, what
> should
> I do in my cpp component to load the LUA script?
>

Use the loadService to load the lua plugin, then execute the lua script
with the lua plugin "execute" function. I can't remind its name but after
loading the plugin, you can type something like "ls lua" and you'll see
it's interface.

(Btw I didn't find this API in the cookbook or API doc, where should we
look to have the lua service operation details ?)

> --
> Orocos-Users mailing list
> Orocos-Users [..] ...
> http://lists.mech.kuleuven.be/mailman/listinfo/orocos-users
>

load a lua script in a cpp component

Hi Willy and Benoit,

On Wed, Feb 15, 2012 at 09:20:46AM +0100, Willy Lambert wrote:
> 2012/2/14 <goepfert [dot] benoit [..] ...>
>
> I've added the method loadService in my TaskContext.cpp but then, what
> should
> I do in my cpp component to load the LUA script?
>
>
> Use the loadService to load the lua plugin, then execute the lua script with
> the lua plugin "execute" function. I can't remind its name but after loading
> the plugin, you can type something like "ls lua" and you'll see it's interface.
>
> (Btw I didn't find this API in the cookbook or API doc, where should we look to
> have the lua service operation details ?)

Here is an example of how the Lua service can be used:

http://www.orocos.org/wiki/orocos/toolchain/LuaCookbook#toc131

It runs a program stored in a string. If you want to execute a file in
the service, just replace:

component:provides("Lua"):exec_str(string)

with

component:provides("Lua"):exec_file(filename)

In the Lua taskbrowser you can run the following to figure out
the API of a service:

$ rttlua
OROCOS RTTLua 1.0-beta4 / Lua 5.1.4 (gnulinux)
Use Ctrl-d to quit.
> depl=rtt.getTC():getPeer("deployer") -- get deployer
> depl:loadService("deployer", "Lua") -- load the Lua Service
> =depl:provides("Lua") -- prints it's API
Service: Lua
Subservices:
Operations: exec_file, exec_str
Ports:

Assuming you have a file 'hello.lua' with the contents:
<file>
print("hello from the Lua service")
<file>

you can run:

> depl:provides("Lua"):exec_file("hello.lua")
hello from the Lua service
>

HTH
Markus

load a lua script in a cpp component

2012/2/15 Markus Klotzbuecher <markus [dot] klotzbuecher [..] ...>

> Hi Willy and Benoit,
>

Hi Markus,

>
> On Wed, Feb 15, 2012 at 09:20:46AM +0100, Willy Lambert wrote:
> > 2012/2/14 <goepfert [dot] benoit [..] ...>
> >
> > I've added the method loadService in my TaskContext.cpp but then,
> what
> > should
> > I do in my cpp component to load the LUA script?
> >
> >
> > Use the loadService to load the lua plugin, then execute the lua script
> with
> > the lua plugin "execute" function. I can't remind its name but after
> loading
> > the plugin, you can type something like "ls lua" and you'll see it's
> interface.
> >
> > (Btw I didn't find this API in the cookbook or API doc, where should we
> look to
> > have the lua service operation details ?)
>
> Here is an example of how the Lua service can be used:
>
> http://www.orocos.org/wiki/orocos/toolchain/LuaCookbook#toc131
>
>
I think that what Benoit is trying to is to call the execution of a lua
script from C++.

The first part is getting a handler on the service (it is like using the
marshalling or scripting service in C++).
The second is to call the C++ equivalent exec_str in something like the
configureHook.

But anyway Benoit, when all this will be done, you will have to put
something in this lua script and then the cookbook will help you so keep it
in your bookmarks ;p

> It runs a program stored in a string. If you want to execute a file in
> the service, just replace:
>
> component:provides("Lua"):exec_str(string)
>
> with
>
> component:provides("Lua"):exec_file(filename)
>
> In the Lua taskbrowser you can run the following to figure out
> the API of a service:
>
> $ rttlua
> OROCOS RTTLua 1.0-beta4 / Lua 5.1.4 (gnulinux)
> Use Ctrl-d to quit.
> > depl=rtt.getTC():getPeer("deployer") -- get deployer
> > depl:loadService("deployer", "Lua") -- load the Lua Service
> > =depl:provides("Lua") -- prints it's API
> Service: Lua
> Subservices:
> Operations: exec_file, exec_str
> Ports:
>
> Assuming you have a file 'hello.lua' with the contents:
> <file>
> print("hello from the Lua service")
> <file>
>
> you can run:
>
> > depl:provides("Lua"):exec_file("hello.lua")
> hello from the Lua service
> >
>
> HTH
> Markus
>
>
>
>

load a lua script in a cpp component

2012/2/3 Markus Klotzbuecher <markus [dot] klotzbuecher [..] ...>

> Hi Benoit,
>
> On Thu, Feb 02, 2012 at 11:12:36PM +0000, goepfert [dot] benoit [..] ... wrote:
> >
> > Until now, I was specifying the behaviour of my components in the .cpp
> files
> > via the "hook" (configureHook, updateHook,...) functions. But now, I
> would
> > like to split the structure (still in .cpp files) and the behaviour by
> using
> > scripts (.lua files for the moment) for the latter. How could I do this?
> I
> > know it's possible to load a script in a component by using the following
> > command :
> >
> > this->getProvider("scripting")->loadPrograms("my_script.ops");
>
> This is the way to load an script written in the Orocos scripting
> language. To load a Lua script in C++ component you have to use the
> LuaService instead. The basic steps are:
>
> 1. Call the deployer "loadService" operation to load the "Lua" service
> into the component in question
>
> 2. Call the exec_file operation of the service to execute a file in
> the service (componentA:provides("Lua"):exec_file("scriptA.lua")
>
> See this section in the Cookbook for more details:
> http://www.orocos.org/wiki/orocos/toolchain/luacookbook#toc42

Here is my deploy.ops file :
loadService ("Deployer","Lua")
Lua.exec_file("deploy.lua")

Then you can deploy your application in the deploy.lua file with the lua
syntax

>
>
> You can then install an ExecutionEngine Hook to be triggered at the
> same frequency than the host component.
>
> > but it's very blur for me and all my attempts to exploit this way have
> > failed.
> > By the way, the best for me would be to connect the script and the
> component
> > directly in the XML mapping file, if it makes sense.
>
> The XML doesn't support the specifics of Lua components and likely
> never will. I am currently working on a "next generation" deployment
> DSL to support advanced deployment that will cover this use case, but
> it might take a few weeks still.
>
> Meanwhile your only option are deployment scripts.
>
> Best regards
> Markus
>
>
>
> --
> Orocos-Users mailing list
> Orocos-Users [..] ...
> http://lists.mech.kuleuven.be/mailman/listinfo/orocos-users
>

load a lua script in a cpp component

I tried the following instructions :

In my file ComponentA.cpp : ... this->getProvider<Scripting>("scripting")->runScript("deploy.ops"); ...

and in deploy.ops (as shown by willy) : loadService("Deployer","Lua") Lua.exec_file("deploy.lua")

but it doesn't work and I get the following errors :

  1. rosrun ocl deployer-gnulinux -s connect.xml

0.186 [ ERROR ][ScriptingService] deploy.ops :Parse error at line 1: No method "loadService" registered for the object or task "ComponentA".

What's the problem?

Best regards,

Benoit