evarobot_orientation.cpp
Go to the documentation of this file.
00001 #include "evarobot_orientation/evarobot_orientation.h"
00002 
00003 int i_error_code = 0;
00004 
00005 void ParseRegisters(int32_t i_register_data, int & i_data_l, int & i_data_h)
00006 {
00007         IMUM6::UNION32 union32;
00008 
00009         union32.i = 0x00000000;
00010         union32.i |= (((signed int)i_register_data & 0x000000FF) << 8) | (((signed int)i_register_data & 0x0000FF00) >> 8);
00011         i_data_l = union32.i;
00012         
00013         union32.i = 0x00000000;
00014         union32.i |= ((((signed int)i_register_data >> 16) & 0x000000FF) << 8) | ((((signed int)i_register_data >> 16) & 0x0000FF00) >> 8);
00015         i_data_h = union32.i;
00016 }
00017 
00018 void ParseRegisters(int32_t i_register_data, int & i_data_h)
00019 {
00020         IMUM6::UNION32 union32;
00021         
00022         union32.i = 0x00000000;
00023         union32.i |= ((((signed int)i_register_data >> 16) & 0x000000FF) << 8) | ((((signed int)i_register_data >> 16) & 0x0000FF00) >> 8);
00024         i_data_h = union32.i;
00025 }
00026 
00027 void ProduceDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &stat)
00028 {
00029     if(i_error_code<0)
00030     {
00031         stat.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "%s", GetErrorDescription(i_error_code).c_str());
00032         i_error_code = 0;
00033     }
00034     else
00035     {
00036                 stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "EvarobotOdometry: No problem.");
00037     }
00038 }
00039 
00040 int main(int argc, char **argv)
00041 {
00042         unsigned char u_c_spi_mode;
00043         tcflag_t parity;
00044         tcflag_t parity_on;
00045         
00046         vector<int> T_i_registers;
00047         
00048         IMUM6::UM6_DATA data;
00049         
00050         // ROS PARAMS
00051         
00052         double d_frequency;
00053                 
00054         bool b_always_on;
00055         // rosparams end
00056         
00057         ros::Publisher pub_um6;
00058         sensor_msgs::Imu imu_packet;
00059                 
00060         ros::init(argc, argv, "/evarobot_orientation");
00061         ros::NodeHandle n;
00062         
00063         n.param("evarobot_orientation/alwaysOn", b_always_on, false);
00064                         
00065         if(!n.getParam("evarobot_orientation/frequency", d_frequency))
00066         {
00067                 //ROS_ERROR("Failed to get param 'frequency'");
00068                 ROS_INFO(GetErrorDescription(-116).c_str());
00069         i_error_code = -116;
00070         } 
00071         
00072         
00073         // Set publisher
00074         pub_um6 = n.advertise<sensor_msgs::Imu>("imu", 1);
00075         #ifdef TEST
00076         ros::Publisher pub_pose_demo = n.advertise<nav_msgs::Odometry>("odom_demo", 1);
00077         #endif
00078 
00079         // Define frequency
00080         ros::Rate loop_rate(d_frequency);
00081 
00082         stringstream ss_frame;
00083         ss_frame << n.resolveName(n.getNamespace(), true) << "/imu_link";
00084 
00085         // init imu packets
00086         imu_packet.header.frame_id = ss_frame.str();
00087 
00088 
00089         
00090         switch(PARITY)
00091         {
00092                 case 0:
00093                 {
00094                         parity = 0;
00095                         parity_on = 0;
00096                         break;
00097                 }
00098                 
00099                 case 1:
00100                 {
00101                         parity = PARODD;
00102                         parity_on = PARENB;
00103                         break;
00104                 }
00105                 
00106                 case 2:
00107                 {
00108                         parity = ~PARODD;
00109                         parity_on = PARENB;
00110                         break;
00111                 }
00112                 
00113                 default:
00114                 {
00115                         //ROS_ERROR("Wrong Parity Mode. It should be 0-2.");
00116                         ROS_INFO(GetErrorDescription(-117).c_str());
00117                         i_error_code = -117;
00118                 }
00119         }
00120         
00121         // ROS PARAMS
00122     double d_min_freq = 0.2;
00123         double d_max_freq = 10.0;
00124 
00125         // Diagnostics
00126         diagnostic_updater::Updater updater;
00127         updater.setHardwareID("EvarobotOrientation");
00128         updater.add("orientation", &ProduceDiagnostics);
00129 
00130         diagnostic_updater::HeaderlessTopicDiagnostic pub_freq("orientation", updater,
00131             diagnostic_updater::FrequencyStatusParam(&d_min_freq, &d_max_freq, 0.1, 10));
00132         
00133         IMSerial * p_im_serial;
00134         IMUM6 * p_im_um6;
00135         try{
00136                 // Creating serial and um6 objects.
00137                 IMSerial * p_im_serial = new IMSerial(SERIAL_PATH, BAUDRATE, DATABITS, parity, parity_on, STOP_BITS);
00138                 IMUM6 * p_im_um6 = new IMUM6(p_im_serial);
00139         }catch(int e){
00140                 ROS_INFO(GetErrorDescription(e).c_str());
00141                 i_error_code = e;
00142         }
00143         
00144 
00145         while(ros::ok())
00146         {
00147                 try{            
00148                         if(p_im_um6->GetRawData(data))
00149                         {
00150                         
00151                                 if(!p_im_um6->CheckData()){
00152                                         //ROS_ERROR("Checksum error in serial communication\n");
00153                                         ROS_INFO(GetErrorDescription(-118).c_str());
00154                                         i_error_code = -118;
00155                                 }
00156 
00157                                 
00158                                 p_im_um6->ProcessData(T_i_registers);
00159                                 
00160                                 #ifdef DEBUG
00161                                 
00162                                 ROS_DEBUG("EvarobotOdometry: Read Registers: \n");
00163                                 for(uint i = 0; i < T_i_registers.size(); i++)
00164                                 {
00165                                         ROS_DEBUG("EvarobotOdometry: %x_", T_i_registers[i]);
00166                                         ROS_DEBUG("EvarobotOdometry: ::%x_", p_im_um6->GetDataRegister(T_i_registers[i]) );
00167                                 }
00168                                 
00169                                 
00170                                 #endif
00171                                 
00172                                 geometry_msgs::Quaternion orientation;
00173                                 geometry_msgs::Vector3 linear_acceleration;
00174                                 
00175                                 #ifdef TEST
00176                                 nav_msgs::Odometry pose_demo;
00177                                 #endif
00178                                 
00179                                 int i_orientation_x, i_orientation_y, i_orientation_z, i_orientation_w;
00180                                 int i_linear_acceleration_x, i_linear_acceleration_y, i_linear_acceleration_z;
00181                         
00182                                 ParseRegisters(p_im_um6->GetDataRegister(IMUM6::REGISTERS::UM6_QUAT_AB), i_orientation_y, i_orientation_x);
00183                                  
00184                                 ParseRegisters(p_im_um6->GetDataRegister(IMUM6::REGISTERS::UM6_QUAT_CD), i_orientation_w, i_orientation_z);
00185                                  
00186                                 ParseRegisters(p_im_um6->GetDataRegister(IMUM6::REGISTERS::UM6_ACCEL_PROC_XY), i_linear_acceleration_y, i_linear_acceleration_x);
00187                                 
00188                                 ParseRegisters(p_im_um6->GetDataRegister(IMUM6::REGISTERS::UM6_ACCEL_PROC_Z), i_linear_acceleration_z);
00189                                 
00190                                 linear_acceleration.x = 0.000183105 * (double)i_linear_acceleration_x;
00191                                 linear_acceleration.y = 0.000183105 * (double)i_linear_acceleration_y;
00192                                 linear_acceleration.z = 0.000183105 * (double)i_linear_acceleration_z;
00193                                 
00194                                 orientation.x = 0.0000335693 * (double)i_orientation_x;
00195                                 orientation.y = 0.0000335693 * (double)i_orientation_y;
00196                                 orientation.z = 0.0000335693 * (double)i_orientation_z;
00197                                 orientation.w = 0.0000335693 * (double)i_orientation_w;
00198                                 
00199                                 
00200                                 #ifdef TEST
00201                                 pose_demo.header.stamp = ros::Time::now();
00202                                 pose_demo.header.frame_id = "odom";
00203                                 
00204                                 
00205                                 pose_demo.pose.pose.position.x = 0.0;
00206                                 pose_demo.pose.pose.position.y = 0.0;
00207                                 pose_demo.pose.pose.position.z = 0.0;
00208                                 pose_demo.pose.pose.orientation = orientation;
00209                                 #endif
00210                                 
00211                                 imu_packet.orientation = orientation;
00212                                 imu_packet.linear_acceleration = linear_acceleration;
00213                                 imu_packet.header.stamp = ros::Time::now();
00214                                                 
00215                                 // Publish Data
00216                                 if(pub_um6.getNumSubscribers() > 0 || b_always_on)
00217                                 {
00218                                         pub_um6.publish(imu_packet);
00219                                 }
00220                                 
00221                                 #ifdef TEST
00222                                 if(pub_um6.getNumSubscribers() > 0 || b_always_on)
00223                                 {
00224                                         pub_pose_demo.publish(pose_demo);
00225                                 }
00226                                 #endif
00227                                                 
00228                         
00229                         }
00230                 }catch(int e){
00231                         ROS_INFO(GetErrorDescription(e).c_str());
00232                         i_error_code = e;
00233                 }
00234                 updater.update();
00235                 loop_rate.sleep();
00236         
00237         }
00238         
00239         return 0;
00240 }


evarobot_orientation
Author(s): Mehmet Akcakoca
autogenerated on Fri Feb 12 2016 01:15:29