kinton_std_msgs_node.cpp
Go to the documentation of this file.
00001 #include "kinton_std_msgs_node.h"
00002 
00003 KintonOdomAlgNode::KintonOdomAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<KintonOdomAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   this->loop_rate_ = 100;//in [Hz]
00008 
00009   //Get fixed transforms between the IMU data and the robot frame
00010   tf::TransformListener listener1,listener2,listener3;
00011   tf::StampedTransform tf_accelerometers,tf_gyros,tf_compass;
00012 
00013   //wait 1 secs waiting for the other nodes to start
00014   sleep(1.0);
00015 
00016   try {
00017     listener1.waitForTransform("/base_link","/accelerometers", ros::Time::now(), ros::Duration(1.0));
00018     listener1.lookupTransform("/base_link","/accelerometers", ros::Time::now(), tf_accelerometers);
00019   } 
00020   catch (tf::TransformException ex1) {
00021     ROS_ERROR("Accelerometers Transform error: %s",ex1.what());
00022   }
00023 
00024   try {
00025     listener2.waitForTransform("/base_link","/gyros", ros::Time::now(), ros::Duration(1.0));
00026     listener2.lookupTransform("/base_link","/gyros", ros::Time::now(), tf_gyros);
00027   }
00028   catch (tf::TransformException ex2) {
00029     ROS_ERROR("Gyros Transform error: %s",ex2.what());
00030   }
00031 
00032   try {
00033     listener3.waitForTransform("/base_link","/compass", ros::Time::now(), ros::Duration(1.0));
00034     listener3.lookupTransform("/base_link","/compass", ros::Time::now(), tf_compass);
00035   }
00036   catch (tf::TransformException ex3) {
00037     ROS_ERROR("Compass Transform error: %s",ex3.what());
00038   }
00039 
00040 
00041   //Transform to eigen 4x4 matrix
00042   this->T_acc_ = getTransform(tf_accelerometers);
00043   this->T_gyr_ = getTransform(tf_gyros);
00044   this->T_comp_ = getTransform(tf_compass);
00045 
00046   // [init publishers]
00047   this->imu_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Imu>("raw_imu", 100);
00048   this->height_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PointStamped>("raw_height", 100);
00049   
00050   // [init subscribers]
00051   this->imu_subscriber_ = this->public_node_handle_.subscribe("kinton_imu", 100, &KintonOdomAlgNode::imu_callback, this);
00052   this->height_subscriber_ = this->public_node_handle_.subscribe("kinton_height", 100, &KintonOdomAlgNode::height_callback, this);
00053 
00054    
00055   // [init services]
00056   
00057   // [init clients]
00058   
00059   // [init action servers]
00060   
00061   // [init action clients]
00062 }
00063 
00064 KintonOdomAlgNode::~KintonOdomAlgNode(void)
00065 {
00066   // [free dynamic memory]
00067 }
00068 
00069 void KintonOdomAlgNode::mainNodeThread(void)
00070 {
00071 
00072   // [fill srv structure and make request to the server]
00073   
00074   // [fill action structure and make request to the action server]
00075 
00076   // [publish messages]
00077 
00078 }
00079 
00080 /*  [subscriber callbacks] */
00081 
00082 void KintonOdomAlgNode::imu_callback(const sensor_msgs::Imu::ConstPtr& msg) 
00083 { 
00084 
00085   //Considering the IMU frame with X(Fwd)-Y(Left)-Z(up), but the gravity positive (it should be removed)
00086 
00087 
00088   //ROS_INFO("KintonOdomAlgNode::imu_callback: New Message Received"); 
00089 
00090   //use appropiate mutex to shared variables if necessary 
00091   //this->alg_.lock(); 
00092   this->imu_mutex_.enter(); 
00093 
00094   Eigen::MatrixXd pos_ang_imu(4,1), acc_lin_imu(4,1), vel_ang_imu(4,1);
00095 
00096   //Get vectors /////////////////////////////////////////////////////////////
00097   //linear accelerations in base_link (roll change of pi/2)
00098   acc_lin_imu(0,0) = msg->linear_acceleration.x;
00099   acc_lin_imu(1,0) = msg->linear_acceleration.y; 
00100   acc_lin_imu(2,0) = -msg->linear_acceleration.z; 
00101   acc_lin_imu(3,0) = 1.0;
00102 
00103   //angular velocities in base_link
00104   vel_ang_imu(0,0) = msg->angular_velocity.x;
00105   vel_ang_imu(1,0) = msg->angular_velocity.y;
00106   vel_ang_imu(2,0) = msg->angular_velocity.z;
00107   vel_ang_imu(3,0) = 1.0;
00108 
00109   //angles base_link r.t. world
00110   tf::Quaternion q;
00111   tf::quaternionMsgToTF(msg->orientation,q);
00112   tf::Matrix3x3(q).getRPY(pos_ang_imu(0,0), pos_ang_imu(1,0), pos_ang_imu(2,0));
00113   pos_ang_imu(3,0) = 1.0;
00114 
00115   //Transform data to base_link frame /////////////////////////////////////////
00116   //To simplify the translation between frames won't be considered
00117   acc_lin_imu = this->T_acc_ * acc_lin_imu;
00118   vel_ang_imu = this->T_gyr_ * vel_ang_imu;
00119   pos_ang_imu = this->T_comp_ * pos_ang_imu;
00120 
00121   geometry_msgs::Quaternion rot;
00122   rot = tf::createQuaternionMsgFromRollPitchYaw(pos_ang_imu(0,0),pos_ang_imu(1,0),pos_ang_imu(2,0));
00123 
00124 
00125   //Publish the odometry message over ROS
00126   this->Imu_msg_.header = msg->header;
00127   this->Imu_msg_.orientation = rot;
00128   this->Imu_msg_.orientation_covariance = msg->orientation_covariance;
00129   this->Imu_msg_.angular_velocity.x = vel_ang_imu(0,0);
00130   this->Imu_msg_.angular_velocity.y = vel_ang_imu(1,0);
00131   this->Imu_msg_.angular_velocity.z = vel_ang_imu(2,0);
00132   this->Imu_msg_.angular_velocity_covariance = msg->angular_velocity_covariance;
00133   this->Imu_msg_.linear_acceleration.x = acc_lin_imu(0,0);
00134   this->Imu_msg_.linear_acceleration.y = acc_lin_imu(1,0);
00135   this->Imu_msg_.linear_acceleration.z = acc_lin_imu(2,0);
00136   this->Imu_msg_.linear_acceleration_covariance = msg->linear_acceleration_covariance;
00137 
00138   this->imu_publisher_.publish(this->Imu_msg_);
00139 
00140   //unlock previously blocked shared variables 
00141   //this->alg_.unlock(); 
00142   this->imu_mutex_.exit(); 
00143 }
00144 
00145 void KintonOdomAlgNode::height_callback(const mav_msgs::Height::ConstPtr& msg) 
00146 {
00147   //ROS_INFO("KintonOdomAlgNode::height_callback: New Message Received"); 
00148 
00149   //use appropiate mutex to shared variables if necessary 
00150   //this->alg_.lock(); 
00151   this->height_mutex_.enter(); 
00152 
00153   this->Height_msg_.header = msg->header;
00154   this->Height_msg_.point.x =
00155   this->Height_msg_.point.y = 0.0;
00156   this->Height_msg_.point.z = msg->height;
00157 
00158   this->height_publisher_.publish(this->Height_msg_);
00159 
00160   //unlock previously blocked shared variables 
00161   //this->alg_.unlock(); 
00162   this->height_mutex_.exit(); 
00163 
00164 }
00165 /*  [service callbacks] */
00166 
00167 /*  [action callbacks] */
00168 
00169 /*  [action requests] */
00170 
00171 Eigen::Matrix4d KintonOdomAlgNode::getTransform(const tf::StampedTransform& transform)
00172 {
00173 
00174   double roll,pitch,yaw;
00175   tf::Quaternion q =  transform.getRotation();
00176   tf::Matrix3x3(q).getRPY(roll,pitch,yaw); 
00177 
00178   Eigen::Matrix3d Rot;
00179   Rot = Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitX()) \
00180       * Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitY()) \
00181       * Eigen::AngleAxisd(yaw, Eigen::Vector3d::UnitZ());
00182   Eigen::Matrix4d T = Eigen::Matrix4d::Zero();
00183 
00184   T.block(0,0,3,3) = Rot;
00185   T(0,3) = transform.getOrigin().x();
00186   T(1,3) = transform.getOrigin().y();
00187   T(2,3) = transform.getOrigin().z();
00188   T(3,3) = 1.0;  
00189 
00190   //ROS_INFO("x: %f y: %f z: %f roll: %f, pitch: %f, yaw: %f",transform.getOrigin().x(),transform.getOrigin().y(),transform.getOrigin().z(),roll,pitch,yaw);
00191 
00192   return T;
00193 }
00194 
00195 void KintonOdomAlgNode::node_config_update(Config &config, uint32_t level)
00196 {
00197   this->alg_.lock();
00198 
00199   this->alg_.unlock();
00200 }
00201 
00202 void KintonOdomAlgNode::addNodeDiagnostics(void)
00203 {
00204 }
00205 
00206 /* main function */
00207 int main(int argc,char *argv[])
00208 {
00209   return algorithm_base::main<KintonOdomAlgNode>(argc, argv, "kinton_odom_alg_node");
00210 }


kinton_odom
Author(s): asantamaria
autogenerated on Fri Dec 6 2013 21:55:13