7 from actionlib
import SimpleActionClient
10 from geometry_msgs.msg
import Pose, Twist, PoseStamped
11 from move_base_msgs.msg
import MoveBaseGoal, MoveBaseFeedback, MoveBaseAction
13 from caster_base.srv
import SetDigitalOutput
14 from caster_app.msg
import DockAction, DockFeedback, DockResult
26 self.
__base_frame = rospy.get_param(
"~base_frame",
"base_footprint")
29 self.__dock_ready_pose.position.x = rospy.get_param(
"~dock/pose_x")
30 self.__dock_ready_pose.position.y = rospy.get_param(
"~dock/pose_y")
31 self.__dock_ready_pose.position.z = rospy.get_param(
"~dock/pose_z")
32 self.__dock_ready_pose.orientation.x = rospy.get_param(
"~dock/orientation_x")
33 self.__dock_ready_pose.orientation.y = rospy.get_param(
"~dock/orientation_y")
34 self.__dock_ready_pose.orientation.z = rospy.get_param(
"~dock/orientation_z")
35 self.__dock_ready_pose.orientation.w = rospy.get_param(
"~dock/orientation_w")
41 rospy.loginfo(
"dock_pose:")
45 rospy.loginfo(
"wait for movebase server...")
46 self.__movebase_client.wait_for_server()
47 rospy.loginfo(
"movebase server connected")
49 self.
__cmd_pub = rospy.Publisher(
'cmd_vel', Twist, queue_size=10)
58 rospy.loginfo(
"Creating ActionServer [%s]\n", name)
63 ps.header.frame_id =
"dock" 69 self.__dock_ready_pose_2.pose.position.z = -1.0
70 rospy.logwarn(
"tf 1error, %s" % e)
72 self.__dock_ready_pose_2.pose.position.z = 0.0
80 if self.__saved_goal.dock ==
True:
82 rospy.logwarn(
"rejected, robot has already docked")
83 gh.set_rejected(
None,
"already docked")
85 rospy.loginfo(
"Docking")
86 gh.set_accepted(
"Docking")
88 elif self.__saved_goal.dock ==
False:
90 rospy.logwarn(
"cancel_all_goals")
91 self.__movebase_client.cancel_all_goals()
95 self.__cmd_pub.publish(cmd)
97 rospy.logwarn(
"rejected, robot is not on charging")
98 gh.set_rejected(
None,
"robot is not on charging")
100 rospy.loginfo(
"Start undock")
101 gh.set_accepted(
"Start undock")
104 rospy.logwarn(
"unknown dock data type, should be true or false")
107 rospy.loginfo(
"set relay %d" % state)
108 rospy.loginfo(
"check caster base service...")
109 rospy.wait_for_service(
'caster_base_node/set_digital_output')
110 rospy.loginfo(
"service exist")
113 set_digital_output = rospy.ServiceProxy(
'caster_base_node/set_digital_output', SetDigitalOutput)
114 resp = set_digital_output(4, state)
115 except rospy.ServiceException, e:
116 print "Service call failed: %s" % e
119 self.__movebase_client.cancel_goal()
120 rospy.logwarn(
"cancel callback")
126 ca_feedback = DockFeedback()
129 last_pose, last_quaternion = self.__tf_listener.lookupTransform(self.
__odom_frame, self.
__base_frame, rospy.Time(0))
132 rospy.logwarn(
"tf error")
135 while delta_distance < self.
__dock_distance-0.30
and not rospy.is_shutdown():
136 self.__cmd_pub.publish(cmd)
139 current_pose, current_quaternion = self.__tf_listener.lookupTransform(self.
__odom_frame, self.
__base_frame, rospy.Time(0))
140 delta_distance = math.sqrt(math.pow(current_pose[0]-last_pose[0],2)+math.pow(current_pose[1]-last_pose[1],2)+math.pow(current_pose[2]-last_pose[2],2))
142 ca_feedback.dock_feedback =
"Moving to Dock, %fm left" % (self.
__dock_distance-delta_distance)
143 self.__saved_gh.publish_feedback(ca_feedback)
145 rospy.loginfo(
"Moving to Dock, %fm left" % (self.
__dock_distance-delta_distance))
148 rospy.logwarn(
"tf error aa")
150 rospy.Rate(20).sleep()
152 ca_feedback.dock_feedback =
"Stop on Dock" 153 self.__saved_gh.publish_feedback(ca_feedback)
154 rospy.loginfo(
"stop robot")
158 self.__cmd_pub.publish(cmd)
164 self.__saved_gh.set_succeeded(
None,
"Docked")
165 rospy.loginfo(
"Docked")
169 mb_goal = MoveBaseGoal()
170 mb_goal.target_pose.header.stamp = rospy.Time.now()
171 mb_goal.target_pose.header.frame_id = self.
__map_frame 174 self.__movebase_client.send_goal(mb_goal)
176 self.__movebase_client.wait_for_result()
177 rospy.loginfo(
"arrived __dock_ready_pose")
179 rospy.Rate(1).sleep()
181 mb_goal.target_pose.header.stamp = rospy.Time.now()
182 mb_goal.target_pose.header.frame_id = self.
__map_frame 184 if self.__dock_ready_pose_2.pose.position.z == -0.3:
185 rospy.logwarn(
"dock_ready_pose_2 failed")
188 t = self.__dock_ready_pose_2.pose
191 mb_goal.target_pose.pose = t
193 rospy.logwarn(
"move to dock_ready_pose_2")
194 self.__movebase_client.send_goal(mb_goal)
196 self.__movebase_client.wait_for_result()
197 rospy.loginfo(
"arrived dock_ready_pose_2")
205 ca_feedback = DockFeedback()
208 last_pose, last_quaternion = self.__tf_listener.lookupTransform(self.
__odom_frame, self.
__base_frame, rospy.Time(0))
211 rospy.logwarn(
"tf error")
214 while delta_distance < self.
__dock_distance and not rospy.is_shutdown():
215 self.__cmd_pub.publish(cmd)
218 current_pose, current_quaternion = self.__tf_listener.lookupTransform(self.
__odom_frame, self.
__base_frame, rospy.Time(0))
219 delta_distance = math.sqrt(math.pow(current_pose[0]-last_pose[0],2)+math.pow(current_pose[1]-last_pose[1],2)+math.pow(current_pose[2]-last_pose[2],2))
221 ca_feedback.dock_feedback =
"Undock, %fm left" % (self.
__dock_distance-delta_distance)
222 self.__saved_gh.publish_feedback(ca_feedback)
224 rospy.loginfo(
"Undock, %fm left" % (self.
__dock_distance-delta_distance))
227 rospy.logwarn(
"tf error aa")
229 rospy.Rate(20).sleep()
231 ca_feedback.dock_feedback =
"Stop on DockReady" 232 self.__saved_gh.publish_feedback(ca_feedback)
233 rospy.loginfo(
"stop robot")
237 self.__cmd_pub.publish(cmd)
243 self.__saved_gh.set_succeeded(
None,
"Undocked")
244 rospy.loginfo(
"UnDocked")
247 rospy.init_node(
"dock_server")
253 if __name__ ==
'__main__':
def __goal_callback(self, gh)
def __moveto_dock_ready_allin(self)
def __set_charge_relay(self, state)
def __dock_pose_callback(self, data)
def __cancel_callback(self, gh)