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

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

corrected simu/device id for sensors

File size: 4.5 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
51 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
52 sizeof(gps_states_t));
53
54 SetIsReady(true);
55}
56
57
58SimuGps::SimuGps(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
59 : NmeaGps(parent, name), Thread(parent, name, 0) {
60 dataRate = NULL;
61
62 shmem = new SharedMem((Thread *)this,ShMemName(modelId, deviceId),
63 sizeof(gps_states_t));
64
65 SetIsReady(true);
66}
67
68SimuGps::~SimuGps() {
69 SafeStop();
70 Join();
71}
72
73string SimuGps::ShMemName(uint32_t modelId,uint32_t deviceId) {
74 ostringstream dev_name;
75 dev_name << "simu" << modelId << "_gps_" << deviceId;
76 return dev_name.str().c_str();
77}
78
79void SimuGps::UpdateFrom(const io_data *data) {
80 if (data != NULL) {
81 cvmatrix *input = (cvmatrix *)data;
82 gps_states_t state;
83
84 input->GetMutex();
85 //simulator is ned, convert it to enu
86 //TODO: put simulator in enu?
87 state.e = input->ValueNoMutex(5, 0);//y simulator
88 state.n = input->ValueNoMutex(4, 0);//x simulator
89 state.u = -input->ValueNoMutex(6, 0);//z simulator
90 state.ve = input->ValueNoMutex(11, 0);//vy simulator
91 state.vn = input->ValueNoMutex(10, 0);//vx simulator
92 input->ReleaseMutex();
93
94 shmem->Write((char *)&state, sizeof(gps_states_t));
95 }
96}
97
98void SimuGps::Run(void) {
99 gps_states_t state;
100 char buf[512];
101 nmeaGPGGA gga;
102 nmeaGPVTG vtg;
103 nmeaPOS pos;
104 nmeaINFO info;
105
106 vtg.dir_t='T';
107 vtg.dec_m='M';
108 vtg.spk_k='K';
109 vtg.spn_n='N';
110
111 if (dataRate == NULL) {
112 Thread::Err("not applicable for simulation part.\n");
113 return;
114 }
115
116 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
117
118 WarnUponSwitches(true);
119
120
121 while (!ToBeStopped()) {
122 WaitPeriod();
123
124 if (dataRate->ValueChanged() == true) {
125 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
126 }
127
128 shmem->Read((char *)&state, sizeof(gps_states_t));
129
130 double x, y, z;
131 ENU_2_ECEF(state.e, state.n, state.u, x,y,z, Euler::ToRadian(longitudeRef->Value()), Euler::ToRadian(latitudeRef->Value()), altitudeRef->Value());
132 ECEF_2_Geographique( x, y, z,pos.lon, pos.lat, gga.elv);
133 nmea_pos2info(&pos,&info);
134
135 if(pos.lat>0) {
136 gga.ns='N';
137 gga.lat=info.lat;
138 } else {
139 gga.ns='S';
140 gga.lat=-info.lat;
141 }
142 if(pos.lon>0) {
143 gga.ew='E';
144 gga.lon=info.lon;
145 } else {
146 gga.ew='W';
147 gga.lon=-info.lon;
148 }
149
150 gga.sig=fixQuality->Value();
151 gga.satinuse=numberOfSatellites->Value();
152
153 nmea_gen_GPGGA(buf,sizeof(buf),&gga);
154 parseFrame(buf,sizeof(buf));
155
156 vtg.dir=90-Euler::ToDegree(atan2f(state.vn,state.ve));
157 vtg.spk=sqrtf(state.ve*state.ve+state.vn*state.vn)*3600./1000.;
158 nmea_gen_GPVTG(buf,sizeof(buf),&vtg);
159 parseFrame(buf,sizeof(buf));
160 }
161
162 WarnUponSwitches(false);
163}
164
165} // end namespace sensor
166} // end namespace framewor
Note: See TracBrowser for help on using the repository browser.