komodo_sensors.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include "komodo_sensors/apm_adc.h"
00003 #include "komodo_sensors/apm_gps.h"
00004 #include "komodo_sensors/apm_imu.h"
00005 #include "sensor_msgs/Imu.h"
00006 #include "sensor_msgs/Range.h"
00007 #include "sensor_msgs/NavSatFix.h"
00008 #include "sensor_msgs/NavSatStatus.h"
00009 #include "geometry_msgs/Quaternion.h"
00010 #include "geometry_msgs/Vector3Stamped.h"
00011 #include "tf/transform_datatypes.h"
00012 #include "std_msgs/Float32.h"
00013 //#include <tf/transform_broadcaster.h>
00014 
00015 ros::Publisher pc_bat_pub;
00016 
00017 ros::Publisher imu_raw_pub;
00018 ros::Publisher imu_mag_pub;
00019 
00020 ros::Publisher us1_pub;
00021 ros::Publisher us2_pub;
00022 ros::Publisher us3_pub;
00023 ros::Publisher ir1_pub;
00024 ros::Publisher ir2_pub;
00025 ros::Publisher ir3_pub;
00026 
00027 ros::Publisher gps_pub;
00028 
00029 sensor_msgs::Range us1_range_msg;
00030 sensor_msgs::Range us2_range_msg;
00031 sensor_msgs::Range us3_range_msg;
00032 sensor_msgs::Range ir1_range_msg;
00033 sensor_msgs::Range ir2_range_msg;
00034 sensor_msgs::Range ir3_range_msg;
00035 
00036 std::string gps_frame_id;
00037 
00038 //tf::TransformBroadcaster *imu_broadcaster;
00039 
00040 void gpsCallback(const komodo_sensors::apm_gps::ConstPtr& msg) {
00041 
00042         // ROS_INFO("lat:%.7f  lon:%.7f  alt:%.2f  satelites:%d  hdop:%d  status:%d",(float)msg->lat/10000000.0,(float)msg->lon/10000000.0,(float)msg->alt/100.0,msg->sats,msg->hdop,msg->status);
00043         sensor_msgs::NavSatFix gps_msg;
00044         gps_msg.header.frame_id = gps_frame_id;
00045         gps_msg.header.stamp = ros::Time::now();
00046         gps_msg.latitude = (float)msg->lat / 10000000.0;
00047         gps_msg.longitude = (float)msg->lon / 10000000.0;
00048         gps_msg.altitude = (float)msg->alt / 100;
00049 
00050         sensor_msgs::NavSatStatus gps_status_msg;
00051 
00052         gps_status_msg.service = 1; //SERVICE_GPS;
00053         if ((msg->status == 0) || (msg->status == 1))
00054                 gps_status_msg.status = 1; //STATUS_NO_FIX;
00055         else
00056                 gps_status_msg.status = 0; //STATUS_FIX;
00057 
00058         gps_msg.status = gps_status_msg;
00059 
00060         gps_msg.position_covariance_type = 1;
00061 //uint8 COVARIANCE_TYPE_UNKNOWN = 0
00062 //uint8 COVARIANCE_TYPE_APPROXIMATED = 1
00063 //uint8 COVARIANCE_TYPE_DIAGONAL_KNOWN = 2
00064 //uint8 COVARIANCE_TYPE_KNOWN = 3
00065 //float64[9] position_covariance;
00066         float cov = ((float) (msg->hdop) / 100.0) * ((float) (msg->hdop) / 100.0);
00067         gps_msg.position_covariance[0] = cov;
00068         gps_msg.position_covariance[4] = cov;
00069         gps_msg.position_covariance[8] = cov;
00070 //lat_lon_cov=((float)hdop/100)^2;
00071 
00072 //gps_msg.position_covariance=position_covariance;
00073 
00074         gps_pub.publish(gps_msg);
00075 //gps status:
00076         //NO_GPS = 0,             ///< No GPS connected/detected
00077         //  NO_FIX = 1,             ///< Receiving valid GPS messages but no lock
00078         //  GPS_OK = 2              ///< Receiving valid messages and locked
00079 }
00080 
00081 void adcCallback(const komodo_sensors::apm_adc::ConstPtr& msg) {
00082         std_msgs::Float32 v_bat;
00083         v_bat.data = (float)(msg->A7) * 5.0 / 1023.0;
00084         pc_bat_pub.publish(v_bat);
00085 
00086         //ROS_INFO("A0:%.2f  A1:%.2f  A2:%.2f  A3:%.2f  A4:%.2f  A5:%.2f  A6:%.2f  A7:%.2f", (float)msg->A0/10,(float)msg->A1/10,(float)msg->A2/10,(float)msg->A3/10,(float)msg->A4/10,(float)msg->A5/10,(float)msg->A6/10,(float)msg->A7/10);
00087         ros::Time now = ros::Time::now();
00088 
00089         us1_range_msg.header.stamp = now;
00090         us1_range_msg.range = (float) msg->A0 / 1.7 * 2.54 / 100.0;
00091 
00092         us2_range_msg.header.stamp = ros::Time::now();
00093         us2_range_msg.range = (float) msg->A1 / 1.7 * 2.54 / 100.0;
00094 
00095         us3_range_msg.header.stamp = now;
00096         us3_range_msg.range = (float) msg->A2 / 1.7 * 2.54 / 100.0;
00097         
00098         ir1_range_msg.header.stamp = now;
00099         //ir1_range_msg.range = ((6762 / ((float) msg->A3 - 9)) - 6)/100; //from sensor back
00100         
00101         
00102         if (((float)(msg->A3) >=80)&&((float)(msg->A3) <= 500)) ir1_range_msg.range = (4800.0/((float)(msg->A3 - 20)))/100.0; //from sensor back
00103         else if ((float)(msg->A3) <80) ir1_range_msg.range =ir1_range_msg.max_range;
00104         else ir1_range_msg.range =ir1_range_msg.min_range;
00105 
00106         
00107         
00108         ir2_range_msg.header.stamp = now;
00109         //ir2_range_msg.range = ((6762 / ((float) msg->A4 - 9)) - 6)/100; //from sensor back
00110         if (((float)(msg->A4) >=80)&&((float)(msg->A4) <= 500)) ir2_range_msg.range = (4800.0/((float)(msg->A4 - 20)))/100.0; //from sensor back
00111                 else if ((float)(msg->A4) <80) ir2_range_msg.range =ir2_range_msg.max_range;
00112                 else ir2_range_msg.range =ir2_range_msg.min_range;
00113 
00114         
00115         
00116         ir3_range_msg.header.stamp = now;
00117         //ir3_range_msg.range = ((6762 / ((float) msg->A5 - 9)) - 6)/100; //from sensor back
00118         if (((float)(msg->A5) >=80)&&((float)(msg->A5) <= 500)) ir3_range_msg.range = (4800.0/((float)(msg->A5 - 20)))/100.0; //from sensor back
00119                 else if ((float)(msg->A5) <80) ir3_range_msg.range =ir3_range_msg.max_range;
00120                 else ir3_range_msg.range =ir3_range_msg.min_range;
00121 
00122         
00123         us1_pub.publish(us1_range_msg);
00124         us2_pub.publish(us2_range_msg);
00125         us3_pub.publish(us3_range_msg);
00126         ir1_pub.publish(ir1_range_msg);
00127         ir2_pub.publish(ir2_range_msg);
00128         ir3_pub.publish(ir3_range_msg);
00129         //ROS_INFO("r0=%2.2f",us1_range_msg.range);
00130 }
00131 
00132 void imuCallback(const komodo_sensors::apm_imu::ConstPtr& msg) {
00133 
00134         // ROS_INFO("ax:%.5f  ay:%.5f az:%.5f    gx:%.5f  gy:%.5f gz:%.5f    mx:%.5f  my:%.5f mz:%.5f", msg->ax,msg->ay,msg->az, msg->gx,msg->gy,msg->gz, msg->mx,msg->my,msg->mz);
00135         //  ROS_INFO("r:%2.2f  p:%2.2f  y:%2.2f    h:%3.2f  ax:%2.3f  ay:%2.3f az:%2.3f    length: %2.2f", msg->roll*57.29577951,msg->pitch*57.29577951,msg->yaw*57.29577951,msg->heading*57.29577951, msg->ax,msg->ay,msg->az, sqrt(msg->ax*msg->ax+msg->ay*msg->ay+msg->az*msg->az ));
00136         //ROS_INFO("ax:%2.3f  ay:%2.3f az:%2.3f    length: %2.2f", msg->ax,msg->ay,msg->az, sqrt(msg->ax*msg->ax+msg->ay*msg->ay+msg->az*msg->az ));
00137 
00138         sensor_msgs::Imu imu_msg;
00139         geometry_msgs::Vector3Stamped m;
00140 
00141         imu_msg.header.frame_id = "imu_base_link";
00142         imu_msg.header.stamp = ros::Time::now();
00143 
00144         imu_msg.linear_acceleration.x = msg->ax;
00145         imu_msg.linear_acceleration.y = msg->ay;
00146         imu_msg.linear_acceleration.z = msg->az;
00147         imu_msg.angular_velocity.x = msg->gx;
00148         imu_msg.angular_velocity.y = msg->gy;
00149         imu_msg.angular_velocity.z = msg->gz;
00150 
00151         double angular_velocity_stdev_ = 0.012;
00152         double orientation_stdev_ = 0.035;
00153         double linear_acceleration_stdev_ = 0.098;
00154         double angular_velocity_covariance = angular_velocity_stdev_
00155                         * angular_velocity_stdev_;
00156         double orientation_covariance = orientation_stdev_ * orientation_stdev_;
00157         double linear_acceleration_covariance = linear_acceleration_stdev_
00158                         * linear_acceleration_stdev_;
00159 
00160         imu_msg.linear_acceleration_covariance[0] = linear_acceleration_covariance;
00161         imu_msg.linear_acceleration_covariance[4] = linear_acceleration_covariance;
00162         imu_msg.linear_acceleration_covariance[8] = linear_acceleration_covariance;
00163 
00164         imu_msg.angular_velocity_covariance[0] = angular_velocity_covariance;
00165         imu_msg.angular_velocity_covariance[4] = angular_velocity_covariance;
00166         imu_msg.angular_velocity_covariance[8] = angular_velocity_covariance;
00167 
00168         imu_msg.orientation_covariance[0] = orientation_covariance;
00169         imu_msg.orientation_covariance[4] = orientation_covariance;
00170         imu_msg.orientation_covariance[8] = orientation_covariance;
00171 
00172         m.header.frame_id = "imu_base_link";
00173         m.header.stamp = ros::Time::now();
00174         m.vector.x = msg->mx;
00175         m.vector.y = msg->my;
00176         m.vector.z = msg->mz;
00177         //tf::TransformBroadcaster odom_broadcaster;
00178         //geometry_msgs::Quaternion Q=tf::createQuaternionMsgFromRollPitchYaw(roll*0.01745329, pitch*0.01745329, yaw*0.01745329);
00179 
00180         //geometry_msgs::Quaternion Q = tf::createQuaternionMsgFromYaw(yaw*0.01745329);
00181         //imu_msg.orientation=Q;
00182 
00183         imu_raw_pub.publish(imu_msg);
00184         imu_mag_pub.publish(m);
00185         /*
00186          geometry_msgs::TransformStamped imu_trans;
00187          imu_trans.header.stamp = now;
00188          imu_trans.header.frame_id = odom_frame_id;
00189          imu_trans.child_frame_id = "base_footprint";
00190          odom_trans.transform.rotation = quat;
00191          imu_broadcaster->sendTransform(imu_trans);*/
00192 
00193 }
00194 
00195 int main(int argc, char **argv) {
00196 
00197         // ros::init(argc, argv, "apm_talker");
00198         ros::init(argc, argv, "apm_driver");
00199 
00200         ros::NodeHandle n;
00201 
00202         n.param("GPS_frame_id", gps_frame_id, std::string("/map"));
00203 
00204         ros::Subscriber adc_sub = n.subscribe("apm_adc", 10, adcCallback);
00205         ros::Subscriber imu_sub = n.subscribe("apm_imu", 10, imuCallback);
00206         ros::Subscriber gps_sub = n.subscribe("apm_gps", 10, gpsCallback);
00207 
00208         imu_raw_pub = n.advertise < sensor_msgs::Imu > ("imu/data_raw", 10);
00209         imu_mag_pub = n.advertise < geometry_msgs::Vector3Stamped > ("imu/mag", 10);
00210 
00211         us1_range_msg.header.frame_id = "/Right_US_link";
00212         us1_range_msg.field_of_view = 0.8;
00213         us1_range_msg.radiation_type = 0; //us
00214         us1_range_msg.min_range = 0.2;
00215         us1_range_msg.max_range = 6.5;
00216 
00217         us2_range_msg.header.frame_id = "/Back_US_link";
00218         us2_range_msg.field_of_view = 0.8;
00219         us2_range_msg.radiation_type = 0; //us
00220         us2_range_msg.min_range = 0.2;
00221         us2_range_msg.max_range = 6.5;
00222 
00223         us3_range_msg.header.frame_id = "/Left_US_link";
00224         us3_range_msg.field_of_view = 0.8;
00225         us3_range_msg.radiation_type = 0; //us
00226         us3_range_msg.min_range = 0.2;
00227         us3_range_msg.max_range = 6.5;
00228 
00229         ir1_range_msg.header.frame_id = "/Right_IR_link";
00230         ir1_range_msg.field_of_view = 0.05;
00231         ir1_range_msg.radiation_type = 1; //ir
00232         ir1_range_msg.min_range = 0.1;
00233         ir1_range_msg.max_range = 0.8;
00234 
00235         ir2_range_msg.header.frame_id = "/Back_IR_link";
00236         ir2_range_msg.field_of_view = 0.05;
00237         ir2_range_msg.radiation_type = 1; //ir
00238         ir2_range_msg.min_range = 0.1;
00239         ir2_range_msg.max_range = 0.8;
00240 
00241         ir3_range_msg.header.frame_id = "/Left_IR_link";
00242         ir3_range_msg.field_of_view = 0.05;
00243         ir3_range_msg.radiation_type = 1; //ir
00244         ir3_range_msg.min_range = 0.1;
00245         ir3_range_msg.max_range = 0.8;
00246 
00247         us1_pub = n.advertise < sensor_msgs::Range > ("Rangers/Right_US", 5);
00248         us2_pub = n.advertise < sensor_msgs::Range > ("Rangers/Back_US", 5);
00249         us3_pub = n.advertise < sensor_msgs::Range > ("Rangers/Left_US", 5);
00250         ir1_pub = n.advertise < sensor_msgs::Range > ("Rangers/Right_IR", 5);
00251         ir2_pub = n.advertise < sensor_msgs::Range > ("Rangers/Back_IR", 5);
00252         ir3_pub = n.advertise < sensor_msgs::Range > ("Rangers/Left_IR", 5);
00253 
00254         gps_pub = n.advertise < sensor_msgs::NavSatFix > ("gps_publish", 5);
00255 
00256         pc_bat_pub = n.advertise < std_msgs::Float32 > ("pc_bat", 5);
00257 
00258         //imu_broadcaster = new tf::TransformBroadcaster;
00259 
00260         ros::spin();
00261 
00262         return 0;
00263 }
00264 


komodo_sensors
Author(s): RoboTiCan
autogenerated on Tue Jan 7 2014 11:20:00