/
write.jl
182 lines (151 loc) · 6.79 KB
/
write.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function set_vector_attribute(element::XMLElement, attr::AbstractString, vec::AbstractVector)
set_attribute(element, attr, join(vec, ' '))
end
function to_urdf(body::RigidBody)
xml_link = new_element("link")
set_attribute(xml_link, "name", string(body))
isroot = !has_defined_inertia(body)
if !isroot
xml_inertial = new_child(xml_link, "inertial")
xml_origin = new_child(xml_inertial, "origin")
xml_mass = new_child(xml_inertial, "mass")
xml_inertia = new_child(xml_inertial, "inertia")
inertia = spatial_inertia(body)
if inertia.mass > 0
origin = center_of_mass(inertia)
centroidal = CartesianFrame3D()
to_centroidal_frame = Transform3D(inertia.frame, centroidal, -origin.v)
inertia = transform(inertia, to_centroidal_frame)
@assert center_of_mass(inertia) ≈ Point3D(centroidal, zero(typeof(origin.v)))
else
origin = zero(center_of_mass(inertia))
end
set_vector_attribute(xml_origin, "xyz", origin.v)
set_vector_attribute(xml_origin, "rpy", zero(origin.v))
set_attribute(xml_mass, "value", inertia.mass)
set_attribute(xml_inertia, "ixx", inertia.moment[1, 1])
set_attribute(xml_inertia, "ixy", inertia.moment[1, 2])
set_attribute(xml_inertia, "ixz", inertia.moment[1, 3])
set_attribute(xml_inertia, "iyy", inertia.moment[2, 2])
set_attribute(xml_inertia, "iyz", inertia.moment[2, 3])
set_attribute(xml_inertia, "izz", inertia.moment[3, 3])
end
xml_link
end
function process_joint_type!(xml_joint::XMLElement, joint::Joint)
if isfloating(joint)
# handle this here instead of using dispatch to support user-defined
# floating joint types out of the box
set_attribute(xml_joint, "type", "floating")
else
throw(ArgumentError("Joint type $(typeof(joint_type(joint))) not handled."))
end
xml_joint
end
function process_joint_type!(xml_joint::XMLElement, joint::Joint{<:Any, <:Fixed})
set_attribute(xml_joint, "type", "fixed")
xml_joint
end
function process_joint_type!(xml_joint::XMLElement, joint::Joint{<:Any, <:Planar})
set_attribute(xml_joint, "type", "planar")
jtype = joint_type(joint)
xml_axis = new_child(xml_joint, "axis")
set_vector_attribute(xml_axis, "xyz", jtype.x_axis × jtype.y_axis)
xml_joint
end
function process_joint_type!(xml_joint::XMLElement, joint::Joint{T, JT}) where {T, JT<:Union{Revolute, Prismatic}}
jtype = joint_type(joint)
xml_axis = new_child(xml_joint, "axis")
set_vector_attribute(xml_axis, "xyz", jtype.axis)
qbounds, vbounds, τbounds = position_bounds(joint), velocity_bounds(joint), effort_bounds(joint)
@assert length(qbounds) == length(vbounds) == length(τbounds) == 1
qbound, vbound, τbound = qbounds[1], vbounds[1], τbounds[1]
@assert upper(vbound) == -lower(vbound)
@assert upper(τbound) == -lower(τbound)
realline = Bounds(-typemax(T), typemax(T))
xml_limit = new_child(xml_joint, "limit")
set_position_limits = true
if JT <: Revolute
if qbound == realline
set_attribute(xml_joint, "type", "continuous")
set_position_limits = false # continuous joints don't allow `lower` and `upper`
else
set_attribute(xml_joint, "type", "revolute")
end
else # Prismatic
set_attribute(xml_joint, "type", "prismatic")
end
if set_position_limits
set_attribute(xml_limit, "lower", lower(qbound))
set_attribute(xml_limit, "upper", upper(qbound))
end
set_attribute(xml_limit, "effort", upper(τbound))
set_attribute(xml_limit, "velocity", upper(vbound))
xml_joint
end
function to_urdf(joint::Joint, mechanism::Mechanism)
parent = predecessor(joint, mechanism)
child = successor(joint, mechanism)
to_parent = joint_to_predecessor(joint)
xyz = translation(to_parent)
rpy = RotZYX(rotation(to_parent))
xml_joint = new_element("joint")
set_attribute(xml_joint, "name", string(joint))
xml_parent = new_child(xml_joint, "parent")
set_attribute(xml_parent, "link", string(parent))
xml_child = new_child(xml_joint, "child")
set_attribute(xml_child, "link", string(child))
xml_origin = new_child(xml_joint, "origin")
set_vector_attribute(xml_origin, "xyz", xyz)
set_vector_attribute(xml_origin, "rpy", [rpy.theta3, rpy.theta2, rpy.theta1])
process_joint_type!(xml_joint, joint)
xml_joint
end
function to_urdf(mechanism::Mechanism; robot_name::Union{Nothing, AbstractString}=nothing, include_root::Bool=true)
@assert !has_loops(mechanism)
xdoc = XMLDocument()
xroot = create_root(xdoc, "robot")
if robot_name !== nothing
set_attribute(xroot, "name", robot_name)
end
bodies_to_include = include_root ? bodies(mechanism) : non_root_bodies(mechanism)
for body in bodies(mechanism)
!include_root && isroot(body, mechanism) && continue
add_child(xroot, to_urdf(body))
end
for joint in tree_joints(mechanism)
!include_root && isroot(predecessor(joint, mechanism), mechanism) && continue
add_child(xroot, to_urdf(joint, mechanism))
end
xdoc
end
"""
Serialize a `Mechanism` to the [URDF](https://wiki.ros.org/urdf/XML/model) file format.
Limitations:
* for `<link>` tags, only the `<inertial>` tag is written; there is no support for `<visual>` and `<collision>` tags.
* for `<joint>` tags, only the `<origin>`, `<parent>`, `<child>`, and `<limit>` tags are written. There is no support for the `<calibration>` and `<safety_controller>` tags.
These limitations are simply due to the fact that `Mechanism`s do not store the required information to write these tags.
Keyword arguments:
* `robot_name`: used to set the `name` attribute of the root `<robot>` tag in the URDF. Default: `nothing` (name attribute will not be set).
* `include_root`: whether to include `root_body(mechanism)` in the URDF. If `false`, joints with `root_body(mechanism)` as their predecessor will also be omitted. Default: `true`.
"""
function write_urdf end
const write_urdf_name_kwarg_doc = "Optionally, the `robot_name` keyword argument can be used to specify the robot's name."
"""
$(SIGNATURES)
Write a URDF representation of `mechanism` to the stream `io` (a `Base.IO`).
$write_urdf_name_kwarg_doc
"""
function write_urdf(io::IO, mechanism::Mechanism; robot_name=nothing, include_root=true)
show(io, to_urdf(mechanism; robot_name=robot_name, include_root=include_root))
end
"""
$(SIGNATURES)
Write a URDF representation of `mechanism` to a file.
$write_urdf_name_kwarg_doc
"""
function write_urdf(filename::AbstractString, mechanism::Mechanism; robot_name=nothing, include_root=true)
open(filename, "w") do io
write_urdf(io, mechanism, robot_name=robot_name, include_root=include_root)
end
end