Demo entry 4035558

mechanismPlot

   

Submitted by anonymous on Mar 14, 2016 at 10:13
Language: Python. Code size: 5.9 kB.

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Rectangle, Button
import mechanismPositionCalc as mc
import numpy as np

# import matplotlib as mpl
# mpl.rc('text', usetex=True)

# ---- Initial Data ---- #
zero = [0, -150.0]                                  # [X, Y] coordinates of sensor position in mm
rod0 = 260.0                                        # Rod that connects steering rack to sensorArm in mm
steer0 = 0                                          # Instant rack travel in mm
sensorArm0 = 100.0                                  # Arm connected to sensor in mm
totalRackTravel = 150                               # Total possible rack travel in mm
pos = mc.mechanismCalc(240.0, sensorArm0, rod0, steer0, zero).positionCalc()    # returns the [X,Y] of all three points
t = [i[0] for i in pos]                             # X coordinates for A, B and C points of triangle
s = [i[1] for i in pos]                             # Y coordinates for A, B and C points of triangle

rackTravel = np.arange(-totalRackTravel/2, totalRackTravel/2 + 1, 1)
teta = np.zeros(len(rackTravel))
for i in range(len(rackTravel)):
    angles = mc.mechanismCalc(240.0, sensorArm0, rod0, rackTravel[i], zero).anglesCalc()
    teta[i] = angles[3]

# ---- Interactive Plot ---- #
fig = plt.figure(figsize=(16, 12), dpi=80)
ax = plt.subplot(224)
plt.subplots_adjust(left=0.1, bottom=0.05, hspace=0.3)
l5, = plt.plot([zero[0], zero[0]], [zero[1] - 25, zero[1] + 25], lw=3, color='green', label=r'Suspension Travel $\pm$ 25 mm')
l, = plt.plot(t, s, lw=2, color='red', label='Mechanism')
l4, = plt.plot(zero[0], zero[1], 'ro', label='Sensor')
plt.plot([165, 315], [0, 0], label='Rack travel')
plt.axis([-180, 350, -250, 100])
plt.grid(True)
plt.legend(loc=1, prop={'size': 8}, numpoints=1)
plt.gca().invert_yaxis()
plt.gca().set_aspect('equal', adjustable='box')

ax2 = plt.subplot(222)
l2, = plt.plot(rackTravel*9.6, teta - min(teta), color='magenta', label='M')
plt.ylabel(r'$\theta$: sensor angle (degrees)')
plt.xlabel('Steering Wheel (degrees)')
plt.title('Steering wheel angle vs sensor angle')
plt.axis([-800, 800, 0, 180])
plt.grid(True)

ax3 = plt.subplot(221)
l3, = plt.plot(rackTravel, teta - min(teta), color='green', label='M')
plt.ylabel(r'$\theta$: sensor angle (degrees)')
plt.xlabel('Rack travel (mm)')
plt.title('Rack travel vs sensor angle')
plt.axis([-totalRackTravel/2 - 15, totalRackTravel/2 + 15, 0, 180])
plt.grid(True)

# ax4 = fig.add_subplot(224)
# plt.plot()
# plt.axis([0, 100, 0, 100])
# plt.axis('Off')
# text1 = r'''\begin{equation} \theta = 180 - \arccos \left(-\frac{d^2 - s^2 - c^2}{2sc}\right) - \arcsin \left(\frac{s \sin\alpha}{d} \right) \end{equation}'''
# plt.text(0, 80, text1, size=14)
# text2 = r'''where: \\ \\ $d$ = distance between sensor and connection point \\  $s$ = sensorArm \\ $c$ = connectigRod \\ $\alpha$ = angle between sensorArm and connectingRod'''
# plt.text(0, 40, text2, size=12)

# ---- Axes Areas ---- #
axcolor = 'white'
axsensor = plt.axes([0.15, 0.3, 0.25, 0.015], axisbg=axcolor)
axrod  = plt.axes([0.15, 0.275, 0.25, 0.015], axisbg=axcolor)
axsteer = plt.axes([0.15, 0.25, 0.25, 0.015], axisbg=axcolor)
axzerox = plt.axes([0.15, 0.225, 0.25, 0.015], axisbg=axcolor)
axzeroy = plt.axes([0.15, 0.2, 0.25, 0.015], axisbg=axcolor)

# ---- Button Areas ---- #
resetax = plt.axes([0.3, 0.125, 0.1, 0.04])
calculateax = plt.axes([0.1, 0.125, 0.15, 0.04])

# ---- Sliders and Button ---- #
ssteer = Slider(axsteer, 'Steer', -totalRackTravel/2, totalRackTravel/2, valinit=steer0, color='darkblue')
srod = Slider(axrod, 'Connection Rod [mm]', 140.0, 380.0, valinit=rod0, color='lightgrey')
ssensor = Slider(axsensor, 'Sensor Arm [mm]', 20.0, 180.0, valinit=sensorArm0, color='lightgrey')
szerox = Slider(axzerox, 'Sensor X [mm]', -100.0, 100.0, valinit=zero[0], color='lightgrey')
szeroy = Slider(axzeroy, 'Sensor Y [mm]', -200.0, 0.0, valinit=zero[1], color='lightgrey')
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
buttoncalculate = Button(calculateax, 'Calculate Angles', color=axcolor, hovercolor='0.975')


# ---- Update values function ---- #
def update(val):
    rod = srod.val
    steer = ssteer.val
    sensor = ssensor.val
    zeroxy = [szerox.val, szeroy.val]
    positionTest = mc.mechanismCalc(240.0, sensor, rod, steer, zeroxy)
    pos = positionTest.positionCalc()
    l.set_ydata([i[1] for i in pos])
    l.set_xdata([i[0] for i in pos])
    l4.set_ydata(pos[0][1])
    l4.set_xdata(pos[0][0])
    suspensionTravely = [pos[0][1] - 25, pos[0][1] + 25]
    suspensionTravelx = [pos[0][0], pos[0][0]]
    l5.set_ydata(suspensionTravely)
    l5.set_xdata(suspensionTravelx)
    positionTest.validateSizes()
    plt.draw()


# ---- Reset values function ---- #
def reset(event):
    ssteer.reset()
    srod.reset()
    ssensor.reset()
    szerox.reset()
    szeroy.reset()


# ---- Calculate sensor angle values function ---- #
def calculateAngles(event):
    rod = srod.val
    steer = ssteer.val
    sensor = ssensor.val
    zeroxy = [szerox.val, szeroy.val]
    l2.set_xdata(rackTravel*9.6)
    l3.set_xdata(rackTravel)
    tetalocal = np.zeros(len(rackTravel))
    for i in range(len(rackTravel)):
        angles = mc.mechanismCalc(240.0, sensor, rod, rackTravel[i], zeroxy).anglesCalc()
        tetalocal[i] = angles[3]
    l2.set_ydata(tetalocal - min(tetalocal))
    l3.set_ydata(tetalocal - min(tetalocal))
    plt.draw()

# ---- Actions that call functions ---- #
ssteer.on_changed(update)
srod.on_changed(update)
ssensor.on_changed(update)
szerox.on_changed(update)
szeroy.on_changed(update)
button.on_clicked(reset)
button.on_clicked(calculateAngles)
buttoncalculate.on_clicked(calculateAngles)

# ---- Show figure with plots ---- #
plt.show()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).