Skip to content

Commit 2a3f3ea

Browse files
committed
Merge branch 'main' of https://github.com/dojo-sim/Dojo.jl into main
2 parents 4682c1a + ab21c05 commit 2a3f3ea

File tree

17 files changed

+277
-167
lines changed

17 files changed

+277
-167
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ A differentiable simulator for robotics.
77

88
# Examples (RSS 2022)
99

10-
## Simulation
10+
## Simulation
1111

12-
### Atlas drop
12+
### Atlas drop
1313
<img src="examples/animations/atlas_drop.gif" width="100"/>
1414

1515
### Astronaut
1616
<img src="examples/animations/astronaut.gif" width="200"/>
1717

18-
### Friction-cone comparison
18+
### Friction-cone comparison
1919
- blue = nonlinear friction cone
2020
- orange = linear friction cone
21-
- magenta = MuJoCo friction model
21+
- magenta = MuJoCo friction model
2222
<img src="examples/animations/cone_compare_mujoco.gif" height="150"/>
2323

2424
### Dzhanibekov effect
@@ -27,28 +27,31 @@ A differentiable simulator for robotics.
2727
### Tippe top
2828
<img src="examples/animations/tippetop.gif" width="150"/>
2929

30-
## Trajectory Optimization
30+
### Pendulum swing-up
31+
<img src="examples/animations/pendulum.gif" width="150"/>
3132

32-
### Box
33+
## Trajectory Optimization
34+
35+
### Box
3336
<img src="examples/animations/box_right.gif" width="200"/>
3437

3538
<img src="examples/animations/box_up.gif" width="95"/>
3639

37-
### Hopper
40+
### Hopper
3841
<img src="examples/animations/hopper_max.gif" width="100"/>
3942

40-
### Quadruped
43+
### Quadruped
4144
<img src="examples/animations/quadruped_min.gif" width="200"/>
4245

4346
### Atlas
4447
<img src="examples/animations/atlas_ilqr.gif" width="200"/>
4548

46-
### Cart-pole
49+
### Cart-pole
4750
<img src="examples/animations/cartpole_max.gif" width="200"/>
4851

49-
## Reinforcement Learning
52+
## Reinforcement Learning
5053

51-
### Half Cheetah
54+
### Half Cheetah
5255
<img src="examples/animations/halfcheetah_ars.gif" width="600"/>
5356

5457
### Ant
@@ -65,9 +68,9 @@ Learning friction coefficient:
6568
### Toss
6669
<img src="examples/animations/box_toss.gif" width="300"/>
6770

68-
## Installation
71+
## Installation
6972

70-
`Dojo` can be added via the Julia package manager (type `]`):
73+
`Dojo` can be added via the Julia package manager (type `]`):
7174
```julia
7275
pkg> add Dojo
7376
```
@@ -88,6 +91,3 @@ pkg> add Dojo#main
8891

8992
## How To Contribute
9093
Please submit a pull request, open an issue, or reach out to: thowell@stanford.edu (Taylor) or simonlc@stanford.edu (Simon)
91-
92-
93-

docs/src/assets/dzhanibekov_nasa.gif

1.99 MB
Loading

docs/src/assets/quadruped_min.gif

-802 KB
Binary file not shown.

docs/src/define_controller.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
# Defining a Controller
1+
# Defining a Controller
2+
Here, we explain how to write a controller and simulate its effect on a dynamical system
3+
i.e. a [`Mechanism`](@ref).
4+
We focus on a simple pendulum swing-up.
5+
6+
Load Dojo and use the pendulum mechanism with desired simulation time step, desired gravity and desired damping at the joint.
7+
8+
```julia
9+
using Dojo
10+
11+
mechanism = get_mechanism(:pendulum,
12+
timestep=0.01,
13+
gravity=-9.81,
14+
damper=5.0)
15+
```
16+
17+
Define the controller. This is a method that takes 2 input arguments:
18+
- a [`Mechanism`](@ref),
19+
- an integer `k` indicating the current simulation step.
20+
The controller computes the control inputs based on the current state `x`, the goal state `x_goal` and a proportional gain `K`.
21+
22+
23+
```julia
24+
function controller!(mechanism, k)
25+
## Target state
26+
x_goal = [1.0 * π; 0.0]
27+
28+
## Current state
29+
x = get_minimal_state(mechanism)
30+
31+
## Gains
32+
K = [5.0 0.5] * 0.1
33+
34+
# Control inputs
35+
u = -K * (x - x_goal)
36+
set_input!(mechanism, u)
37+
end
38+
```
39+
40+
We initialize the pendulum in the lower position, and we will let the controller perform the swing-up movement.
41+
```julia
42+
initialize!(mechanism, :pendulum,
43+
angle=0.0 * π,
44+
angular_velocity=0.0);
45+
```
46+
47+
We simulate the system for 2 seconds using the `controller!`.
48+
```julia
49+
storage = simulate!(mechanism, 2.0, controller!,
50+
record=true,
51+
verbose=true);
52+
```
53+
54+
We visualize the results.
55+
```julia
56+
vis = Visualizer()
57+
open(vis)
58+
visualize(mechanism, storage, vis=vis);
59+
```
60+
61+
You should get something like this,
62+
```@raw html
63+
<img src="./../../examples/animations/pendulum.gif" width="300"/>
64+
```

docs/src/define_simulation.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
# Defining a Simulation
1+
# Defining a Simulation
2+
Here, we explain how to simulate a dynamical system i.e. a [`Mechanism`](@ref) forward in time.
3+
The example that we are trying to replicate the Dzhanibekov effect shown below.
4+
5+
![dzhanibekov](./assets/dzhanibekov_nasa.gif)
6+
7+
8+
Load the `Dojo` package.
9+
```julia
10+
using Dojo
11+
```
12+
13+
Define the simulation time step, and other parameters like the gravity.
14+
```julia
15+
timestep=0.01
16+
gravity=0.0
17+
```
18+
19+
We use the mechanism called `:dzhanibekov`.
20+
```julia
21+
mech = get_mechanism(:dzhanibekov,
22+
timestep=timestep,
23+
gravity=gravity);
24+
```
25+
26+
We initialize the system with a given initial angular velocity.
27+
```julia
28+
initialize_dzhanibekov!(mech,
29+
angular_velocity=[15.0; 0.01; 0.0])
30+
```
31+
32+
We simulate this system for 4 seconds, we record the resulting trajectory in `storage`,
33+
```julia
34+
storage = simulate!(mech, 4.00,
35+
record=true,
36+
verbose=false)
37+
```
38+
39+
We visualize the trajectory in the browser,
40+
```julia
41+
vis = Visualizer()
42+
open(vis)
43+
visualize(mech, storage, vis=vis)
44+
```
45+
46+
And voila! You should see something like this;
47+
48+
```@raw html
49+
<img src="./../../examples/animations/dzhanibekov.gif" width="300"/>
50+
```

docs/src/impact.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ where $\circ$ is an element-wise product operator, enforces zero force if the bo
2121

2222

2323
### Constructor
24-
```@docs
25-
ImpactContact
26-
```
24+
[`ImpactContact`](@ref)

docs/src/linearized_friction.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ $$[v^T -v^T]^T + \psi \mathbf{1} - \eta = 0, \\
2929
where $\psi \in \mathbf{R}$ and $\eta \in \mathbf{R}^{4}$ are the dual variables associated with the friction cone and positivity constraints, respectively, and $\textbf{1}$ is a vector of ones.
3030

3131
### Constructor
32-
```@docs
33-
LinearContact
34-
```
32+
[`LinearContact`](@ref)

docs/src/nonlinear_friction.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,4 @@ v^{(i)}(z, z_{+}) - \eta_{(2:3)}^{(i)} = 0, \quad i = 1, \dots, P, \\
4545
where $u \in \mathbf{R}^m$ is the control input at the current time step, $\lambda = (\beta^{(1)}_{(2:3)}, \gamma^{(1)}, \dots, \beta^{(P)}_{(2:3)}, \gamma^{(P)}) \in \mathbf{\Lambda}$ is the concatenation of impact and friction impulses, $B : \mathbf{Z} \rightarrow \mathbf{R}^{6N \times m}$ is the input Jacobian mapping control inputs into maximal coordinates, $C : \mathbf{Z} \rightarrow \mathbf{R}^{\text{dim}(\mathbf{\Lambda}) \times 6N}$ is a contact Jacobian mapping between maximal coordinates and contact surfaces, $s \in \mathbf{R}^P$ is a slack variable introduced for convenience, and $v^{(i)} : \mathbf{Z} \times \mathbf{Z} \rightarrow \mathbf{R}^2$ is the tangential velocity at contact point $i$. Joint limits and internal friction are readily incorporated into this problem formulation.
4646

4747
### Constructor
48-
```@docs
49-
NonlinearContact
50-
```
48+
[`NonlinearContact`](@ref)
Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
1-
function get_dzhanibekov(;
2-
timestep=0.01,
3-
gravity=-9.81,
4-
color=magenta) where T
1+
function get_dzhanibekov(;
2+
timestep=0.01,
3+
gravity=-9.81,
4+
color=RGBA(0.9,0.9,0.9,1)) where T
55

66
radius = 0.1
77
body_length = 1.0
88
body_mass = 1.0
99
origin = Origin{Float64}()
10-
main_body = Capsule(radius, body_length, body_mass,
11-
color=color)
12-
side_body = Capsule(0.5 * radius, 0.35 * body_length, 0.1 * body_mass,
13-
axis_offset=UnitQuaternion(RotY(0.5 * π)),
14-
color=color)
10+
main_body = Capsule(radius, body_length, body_mass,
11+
color=color, name=:main)
12+
side_body = Capsule(0.5 * radius, 0.35 * body_length, 0.5 * body_mass,
13+
axis_offset=UnitQuaternion(RotY(0.5 * π)),
14+
color=color, name=:side)
1515
links = [main_body, side_body]
1616

1717
# Joint Constraints
18-
joint_float = JointConstraint(Floating(origin, links[1]))
19-
joint_attach = JointConstraint(Fixed(links[1], links[2];
20-
parent_vertex=szeros(3),
21-
child_vertex=[-0.25 * body_length; 0.0; 0.0]))
22-
joints = [joint_float, joint_attach]
18+
joint_float = JointConstraint(Floating(origin, links[1]), name=:floating)
19+
joint_fixed = JointConstraint(Fixed(links[1], links[2];
20+
parent_vertex=szeros(3),
21+
child_vertex=[-0.25 * body_length; 0.0; 0.0]), name=:fixed)
22+
joints = [joint_float, joint_fixed]
2323

24+
links[1].inertia = Diagonal([3e-2, 1e-3, 1e-1])
2425
# Mechanism
25-
return Mechanism(origin, links, joints,
26-
gravity=gravity,
26+
return Mechanism(origin, links, joints,
27+
gravity=gravity,
2728
timestep=timestep)
2829
end
2930

30-
function initialize_dzhanibekov!(mech::Mechanism{T,Nn,Ne,Nb};
31-
linear_velocity=zeros(3),
31+
function initialize_dzhanibekov!(mechanism::Mechanism{T,Nn,Ne,Nb};
32+
linear_velocity=zeros(3),
3233
angular_velocity=zeros(3)) where {T,Nn,Ne,Nb}
3334

34-
set_maximal_configurations!(mech.origin, mech.bodies[3])
35-
set_maximal_velocities!(mech.bodies[3],
36-
v=linear_velocity,
37-
ω=angular_velocity)
38-
39-
set_maximal_configurations!(mech.bodies[3], mech.bodies[4],
40-
Δx=[0.25; 0.0; 0.0])
41-
set_maximal_velocities!(mech.bodies[4],
42-
v=zeros(3),
43-
ω=zeros(3))
44-
end
35+
zero_velocity!(mechanism)
36+
set_minimal_coordinates_velocities!(mechanism, get_joint(mechanism, :floating);
37+
xmin=[0;0;1;0;0;0; linear_velocity; angular_velocity])
38+
set_minimal_coordinates_velocities!(mechanism, get_joint(mechanism, :fixed))
39+
end

0 commit comments

Comments
 (0)