@@ -39,24 +39,24 @@ const _rospy_imports = Dict{String,ROSPackage}()
39
39
const _rospy_objects = Dict {String,PyObject} ()
40
40
const _rospy_modules = Dict {String,PyObject} ()
41
41
42
- const _ros_builtin_types = Dict {String, Symbol } (
43
- " bool" => : Bool ,
44
- " int8" => : Int8 ,
45
- " int16" => : Int16 ,
46
- " int32" => : Int32 ,
47
- " int64" => : Int64 ,
48
- " uint8" => : UInt8 ,
49
- " uint16" => : UInt16 ,
50
- " uint32" => : UInt32 ,
51
- " uint64" => : UInt64 ,
52
- " float32" => : Float32 ,
53
- " float64" => : Float64 ,
54
- " string" => : String ,
55
- " time" => : Time ,
56
- " duration" => : Duration ,
42
+ const _ros_builtin_types = Dict {String,DataType } (
43
+ " bool" => Bool,
44
+ " int8" => Int8,
45
+ " int16" => Int16,
46
+ " int32" => Int32,
47
+ " int64" => Int64,
48
+ " uint8" => UInt8,
49
+ " uint16" => UInt16,
50
+ " uint32" => UInt32,
51
+ " uint64" => UInt64,
52
+ " float32" => Float32,
53
+ " float64" => Float64,
54
+ " string" => String,
55
+ " time" => Time,
56
+ " duration" => Duration,
57
57
# Deprecated by ROS but supported here
58
- " char" => : UInt8 ,
59
- " byte" => : Int8 ,
58
+ " char" => UInt8,
59
+ " byte" => Int8,
60
60
)
61
61
62
62
# Abstract supertypes of all generated types
@@ -143,18 +143,18 @@ function _rosimport(package::String, ismsg::Bool, names::String...)
143
143
end
144
144
145
145
"""
146
- rostypegen()
146
+ rostypegen(rosrootmod::Module=Main )
147
147
148
- Initiate the Julia type generation process after importing some ROS types. Creates modules in `Main`
149
- with the same behavior as imported ROS modules in python. Should only be called once, after all
150
- `@rosimport` statements are done.
148
+ Initiate the Julia type generation process after importing some ROS types. Creates modules in
149
+ rootrosmod (default is `Main`) with the same behavior as imported ROS modules in python.
150
+ Should only be called once, after all `@rosimport` statements are done.
151
151
"""
152
- function rostypegen ()
152
+ function rostypegen (rosrootmod :: Module = Main )
153
153
global _rospy_imports
154
154
pkgdeps = _collectdeps (_rospy_imports)
155
155
pkglist = _order (pkgdeps)
156
156
for pkg in pkglist
157
- buildpackage (_rospy_imports[pkg])
157
+ buildpackage (_rospy_imports[pkg], rosrootmod )
158
158
end
159
159
end
160
160
@@ -277,38 +277,47 @@ function _import_rospy_pkg(package::String)
277
277
end
278
278
279
279
# The function that creates and fills the generated top-level modules
280
- function buildpackage (pkg:: ROSPackage )
280
+ function buildpackage (pkg:: ROSPackage , rosrootmod :: Module )
281
281
@debug (" Building package: " , _name (pkg))
282
282
283
283
# Create the top-level module for the package in Main
284
284
pkgsym = Symbol (_name (pkg))
285
- pkgcode = Expr (:toplevel , :(module ($ pkgsym) end ))
286
- Main. eval (pkgcode)
287
- pkgmod = Main. eval (pkgsym)
285
+ pkgcode = :(module ($ pkgsym) end )
286
+ pkginitcode = :(function __init__ () end )
288
287
289
288
# Add msg and srv submodules if needed
290
289
@debug_addindent
291
290
if length (pkg. msg. members) > 0
292
291
msgmod = :(module msg end )
293
- msgcode = modulecode (pkg. msg)
292
+ msgcode = modulecode (pkg. msg, rosrootmod )
294
293
for expr in msgcode
295
294
push! (msgmod. args[3 ]. args, expr)
296
295
end
297
- eval (pkgmod, msgmod)
296
+ push! (pkgcode. args[3 ]. args, msgmod)
297
+ for typ in pkg. msg. members
298
+ push! (pkginitcode. args[2 ]. args, :(@rosimport $ (pkgsym). msg: $ (Symbol (typ))))
299
+ end
298
300
end
299
301
if length (pkg. srv. members) > 0
300
302
srvmod = :(module srv end )
301
- srvcode = modulecode (pkg. srv)
303
+ srvcode = modulecode (pkg. srv, rosrootmod )
302
304
for expr in srvcode
303
305
push! (srvmod. args[3 ]. args, expr)
304
306
end
305
- eval (pkgmod, srvmod)
307
+ push! (pkgcode. args[3 ]. args, srvmod)
308
+ for typ in pkg. srv. members
309
+ push! (pkginitcode. args[2 ]. args, :(@rosimport $ (pkgsym). srv: $ (Symbol (typ))))
310
+ end
306
311
end
312
+ push! (pkgcode. args[3 ]. args, :(import RobotOS. @rosimport ))
313
+ push! (pkgcode. args[3 ]. args, pkginitcode)
314
+ pkgcode = Expr (:toplevel , pkgcode)
315
+ rosrootmod. eval (pkgcode)
307
316
@debug_subindent
308
317
end
309
318
310
319
# Generate all code for a .msg or .srv module
311
- function modulecode (mod:: ROSModule )
320
+ function modulecode (mod:: ROSModule , rosrootmod :: Module )
312
321
@debug (" submodule: " , _fullname (mod))
313
322
modcode = Expr[]
314
323
@@ -325,7 +334,7 @@ function modulecode(mod::ROSModule)
325
334
end
326
335
)
327
336
# Import statement specific to the module
328
- append! (modcode, _importexprs (mod))
337
+ append! (modcode, _importexprs (mod, rosrootmod ))
329
338
# The exported names
330
339
push! (modcode, _exportexpr (mod))
331
340
@@ -340,20 +349,20 @@ function modulecode(mod::ROSModule)
340
349
end
341
350
342
351
# The imports specific to each module, including dependant packages
343
- function _importexprs (mod:: ROSMsgModule )
352
+ function _importexprs (mod:: ROSMsgModule , rosrootmod :: Module )
344
353
imports = Expr[Expr (:import , :RobotOS , :AbstractMsg )]
345
354
othermods = filter (d -> d != _name (mod), mod. deps)
346
- append! (imports, [Expr (:using ,:Main ,Symbol (m),:msg ) for m in othermods])
355
+ append! (imports, [Expr (:using ,fullname (rosrootmod) ... ,Symbol (m),:msg ) for m in othermods])
347
356
imports
348
357
end
349
- function _importexprs (mod:: ROSSrvModule )
358
+ function _importexprs (mod:: ROSSrvModule , rosrootmod :: Module )
350
359
imports = Expr[
351
360
Expr (:import , :RobotOS , :AbstractSrv ),
352
361
Expr (:import , :RobotOS , :AbstractService ),
353
362
Expr (:import , :RobotOS , :_srv_reqtype ),
354
363
Expr (:import , :RobotOS , :_srv_resptype ),
355
364
]
356
- append! (imports, [Expr (:using ,:Main ,Symbol (m),:msg ) for m in mod. deps])
365
+ append! (imports, [Expr (:using ,fullname (rosrootmod) ... ,Symbol (m),:msg ) for m in mod. deps])
357
366
imports
358
367
end
359
368
@@ -519,7 +528,7 @@ function _addtypemember!(exprs, namestr, typestr)
519
528
end
520
529
j_typ = _ros_builtin_types[typestr]
521
530
# Compute the default value now
522
- j_def = @eval _typedefault ($ j_typ)
531
+ j_def = _typedefault (j_typ)
523
532
end
524
533
525
534
namesym = Symbol (namestr)
0 commit comments