From ad5eb5bbb3e353cb392efaf2fa373df0ad50f3a0 Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Tue, 20 Aug 2024 09:03:00 -0500 Subject: [PATCH] Update runtime --- .gitignore | 3 +- config.yml | 2 +- run.py | 226 +++++++++++++++++++++++++++++------------------------ 3 files changed, 125 insertions(+), 106 deletions(-) diff --git a/.gitignore b/.gitignore index bd6e02a..172625c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ map*.png build # Generated label images labels -temp \ No newline at end of file +temp +pick_count.txt \ No newline at end of file diff --git a/config.yml b/config.yml index 3a9f9ec..c6a5bd6 100644 --- a/config.yml +++ b/config.yml @@ -6,7 +6,7 @@ core: loopspeed: 60 # fps mqtt: - enabled: False + enabled: True server: 172.31.108.4 arm: diff --git a/run.py b/run.py index f1b4e38..f48057c 100755 --- a/run.py +++ b/run.py @@ -661,6 +661,7 @@ def mainloop_server(pool, manager): global mainloop_get global cable_list_state global scan_value + global sensors global oldmode global arm_updates global animation_wait @@ -675,6 +676,7 @@ def mainloop_server(pool, manager): global arm_distance_old global arm_distance_total global placed + global spot if mode != oldmode: print(" ***** Running mode:", mode, "***** ") @@ -924,81 +926,7 @@ def mainloop_server(pool, manager): pass - if mode == "Idle": - # do nothing - if arm_ready is False: - pass - - else: - global mainloop_get - - - if not mainloop_get.empty(): - global sensors - action, get_cable = mainloop_get.get() - if action == "show": - animation_wait = False - ring_animation = get_cable - start_animation = True - led_set_mode = "Show" - fprint("Showing cable at position " + str(get_cable)) - elif action == "shutdown": - fprint("SHUTTING DOWN!!") - pool.apply_async(ur5_control.move_to_packup, (arm,), callback=arm_start_callback, error_callback=handle_error) - sleep(30) - killme.set(1) - elif action == "returnCheck": - print("Checking sensors..") - if real: - newtube = get_full_spot(sensors) - else: - newtube = -1 - if newtube >= 0: - # need to return a cable - mainloop_get.put(("return", newtube)) - mainloop_get.put(("returnCheck", 0)) - else: - fprint("Movement requested. Keep clear of the machine!") - - placed = 0 - arm_distance_total = 0 - #mqtt_send("{\"value\": " + str(time.time() * 1000) + " }", "cycle_start") - cycle_start_time = int(time.time() * 1000) - increment_counter() - mqtt_send("{\"value\": " + str(1) + " }", "pick_count_total") - if get_cable > -1: - - global spot - if action == "pickup": - spot = get_open_spot(sensors) - - if spot is not False: - arm_ready = False - if real: - animation_wait = False - ring_animation = get_cable - start_animation = True - led_set_mode = "GrabAA" - pool.apply_async(ur5_control.holder_to_tray, (arm, arm_updates, get_cable, spot), callback=arm_start_callback, error_callback=handle_error) - else: - arm_ready = True - fprint("Getting cable at position " + str(get_cable)) - mode = "Pickup" - cable_list_state[get_cable] = False # mark as removed - - - if action == "return": - arm_ready = False - fprint("Returning cable from tray position " + str(get_cable)) - if real: - failcount = 0 - pool.apply_async(ur5_control.tray_to_camera, (arm, arm_updates, get_cable), callback=arm_start_callback, error_callback=handle_error) - else: - arm_ready = True - mode = "ReturnC" - else: - # LED idle anim - pass + if mode == "Pickup": # complete @@ -1063,42 +991,52 @@ def mainloop_server(pool, manager): pool.apply_async(camera.read_qr, (10,), callback=camera_start_callback, error_callback=handle_error) elif scan_value.upper().find("BLDN.APP/") > -1: - scan_value = scan_value[scan_value.upper().find("BLDN.APP/")+9:] + scan_value = scan_value[scan_value.upper().find("BLDN.APP/")+9:].strip("\r\n\t ") - fprint("Got cable: " + str(scan_value)) + if scan_value[0:2] == "BL" or scan_value[0:2] == "AW": scan_value = scan_value[2:] - print(cable_list) - print(scan_value) + #print(cable_list) + fprint("Got cable: " + repr(scan_value)) + for idx in range(len(cable_list)): cable = cable_list[idx] - if cable == scan_value and cable_list_state[idx] == False: - cable_list_state[idx] = True # mark cable as returned - arm_ready = False - if real: - animation_wait = True - ring_animation = idx - led_set_mode = "GrabC" - start_animation = True - pool.apply_async(ur5_control.camera_to_holder, (arm, arm_updates, idx), callback=arm_start_callback, error_callback=handle_error) + + if cable is not False and cable.find(scan_value) == 0 and len(scan_value) == len(cable): + if cable_list_state[idx] == False: + cable_list_state[idx] = True # mark cable as returned + arm_ready = False + if real: + animation_wait = True + ring_animation = idx + led_set_mode = "GrabC" + start_animation = True + fprint("Returning to spot " + str(idx)) + pool.apply_async(ur5_control.camera_to_holder, (arm, arm_updates, idx), callback=arm_start_callback, error_callback=handle_error) + else: + arm_ready = True + mode = "Return" + break else: - arm_ready = True - mode = "Return" - break - elif cable == scan_value and cable_list_state[idx] == True: - fprint("WARNING: Holder still marked as occupied!") - arm_ready = False - if real: - animation_wait = True - ring_animation = idx - led_set_mode = "GrabC" - start_animation = True - pool.apply_async(ur5_control.camera_to_holder, (arm, arm_updates, idx), callback=arm_start_callback, error_callback=handle_error) - else: - arm_ready = True - mode = "Return" - break + print(cable_list) + print("Length:", len(cable_list)) + for idx in range(len(cable_list)-1,-1,-1): + cable = cable_list[idx] + if cable is False: + if real: + animation_wait = True + ring_animation = idx + led_set_mode = "GrabC" + start_animation = True + cable_list_state[idx] = True + fprint("Returning to unused spot " + str(idx)) + pool.apply_async(ur5_control.camera_to_holder, (arm, arm_updates, idx), callback=arm_start_callback, error_callback=handle_error) + else: + arm_ready = True + + if mode == "Scan": + fprint("Unable to match cable.") mode = "Idle" else: # camera not ready timecount += 1 @@ -1121,6 +1059,85 @@ def mainloop_server(pool, manager): # led animation pass + if mode == "IdleWait": + if arm_ready is True: + mode = "Idle" + + if mode == "Idle": + # do nothing + if arm_ready is False: + mode = "IdleWait" + + else: + global mainloop_get + + + if not mainloop_get.empty(): + + action, get_cable = mainloop_get.get() + if action == "show": + animation_wait = False + ring_animation = get_cable + start_animation = True + led_set_mode = "Show" + fprint("Showing cable at position " + str(get_cable)) + elif action == "shutdown": + fprint("SHUTTING DOWN!!") + pool.apply_async(ur5_control.move_to_packup, (arm,), callback=arm_start_callback, error_callback=handle_error) + sleep(30) + killme.set(1) + elif action == "returnCheck": + print("Checking sensors..") + if real: + newtube = get_full_spot(sensors) + else: + newtube = -1 + if newtube is not False and newtube >= 0: + # need to return a cable + mainloop_get.put(("return", newtube)) + mainloop_get.put(("returnCheck", 0)) + else: + fprint("Movement requested. Keep clear of the machine!") + + placed = 0 + arm_distance_total = 0 + #mqtt_send("{\"value\": " + str(time.time() * 1000) + " }", "cycle_start") + cycle_start_time = int(time.time() * 1000) + increment_counter() + mqtt_send("{\"value\": " + str(1) + " }", "pick_count_total") + if get_cable > -1: + + + if action == "pickup": + spot = get_open_spot(sensors) + + if spot is not False: + arm_ready = False + if real: + animation_wait = False + ring_animation = get_cable + start_animation = True + led_set_mode = "GrabAA" + pool.apply_async(ur5_control.holder_to_tray, (arm, arm_updates, get_cable, spot), callback=arm_start_callback, error_callback=handle_error) + else: + arm_ready = True + fprint("Getting cable at position " + str(get_cable)) + mode = "Pickup" + cable_list_state[get_cable] = False # mark as removed + + + if action == "return": + arm_ready = False + fprint("Returning cable from tray position " + str(get_cable)) + if real: + failcount = 0 + pool.apply_async(ur5_control.tray_to_camera, (arm, arm_updates, get_cable), callback=arm_start_callback, error_callback=handle_error) + else: + arm_ready = True + mode = "ReturnC" + else: + # LED idle anim + pass def ping(host): @@ -1162,6 +1179,7 @@ def setup_client(pool): sleep(0.25) fprint("VM online.") + sleep(2) # Windows client setup fprint("Running full jukebox control system...") jb = subprocess.Popen("ssh root@192.168.1.25 -t -- /root/jukebox-software/run.sh".split(' '), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True, encoding='utf-8')