Skip to content

Commit

Permalink
move to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Sep 19, 2024
1 parent f119c45 commit edf0204
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 325 deletions.
9 changes: 6 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ version = "0.2.0"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
JuliaSimCompiler = "8391cb6b-4921-5777-4e45-fd9aab8cb88d"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
Expand All @@ -21,9 +18,15 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"


[extensions]
Render = ["Makie"]
URDF = ["LightXML", "Graphs", "MetaGraphsNext", "JuliaFormatter"]

[compat]
CoordinateTransformations = "0.6"
Expand Down
6 changes: 3 additions & 3 deletions docs/src/urdf.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# URDF import

Multibody.jl supports import of [URDF files](https://wiki.ros.org/urdf) by means of the function [`urdf2multibody`](@ref).
The functionality requires the user to install and load the packages LightXML.jl, Graphs.jl, MetaGraphs.jl and JuliaFormatter.jl, e.g.,
The functionality requires the user to install and load the packages LightXML.jl, Graphs.jl, MetaGraphsNext.jl and JuliaFormatter.jl, e.g.,
```julia
using Pkg
Pkg.add([
"LightXML",
"Graphs",
"MetaGraphs",
"MetaGraphsNext",
"JuliaFormatter"
])
using LightXML, Graphs, MetaGraphs, JuliaFormatter
using Multibody, LightXML, Graphs, MetaGraphsNext, JuliaFormatter
```


Expand Down
4 changes: 4 additions & 0 deletions ext/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[deps]
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
JuliaSimCompiler = "8391cb6b-4921-5777-4e45-fd9aab8cb88d"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Multibody = "e1cad5d1-98ef-44f9-a79a-9ca4547f95b9"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Expand Down
3 changes: 3 additions & 0 deletions ext/Render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,9 @@ function rot_from_line(d)
RotMatrix{3}([x y d])
end

Multibody.render!(scene, ::typeof(URDFRevolute), sys, sol, t) = false
Multibody.render!(scene, ::typeof(URDFPrismatic), sys, sol, t) = false

# ==============================================================================
## PlanarMechanics
# ==============================================================================
Expand Down
18 changes: 18 additions & 0 deletions src/Multibody.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ A boolean indicating whether or not the component performed any rendering. Typic
"""
function render! end

"""
urdf2multibody(filename::AbstractString; extras=false, out=nothing, worldconnection = :rigid)
Translate a URDF file into a Multibody model. Only available if LightXML.jl, Graphs.jl, MetaGraphs.jl and JuliaFormatter.jl are installed and loaded byt the user.
Example usage:
```
urdf2multibody(joinpath(dirname(pathof(Multibody)), "..", "test/doublependulum.urdf"), extras=true, out="/tmp/urdf_import.jl")
```
## Keyword arguments
- `extras=false`: If `true`, the generated code will include package imports, a simulation of the model and a rendering of the model.
- `out=nothing`: If provided, the generated code will be written to this file, otherwise the string will only be returned.
- `worldconnection=:rigid`: If `:rigid`, the world frame will be connected to the root link with a rigid connection. If a joint constructor is provided, this component will be instantiated and the root link is connected to the world through this, e.g., `worldconnection = FreeMotion`, `()->Prismatic(n=[0, 1, 0])` etc.
"""
function urdf2multibody end
export urdf2multibody, URDFRevolute, URDFPrismatic

const t = let
(@independent_variables t)[1]
end
Expand Down
88 changes: 88 additions & 0 deletions src/joints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,92 @@ end
connect(revolute.frame_b, frame_b)
end

end

@component function URDFRevolute(; name, r, R, axisflange = false, kwargs...)
if R == I(3) && r == zeros(3)
return j
end

systems = @named begin
frame_a = Frame()
frame_b = Frame()
rev = Revolute(; axisflange, kwargs...)
end
if R == I(3)
@named trans = FixedTranslation(; r, render=false)
else
R = RotMatrix{3}(R)
n = rotation_axis(R)
angle = rotation_angle(R)
@named trans = FixedRotation(; r, n, angle, render=false)
end
push!(systems, trans)
connections = [
connect(frame_a, trans.frame_a)
connect(trans.frame_b, rev.frame_a)
connect(rev.frame_b, frame_b)
]
if axisflange
more_systems = @named begin
axis = Rotational.Flange()
support = Rotational.Flange()
end
systems = [systems; more_systems]
connections = [
connections
connect(axis, rev.axis)
connect(support, rev.support)
]
end
ODESystem(connections, t; systems, name)
end

@component function URDFPrismatic(; name, r, R, axisflange = false, kwargs...)
if R == I(3) && r == zeros(3)
return j
end

systems = @named begin
frame_a = Frame()
frame_b = Frame()
rev = Prismatic(; axisflange, kwargs...)
end
if R == I(3)
@named trans = FixedTranslation(; r, render=false)
else
R = RotMatrix{3}(R)
n = rotation_axis(R)
angle = rotation_angle(R)
@named trans = FixedRotation(; r, n, angle, render=false)
end
push!(systems, trans)
connections = [
connect(frame_a, trans.frame_a)
connect(trans.frame_b, rev.frame_a)
connect(rev.frame_b, frame_b)
]
if axisflange
more_systems = @named begin
axis = Rotational.Flange()
support = Rotational.Flange()
end
systems = [systems; more_systems]
connections = [
connections
connect(axis, rev.axis)
connect(support, rev.support)
]
end
ODESystem(connections, t; systems, name)
end

@mtkmodel NullJoint begin
@components begin
frame_a = Frame()
frame_b = Frame()
end
@equations begin
connect(frame_a, frame_b)
end
end
Loading

0 comments on commit edf0204

Please sign in to comment.