Compare commits

..

No commits in common. "fef5385da77cf21e9d272cb345cbcf50f1389c07" and "9973abad787d5113f209e8714988b3fae83e8588" have entirely different histories.

View File

@ -10,7 +10,7 @@ import phue, pygame, colorsys
viewsize = 600 viewsize = 600
shrink = 0.9 shrink = 0.9
vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in
[(0, 0)] + [(sin(n*pi/5), -cos(n*pi/5)) for n in range(10)] + [(0, 0)] [(0, 0)] + [(cos(-n*pi/5), sin(-n*pi/5)) for n in range(10)] + [(0, 0)]
] ]
# edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C) # edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C)
@ -55,8 +55,8 @@ clock = pygame.time.Clock()
# --- copied from firestar.py # --- copied from firestar.py
##b = phue.Bridge('172.18.130.12') b = phue.Bridge('172.18.130.12')
b = None ##b = None
Nedges = 30; Nedges = 30;
edgecode = [None] * Nedges; edgecode = [None] * Nedges;
@ -72,10 +72,7 @@ lasthue = 65535
fullbright = 254 fullbright = 254
fullsat = 254 fullsat = 254
# --- control methods and diagnostics # --- control methods
updates = 0
new_updates = 0
def set_light_hsv(e, color, hit_bridge): def set_light_hsv(e, color, hit_bridge):
command = { command = {
@ -83,86 +80,39 @@ def set_light_hsv(e, color, hit_bridge):
'bri' : int(color[2]*fullbright), 'bri' : int(color[2]*fullbright),
'sat' : int(color[1]*fullsat) 'sat' : int(color[1]*fullsat)
} }
if hit_bridge: if hit_bridge and b != None:
if b != None:
b.set_light(edges[e].light_id, command) b.set_light(edges[e].light_id, command)
global new_updates
new_updates += 1
rgb_color = tuple(255*c for c in colorsys.hsv_to_rgb(*color)) rgb_color = tuple(255*c for c in colorsys.hsv_to_rgb(*color))
pygame.draw.aaline(screen, rgb_color, vertices[vertex_adj[e][0]], vertices[vertex_adj[e][1]]) pygame.draw.aaline(screen, rgb_color, vertices[vertex_adj[e][0]], vertices[vertex_adj[e][1]])
# === messages and mode selection ===
mode = 0
# data labels
updates_label = font.render('updates', True, (255, 255, 255))
energy_label = font.render('energy', True, (255, 255, 255))
position_label = [
font.render('temp[0]', True, (255, 255, 255)),
font.render('charge[0]', True, (255, 255, 255))
]
# instructions
mode_inds = [
[
font.render('[d] heat', True, (255, 255, 255)),
font.render('[f] wave', True, (150, 150, 150))
],
[
font.render('[d] heat', True, (150, 150, 150)),
font.render('[f] wave', True, (255, 255, 255))
]
]
bop_instr = [
font.render('press [j] to bop', True, (255, 255, 255)),
font.render('press [j] or [k] to bop', True, (255, 255, 255))
]
drain_instr = font.render('hold [space] to drain', True, (255, 255, 255))
# set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
background.blit(drain_instr, (340, 60 + viewsize))
# === interaction parameters === # === interaction parameters ===
# these frame rate and threshold settings are untested, but the exponential frame_rate = 2
# moving average update frequency suggests they should be just barely in bounds
frame_rate = 10
ema_weight = 2/(frame_rate+1)
inductance = 0.001 inductance = 0.001
therm_conduct = 0.01 therm_conduct = 0.001
drain = False drain = False
drain_rate = 0.1 drain_rate = 0.1
# === phase space === # === phase space ===
# heat equation
temp = 30*[0]
new_temp = 30*[0]
# wave equation
charge = 12*[0] charge = 12*[0]
current = 30*[0] current = 30*[0]
litness = 30*[float("inf")] last_litness = 30*[float("inf")]
change_threshold = 0.15 change_threshold = 0.05
# === main loop === # === main loop ===
def heat_evolution(): def heat_evolution():
for e, f in cyc_edge_adj: for e, f in cyc_edge_adj:
flow = therm_conduct*(temp[e] - temp[f]) flow = therm_conduct*(current[e] - current[f])
new_temp[e] -= flow current[e] -= flow
new_temp[f] += flow current[f] += flow
for e in range(30):
if drain: if drain:
new_temp[e] *= 1 - drain_rate current[v] *= 1 - drain_rate
temp[e] = max(new_temp[e], 0)
def wave_evolution(): def wave_evolution():
# use verlet integration, first updating the currents and then using the new # use verlet integration, first updating the currents and then using the new
@ -178,42 +128,36 @@ def wave_evolution():
for v in range(12): for v in range(12):
charge[v] *= 1 - drain_rate charge[v] *= 1 - drain_rate
def set_heat_light(e, i):
new_litness = 1 - exp(-2*i)
if abs(new_litness - litness[e]) > change_threshold:
hit_bridge = True
litness[e] = new_litness
else:
hit_bridge = False
set_light_hsv(e, (0.167*litness[e], 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
def set_wave_light(e, i): def set_wave_light(e, i):
new_litness = 1 - exp(-2*i*i) litness = 1 - exp(-2*i*i)
if abs(new_litness - litness[e]) > change_threshold: hit_bridge = abs(litness - last_litness[e]) > change_threshold
hit_bridge = True set_light_hsv(e, (0.45 + litness*0.25, 1 - 0.5*litness*litness, litness), hit_bridge)
litness[e] = new_litness if hit_bridge:
else: last_litness[e] = litness
hit_bridge = False
set_light_hsv(e, (0.45 + litness[e]*0.25, 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
def handle_heat_events(): def show_data(screen):
global drain, mode energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
for event in pygame.event.get(): text = font.render('energy', True, (255, 255, 255))
if event.type == pygame.KEYDOWN: screen.blit(text, (20, viewsize))
if event.key == pygame.K_j: text = font.render(str(energy), True, (255, 255, 255))
new_temp[0] += 10 screen.blit(text, (120, viewsize))
elif event.key == pygame.K_SPACE: text = font.render('charge[0]', True, (255, 255, 255))
drain = True screen.blit(text, (20, 30 + viewsize))
elif event.key == pygame.K_f: text = font.render(str(charge[0]), True, (255, 255, 255))
mode = 1 screen.blit(text, (120, 30 + viewsize))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def handle_wave_events(): if __name__ == '__main__':
global drain, mode # set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
text = font.render('press [j] or [k] to pluck', True, (255, 255, 255))
background.blit(text, (340, viewsize))
text = font.render('hold [space] to drain', True, (255, 255, 255))
background.blit(text, (340, 30 + viewsize))
to_light = 0
while True:
# handle events
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j: if event.key == pygame.K_j:
@ -224,69 +168,26 @@ def handle_wave_events():
charge[7] -= 100 charge[7] -= 100
elif event.key == pygame.K_SPACE: elif event.key == pygame.K_SPACE:
drain = True drain = True
elif event.key == pygame.K_d: pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize))
mode = 0
elif event.type == pygame.KEYUP: elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE: if event.key == pygame.K_SPACE:
drain = False drain = False
elif event.type == pygame.QUIT: elif event.type == pygame.QUIT:
quit() quit()
def show_data(screen):
# light updates
global updates, new_updates
updates = ema_weight*new_updates + (1 - ema_weight)*updates
new_updates = 0
updates_value = font.render(str(frame_rate * updates), True, (255, 255, 255))
screen.blit(updates_label, (20, viewsize))
screen.blit(updates_value, (120, viewsize))
# energy
if mode == 0:
energy = sum(temp)
elif mode == 1:
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
energy_value = font.render(str(energy), True, (255, 255, 255))
screen.blit(energy_label, (20, 30 + viewsize))
screen.blit(energy_value, (120, 30 + viewsize))
# a position
##position_value = None
if mode == 0:
position_value = font.render(str(temp[0]), True, (255, 255, 255))
elif mode == 1:
position_value = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(position_label[mode], (20, 60 + viewsize))
screen.blit(position_value, (120, 60 + viewsize))
# instructions
screen.blit(mode_inds[mode][0], (340, viewsize))
screen.blit(mode_inds[mode][1], (420, viewsize))
screen.blit(bop_instr[mode], (340, 30 + viewsize))
if __name__ == '__main__':
while True:
# handle events
if mode == 0:
handle_heat_events()
elif mode == 1:
handle_wave_events()
# clear screen # clear screen
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# show data and instructions # show data
show_data(screen) show_data(screen)
# show state and evolve state # show state
if mode == 0:
for e in range(30):
set_heat_light(e, temp[e])
heat_evolution()
elif mode == 1:
for e in range(30): for e in range(30):
set_wave_light(e, current[e]) set_wave_light(e, current[e])
# evolve state
wave_evolution() wave_evolution()
##heat_evolution()
# step # step
pygame.display.flip() pygame.display.flip()