uncalibvs_alg_node.cpp
Go to the documentation of this file.
00001 #include "uncalibvs_alg_node.h"
00002 
00003 UncalibvsAlgNode::UncalibvsAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<Uncalibvs_Algorithm>(),
00005   activate_(false),
00006   traditional_(false),
00007   random_points_(false),
00008   quadrotor_(true),
00009   output_files_(false),
00010   estop_(false),
00011   serial_(false),
00012   calibrated_ctrl_(true),
00013   lambda_(0.01),
00014   final_z_(1.0),
00015   ENUyaw_(0.0),
00016   ENUyaw_fix_(0.0),
00017   height_fix_(0.0),
00018   tag_thrust_kp_(0.05),
00019   tag_thrust_kd_(0.05),
00020   tag_roll_kp_(0.1),
00021   tag_roll_kd_(0.15),
00022   tag_pitch_kp_(0.1),
00023   tag_pitch_kd_(0.15),
00024   tag_yaw_kp_(0.05),
00025   tag_yaw_kd_(0.05),  
00026   thrust_kp_(0.06),
00027   thrust_kd_(0.005),
00028   roll_kp_(0.2),
00029   roll_kd_(0),
00030   pitch_kp_(0.2),
00031   pitch_kd_(0),
00032   yaw_kp_(0.2),
00033   yaw_kd_(0),  
00034   sat_min_(0.42745),
00035   sat_max_(0.53737),
00036   rollpitch_sat_min_(-0.146),
00037   rollpitch_sat_max_(0.146),
00038   yaw_sat_min_(-1),
00039   yaw_sat_max_(1),
00040   ctrl_ref_(0.447),
00041   trimm_y_(0.0),
00042   trimm_x_(0.0),
00043   buff_speed_(1),
00044   path_("/home/asantamaria/Desktop/poses.txt"),
00045   rollpitch_div_(1000),
00046   Rquad_inertial_(Eigen::MatrixXf::Zero(3,3))
00047 {
00048   this->init_=true; 
00049   //init class attributes if necessary
00050   this->loop_rate_ = 100;//in [Hz]
00051 
00052   // [init publishers]
00053   this->body_twist_publisher_ = this->public_node_handle_.advertise<geometry_msgs::Twist>("body_twist", 1);
00054   this->yaw_control_publisher_ = this->public_node_handle_.advertise<iri_uncalibvs::control>("yaw_control", 1);
00055   this->pitch_control_publisher_ = this->public_node_handle_.advertise<iri_uncalibvs::control>("pitch_control", 1);
00056   this->roll_control_publisher_ = this->public_node_handle_.advertise<iri_uncalibvs::control>("roll_control", 1);
00057   this->thrust_control_publisher_ = this->public_node_handle_.advertise<iri_uncalibvs::control>("thrust_control", 1);
00058   this->state_publisher_ = this->public_node_handle_.advertise<mav_msgs::Status>("state", 10);
00059   this->cmd_yaw_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("cmd_yaw", 10);
00060   this->cmd_pitch_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("cmd_pitch", 10);
00061   this->cmd_roll_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("cmd_roll", 10);
00062   this->cmd_thrust_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("cmd_thrust", 10);
00063   this->ESTOP_publisher_ = this->public_node_handle_.advertise<std_msgs::Bool>("ESTOP", 1);
00064 
00065   // [init subscribers]
00066   this->ll_status_subscriber_ = this->public_node_handle_.subscribe("ll_status", 10, &UncalibvsAlgNode::ll_status_callback, this);
00067   this->height_subscriber_ = this->public_node_handle_.subscribe("height", 10, &UncalibvsAlgNode::height_callback, this);
00068   this->imu_subscriber_ = this->public_node_handle_.subscribe("imu", 10, &UncalibvsAlgNode::imu_callback, this);
00069   this->model_state_subscriber_ = this->public_node_handle_.subscribe("model_state", 10, &UncalibvsAlgNode::model_state_callback, this);
00070   this->input_tag_subscriber_ = this->public_node_handle_.subscribe("input_tag", 10, &UncalibvsAlgNode::input_tag_callback, this);
00071  
00072   // Get initial time
00073   this->time_last_ = ros::Time::now();
00074   
00075   //Init Thrust PID
00076   this->tag_pid_thrust_.initPid(this->tag_thrust_kp_, 0, this->tag_thrust_kd_, 0, 0);
00077   this->pid_thrust_.initPid(this->thrust_kp_, 0, this->thrust_kd_, 0, 0);
00078   
00079   //Init Roll PID
00080   this->tag_pid_roll_.initPid(this->tag_roll_kp_, 0, this->tag_roll_kd_, 0, 0);  
00081   this->pid_roll_.initPid(this->roll_kp_, 0, this->roll_kd_, 0, 0);
00082   
00083   //Init Pitch PID
00084   this->tag_pid_pitch_.initPid(this->tag_pitch_kp_, 0, this->tag_pitch_kd_, 0, 0);  
00085   this->pid_pitch_.initPid(this->pitch_kp_, 0, this->pitch_kd_, 0, 0);
00086   
00087   //Init Yaw PID
00088   this->tag_pid_yaw_.initPid(this->tag_yaw_kp_, 0, this->tag_yaw_kd_, 0, 0);  
00089   this->pid_yaw_.initPid(this->yaw_kp_, 0, this->yaw_kd_, 0, 0);
00090 
00091   // [init services]
00092   
00093   // [init clients]
00094   
00095   // [init action servers]
00096   
00097   // [init action clients]
00098 }
00099 
00100 UncalibvsAlgNode::~UncalibvsAlgNode(void)
00101 {
00102   // [free dynamic memory]
00103 }
00104 
00105 void UncalibvsAlgNode::mainNodeThread(void)
00106 {
00107   
00108   // [fill msg structures]
00109   //this->body_twist_msg_.data = my_var;
00110   //this->yaw_control_msg_.data = my_var;
00111   //this->pitch_control_msg_.data = my_var;
00112   //this->roll_control_msg_.data = my_var;
00113   //this->thrust_control_msg_.data = my_var;
00114   //this->Status_msg_.data = my_var;
00115   //this->cmd_yaw_msg_.data = my_var;
00116   //this->cmd_pitch_msg_.data = my_var;
00117   //this->cmd_roll_msg_.data = my_var;
00118   //this->cmd_thrust_msg_.data = my_var;
00119   //this->Bool_msg_.data = my_var;
00120   
00121   // [fill srv structure and make request to the server]
00122   
00123   // [fill action structure and make request to the action server]
00124   
00125   
00126   //Emergency STOP enable
00127   this->Bool_msg_.data=this->estop_;
00128   if (this->estop_) {
00129     ROS_ERROR_STREAM("EMERGENCY_STOP ENABLED");
00130     this->ESTOP_publisher_.publish(this->Bool_msg_);
00131   }
00132  
00133   // [publish messages]
00134   this->body_twist_publisher_.publish(this->body_twist_msg_);
00135   this->yaw_control_publisher_.publish(this->yaw_control_msg_);
00136   this->pitch_control_publisher_.publish(this->pitch_control_msg_);
00137   this->roll_control_publisher_.publish(this->roll_control_msg_);
00138   this->thrust_control_publisher_.publish(this->thrust_control_msg_);
00139   this->state_publisher_.publish(this->Status_msg_);
00140   this->cmd_yaw_publisher_.publish(this->cmd_yaw_msg_);
00141   this->cmd_pitch_publisher_.publish(this->cmd_pitch_msg_);
00142   this->cmd_roll_publisher_.publish(this->cmd_roll_msg_);
00143   this->cmd_thrust_publisher_.publish(this->cmd_thrust_msg_);
00144 }
00145 
00146 /*  [subscriber callbacks] */
00147 void UncalibvsAlgNode::ll_status_callback(const asctec_msgs::LLStatus::ConstPtr& msg) 
00148 { 
00149   //ROS_INFO("UncalibvsAlgNode::ll_status_callback: New Message Received"); 
00150 
00151   //use appropiate mutex to shared variables if necessary 
00152   //this->alg_.lock(); 
00153   this->ll_status_mutex_.enter(); 
00154 
00155   //std::cout << msg->data << std::endl; 
00156   int ll_status;
00157   ll_status=(int)msg->flightMode;
00158   
00159   std::bitset<32> flight_mode;
00160   flight_mode.reset();
00161   
00162   
00163   int *y,x;
00164   y = reinterpret_cast<int*>(&ll_status);
00165   x=*y;
00166   for(short i=0;i<32;++i) {
00167     flight_mode[i] = x%2;
00168     x /= 2;
00169   }
00170 
00171   
00172   if ((ll_status|0b1111111101111111)!=0b1111111111111111)   ROS_DEBUG("NO Emergency");
00173   if ((ll_status|0b1111111111111101)!=0b1111111111111111)   ROS_DEBUG("NO Height Control");
00174   if ((ll_status|0b1111111111111011)!=0b1111111111111111)   ROS_DEBUG("NO GPS Mode");
00175   if ((ll_status|0b1111111111011111)!=0b1111111111111111) {
00176     ROS_DEBUG("NO Serial Enable");
00177     this->serial_=false;
00178     this->height_fix_=0;
00179   }
00180   else {
00181     this->serial_=true;
00182   }
00183   
00184   if ((ll_status|0b1111111110111111)!=0b1111111111111111)   ROS_DEBUG("NO Serial Active");
00185 
00186   //unlock previously blocked shared variables 
00187   //this->alg_.unlock(); 
00188   this->ll_status_mutex_.exit(); 
00189 }
00190 void UncalibvsAlgNode::height_callback(const mav_msgs::Height::ConstPtr& msg) 
00191 { 
00192   //ROS_INFO("UncalibvsAlgNode::height_callback: New Message Received"); 
00193 
00194   //use appropiate mutex to shared variables if necessary 
00195   //this->alg_.lock(); 
00196   this->height_mutex_.enter(); 
00197 
00198   //std::cout << msg->data << std::endl; 
00199 
00200   this->height_=msg->height;
00201   
00202   // Initialize the height to fix the quadrotor if no tag is detected
00203   if(this->height_fix_==0 && this->serial_) this->height_fix_=this->height_;
00204   
00205   //unlock previously blocked shared variables 
00206   //this->alg_.unlock(); 
00207   this->height_mutex_.exit(); 
00208 }
00209 void UncalibvsAlgNode::imu_callback(const sensor_msgs::Imu::ConstPtr& msg) 
00210 { 
00211   //ROS_INFO("UncalibvsAlgNode::imu_callback: New Message Received"); 
00212 
00213   //use appropiate mutex to shared variables if necessary 
00214   //this->alg_.lock(); 
00215   this->imu_mutex_.enter(); 
00216 
00217    //std::cout << msg->data << std::endl; 
00218   this->twist_quad_.linear.x=0.0;
00219   this->twist_quad_.linear.y=0.0;
00220   this->twist_quad_.linear.z=0.0;
00221   this->twist_quad_.angular.x=-msg->angular_velocity.x;
00222   this->twist_quad_.angular.y=msg->angular_velocity.y;
00223   this->twist_quad_.angular.z=msg->angular_velocity.z;
00224 
00225   //Get quadrotor pose to extract body to inertial rotation
00226   geometry_msgs::Pose qpose;
00227   qpose.position.x=0.0;
00228   qpose.position.y=0.0;
00229   qpose.position.z=0.0;
00230   qpose.orientation=msg->orientation;
00231   
00232   tf::Transform tf_pose;
00233   double ya,pi,ro;
00234   tf::poseMsgToTF(qpose, tf_pose);
00235   tf_pose.getBasis().getRPY(ro,pi,ya);
00236 
00237 
00238   //   ROS_INFO("comp roll: %2.4f",ro);
00239   //   ROS_INFO("comp pitch: %2.4f",pi);
00240   //   ROS_INFO("comp yaw: %2.4f",ya); 
00241   
00242   //rotation between body frame and commands frame. Vx, Vy and Vz are commanded in the command frame, angular velocities in the Body frame,
00243   //thus the linear velocities calculated to the body frame should be rotated to the command frame.
00244   
00245   //Roll and pitch lectures readed from compass
00246   Eigen::Matrix3f Rwb,Rfix,Rwfb,Rfbi;
00247   // euler convention zyx 
00248   // Rotation from world to body (sensors) frame
00249   Rwb = Eigen::AngleAxisf(ro, Eigen::Vector3f::UnitX()) \
00250       * Eigen::AngleAxisf(pi, Eigen::Vector3f::UnitY()) \
00251       * Eigen::AngleAxisf(ya, Eigen::Vector3f::UnitZ());
00252   
00253   //ROS_WARN_STREAM(Rwb);    
00254   //Rotation from Body (sensors) to quadrotor front body attahed frame.
00255   //Yaw fixed to 3pi/4 (relation between the compass readings and the front body frame)
00256   Rfix=Eigen::MatrixXf::Zero(3,3);
00257   double zang=0.75*3.1416;
00258   //   Rfix(0,0)=cos(zang);
00259   //   Rfix(0,1)=sin(zang);
00260   //   Rfix(1,0)=-sin(zang);
00261   //   Rfix(1,1)=cos(zang);
00262   //   Rfix(2,2)=1.0;
00263   Rfix = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitX()) \
00264        * Eigen::AngleAxisf(0, Eigen::Vector3f::UnitY()) \
00265        * Eigen::AngleAxisf(zang, Eigen::Vector3f::UnitZ());
00266   
00267   //ROS_INFO_STREAM(Rfix);
00268   //Rotation obtained using sensor reading and fixed yaw relation (from inertial to front body)
00269   Rwfb=Rwb*Rfix;
00270   //ROS_WARN_STREAM(Rwfb);
00271   // Get world to front body angles
00272   //print angles of pitch and roll inertial
00273   Eigen::MatrixXf wfbangl(3,1);
00274   wfbangl=Rwfb.eulerAngles(0,1,2);
00275   
00276   // Front body inertial
00277   Rfbi = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitX()) \
00278          * Eigen::AngleAxisf(0, Eigen::Vector3f::UnitY()) \
00279          * Eigen::AngleAxisf(wfbangl(2), Eigen::Vector3f::UnitZ());
00280   
00281   //ROS_INFO_STREAM(Rfbi); 
00282   // Front body to inertial
00283   Rquad_inertial_=Rfbi.transpose()*Rwfb;
00284   
00285   //ROS_ERROR_STREAM(Rquad_inertial_);
00286   
00287   
00288   this->ENUyaw_=wfbangl(2);
00289     // Initialize the height to fix the quadrotor if no tag is detected
00290   if(this->ENUyaw_fix_==0 && this->serial_) this->ENUyaw_fix_=this->ENUyaw_;
00291   
00292   
00293   //   print angles of pitch and roll inertial
00294   //   Eigen::MatrixXf angl(3,1);
00295   //   angl=Rquad_inertial_.eulerAngles(0,1,2);
00296     
00297   //   ROS_INFO("roll: %2.4f",angl(0));
00298   //   ROS_INFO("pitch: %2.4f",angl(1));
00299   //   ROS_INFO("yaw: %2.4f",angl(2));
00300     
00301     
00302   //   double alfa;
00303   //   Eigen::MatrixXf Norm(3,1),zbody(3,1);
00304   // 
00305   //   zbody=Rwfb.col(2);
00306   // 
00307   //   Norm(0)=zbody(1);
00308   //   Norm(1)=-zbody(0);
00309   //   Norm(2)=0.0;
00310   //   
00311   //   Norm=Norm/(sqrt((Norm(0)*Norm(0))+(Norm(1)*Norm(1))+(Norm(2)*Norm(2))));
00312   // 
00313   //   alfa=acos(zbody(2));
00314   // 
00315   //   Rquad_inertial_(0,0)=cos(alfa)+(Norm(0)*Norm(0)*(1-cos(alfa)));
00316   //   Rquad_inertial_(0,1)=(Norm(0)*Norm(1)*(1-cos(alfa)))-(Norm(2)*sin(alfa));
00317   //   Rquad_inertial_(0,2)=(Norm(0)*Norm(2)*(1-cos(alfa)))+(Norm(1)*sin(alfa));
00318   //   Rquad_inertial_(1,0)=(Norm(0)*Norm(1)*(1-cos(alfa)))+(Norm(2)*sin(alfa));
00319   //   Rquad_inertial_(1,1)=cos(alfa)+(Norm(1)*Norm(1)*(1-cos(alfa)));
00320   //   Rquad_inertial_(1,2)=(Norm(1)*Norm(2)*(1-cos(alfa)))-(Norm(0)*sin(alfa));
00321   //   Rquad_inertial_(2,0)=(Norm(0)*Norm(2)*(1-cos(alfa)))-(Norm(1)*sin(alfa));
00322   //   Rquad_inertial_(2,1)=(Norm(1)*Norm(2)*(1-cos(alfa)))+(Norm(0)*sin(alfa));
00323   //   Rquad_inertial_(2,2)=cos(alfa)+(Norm(2)*Norm(2)*(1-cos(alfa)));
00324 
00325 
00326   // Convert quaternion to RPY.
00327   //tf::Quaternion qt;
00328   //tf::quaternionMsgToTF(msg->orientation, qt);
00329   //tf::Matrix3x3(qt).getRPY(this->ENUroll_,this->ENUpitch_,this->ENUyaw_);
00330 
00331   //unlock previously blocked shared variables 
00332   //this->alg_.unlock(); 
00333   this->imu_mutex_.exit(); 
00334 }
00335 
00336 void UncalibvsAlgNode::model_state_callback(const asctec_msgs::IMUCalcData::ConstPtr& msg) 
00337 { 
00338   //ROS_INFO("UncalibvsAlgNode::model_state_callback: New Message Received"); 
00339 
00340   //use appropiate mutex to shared variables if necessary 
00341   //this->alg_.lock(); 
00342   this->model_state_mutex_.enter(); 
00343   
00345   int size=this->buff_speed_;
00346   std::vector<double> buff_x(size),buff_y(size);
00347   buff_x.reserve(size);
00348   buff_y.reserve(size);
00349   double m_x,m_y;
00350   int index;
00351   m_x=0.0;
00352   m_y=0.0;
00353   if (index==size) index=0;
00354   buff_x[index]=msg->speed_x;
00355   buff_y[index]=msg->speed_y;
00356   index=index+1; 
00357   for(int i=0; i<(size-1); ++i){
00358     m_x += buff_x[i];
00359     m_y += buff_y[i];
00360   }
00361   
00362   Eigen::MatrixXf body_speed(3,1),inertial_speed(3,1);
00363   
00364   //   body_speed(0,0)=m_x/size;
00365   //   body_speed(1,0)=m_y/size;
00366   body_speed(0,0)=msg->speed_x;
00367   body_speed(1,0)=msg->speed_y;
00368   body_speed(2,0)=msg->speed_z;
00369   
00370   Eigen::Matrix3f Rfix;
00371   // Convert from sensors frame to front frame
00372   Rfix=Eigen::MatrixXf::Zero(3,3);
00373   double zang=0.75*3.1416;
00374   Rfix = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitX()) \
00375        * Eigen::AngleAxisf(0, Eigen::Vector3f::UnitY()) \
00376        * Eigen::AngleAxisf(zang, Eigen::Vector3f::UnitZ());
00377        
00378   // convert from sensors frame velocities to front inertial frame velocities
00379   //inertial_speed=Rquad_inertial_*Rfix*body_speed;
00380   inertial_speed=Rfix*body_speed;          
00381   /*  ROS_INFO("spedd_x: %2.4f",this->twist_quad_asctec_.linear.x);
00382   ROS_INFO("spedd_y: %2.4f",this->twist_quad_asctec_.linear.y); */ 
00383   
00384   this->twist_quad_asctec_.linear.x=inertial_speed(0,0);
00385   this->twist_quad_asctec_.linear.y=inertial_speed(1,0);
00386   this->twist_quad_asctec_.linear.z=inertial_speed(2,0);
00387   this->twist_quad_asctec_.angular.x=msg->angvel_roll;
00388   this->twist_quad_asctec_.angular.y=msg->angvel_nick;
00389   this->twist_quad_asctec_.angular.z=msg->angvel_yaw;
00390   
00391   this->body_twist_msg_=this->twist_quad_asctec_;
00392   
00393   
00394   //   ROS_INFO("spedd_x: %2.4f",this->twist_quad_asctec_.linear.x);
00395   //   ROS_INFO("spedd_y: %2.4f",this->twist_quad_asctec_.linear.y);
00396   //   ROS_INFO("spedd_z: %2.4f",this->twist_quad_asctec_.linear.z);
00397   
00398   
00399 
00400   //scale angular velocities 64.8=1 degree/s --> [-100..100]  
00401   //if (twist_quad_.angular.x!=0) twist_quad_.angular.x=(twist_quad_.angular.x/116.64)+100;
00402   //if (twist_quad_.angular.y!=0) twist_quad_.angular.y=(twist_quad_.angular.y/116.64)+100;      
00403   //if (twist_quad_.angular.z!=0) twist_quad_.angular.z=(twist_quad_.angular.z/116.64)+100;    
00404 
00405   //this->alg_.lock();   
00406   this->model_state_mutex_.exit(); 
00407 }
00408 
00409 void UncalibvsAlgNode::input_tag_callback(const ar_pose::ARMarkers::ConstPtr& msg) 
00410 { 
00411 
00412   //use appropiate mutex to shared variables if necessary 
00413   this->alg_.lock();   
00414 
00415   this->time_ = ros::Time::now();
00416 
00417   double x,y,z,yaw,pitch,roll,tag_cmd_pid_roll,tag_cmd_pid_pitch,tag_cmd_pid_yaw,tag_cmd_pid_thrust,cmd_pid_roll,cmd_pid_pitch,cmd_pid_yaw,cmd_pid_thrust;
00418   x=0;
00419   y=0;
00420   z=0;
00421   roll=0;
00422   pitch=0;
00423   yaw=0;
00424   tag_cmd_pid_roll=0;
00425   tag_cmd_pid_pitch=0;
00426   tag_cmd_pid_yaw=0;
00427   tag_cmd_pid_thrust=0;
00428   cmd_pid_roll=0;
00429   cmd_pid_pitch=0;
00430   cmd_pid_yaw=0;
00431   cmd_pid_thrust=0;
00432   
00433   Eigen::Matrix3f R;
00434   Eigen::MatrixXf Vel_quad(6,1);
00435   Vel_quad=Eigen::MatrixXf::Zero(6,1);
00436   this->error_=Eigen::MatrixXf::Zero(6,1);
00437   this->tag_error_=Eigen::MatrixXf::Zero(6,1);
00438  
00439   if (msg->markers.empty()){
00440      this->init_=true;  
00441     
00442      // thrust control /////////////////////////////////////////////////////////////////////////////////////////////////
00443      this->error_(2)=this->height_ - this->height_fix_;
00444      
00445      // roll control ////////////////////////////////////////////////////////////////////////////////////////////////
00446      this->error_(1)= -(this->twist_quad_asctec_.linear.y/this->rollpitch_div_) - 0.0;
00447 
00448      // pitch control //////////////////////////////////////////////////////////////////////////////////////////////////
00449      this->error_(0)=this->twist_quad_asctec_.linear.x/this->rollpitch_div_- 0.0;
00450      
00451      // yaw control //////////////////////////////////////////////////////////////////////////////////////////////////
00452      this->error_(5)=this->ENUyaw_ - this->ENUyaw_fix_;
00453      
00455      this->error_(1)=0.0;
00456      this->error_(0)=0.0;
00457      this->error_(5)=0.0;
00458      this->tag_error_=this->error_;
00461      this->thrust_control_msg_.marker=false;
00462      this->roll_control_msg_.marker=false;
00463      this->pitch_control_msg_.marker=false;
00464      this->yaw_control_msg_.marker=false;
00465      
00466   }
00467   
00468   else if (!msg->markers.empty()){
00469     
00470     geometry_msgs::Pose pose;
00471     pose = msg->markers.back().pose.pose;
00472     
00473     ros::Time mt,mt_old;
00474     mt_old=mt;
00475     //Actual reception time
00476     mt = msg->markers.back().header.stamp;
00477 
00478     //reset time that will reset the algorithm integration
00479     if (this->init_){
00480       mt_old=mt;
00481       this->init_=false;
00482     }
00483      
00484     ros::Duration Ts;  
00485     Ts=mt-mt_old;
00486     
00487     double secs;
00488     secs=Ts.toSec();  
00489     
00490     if (this->activate_){
00491       //this->input_mutex_.enter(); 
00492     
00493       // Convert pose from marker////////////////////////////////////////////////
00494       // Convert quaternion to RPY.
00495       tf::Transform tf_pose;
00496     
00497       tf::poseMsgToTF(pose, tf_pose);
00498       tf_pose.getBasis().getRPY(roll,pitch,yaw);
00499   
00500       // euler convention zyx  
00501       R = Eigen::AngleAxisf(roll, Eigen::Vector3f::UnitZ()) \
00502           * Eigen::AngleAxisf(pitch, Eigen::Vector3f::UnitY()) \
00503           * Eigen::AngleAxisf(yaw, Eigen::Vector3f::UnitX());
00504 
00505       x = pose.position.x;
00506       y = pose.position.y;
00507       z = pose.position.z;
00508 
00509       Eigen::Matrix4f cTo(4,4);
00510     
00511       cTo.block(0,0,3,3)=R;  
00512       cTo.block(0,3,3,1) << x, y, z;
00513       cTo.row(3) << 0.0, 0.0, 0.0, 1.0;
00514     
00515       // Extract the Non DOF velocities ///////////////////////////////////    
00516       Eigen::MatrixXf V2(2,1);
00517       V2=Eigen::MatrixXf::Zero(2,1);
00518       if (this->quadrotor_){
00519         V2(0,0)=this->twist_quad_.angular.x;
00520         V2(1,0)=this->twist_quad_.angular.y; 
00521         //V2(0,0)=0.0;
00522         //V2(1,0)=0.0; 
00523       }
00524     
00525       // ARM NON existent in the real robot
00526       bool arm = false;
00527       KDL::Chain kdl_chain;
00528       Eigen::MatrixXf q;
00529       q=Eigen::MatrixXf::Zero(11,1);
00530       
00531   //    if (calibrated_ctrl_) {
00533         this->tag_error_(0)=0.0-y;
00534         this->tag_error_(1)=0.0-x;
00535         this->tag_error_(2)=z-this->final_z_;
00536         this->tag_error_(5)=0.0-yaw;
00537   //    }      
00538   //    else {
00540         uncalib_vs_alg_.uncalib_vs(this->traditional_,this->random_points_,this->quadrotor_,arm,this->output_files_,this->path_,this->lambda_,this->final_z_,cTo,secs,V2,kdl_chain,q,this->Rquad_inertial_,Vel_quad);
00541         //roll and pitch sign changed
00542         this->error_=-Vel_quad/lambda_;         
00543   //    }
00544     }
00545     // Get a as heigh to be fixed, the last one when a tag was found
00546     if (this->serial_) {
00547       this->height_fix_=this->height_;
00548       this->ENUyaw_fix_=this->ENUyaw_;
00549     }
00550     
00551      this->thrust_control_msg_.marker=true;
00552      this->roll_control_msg_.marker=true;
00553      this->pitch_control_msg_.marker=true;
00554      this->yaw_control_msg_.marker=true;
00555 
00556   }
00557   
00559   tag_cmd_pid_thrust = this->tag_pid_thrust_.updatePid(this->tag_error_(2), this->time_ - this->time_last_);
00560   tag_cmd_pid_thrust=tag_cmd_pid_thrust+this->ctrl_ref_;
00561   if (tag_cmd_pid_thrust>this->sat_max_) tag_cmd_pid_thrust=this->sat_max_;
00562   if (tag_cmd_pid_thrust<this->sat_min_) tag_cmd_pid_thrust=this->sat_min_; 
00563   
00564   cmd_pid_thrust = this->pid_thrust_.updatePid(this->error_(2), this->time_ - this->time_last_);
00565   cmd_pid_thrust=cmd_pid_thrust+this->ctrl_ref_;
00566   if (cmd_pid_thrust>this->sat_max_) cmd_pid_thrust=this->sat_max_;
00567   if (cmd_pid_thrust<this->sat_min_) cmd_pid_thrust=this->sat_min_;
00568 
00569   this->thrust_control_msg_.name="Thrust";
00570   this->thrust_control_msg_.tag_error=this->tag_error_(2);
00571   this->thrust_control_msg_.tag_satured=tag_cmd_pid_thrust;
00572   this->thrust_control_msg_.uncal_error=this->error_(2);
00573   this->thrust_control_msg_.uncal_satured=cmd_pid_thrust;  
00574   
00575   if (calibrated_ctrl_) this->cmd_thrust_msg_.data=tag_cmd_pid_thrust;
00576   else this->cmd_thrust_msg_.data=cmd_pid_thrust;
00577   
00579   tag_cmd_pid_roll = this->tag_pid_roll_.updatePid(this->tag_error_(1), this->time_ - this->time_last_);
00580   tag_cmd_pid_roll=tag_cmd_pid_roll-this->trimm_y_;
00581   if (tag_cmd_pid_roll>this->rollpitch_sat_max_) tag_cmd_pid_roll=this->rollpitch_sat_max_;
00582   if (tag_cmd_pid_roll<this->rollpitch_sat_min_) tag_cmd_pid_roll=this->rollpitch_sat_min_;  
00583     
00584   cmd_pid_roll = this->pid_roll_.updatePid(this->error_(1), this->time_ - this->time_last_);
00585   cmd_pid_roll=cmd_pid_roll-this->trimm_y_;
00586   if (cmd_pid_roll>this->rollpitch_sat_max_) cmd_pid_roll=this->rollpitch_sat_max_;
00587   if (cmd_pid_roll<this->rollpitch_sat_min_) cmd_pid_roll=this->rollpitch_sat_min_;
00588 
00589   this->roll_control_msg_.name="Roll";
00590   this->roll_control_msg_.tag_error=this->tag_error_(1);
00591   this->roll_control_msg_.tag_satured=tag_cmd_pid_roll;
00592   this->roll_control_msg_.uncal_error=this->error_(1);
00593   this->roll_control_msg_.uncal_satured=cmd_pid_roll; 
00594   
00595   if (calibrated_ctrl_) this->cmd_roll_msg_.data=tag_cmd_pid_roll;
00596   else this->cmd_roll_msg_.data=cmd_pid_roll;
00597   
00599   tag_cmd_pid_pitch = this->tag_pid_pitch_.updatePid(this->tag_error_(0), this->time_ - this->time_last_);
00600   tag_cmd_pid_pitch=tag_cmd_pid_pitch-this->trimm_x_;
00601   if (tag_cmd_pid_pitch>this->rollpitch_sat_max_) tag_cmd_pid_pitch=this->rollpitch_sat_max_;
00602   if (tag_cmd_pid_pitch<this->rollpitch_sat_min_) tag_cmd_pid_pitch=this->rollpitch_sat_min_; 
00603   
00604   cmd_pid_pitch = this->pid_pitch_.updatePid(this->error_(0), this->time_ - this->time_last_);
00605   cmd_pid_pitch=cmd_pid_pitch-this->trimm_x_;
00606   if (cmd_pid_pitch>this->rollpitch_sat_max_) cmd_pid_pitch=this->rollpitch_sat_max_;
00607   if (cmd_pid_pitch<this->rollpitch_sat_min_) cmd_pid_pitch=this->rollpitch_sat_min_;
00608 
00609   this->pitch_control_msg_.name="Pitch";
00610   this->pitch_control_msg_.tag_error=this->tag_error_(0);
00611   this->pitch_control_msg_.tag_satured=tag_cmd_pid_pitch;
00612   this->pitch_control_msg_.uncal_error=this->error_(0);
00613   this->pitch_control_msg_.uncal_satured=cmd_pid_pitch;
00614   
00615   if (calibrated_ctrl_) this->cmd_pitch_msg_.data=tag_cmd_pid_pitch;
00616   else this->cmd_pitch_msg_.data=cmd_pid_pitch;
00617 
00619   tag_cmd_pid_yaw  = this->tag_pid_yaw_.updatePid(this->tag_error_(5), this->time_ - this->time_last_);
00620   if (tag_cmd_pid_yaw>yaw_sat_max_) tag_cmd_pid_yaw=yaw_sat_max_;
00621   if (tag_cmd_pid_yaw<yaw_sat_min_) tag_cmd_pid_yaw=yaw_sat_min_;
00622   
00623   cmd_pid_yaw = this->pid_yaw_.updatePid(-(this->error_(5)), this->time_ - this->time_last_);
00624   if (cmd_pid_yaw>yaw_sat_max_) cmd_pid_yaw=yaw_sat_max_;
00625   if (cmd_pid_yaw<yaw_sat_min_) cmd_pid_yaw=yaw_sat_min_;
00626 
00627   this->yaw_control_msg_.name="Yaw";
00628   this->yaw_control_msg_.tag_error=this->tag_error_(5);
00629   this->yaw_control_msg_.tag_satured=tag_cmd_pid_yaw;
00630   this->yaw_control_msg_.uncal_error=this->error_(5);
00631   this->yaw_control_msg_.uncal_satured=cmd_pid_yaw;
00632   
00633   if (calibrated_ctrl_) this->cmd_yaw_msg_.data=tag_cmd_pid_yaw; 
00634   else this->cmd_yaw_msg_.data=cmd_pid_yaw;  
00635   
00637   
00638   ros::Time stamp = ros::Time::now();
00639   this->Status_msg_.header.stamp=stamp;
00640 
00641   this->time_last_ = this->time_;
00642   
00643   //unlock previously blocked shared variables 
00644   this->alg_.unlock(); 
00645 
00646 }
00647 
00648 /*  [service callbacks] */
00649 
00650 /*  [action callbacks] */
00651 
00652 
00653 void UncalibvsAlgNode::node_config_update(Config &config, uint32_t level)
00654 {
00655   this->alg_.lock();
00656   
00657   this->activate_=config.activate;
00658   this->traditional_=config.traditional;
00659   this->lambda_=config.lambda;
00660   this->final_z_=config.final_z;
00661   this->random_points_=config.random_points;
00662   this->quadrotor_=config.quadrotor;
00663   this->output_files_=config.output_files;
00664   this->path_=config.path;
00665   this->tag_thrust_kp_=config.tag_thrust_kp;
00666   this->tag_thrust_kd_=config.tag_thrust_kd;
00667   this->tag_roll_kp_=config.tag_roll_kp;
00668   this->tag_roll_kd_=config.tag_roll_kd;
00669   this->tag_pitch_kp_=config.tag_pitch_kp;
00670   this->tag_pitch_kd_=config.tag_pitch_kd;
00671   this->tag_yaw_kp_=config.tag_yaw_kp;
00672   this->tag_yaw_kd_=config.tag_yaw_kd;   
00673   this->thrust_kp_=config.thrust_kp;
00674   this->thrust_kd_=config.thrust_kd;
00675   this->roll_kp_=config.roll_kp;
00676   this->roll_kd_=config.roll_kd;
00677   this->pitch_kp_=config.pitch_kp;
00678   this->pitch_kd_=config.pitch_kd;
00679   this->yaw_kp_=config.yaw_kp;
00680   this->yaw_kd_=config.yaw_kd;     
00681   this->sat_min_=config.thrust_sat_min;
00682   this->sat_max_=config.thrust_sat_max;
00683   this->ctrl_ref_=config.thrust_ctrl_ref;
00684   this->trimm_y_=config.trimm_y;
00685   this->trimm_x_=config.trimm_x;
00686   this->rollpitch_sat_min_=config.rollpitch_sat_min;
00687   this->rollpitch_sat_max_=config.rollpitch_sat_max;
00688   this->yaw_sat_min_=config.yaw_sat_min;
00689   this->yaw_sat_max_=config.yaw_sat_max;
00690   this->rollpitch_div_=config.rollpitch_div;
00691   this->buff_speed_=config.buff_speed;
00692   this->estop_=config.EMERGENCY_STOP;
00693   this->calibrated_ctrl_=config.calibrated_ctrl;
00694   
00695   
00696   //Thrust PID
00697   this->tag_pid_thrust_.setGains(this->tag_thrust_kp_, 0, this->tag_thrust_kd_, 0, 0);
00698   this->pid_thrust_.setGains(this->thrust_kp_, 0, this->thrust_kd_, 0, 0);
00699     
00700   //Roll PID
00701   this->tag_pid_roll_.setGains(this->tag_roll_kp_, 0, this->tag_roll_kd_, 0, 0);
00702   this->pid_roll_.setGains(this->roll_kp_, 0, this->roll_kd_, 0, 0);
00703   
00704   //Pitch PID
00705   this->tag_pid_pitch_.setGains(this->tag_pitch_kp_, 0, this->tag_pitch_kd_, 0, 0);  
00706   this->pid_pitch_.setGains(this->pitch_kp_, 0, this->pitch_kd_, 0, 0);
00707   
00708   //Yaw PID
00709   this->tag_pid_yaw_.setGains(this->tag_yaw_kp_, 0, this->tag_yaw_kd_, 0, 0);  
00710   this->pid_yaw_.setGains(this->yaw_kp_, 0, this->yaw_kd_, 0, 0);
00711   
00712   this->alg_.unlock();
00713 }
00714 
00715 void UncalibvsAlgNode::addNodeDiagnostics(void)
00716 {
00717 }
00718 
00719 /* main function */
00720 int main(int argc,char *argv[])
00721 {
00722 
00723   return algorithm_base::main<UncalibvsAlgNode>(argc, argv, "uncalibvs_alg_node");
00724 }


iri_uncalibvs
Author(s): Àngel Sanatamaria Navarro
autogenerated on Fri Dec 6 2013 23:40:59