Skip to content

Commit

Permalink
Add __init__() method to generated modules to avoid double @rosimport…
Browse files Browse the repository at this point in the history
… when precompiling results of rostypegen
  • Loading branch information
schmrlng committed Aug 4, 2018
1 parent 8d04836 commit ea29bda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
12 changes: 2 additions & 10 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ regenerated, the first version will remain.
### Compatibility with Package Precompilation
As described above, by default `rostypegen` creates modules in `Main` -- however,
this behavior is incompatible with Julia package precompilation. If you are using
`RobotOS` in the context of a module, as opposed to a script, you may reduce
`RobotOS` in your own module or package, as opposed to a script, you may reduce
load-time latency (useful for real-life applications!) by generating the ROS type
modules inside your package module using an approach similar to the example below:

Expand All @@ -87,20 +87,12 @@ modules inside your package module using an approach similar to the example belo
import .geometry_msgs.msg: Pose
# ...

function __init__()
@rosimport geometry_msgs.msg: Pose
# ...
end

end

In this case, we have provided `rostypegen` with a root module (`MyROSPackage`)
for type generation. The Julia type corresponding to `geometry_msgs/Pose` now
lives at `MyROSPackage.geometry_msgs.msg.Pose`; note the extra dot in
`import .geometry_msgs.msg: Pose`. A precompiled package using `RobotOS` must
also duplicate any `@rosimport` statements within the `__init__` function in
its module so that necessary memory/objects in the Python runtime can be
allocated each time the package is used.
`import .geometry_msgs.msg: Pose`.

## Usage: ROS API

Expand Down
17 changes: 13 additions & 4 deletions src/gentypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,13 @@ function _import_rospy_pkg(package::String)
end

#The function that creates and fills the generated top-level modules
function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
function buildpackage(pkg::ROSPackage, rosrootmod::Module)
@debug("Building package: ", _name(pkg))

#Create the top-level module for the package in Main
pkgsym = Symbol(_name(pkg))
pkgcode = :(module ($pkgsym) end)
pkginitcode = :(function __init__() end)

#Add msg and srv submodules if needed
@debug_addindent
Expand All @@ -293,6 +294,9 @@ function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
push!(msgmod.args[3].args, expr)
end
push!(pkgcode.args[3].args, msgmod)
for typ in pkg.msg.members
push!(pkginitcode.args[2].args, :(@rosimport $(pkgsym).msg: $(Symbol(typ))))
end
end
if length(pkg.srv.members) > 0
srvmod = :(module srv end)
Expand All @@ -301,14 +305,19 @@ function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
push!(srvmod.args[3].args, expr)
end
push!(pkgcode.args[3].args, srvmod)
for typ in pkg.srv.members
push!(pkginitcode.args[2].args, :(@rosimport $(pkgsym).srv: $(Symbol(typ))))
end
end
push!(pkgcode.args[3].args, :(import RobotOS.@rosimport))
push!(pkgcode.args[3].args, pkginitcode)
pkgcode = Expr(:toplevel, pkgcode)
rosrootmod.eval(pkgcode)
@debug_subindent
end

#Generate all code for a .msg or .srv module
function modulecode(mod::ROSModule, rosrootmod::Module=Main)
function modulecode(mod::ROSModule, rosrootmod::Module)
@debug("submodule: ", _fullname(mod))
modcode = Expr[]

Expand Down Expand Up @@ -340,13 +349,13 @@ function modulecode(mod::ROSModule, rosrootmod::Module=Main)
end

#The imports specific to each module, including dependant packages
function _importexprs(mod::ROSMsgModule, rosrootmod::Module=Main)
function _importexprs(mod::ROSMsgModule, rosrootmod::Module)
imports = Expr[Expr(:import, :RobotOS, :AbstractMsg)]
othermods = filter(d -> d != _name(mod), mod.deps)
append!(imports, [Expr(:using,Symbol(rosrootmod),Symbol(m),:msg) for m in othermods])
imports
end
function _importexprs(mod::ROSSrvModule, rosrootmod::Module=Main)
function _importexprs(mod::ROSSrvModule, rosrootmod::Module)
imports = Expr[
Expr(:import, :RobotOS, :AbstractSrv),
Expr(:import, :RobotOS, :AbstractService),
Expand Down

0 comments on commit ea29bda

Please sign in to comment.