Skip to content

Commit 8e04c57

Browse files
linting
1 parent dd67ab7 commit 8e04c57

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

spot_wrapper/spot_mission_wrapper.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import logging
2+
3+
from bosdyn.api.mission import nodes_pb2
4+
from bosdyn.client import RpcError, robot_command
5+
from bosdyn.client.lease import LeaseClient, LeaseWallet
6+
from bosdyn.client.robot import Robot
7+
from bosdyn.mission.client import (
8+
CompilationError,
9+
MissionClient,
10+
NoMissionError,
11+
NoMissionPlayingError,
12+
ValidationError,
13+
)
14+
15+
from spot_wrapper.wrapper_helpers import RobotState
16+
17+
18+
class SpotMission:
19+
"""
20+
Allow access to mission functionality through the SDK
21+
"""
22+
23+
def __init__(
24+
self,
25+
robot: Robot,
26+
logger: logging.Logger,
27+
robot_state: RobotState,
28+
mission_client: MissionClient,
29+
robot_command_client: robot_command.RobotCommandClient,
30+
lease_client: LeaseClient,
31+
) -> None:
32+
self._robot = robot
33+
self._logger = logger
34+
self._mission_client: MissionClient = mission_client
35+
self._robot_command_client = robot_command_client
36+
self._lease_client = lease_client
37+
self._robot_state = robot_state
38+
self._spot_check_resp = None
39+
self._lease = None
40+
self._lease_wallet: LeaseWallet = self._lease_client.lease_wallet
41+
42+
def load_mission(self, root: nodes_pb2.Node, leases=None, data_chunk_byte_size: Optional[int] = None):
43+
"""Load a mission
44+
Args:
45+
root: Root node in a mission.
46+
leases: All leases necessary to initialize a mission.
47+
data_chunk_byte_size: Optional max size of each streamed message
48+
Raises:
49+
RpcError: Problem communicating with the robot.
50+
bosdyn.mission.client.CompilationError: The mission failed to compile.
51+
bosdyn.mission.client.ValidationError: The mission failed to validate.
52+
"""
53+
if leases is None:
54+
leases = []
55+
if data_chunk_byte_size:
56+
return self._load_mission_as_chunks(root, leases, data_chunk_byte_size)
57+
try:
58+
return self._mission_client.load_mission_async(root, leases)
59+
except RpcError:
60+
return False, "Could not communicate with the robot"
61+
except CompilationError as e:
62+
return False, f"The mission failed to compile: {e}"
63+
except ValidationError as e:
64+
return False, f"The mission could not be validated: {e}"
65+
66+
def _load_mission_as_chunks(self, root: nodes_pb2.Node, leases=None, data_chunk_byte_size: int = 1000 * 1000):
67+
"""Load a mission onto the robot.
68+
Args:
69+
root: Root node in a mission.
70+
leases: All leases necessary to initialize a mission.
71+
data_chunk_byte_size: max size of each streamed message
72+
Raises:
73+
RpcError: Problem communicating with the robot.
74+
bosdyn.mission.client.CompilationError: The mission failed to compile.
75+
bosdyn.mission.client.ValidationError: The mission failed to validate.
76+
"""
77+
if leases is None:
78+
leases = []
79+
try:
80+
return self._mission_client.load_mission_as_chunks2(root, leases, data_chunk_byte_size)
81+
except RpcError:
82+
return False, "Could not communicate with the robot"
83+
except CompilationError as e:
84+
return False, f"The mission failed to compile: {e}"
85+
except ValidationError as e:
86+
return False, f"The mission could not be validated: {e}"
87+
88+
def get_mission_info(self):
89+
"""Get static information about the loaded mission.
90+
91+
Raises:
92+
RpcError: Problem communicating with the robot.
93+
"""
94+
try:
95+
return self._mission_client.get_info()
96+
except RpcError:
97+
return False, "Could not communicate with the robot"
98+
99+
def play_mission(
100+
self,
101+
pause_time_secs: int,
102+
leases=None,
103+
settings=None,
104+
):
105+
"""Play loaded mission or continue a paused mission
106+
Args:
107+
pause_time_secs: Absolute time when the mission should pause execution. Subsequent RPCs
108+
will override this value, so you can use this to say "if you don't hear from me again,
109+
stop running the mission at this time."
110+
leases: Leases the mission service will need to use. Unlike other clients, these MUST
111+
be specified.
112+
Raises:
113+
RpcError: Problem communicating with the robot.
114+
NoMissionError: No mission Loaded.
115+
"""
116+
if leases is None:
117+
leases = []
118+
try:
119+
return self._mission_client.play_mission_async(pause_time_secs, leases, settings)
120+
except RpcError:
121+
return False, "Could not communicate with the robot"
122+
except NoMissionError:
123+
return False, "No mission loaded"
124+
125+
def get_mission_state(
126+
self,
127+
upper_tick_bound: Optional[int] = None,
128+
lower_tick_bound: Optional[int] = None,
129+
past_ticks: Optional[int] = None,
130+
):
131+
"""Get the state of the current playing mission
132+
Raises:
133+
RpcError: Problem communicating with the robot.
134+
NoMissionPlayingError: No mission playing.
135+
"""
136+
try:
137+
return self._mission_client.get_state_async(upper_tick_bound, lower_tick_bound, past_ticks)
138+
except RpcError:
139+
return False, "Could not communicate with the robot"
140+
except NoMissionPlayingError:
141+
return False, "No mission playing"
142+
143+
def pause_mission(self):
144+
"""Pause the current mission
145+
Raises:
146+
RpcError: Problem communicating with the robot.
147+
NoMissionPlayingError: No mission playing.
148+
"""
149+
try:
150+
return self._mission_client.pause_mission_async()
151+
except RpcError:
152+
return False, "Could not communicate with the robot"
153+
except NoMissionPlayingError:
154+
return False, "No mission playing"
155+
156+
def restart_mission(self, pause_time_secs, leases=None, settings=None):
157+
"""Restart mission from the beginning
158+
Args:
159+
pause_time_secs: Absolute time when the mission should pause execution. Subsequent RPCs
160+
to RestartMission will override this value, so you can use this to say "if you don't hear
161+
from me again, stop running the mission at this time."
162+
leases: Leases the mission service will need to use. Unlike other clients, these MUST
163+
be specified.
164+
Raises:
165+
RpcError: Problem communicating with the robot.
166+
NoMissionError: No Mission Loaded.
167+
bosdyn.mission.client.ValidationError: The mission failed to validate.
168+
"""
169+
if leases is None:
170+
leases = []
171+
try:
172+
return self._mission_client.restart_mission_async(pause_time_secs, leases, settings)
173+
except RpcError:
174+
return False, "Could not communicate with the robot"
175+
except NoMissionError:
176+
return False, "No mission loaded"
177+
except ValidationError as e:
178+
return False, f"The mission could not be validated: {e}"
179+
180+
def stop_mission(self):
181+
"""Stop the current mission
182+
Raises:
183+
RpcError: Problem communicating with the robot.
184+
NoMissionPlayingError: No mission playing.
185+
"""
186+
try:
187+
return self._mission_client.stop_mission_async()
188+
except RpcError:
189+
return False, "Could not communicate with the robot"
190+
except NoMissionPlayingError:
191+
return False, "No mission playing"

0 commit comments

Comments
 (0)