services.py
Go to the documentation of this file.
1 import rospy
2 from clearpath_control_msgs.srv import SetControlMode
3 from clearpath_control_msgs.msg import ControlMode
4 from std_srvs.srv import SetBool
5 from clearpath_localization_msgs.srv import SetDatum
6 
7 
8 # The name of the service to set the operating mode
9 SET_MODE_SERVICE = 'control_selection/set_mode'
10 # The name of the service to 'pause' missions
11 PAUSE_SERVICE = 'control_selection/autonomy_pause'
12 # The name of the service to 'resume' missions
13 RESUME_SERVICE = 'control_selection/autonomy_resume'
14 # The name of the service to set the datum
15 SET_DATUM_SERVICE = 'localization/set_datum'
16 # The name of the service to set obstacle avoidance
17 SET_OBSTACLE_AVOIDANCE_SERVICE = 'navigation/set_obstacle_avoidance'
18 
19 
20 class Services:
21  """Encapsulates the services provided by the Autonomy API."""
22 
23  @staticmethod
24  def setMode(mode):
25  """Sets the control mode (NEUTRAL, MANUAL, or AUTONOMY).
26 
27  Parameters
28  ----------
29  mode : int8 (enum)
30  The current control mode (NEUTRAL, MANUAL, or AUTONOMY)
31 
32  Returns
33  -------
34  bool
35  True if the mode was set successfully, else False
36  """
37 
38  control_mode = ControlMode()
39  control_mode.mode = mode
40  rospy.wait_for_service(SET_MODE_SERVICE, timeout=2.0)
41  try:
42  set_mode_service = rospy.ServiceProxy(SET_MODE_SERVICE, SetControlMode)
43  set_mode_service(control_mode) # does not return a value; assume success
44  return True
45  except rospy.ServiceException as e:
46  rospy.logerr("setMode Service call failed: %s" % e)
47  return False
48 
49  @staticmethod
50  def pauseMission():
51  """Pauses the mission that is currently executing.
52 
53  To resume the mission, see resumeMission().
54  To stop the mission, see stopMission().
55 
56  Returns
57  -------
58  bool
59  True if the mission was paused successfully, else False
60  """
61 
62  rospy.wait_for_service(PAUSE_SERVICE, timeout=2.0)
63  try:
64  pause_service = rospy.ServiceProxy(PAUSE_SERVICE, SetBool)
65  resp = pause_service(True)
66  return resp.success
67  except rospy.ServiceException as e:
68  rospy.logerr("pauseMission Service call failed: %s" % e)
69  return False
70 
71  @staticmethod
73  """Resumes the mission that was previously paused with pauseMission().
74 
75  Returns
76  -------
77  bool
78  True if the mission was resumed successfully, else False
79  """
80 
81  rospy.wait_for_service(RESUME_SERVICE, timeout=2.0)
82  try:
83  resume_service = rospy.ServiceProxy(RESUME_SERVICE, SetBool)
84  resp = resume_service(True)
85  return resp.success
86  except rospy.ServiceException as e:
87  rospy.logerr("resumeMission Service call failed: %s" % e)
88  return False
89 
90  @staticmethod
91  def setDatum(coordinate_lat_lon):
92  """Sets the datum position.
93 
94  Parameters
95  ----------
96  coordinate_lat_lon : CoordinateLatLon
97  The coordinate of the datum
98 
99  Returns
100  -------
101  bool
102  True if the datum was set successfully, else False
103  """
104 
105  rospy.wait_for_service(SET_DATUM_SERVICE, timeout=2.0)
106  try:
107  set_datum_service = rospy.ServiceProxy(SET_DATUM_SERVICE, SetDatum)
108  resp = set_datum_service(
109  float(coordinate_lat_lon.getLat()), float(coordinate_lat_lon.getLon()))
110  return resp.success
111  except rospy.ServiceException as e:
112  rospy.logerr("setDatum Service call failed: %s" % e)
113  return False
114 
115  @staticmethod
116  def setObstacleAviodance(enable_avoidance):
117  """Enables/disables obstacle avoidance.
118 
119  Parameters
120  ----------
121  enable_avoidance : bool
122  If True, enable obstacle avoidance; if False, disable obstacle avoidance
123 
124  Returns
125  -------
126  bool
127  True if obstacle avoidance was updated successfully, else False
128  """
129 
130  rospy.wait_for_service(SET_OBSTACLE_AVOIDANCE_SERVICE, timeout=2.0)
131  try:
132  set_avoidance_service = rospy.ServiceProxy(SET_OBSTACLE_AVOIDANCE_SERVICE, SetBool)
133  resp = set_avoidance_service(enable_avoidance)
134  return resp.success
135  except rospy.ServiceException as e:
136  rospy.logerr("setObstacleAviodance Service call failed: %s" % e)
137  return False
clearpath_onav_api_examples_lib.services.Services.setObstacleAviodance
def setObstacleAviodance(enable_avoidance)
Definition: services.py:116
clearpath_onav_api_examples_lib.services.Services.setDatum
def setDatum(coordinate_lat_lon)
Definition: services.py:91
clearpath_onav_api_examples_lib.services.Services.resumeMission
def resumeMission()
Definition: services.py:72
clearpath_onav_api_examples_lib.services.Services.setMode
def setMode(mode)
Definition: services.py:24
clearpath_onav_api_examples_lib.services.Services.pauseMission
def pauseMission()
Definition: services.py:50
clearpath_onav_api_examples_lib.services.Services
Definition: services.py:20


clearpath_onav_api_examples_lib
Author(s):
autogenerated on Sun Nov 12 2023 03:29:19