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

Last change on this file since 214 was 214, checked in by Sanahuja Guillaume, 6 years ago

matrix

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 <Matrix.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 Matrix *input = (Matrix *)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 gga.elv_units='M';
112 gga.diff_units='M';
113
114 if (dataRate == NULL) {
115 Thread::Err("not applicable for simulation part.\n");
116 return;
117 }
118
119 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
120
121 WarnUponSwitches(true);
122
123
124 while (!ToBeStopped()) {
125 WaitPeriod();
126
127 if (dataRate->ValueChanged() == true) {
128 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
129 }
130
131 shmem->Read((char *)&state, sizeof(gps_states_t));
132
133 double x, y, z;
134 ENU_2_ECEF(state.e, state.n, state.u, x,y,z, Euler::ToRadian(longitudeRef->Value()), Euler::ToRadian(latitudeRef->Value()), altitudeRef->Value());
135 ECEF_2_Geographique( x, y, z,pos.lon, pos.lat, gga.elv);
136 nmea_pos2info(&pos,&info);
137
138 if(pos.lat>0) {
139 gga.ns='N';
140 gga.lat=info.lat;
141 } else {
142 gga.ns='S';
143 gga.lat=-info.lat;
144 }
145 if(pos.lon>0) {
146 gga.ew='E';
147 gga.lon=info.lon;
148 } else {
149 gga.ew='W';
150 gga.lon=-info.lon;
151 }
152
153 gga.sig=fixQuality->Value();
154 gga.satinuse=numberOfSatellites->Value();
155
156 int size=nmea_gen_GPGGA(buf,sizeof(buf),&gga);
157 parseFrame(buf,size);
158
159 vtg.dir=90-Euler::ToDegree(atan2f(state.vn,state.ve));
160 vtg.spk=sqrtf(state.ve*state.ve+state.vn*state.vn)*3600./1000.;
161 size=nmea_gen_GPVTG(buf,sizeof(buf),&vtg);
162 parseFrame(buf,size);
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.