import psutil
L = psutil.Process().parent().cmdline()
if any("jupyter-notebook" in string for string in L):
%matplotlib notebook
elif any("jupyterhub" in string for string in L):
%matplotlib widget
else:
%matplotlib inline
from numpy import sin, cos
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import scipy.integrate as integrate
import matplotlib.animation as animation
from collections import deque
from scipy.integrate import solve_ivp
from sympy import *
from CAS_lib import minreal
from scipy import signal
m1=20 #kg
m2=375 #kg
m = [m1,m2]
b =9800 #N/(m/s)
kw=1e6 #N/m
ks=1.3e5 #N/m
# create a time array from 0..t_stop sampled at 0.02 second steps
dt = 0.01 # time step
t_stop = 2 # how many seconds to simulate
TSPAN = [0, t_stop] #simulation time spa
TIME = np.arange(0, t_stop, dt) # simulation time serie
y0 = [0,0,0,0]
history_len = 1000 # how many trajectory points to display
r=0
offset1=0.3 #The offset used by the wheel equilibrium equations [m]
offset2=0.5#The offset used by the wheel equilibrium equations [m]
L=0.5
H=0.2
A = Matrix([[0,0,1,0 ], [0,0,0,1],[(-ks-kw)/m1, ks/m1, -b/m1, b/m1],[ks/m2,-ks/m2,b/m2,-b/m2]])
B = Matrix([[0],[0],[kw/m1],[0]])
s = symbols('s')
TFs =simplify((s*eye(4)-A).inv()*B)
n, d = TFs[0].as_numer_denom() # Take the first element in the list: X(s)/U(s)
num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s
den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s
num = [float(item) for item in num] #convert all elements in the list to floating points
den = [float(item) for item in den] #convert all elements in the list to floating points
H1 = signal.TransferFunction(num,den) #Create the transfer function
H1 = minreal(H1)
n, d = TFs[1].as_numer_denom() # Take the second element in the list: Y(s)/U(s)
num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s
den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s
num = [float(item) for item in num] #convert all elements in the list to floating points
den = [float(item) for item in den] #convert all elements in the list to floating points
H2 = signal.TransferFunction(num,den) #Create the transfer function
H2 = minreal(H2)
a= 0*np.ones(int(t_stop/(2*dt))) # 1 second zero values for input
b= 0.2*np.ones(int(t_stop/(2*dt))) # 1 second 0.2 values for input
U = np.concatenate((a, b), axis=0) # combine both vectors a and b
T1,yout1,vout1 = signal.lsim(H1, U, TIME, y0)
T2,yout2,vout2 = signal.lsim(H2, U, TIME, y0)
x = yout1
y = yout2
#vx= yout.sol(TIME)[2,:]
#vy= yout.sol(TIME)[3,:]
plt.close("all")
fig = plt.figure(figsize=(9, 9))
ax = fig.add_subplot(3,1,1,autoscale_on=False, xlim=(0,t_stop),ylim=(x.min(),y.max()+2*offset1+offset2))
trace, = ax.plot([], [], ',-', lw=2)
#ax.set_aspect('equal')
plt.xlabel("Positions of both masses [m]")
ax.grid()
ax1 = fig.add_subplot(3,1,2,autoscale_on=False, xlim =(0,t_stop),ylim=(x.min(),x.max()))
trace1, = ax1.plot([], [], ',-', lw=2)
plt.xlabel("Travel time [s]")
plt.ylabel("Position of mass 1 [m/s]")
ax1.grid()
ax2 = fig.add_subplot(3,1,3,autoscale_on=False, xlim =(0,t_stop),ylim=(y.min(),y.max()))
trace2, = ax2.plot([], [], ',-', lw=2)
plt.xlabel("Travel time [s]")
plt.ylabel("Position of mass 2 [m/s]")
ax2.grid()
line, = ax.plot([], [], '-', lw=2)
patch = plt.Circle((r, -r), r)
ax.add_patch(patch)
time_template = 'time = %.1fs'
time_text = ax.text(0.03, 0.8, '', transform=ax.transAxes)
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
history_x1, history_y1 = deque(maxlen=history_len), deque(maxlen=history_len)
history_x2, history_y2 = deque(maxlen=history_len), deque(maxlen=history_len)
plt.show()
def animate(i):
t = time.time()
if TIME[i]<1:
u=0
else:
u=0.2
thisx = [TIME[i]-L,TIME[i]+L,TIME[i]+L, TIME[i]-L ,TIME[i]-L ]
thisy = [offset2+y[i]+H,offset2+y[i]+H,offset2+y[i]-H,offset2+y[i]-H,offset2+y[i]+H]
line.set_data(thisx, thisy)
time_text.set_text(time_template % (i*dt))
patch.center = (TIME[i],(x[i]+offset1)-(x[i]+offset1-u)/2)
patch.radius = (x[i]+offset1-u)/2
ax.add_patch(patch)
if i == 0:
history_x.clear()
history_y.clear()
history_x.appendleft(TIME[i])
history_y.appendleft(u)
trace.set_data(history_x, history_y)
if i == 0:
history_x1.clear()
history_y1.clear()
history_x2.clear()
history_y2.clear()
history_x1.appendleft(TIME[i])
history_y1.appendleft(x[i])
trace1.set_data(history_x1, history_y1)
history_x2.appendleft(TIME[i])
history_y2.appendleft(y[i])
trace2.set_data(history_x2, history_y2)
elapsed = time.time() - t
time.sleep(dt-elapsed)
return line, time_text
ani = animation.FuncAnimation(fig, animate, frames=len(x), interval=dt*1000, blit=True,repeat=False)
plt.show()