Skip to content

Commit 0f7101e

Browse files
committed
Merge branch 'main' of https://github.com/DojoSim/Dojo.jl into main
2 parents f7f4147 + 7d8dd00 commit 0f7101e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+391
-500
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://dojo-sim.github.io/Dojo.jl/dev)
44

55
# Dojo
6-
A differentiable simulator for robotics.
6+
A differentiable simulator for robotics
77

88
# Examples (RSS 2022)
99

docs/make.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Documenter, Dojo
44

55
makedocs(
66
modules = [Dojo],
7-
format = Documenter.HTML(prettyurls = false),
7+
format = Documenter.HTML(prettyurls=false),
88
sitename = "Dojo",
99
pages = [
1010
##############################################

environments/atlas/methods/env.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function atlas(;
2121
name=:robot,
2222
infeasible_control=false,
2323
opts_step=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
24-
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5)) where T
24+
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
25+
T=Float64)
2526

2627
mechanism = get_mechanism(:atlas,
2728
timestep=timestep,

environments/atlas/methods/initialize.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function initialize_atlas!(mechanism::Mechanism;
223223
return nothing
224224
end
225225

226-
function initialize_atlasstance!(mechanism::Mechanism;
226+
function initialize_atlas_stance!(mechanism::Mechanism;
227227
body_position=[0.0, 0.0, 0.2],
228228
body_orientation=[0.0, 0.0, 0.0],
229229
link_linear_velocity=[zeros(3) for i=1:length(mechanism.bodies)],

environments/box/methods/env.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ function block(;
1919
name::Symbol=:robot,
2020
control_scaling=Diagonal(ones(3)),
2121
opts_step=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
22-
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5)) where T
22+
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
23+
T=Float64)
2324

2425
mechanism = get_box(
2526
timestep=timestep,

environments/box2d/methods/env.jl renamed to environments/box2D/methods/env.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
################################################################################
22
# Box (2D)
33
################################################################################
4-
struct Box2d end
4+
struct Box2D end
55

6-
function box2d(;
6+
function box2D(;
77
representation=:minimal,
88
timestep=0.05,
99
gravity=[0.0; 0.0; -9.81],
@@ -18,12 +18,12 @@ function box2d(;
1818
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
1919
T=Float64)
2020

21-
mechanism = get_mechanism(:box2d,
21+
mechanism = get_mechanism(:box2D,
2222
timestep=timestep,
2323
gravity=gravity,
2424
friction_coefficient=friction_coefficient)
2525

26-
initialize!(mechanism, :box2d)
26+
initialize!(mechanism, :box2D)
2727

2828
if representation == :minimal
2929
nx = minimal_dimension(mechanism)

environments/box2d/methods/initialize.jl renamed to environments/box2D/methods/initialize.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function get_box2d(;
1+
function get_box2D(;
22
timestep=0.01,
33
gravity=[0.0; 0.0; -9.81],
44
friction_coefficient=0.8,
55
radius=0.0,
66
side=0.5,
77
contact=true,
88
contact_type=:nonlinear,
9-
mode=:box2d,
9+
mode=:box2D,
1010
T=Float64)
1111

1212
# Parameters
@@ -23,15 +23,15 @@ function get_box2d(;
2323
# Corner vectors
2424
if mode == :particle
2525
corners = [[0.0, 0.0, 0.0]]
26-
elseif mode == :box2d
26+
elseif mode == :box2D
2727
corners = [
2828
[[0.0, side / 2.0, side / 2.0]]
2929
[[0.0, side / 2.0, -side / 2.0]]
3030
[[0.0, -side / 2.0, side / 2.0]]
3131
[[0.0, -side / 2.0, -side / 2.0]]
3232
]
3333
else
34-
@error "incorrect mode specified, try :particle or :box2d"
34+
@error "incorrect mode specified, try :particle or :box2D"
3535
end
3636
n = length(corners)
3737
normal = [[0.0, 0.0, 1.0] for i = 1:n]
@@ -55,7 +55,7 @@ function get_box2d(;
5555
return mech
5656
end
5757

58-
function initialize_box2d!(mechanism::Mechanism{T};
58+
function initialize_box2D!(mechanism::Mechanism{T};
5959
position=[0.0, 1.0],
6060
linear_velocity=[0.0, 0.0],
6161
orientation=0.0,

environments/cartpole/methods/env.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function cartpole(;
1616
name=:robot,
1717
control_scaling=Diagonal(ones(1)),
1818
opts_step=SolverOptions(),
19-
opts_grad=SolverOptions()) where T
19+
opts_grad=SolverOptions(),
20+
T=Float64)
2021

2122
mechanism = get_cartpole(
2223
timestep=timestep,

environments/cartpole/methods/initialize.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ function get_cartpole(;
22
timestep=0.1,
33
gravity=[0.0; 0.0; -9.81],
44
spring=0.0,
5-
damper=0.0) where T
5+
damper=0.0,
6+
T=Float64)
67

78
# Parameters
89
slider_axis = [0.0; 1.0; 0.0]

environments/dzhanibekov/methods/initialize.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function get_dzhanibekov(;
22
timestep=0.01,
33
gravity=-9.81,
4-
color=RGBA(0.9,0.9,0.9,1)) where T
4+
color=RGBA(0.9,0.9,0.9,1),
5+
T=Float64)
56

67
radius = 0.1
78
body_length = 1.0

environments/include.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
include("ant/methods/initialize.jl")
55
include("atlas/methods/initialize.jl")
66
include("box/methods/initialize.jl")
7-
include("box2d/methods/initialize.jl")
7+
include("box2D/methods/initialize.jl")
88
include("cartpole/methods/initialize.jl")
99
include("dzhanibekov/methods/initialize.jl")
1010
include("fourbar/methods/initialize.jl")

environments/raiberthopper/methods/initialize.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function initialize_raiberthopper!(mech::Mechanism{T,Nn,Ne,Nb};
7979
pbody = mech.bodies[1]
8080
cbody = mech.bodies[2]
8181
joint2 = mech.joints[2]
82-
tra2 = joint2.constraints[1]
82+
tra2 = joint2.translational
8383

8484
# origin to body
8585
set_maximal_configurations!(mech.origin, pbody,

environments/rexhopper/methods/env.jl

+6-5
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ function rexhopper(;
6666
fu = zeros(nx, nu)
6767

6868
u_prev = zeros(nu)
69-
control_mask = infeasible_control ? I(nu) : cat(zeros(3,3), I(3), 1, 0, 1, zeros(5,5), dims=(1,2))
70-
motor_gear = ones(nu)
71-
control_scaling = Diagonal(motor_gear)
69+
control_map = infeasible_control ? 1.0 * I(nu) : cat(zeros(3, 3), 1.0 * I(3), 1.0, 0.0, 1.0, zeros(5, 5), dims=(1,2))
7270

7371
build_robot(mechanism, vis=vis, name=name)
7472

7573
TYPES = [RexHopper, T, typeof(mechanism), typeof(aspace), typeof(ospace), typeof(info)]
76-
env = Environment{TYPES...}(mechanism, representation, aspace, ospace,
74+
env = Environment{TYPES...}(mechanism,
75+
representation,
76+
aspace,
77+
ospace,
7778
x, fx, fu,
78-
u_prev, control_mask' * control_scaling,
79+
u_prev, control_map,
7980
nx, nu, no,
8081
info,
8182
[rng], vis,

environments/tennisracket/methods/initialize.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ function get_tennisracket(;
33
gravity=[0.0; 0.0; -9.81],
44
T=Float64)
55

6-
origin = Origin{T}(name="origin")
6+
origin = Origin{T}(name=:origin)
77
mass = 1.0
88
h = 1.0
9-
bodies = [Box(h / 25.0, h / 2, h, mass, color=RGBA(1.0, 0.0, 0.0), name=:box)]
9+
bodies = [Box(h / 25.0, h / 2.0, h, mass, color=RGBA(1.0, 0.0, 0.0), name=:box)]
1010
joints = [JointConstraint(Floating(origin, bodies[1]), name=:floating_joint)]
1111
mechanism = Mechanism(origin, bodies, joints,
1212
timestep=timestep,

environments/walker/methods/env.jl

+33-25
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@
55
"""
66
struct Walker end
77

8-
function walker(; mode::Symbol=:minimal, dt::T=0.05, gravity=[0.0; 0.0; -9.81],
9-
friction_coefficient::T=1.9, spring=0.0, damper=0.1,
10-
seed=1, contact::Bool=true, info=nothing, vis::Visualizer=Visualizer(), name::Symbol=:robot,
11-
opts_step=SolverOptions(), opts_grad=SolverOptions()) where T
8+
function walker(;
9+
mode::Symbol=:minimal,
10+
timestep=0.05,
11+
gravity=[0.0; 0.0; -9.81],
12+
friction_coefficient=1.9,
13+
spring=0.0,
14+
damper=0.1,
15+
seed=1,
16+
contact_feet=true,
17+
contact_body=true,
18+
info=nothing,
19+
vis=Visualizer(),
20+
name=:robot,
21+
opts_step=SolverOptions(),
22+
opts_grad=SolverOptions(),
23+
T=Float64)
24+
25+
mechanism = get_walker(
26+
timestep=timestep,
27+
gravity=gravity,
28+
friction_coefficient=friction_coefficient,
29+
spring=spring,
30+
damper=damper,
31+
contact_feet=contact_feet,
32+
contact_body=contact_body)
1233

13-
mechanism = get_walker(timestep=dt, gravity=gravity, friction_coefficient=friction_coefficient, spring=spring, damper=damper, contact=contact)
1434
initialize_walker!(mechanism)
1535

1636
if mode == :minimal
@@ -40,9 +60,11 @@ function walker(; mode::Symbol=:minimal, dt::T=0.05, gravity=[0.0; 0.0; -9.81],
4060
u_prev = zeros(nu)
4161
control_mask = [zeros(nu, 3) I(nu)]
4262
motor_gear = [100, 100, 100, 100, 100, 100.]
43-
control_scaling = Diagonal(dt * motor_gear)
63+
control_scaling = Diagonal(timestep * motor_gear)
4464

45-
build_robot(mechanism, vis=vis, name=name)
65+
build_robot(mechanism,
66+
vis=vis,
67+
name=name)
4668

4769
TYPES = [Walker, T, typeof(mechanism), typeof(aspace), typeof(ospace), typeof(info)]
4870
env = Environment{TYPES...}(mechanism, mode, aspace, ospace,
@@ -61,7 +83,8 @@ function reset(env::Environment{Walker}; x=nothing, reset_noise_scale = 0.005)
6183
env.state .= x
6284
else
6385
# initialize above the ground to make sure that with random initialization we do not violate the ground constraint.
64-
initialize!(env.mechanism, :walker, z = 0.25)
86+
initialize!(env.mechanism, :walker,
87+
z=0.25)
6588
x0 = get_minimal_state(env.mechanism)
6689
nx = minimal_dimension(env.mechanism)
6790

@@ -80,7 +103,8 @@ function reset(env::Environment{Walker}; x=nothing, reset_noise_scale = 0.005)
80103
return get_observation(env)
81104
end
82105

83-
function get_observation(env::Environment{Walker}; full_state::Bool=false)
106+
function get_observation(env::Environment{Walker};
107+
full_state=false)
84108
full_state && (return env.state)
85109
nx = minimal_dimension(env.mechanism)
86110
if env.representation == :minimal
@@ -124,19 +148,3 @@ function is_done(::Environment{Walker}, x)
124148
done = !((height > 0.8) && (height < 2.0) && (abs(ang) < 1.0))
125149
return done
126150
end
127-
128-
129-
#
130-
# env.state
131-
# i_torso = findfirst(body -> body.name == "torso", collect(env.mechanism.bodies))
132-
# z_torso = z[(i_torso-1)*13 .+ (1:13)]
133-
# x_velocity = z_torso[4]
134-
# z[3*13 + 4] = 324.0
135-
# z
136-
# set_maximal_state!(env.mechanism, z)
137-
#
138-
# initialize!(env.mechanism, :walker, x = 111.0, z = 1.0, θ=0.18)
139-
# x = get_minimal_state(env.mechanism)
140-
# z = get_maximal_state(env.mechanism)
141-
# is_done(env, x)
142-
#

examples/Project.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[deps]
22
IterativeLQR = "605048dd-e178-462b-beb9-98a09398ef27"
3+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
34
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
45
PGFPlots = "3b7a836e-365b-5785-a47d-02c71176b4aa"
56
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
67

78
[compat]
89
IterativeLQR = "0.1.1"
910
Literate = "2.13.0"
10-
Plots = "1.25.11"
1111
PGFPlots = "3.4.1"
12+
Plots = "1.25.11"
13+
JLD2 = "0.4.21"

examples/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dojo.jl examples
2+
3+
This directory contains examples using Dojo.
4+
The `.jl` files in each subdirectory are meant to be processed using [Literate.jl](https://github.com/fredrikekre/Literate.jl).
5+
During the build process, the `.jl` files are converted to notebooks.
6+
7+
Installation of the package generates notebooks and they can be run locally by performing the following steps:
8+
9+
1. [install Dojo.jl](https://github.com/dojo-sim/Dojo.jl)
10+
2. [install IJulia](https://github.com/JuliaLang/IJulia.jl) (`add` it to the default project)
11+
3. in the Julia REPL, run (do once)
12+
```
13+
using Pkg
14+
Pkg.build("Dojo")
15+
```
16+
4. interact with notebooks
17+
```
18+
using IJulia, Dojo
19+
notebook(dir=joinpath(dirname(pathof(Dojo)), "..", "examples"))
20+
````
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set_camera!(env.vis,
2+
zoom=5.0,
3+
cam_pos=[0.0, -5.0, 0.0])
4+
5+
z = [minimal_to_maximal(env.mechanism, x) for x in x_sol]
6+
t = 1 #10, 20, 30, 41
7+
set_robot(env.vis, env.mechanism, z[t])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# linear cone
2+
line_mat_lc = LineBasicMaterial(color=color_lc, linewidth=10.0)
3+
points_lc = Vector{Point{3,Float64}}()
4+
for (i, xt) in enumerate(storage_lc.x[1])
5+
k = xt
6+
push!(points_lc, Point(k[1], k[2] + 0.04, k[3]))
7+
end
8+
9+
setobject!(vis[:path_lc], Line(points_lc, line_mat_lc))
10+
11+
# nonlinear cone
12+
line_mat_nc = Dojo.LineBasicMaterial(color=color_nc, linewidth=25.0)
13+
points_nc = Vector{Point{3,Float64}}()
14+
for (i, xt) in enumerate(storage_nc.x[1])
15+
k = xt
16+
push!(points_nc, Point(k[1], k[2] - 0.04, k[3] + 0.0))
17+
end
18+
setobject!(vis[:path_nc], MeshCat.Line(points_nc, line_mat_nc))
19+
20+
# MuJoCo
21+
line_mat_mj = LineBasicMaterial(color=color_mj, linewidth=25.0)
22+
points_mj = Vector{Point{3,Float64}}()
23+
for (i, xt) in enumerate(storage_mj.x[1])
24+
k = xt
25+
push!(points_mj, Point(k[1], k[2], k[3]))
26+
end
27+
setobject!(vis[:path_mj], MeshCat.Line(points_mj, line_mat_mj))
28+
29+
settransform!(vis[:lc], MeshCat.Translation(0,+0.04,0))
30+
settransform!(vis[:nc], MeshCat.Translation(0,-0.04,0))
31+
settransform!(vis[:mj], MeshCat.Translation(0,+0.00,0))

0 commit comments

Comments
 (0)