5 from move_base_msgs.msg
import MoveBaseAction, MoveBaseGoal
6 from actionlib_msgs.msg
import GoalStatus
7 from geometry_msgs.msg
import PoseStamped, PoseArray, PolygonStamped
8 from nav_msgs.msg
import Path
9 from std_msgs.msg
import Bool, Empty
14 rospy.init_node(
'move_base_sequence')
17 self.
poses_pub = rospy.Publisher(
"/move_base_sequence/wayposes",PoseArray,queue_size=10)
19 self.
path_pub = rospy.Publisher(
"/move_base_sequence/path",Path,queue_size=10)
20 _ = rospy.Subscriber(
"/move_base_sequence/wayposes",PoseArray,self.
set_poses)
21 _ = rospy.Subscriber(
"/move_base_sequence/corner_pose",PoseStamped,self.
add_pose)
22 _ = rospy.Service(
'/move_base_sequence/get_state', get_state, self.
get_state)
23 _ = rospy.Service(
'/move_base_sequence/toggle_state', toggle_state, self.
toggle_state)
24 _ = rospy.Service(
'/move_base_sequence/set_state', set_state, self.
set_state)
25 _ = rospy.Service(
'/move_base_sequence/reset', reset, self.
reset)
29 rospy.set_param(
"/move_base_sequence/abortion_behaviour",
"stop")
30 rospy.set_param(
"/move_base_sequence/is_repeating",
True)
38 self.path.header.frame_id =
"map" 39 self.path.header.stamp = rospy.Time.now()
45 self.poses.header.frame_id =
"map" 46 self.poses.header.stamp = rospy.Time.now()
50 rospy.loginfo(
"Waiting for move_base action server...")
51 wait = self.client.wait_for_server(rospy.Duration(5.0))
53 rospy.logerr(
"Action server not available!")
56 rospy.loginfo(
"Connected to move base server, moving the base "+ (
"one way trip" if rospy.get_param(
"/move_base_sequence/is_repeating",
True) ==
False else "in a repetitive way"))
57 rospy.loginfo(
"Waiting for goals..")
61 self.
reset(resetRequest())
62 print(
"move_base_sequence ended successfully!")
69 if self.
_sending: self.client.cancel_goal()
71 self.path.header.frame_id =
"map" 72 self.path.header.stamp = rospy.Time.now()
76 self.
poses = PoseArray()
77 self.poses.header.frame_id =
"map" 78 self.poses.header.stamp = rospy.Time.now()
81 self.poses_pub.publish(self.
poses)
83 self.path_pub.publish(self.
path)
85 rospy.loginfo(
"Sequence reset done!.. waiting for new goals..")
86 return resetResponse(
True)
89 rospy.logerr(
"A problem occured during resetting!")
90 return resetResponse(
False)
94 rospy.logwarn(
"the sent state is the same as the internal state! no change is applied.")
95 return set_stateResponse(
False)
97 rospy.loginfo(
"set_state Success! State now is "+ (
"operating" if self.
__state__ else "paused."))
99 if self.
_sending: self.client.cancel_goal()
100 return set_stateResponse(
True)
106 if self.
_sending: self.client.cancel_goal()
107 rospy.loginfo(
"toggle_state Success! State now is "+ (
"operating" if self.
__state__ else "paused."))
108 return toggle_stateResponse(
True)
110 rospy.logerr(
"Something went wrong while toggling the state!")
111 return toggle_stateResponse(
False)
115 return get_stateResponse(
"operating" if self.
__state__ else "Paused")
118 self.poses.poses.append(msg.pose)
120 self.path.poses.append(msg)
121 self.poses.header.stamp = rospy.Time.now()
123 self.path.header.stamp = rospy.Time.now()
124 if self.
_internal_call: rospy.loginfo(
"Adding a goal from a pose array...")
125 else: rospy.loginfo(
"Adding a new pose to the list.")
129 self.poses_pub.publish(self.
poses)
131 self.path_pub.publish(self.
path)
136 temp_posestamped = PoseStamped()
137 temp_posestamped.header = self.poses.header
138 for pose
in msg.poses:
139 temp_posestamped.pose = pose
141 self.poses_pub.publish(self.
poses)
143 self.path_pub.publish(self.
path)
144 rospy.loginfo(
"Successfully added goal poses!")
151 elif self.
_i <= len(self.poses.poses)-1:
158 rospy.loginfo(
"Goal pose "+str(self.
_i)+
" is now being processed by the Action Server...")
170 rospy.loginfo(
"Goal pose "+str(self._i)+
" received a cancel request after it started executing, successfully cancelled!")
173 rospy.loginfo(
"Goal pose "+str(self._i)+
" received a cancel request before it started executing, successfully cancelled!")
176 rospy.loginfo(
"Goal pose "+str(self._i)+
" REACHED!")
177 self._set_next_goal()
180 behav = rospy.get_param(
"/move_base_sequence/abortion_behaviour",
"stop")
181 if behav ==
"stop": self.__state__ =
False 182 elif not (behav ==
"continue"): rospy.logwarn(
"Param /move_base_sequence/abortion_behaviour is neither 'stop' nor 'continue'! continue is assumed.")
183 rospy.logerr (
"Goal pose "+str(self._i)+
" aborted," +(
"stopping sequence execution," if behav==
'stop' else "continuing with next goals, ")+
"check any errors!")
184 self._set_next_goal()
187 rospy.logerr(
"Goal pose "+str(self._i)+
" has been rejected by the Action Server. moving to next goal.")
188 self._set_next_goal()
190 self._sending =
False 195 if self.
_i <= len(self.poses.poses)-1:
pass 196 elif self.
_i > len(self.poses.poses)-1:
197 if rospy.get_param(
"/move_base_sequence/is_repeating",
True):
199 rospy.loginfo(
"reached the end of the sequence successfully, repeating it again!")
202 rospy.loginfo(
"reached the end of the sequence successfully, waiting another sequence!")
205 self.path.header.frame_id =
"map" 206 self.path.header.stamp = rospy.Time.now()
210 self.
poses = PoseArray()
211 self.poses.header.frame_id =
"map" 212 self.poses.header.stamp = rospy.Time.now()
215 self.poses_pub.publish(self.
poses)
217 self.path_pub.publish(self.
path)
224 goal = MoveBaseGoal()
225 goal.target_pose.header.frame_id =
"map" 226 goal.target_pose.header.stamp = rospy.Time.now()
227 goal.target_pose.pose = self.poses.poses[self.
_i]
228 rospy.loginfo(
"Sending goal pose "+str(self.
_i)+
" to Action Server")
234 if __name__ ==
'__main__':
236 while not rospy.is_shutdown():
237 move_base_sequence.check_newgoals()
def move_base_client(self)
def toggle_state(self, request)
def done_cb(self, status, result)
def feedback_cb(self, feedback)
def get_state(self, request)
def set_state(self, request)