four_multi
Multi-Antenna,Multi-Node,Multi-Band,Multi-Cell
core/gps.cpp
00001 //
00002 // Copyright 2011-2013, Per Zetterberg, KTH Royal Institute of Technology
00003 //
00004 // This program is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 
00019 #include "gps.hpp"
00020 #include <iostream>
00021 #include <boost/asio/io_service.hpp>
00022 #include <boost/asio/serial_port.hpp>
00023 
00024 
00025 
00026 unsigned int
00027 gps_wait_until(const char* device_name, const unsigned int gps_time_combined ) {
00028   uint32_t hour, minute, second, combined;
00029 
00030 
00031   char data[GPS_CHAR_BUFFER_SIZE];
00032   std::string stored_gps_str, gps_line_str, time_str;
00033   unsigned long first_position, second_position, data_field_position;
00034 
00035 
00036 
00037   boost::asio::io_service io;
00038   boost::asio::serial_port port( io ); 
00039 
00040 
00041   port.open( device_name );   // or whatever the device might be 
00042 
00043 
00044   port.set_option( boost::asio::serial_port_base::baud_rate( 4800 ) );
00045   port.set_option( boost::asio::serial_port_base::character_size( 8 ) );
00046 
00047 
00048 
00049   port.set_option(  boost::asio::serial_port_base::
00050                     parity( boost::asio::serial_port_base::parity::none ) );
00051 
00052   port.set_option( boost::asio::serial_port_base::
00053                    stop_bits( boost::asio::serial_port_base::stop_bits::two ) );
00054   
00055 
00056   port.set_option( boost::asio::serial_port_base::
00057                  flow_control( boost::asio::serial_port_base::
00058                                flow_control::none ) );
00059 
00060 
00061   bool first_time=true;
00062   while (1) {
00063     usleep(10000);
00064     long n=port.read_some(boost::asio::buffer(data, GPS_CHAR_BUFFER_SIZE));
00065     data[n]=0;
00066     if (first_time) {
00067       if (gps_time_combined<100000)
00068         std::cout << "=========== GPS Started! =============== \n";
00069       first_time=false;
00070     }
00071 
00072     //std::cout << data; 
00073     stored_gps_str+=data;
00074     //std::cout << stored_gps_str << std::endl;
00075     first_position=stored_gps_str.find('$');
00076     //std::cout << "first_position=" << first_position << std::endl;
00077     if (!(first_position==std::string::npos)) {
00078       second_position=stored_gps_str.find('$',first_position+1);
00079       //std::cout << "second_position=" << second_position << std::endl;
00080       if (!(second_position==std::string::npos)) {
00081         gps_line_str=stored_gps_str.substr(first_position,second_position-first_position-1);
00082         stored_gps_str=stored_gps_str.substr(second_position);
00083         //std::cout << "================ start" << std::endl;
00084         //std::cout << gps_line_str;
00085         //std::cout << std::endl;
00086         //std::cout << "================ stop" << std::endl;
00087         // Process gps_line_str
00088         data_field_position=gps_line_str.find("GPRMC",0);
00089         if (!(data_field_position==std::string::npos)) {
00090           time_str=gps_line_str.substr(7,6);
00091           //std::cout << "T=" << time_str << std::endl;
00092           
00093           hour=atoi( time_str.substr(0,2).c_str());
00094           minute=atoi( time_str.substr(2,2).c_str());
00095           second=atoi( time_str.substr(4,2).c_str());
00096 
00097           //std::cout << "hour=" << hour << " minute=" 
00098           //    << minute << " second=" << second << " \n";
00099           
00100           combined=hour*60*60+minute*60+second;
00101 
00102           //std::cout << "combined=" << combined << std::endl;
00103           if (combined>=gps_time_combined)
00104             break;
00105 
00106 
00107 
00108         };
00109       };
00110     };
00111   };
00112 
00113   //std::cout << std::endl;
00114   port.close();
00115 
00116 
00117   return gps_time_combined;
00118 
00119 }
00120 
00121 
00122 unsigned int
00123 gps_fix_indicator(const char* device_name) {
00124 
00125 
00126 
00127   char data[GPS_CHAR_BUFFER_SIZE];
00128   std::string stored_gps_str, gps_line_str, temp_str;
00129   unsigned long first_position, second_position, data_field_position;
00130   uint32_t fix;
00131 
00132 
00133   boost::asio::io_service io;
00134   boost::asio::serial_port port( io ); 
00135 
00136 
00137 
00138   port.open( device_name );   // or whatever the device might be 
00139 
00140 
00141   port.set_option( boost::asio::serial_port_base::baud_rate( 4800 ) );
00142   port.set_option( boost::asio::serial_port_base::character_size( 8 ) );
00143 
00144 
00145 
00146   port.set_option(  boost::asio::serial_port_base::
00147                     parity( boost::asio::serial_port_base::parity::none ) );
00148 
00149   port.set_option( boost::asio::serial_port_base::
00150                    stop_bits( boost::asio::serial_port_base::stop_bits::two ) );
00151   
00152 
00153   port.set_option( boost::asio::serial_port_base::
00154                  flow_control( boost::asio::serial_port_base::
00155                                flow_control::none ) );
00156 
00157 
00158 
00159   while (1) {
00160     long n=port.read_some(boost::asio::buffer(data, GPS_CHAR_BUFFER_SIZE));
00161     data[n]=0;
00162     //std::cout << data ;
00163     stored_gps_str+=data;
00164     //std::cout << stored_gps_str << std::endl;
00165     first_position=stored_gps_str.find('$');
00166     //std::cout << "first_position=" << first_position << std::endl;
00167     //std::cout << "std::string::npos=" << std::string::npos << std::endl;
00168     if (!(first_position==std::string::npos)) {
00169       second_position=stored_gps_str.find('$',first_position+1);
00170       //std::cout << "second_position=" << second_position << std::endl;
00171       if (!(second_position==std::string::npos)) {
00172         //std::cout << "str=" << stored_gps_str << std::endl;
00173         gps_line_str=stored_gps_str.substr(first_position,second_position-first_position-1);
00174         stored_gps_str=stored_gps_str.substr(second_position);
00175         //std::cout << "================ start" << std::endl;
00176         //std::cout << gps_line_str;
00177         //std::cout << std::endl;
00178         //std::cout << "================ stop" << std::endl;
00179         // Process gps_line_str
00180         data_field_position=gps_line_str.find("GPGGA",0);
00181         if (!(data_field_position==std::string::npos)) {
00182 
00183           //std::cout << "T=" << gps_line_str << std::endl;
00184           temp_str=gps_line_str;
00185           for (int i1=0;i1<6;i1++) {
00186             temp_str=temp_str.substr(temp_str.find(',')+1);
00187           };
00188 
00189           fix=atoi( temp_str.substr(0,1).c_str());
00190           //std::cout << "R=" << temp_str.substr(0,1) << std::endl;
00191 
00192           
00193           break;
00194 
00195 
00196 
00197         };
00198       };
00199     };
00200   };
00201 
00202   //std::cout << std::endl;
00203   port.close();
00204 
00205 
00206   return fix;
00207 
00208 }
 All Classes Functions Variables