amcl_gps.cc

00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2000  Brian Gerkey   &  Kasper Stoy
00004  *                      gerkey@usc.edu    kaspers@robotics.usc.edu
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00022 //
00023 // Desc: AMCL gps routines
00024 // Author: Andrew Howard
00025 // Date: 6 Feb 2003
00026 // CVS: $Id: amcl_gps.cc 2394 2004-09-07 00:20:48Z gerkey $
00027 //
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include "config.h"
00032 #endif
00033 
00034 #include "devicetable.h"
00035 #include "amcl_gps.h"
00036 
00037 
00039 // Default constructor
00040 AMCLGps::AMCLGps()
00041 {
00042   this->device = NULL;
00043   this->model = NULL;
00044   
00045   return;
00046 }
00047 
00048 
00050 // Load gps settings
00051 int AMCLGps::Load(ConfigFile* cf, int section)
00052 {
00053   // Device stuff
00054   this->gps_index = cf->ReadInt(section, "gps_index", -1);
00055   
00056   // Create the gps model
00057   this->model = gps_alloc();
00058   this->model->utm_base_e = cf->ReadTupleFloat(section, "utm_base", 0, -1);
00059   this->model->utm_base_n = cf->ReadTupleFloat(section, "utm_base", 1, -1);
00060 
00061   return 0;
00062 }
00063 
00064 
00066 // Unload the model
00067 int AMCLGps::Unload(void)
00068 {
00069   gps_free(this->model);
00070   this->model = NULL;
00071   
00072   return 0;
00073 }
00074 
00075 
00077 // Set up the gps
00078 int AMCLGps::Setup(void)
00079 {
00080   player_device_id_t id;
00081 
00082   // Subscribe to the Gps device
00083   id.code = PLAYER_GPS_CODE;
00084   id.index = this->gps_index;
00085 
00086   this->device = deviceTable->GetDriver(id);
00087   if (!this->device)
00088   {
00089     PLAYER_ERROR("unable to locate suitable gps device");
00090     return -1;
00091   }
00092   if (this->device->Subscribe(this) != 0)
00093   {
00094     PLAYER_ERROR("unable to subscribe to gps device");
00095     return -1;
00096   }
00097 
00098   return 0;
00099 }
00100 
00101 
00103 // Shut down the gps
00104 int AMCLGps::Shutdown(void)
00105 {  
00106   this->device->Unsubscribe(this);
00107   this->device = NULL;
00108 
00109   return 0;
00110 }
00111 
00112 
00114 // Check for new gps data
00115 bool AMCLGps::GetData()
00116 {
00117   size_t size;
00118   player_gps_data_t data;
00119   uint32_t tsec, tusec;
00120 
00121   // Get the gps device data
00122   size = this->device->GetData(this, (uint8_t*) &data, sizeof(data), &tsec, &tusec);
00123   if (size < sizeof(data))
00124     return false;
00125 
00126   if (tsec == this->tsec && tusec == this->tusec)
00127     return false;
00128 
00129   this->tsec = tsec;
00130   this->tusec = tusec;
00131 
00132   this->utm_e = ((int32_t) ntohl(data.utm_e)) / 100.0;
00133   this->utm_n = ((int32_t) ntohl(data.utm_n)) / 100.0;
00134   this->err_horz = ((int32_t) ntohl(data.err_horz)) / 1000.0;
00135   
00136   return true;
00137 }
00138 
00139 
00141 // Initialize from the GPS sensor model
00142 bool AMCLGps::InitSensor(pf_t *pf, pf_vector_t mean, pf_matrix_t cov)
00143 {
00144   // Check for new data
00145   if (!this->GetData())
00146     return false;
00147 
00148   printf("init gps %f %f %f\n", 
00149          this->utm_e, this->utm_n, this->err_horz);
00150 
00151   // Pick up UTM base coordinate
00152   if (this->model->utm_base_e < 0 || this->model->utm_base_n < 0)
00153   {
00154     this->model->utm_base_e = this->utm_e;
00155     this->model->utm_base_n = this->utm_n;
00156     PLAYER_WARN2("UTM base coord not set; defaulting to [%.3f %.3f]",
00157                  this->model->utm_base_e, this->model->utm_base_n);
00158   }
00159 
00160   // Update the gps sensor model with the latest gps measurements
00161   gps_set_utm(this->model, this->utm_e, this->utm_n, this->err_horz);
00162 
00163   // Initialize the initialization routines
00164   gps_init_init(this->model);
00165   
00166   // Draw samples from the gps distribution
00167   pf_init(pf, (pf_init_model_fn_t) gps_init_model, this->model);
00168   
00169   gps_init_term(this->model);
00170   
00171   return true;
00172 }
00173 
00174 
00176 // Apply the gps sensor model
00177 bool AMCLGps::UpdateSensor(pf_t *pf)
00178 {
00179   // Check for new data
00180   if (!this->GetData())
00181     return false;
00182   
00183   printf("update gps %f %f %f\n", 
00184          this->utm_e, this->utm_n, this->err_horz);
00185     
00186   // Update the gps sensor model with the latest gps measurements
00187   gps_set_utm(this->model, this->utm_e, this->utm_n, this->err_horz);
00188 
00189   // Apply the gps sensor model
00190   pf_update_sensor(pf, (pf_sensor_model_fn_t) gps_sensor_model, this->model);  
00191 
00192   return true;
00193 }
00194 
00195 #ifdef INCLUDE_RTKGUI
00196 
00198 // Setup the GUI
00199 void AMCLGps::SetupGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig)
00200 {
00201   this->fig = rtk_fig_create(canvas, NULL, 0);
00202   
00203   return;
00204 }
00205 
00206 
00208 // Shutdown the GUI
00209 void AMCLGps::ShutdownGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig)
00210 {
00211   rtk_fig_destroy(this->fig);
00212   this->fig = NULL;
00213 
00214   return;
00215 }
00216 
00218 // Draw the gps values
00219 void AMCLGps::UpdateGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig)
00220 {
00221   rtk_fig_clear(this->fig);
00222   rtk_fig_color_rgb32(this->fig, 0xFF00FF);
00223 
00224   rtk_fig_ellipse(this->fig,
00225                   this->utm_e - this->model->utm_base_e,
00226                   this->utm_n - this->model->utm_base_n, 0,
00227                   this->err_horz, this->err_horz, 0);
00228   
00229   return;
00230 }
00231 
00232 #endif

Last updated 12 September 2005 21:38:45