Pico GPS Teseo
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; }
64 if (auto result = nmea::rmc::from_data(reply)) { // TODO: when GCC 15.1 released for ARM, use auto [result]
65 valid = true;
66 rmc = result.result;
67 }
68 return valid;
69}
70
71size_t retrieve_gsv() {
72 unsigned int count; // intentionally uninitialised
73 valid = gps.ask_gsv(replies, count);
74 if (!valid) { return 0; }
75 size_t index = 0;
76 for(const auto& r : std::ranges::subrange(replies.begin(), replies.begin() + count)) {
77 if (auto result = nmea::gsv::from_data(r)) {
78 gsv_set[index] = result.result;
79 } else {
80 break;
81 }
82 index++;
83 }
84
85 // clean out unused tail of the container
86 std::for_each( gsv_set.begin() + count, gsv_set.end(), [](auto& o){ o = {}; });
87
88 return index;
89}
90
92 size_t i = std::count_if(gsv_set.begin(), gsv_set.end(),
93 [source](const auto& o){ return (o.source == source); });
94 return i;
95}
96
98 gps.writer().set([](const std::string& s) -> void { write(s); });
99 gps.reader().set([](std::string& s) -> void { read(s); });
100 gps.resetter().set([]() -> void { port_pico::reset(); });
101}
102
103int main() {
104 initialize();
105 setCallbacks();
106
107 /*
108 when the teseo is preset for i2c according to AN5203,
109 init is not required, and you can cut 4s 10ms from the startup sequence
110 https://www.st.com/resource/en/application_note/an5203-teseoliv3f--i2c-positioning-sensor--stmicroelectronics.pdf
111 */
112 gps.initialize();
113
114 while (true) {
115 size_t count; // intentionally uninitialised
116 printf("+-- start --+\r\n");
117
118 // gather data
119 retrieve_rmc();
120 retrieve_gsv();
121
122 //print rmc info
123 printf("time: ");
124 print_t(rmc.t);
125 printf("\r\nstatus: %s\r\nlat(n) lon(e): %f %f\r\ndate: ",
126 rmc.valid? "active" : "void", rmc.lat, rmc.lon);
127 print_d(rmc.d);
128 printf("\r\n");
129
130
131 // aggregate and print data for GPS and GLONASS
134 printf(" count: %i\r\n", count);
137 printf(" count: %i\r\n", count);
138
139 // print satellites
140 for(auto o : gsv_set | std::views::filter([](const auto& s){ return s.source != nmea::talker_id::notset;})) {
141 print_talker(o.source);
142 printf(" sat id: ");
143 for (const auto& s : o.sats | std::views::filter([](const nmea::gsv_sat& s){ return s.prn != 0;})) {
144 printf(" %i", s.prn);
145 }
146 printf(". \r\n");
147 }
148
149 // print all satellites accross constellations with their attributes
150 for(auto o : gsv_set | std::views::filter([](const auto& s){ return s.source != nmea::talker_id::notset;})) {
151 for (const auto& s : o.sats | std::views::filter([](const nmea::gsv_sat& s){ return s.prn != 0;})) {
152 printf("sat id: %i, elev: %i, azim: %i, snr: %i, source: ", s.prn, s.elev, s.azim, s.snr);
153 print_talker(o.source);
154 printf(".\r\n");
155 }
156 }
157
158 printf("+-- end --+\r\n\r\n");
159 sleep_ms(1000);
160 }
161}
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
static gsv_result from_data(const std::string &data)
Definition nmea.cpp:260
static rmc_result from_data(const std::string &data)
Definition nmea.cpp:323
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