set_gps_reference_node.cpp
Go to the documentation of this file.
00001 /*
00002  * set_gps_reference.cpp
00003  *
00004  *  Created on: Jun 29, 2011
00005  *      Author: acmarkus
00006  */
00007 
00008 #include <ros/ros.h>
00009 #include <sensor_msgs/NavSatFix.h>
00010 #include <string>
00011 
00012 double g_lat_ref;
00013 double g_lon_ref;
00014 double g_alt_ref;
00015 int g_its;
00016 
00017 enum EMode {MODE_AVERAGE = 0, MODE_WAIT}; // average over, or wait for, n GPS fixes
00018 EMode g_mode;
00019 
00020 void gpsCb(const sensor_msgs::NavSatFixConstPtr & msg) {
00021 
00022   static int count = 1;
00023 
00024   if (msg->status.status < sensor_msgs::NavSatStatus::STATUS_FIX) {
00025     ROS_WARN_STREAM_THROTTLE(1, "No GPS fix");
00026     return;
00027   }
00028 
00029   g_lat_ref += msg->latitude;
00030   g_lon_ref += msg->longitude;
00031   g_alt_ref += msg->altitude;
00032 
00033   ROS_INFO("Current measurement: %f, %f, %f", msg->latitude, msg->longitude, msg->altitude);
00034 
00035   if (count == g_its) {
00036     if (g_mode == MODE_AVERAGE) {
00037         g_lat_ref /= g_its;
00038         g_lon_ref /= g_its;
00039         g_alt_ref /= g_its;
00040     }
00041     else {
00042         g_lat_ref = msg->latitude;
00043         g_lon_ref = msg->longitude;
00044         g_alt_ref = msg->altitude;
00045     }
00046 
00047     ros::NodeHandle nh;
00048     nh.setParam("/gps_ref_latitude", g_lat_ref);
00049     nh.setParam("/gps_ref_longitude", g_lon_ref);
00050     nh.setParam("/gps_ref_altitude", g_alt_ref);
00051 
00052     ROS_INFO("Final reference position: %f, %f, %f", g_lat_ref, g_lon_ref, g_alt_ref);
00053 
00054     ros::shutdown();
00055     return;
00056   }
00057   else {
00058     ROS_INFO("    Still waiting for %d measurements", g_its - count);
00059   }
00060 
00061   count++;
00062 }
00063 
00064 int main(int argc, char** argv) {
00065 
00066   ros::init(argc, argv, "set_gps_reference");
00067   ros::NodeHandle nh;
00068 
00069   g_lat_ref = 0.0;
00070   g_lon_ref = 0.0;
00071   g_alt_ref = 0.0;
00072 
00073   ros::V_string args;
00074   ros::removeROSArgs(argc, argv, args);
00075 
00076   g_its = 50; // default number of fixes
00077   g_mode = MODE_AVERAGE; // average by default
00078 
00079   // Look for argument: number of iterations/fixes
00080   if (args.size() >= 2) { // executable name + number of iterations (2 args)
00081     const int parsed = atoi(args[1].c_str());
00082     if (parsed > 0) g_its = parsed;
00083   }
00084 
00085   // Look for argument: mode (average or wait)
00086   if (args.size() >= 3) { // executable name + number of iterations + mode (3 args)
00087     if (args[2] == "wait")
00088         g_mode = MODE_WAIT;
00089   }
00090 
00091   ROS_INFO("Usage: set_gps_reference [n_fixes] [average|wait], defaults: n_fixes=50, average");
00092 
00093   ROS_INFO("Taking %d measurements and %s\n", g_its, (g_mode == MODE_AVERAGE)? "averaging to get the reference" : "taking the last as reference");
00094 
00095   ros::Subscriber gps_sub = nh.subscribe("gps", 1, &gpsCb);
00096 
00097   ros::spin();
00098 }


asctec_hl_gps
Author(s): Markus Achtelik
autogenerated on Thu Jun 6 2019 20:57:13