head_pose_kalman.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib; roslib.load_manifest('head_pose_estimation')
00003 import rospy
00004 from geometry_msgs.msg import PoseStamped, Pose, Point, Quaternion, Vector3
00005 from tf.transformations import euler_from_quaternion, quaternion_from_euler
00006 from collections import deque
00007 from state_filters import KalmanFilter
00008 from head_pose_estimation.cfg import FilterConfig
00009 from dynamic_reconfigure.server import Server
00010 import numpy as np
00011 import cv
00012 from threading import RLock
00013 pose_pub = None
00014 
00015 cov_m = [
00016         [0.09, 0.00],
00017         [0.00, 0.09]
00018 ]
00019 
00020 cov_p = [
00021         [0.28409617, 0.00000000],
00022         [0.00000000, 6.24341614]
00023 ]
00024 
00025 class Filter(object):
00026         control_lock = RLock()
00027         kf = KalmanFilter(np.float32(cov_m), 2, 2, 2)
00028         control = np.float32([0,0])
00029         def __init__(self):
00030                 self.kf.set_control_matrix(cv.fromarray(np.matrix([[1.0,0.0],[0.0,1.0]])))
00031         def pose_sub(self, pose_msg):
00032                 r,p,y = euler_from_quaternion([
00033                                 pose_msg.pose.orientation.x,
00034                                 pose_msg.pose.orientation.y,
00035                                 pose_msg.pose.orientation.z,
00036                                 pose_msg.pose.orientation.w
00037                 ])                                      
00038                                         
00039                 self.kf.update([p,y])
00040                 with self.control_lock:
00041                         filtered = self.kf.predict(self.control)
00042                         self.control = np.float32([0,0])
00043                 
00044                 filtered_pose = PoseStamped(
00045                         header  = pose_msg.header,
00046                         pose    = Pose(
00047                                                 pose_msg.pose.position,
00048                                                 Quaternion(*quaternion_from_euler(r, filtered[0], filtered[1]))
00049                                           )
00050                 )
00051                 pose_pub.publish(filtered_pose)
00052                 
00053 
00054         def flow_cb(self, msg):
00055                 print msg
00056                 with self.control_lock:
00057                         #self.control += [msg.x, msg.y]
00058                         self.control += [msg.y, msg.x]
00059                         #self.control = [0,0]
00060                         
00061         def cfg_cb(self, config, level):
00062                 pass
00063 
00064 if __name__ == '__main__':
00065         rospy.init_node('head_pose_filter')
00066         f = Filter()
00067         rospy.Subscriber('head_pose', PoseStamped, f.pose_sub)
00068         rospy.Subscriber('flow', Vector3, f.flow_cb)
00069         Server(FilterConfig, f.cfg_cb)
00070         
00071         pose_pub = rospy.Publisher('head_pose_filtered', PoseStamped)
00072         rospy.spin()


head_pose_estimation
Author(s): Daniel Lazewatsky
autogenerated on Mon Oct 6 2014 00:23:14