source: flair-src/branches/mavlink/lib/FlairSensorActuator/src/Gps.cpp@ 89

Last change on this file since 89 was 42, checked in by Sanahuja Guillaume, 8 years ago

m

File size: 7.8 KB
Line 
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
35using std::string;
36using namespace Geodesie;
37using namespace flair::core;
38using namespace flair::gui;
39
40namespace flair {
41namespace sensor {
42
43Gps::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
106Gps::~Gps() {
107 nmea_parser_destroy(&parser);
108 delete main_tab;
109}
110
111void 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
137DataPlot1D *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
146DataPlot1D *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
155DataPlot1D *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
164DataPlot1D *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
173DataPlot1D *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
182Layout *Gps::GetLayout(void) const { return sensor_tab; }
183
184Tab *Gps::GetPlotTab(void) const { return plot_tab; }
185
186TabWidget *Gps::GetTab(void) const { return tab; }
187
188uint16_t Gps::NbSat(void) const {
189 output->GetMutex();
190 uint16_t result = nb_sat;
191 output->ReleaseMutex();
192 return result;
193}
194
195Gps::FixQuality_t Gps::FixQuality(void) const {
196 output->GetMutex();
197 FixQuality_t result = fix;
198 output->ReleaseMutex();
199 return result;
200}
201
202void Gps::SetRef(void) { take_ref = true; }
203
204void 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
212void 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: %s\n",frame);
219 }
220
221 result = nmea_parse_GPGGA(frame, frame_size, &pack);
222
223 if (result == 1) {
224 // Printf("%s\n",frame);
225 // Printf("nb:%i fix:%i lat:%f long:%f alt:%f vel:%f
226 // angle:%f\n",pack.satinuse,pack.sig,info.lat,info.lon,info.elv,info.speed*1000./3600.,info.direction);
227 // Printf("lat:%f long:%f\n",pos.lat,pos.lon);
228 output->GetMutex(); // on utilise le mutex de l'output pour fix et nb_sat
229 if ((int)fix != pack.sig) {
230 fix = (FixQuality_t)pack.sig;
231 fix_label->SetText("fix: %i", fix);
232 }
233 if (nb_sat != pack.satinuse) {
234 nb_sat = pack.satinuse;
235 nb_sat_label->SetText("nb_sat: %i", nb_sat);
236 }
237 output->ReleaseMutex();
238
239 nmea_info2pos(&info, &pos);
240 position->SetCoordinates(Euler::ToDegree(pos.lat), Euler::ToDegree(pos.lon),
241 info.elv);
242
243 if ((info.sig == 2 && alt_ref == 0) || button_ref->Clicked() == true ||
244 take_ref == true) {
245 Printf("prise pos ref\n");
246 lat_ref = pos.lat;
247 long_ref = pos.lon;
248 alt_ref = info.elv;
249 take_ref = false;
250 }
251 // if(alt_ref!=0)
252 {
253 double x, y, z;
254 double e, n, u;
255 Geographique_2_ECEF(pos.lon, pos.lat, info.elv, x, y, z);
256 ECEF_2_ENU(x, y, z, e, n, u, long_ref, lat_ref, alt_ref);
257 // Printf("lon:%f lat:%f elv:%f\n",pos.lon,pos.lat,info.elv);
258
259 // on prend une fois pour toute le mutex et on fait des accès directs
260 output->GetMutex();
261 output->SetValueNoMutex(0, 0, e);
262 output->SetValueNoMutex(1, 0, n);
263 output->SetValueNoMutex(2, 0, u);
264
265 int index = 3;
266 if ((NMEAFlags & VTG) != 0) {
267 output->SetValueNoMutex(index, 0,
268 info.speed * 1000. / 3600. *
269 sin(Euler::ToRadian(info.direction)));
270 output->SetValueNoMutex(index + 1, 0,
271 info.speed * 1000. / 3600. *
272 cos(Euler::ToRadian(info.direction)));
273 index += 2;
274 }
275 if ((NMEAFlags & GST) != 0) {
276 // Thread::Printf("dev_lon:%f dev_lat:%f
277 // dev_elv:%f\n",info.dev_lat,info.dev_lon,info.dev_elv);
278 output->SetValueNoMutex(index, 0, info.dev_lat);
279 output->SetValueNoMutex(index + 1, 0, info.dev_lon);
280 output->SetValueNoMutex(index + 2, 0, info.dev_elv);
281 index += 3;
282 }
283 output->ReleaseMutex();
284
285 output->SetDataTime(GetTime());
286 ProcessUpdate(output);
287 }
288 }
289}
290
291} // end namespace sensor
292} // end namespace framewor
Note: See TracBrowser for help on using the repository browser.