Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SW-1890] modify lease acquiring/taking #154

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions spot_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from bosdyn.client.graph_nav import GraphNavClient
from bosdyn.client.gripper_camera_param import GripperCameraParamClient
from bosdyn.client.image import ImageClient
from bosdyn.client.lease import Lease, LeaseClient, LeaseKeepAlive
from bosdyn.client.lease import Lease, LeaseClient, LeaseKeepAlive, LeaseResponseError
from bosdyn.client.license import LicenseClient
from bosdyn.client.manipulation_api_client import ManipulationApiClient
from bosdyn.client.map_processing import MapProcessingServiceClient
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def releaseEStop(self) -> None:
self._estop_endpoint = None

def takeLease(self) -> typing.Tuple[bool, typing.Optional[Lease]]:
"""Take lease for the robot, which may have been taken already."""
"""Forcefully take the lease for the robot, which may have been taken already."""
lease = self._lease_client.take()
have_new_lease = (self._lease is None and lease is not None) or (
str(lease.lease_proto) != str(self._lease.lease_proto)
Expand All @@ -1042,19 +1042,23 @@ def takeLease(self) -> typing.Tuple[bool, typing.Optional[Lease]]:
return have_new_lease, self._lease

def getLease(self) -> typing.Optional[Lease]:
"""Get a lease for the robot and keep the lease alive automatically."""
"""Get a lease for the robot (if available) and keep the lease alive automatically."""
if self._use_take_lease:
lease = self._lease_client.take()
else:
lease = self._lease_client.acquire()
have_new_lease = (self._lease is None and lease is not None) or (
str(lease.lease_proto) != str(self._lease.lease_proto)
)
if have_new_lease:
if self._lease_keepalive is not None:
self._lease_keepalive.shutdown()
self._lease_keepalive = LeaseKeepAlive(self._lease_client)
self._lease = lease
try:
lease = self._lease_client.acquire()
have_new_lease = (self._lease is None and lease is not None) or (
str(lease.lease_proto) != str(self._lease.lease_proto)
)
if have_new_lease:
if self._lease_keepalive is not None:
self._lease_keepalive.shutdown()
self._lease_keepalive = LeaseKeepAlive(self._lease_client)
self._lease = lease
except LeaseResponseError as e:
self._logger.error("Unable to acquire lease: " + str(e))

return self._lease

def releaseLease(self) -> None:
Expand All @@ -1079,7 +1083,7 @@ def disconnect(self) -> None:
self._valid = False
if self._robot.time_sync:
self._robot.time_sync.stop()
self.releaseLease()
self.release()
self.releaseEStop()

def _robot_command(
Expand All @@ -1106,6 +1110,8 @@ def _robot_command(
timesync_endpoint=timesync_endpoint,
)
return True, "Success", command_id
except LeaseResponseError as e:
return False, "Failed to acquire lease: " + str(e), None
except Exception as e:
self._logger.error(f"Unable to execute robot command: {e}")
return False, str(e), None
Expand Down