From 963d68196ec757ecd6246954acffb782a1e8e1fe Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sat, 28 Sep 2019 22:43:13 +0200 Subject: [PATCH 01/11] Add thresholding to reduce light update frequency It's not very effective, and Glen says things look fine without, so I'll abandon this branch for now. --- interactions.py | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/interactions.py b/interactions.py index f9cab9c..cc5d9d8 100644 --- a/interactions.py +++ b/interactions.py @@ -55,8 +55,8 @@ clock = pygame.time.Clock() # --- copied from firestar.py -b = phue.Bridge('172.18.130.12') -##b = None +##b = phue.Bridge('172.18.130.12') +b = None Nedges = 30; edgecode = [None] * Nedges; @@ -72,7 +72,9 @@ lasthue = 65535 fullbright = 254 fullsat = 254 -# --- control methods +# --- control methods and diagnostics + +updates = 0 def set_light_hsv(e, color, hit_bridge): command = { @@ -80,8 +82,11 @@ def set_light_hsv(e, color, hit_bridge): 'bri' : color[2]*fullbright, 'sat' : color[1]*fullsat } - if hit_bridge and b != None: - b.set_light(edges[e].light_id, command) + if hit_bridge: + if b != None: + b.set_light(edges[e].light_id, command) + global updates + updates += 1 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]]) @@ -101,7 +106,7 @@ drain_rate = 0.1 charge = 12*[0] current = 30*[0] -last_litness = 30*[float("inf")] +litness = 30*[float("inf")] change_threshold = 0.05 # === main loop === @@ -129,22 +134,33 @@ def wave_evolution(): charge[v] *= 1 - drain_rate def set_wave_light(e, i): - litness = 1 - exp(-2*i*i) - hit_bridge = abs(litness - last_litness[e]) > change_threshold - set_light_hsv(e, (0.45 + litness*0.25, 1 - 0.5*litness*litness, litness), hit_bridge) - if hit_bridge: - last_litness[e] = litness + new_litness = 1 - exp(-2*i*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.45 + litness[e]*0.25, 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge) def show_data(screen): + # light updates + text = font.render('updates', True, (255, 255, 255)) + screen.blit(text, (20, viewsize)) + text = font.render(str(updates), True, (255, 255, 255)) + screen.blit(text, (120, viewsize)) + + # energy energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) text = font.render('energy', True, (255, 255, 255)) - screen.blit(text, (20, viewsize)) - text = font.render(str(energy), True, (255, 255, 255)) - screen.blit(text, (120, viewsize)) - text = font.render('charge[0]', True, (255, 255, 255)) screen.blit(text, (20, 30 + viewsize)) - text = font.render(str(charge[0]), True, (255, 255, 255)) + text = font.render(str(energy), True, (255, 255, 255)) screen.blit(text, (120, 30 + viewsize)) + + # charge[0] + text = font.render('charge[0]', True, (255, 255, 255)) + screen.blit(text, (20, 60 + viewsize)) + text = font.render(str(charge[0]), True, (255, 255, 255)) + screen.blit(text, (120, 60 + viewsize)) if __name__ == '__main__': # set up background From c1b2b45dbdf0d0c798c431bf55bb0ec47b57a2fe Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sat, 28 Sep 2019 23:01:11 +0200 Subject: [PATCH 02/11] Measure moving average of update rate Okay, the threshoding is _kind_ of effective, and the version Glen liked was actually doing it. In light of that, I've quantified the update frequency a bit better. --- interactions.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/interactions.py b/interactions.py index cc5d9d8..ddaa5ce 100644 --- a/interactions.py +++ b/interactions.py @@ -75,6 +75,7 @@ fullsat = 254 # --- control methods and diagnostics updates = 0 +new_updates = 0 def set_light_hsv(e, color, hit_bridge): command = { @@ -85,14 +86,15 @@ def set_light_hsv(e, color, hit_bridge): if hit_bridge: if b != None: b.set_light(edges[e].light_id, command) - global updates - updates += 1 + global new_updates + new_updates += 1 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]]) # === interaction parameters === frame_rate = 24 +ema_weight = 2/(frame_rate+1) inductance = 0.001 @@ -144,9 +146,12 @@ def set_wave_light(e, i): def show_data(screen): # light updates + global updates, new_updates + updates = ema_weight*new_updates + (1 - ema_weight)*updates + new_updates = 0 text = font.render('updates', True, (255, 255, 255)) screen.blit(text, (20, viewsize)) - text = font.render(str(updates), True, (255, 255, 255)) + text = font.render(str(updates / (1 - 0.9)), True, (255, 255, 255)) screen.blit(text, (120, viewsize)) # energy From db4cff118650d58f45a52a04f66e2fdfbe23cf8e Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sat, 28 Sep 2019 23:30:21 +0200 Subject: [PATCH 03/11] Fix vertex adjacency error --- interactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions.py b/interactions.py index f9cab9c..c13478e 100644 --- a/interactions.py +++ b/interactions.py @@ -18,7 +18,7 @@ vertex_adj = [ (0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 8), (2, 6), (6, 10), (10, 4), (4, 8), (2, 9), (2, 11), (2, 5), - (4, 1), (4, 9), (4, 7), + (4, 1), (4, 11), (4, 7), (6, 3), (6, 11), (6, 9), (8, 5), (8, 11), (8, 1), (10, 7), (10, 11), (10, 3), From 4c4d824f6f113c1059bb09a52e35162b86167bac Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 00:05:27 +0200 Subject: [PATCH 04/11] Prepare to merge Glen's changes --- interactions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/interactions.py b/interactions.py index ddaa5ce..8153088 100644 --- a/interactions.py +++ b/interactions.py @@ -79,9 +79,9 @@ new_updates = 0 def set_light_hsv(e, color, hit_bridge): command = { - 'hue' : color[0]*lasthue, - 'bri' : color[2]*fullbright, - 'sat' : color[1]*fullsat + 'hue' : int(color[0]*lasthue), + 'bri' : int(color[2]*fullbright), + 'sat' : int(color[1]*fullsat) } if hit_bridge: if b != None: @@ -93,7 +93,7 @@ def set_light_hsv(e, color, hit_bridge): # === interaction parameters === -frame_rate = 24 +frame_rate = 2 ema_weight = 2/(frame_rate+1) inductance = 0.001 From 2e03b2e1a9edf2e91f728bb8089d177063e675b4 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 00:42:07 +0200 Subject: [PATCH 05/11] Correct moving average computation --- interactions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/interactions.py b/interactions.py index 9f0b0db..f2dd313 100644 --- a/interactions.py +++ b/interactions.py @@ -93,7 +93,9 @@ def set_light_hsv(e, color, hit_bridge): # === interaction parameters === -frame_rate = 2 +# these frame rate and threshold settings are untested, but the exponential +# moving average update frequency suggests they should be just barely in bounds +frame_rate = 10 ema_weight = 2/(frame_rate+1) inductance = 0.001 @@ -109,7 +111,7 @@ charge = 12*[0] current = 30*[0] litness = 30*[float("inf")] -change_threshold = 0.05 +change_threshold = 0.15 # === main loop === @@ -151,7 +153,7 @@ def show_data(screen): new_updates = 0 text = font.render('updates', True, (255, 255, 255)) screen.blit(text, (20, viewsize)) - text = font.render(str(updates / (1 - 0.9)), True, (255, 255, 255)) + text = font.render(str(frame_rate * updates), True, (255, 255, 255)) screen.blit(text, (120, viewsize)) # energy From 0ac76265a083ee167e1e6b69eb031feacf7f25a6 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 01:23:02 +0200 Subject: [PATCH 06/11] Start developing heat evolution --- interactions.py | 89 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/interactions.py b/interactions.py index 71c63b7..073dce1 100644 --- a/interactions.py +++ b/interactions.py @@ -55,8 +55,8 @@ clock = pygame.time.Clock() # --- copied from firestar.py -b = phue.Bridge('172.18.130.12') -##b = None +##b = phue.Bridge('172.18.130.12') +b = None Nedges = 30; edgecode = [None] * Nedges; @@ -87,17 +87,22 @@ def set_light_hsv(e, color, hit_bridge): # === interaction parameters === -frame_rate = 2 +frame_rate = 24 inductance = 0.001 -therm_conduct = 0.001 +therm_conduct = 0.01 drain = False drain_rate = 0.1 # === phase space === +# heat equation +temp = 30*[0] +new_temp = 30*[0] + +# wave equation charge = 12*[0] current = 30*[0] @@ -108,11 +113,13 @@ change_threshold = 0.05 def heat_evolution(): for e, f in cyc_edge_adj: - flow = therm_conduct*(current[e] - current[f]) - current[e] -= flow - current[f] += flow + flow = therm_conduct*(temp[e] - temp[f]) + new_temp[e] -= flow + new_temp[f] += flow + for e in range(30): if drain: - current[v] *= 1 - drain_rate + new_temp[e] *= 1 - drain_rate + temp[e] = max(new_temp[e], 0) def wave_evolution(): # use verlet integration, first updating the currents and then using the new @@ -128,6 +135,15 @@ def wave_evolution(): for v in range(12): charge[v] *= 1 - drain_rate +def set_heat_light(e, i): + litness = 1 - exp(-2*i) + if abs(litness - last_litness[e]) > change_threshold: + hit_bridge = True + last_litness[e] = litness + else: + hit_bridge = False + set_light_hsv(e, (0.167*last_litness[e], 1 - 0.5*last_litness[e]*last_litness[e], last_litness[e]), hit_bridge) + def set_wave_light(e, i): litness = 1 - exp(-2*i*i) hit_bridge = abs(litness - last_litness[e]) > change_threshold @@ -135,6 +151,38 @@ def set_wave_light(e, i): if hit_bridge: last_litness[e] = litness +def handle_heat_events(): + global drain + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_j: + new_temp[0] += 10 + elif event.key == pygame.K_SPACE: + drain = True + elif event.type == pygame.KEYUP: + if event.key == pygame.K_SPACE: + drain = False + elif event.type == pygame.QUIT: + quit() + +def handle_wave_events(): + global drain + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_j: + charge[0] += 100 + charge[11] -= 100 + elif event.key == pygame.K_k: + charge[5] += 100 + charge[7] -= 100 + elif event.key == pygame.K_SPACE: + drain = True + elif event.type == pygame.KEYUP: + if event.key == pygame.K_SPACE: + drain = False + elif event.type == pygame.QUIT: + quit() + def show_data(screen): energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) text = font.render('energy', True, (255, 255, 255)) @@ -158,22 +206,8 @@ if __name__ == '__main__': to_light = 0 while True: # handle events - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_j: - charge[0] += 100 - charge[11] -= 100 - elif event.key == pygame.K_k: - charge[5] += 100 - charge[7] -= 100 - elif event.key == pygame.K_SPACE: - drain = True - pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize)) - elif event.type == pygame.KEYUP: - if event.key == pygame.K_SPACE: - drain = False - elif event.type == pygame.QUIT: - quit() + ##handle_wave_events() + handle_heat_events() # clear screen screen.blit(background, (0, 0)) @@ -183,11 +217,12 @@ if __name__ == '__main__': # show state for e in range(30): - set_wave_light(e, current[e]) + ##set_wave_light(e, current[e]) + set_heat_light(e, temp[e]) # evolve state - wave_evolution() - ##heat_evolution() + ##wave_evolution() + heat_evolution() # step pygame.display.flip() From b11420daab66646f067d0fd7b315c25142af3930 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 03:40:43 +0200 Subject: [PATCH 07/11] Update instructions --- interactions.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/interactions.py b/interactions.py index 073dce1..d3e8b31 100644 --- a/interactions.py +++ b/interactions.py @@ -184,21 +184,22 @@ def handle_wave_events(): quit() def show_data(screen): - energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) + energy = sum(temp) text = font.render('energy', True, (255, 255, 255)) screen.blit(text, (20, viewsize)) text = font.render(str(energy), True, (255, 255, 255)) screen.blit(text, (120, viewsize)) - text = font.render('charge[0]', True, (255, 255, 255)) + text = font.render('temp[0]', True, (255, 255, 255)) screen.blit(text, (20, 30 + viewsize)) - text = font.render(str(charge[0]), True, (255, 255, 255)) + text = font.render(str(temp[0]), True, (255, 255, 255)) screen.blit(text, (120, 30 + viewsize)) if __name__ == '__main__': # 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)) + ##text = font.render('press [j] or [k] to pluck', True, (255, 255, 255)) + text = font.render('press [j] 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)) From 56f42684f3e6f99db1e0211928c1cb872bc08116 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 03:42:13 +0200 Subject: [PATCH 08/11] Turn on bridge connection --- interactions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interactions.py b/interactions.py index d3e8b31..8f478a7 100644 --- a/interactions.py +++ b/interactions.py @@ -55,8 +55,8 @@ clock = pygame.time.Clock() # --- copied from firestar.py -##b = phue.Bridge('172.18.130.12') -b = None +b = phue.Bridge('172.18.130.12') +##b = None Nedges = 30; edgecode = [None] * Nedges; From 9dc6098ff312c13717fca915866a019c429c3b3b Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 16:26:08 +0200 Subject: [PATCH 09/11] Set up heat/wave mode switch --- interactions.py | 117 +++++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 40 deletions(-) diff --git a/interactions.py b/interactions.py index 8328444..a002dc7 100644 --- a/interactions.py +++ b/interactions.py @@ -91,6 +91,40 @@ def set_light_hsv(e, color, hit_bridge): 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]]) +# === 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 === # these frame rate and threshold settings are untested, but the exponential @@ -163,13 +197,15 @@ def set_wave_light(e, i): 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(): - global drain + global drain, mode for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_j: new_temp[0] += 10 elif event.key == pygame.K_SPACE: drain = True + elif event.key == pygame.K_f: + mode = 1 elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: drain = False @@ -177,7 +213,7 @@ def handle_heat_events(): quit() def handle_wave_events(): - global drain + global drain, mode for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_j: @@ -188,6 +224,8 @@ def handle_wave_events(): charge[7] -= 100 elif event.key == pygame.K_SPACE: drain = True + elif event.key == pygame.K_d: + mode = 0 elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: drain = False @@ -199,57 +237,56 @@ def show_data(screen): global updates, new_updates updates = ema_weight*new_updates + (1 - ema_weight)*updates new_updates = 0 - text = font.render('updates', True, (255, 255, 255)) - screen.blit(text, (20, viewsize)) - text = font.render(str(frame_rate * updates), True, (255, 255, 255)) - screen.blit(text, (120, viewsize)) + 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 - ##energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) - energy = sum(temp) - text = font.render('energy', True, (255, 255, 255)) - screen.blit(text, (20, 30 + viewsize)) - text = font.render(str(energy), True, (255, 255, 255)) - screen.blit(text, (120, 30 + viewsize)) + 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)) - # charge[0] - ##text = font.render('charge[0]', True, (255, 255, 255)) - text = font.render('temp[0]', True, (255, 255, 255)) - screen.blit(text, (20, 60 + viewsize)) - ##text = font.render(str(charge[0]), True, (255, 255, 255)) - text = font.render(str(temp[0]), True, (255, 255, 255)) - screen.blit(text, (120, 60 + 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__': - # 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)) - text = font.render('press [j] 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 - ##handle_wave_events() - handle_heat_events() + if mode == 0: + handle_heat_events() + elif mode == 1: + handle_wave_events() # clear screen screen.blit(background, (0, 0)) - # show data + # show data and instructions show_data(screen) - # show state - for e in range(30): - ##set_wave_light(e, current[e]) - set_heat_light(e, temp[e]) - - # evolve state - ##wave_evolution() - heat_evolution() + # show state and evolve 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): + set_wave_light(e, current[e]) + wave_evolution() # step pygame.display.flip() From fef5385da77cf21e9d272cb345cbcf50f1389c07 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 16:28:28 +0200 Subject: [PATCH 10/11] Reorient display This reflects the way the FireStar sits on the ground. --- interactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions.py b/interactions.py index a002dc7..ea14c44 100644 --- a/interactions.py +++ b/interactions.py @@ -10,7 +10,7 @@ import phue, pygame, colorsys viewsize = 600 shrink = 0.9 vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in - [(0, 0)] + [(cos(-n*pi/5), sin(-n*pi/5)) for n in range(10)] + [(0, 0)] + [(0, 0)] + [(sin(n*pi/5), -cos(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) From d54d4628af480449f41a74a224f07a542815cbe3 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 29 Sep 2019 16:35:06 +0200 Subject: [PATCH 11/11] Turn on bridge connection --- interactions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interactions.py b/interactions.py index ea14c44..9fd3e86 100644 --- a/interactions.py +++ b/interactions.py @@ -55,8 +55,8 @@ clock = pygame.time.Clock() # --- copied from firestar.py -##b = phue.Bridge('172.18.130.12') -b = None +b = phue.Bridge('172.18.130.12') +##b = None Nedges = 30; edgecode = [None] * Nedges;