Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-cellular-gps-bg96
BG96_GNSS.cpp
00001 #include "BG96_GNSS.h" 00002 00003 BG96_GNSS::BG96_GNSS() 00004 { 00005 _bg96_at_handler = CellularContext::get_default_instance()->get_device()->get_at_handler(); 00006 printf("[GPS] Created BG96 Object with AT HANDLER ADDR[%p].\r\n", _bg96_at_handler); 00007 } 00008 00009 uint8_t BG96_GNSS::set_gps_power(bool state) 00010 { 00011 char _at_cmd[16]; 00012 uint8_t status = SUCCESS; 00013 00014 sprintf((char *)_at_cmd, "%s", state ? "AT+QGPS=1" : "AT+QGPSEND"); 00015 _bg96_at_handler->lock(); 00016 _bg96_at_handler->cmd_start(_at_cmd); 00017 _bg96_at_handler->cmd_stop_read_resp(); 00018 if (_bg96_at_handler->get_last_error() != NSAPI_ERROR_OK) { 00019 printf("[GPS] Failure while trying to change BG96 power.\r\n"); 00020 status = FAILURE; 00021 } 00022 printf("[GPS] Powered ON the GPS module.\r\n"); 00023 _bg96_at_handler->unlock(); 00024 00025 return status; 00026 } 00027 00028 uint8_t BG96_GNSS::get_gps_data(gps_data *data) 00029 { 00030 uint8_t status = SUCCESS; 00031 00032 _bg96_at_handler->lock(); 00033 _bg96_at_handler->cmd_start("AT+QGPSLOC=2"); 00034 _bg96_at_handler->cmd_stop(); 00035 00036 if (_bg96_at_handler->get_last_error() == NSAPI_ERROR_DEVICE_ERROR) { 00037 printf("[GPS] Error while trying to get GPS data.\r\n"); 00038 status = FAILURE; 00039 } 00040 else { 00041 _bg96_at_handler->set_stop_tag("OK"); 00042 _bg96_at_handler->resp_start(); 00043 if (_read_gps(data) == FAILURE) { 00044 printf("[GPS] Error while trying to read GPS data.\r\n"); 00045 printf("[GPS] Ensure that GPS antenna is plugged in.\r\n"); 00046 status = FAILURE; 00047 } 00048 _bg96_at_handler->resp_stop(); 00049 } 00050 _bg96_at_handler->unlock(); 00051 00052 return status; 00053 } 00054 00055 uint8_t BG96_GNSS::_read_gps(gps_data *data) 00056 { 00057 ssize_t bg96_read_size; 00058 uint8_t status = SUCCESS; 00059 00060 bg96_read_size = _bg96_at_handler->read_string(data->utc, sizeof(data->utc)); // header 00061 if (bg96_read_size == -1) { 00062 status = FAILURE; 00063 } 00064 else { 00065 // bg96_read_size = _bg96_instance->read_string(data->utc, sizeof(data->utc)); // time 00066 bg96_read_size = _bg96_at_handler->read_string(data->lat, sizeof(data->lat)); // lat 00067 bg96_read_size = _bg96_at_handler->read_string(data->lon, sizeof(data->lon)); // lon 00068 bg96_read_size = _bg96_at_handler->read_string(data->hdop, sizeof(data->hdop)); // hdop 00069 bg96_read_size = _bg96_at_handler->read_string(data->altitude, sizeof(data->altitude)); // altitude 00070 bg96_read_size = _bg96_at_handler->read_string(data->fix, sizeof(data->fix)); // fix 00071 bg96_read_size = _bg96_at_handler->read_string(data->cog, sizeof(data->cog)); // cog 00072 bg96_read_size = _bg96_at_handler->read_string(data->spkm, sizeof(data->spkm)); // spkm 00073 bg96_read_size = _bg96_at_handler->read_string(data->spkn, sizeof(data->spkn)); // spkn 00074 bg96_read_size = _bg96_at_handler->read_string(data->date, sizeof(data->date)); // date 00075 bg96_read_size = _bg96_at_handler->read_string(data->nsat, sizeof(data->nsat)); // nsat 00076 } 00077 00078 return status; 00079 } 00080 00081 void BG96_GNSS::print_gps_data(gps_data *data) 00082 { 00083 printf("[GPS] Time: \tUTC:%s\r\n", data->utc); 00084 printf("[GPS] Latitude: \tLAT:%s\r\n", data->lat); 00085 printf("[GPS] Longitude: \tLON:%s\r\n", data->lon); 00086 printf("[GPS] Horizontal: \tHDOP:%s\r\n", data->hdop); 00087 printf("[GPS] Altitude: \tALT:%s\r\n", data->altitude); 00088 printf("[GPS] Positioning: \tFIX:%s\r\n", data->fix); 00089 printf("[GPS] Course over time: COG:%s\r\n", data->cog); 00090 printf("[GPS] Speed km: \tSPKM:%s\r\n", data->spkm); 00091 printf("[GPS] Speed knots: \tSPKN:%s\r\n", data->spkn); 00092 printf("[GPS] Date: \t\tDATE:%s\r\n", data->date); 00093 printf("[GPS] Satelites: \tNSAT:%s\r\n", data->nsat); 00094 }
Generated on Wed Jul 13 2022 14:57:15 by
1.7.2