[3] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[3] | 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>
|
---|
[51] | 21 | #include <GpsData.h>
|
---|
[55] | 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"
|
---|
[3] | 30 |
|
---|
| 31 | using std::string;
|
---|
[55] | 32 | using std::ostringstream;
|
---|
[3] | 33 | using namespace flair::core;
|
---|
[55] | 34 | using namespace flair::gui;
|
---|
| 35 | using namespace Geodesie;
|
---|
[3] | 36 |
|
---|
[15] | 37 | namespace flair {
|
---|
| 38 | namespace sensor {
|
---|
[3] | 39 |
|
---|
[137] | 40 | SimuGps::SimuGps(string name,
|
---|
[158] | 41 | NmeaGps::NMEAFlags_t NMEAFlags, uint32_t modelId,uint32_t deviceId,uint8_t priority)
|
---|
[137] | 42 | : NmeaGps(name, NMEAFlags),Thread(getFrameworkManager(), name, priority) {
|
---|
[3] | 43 |
|
---|
[55] | 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 |
|
---|
[158] | 51 | shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
|
---|
[55] | 52 | sizeof(gps_states_t));
|
---|
[157] | 53 |
|
---|
| 54 | SetIsReady(true);
|
---|
[55] | 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[158] | 58 | SimuGps::SimuGps(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
|
---|
[55] | 59 | : NmeaGps(parent, name), Thread(parent, name, 0) {
|
---|
| 60 | dataRate = NULL;
|
---|
| 61 |
|
---|
[158] | 62 | shmem = new SharedMem((Thread *)this,ShMemName(modelId, deviceId),
|
---|
[55] | 63 | sizeof(gps_states_t));
|
---|
[157] | 64 |
|
---|
| 65 | SetIsReady(true);
|
---|
[55] | 66 | }
|
---|
| 67 |
|
---|
[15] | 68 | SimuGps::~SimuGps() {
|
---|
| 69 | SafeStop();
|
---|
| 70 | Join();
|
---|
[3] | 71 | }
|
---|
| 72 |
|
---|
[158] | 73 | string 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 |
|
---|
[55] | 79 | void 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?
|
---|
[68] | 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
|
---|
[55] | 92 | input->ReleaseMutex();
|
---|
| 93 |
|
---|
| 94 | shmem->Write((char *)&state, sizeof(gps_states_t));
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[15] | 98 | void SimuGps::Run(void) {
|
---|
[55] | 99 | gps_states_t state;
|
---|
[68] | 100 | char buf[512];
|
---|
[51] | 101 | nmeaGPGGA gga;
|
---|
| 102 | nmeaGPVTG vtg;
|
---|
[55] | 103 | nmeaPOS pos;
|
---|
| 104 | nmeaINFO info;
|
---|
| 105 |
|
---|
[68] | 106 | vtg.dir_t='T';
|
---|
| 107 | vtg.dec_m='M';
|
---|
| 108 | vtg.spk_k='K';
|
---|
| 109 | vtg.spn_n='N';
|
---|
| 110 |
|
---|
[55] | 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 |
|
---|
[15] | 118 | WarnUponSwitches(true);
|
---|
[3] | 119 |
|
---|
[51] | 120 |
|
---|
[15] | 121 | while (!ToBeStopped()) {
|
---|
| 122 | WaitPeriod();
|
---|
[51] | 123 |
|
---|
[55] | 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;
|
---|
[68] | 131 | ENU_2_ECEF(state.e, state.n, state.u, x,y,z, Euler::ToRadian(longitudeRef->Value()), Euler::ToRadian(latitudeRef->Value()), altitudeRef->Value());
|
---|
[55] | 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 |
|
---|
[51] | 153 | nmea_gen_GPGGA(buf,sizeof(buf),&gga);
|
---|
| 154 | parseFrame(buf,sizeof(buf));
|
---|
[68] | 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));
|
---|
[15] | 160 | }
|
---|
[3] | 161 |
|
---|
[15] | 162 | WarnUponSwitches(false);
|
---|
[3] | 163 | }
|
---|
| 164 |
|
---|
| 165 | } // end namespace sensor
|
---|
[170] | 166 | } // end namespace flair
|
---|