In this exercise, we will use the GNSS interface in the Modem library to enable the GNSS receiver on the nRF9160 SiP and retrieve the GPS coordinates.
Exercise Steps
1. In the GitHub repository for this course, go to the base code for this exercise, found in lesson6/cellfund_less6_exer1 of whichever version directory you are using (v2.2.0-v2.3.0 or v2.4.0).
2. Enable relevant configurations in prj.conf.
2.1 Enable the modem’s network mode for GPS through CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT.
CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT_GPS=y
2.2 Enable the following two configurations two enable printing floating-point numbers to console.
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_FPU=y
The GNSS interface is included when enabling the modem library (CONFIG_NRF_MODEM_LIB).
3. In the Kconfig file define the configurations for the fix interval and fix retry period.
3.1 Define the configuration GNSS_PERIODIC_INTERVAL that specifies at which interval the GNSS receiver will initiate searching for a fix.
config GNSS_PERIODIC_INTERVAL
int "Fix interval for periodic GPS fixes"
range 10 65535
default 120
help
Fix interval (in seconds) for periodic fixes.
3.2 Define the configuration GNSS_PERIODIC_TIMEOUT, which sets the maximum time the GNSS receiver is allowed to run while trying to produce a valid PVT estimate.
config GNSS_PERIODIC_TIMEOUT
int "Fix timeout for periodic GPS fixes"
range 0 65535
default 480
help
Fix timeout (in seconds) for periodic fixes.
If set to zero, GNSS is allowed to run indefinitely until a valid PVT estimate is produced.
We are configuring the GNSS in periodic mode, by setting the fix interval to 2 minutes and the fix retry period to 8 minutes.
4. In main.c, include the header file for the GNSS interface.
6. Define the function print_fix_data(), to log fix data in a readable format.
This function will take a parameter of type struct nrf_modem_gnss_pvt_data_frame and print out the members double latitude, double longitude, float altitude and struct nrf_modem_gnss_datetime datetime
When printing floats, the format specifier (%*.*f) determines how many digits to print. The number before the decimal point sets the number of integers and the number after sets the number of decimals.
7. In the event handler gnss_event_handler(), add the events EVT_PVT, EVT_PERIODIC_WAKEUP and EVT_SLEEP_AFTER_FIX.
7.1 On a PVT event, read PVT data from GNSS, check if it’s a valid fix and print fix data to console.
This will tell us when the GNSS is sleeping and when it wakes up to get the next fix.
case NRF_MODEM_GNSS_EVT_PERIODIC_WAKEUP:
LOG_INF("GNSS has woken up");
break;
case NRF_MODEM_GNSS_EVT_SLEEP_AFTER_FIX:
LOG_INF("GNSS enters sleep after fix");
break;
8. In main(), activate the GNSS stack only.
Using lte_lc_func_mode_set(), set the modem to mode LTE_LC_FUNC_MODE_ACTIVATE_GNSS. This will activate only the GNSS stack.
if (lte_lc_func_mode_set(LTE_LC_FUNC_MODE_ACTIVATE_GNSS) != 0) {
LOG_ERR("Failed to activate GNSS functional mode");
return;
}
In an actual application, you wouldn’t completely deactivate LTE but rather configure PSM or eDRX so that the GNSS and LTE can be used rather often without having to completely activate and deactivate the modes.
We will take a look at how to do this in Exercise 2.
if (nrf_modem_gnss_fix_interval_set(CONFIG_GNSS_PERIODIC_INTERVAL) != 0) {
LOG_ERR("Failed to set GNSS fix interval");
return;
}
if (nrf_modem_gnss_fix_retry_set(CONFIG_GNSS_PERIODIC_TIMEOUT) != 0) {
LOG_ERR("Failed to set GNSS fix retry");
return;
}
11. When everything is configured, start the GNSS receiver using nrf_modem_gnss_start().
LOG_INF("Starting GNSS");
if (nrf_modem_gnss_start() != 0) {
LOG_ERR("Failed to start GNSS");
return;
}
12. Upon the first fix, print the time it took from GNSS start to first fix (Time-To-First-Fix).
12.1 Declare the variable gnss_start_time to store the GNSS start time and a flag first_fix.
12.2 After starting GNSS, log the system uptime using k_uptime_get().
gnss_start_time = k_uptime_get();
12.3 In gnss_event_handler(), check whether valid fixes are the first using the first_fix flag, and if so, print the time from GNSS start to the current system uptime and set the first_fix flag false.
if (!first_fix) {
first_fix = true;
dk_set_led_on(DK_LED1);
LOG_INF("Time to first fix: %2.1lld s", (k_uptime_get() - gnss_start_time)/1000);
}
13. Build the exercise and flash it to your board.
14. To test the application, make sure you are outdoors under an open sky to achieve the best conditions.
14.1 Run the application until the LED indicating the first fix lights up. Your console should look something like this.
We can see that the time to first fix took 34 seconds, which is an expected amount of time.
14.2. Now run the appliation for around 10 minutes. The output should look something like this.
[00:00:36.771,087] <inf> Lesson6_Exercise1: Searching...
[00:00:37.771,179] <inf> Lesson6_Exercise1: Searching...
[00:00:38.820,800] <inf> Lesson6_Exercise1: Searching...
[00:00:38.820,861] <inf> Lesson6_Exercise1: Latitude: 63.421116
[00:00:38.820,892] <inf> Lesson6_Exercise1: Longitude: 10.437321
[00:00:38.820,892] <inf> Lesson6_Exercise1: Altitude: 154.2 m
[00:00:38.820,892] <inf> Lesson6_Exercise1: Time (UTC): 10:28:49.022
[00:00:38.820,922] <inf> Lesson6_Exercise1: Time to first fix: 34 s
[00:00:38.822,265] <inf> Lesson6_Exercise1: GNSS enter sleep after fix
[00:02:04.753,479] <inf> Lesson6_Exercise1: GNSS has woken up
[00:02:04.782,135] <inf> Lesson6_Exercise1: Searching...
[00:02:05.813,415] <inf> Lesson6_Exercise1: Searching...
[00:02:05.813,476] <inf> Lesson6_Exercise1: Latitude: 63.421127
[00:02:05.813,507] <inf> Lesson6_Exercise1: Longitude: 10.437298
[00:02:05.813,507] <inf> Lesson6_Exercise1: Altitude: 158.2 m
[00:02:05.813,537] <inf> Lesson6_Exercise1: Time (UTC): 10:30:16.030
[00:02:05.814,666] <inf> Lesson6_Exercise1: GNSS enter sleep after fix
[00:04:04.758,117] <inf> Lesson6_Exercise1: GNSS has woken up
[00:04:04.786,712] <inf> Lesson6_Exercise1: Searching...
[00:04:05.815,643] <inf> Lesson6_Exercise1: Searching...
[00:04:05.815,704] <inf> Lesson6_Exercise1: Latitude: 63.421158
[00:04:05.815,734] <inf> Lesson6_Exercise1: Longitude: 10.437305
[00:04:05.815,734] <inf> Lesson6_Exercise1: Altitude: 161.0 m
[00:04:05.815,734] <inf> Lesson6_Exercise1: Time (UTC): 10:32:16.030
[00:04:05.817,047] <inf> Lesson6_Exercise1: GNSS enter sleep after fix
From the time stamps, it is clear that after getting the first fix, the GNSS sleeps then wakes up when the GNSS fix interval has passed (120 seconds or 2 minutes) and starts searching for a new fix. Then it sleeps and repeats.
Let’s modify the application to print a bit more debugging information when the GNSS is running.
15. Print the satellites that the GNSS is currently tracking and their signal strength.
The PVT event payload contains the array sv[12] of typestruct nrf_modem_gnss_sv, which describes up to 12 of the space vehicles used for the measurements. It has the following signature
We are interested in the carrier-to-noise density ratio for all the valid satellites the GNSS is currently tracking, which is found in the member cn0. This tells us something about the signal strength from that satellite and it has to be above 30 dB/Hz (cn0 = 300) for the GNSS receiver to consider the satellite “healthy” and use it in calculations.
Insert the following code in the PVT event in gnss_event_handler().
int num_satellites = 0;
for (int i = 0; i < 12 ; i++) {
if (pvt_data.sv[i].signal != 0) {
LOG_INF("sv: %d, cn0: %d", pvt_data.sv[i].sv, pvt_data.sv[i].cn0);
num_satellites++;
}
}
LOG_INF("Number of satellites: %d", num_satellites);
16. Build the exercise and flash it to your board.
You should have a log output that looks something like this.
Printing the satellite information and signal strength is useful for debugging purposes if your application is not getting a fix.
Because the GNSS is solving for four unknown variables, it needs at least 4 satellites to get a valid fix.
Nordic Developer Academy Privacy Policy
1. Introduction
In this Privacy Policy you will find information on Nordic Semiconductor ASA (“Nordic Semiconductor”) processes your personal data when you use the Nordic Developer Academy.
References to “we” and “us” in this document refers to Nordic Semiconductor.
2. Our processing of personal data when you use the Nordic Developer Academy
2.1 Nordic Developer Academy
Nordic Semiconductor processes personal data in order to provide you with the features and functionality of the Nordic Developer Academy. Creating a user account is optional, but required if you want to track you progress and view your completed courses and obtained certificates. If you choose to create a user account, we will process the following categories of personal data:
Email
Name
Password (encrypted)
Course progression (e.g. which course you have completely or partly completed)
Certificate information, which consists of name of completed course and the validity of the certificate
Course results
During your use of the Nordic Developer Academy, you may also be asked if you want to provide feedback. If you choose to respond to any such surveys, we will also process the personal data in your responses in that survey.
The legal basis for this processing is GDPR article 6 (1) b. The processing is necessary for Nordic Semiconductor to provide the Nordic Developer Academy under the Terms of Service.
2.2 Analytics
If you consent to analytics, Nordic Semiconductor will use Google Analytics to obtain statistics about how the Nordic Developer Academy is used. This includes collecting information on for example what pages are viewed, the duration of the visit, the way in which the pages are maneuvered, what links are clicked, technical information about your equipment. The information is used to learn how Nordic Developer Academy is used and how the user experience can be further developed.
2.2 Newsletter
You can consent to receive newsletters from Nordic from within the Nordic Developer Academy. How your personal data is processed when you sign up for our newsletters is described in the Nordic Semiconductor Privacy Policy.
3. Retention period
We will store your personal data for as long you use the Nordic Developer Academy. If our systems register that you have not used your account for 36 months, your account will be deleted.
4. Additional information
Additional information on how we process personal data can be found in the Nordic Semiconductor Privacy Policy and Cookie Policy.
Nordic Developer Academy Terms of Service
1. Introduction
These terms and conditions (“Terms of Use”) apply to the use of the Nordic Developer Academy, provided by Nordic Semiconductor ASA, org. nr. 966 011 726, a public limited liability company registered in Norway (“Nordic Semiconductor”).
Nordic Developer Academy allows the user to take technical courses related to Nordic Semiconductor products, software and services, and obtain a certificate certifying completion of these courses. By completing the registration process for the Nordic Developer Academy, you are agreeing to be bound by these Terms of Use.
These Terms of Use are applicable as long as you have a user account giving you access to Nordic Developer Academy.
2. Access to and use of Nordic Developer Academy
Upon acceptance of these Terms of Use you are granted a non-exclusive right of access to, and use of Nordic Developer Academy, as it is provided to you at any time. Nordic Semiconductor provides Nordic Developer Academy to you free of charge, subject to the provisions of these Terms of Use and the Nordic Developer Academy Privacy Policy.
To access select features of Nordic Developer Academy, you need to create a user account. You are solely responsible for the security associated with your user account, including always keeping your login details safe.
You will able to receive an electronic certificate from Nordic Developer Academy upon completion of courses. By issuing you such a certificate, Nordic Semiconductor certifies that you have completed the applicable course, but does not provide any further warrants or endorsements for any particular skills or professional qualifications.
Nordic Semiconductor will continuously develop Nordic Developer Academy with new features and functionality, but reserves the right to remove or alter any existing functions without notice.
3. Acceptable use
You undertake that you will use Nordic Developer Academy in accordance with applicable law and regulations, and in accordance with these Terms of Use. You must not modify, adapt, or hack Nordic Developer Academy or modify another website so as to falsely imply that it is associated with Nordic Developer Academy, Nordic Semiconductor, or any other Nordic Semiconductor product, software or service.
You agree not to reproduce, duplicate, copy, sell, resell or in any other way exploit any portion of Nordic Developer Academy, use of Nordic Developer Academy, or access to Nordic Developer Academy without the express written permission by Nordic Semiconductor. You must not upload, post, host, or transmit unsolicited email, SMS, or \”spam\” messages.
You are responsible for ensuring that the information you post and the content you share does not;
contain false, misleading or otherwise erroneous information
infringe someone else’s copyrights or other intellectual property rights
contain sensitive personal data or
contain information that might be received as offensive or insulting.
Such information may be removed without prior notice.
Nordic Semiconductor reserves the right to at any time determine whether a use of Nordic Developer Academy is in violation of its requirements for acceptable use.
Violation of the at any time applicable requirements for acceptable use may result in termination of your account. We will take reasonable steps to notify you and state the reason for termination in such cases.
4. Routines for planned maintenance
Certain types of maintenance may imply a stop or reduction in availability of Nordic Developer Academy. Nordic Semiconductor does not warrant any level of service availability but will provide its best effort to limit the impact of any planned maintenance on the availability of Nordic Developer Academy.
5. Intellectual property rights
Nordic Semiconductor retains all rights to all elements of Nordic Developer Academy. This includes, but is not limited to, the concept, design, trademarks, know-how, trade secrets, copyrights and all other intellectual property rights.
Nordic Semiconductor receives all rights to all content uploaded or created in Nordic Developer Academy. You do not receive any license or usage rights to Nordic Developer Academy beyond what is explicitly stated in this Agreement.
6. Liability and damages
Nothing within these Terms of Use is intended to limit your statutory data privacy rights as a data subject, as described in the Nordic Developer Academy Privacy Policy. You acknowledge that errors might occur from time to time and waive any right to claim for compensation as a result of errors in Nordic Developer Academy. When an error occurs, you shall notify Nordic Semiconductor of the error and provide a description of the error situation.
You agree to indemnify Nordic Semiconductor for any loss, including indirect loss, arising out of or in connection with your use of Nordic Developer Academy or violations of these Terms of Use. Nordic Semiconductor shall not be held liable for, and does not warrant that (i) Nordic Developer Academy will meet your specific requirements, (ii) Nordic Developer Academy will be uninterrupted, timely, secure, or error-free, (iii) the results that may be obtained from the use of Nordic Developer Academy will be accurate or reliable, (iv) the quality of any products, services, information, or other material purchased or obtained by you through Nordic Developer Academy will meet your expectations, or that (v) any errors in Nordic Developer Academy will be corrected.
You accept that this is a service provided to you without any payment and hence you accept that Nordic Semiconductor will not be held responsible, or liable, for any breaches of these Terms of Use or any loss connected to your use of Nordic Developer Academy. Unless otherwise follows from mandatory law, Nordic Semiconductor will not accept any such responsibility or liability.
7. Change of terms
Nordic Semiconductor may update and change the Terms of Use from time to time. Nordic Semiconductor will seek to notify you about significant changes before such changes come into force and give you a possibility to evaluate the effects of proposed changes. Continued use of Nordic Developer Academy after any such changes shall constitute your acceptance of such changes. You can review the current version of the Terms of Use at any time at https://academy.nordicsemi.com/terms-of-service/
8. Transfer of rights
Nordic Semiconductor is entitled to transfer its rights and obligation pursuant to these Terms of Use to a third party as part of a merger or acquisition process, or as a result of other organizational changes.
9. Third Party Services
To the extent Nordic Developer Academy facilitates access to services provided by a third party, you agree to comply with the terms governing such third party services. Nordic Semiconductor shall not be held liable for any errors, omissions, inaccuracies, etc. related to such third party services.
10. Dispute resolution
The Terms of Use and any other legally binding agreement between yourself and Nordic Semiconductor shall be subject to Norwegian law and Norwegian courts’ exclusive jurisdiction.