Skip to content

Commit d9886bd

Browse files
committed
cleaning pendulum example
1 parent 1f314b4 commit d9886bd

File tree

7 files changed

+149
-90
lines changed

7 files changed

+149
-90
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+
### Tippe top
31+
<img src="examples/animations/pendulum.gif" width="150"/>
32+
33+
## Trajectory Optimization
3134

32-
### Box
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/define_controller.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,58 @@
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+
7+
8+
9+
# ## Setup
10+
using Dojo
11+
12+
# ## Mechanism
13+
mechanism = get_mechanism(:pendulum,
14+
timestep=0.01,
15+
gravity=-9.81,
16+
damper=5.0,
17+
spring=0.0)
18+
19+
# ## Controller
20+
function controller!(mechanism, k)
21+
## Target state
22+
x_goal = [1.0 * π; 0.0]
23+
24+
## Current state
25+
x = get_minimal_state(mechanism)
26+
27+
## Gains
28+
K = [5.0 0.5] * 0.1
29+
30+
off = 0
31+
for joint in mechanism.joints
32+
nu = input_dimension(joint)
33+
34+
## Get joint configuration + velocity
35+
xi = x[off .+ (1:2nu)]
36+
xi_goal = x_goal[off .+ (1:2nu)]
37+
38+
## Control
39+
ui = -K * (xi - xi_goal)
40+
set_input!(joint, ui)
41+
42+
off += nu
43+
end
44+
end
45+
46+
# ##Simulate
47+
initialize!(mechanism, :pendulum,
48+
angle=0.0 * π,
49+
angular_velocity=0.0);
50+
51+
storage = simulate!(mechanism, 2.0, controller!,
52+
record=true,
53+
verbose=true);
54+
55+
# ## Visualize
56+
vis = Visualizer()
57+
render(vis)
58+
visualize(mechanism, storage, vis=vis);

environments/pendulum/methods/initialize.jl

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
1-
function get_pendulum(;
2-
timestep=0.01,
3-
gravity=-9.81,
4-
mass=1.0,
1+
function get_pendulum(;
2+
timestep=0.01,
3+
gravity=-9.81,
4+
mass=1.0,
55
len=1.0,
6-
spring=0.0,
7-
damper=0.0,
8-
spring_offset=szeros(1),
6+
spring=0.0,
7+
damper=0.0,
8+
spring_offset=szeros(1),
99
axis_offset=one(UnitQuaternion),
1010
T=Float64)
1111

1212
# Parameters
1313
joint_axis = [1.0; 0.0; 0.0]
1414
width, depth = 0.1, 0.1
15-
child_vertex = [0.0; 0.0; len / 2.0] # joint connection point
15+
parent_vertex = [0.0; 0.0; 1.1 * len] # joint connection point
16+
child_vertex = [0.0; 0.0; 0.5 * len] # joint connection point
1617

1718
# Links
1819
origin = Origin{T}()
19-
pbody = Box(width, depth, len, mass)
20+
body = Box(width, depth, len, mass, name=:pendulum)
2021

2122
# Constraints
22-
joint_between_origin_and_pbody = JointConstraint(Revolute(origin, pbody, joint_axis;
23-
child_vertex=child_vertex,
24-
spring=spring,
25-
damper=damper,
23+
joint = JointConstraint(Revolute(origin, body, joint_axis;
24+
parent_vertex=parent_vertex,
25+
child_vertex=child_vertex,
26+
spring=spring,
27+
damper=damper,
2628
rot_spring_offset=spring_offset,
27-
axis_offset=axis_offset))
29+
axis_offset=axis_offset), name=:joint)
2830

29-
bodies = [pbody]
30-
joints = [joint_between_origin_and_pbody]
31+
bodies = [body]
32+
joints = [joint]
3133

32-
mech = Mechanism(origin, bodies, joints,
33-
gravity=gravity,
34-
timestep=timestep,
35-
spring=spring,
34+
mech = Mechanism(origin, bodies, joints,
35+
gravity=gravity,
36+
timestep=timestep,
37+
spring=spring,
3638
damper=damper)
3739

3840
return mech
3941
end
4042

41-
function initialize_pendulum!(mechanism::Mechanism;
42-
angle=0.7,
43+
function initialize_pendulum!(mechanism::Mechanism;
44+
angle=0.7,
4345
angular_velocity=0.0) where T
4446
joint = mechanism.joints[1]
45-
set_minimal_coordinates_velocities!(mechanism, joint;
47+
set_minimal_coordinates_velocities!(mechanism, joint;
4648
xmin=[angle, angular_velocity])
4749
end
4850

49-
function get_npendulum(;
50-
timestep=0.01,
51-
gravity=-9.81,
52-
mass=1.0,
51+
function get_npendulum(;
52+
timestep=0.01,
53+
gravity=-9.81,
54+
mass=1.0,
5355
len=1.0,
54-
spring=0.0,
55-
damper=0.0,
56+
spring=0.0,
57+
damper=0.0,
5658
num_bodies=5,
57-
basetype=:Revolute,
59+
basetype=:Revolute,
5860
joint_type=:Revolute,
59-
T=Float64)
61+
T=Float64)
6062

6163
# Parameters
6264
ex = [1.0; 0.0; 0.0]
@@ -69,31 +71,31 @@ function get_npendulum(;
6971
bodies = [Box(r, r, len, mass, color=RGBA(1.0, 0.0, 0.0)) for i = 1:num_bodies]
7072

7173
# Constraints
72-
jointb1 = JointConstraint(Prototype(basetype, origin, bodies[1], ex;
73-
child_vertex=vert11,
74-
spring=spring,
74+
jointb1 = JointConstraint(Prototype(basetype, origin, bodies[1], ex;
75+
child_vertex=vert11,
76+
spring=spring,
7577
damper=damper))
7678
if num_bodies > 1
77-
joints = [JointConstraint(Prototype(joint_type, bodies[i - 1], bodies[i], ex;
78-
parent_vertex=vert12,
79-
child_vertex=vert11,
80-
spring=spring,
79+
joints = [JointConstraint(Prototype(joint_type, bodies[i - 1], bodies[i], ex;
80+
parent_vertex=vert12,
81+
child_vertex=vert11,
82+
spring=spring,
8183
damper=damper)) for i = 2:num_bodies]
8284
joints = [jointb1; joints]
8385
else
8486
joints = [jointb1]
8587
end
8688

87-
mech = Mechanism(origin, bodies, joints,
88-
gravity=gravity,
89+
mech = Mechanism(origin, bodies, joints,
90+
gravity=gravity,
8991
timestep=timestep)
9092
return mech
9193
end
9294

93-
function initialize_npendulum!(mechanism::Mechanism{T};
94-
base_angle=π / 4.0,
95+
function initialize_npendulum!(mechanism::Mechanism{T};
96+
base_angle=π / 4.0,
9597
base_angular_velocity=[0.0, 0.0, 0.0],
96-
relative_linear_velocity=[0.0, 0.0, 0.0],
98+
relative_linear_velocity=[0.0, 0.0, 0.0],
9799
relative_angular_velocity=[0.0, 0.0, 0.0]) where T
98100

99101
pbody = mechanism.bodies[1]
@@ -102,19 +104,19 @@ function initialize_npendulum!(mechanism::Mechanism{T};
102104
vert12 = -vert11
103105

104106
# set position and velocities
105-
set_maximal_configurations!(mechanism.origin, pbody,
106-
child_vertex=vert11,
107+
set_maximal_configurations!(mechanism.origin, pbody,
108+
child_vertex=vert11,
107109
Δq=UnitQuaternion(RotX(base_angle)))
108-
set_maximal_velocities!(pbody,
110+
set_maximal_velocities!(pbody,
109111
ω=base_angular_velocity)
110112

111113
previd = pbody.id
112114
for (i, body) in enumerate(Iterators.drop(mechanism.bodies, 1))
113-
set_maximal_configurations!(get_body(mechanism, previd), body,
114-
parent_vertex=vert12,
115+
set_maximal_configurations!(get_body(mechanism, previd), body,
116+
parent_vertex=vert12,
115117
child_vertex=vert11)
116-
set_maximal_velocities!(get_body(mechanism, previd), body,
117-
parent_vertex=vert12,
118+
set_maximal_velocities!(get_body(mechanism, previd), body,
119+
parent_vertex=vert12,
118120
child_vertex=vert11,
119121
Δv=relative_linear_velocity, Δω=1 / i * relative_angular_velocity)
120122
previd = body.id

examples/animations/pendulum.gif

1.41 MB
Loading

examples/animations/pendulum.mp4

96.2 KB
Binary file not shown.

examples/simulation/dzhanibekov.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ mech = get_mechanism(:dzhanibekov,
1414

1515
# ## Simulate
1616
initialize_dzhanibekov!(mech,
17-
angular_velocity=[8.0; 0.01; 0.0])
17+
angular_velocity=[8.0; 0.01; 0.0]);
1818
storage = simulate!(mech, 8.00,
1919
record=true,
20-
verbose=false)
20+
verbose=false);
2121

2222
# ## Visualizers
2323
vis = Visualizer()
24-
open(vis)
25-
visualize(mech, storage, vis=vis)
24+
render(vis)
25+
visualize(mech, storage, vis=vis);

examples/simulation/pendulum.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@
66
using Dojo
77

88
# ## Mechanism
9-
mechanism = get_mechanism(:pendulum,
10-
timestep=0.01,
11-
gravity=-9.81,
12-
spring)
9+
mechanism = get_mechanism(:pendulum,
10+
timestep=0.01,
11+
gravity=-9.81,
12+
damper=5.0,
13+
spring=0.0)
1314

1415
# ## Controller
1516
function controller!(mechanism, k)
1617
## Target state
1718
x_goal = [1.0 * π; 0.0]
1819

1920
## Current state
20-
x = get_minimal_state(mechanism)
21+
x = get_minimal_state(mechanism)
2122

22-
## Gains
23-
K = [10.0 0.5] * 0.1
23+
## Gains
24+
K = [5.0 0.5] * 0.1
2425

2526
off = 0
2627
for joint in mechanism.joints
2728
nu = input_dimension(joint)
28-
29+
2930
## Get joint configuration + velocity
3031
xi = x[off .+ (1:2nu)]
3132
xi_goal = x_goal[off .+ (1:2nu)]
32-
33+
3334
## Control
34-
ui = -K * (xi - xi_goal)
35+
ui = -K * (xi - xi_goal)
3536
set_input!(joint, ui)
3637

3738
off += nu
3839
end
3940
end
4041

4142
# ##Simulate
42-
initialize!(mechanism, :pendulum,
43-
angle=0.0 * π,
44-
angular_velocity=0.0)
43+
initialize!(mechanism, :pendulum,
44+
angle=0.0 * π,
45+
angular_velocity=0.0);
4546

46-
storage = simulate!(mechanism, 10.0, controller!,
47-
record=true,
48-
verbose=true)
47+
storage = simulate!(mechanism, 2.0, controller!,
48+
record=true,
49+
verbose=true);
4950

5051
# ## Visualize
51-
vis=visualizer()
52+
vis = Visualizer()
5253
render(vis)
53-
visualize(mechanism, storage,
54-
vis=vis)
54+
visualize(mechanism, storage, vis=vis);

0 commit comments

Comments
 (0)