Skip to content

Commit ae22b3e

Browse files
committed
fixing tippetop
1 parent ab21c05 commit ae22b3e

File tree

4 files changed

+114
-100
lines changed

4 files changed

+114
-100
lines changed

docs/src/define_mechanism.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# Defining a Mechanism
1+
# Defining a Mechanism
2+
Here, we describe how to define your own dynamical system of [`Mechanism`](@ref). After it has been defined, it will be extremely easy to simulate it, control it, perform trajectory optimization on it, or even policy optimization.
3+
4+
We're going to build a tippe top.

environments/snake/methods/initialize.jl

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
function get_snake(;
2-
timestep=0.01,
3-
gravity=[0.0; 0.0; -9.81],
4-
friction_coefficient=0.8,
1+
function get_snake(;
2+
timestep=0.01,
3+
gravity=[0.0; 0.0; -9.81],
4+
friction_coefficient=0.8,
55
contact=true,
6-
contact_type=:nonlinear,
7-
spring=0.0,
8-
damper=0.0,
6+
contact_type=:nonlinear,
7+
spring=0.0,
8+
damper=0.0,
99
num_bodies=2,
10-
joint_type=:Spherical,
11-
height=1.0,
10+
joint_type=:Spherical,
11+
length=1.0,
1212
radius=0.05,
1313
T=Float64)
1414

1515
# Parameters
16-
ex = [1.0; 0.0; 0.0]
17-
ey = [0.0; 1.0; 0.0]
18-
ez = [0.0; 0.0; 1.0]
16+
ex = [1.0; 0.0; 0.0]
17+
ey = [0.0; 1.0; 0.0]
18+
ez = [0.0; 0.0; 1.0]
1919

20-
vert11 = [0.0; 0.0; height / 2.0]
20+
vert11 = [0.0; 0.0; length / 2.0]
2121
vert12 = -vert11
2222

2323
# Links
2424
origin = Origin{T}()
25-
bodies = [Box(3 * radius, 2 * radius, height, height, color=RGBA(1.0, 0.0, 0.0)) for i = 1:num_bodies]
25+
bodies = [Box(3 * radius, 2 * radius, length, length, color=RGBA(0.9, 0.9, 0.9)) for i = 1:num_bodies]
2626

2727
# Constraints
28-
jointb1 = JointConstraint(Floating(origin, bodies[1],
29-
spring=0.0,
28+
jointb1 = JointConstraint(Floating(origin, bodies[1],
29+
spring=0.0,
3030
damper=0.0))
3131

3232
if num_bodies > 1
33-
joints = [JointConstraint(Prototype(joint_type, bodies[i - 1], bodies[i], ex;
34-
parent_vertex=vert12,
35-
child_vertex=vert11,
36-
spring=spring,
33+
joints = [JointConstraint(Prototype(joint_type, bodies[i - 1], bodies[i], ex;
34+
parent_vertex=vert12,
35+
child_vertex=vert11,
36+
spring=spring,
3737
damper=damper)) for i = 2:num_bodies]
3838
joints = [jointb1; joints]
3939
else
@@ -45,35 +45,35 @@ function get_snake(;
4545
normal = [[0.0; 0.0; 1.0] for i = 1:n]
4646
friction_coefficient = friction_coefficient * ones(n)
4747

48-
contacts1 = contact_constraint(bodies, normal,
49-
friction_coefficient=friction_coefficient,
50-
contact_points=fill(vert11, n),
48+
contacts1 = contact_constraint(bodies, normal,
49+
friction_coefficient=friction_coefficient,
50+
contact_points=fill(vert11, n),
5151
contact_type=contact_type) # we need to duplicate point for prismatic joint for instance
52-
contacts2 = contact_constraint(bodies, normal,
53-
friction_coefficient=friction_coefficient,
54-
contact_points=fill(vert12, n),
52+
contacts2 = contact_constraint(bodies, normal,
53+
friction_coefficient=friction_coefficient,
54+
contact_points=fill(vert12, n),
5555
contact_type=contact_type)
56-
mech = Mechanism(origin, bodies, joints, [contacts1; contacts2],
57-
gravity=gravity,
58-
timestep=timestep,
59-
spring=spring,
56+
mech = Mechanism(origin, bodies, joints, [contacts1; contacts2],
57+
gravity=gravity,
58+
timestep=timestep,
59+
spring=spring,
6060
damper=damper)
6161
else
62-
mech = Mechanism(origin, bodies, joints,
63-
gravity=gravity,
64-
timestep=timestep,
65-
spring=spring,
62+
mech = Mechanism(origin, bodies, joints,
63+
gravity=gravity,
64+
timestep=timestep,
65+
spring=spring,
6666
damper=damper)
6767
end
6868
return mech
6969
end
7070

71-
function initialize_snake!(mechanism::Mechanism{T};
71+
function initialize_snake!(mechanism::Mechanism{T};
7272
base_position=[0.0, -0.5, 0.0],
7373
base_orientation=UnitQuaternion(RotX(0.6 * π)),
74-
base_linear_velocity=zeros(3),
74+
base_linear_velocity=zeros(3),
7575
base_angular_velocity=zeros(3),
76-
relative_linear_velocity=zeros(3),
76+
relative_linear_velocity=zeros(3),
7777
relative_angular_velocity=zeros(3)) where T
7878

7979
pbody = mechanism.bodies[1]
@@ -82,22 +82,22 @@ function initialize_snake!(mechanism::Mechanism{T};
8282
vert12 = -vert11
8383

8484
# set position and velocities
85-
set_maximal_configurations!(mechanism.origin, pbody,
86-
child_vertex=base_position,
85+
set_maximal_configurations!(mechanism.origin, pbody,
86+
child_vertex=base_position,
8787
Δq=base_orientation)
88-
set_maximal_velocities!(pbody,
89-
v=base_linear_velocity,
88+
set_maximal_velocities!(pbody,
89+
v=base_linear_velocity,
9090
ω=base_angular_velocity)
9191

9292
previd = pbody.id
9393
for body in mechanism.bodies[2:end]
94-
set_maximal_configurations!(get_body(mechanism, previd), body,
95-
parent_vertex=vert12,
94+
set_maximal_configurations!(get_body(mechanism, previd), body,
95+
parent_vertex=vert12,
9696
child_vertex=vert11)
97-
set_maximal_velocities!(get_body(mechanism, previd), body,
98-
parent_vertex=vert12,
97+
set_maximal_velocities!(get_body(mechanism, previd), body,
98+
parent_vertex=vert12,
9999
child_vertex=vert11,
100-
Δv=relative_linear_velocity,
100+
Δv=relative_linear_velocity,
101101
Δω=relative_angular_velocity)
102102
previd = body.id
103103
end
Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
function get_tippetop(;
2-
timestep=0.01,
3-
gravity=[0.0; 0.0; -9.81],
4-
friction_coefficient=0.8,
5-
contact=true,
1+
function get_tippetop(;
2+
timestep=0.01,
3+
gravity=[0.0; 0.0; -9.81],
4+
friction_coefficient=0.4,
5+
contact=true,
66
contact_type=:nonlinear,
77
T=Float64)
88

99
origin = Origin{T}(name=:origin)
1010
radius = 0.5
1111
mass = 1.0
1212
α = 0.2
13-
bodies = [Sphere(radius, mass, name=:sphere1, color=cyan), Sphere(radius*α, mass*α, name=:sphere2, color=cyan)]
14-
joints = [JointConstraint(Floating(origin, bodies[1]),
13+
bodies = [Sphere(radius, mass, name=:sphere1, color=cyan), Sphere(radius*α, mass*α^3, name=:sphere2, color=cyan)]
14+
joints = [JointConstraint(Floating(origin, bodies[1]),
1515
name=:floating_joint),
16-
JointConstraint(Fixed(bodies[1], bodies[2],
17-
parent_vertex=[0,0,radius],
16+
JointConstraint(Fixed(bodies[1], bodies[2],
17+
parent_vertex=[0,0,radius],
1818
child_vertex=zeros(3)), name = :fixed_joint),]
1919
mechanism = Mechanism(origin, bodies, joints,
20-
timestep=timestep,
20+
timestep=timestep,
2121
gravity=gravity)
2222

2323
# modify inertias
@@ -27,58 +27,69 @@ function get_tippetop(;
2727
contact = [0.0, 0.0, 0.0]
2828
normal = [0.0, 0.0, 1.0]
2929
contacts = [
30-
contact_constraint(get_body(mechanism, :sphere1), normal,
31-
friction_coefficient=friction_coefficient,
32-
contact_point=contact, offset=[0.0, 0.0, radius],
30+
contact_constraint(get_body(mechanism, :sphere1), normal,
31+
friction_coefficient=friction_coefficient,
32+
contact_point=contact, offset=[0.0, 0.0, radius],
3333
contact_type=contact_type),
34-
contact_constraint(get_body(mechanism, :sphere2), normal,
35-
friction_coefficient=friction_coefficient,
36-
contact_point=contact,
37-
offset=[0.0, 0.0, radius * α],
34+
contact_constraint(get_body(mechanism, :sphere2), normal,
35+
friction_coefficient=friction_coefficient,
36+
contact_point=contact,
37+
offset=[0.0, 0.0, radius * α],
3838
contact_type=contact_type)
3939
]
4040
set_minimal_coordinates!(mechanism, get_joint(mechanism, :floating_joint), [0.0; 0.0; radius; zeros(3)])
4141
mechanism = Mechanism(origin, bodies, joints, contacts,
42-
gravity=gravity,
42+
gravity=gravity,
4343
timestep=timestep)
4444
end
4545

4646
return mechanism
4747
end
4848

49-
function initialize_tippetop!(mechanism::Mechanism{T};
49+
function initialize_tippetop!(mechanism::Mechanism{T};
5050
x=zeros(3),
51-
q=one(UnitQuaternion),
51+
q=one(UnitQuaternion),
5252
v=zeros(3),
5353
ω=zeros(3)) where T
5454

5555
joint2 = get_joint(mechanism, :fixed_joint)
56-
radius = joint2.constraints[1].vertices[1][3]
56+
radius = joint2.translational.vertices[1][3]
5757
origin = mechanism.origin
5858
pbody = get_body(mech, :sphere1)
5959
cbody = get_body(mech, :sphere2)
6060

6161
zero_velocity!(mechanism)
62-
63-
set_maximal_configurations!(origin, pbody;
64-
parent_vertex=[0.0; 0.0; radius],
65-
child_vertex=[0.0; 0.0; 0.0],
66-
Δx=x,
62+
63+
set_maximal_configurations!(origin, pbody;
64+
parent_vertex=[0.0; 0.0; radius],
65+
child_vertex=[0.0; 0.0; 0.0],
66+
Δx=x,
6767
Δq=q)
68-
set_maximal_configurations!(pbody, cbody;
69-
parent_vertex=[0.0; 0.0; radius],
70-
child_vertex=[0.0; 0.0; 0.0],
71-
Δx=[0.0; 0.0; 0.0],
68+
set_maximal_configurations!(pbody, cbody;
69+
parent_vertex=[0.0; 0.0; radius],
70+
child_vertex=[0.0; 0.0; 0.0],
71+
Δx=[0.0; 0.0; 0.0],
7272
Δq=one(UnitQuaternion))
73-
set_maximal_velocities!(origin, pbody;
74-
parent_vertex=[0.0; 0.0; radius],
75-
child_vertex=[0.0; 0.0; 0.0],
76-
Δv=v,
73+
set_maximal_velocities!(origin, pbody;
74+
parent_vertex=[0.0; 0.0; radius],
75+
child_vertex=[0.0; 0.0; 0.0],
76+
Δv=v,
7777
Δω=ω)
78-
set_maximal_velocities!(pbody, cbody;
79-
parent_vertex=[0.0; 0.0; radius],
80-
child_vertex=[0.0; 0.0; 0.0],
81-
Δv=[0.0; 0.0; 0.0],
78+
set_maximal_velocities!(pbody, cbody;
79+
parent_vertex=[0.0; 0.0; radius],
80+
child_vertex=[0.0; 0.0; 0.0],
81+
Δv=[0.0; 0.0; 0.0],
8282
Δω=[0.0; 0.0; 0.0])
8383
return nothing
8484
end
85+
86+
87+
88+
mech = get_tippetop(timestep=0.01)
89+
initialize!(mech, :tippetop, ω=[0,0.05,5.0])
90+
91+
storage = simulate!(mech, 3.0, verbose=false, record=true)
92+
93+
# vis = Visualizer()
94+
# open(vis)
95+
visualize(mech, storage, vis=vis)

examples/simulation/tippetop.jl

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

88
# ## Mechanism
9-
mech = get_mechanism(:tippetop,
10-
timestep=0.01,
11-
gravity=-9.81,
12-
contact=true,
9+
mech = get_mechanism(:tippetop,
10+
timestep=0.01,
11+
gravity=-9.81,
12+
friction_coefficient=0.4,
13+
contact=true,
1314
contact_type=:nonlinear)
1415

1516
# ## Simulate
16-
initialize!(mech, :tippetop,
17-
x=[0.0, 0.0, 1.0],
18-
q=UnitQuaternion(RotX(0.01 * π)),
17+
initialize!(mech, :tippetop,
18+
x=[0.0, 0.0, 1.0],
19+
q=UnitQuaternion(RotX(0.01 * π)),
1920
ω=[0.0, 0.01, 50.0])
2021

21-
storage = simulate!(mech, 25.0,
22-
record=true,
23-
verbose=false,
24-
opts=SolverOptions(verbose=false, btol=1e-6))
22+
storage = simulate!(mech, 10.0,
23+
record=true,
24+
verbose=false,
25+
opts=SolverOptions(verbose=false, btol=1e-5))
2526

2627
# ## Open visualizer
27-
vis=visualizer()
28-
open(vis)
29-
visualize(mech, storage,
30-
vis=vis)
28+
vis = Visualizer()
29+
render(vis)
30+
visualize(mech, storage, vis=vis)

0 commit comments

Comments
 (0)