Skip to content

Commit

Permalink
MAJOR: BUGFIX: Robot class doesn't delete the persistent robot connec…
Browse files Browse the repository at this point in the history
…tion. It relises on the user to close the persistent connection (by calling robot.close() on atleast one of the robot instances) before existing the program. Skipping this for sim-robot shouldn't be a problem. For hardware robot it will leave the robot running. This cleans up the bug that was preventing clean termination
  • Loading branch information
vikashplus committed Jan 7, 2024
1 parent 7378a88 commit 82c6ab4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
16 changes: 8 additions & 8 deletions robohive/robot/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Robot():
"""
A unified viewpoint of robot between simulation(sim) and hardware(hdr)
"""
# Cached robot that is shared for the application lifetime.
# Cached a persistent connection to the robot that is shared for the application's lifetime.
robot_config = None

def __init__(self,
Expand Down Expand Up @@ -780,9 +780,14 @@ def reset(self,
return feasibe_pos, feasibe_vel


# close connection and exit out of the robot
# Clear the robot class. Note that it doesn't close the persistent connection
def __del__(self):
if self.robot_config is not None and self.is_hardware:
raise RuntimeWarning("RoboHive:> Robot class is being cleared from the workspace. This is expected if we still need to maintain the active connection to the hardware. A persistent connection to robot is still maintained and will be used next time a robot class is created. Ensure that a robot.close() is called to terminate the persistent connection before exiting the program.")

# Close the persistnent connection to the robot. This should be called only once at the end when persistent connection is no longer needed.
def close(self):
if self.robot_config:
if self.robot_config is not None:
status = self.hardware_close() if self.is_hardware else True
if status:
prompt(f"Closed {self.name} (Status: {status})", 'white', 'on_grey', flush=True)
Expand All @@ -793,11 +798,6 @@ def close(self):
prompt(f"Trying to close a non-existent robot", flush=True, type=Prompt.WARN)


# destructor
def __del__(self):
self.close()


def demo_robot():
from robohive.utils import gym

Expand Down
11 changes: 0 additions & 11 deletions robohive/utils/prompt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,6 @@ def prompt(data, color=None, on_color=None, flush=False, end="\n", type:Prompt=P

global PROMPT_CACHE

# Why is cprint None during exit? HACK as a workaround for now
if cprint is None:
# cprint(data, color=color, on_color=on_color, flush=flush, end=end) # throws error
print(data, flush=flush, end=end)

# Why is Prompt None during exit? HACK as a workaround for now
if Prompt is None:
# print(Prompt.ONCE, "=============", type, flush=True) # throws error
print(data, flush=flush, end=end)
return

if type == Prompt.ONCE:
data_hash = hash(data)
if data_hash in PROMPT_CACHE:
Expand Down

0 comments on commit 82c6ab4

Please sign in to comment.