Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 from threading import Lock
00035 
00036 import rospy
00037 import actionlib
00038 
00039 from tf.listener import TransformListener
00040 from tf.transformations import quaternion_matrix
00041 
00042 from actionlib_msgs.msg import GoalStatus, GoalStatusArray
00043 from control_msgs.msg import PointHeadAction, PointHeadGoal
00044 from geometry_msgs.msg import PointStamped
00045 from nav_msgs.msg import Path
00046 
00047 class NavHeadController:
00048 
00049     def __init__(self):
00050         self.has_goal = False
00051 
00052         
00053         self.x = 1.0
00054         self.y = 0.0
00055         self.mutex = Lock()
00056 
00057         self.listener = TransformListener()
00058 
00059         self.client = actionlib.SimpleActionClient("head_controller/point_head", PointHeadAction)
00060         self.client.wait_for_server()
00061 
00062         self.plan_sub = rospy.Subscriber("move_base/TrajectoryPlannerROS/local_plan", Path, self.planCallback)
00063         self.stat_sub = rospy.Subscriber("move_base/status", GoalStatusArray, self.statCallback)
00064 
00065     def statCallback(self, msg):
00066         goal = False
00067         for status in msg.status_list:
00068             if status.status == GoalStatus.ACTIVE:
00069                 goal = True
00070                 break
00071         self.has_goal = goal
00072 
00073     def planCallback(self, msg):
00074         
00075         pose_stamped = msg.poses[-1]
00076         pose = pose_stamped.pose
00077 
00078         
00079         R = quaternion_matrix([pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w])
00080         point = [1, 0, 0, 1]
00081         M = R*point
00082 
00083         p = PointStamped()
00084         p.header.frame_id = pose_stamped.header.frame_id
00085         p.header.stamp = rospy.Time(0)
00086         p.point.x += pose_stamped.pose.position.x + M[0,0]
00087         p.point.y += pose_stamped.pose.position.y + M[1,0]
00088         p.point.z += pose_stamped.pose.position.z + M[2,0]
00089 
00090         
00091         p = self.listener.transformPoint("base_link", p)
00092 
00093         
00094         with self.mutex:
00095             if p.point.x < 0.65:
00096                 self.x = 0.65
00097             else:
00098                 self.x = p.point.x
00099             if p.point.y > 0.5:
00100                 self.y = 0.5
00101             elif p.point.y < -0.5:
00102                 self.y = -0.5
00103             else:
00104                 self.y = p.point.y
00105 
00106     def loop(self):
00107         while not rospy.is_shutdown():
00108             if self.has_goal:
00109                 goal = PointHeadGoal()
00110                 goal.target.header.stamp = rospy.Time.now()
00111                 goal.target.header.frame_id = "base_link"
00112                 with self.mutex:
00113                     goal.target.point.x = self.x
00114                     goal.target.point.y = self.y
00115                     self.x = 1
00116                     self.y = 0
00117                 goal.target.point.z = 0.0
00118                 goal.min_duration = rospy.Duration(1.0)
00119 
00120                 self.client.send_goal(goal)
00121                 self.client.wait_for_result()
00122 
00123                 with self.mutex:
00124                     goal.target.point.x = self.x
00125                     goal.target.point.y = self.y
00126                     self.x = 1
00127                     self.y = 0
00128                 goal.target.point.z = 0.75
00129 
00130                 self.client.send_goal(goal)
00131                 self.client.wait_for_result()
00132             else:
00133                 rospy.sleep(1.0)
00134 
00135 if __name__=="__main__":
00136     rospy.init_node("tilt_head_node")
00137     h = NavHeadController()
00138     h.loop()