forked from antoinefalisse/predsim_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contactModels.py
136 lines (123 loc) · 5.76 KB
/
contactModels.py
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
'''
This script contains classes that implement contact models.
'''
# %% Import packages.
import numpy as np
import casadi as ca
# %% This class implements a smooth approximation of Simbody's Hunt-Crossley
# contact model: https://github.com/simbody/simbody/blob/master/Simbody/include/simbody/internal/SmoothSphereHalfSpaceForce.h
class smoothSphereHalfSpaceForce:
def __init__(self, stiffness, radius, dissipation, transitionVelocity,
staticFriction, dynamicFriction, viscousFriction, normal):
self.stiffness = stiffness
self.radius = radius
self.dissipation = dissipation
self.transitionVelocity = transitionVelocity
self.staticFriction = staticFriction
self.dynamicFriction = dynamicFriction
self.viscousFriction = viscousFriction
self.normal = normal
def getContactForce(self, locSphere_inB, posB_inG, lVelB_inG, aVelB_inG,
RBG_inG, TBG_inG):
# Constant values.
eps = 1e-5
eps2 = 1e-16
bv = 50
bd = 300
# Express sphere position in ground.
locSphere_inG = (np.matmul(RBG_inG, locSphere_inB)+TBG_inG).T
# Contact point position.
locContact_inG = locSphere_inG - np.array([[0, self.radius, 0]])
# Indentation.
indentation = -locContact_inG[0, 1]
# Contact point velocity.
term1 = aVelB_inG.T
term2 = (locSphere_inG - np.array([[0, self.radius, 0]]) +
0.5 * np.array([[0, indentation, 0]]) - posB_inG.T)
crossProduct0 = term1[0, 1]*term2[0, 2] - term1[0, 2]*term2[0, 1]
crossProduct1 = term1[0, 2]*term2[0, 0] - term1[0, 0]*term2[0, 2]
crossProduct2 = term1[0, 0]*term2[0, 1] - term1[0, 1]*term2[0, 0]
v = lVelB_inG.T + [[crossProduct0], [crossProduct1], [crossProduct2]]
vnormal = (v[0, 0]*self.normal[0, 0] + v[0, 1]*self.normal[0, 1] +
v[0, 2]*self.normal[0, 2])
vtangent = v - vnormal * self.normal
indentationVel = -vnormal
# Stiffness force.
k = 0.5 * (self.stiffness) ** (2/3)
fH = ((4/3) * k * np.sqrt(self.radius * k) *
((np.sqrt(indentation * indentation + eps)) ** (3/2)))
# Dissipation force.
fHd = fH * (1 + 1.5 * self.dissipation * indentationVel)
fn = ((0.5 *
np.tanh(bv * (indentationVel + 1 / (1.5 * self.dissipation))) +
0.5 + eps2) * (0.5 * np.tanh(bd * indentation) + 0.5 + eps2) *
fHd)
force = fn * self.normal
# Friction force.
aux = ((vtangent[0, 0]) ** 2 + (vtangent[0, 1]) ** 2 +
(vtangent[0, 2]) ** 2 + eps)
vslip = aux ** (0.5)
vrel = vslip / self.transitionVelocity
ffriction = fn * (np.fmin(vrel, 1) * (self.dynamicFriction +
2 * (self.staticFriction - self.dynamicFriction) /
(1 + vrel * vrel)) + self.viscousFriction * vslip)
# Contact force.
contactForce = force + ffriction * (-vtangent) / vslip
return contactForce
# %% CasADi-specific implementation.
class smoothSphereHalfSpaceForce_ca:
def __init__(self, stiffness, radius, dissipation, transitionVelocity,
staticFriction, dynamicFriction, viscousFriction, normal):
self.stiffness = stiffness
self.radius = radius
self.dissipation = dissipation
self.transitionVelocity = transitionVelocity
self.staticFriction = staticFriction
self.dynamicFriction = dynamicFriction
self.viscousFriction = viscousFriction
self.normal = normal
def getContactForce(self, locSphere_inB, posB_inG, lVelB_inG, aVelB_inG,
RBG_inG, TBG_inG):
# Constant values.
eps = 1e-5
eps2 = 1e-16
bv = 50
bd = 300
# Express sphere position in ground.
locSphere_inG = (np.matmul(RBG_inG, locSphere_inB)+TBG_inG).T
# Contact point position.
locContact_inG = locSphere_inG - np.array([[0, self.radius, 0]])
# Indentation.
indentation = -locContact_inG[0, 1]
# Contact point velocity.
v = lVelB_inG.T + ca.cross(aVelB_inG.T,
locSphere_inG -
np.array([[0, self.radius, 0]]) +
0.5 * np.array([[0, indentation, 0]]) -
posB_inG.T)
vnormal = (v[0, 0]*self.normal[0, 0] + v[0, 1]*self.normal[0, 1] +
v[0, 2]*self.normal[0, 2])
vtangent = v - vnormal * self.normal
indentationVel = -vnormal
# Stiffness force.
k = 0.5 * (self.stiffness) ** (2/3)
fH = ((4/3) * k * np.sqrt(self.radius * k) *
((np.sqrt(indentation * indentation + eps)) ** (3/2)))
# Dissipation force.
fHd = fH * (1 + 1.5 * self.dissipation * indentationVel)
fn = ((0.5 *
np.tanh(bv * (indentationVel + 1 / (1.5 * self.dissipation))) +
0.5 + eps2) * (0.5 * np.tanh(bd * indentation) + 0.5 + eps2) *
fHd)
force = fn * self.normal
# Friction force.
aux = ((vtangent[0, 0]) ** 2 + (vtangent[0, 1]) ** 2 +
(vtangent[0, 2]) ** 2 + eps)
vslip = aux ** (0.5)
vrel = vslip / self.transitionVelocity
ffriction = fn * (np.fmin(vrel, 1) * (self.dynamicFriction +
2 * (self.staticFriction - self.dynamicFriction) /
(1 + vrel * vrel)) + self.viscousFriction * vslip)
# Contact force.
contactForce = force + ffriction * (-vtangent) / vslip
return contactForce