Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <vrpn_tracker/VRPNTrackerClient.hpp>
00009
00010 void VRPN_CALLBACK viconTracker_handleTracker(void* userData, const vrpn_TRACKERCB t ) {
00011 VRPNTrackerClient* client = (VRPNTrackerClient *) userData;
00012 client->handleVRPNTrackerCB(t);
00013 }
00014
00015
00016 VRPNTrackerClient::VRPNTrackerClient(const std::string& objectName)
00017 {
00018 opttrackRotationMatrix.resize(9);
00019 opttrackRotationMatrix[0] = 1.0; opttrackRotationMatrix[1] = 0; opttrackRotationMatrix[2] = 0;
00020 opttrackRotationMatrix[3] = 0; opttrackRotationMatrix[4] = 0; opttrackRotationMatrix[5] = -1.0;
00021 opttrackRotationMatrix[6] = 0; opttrackRotationMatrix[7] = 1.0; opttrackRotationMatrix[8] = 0;
00022
00023 options = new VRPNTrackerClientOptions();
00024 std::string vrpnObjectName = objectName + "@" + options->tVRPNHostname;
00025 if(options->tTopicName.compare(VRPN_TOPIC_UNDEF_STRING) == 0){
00026 pub = nodeHandle.advertise<geometry_msgs::PoseStamped>(objectName, 10);
00027 }
00028 else{
00029 pub = nodeHandle.advertise<geometry_msgs::PoseStamped>(options->tTopicName, 10);
00030 }
00031
00032 childFrameID = objectName;
00033 poseStamped.header.frame_id = options->tChildFrameID;
00034
00035
00036 vrpnTracker = new vrpn_Tracker_Remote(vrpnObjectName.c_str());
00037 vrpnTracker->register_change_handler(this, viconTracker_handleTracker);
00038 }
00039
00040 VRPNTrackerClient::~VRPNTrackerClient()
00041 {
00042 vrpnTracker->unregister_change_handler(this, viconTracker_handleTracker);
00043 delete vrpnTracker;
00044 }
00045
00046 void VRPNTrackerClient::spin()
00047 {
00048 vrpnTracker->mainloop();
00049 }
00050
00051 void VRPNTrackerClient::handleVRPNTrackerCB(const vrpn_TRACKERCB t)
00052 {
00053
00054
00055
00056 std::vector<float> pose(3);
00057 pose[0] = t.pos[0]; pose[1] = t.pos[1]; pose[2] = t.pos[2];
00058 std::vector<float> rotPose(3);
00059
00060 if(options->useOptitrackToENURot){
00061 rotPose = multMatrixVector(opttrackRotationMatrix, pose);
00062 }
00063 else{
00064 rotPose = pose;
00065 }
00066
00067 if(options->useVRPNServerTime)
00068 poseStamped.header.stamp = ros::Time(t.msg_time.tv_sec, t.msg_time.tv_usec*1000);
00069 else
00070 poseStamped.header.stamp = ros::Time::now();
00071
00072 if(options->useNEDconvention){
00073 poseStamped.pose.position.x = rotPose[0];
00074 poseStamped.pose.position.y = -rotPose[1];
00075 poseStamped.pose.position.z = -rotPose[2];
00076 }
00077 else{
00078 poseStamped.pose.position.x = rotPose[0];
00079 poseStamped.pose.position.y = rotPose[1];
00080 poseStamped.pose.position.z = rotPose[2];
00081 }
00082
00083
00084 std::vector<float> ori(3);
00085 ori[0] = t.quat[0]; ori[1] = t.quat[1]; ori[2] = t.quat[2];
00086 std::vector<float> rotOri(3);
00087 if(options->useOptitrackToENURot){
00088 rotOri = multMatrixVector(opttrackRotationMatrix, ori);
00089 }
00090 else{
00091 rotOri = ori;
00092 }
00093
00094 if(options->useNEDconvention){
00095 poseStamped.pose.orientation.x = rotOri[0];
00096 poseStamped.pose.orientation.y = -rotOri[1];
00097 poseStamped.pose.orientation.z = -rotOri[2];
00098 poseStamped.pose.orientation.w = t.quat[3];
00099 }
00100 else{
00101 poseStamped.pose.orientation.x = rotOri[0];
00102 poseStamped.pose.orientation.y = rotOri[1];
00103 poseStamped.pose.orientation.z = rotOri[2];
00104 poseStamped.pose.orientation.w = t.quat[3];
00105 }
00106 pub.publish(poseStamped);
00107
00108
00109 if (options->tEnableTF) {
00110 tf::Transform transform;
00111 transform.setOrigin(tf::Vector3(poseStamped.pose.position.x, poseStamped.pose.position.y, poseStamped.pose.position.z));
00112 transform.setRotation(tf::Quaternion(poseStamped.pose.orientation.x, poseStamped.pose.orientation.y, poseStamped.pose.orientation.z, poseStamped.pose.orientation.w));
00113
00114 tf::StampedTransform tfStampedTransform(transform, poseStamped.header.stamp, poseStamped.header.frame_id, childFrameID);
00115 transBroadcaster.sendTransform(tfStampedTransform);
00116 }
00117 }
00118
00119 std::vector<float> VRPNTrackerClient::multMatrixVector(const std::vector<float> &mat, const std::vector<float> &vec){
00120 std::vector<float> ret;
00121 if(vec.size() != 3 || mat.size() != 9)
00122 return ret;
00123
00124 ret.resize(3);
00125 ret[0] = mat[0]*vec[0]+mat[1]*vec[1]+mat[2]*vec[2];
00126 ret[1] = mat[3]*vec[0]+mat[4]*vec[1]+mat[5]*vec[2];
00127 ret[2] = mat[6]*vec[0]+mat[7]*vec[1]+mat[8]*vec[2];
00128
00129 return ret;
00130
00131 }