test_ekf_pose_slam.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # ROS imports
00004 import roslib 
00005 roslib.load_manifest('pose_ekf_slam')
00006 import rospy
00007 import tf
00008 from sensor_msgs.msg import Imu
00009 from geometry_msgs.msg import PoseWithCovarianceStamped
00010 from geometry_msgs.msg import TwistWithCovarianceStamped
00011 from nav_msgs.msg import Odometry
00012 from pose_ekf_slam.srv import SetPositionRequest, SetPosition
00013 import PyKDL
00014 import numpy as np
00015 
00016 class Navigator :
00017     
00018     def __init__(self, name):
00019         """ Merge different navigation sensor values  """
00020         self.name = name
00021         
00022         # Create publisher
00023         self.pub_imu = rospy.Publisher("/pose_ekf_slam/imu_input", 
00024                                        Imu)
00025         self.pub_vel = rospy.Publisher("/pose_ekf_slam/velocity_update", 
00026                                        TwistWithCovarianceStamped)
00027         self.pub_pose = rospy.Publisher("/pose_ekf_slam/pose_update", 
00028                                         PoseWithCovarianceStamped)
00029         self.pub_landmark = rospy.Publisher("/pose_ekf_slam/landmark_update/landmark_0", 
00030                                             PoseWithCovarianceStamped)
00031         
00032         # Create Subscriber Inputs (u)
00033         rospy.Subscriber("/dataNavigator", Odometry, self.odomInput)
00034  
00035         
00036         # o = tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0, 'sxyz')
00037 
00038         # Create timers
00039         rospy.Timer(rospy.Duration(0.5), self.pubImu)
00040         # rospy.Timer(rospy.Duration(0.5), self.pubVel)
00041         rospy.Timer(rospy.Duration(2.0), self.pubPose)
00042         # rospy.Timer(rospy.Duration(2.0), self.pubLandmark)
00043         
00044         # Init pose
00045         self.position = 0
00046         self.odom = Odometry()
00047 
00048         
00049     def pubImu(self, event):
00050         imu = Imu()
00051         imu.header.stamp = rospy.Time.now()
00052         imu.header.frame_id = 'imu'
00053         imu.orientation.x = 0.0
00054         imu.orientation.y = 0.0
00055         imu.orientation.z = 0.0
00056         imu.orientation.w = 1.0
00057         imu.linear_acceleration.x = 0.00
00058         self.pub_imu.publish(imu)
00059         
00060         # Publish TF
00061         imu_tf = tf.TransformBroadcaster()
00062         o = tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0, 'sxyz')
00063         imu_tf.sendTransform((0.0, 0.0, 0.0), o, imu.header.stamp, 
00064                              imu.header.frame_id, 'robot')
00065 
00066 
00067     def pubVel(self, event):
00068         v = TwistWithCovarianceStamped()
00069         v.header.stamp = rospy.Time.now()
00070         v.header.frame_id = 'vel'
00071         
00072         # create a new velocity
00073         v.twist.twist.linear.x = 0.9
00074         v.twist.twist.linear.y = 0.0
00075         v.twist.twist.linear.z = 0.0
00076         
00077         # Only the number in the covariance matrix diagonal 
00078         # are used for the updates!
00079         v.twist.covariance[0] = 0.01
00080         v.twist.covariance[7] = 0.01
00081         v.twist.covariance[14] = 0.01
00082         
00083         print 'twist msg:', v
00084         self.pub_vel.publish(v)
00085         
00086         # Publish TF
00087         vel_tf = tf.TransformBroadcaster()
00088         o = tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0, 'sxyz')
00089         vel_tf.sendTransform((0.0, 0.0, 0.0), o, v.header.stamp, 
00090                              v.header.frame_id, 'robot')      
00091         
00092         
00093     def pubPose(self, event):
00094         p = PoseWithCovarianceStamped()
00095         p.header.stamp = rospy.Time.now()
00096         p.header.frame_id = 'pose'
00097         
00098         # Create a new pose
00099         p.pose.pose.position.x = self.position
00100         p.pose.pose.position.y = 0.0
00101         p.pose.pose.position.z = 0.0
00102         self.position = self.position + 2
00103        
00104         # Only the number in the covariance matrix diagonal
00105         # are used for the updates!
00106         # Example of X, Y update without modifying the Z
00107         p.pose.covariance[0] = 1.0
00108         p.pose.covariance[7] = 1.0
00109         p.pose.covariance[14] = 9999.0
00110         
00111         print 'pose msg:', p
00112         self.pub_pose.publish(p)
00113         
00114         # Publish TF
00115         pose_tf = tf.TransformBroadcaster()
00116         o = tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0, 'sxyz')
00117         pose_tf.sendTransform((0.0, 0.0, 0.0), o, p.header.stamp, 
00118                               p.header.frame_id, 'robot')      
00119         
00120         
00121     def odomInput(self, odom):
00122         self.odom = odom
00123 
00124         
00125     def pubLandmark(self, event):
00126             
00127         # Create transformation
00128         angle = tf.transformations.euler_from_quaternion(
00129             [self.odom.pose.pose.orientation.x,
00130              self.odom.pose.pose.orientation.y,
00131              self.odom.pose.pose.orientation.z,
00132              self.odom.pose.pose.orientation.w])
00133 
00134         r = PyKDL.Rotation.RPY(angle[0], angle[1], angle[2])
00135         t = PyKDL.Vector(self.odom.pose.pose.position.x, 
00136                          self.odom.pose.pose.position.y, 
00137                          self.odom.pose.pose.position.z)
00138         
00139         
00140         p_t = PyKDL.Vector(10.0, 5.0, 3.0)
00141         
00142         rel_pose = r * (p_t - t)
00143         
00144         print 'landmark_0 pose wrt robot: ', rel_pose
00145             
00146         p = PoseWithCovarianceStamped()
00147         p.header.stamp = rospy.Time.now()
00148         p.header.frame_id = 'landmark_0'
00149         p.pose.covariance[0] = 0.05
00150         p.pose.covariance[7] = 0.05
00151         p.pose.covariance[14] = 0.05
00152 
00153         # Publish TF
00154         pose_tf = tf.TransformBroadcaster()
00155         o = tf.transformations.quaternion_from_euler(0., 0., 0., 'sxyz')
00156         pose_tf.sendTransform((rel_pose.x() + np.random.normal(0.0, 0.05), 
00157                                rel_pose.y() + np.random.normal(0.0, 0.05), 
00158                                rel_pose.z() + np.random.normal(0.0, 0.05)), 
00159                               o, 
00160                               p.header.stamp, 
00161                               p.header.frame_id, 
00162                               'robot')
00163         self.pub_landmark.publish(p)
00164         
00165               
00166 if __name__ == '__main__':
00167     try:
00168         #   Init node
00169         rospy.init_node('test_ekf_slam')
00170         navigator = Navigator(rospy.get_name())
00171         
00172         # Init pose_ekf_slam server
00173         try:
00174             rospy.wait_for_service('/pose_ekf_slam/set_position', 5)
00175             set_pose = rospy.ServiceProxy('pose_ekf_slam/set_position', 
00176                                           SetPosition)
00177             sp = SetPositionRequest()
00178             sp.position.x = 0.0
00179             sp.position.y = 0.0
00180             sp.position.z = 0.0
00181             set_pose(sp)
00182         except rospy.exceptions.ROSException:
00183             rospy.logerr('Error initializing Pose-EKF-SLAM.')
00184             
00185         rospy.spin()
00186     except rospy.ROSInterruptException: 
00187         pass


pose_ekf_slam
Author(s): Narcis palomeras
autogenerated on Mon Jan 6 2014 11:32:43