From 0ad656728d195f80341b86b0b8ad411b0d52bcde Mon Sep 17 00:00:00 2001
From: Cole Deck <cdeck@hawk.iit.edu>
Date: Mon, 29 Jul 2024 13:50:58 -0500
Subject: [PATCH] Add shutdown routine

---
 run.py         | 13 +++++++++++--
 ur5_control.py |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/run.py b/run.py
index 794e0d4..45d7993 100755
--- a/run.py
+++ b/run.py
@@ -338,6 +338,9 @@ def check_server():
                         elif call == 'request':
                             if "position" in data:
                                 mainloop_get.put(("show", data["position"]))
+                    
+                    case "shutdown":
+                        mainloop_get.put(("shutdown", 0))
                     case _:
                         fprint("Unknown/unimplemented data type: " + decoded["type"])
             except Exception as e:
@@ -571,7 +574,7 @@ def mainloop_server(pool, manager):
             print(ring_animation, animation_wait, ledsys.mode, arm_position)
             
 
-    if start_animation:
+    if start_animation and real:
         # animation start requested
         # may not be immediate
         if ring_animation is not None:
@@ -601,7 +604,7 @@ def mainloop_server(pool, manager):
     
     else:
         # no new animation
-        if ring_animation is not None:
+        if ring_animation is not None and real:
             ledsys.mainloop(None, ring_animation, arm_position=arm_position)
             
         else:
@@ -790,6 +793,12 @@ def mainloop_server(pool, manager):
                     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)
                 else:
                     fprint("Movement requested. Keep clear of the machine!")
 
diff --git a/ur5_control.py b/ur5_control.py
index fb32a1d..9380ef2 100755
--- a/ur5_control.py
+++ b/ur5_control.py
@@ -266,6 +266,7 @@ def move_to_home(robot, keep_flip=False, speed=2):
     return True
 
 def move_to_packup(robot, speed=0.25):
+    robot = connect(robot)
     rob = robot.robot
     
     # known good starting point to reach store position