-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathexample_stickslip_event.py
More file actions
194 lines (141 loc) · 4.16 KB
/
example_stickslip_event.py
File metadata and controls
194 lines (141 loc) · 4.16 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#########################################################################################
##
## PathSim example of slip-stick system (box on conveyor belt)
##
#########################################################################################
# IMPORTS ===============================================================================
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import (
Integrator,
Amplifier,
Function,
Source,
Switch,
Adder,
Scope
)
from pathsim.events import ZeroCrossing
from pathsim.solvers import RKBS32
# SYSTEM DEFINITION =====================================================================
#initial position and velocity
x0, v0 = 0, 0
#system parameters
m = 20.0 # mass
k = 70.0 # spring constant
d = 10.0 # spring damping
mu = 1.5 # friction coefficient
g = 9.81 # gravity
v = 2.0 # belt velocity magnitude
T = 50.0 # excitation period
F_c = mu * m * g # friction force
#function for belt velocity
def v_belt(t):
return v * np.sin(2*np.pi*t/T)
# return v * t / T
# return v * (1 - np.exp(-t/T))
#function for coulomb friction force
def f_coulomb(v, vb):
return F_c * np.sign(vb - v)
# system topology -----------------------------------------------------------------------
#blocks that define the system dynamics
Sr = Source(v_belt) # velocity of the belt
I1 = Integrator(v0) # integrator for velocity
I2 = Integrator(x0) # integrator for position
A1 = Amplifier(-d)
A2 = Amplifier(-k)
A3 = Amplifier(1/m)
Fc = Function(f_coulomb) # coulomb friction (kinetic)
P1 = Adder()
Sw = Switch(0) # selecting port '0' initially
#blocks for visualization
Sc1 = Scope(
labels=[
"belt velocity",
"box velocity",
"box position"
]
)
Sc2 = Scope(
labels=[
"box force",
"coulomb force"
]
)
blocks = [Sr, I1, I2, A1, A2, A3, Fc, P1, Sw, Sc1, Sc2]
#connections between the blocks
connections = [
Connection(I1, Sw[1], Fc[0]),
Connection(Sr, Sw[0], Fc[1], Sc1[0]),
Connection(Sw, I2, A1, Sc1[1]),
Connection(I2, A2, Sc1[2]),
Connection(A1, P1[0]),
Connection(A2, P1[1]),
Connection(Fc, P1[2], Sc2[1]),
Connection(P1, A3, Sc2[0]),
Connection(A3, I1)
]
# event management ----------------------------------------------------------------------
def slip_to_stick_evt(t):
_1, v_box , _2 = Sw()
_1, v_belt, _2 = Sr()
dv = v_box - v_belt
return dv
def slip_to_stick_act(t):
#change switch state
Sw.select(0)
I1.off()
Fc.off()
E_slip_to_stick.off()
E_stick_to_slip.on()
E_slip_to_stick = ZeroCrossing(
func_evt=slip_to_stick_evt,
func_act=slip_to_stick_act,
tolerance=1e-3
)
def stick_to_slip_evt(t):
_1, F, _2 = P1()
return F_c - abs(F)
def stick_to_slip_act(t):
#change switch state
Sw.select(1)
I1.on()
Fc.on()
#set integrator state
_1, v_box , _2 = Sw()
I1.engine.set(v_box)
E_slip_to_stick.on()
E_stick_to_slip.off()
E_stick_to_slip = ZeroCrossing(
func_evt=stick_to_slip_evt,
func_act=stick_to_slip_act,
tolerance=1e-3
)
events = [E_slip_to_stick, E_stick_to_slip]
# simulation setup ----------------------------------------------------------------------
#create a simulation instance from the blocks and connections
Sim = Simulation(
blocks,
connections,
events,
dt=0.01,
dt_max=0.1,
log=True,
Solver=RKBS32,
tolerance_lte_abs=1e-6,
tolerance_lte_rel=1e-4
)
# Run Example ===========================================================================
if __name__ == "__main__":
#run the simulation for some time
Sim.run(2*T)
# visualization ---------------------------------------------------------------------
#plot the results directly from the two scopes
fig, ax = Sc1.plot("-", lw=2)
for t in E_slip_to_stick:
ax.axvline( t , ls="--", c="k")
for t in E_stick_to_slip:
ax.axvline( t , ls=":", c="k")
Sc2.plot("-", lw=2)
plt.show()