Skip to content

Commit 5de5e09

Browse files
committed
Change generation of messages that conflict with built-ins
Closes #7
1 parent 7ddd2af commit 5de5e09

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ generated type names into the local namespace.
5555
p = Path()
5656
v = Vector3(1.1,2.2,3.3)
5757

58+
There is one special case, where the ROS type name conflicts with a built-in
59+
Julia type name (e.g., `std_msgs/Float64` or `std_msgs/String`). In these
60+
cases, the generated Julia type will have "Msg" appended to the name for
61+
disambiguation (e.g., `std_msgs.msg.Float64Msg` and `std_msgs.msg.StringMsg`).
62+
5863
An additional function, `rostypereset()`, resets the type generation process,
5964
possibly useful for development in the REPL. When invoked, new `@rosimport`
6065
calls will be needed to generate the same or different types, and previously

src/gentypes.jl

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ end
214214
function _importdeps!(mod::ROSModule, deps::Vector)
215215
global _rospy_imports
216216
for d in deps
217-
if ! (d in keys(_ros_builtin_types))
217+
#We don't care about array types when doing dependency resolution
218+
dclean = _check_array_type(d)[1]
219+
if ! (dclean in keys(_ros_builtin_types))
218220
@debug("Dependency: ", d)
219-
pkgname, typename = _splittypestr(d)
221+
pkgname, typename = _splittypestr(dclean)
220222

221223
@debug_addindent
222224
#Create a new ROSPackage if needed
@@ -230,9 +232,7 @@ function _importdeps!(mod::ROSModule, deps::Vector)
230232
#pushing to a set does not create duplicates
231233
push!(mod.deps, depmod.name)
232234

233-
#Don't want to include the brackets in the string if present
234-
typeclean = _check_array_type(typename)[1]
235-
addtype!(depmod, typeclean)
235+
addtype!(depmod, typename)
236236
@debug_subindent
237237
end
238238
end
@@ -347,8 +347,13 @@ end
347347

348348
#The exported names for each module
349349
function _exportexpr(mod::ROSMsgModule)
350-
exports = [symbol(m) for m in mod.members]
351-
Expr(:export, exports...)
350+
exportexpr = Expr(:export)
351+
for m in mod.members
352+
push!(exportexpr.args, _nameconflicts(m) ?
353+
symbol(string(m,"Msg")) :
354+
symbol(m))
355+
end
356+
exportexpr
352357
end
353358
function _exportexpr(mod::ROSSrvModule)
354359
exportexpr = Expr(:export)
@@ -371,6 +376,11 @@ function buildtype(mod::ROSMsgModule, typename::String)
371376
memtypes = pyobj[:_slot_types]
372377
members = collect(zip(memnames, memtypes))
373378

379+
#Some ROS message names conflict with Julia built-in types
380+
#Append 'Msg' to the type name to resolve
381+
if _nameconflicts(typename)
382+
fulltypestr = fulltypestr * "Msg"
383+
end
374384
typecode(fulltypestr, :MsgT, members)
375385
end
376386

@@ -610,6 +620,17 @@ function _get_rospy_class(typ::DataType)
610620
rospycls
611621
end
612622

623+
#Check if the type name conflicts with a Julia builtin. Currently this is only
624+
#some of the messages from the std_msgs.msg package
625+
function _nameconflicts(typename::String)
626+
try
627+
eval(Base, symbol(typename))
628+
true
629+
catch
630+
false
631+
end
632+
end
633+
613634
#Get a default value for any builtin ROS type
614635
_typedefault{T<:Real}(::Type{T}) = zero(T)
615636
_typedefault(::Type{ASCIIString}) = ""

test/typegeneration.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Compat
66
@rosimport geometry_msgs.msg: PoseStamped, Vector3
77
@rosimport std_srvs.srv.Empty
88
@rosimport nav_msgs.srv.GetPlan
9+
@rosimport std_msgs.msg: Float64, String, Int8MultiArray
910
rostypegen()
1011

1112
@test isdefined(:geometry_msgs)
@@ -59,6 +60,12 @@ path2 = convert(nav_msgs.msg.Path, pypath)
5960
@test path2.poses[1].pose.position.y == 2.
6061
@test path2.poses[1].pose.position.z == 3.
6162

62-
#Empty message
63+
#Issue #6 - Empty message
6364
emptymsg = std_msgs.msg.Empty()
6465
@test length(fieldnames(emptymsg)) == 0
66+
67+
#Issue #7 - Renaming conflicting message types
68+
@test isdefined(std_msgs.msg, :Float64Msg)
69+
@test isdefined(std_msgs.msg, :StringMsg)
70+
intarr = std_msgs.msg.Int8MultiArray()
71+
@test typeof(intarr.data) == Vector{Int8}

0 commit comments

Comments
 (0)