1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2013/08/23
|
---|
6 | // filename: Gps.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: objet integrant le recepteur gps mb800
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "Gps.h"
|
---|
19 | #include "geodesie.h"
|
---|
20 | #include <Euler.h>
|
---|
21 | #include <cvmatrix.h>
|
---|
22 | #include <DataPlot1D.h>
|
---|
23 | #include <Tab.h>
|
---|
24 | #include <TabWidget.h>
|
---|
25 | #include <GridLayout.h>
|
---|
26 | #include <GroupBox.h>
|
---|
27 | #include <PushButton.h>
|
---|
28 | #include <FrameworkManager.h>
|
---|
29 | #include <Map.h>
|
---|
30 | #include <GeoCoordinate.h>
|
---|
31 | #include <Vector3D.h>
|
---|
32 | #include <Label.h>
|
---|
33 | #include <string.h>
|
---|
34 |
|
---|
35 | using std::string;
|
---|
36 | using namespace Geodesie;
|
---|
37 | using namespace flair::core;
|
---|
38 | using namespace flair::gui;
|
---|
39 |
|
---|
40 | namespace flair {
|
---|
41 | namespace sensor {
|
---|
42 |
|
---|
43 | Gps::Gps(const FrameworkManager *parent, string name, NMEAFlags_t NMEAFlags)
|
---|
44 | : IODevice(parent, name) {
|
---|
45 | this->NMEAFlags = NMEAFlags;
|
---|
46 |
|
---|
47 | nmea_zero_INFO(&info);
|
---|
48 | nmea_parser_init(&parser);
|
---|
49 | alt_ref = 0;
|
---|
50 |
|
---|
51 | if ((NMEAFlags & GGA) == 0) {
|
---|
52 | Err("Enable at least GGA sentence\n");
|
---|
53 | }
|
---|
54 |
|
---|
55 | int index = 0;
|
---|
56 | if ((NMEAFlags & GGA) != 0) {
|
---|
57 | index += 3;
|
---|
58 | }
|
---|
59 | if ((NMEAFlags & VTG) != 0) {
|
---|
60 | index += 2;
|
---|
61 | }
|
---|
62 | if ((NMEAFlags & GST) != 0) {
|
---|
63 | index += 3;
|
---|
64 | }
|
---|
65 |
|
---|
66 | cvmatrix_descriptor *desc = new cvmatrix_descriptor(index, 1);
|
---|
67 | index = 0;
|
---|
68 | if ((NMEAFlags & GGA) != 0) {
|
---|
69 | desc->SetElementName(0, 0, "e");
|
---|
70 | desc->SetElementName(1, 0, "n");
|
---|
71 | desc->SetElementName(2, 0, "u");
|
---|
72 | index += 3;
|
---|
73 | }
|
---|
74 | if ((NMEAFlags & VTG) != 0) {
|
---|
75 | desc->SetElementName(index, 0, "ve");
|
---|
76 | desc->SetElementName(index + 1, 0, "vn");
|
---|
77 | index += 2;
|
---|
78 | }
|
---|
79 | if ((NMEAFlags & GST) != 0) {
|
---|
80 | desc->SetElementName(index, 0, "dev_lat");
|
---|
81 | desc->SetElementName(index + 1, 0, "dev_lon");
|
---|
82 | desc->SetElementName(index + 2, 0, "dev_elv");
|
---|
83 | index += 3;
|
---|
84 | }
|
---|
85 | output = new cvmatrix((IODevice *)this, desc, floatType);
|
---|
86 | AddDataToLog(output);
|
---|
87 |
|
---|
88 | // station sol
|
---|
89 | main_tab = new Tab(parent->GetTabWidget(), name);
|
---|
90 | tab = new TabWidget(main_tab->NewRow(), name);
|
---|
91 | sensor_tab = new Tab(tab, "Reglages");
|
---|
92 | GroupBox *reglages_groupbox = new GroupBox(sensor_tab->NewRow(), name);
|
---|
93 | button_ref = new PushButton(reglages_groupbox->NewRow(), "set ref");
|
---|
94 | nb_sat_label = new Label(reglages_groupbox->NewRow(), "nb_sat");
|
---|
95 | fix_label = new Label(reglages_groupbox->LastRowLastCol(), "fix");
|
---|
96 |
|
---|
97 | position = new GeoCoordinate((IODevice *)this, "position", 0, 0, 0);
|
---|
98 |
|
---|
99 | fix = FixQuality_t::Invalid;
|
---|
100 | nb_sat = 0;
|
---|
101 | take_ref = false;
|
---|
102 | nb_sat_label->SetText("nb_sat: %i", nb_sat);
|
---|
103 | fix_label->SetText("fix: %i", fix);
|
---|
104 | }
|
---|
105 |
|
---|
106 | Gps::~Gps() {
|
---|
107 | nmea_parser_destroy(&parser);
|
---|
108 | delete main_tab;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void Gps::UseDefaultPlot(void) {
|
---|
112 | int index = 0;
|
---|
113 | plot_tab = new Tab(tab, "Mesures");
|
---|
114 |
|
---|
115 | if ((NMEAFlags & GGA) != 0) {
|
---|
116 | e_plot = new DataPlot1D(plot_tab->NewRow(), "e", -10, 10);
|
---|
117 | e_plot->AddCurve(output->Element(index));
|
---|
118 | n_plot = new DataPlot1D(plot_tab->LastRowLastCol(), "n", -10, 10);
|
---|
119 | n_plot->AddCurve(output->Element(index + 1));
|
---|
120 | u_plot = new DataPlot1D(plot_tab->LastRowLastCol(), "u", -10, 10);
|
---|
121 | u_plot->AddCurve(output->Element(index + 2));
|
---|
122 | index += 3;
|
---|
123 | }
|
---|
124 | if ((NMEAFlags & VTG) != 0) {
|
---|
125 | ve_plot = new DataPlot1D(plot_tab->NewRow(), "ve", -10, 10);
|
---|
126 | ve_plot->AddCurve(output->Element(index));
|
---|
127 | vn_plot = new DataPlot1D(plot_tab->LastRowLastCol(), "vn", -10, 10);
|
---|
128 | vn_plot->AddCurve(output->Element(index + 1));
|
---|
129 | index += 2;
|
---|
130 | }
|
---|
131 |
|
---|
132 | Tab *map_tab = new Tab(tab, "carte");
|
---|
133 | map = new Map(map_tab->NewRow(), "map");
|
---|
134 | map->AddPoint(position, "drone");
|
---|
135 | }
|
---|
136 |
|
---|
137 | DataPlot1D *Gps::EPlot(void) const {
|
---|
138 | if ((NMEAFlags & GGA) != 0) {
|
---|
139 | return e_plot;
|
---|
140 | } else {
|
---|
141 | Err("GGA sentence not requested\n");
|
---|
142 | return NULL;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | DataPlot1D *Gps::NPlot(void) const {
|
---|
147 | if ((NMEAFlags & GGA) != 0) {
|
---|
148 | return n_plot;
|
---|
149 | } else {
|
---|
150 | Err("GGA sentence not requested\n");
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | DataPlot1D *Gps::UPlot(void) const {
|
---|
156 | if ((NMEAFlags & GGA) != 0) {
|
---|
157 | return u_plot;
|
---|
158 | } else {
|
---|
159 | Err("GGA sentence not requested\n");
|
---|
160 | return NULL;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | DataPlot1D *Gps::VEPlot(void) const {
|
---|
165 | if ((NMEAFlags & VTG) != 0) {
|
---|
166 | return ve_plot;
|
---|
167 | } else {
|
---|
168 | Err("GGA sentence not requested\n");
|
---|
169 | return NULL;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | DataPlot1D *Gps::VNPlot(void) const {
|
---|
174 | if ((NMEAFlags & VTG) != 0) {
|
---|
175 | return vn_plot;
|
---|
176 | } else {
|
---|
177 | Err("GGA sentence not requested\n");
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | Layout *Gps::GetLayout(void) const { return sensor_tab; }
|
---|
183 |
|
---|
184 | Tab *Gps::GetPlotTab(void) const { return plot_tab; }
|
---|
185 |
|
---|
186 | TabWidget *Gps::GetTab(void) const { return tab; }
|
---|
187 |
|
---|
188 | uint16_t Gps::NbSat(void) const {
|
---|
189 | output->GetMutex();
|
---|
190 | uint16_t result = nb_sat;
|
---|
191 | output->ReleaseMutex();
|
---|
192 | return result;
|
---|
193 | }
|
---|
194 |
|
---|
195 | Gps::FixQuality_t Gps::FixQuality(void) const {
|
---|
196 | output->GetMutex();
|
---|
197 | FixQuality_t result = fix;
|
---|
198 | output->ReleaseMutex();
|
---|
199 | return result;
|
---|
200 | }
|
---|
201 |
|
---|
202 | void Gps::SetRef(void) { take_ref = true; }
|
---|
203 |
|
---|
204 | void Gps::GetENUPosition(Vector3D *point) {
|
---|
205 | output->GetMutex();
|
---|
206 | point->x = output->ValueNoMutex(0, 0);
|
---|
207 | point->y = output->ValueNoMutex(1, 0);
|
---|
208 | point->z = output->ValueNoMutex(2, 0);
|
---|
209 | output->ReleaseMutex();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void Gps::parseFrame(const char *frame, int frame_size) {
|
---|
213 |
|
---|
214 | int result;
|
---|
215 |
|
---|
216 | result = nmea_parse(&parser, frame, frame_size, &info);
|
---|
217 | if (result != 1) {
|
---|
218 | Warn("unrecognized nmea sentence\n");
|
---|
219 | Warn("%s\n", frame);
|
---|
220 | }
|
---|
221 |
|
---|
222 | result = nmea_parse_GPGGA(frame, frame_size, &pack);
|
---|
223 |
|
---|
224 | if (result == 1) {
|
---|
225 | // Printf("%s\n",frame);
|
---|
226 | // Printf("nb:%i fix:%i lat:%f long:%f alt:%f vel:%f
|
---|
227 | // angle:%f\n",pack.satinuse,pack.sig,info.lat,info.lon,info.elv,info.speed*1000./3600.,info.direction);
|
---|
228 | // Printf("lat:%f long:%f\n",pos.lat,pos.lon);
|
---|
229 | output->GetMutex(); // on utilise le mutex de l'output pour fix et nb_sat
|
---|
230 | if ((int)fix != pack.sig) {
|
---|
231 | fix = (FixQuality_t)pack.sig;
|
---|
232 | fix_label->SetText("fix: %i", fix);
|
---|
233 | }
|
---|
234 | if (nb_sat != pack.satinuse) {
|
---|
235 | nb_sat = pack.satinuse;
|
---|
236 | nb_sat_label->SetText("nb_sat: %i", nb_sat);
|
---|
237 | }
|
---|
238 | output->ReleaseMutex();
|
---|
239 |
|
---|
240 | nmea_info2pos(&info, &pos);
|
---|
241 | position->SetCoordinates(Euler::ToDegree(pos.lat), Euler::ToDegree(pos.lon),
|
---|
242 | info.elv);
|
---|
243 |
|
---|
244 | if ((info.sig == 2 && alt_ref == 0) || button_ref->Clicked() == true ||
|
---|
245 | take_ref == true) {
|
---|
246 | Printf("prise pos ref\n");
|
---|
247 | lat_ref = pos.lat;
|
---|
248 | long_ref = pos.lon;
|
---|
249 | alt_ref = info.elv;
|
---|
250 | take_ref = false;
|
---|
251 | }
|
---|
252 | // if(alt_ref!=0)
|
---|
253 | {
|
---|
254 | double x, y, z;
|
---|
255 | double e, n, u;
|
---|
256 | Geographique_2_ECEF(pos.lon, pos.lat, info.elv, x, y, z);
|
---|
257 | ECEF_2_ENU(x, y, z, e, n, u, long_ref, lat_ref, alt_ref);
|
---|
258 | // Printf("lon:%f lat:%f elv:%f\n",pos.lon,pos.lat,info.elv);
|
---|
259 |
|
---|
260 | // on prend une fois pour toute le mutex et on fait des accès directs
|
---|
261 | output->GetMutex();
|
---|
262 | output->SetValueNoMutex(0, 0, e);
|
---|
263 | output->SetValueNoMutex(1, 0, n);
|
---|
264 | output->SetValueNoMutex(2, 0, u);
|
---|
265 |
|
---|
266 | int index = 3;
|
---|
267 | if ((NMEAFlags & VTG) != 0) {
|
---|
268 | output->SetValueNoMutex(index, 0,
|
---|
269 | info.speed * 1000. / 3600. *
|
---|
270 | sin(Euler::ToRadian(info.direction)));
|
---|
271 | output->SetValueNoMutex(index + 1, 0,
|
---|
272 | info.speed * 1000. / 3600. *
|
---|
273 | cos(Euler::ToRadian(info.direction)));
|
---|
274 | index += 2;
|
---|
275 | }
|
---|
276 | if ((NMEAFlags & GST) != 0) {
|
---|
277 | // Thread::Printf("dev_lon:%f dev_lat:%f
|
---|
278 | // dev_elv:%f\n",info.dev_lat,info.dev_lon,info.dev_elv);
|
---|
279 | output->SetValueNoMutex(index, 0, info.dev_lat);
|
---|
280 | output->SetValueNoMutex(index + 1, 0, info.dev_lon);
|
---|
281 | output->SetValueNoMutex(index + 2, 0, info.dev_elv);
|
---|
282 | index += 3;
|
---|
283 | }
|
---|
284 | output->ReleaseMutex();
|
---|
285 |
|
---|
286 | output->SetDataTime(GetTime());
|
---|
287 | ProcessUpdate(output);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | } // end namespace sensor
|
---|
293 | } // end namespace framewor
|
---|