added current and resistance calculations and graphs
This commit is contained in:
parent
1fc270deb4
commit
236df3a192
Binary file not shown.
Binary file not shown.
Binary file not shown.
91
main.py
91
main.py
@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import math
|
||||||
from particle import Particle
|
from particle import Particle
|
||||||
from sensor import Sensor
|
from sensor import Sensor
|
||||||
from slider import Slider
|
from slider import Slider
|
||||||
@ -11,42 +12,67 @@ SCREEN_WIDTH = 800
|
|||||||
SCREEN_HEIGHT = 600
|
SCREEN_HEIGHT = 600
|
||||||
|
|
||||||
SENSOR_DISTANCE = 200
|
SENSOR_DISTANCE = 200
|
||||||
|
REST_MEDIUM = 180000
|
||||||
|
|
||||||
y_lim = 40000
|
y_lim = 40000
|
||||||
|
y_lim2 = 0.000000000005
|
||||||
|
|
||||||
|
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
|
||||||
sensor = Sensor(width = 50, distance = SENSOR_DISTANCE, space = 300)
|
sensor = Sensor(width = 50, distance = SENSOR_DISTANCE, space = 300)
|
||||||
|
sensor.inputVoltage(5, -5)
|
||||||
|
|
||||||
silica = Particle(speed = 1, size = 60, perm = 4)
|
|
||||||
|
|
||||||
|
silica = Particle(speed = 1, size = 60, perm = 4, rest = pow(10, 12))
|
||||||
|
|
||||||
time = .1
|
time = .1
|
||||||
time_data = []
|
time_data = []
|
||||||
volume_data = []
|
volume_data = []
|
||||||
|
sensor_data = []
|
||||||
|
rest_data = []
|
||||||
|
current1_data = []
|
||||||
|
current2_data = []
|
||||||
|
|
||||||
|
|
||||||
plt.ion()
|
plt.ion()
|
||||||
fig, ax = plt.subplots()
|
fig, (ax, ax2) = plt.subplots(2, 1, figsize=(10, 10))
|
||||||
line, = ax.plot([], [], 'r-')
|
line, = ax.plot([], [], 'r-')
|
||||||
ax.set_xlim(0, 800)
|
line2, = ax.plot([], [], 'g-')
|
||||||
ax.set_ylim(-1000, y_lim)
|
line3, = ax2.plot([], [], 'b-')
|
||||||
|
line4, = ax2.plot([], [], 'g-')
|
||||||
|
ax.set_xlim(0, 900)
|
||||||
|
ax.set_ylim(-0.01, y_lim)
|
||||||
ax.set_xlabel('Time (s)')
|
ax.set_xlabel('Time (s)')
|
||||||
ax.set_ylabel('Volume')
|
ax.set_ylabel('Volume')
|
||||||
ax.set_title('Volume/time')
|
ax.set_title('Volume/time')
|
||||||
|
|
||||||
|
ax2.set_xlim(0, 900)
|
||||||
|
ax2.set_ylim(-1 * y_lim2, y_lim2)
|
||||||
|
ax2.set_xlabel('Time (s)')
|
||||||
|
ax2.set_ylabel('Current')
|
||||||
|
ax2.set_title('Current/time')
|
||||||
|
|
||||||
slider1 = Slider(20, 20, 100, 20, 20, SENSOR_DISTANCE / 2, 80)
|
slider1 = Slider(20, 20, 100, 20, 20, SENSOR_DISTANCE / 2, 80)
|
||||||
|
slider2 = Slider(20, 50, 100, 20, .1, 10, 1)
|
||||||
|
slider3 = Slider(20, 80, 100, 20, 1, 100, 10)
|
||||||
|
|
||||||
run = True
|
run = True
|
||||||
while run:
|
while run:
|
||||||
|
|
||||||
|
timeScale = slider2.value
|
||||||
|
sensor.inputVoltage(slider3.value, -1 * slider3.value)
|
||||||
|
|
||||||
distance = silica.move(time)
|
distance = silica.move(time)
|
||||||
if distance > SCREEN_WIDTH + (silica.size * 2):
|
if distance > SCREEN_WIDTH + (silica.size * 2):
|
||||||
time =.1
|
time =.1
|
||||||
time_data = []
|
time_data = []
|
||||||
volume_data = []
|
volume_data = []
|
||||||
|
sensor_data = []
|
||||||
|
rest_data = []
|
||||||
|
current1_data = []
|
||||||
|
current2_data = []
|
||||||
|
|
||||||
screen.fill((0,0,0))
|
screen.fill((0,0,0))
|
||||||
|
|
||||||
@ -56,6 +82,8 @@ while run:
|
|||||||
pygame.draw.circle(screen, (0,255,0), (distance - silica.size, 300), 10)
|
pygame.draw.circle(screen, (0,255,0), (distance - silica.size, 300), 10)
|
||||||
|
|
||||||
slider1.draw(screen)
|
slider1.draw(screen)
|
||||||
|
slider2.draw(screen)
|
||||||
|
slider3.draw(screen)
|
||||||
|
|
||||||
silica.updateSize(slider1.value)
|
silica.updateSize(slider1.value)
|
||||||
|
|
||||||
@ -63,26 +91,77 @@ while run:
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
run = False
|
run = False
|
||||||
slider1.handle_event(event)
|
slider1.handle_event(event)
|
||||||
|
slider2.handle_event(event)
|
||||||
|
slider3.handle_event(event)
|
||||||
|
|
||||||
volume = sensor.getParticleVolume(distance, silica)
|
volume = sensor.getParticleVolume(distance, silica)
|
||||||
|
|
||||||
|
sensor_data_volume = sensor.volume - volume
|
||||||
|
sensor_data.append(sensor_data_volume)
|
||||||
|
|
||||||
|
sensor_resistance = REST_MEDIUM * ((pow(sensor.distance, 2) * pow(10, -18)) / (sensor_data_volume * pow(10, -27)))
|
||||||
|
nom_sens_res = REST_MEDIUM * ((sensor.distance * pow(10, -9)) / (sensor.width * sensor.distance * pow(10, -18)))
|
||||||
|
|
||||||
|
if volume:
|
||||||
|
particle_resistance = silica.rest * pow((3/(16 * pow(math.pi, 2) * volume * pow(10, -9))), 1/3)
|
||||||
|
total_resistance_inv = (1 / particle_resistance) + (1 / sensor_resistance)
|
||||||
|
else:
|
||||||
|
particle_resistance = 0
|
||||||
|
total_resistance_inv = 1 / sensor_resistance
|
||||||
|
|
||||||
|
total_resistance = 1 / total_resistance_inv
|
||||||
|
|
||||||
|
current1 = 0
|
||||||
|
current2 = 0
|
||||||
|
|
||||||
|
|
||||||
|
which_sensor = sensor.whichSensor(distance, silica)
|
||||||
|
if which_sensor == 1:
|
||||||
|
current1 = sensor.voltage1 / total_resistance
|
||||||
|
current2 = sensor.voltage2 / nom_sens_res
|
||||||
|
elif which_sensor == 2:
|
||||||
|
current2 = sensor.voltage2 / total_resistance
|
||||||
|
current1 = sensor.voltage1 / nom_sens_res
|
||||||
|
else:
|
||||||
|
current1 = sensor.voltage1 / nom_sens_res
|
||||||
|
current2 = sensor.voltage2 / nom_sens_res
|
||||||
|
|
||||||
|
current1_data.append(current1)
|
||||||
|
current2_data.append(current2)
|
||||||
|
print(f"{current1} = {sensor.voltage1} / {total_resistance}")
|
||||||
|
rest_data.append(total_resistance)
|
||||||
|
|
||||||
if (volume > y_lim):
|
if (volume > y_lim):
|
||||||
y_lim = volume + (volume * 1.2)
|
y_lim = volume + (volume * 1.2)
|
||||||
ax.set_ylim(-1000, y_lim)
|
ax.set_ylim(-1000, y_lim)
|
||||||
|
|
||||||
|
if (current1 > y_lim2):
|
||||||
|
y_lim2 = current1 + (current1 * 1.2)
|
||||||
|
ax2.set_ylim(-1 * y_lim2, y_lim2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
time_data.append(time)
|
time_data.append(time)
|
||||||
volume_data.append(volume)
|
volume_data.append(volume)
|
||||||
|
|
||||||
line.set_xdata(time_data)
|
line.set_xdata(time_data)
|
||||||
line.set_ydata(volume_data)
|
line.set_ydata(volume_data)
|
||||||
|
line2.set_xdata(time_data)
|
||||||
|
line2.set_ydata(sensor_data)
|
||||||
|
line3.set_xdata(time_data)
|
||||||
|
line3.set_ydata(current1_data)
|
||||||
|
line4.set_xdata(time_data)
|
||||||
|
line4.set_ydata(current2_data)
|
||||||
ax.relim()
|
ax.relim()
|
||||||
ax.autoscale_view()
|
ax.autoscale_view()
|
||||||
|
ax2.relim()
|
||||||
|
ax2.autoscale_view()
|
||||||
plt.draw()
|
plt.draw()
|
||||||
plt.pause(0.01)
|
plt.pause(0.01)
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
time = time + 1
|
time = timeScale + time
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
class Particle:
|
class Particle:
|
||||||
def __init__(self, speed, size, perm):
|
def __init__(self, speed, size, perm, rest):
|
||||||
self.speed = speed
|
self.speed = speed
|
||||||
self.size = size
|
self.size = size
|
||||||
self.perm = perm
|
self.perm = perm
|
||||||
|
self.rest = rest
|
||||||
self.volume = (4/3.0) * math.pi * size * size * size
|
self.volume = (4/3.0) * math.pi * size * size * size
|
||||||
|
|
||||||
def move(self, time):
|
def move(self, time):
|
||||||
|
|||||||
BIN
pics/Figure_1.png
Normal file
BIN
pics/Figure_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
61
sensor.py
61
sensor.py
@ -5,6 +5,7 @@ class Sensor:
|
|||||||
self.width = width
|
self.width = width
|
||||||
self.distance = distance
|
self.distance = distance
|
||||||
self.space = space
|
self.space = space
|
||||||
|
self.volume = width * pow(distance, 2)
|
||||||
|
|
||||||
def generate(self, screenWidth, screenHeight, screen):
|
def generate(self, screenWidth, screenHeight, screen):
|
||||||
self.sensor1_x = (screenWidth / 2) - (self.space / 2) - self.width
|
self.sensor1_x = (screenWidth / 2) - (self.space / 2) - self.width
|
||||||
@ -34,45 +35,18 @@ class Sensor:
|
|||||||
pygame.draw.rect(screen, (0, 0, 255), sensor2b)
|
pygame.draw.rect(screen, (0, 0, 255), sensor2b)
|
||||||
|
|
||||||
def testSensor1(self, partCenter, particle):
|
def testSensor1(self, partCenter, particle):
|
||||||
|
if (particle.size >= abs(self.inner1 - (partCenter - particle.size))) and (particle.size >= abs(self.outer1 - (partCenter - particle.size))):
|
||||||
particle_x = partCenter - particle.size
|
volume = ((particle.volume / 2) - (particle.partialVol(particle.size - ((partCenter - particle.size) - self.inner1)))) + ((particle.volume / 2) - particle.partialVol(particle.size - (self.outer1 - (partCenter - particle.size))))
|
||||||
particle_right = particle_x + particle.size
|
|
||||||
particle_left = particle_x - particle.size
|
|
||||||
# Sensor lines on one half of sphere center
|
|
||||||
if particle_right > self.outer1 and particle_x < self.inner1:
|
|
||||||
volume = particle.partialVol(self.width + (particle_right - self.outer1)) - particle.partialVol(particle_right - self.outer1)
|
|
||||||
print("On right half")
|
|
||||||
print(volume)
|
|
||||||
return volume
|
return volume
|
||||||
# if Sensor is on left half of sphere center
|
elif particle.size >= abs(self.inner1 - (partCenter - particle.size)):
|
||||||
elif particle_left < self.inner1 and particle_x > self.outer1:
|
volume = particle.partialVol(particle.size - (self.inner1 - (partCenter - particle.size)))
|
||||||
volume = particle.partialVol(self.width + (self.inner1 - particle_left)) - particle.partialVol(self.inner1 - particle_left)
|
|
||||||
print("On left half")
|
|
||||||
print(volume)
|
|
||||||
return volume
|
return volume
|
||||||
# On bolth halves
|
elif particle.size >= abs(self.outer1 - (partCenter - particle.size)):
|
||||||
elif (particle_x >= self.inner1 and particle_x <= self.outer1) and particle_left < self.inner1 and particle_right > self.outer1:
|
volume = particle.volume - particle.partialVol(particle.size - (self.outer1 - (partCenter - particle.size)))
|
||||||
volume_left = particle.partialVol(self.inner1 - particle_left)
|
|
||||||
volume_right = particle.partialVol(particle_right - self.outer1)
|
|
||||||
volume = particle.volume - (volume_left + volume_right)
|
|
||||||
print("On both halves")
|
|
||||||
print(volume_left)
|
|
||||||
print(volume_right)
|
|
||||||
return volume
|
return volume
|
||||||
elif (particle_right > self.inner1 and particle_right < self.outer1) and particle_x <= self.inner1:
|
elif ((partCenter - particle.size) >= self.inner1 and (partCenter - particle.size) <= self.outer1):
|
||||||
volume = particle.partialVol(particle_right - self.inner1)
|
volume = particle.volume
|
||||||
print("Approaching from left")
|
|
||||||
print(volume)
|
|
||||||
return volume
|
return volume
|
||||||
elif (particle_left > self.inner1 and particle_left < self.outer1) and particle_x >= self.outer1:
|
|
||||||
volume = particle.partialVol(self.outer1 - particle_left)
|
|
||||||
print("Leaving from left")
|
|
||||||
print(volume)
|
|
||||||
return volume
|
|
||||||
elif (particle_right > self.inner1 and particle_right <= self.outer1) and (particle_left >= self.inner1 and particle_left < self.outer1):
|
|
||||||
print("in between")
|
|
||||||
print(particle.volume)
|
|
||||||
return particle.volume
|
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ -94,6 +68,7 @@ class Sensor:
|
|||||||
|
|
||||||
def getParticleVolume(self, partCenter, particle):
|
def getParticleVolume(self, partCenter, particle):
|
||||||
volume1 = self.testSensor1(partCenter, particle)
|
volume1 = self.testSensor1(partCenter, particle)
|
||||||
|
#volume1 = 0
|
||||||
volume2 = self.testSensor2(partCenter, particle)
|
volume2 = self.testSensor2(partCenter, particle)
|
||||||
|
|
||||||
if volume1:
|
if volume1:
|
||||||
@ -102,3 +77,19 @@ class Sensor:
|
|||||||
return volume2
|
return volume2
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def whichSensor(self, partCenter, particle):
|
||||||
|
volume1 = self.testSensor1(partCenter, particle)
|
||||||
|
#volume1 = 0
|
||||||
|
volume2 = self.testSensor2(partCenter, particle)
|
||||||
|
|
||||||
|
if volume1:
|
||||||
|
return 1
|
||||||
|
elif volume2:
|
||||||
|
return 2
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def inputVoltage(self, voltage1, voltage2):
|
||||||
|
self.voltage1 = voltage1
|
||||||
|
self.voltage2 = voltage2
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user