Skip to content

Commit 78b64b5

Browse files
committed
Merge pull request #12 from schmrlng/pull-request/19c0a927
Add getindex method for accessing the member variables of ROS types
2 parents 25d166d + 5780d11 commit 78b64b5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ method is not overloaded for objects. In that case, you must call the
153153
`get_param`, `set_param`, `has_param`, and `delete_param` are all implemented
154154
in the `RobotOS` module with the same syntax as in rospy.
155155

156+
### Message Constants
157+
Message constants may be accessed using `getindex` syntax. For example for [visualization_msgs/Marker.msg](http://docs.ros.org/api/visualization_msgs/html/msg/Marker.html) we have:
158+
159+
import visualization_msgs.msg: Marker
160+
Marker[:SPHERE] == getindex(Marker, :SPHERE) == 2 # true
161+
156162
## ROS Integration
157163

158164
Since Julia code needs no prior compilation, it is possible to integrate very

src/gentypes.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function modulecode(mod::ROSModule)
294294
push!(modcode,
295295
quote
296296
using PyCall
297-
import Base.convert
297+
import Base: convert, getindex
298298
import RobotOS
299299
import RobotOS.Time
300300
import RobotOS.Duration
@@ -406,6 +406,7 @@ end
406406
# (2) Default outer constructer with no arguments
407407
# (3) convert(PyObject, ...)
408408
# (4) convert(..., o::PyObject)
409+
# (5) getindex for accessing member constants
409410
function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
410411
tname = _splittypestr(rosname)[2]
411412
@debug("Type: ", tname)
@@ -454,6 +455,10 @@ function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
454455
jl
455456
end
456457
))
458+
#(5) Accessing member variables through getindex
459+
push!(exprs, :(
460+
getindex(::Type{$jlsym}, s::Symbol) = RobotOS._rospy_objects[$rosname][s]
461+
))
457462

458463
#Now add the meat to the empty expressions above
459464
for (namestr,typ) in members

test/typegeneration.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using PyCall
33
using Compat
44

55
@rosimport geometry_msgs.msg: PoseStamped, Vector3
6+
@rosimport visualization_msgs.msg: Marker
67
@rosimport std_srvs.srv.Empty
78
@rosimport nav_msgs.srv.GetPlan
89
@rosimport std_msgs.msg: Empty
@@ -53,6 +54,9 @@ pose2 = convert(geometry_msgs.msg.PoseStamped, pypose)
5354
@test pose2.pose.position.z == 3.
5455
@test_throws InexactError convert(geometry_msgs.msg.Pose, pypose)
5556

57+
#access message enum
58+
@test visualization_msgs.msg.Marker[:CUBE] == 1
59+
5660
#Proper array handling
5761
path = nav_msgs.msg.Path()
5862
@test typeof(path.poses) == Array{geometry_msgs.msg.PoseStamped,1}

0 commit comments

Comments
 (0)