Skip to content

Commit

Permalink
Add some example code for the python control module
Browse files Browse the repository at this point in the history
  • Loading branch information
Wetmelon committed Aug 14, 2020
1 parent 1e39702 commit 9f1be40
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions analysis/Simulation/TranslationalMass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import matplotlib.pyplot as plt
from control.matlab import *
import numpy as np


# Input: Current (A)
# Output: Torque (Nm)
Expand All @@ -26,10 +28,35 @@ def mass(m, b, k):
def pulley(r):
return tf(r, 1)

sys = series(motor(2.5), pulley(0.015), mass(0.10, 0, 0))
# Make s a transfer function s/1
s = tf('s')
print(s)

# build a new transfer function using our variable s as a handy placeholder
sys = 1 / (s*s + s + 1)
print(sys)

# Hit the system with a step command
yout, T = step(sys)
plt.plot(T, yout)

# convert our continuous time model to discrete time via Tustin at 0.01s timestep
sysd = c2d(tf(sys), 0.01, method='tustin')
print(sysd)

# Hit the discrete system with a step command, and sample it at 0.01 timestep from 0 to 14 seconds
yout, T = step(sysd, np.arange(0, 14, 0.01))
plt.plot(T, yout)
plt.legend(['Continuous', 'Discrete'])

# Build a system based on the series connection of the motor, pulley, and mass "blocks"
sys = series(motor(2.5), pulley(0.015), mass(0.10, .1, .1))
print(tf(sys))

# Step our series system, returning y (outputs) and x (states)
yout, T, xout = step(sys, return_x=True)
print(yout)
# plt.plot(T, yout)
plt.figure()
plt.plot(T, yout)
plt.plot(T, xout)
plt.legend(['Displacement', 'Velocity'])
plt.legend(['Displacement', r'$x$', r'$\dot{x}$'])
plt.show()

0 comments on commit 9f1be40

Please sign in to comment.