Pico GPS Teseo I2C
Loading...
Searching...
No Matches
teseo_nmea_with_data_processing.cpp
Go to the documentation of this file.
1#include <string>
2#include <ranges>
3#include <span>
4#include <chrono>
5
6// for debug messages
7#include <stdio.h>
8#include "pico/stdlib.h"
9
10import teseo;
11import nmea;
12import port_pico_reset;
14
16// for the container that will hold multy-line replies,
17// you can use an array, vector, C array
18// based on what your architecture prefers or requires.
19// vector size is a suggestion. STL will allocate at least NMEA_MAX_REPLIES
20//std::vector<std::string> replies(NMEA_MAX_REPLIES);
21std::array<std::string, NMEA_MAX_REPLIES> replies; // for multi-reply commands
22uint count; // intentionally uninitialised
23std::string reply; // for single reply commands
24bool valid; // intentionally uninitialised
25
26// this array will receive parsed objects.
27// this example will perform aggregations and calculations over the containers.
28std::array<nmea::gsv, NMEA_MAX_REPLIES> gsv_set = {};
30
31void print_t(const nmea::time_t& t) {
32 printf("%02i:%02i:%02i.%03i",
33 (int)(t.hours().count()), (int)(t.minutes().count()),
34 (int)(t.seconds().count()), (int)(t.subseconds().count()));
35}
36
37void print_d(const std::chrono::year_month_day& t) {
38 printf("%4i-%02i-%02i",
39 t.year(), t.month(),
40 t.day());
41}
42
43void print_talker(const nmea::talker_id& talker_id) {
44 switch (talker_id) {
46 printf("gps");
47 break;
49 printf("glonass");
50 break;
52 printf("galileo");
53 break;
54 default:
55 printf("other");
56 break;
57 }
58}
59
61 bool valid; // intentionally uninitialised
62 valid = gps.ask_rmc(reply);
63 if (!valid) { return false; }
65 return valid;
66}
67
68size_t retrieve_gsv() {
69 unsigned int count; // intentionally uninitialised
70 valid = gps.ask_gsv(replies, count);
71 if (!valid) { return 0; }
72 size_t index = 0;
73 for(const auto& r : std::ranges::subrange(replies.begin(), replies.begin() + count)) {
75 if (!valid) {
76 break;
77 }
78 index++;
79 }
80
81 // clean out unused tail of the container
82 std::for_each( gsv_set.begin() + count, gsv_set.end(), [](auto& o){ o = {}; });
83
84 return index;
85}
86
88 size_t i = std::count_if(gsv_set.begin(), gsv_set.end(),
89 [source](const auto& o){ return (o.source == source); });
90 return i;
91}
92
94 gps.writer().set([](const std::string& s) -> void { write(s); });
95 gps.reader().set([](std::string& s) -> void { read(s); });
96 gps.resetter().set([]() -> void { port_pico::reset(); });
97}
98
99int main() {
100 initialize();
101 setCallbacks();
102
103 /*
104 when the teseo is preset for i2c according to AN5203,
105 init is not required, and you can cut 4s 10ms from the startup sequence
106 https://www.st.com/resource/en/application_note/an5203-teseoliv3f--i2c-positioning-sensor--stmicroelectronics.pdf
107 */
108 gps.initialize();
109
110 while (true) {
111 size_t count; // intentionally uninitialised
112 printf("+-- start --+\r\n");
113
114 // gather data
115 retrieve_rmc();
116 retrieve_gsv();
117
118 //print rmc info
119 printf("time: ");
120 print_t(rmc.t);
121 printf("\r\nstatus: %s\r\nlat(n) lon(e): %f %f\r\ndate: ",
122 rmc.valid? "active" : "void", rmc.lat, rmc.lon);
123 print_d(rmc.d);
124 printf("\r\n");
125
126
127 // aggregate and print data for GPS and GLONASS
130 printf(" count: %i\r\n", count);
133 printf(" count: %i\r\n", count);
134
135 // print satellites
136 for(auto o : gsv_set | std::views::filter([](const auto& s){ return s.source != nmea::talker_id::notset;})) {
137 print_talker(o.source);
138 printf(" sat id: ");
139 for (const auto& s : o.sats | std::views::filter([](const nmea::gsv_sat& s){ return s.prn != 0;})) {
140 printf(" %i", s.prn);
141 }
142 printf(". \r\n");
143 }
144
145 // print all satellites accross constellations with their attributes
146 for(auto o : gsv_set | std::views::filter([](const auto& s){ return s.source != nmea::talker_id::notset;})) {
147 for (const auto& s : o.sats | std::views::filter([](const nmea::gsv_sat& s){ return s.prn != 0;})) {
148 printf("sat id: %i, elev: %i, azim: %i, snr: %i, source: ", s.prn, s.elev, s.azim, s.snr);
149 print_talker(o.source);
150 printf(".\r\n");
151 }
152 }
153
154 printf("+-- end --+\r\n\r\n");
155 sleep_ms(1000);
156 }
157}
static bool from_data(const std::string &data, gsv &gsv)
Definition nmea.cpp:253
static bool from_data(const std::string &data, rmc &rmc)
Definition nmea.cpp:314
Driver class for ST Teseo IC.
void initialize()
void read(::std::string &s)
void write(const ::std::string &s)
std::chrono::hh_mm_ss< std::chrono::duration< long long, std::ratio< 1, 1000 > > > time_t
void reset()
Definition reset.cpp:17
void print_d(const std::chrono::year_month_day &t)
void print_t(const nmea::time_t &t)
void print_talker(const nmea::talker_id &talker_id)
size_t count_constellations(const nmea::talker_id source)
std::array< nmea::gsv, NMEA_MAX_REPLIES > gsv_set
std::array< std::string, NMEA_MAX_REPLIES > replies