-
Notifications
You must be signed in to change notification settings - Fork 25
/
set.jl
138 lines (107 loc) · 3.7 KB
/
set.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function set_velocity_solution!(body::Body)
state = body.state
state.vsol[1] = state.v15
state.vsol[2] = state.v15
state.ωsol[1] = state.ω15
state.ωsol[2] = state.ω15
end
function set_previous_configuration!(body::Body{T}, timestep::T) where {T}
x2, v15, q2, ω15 = initial_configuration_velocity(body.state)
body.state.x1 = next_position(x2, -v15, timestep)
body.state.q1 = next_orientation(q2, -ω15, timestep)
end
function initialize_state!(body::Body{T}, timestep) where T
set_previous_configuration!(body, timestep)
state = body.state
state.JF2 = szeros(T,3)
state.Jτ2 = szeros(T,3)
end
function update_state!(body::Body{T}, timestep) where T
state = body.state
state.x1 = state.x2
state.q1 = state.q2
state.v15 = state.vsol[2]
state.ω15 = state.ωsol[2]
state.x2 = next_position(state.x2, state.vsol[2], timestep)
state.q2 = next_orientation(state.q2, state.ωsol[2], timestep)
state.JF2 = szeros(T,3)
state.Jτ2 = szeros(T,3)
end
# maximal
function set_maximal_configurations!(body::Body;
x::AbstractVector=SA[0.0; 0.0; 0.0],
q::Quaternion=one(Quaternion))
body.state.x2 = x
body.state.q2 = q
return body.state.x2, body.state.q2
end
function set_maximal_velocities!(body::Body;
v::AbstractVector=SA[0.0; 0.0; 0.0],
ω::AbstractVector=SA[0.0; 0.0; 0.0])
body.state.v15 = v
body.state.ω15 = ω
return body.state.v15, body.state.ω15
end
function set_maximal_configurations!(pbody::Node, cbody::Body;
parent_vertex::AbstractVector=SA[0.0; 0.0; 0.0],
child_vertex::AbstractVector=SA[0.0; 0.0; 0.0],
Δx::AbstractVector=SA[0.0; 0.0; 0.0],
Δq::Quaternion=one(Quaternion))
q1 = pbody.state.q2
q2 = pbody.state.q2 * Δq
x2 = pbody.state.x2 + vector_rotate(parent_vertex + Δx, q1) - vector_rotate(child_vertex, q2)
return set_maximal_configurations!(cbody; x=x2, q=q2)
end
function set_maximal_velocities!(pbody::Node, cbody::Body;
parent_vertex::AbstractVector=SA[0.0; 0.0; 0.0],
child_vertex::AbstractVector=SA[0.0; 0.0; 0.0],
Δv::AbstractVector=SA[0.0; 0.0; 0.0],
Δω::AbstractVector=SA[0.0; 0.0; 0.0])
x1 = pbody.state.x2
v1 = pbody.state.v15
q1 = pbody.state.q2
ω1 = pbody.state.ω15
x2 = cbody.state.x2
q2 = cbody.state.q2
ω2 = vector_rotate(Δω + ω1, inv(q2) * q1)
ω1w = vector_rotate(ω1, q1)
ω2w = vector_rotate(ω2, q2)
Δvw = vector_rotate(Δv, q1)
cApB_w = (x2 + vector_rotate(child_vertex, q2)) - x1
pBcB_w = - vector_rotate(child_vertex, q2)
v2 = copy(v1)
v2 += skew(ω1w) * cApB_w
v2 += skew(ω2w) * pBcB_w
v2 += Δvw
return set_maximal_velocities!(cbody; v = v2, ω = ω2)
end
"""
set_external_force!(body; force, torque, vertex)
applies an external force on a body
body: Body
force: force in body frame
torque: torque in local frame
vertex: point where force is applied in local frame
"""
function set_external_force!(body::Body; force=zeros(3), torque=zeros(3), vertex=zeros(3))
body.state.Fext = vector_rotate(force, body.state.q2)
body.state.τext = torque + cross(vertex, force)
return
end
"""
add_external_force!(body; force, torque, vertex)
adds an additional external force on a body
body: Body
force: force in body frame
torque: torque in local frame
"""
function add_external_force!(body::Body; force=zeros(3), torque=zeros(3), vertex=zeros(3))
body.state.Fext += vector_rotate(force, body.state.q2)
body.state.τext += torque + cross(vertex, force)
return
end
function clear_external_force!(body::Body{T}) where T
body.state.Fext = szeros(T,3)
body.state.τext = szeros(T,3)
return
end