source: flair-src/trunk/lib/FlairSensorActuator/src/SimuGps.cpp@ 206

Last change on this file since 206 was 206, checked in by Sanahuja Guillaume, 4 years ago

m

File size: 4.6 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: 2014/05/26
6// filename: SimuGps.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class for a simulation GPS
14//
15//
16/*********************************************************************/
17
18#include "SimuGps.h"
19#include <FrameworkManager.h>
20#include <GeoCoordinate.h>
21#include <GpsData.h>
22#include <SharedMem.h>
23#include <SpinBox.h>
24#include <DoubleSpinBox.h>
25#include <GroupBox.h>
26#include <Euler.h>
27#include <cvmatrix.h>
28#include <sstream>
29#include "geodesie.h"
30
31using std::string;
32using std::ostringstream;
33using namespace flair::core;
34using namespace flair::gui;
35using namespace Geodesie;
36
37namespace flair {
38namespace sensor {
39
40SimuGps::SimuGps(string name,
41 NmeaGps::NMEAFlags_t NMEAFlags, uint32_t modelId,uint32_t deviceId,uint8_t priority)
42 : NmeaGps(name, NMEAFlags),Thread(getFrameworkManager(), name, priority) {
43
44 dataRate = new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
45 latitudeRef = new DoubleSpinBox(GetGroupBox()->NewRow(), "latitude ref", " deg", -90, 90, 1, 6,49.402313);
46 longitudeRef = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "longitude ref", " deg", -180, 180, 1, 6,2.795463);
47 altitudeRef= new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "altitude ref", " m", 0, 6000, 100, 1,0);
48 fixQuality = new SpinBox(GetGroupBox()->NewRow(), "fix quality", 1, 8, 1, 1);
49 numberOfSatellites = new SpinBox(GetGroupBox()->NewRow(), "number of satellites", 1, 15, 1, 5);
50 magneticDeclination= new DoubleSpinBox(GetGroupBox()->NewRow(), "magnetic declination", " deg", -180, 180, .1, 2,0);
51
52 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
53 sizeof(gps_states_t));
54
55 SetIsReady(true);
56}
57
58
59SimuGps::SimuGps(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
60 : NmeaGps(parent, name), Thread(parent, name, 0) {
61 dataRate = NULL;
62
63 shmem = new SharedMem((Thread *)this,ShMemName(modelId, deviceId),
64 sizeof(gps_states_t));
65
66 SetIsReady(true);
67}
68
69SimuGps::~SimuGps() {
70 SafeStop();
71 Join();
72}
73
74string SimuGps::ShMemName(uint32_t modelId,uint32_t deviceId) {
75 ostringstream dev_name;
76 dev_name << "simu" << modelId << "_gps_" << deviceId;
77 return dev_name.str().c_str();
78}
79
80void SimuGps::UpdateFrom(const io_data *data) {
81 if (data != NULL) {
82 cvmatrix *input = (cvmatrix *)data;
83 gps_states_t state;
84
85 input->GetMutex();
86 //simulator is ned, convert it to enu
87 //TODO: put simulator in enu?
88 state.e = input->ValueNoMutex(5, 0);//y simulator
89 state.n = input->ValueNoMutex(4, 0);//x simulator
90 state.u = -input->ValueNoMutex(6, 0);//z simulator
91 state.ve = input->ValueNoMutex(11, 0);//vy simulator
92 state.vn = input->ValueNoMutex(10, 0);//vx simulator
93 input->ReleaseMutex();
94
95 shmem->Write((char *)&state, sizeof(gps_states_t));
96 }
97}
98
99void SimuGps::Run(void) {
100 gps_states_t state;
101 char buf[512];
102 nmeaGPGGA gga;
103 nmeaGPVTG vtg;
104 nmeaPOS pos;
105 nmeaINFO info;
106
107 vtg.dir_t='T';
108 vtg.dec_m='M';
109 vtg.spk_k='K';
110 vtg.spn_n='N';
111
112 if (dataRate == NULL) {
113 Thread::Err("not applicable for simulation part.\n");
114 return;
115 }
116
117 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
118
119 WarnUponSwitches(true);
120
121
122 while (!ToBeStopped()) {
123 WaitPeriod();
124
125 if (dataRate->ValueChanged() == true) {
126 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
127 }
128
129 shmem->Read((char *)&state, sizeof(gps_states_t));
130
131 double x, y, z;
132 ENU_2_ECEF(state.e, state.n, state.u, x,y,z, Euler::ToRadian(longitudeRef->Value()), Euler::ToRadian(latitudeRef->Value()), altitudeRef->Value());
133 ECEF_2_Geographique( x, y, z,pos.lon, pos.lat, gga.elv);
134 nmea_pos2info(&pos,&info);
135
136 if(pos.lat>0) {
137 gga.ns='N';
138 gga.lat=info.lat;
139 } else {
140 gga.ns='S';
141 gga.lat=-info.lat;
142 }
143 if(pos.lon>0) {
144 gga.ew='E';
145 gga.lon=info.lon;
146 } else {
147 gga.ew='W';
148 gga.lon=-info.lon;
149 }
150
151 gga.sig=fixQuality->Value();
152 gga.satinuse=numberOfSatellites->Value();
153
154 nmea_gen_GPGGA(buf,sizeof(buf),&gga);
155 Printf("1\n");
156 parseFrame(buf,sizeof(buf));
157
158 vtg.dir=90-Euler::ToDegree(atan2f(state.vn,state.ve));
159 vtg.spk=sqrtf(state.ve*state.ve+state.vn*state.vn)*3600./1000.;
160 nmea_gen_GPVTG(buf,sizeof(buf),&vtg);
161 Printf("2\n");
162 parseFrame(buf,sizeof(buf));
163 }
164
165 WarnUponSwitches(false);
166}
167
168} // end namespace sensor
169} // end namespace flair
Note: See TracBrowser for help on using the repository browser.