MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
sendToFlightgear.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-flightgear/sendToFlightgear.cc,v 1.1 2017/08/25 14:16:20 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 //#include <cstdint>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 
40 #include <fstream>
41 
42 #include "userelem.h"
43 #include "socketstreamdrive.h"
44 #include "socketstream_out_elem.h"
45 
46 #include "bufmod.h"
47 #include "drive_.h"
48 
49 #include "module-flightgear.h"
50 
52 
53 bool FGNetFDMWordSet::IsWord(const std::string& s) const{
55 }
56 
57 bool FGNetCtrlsWordSet::IsWord(const std::string& s) const{
59 }
60 
62  StreamContent* pSC;
63 
64  FieldsDescriptionFG *fieldsDescriptionFGInUse;
65  int totChannels;
66 
67  if(HP.IsKeyWord("NetFDM")){
68  fieldsDescriptionFGInUse = &fieldsDescriptionFGNetFDM;
69  totChannels = fieldsDescriptionFGNetFDM.size();
70  sendToFGWordSetInUse = &fGNetFDMWordSet;
71  }
72  else if(HP.IsKeyWord("NetCtrls")){
73  fieldsDescriptionFGInUse = &fieldsDescriptionFGNetCtrls;
74  totChannels = fieldsDescriptionFGNetCtrls.size();
75  sendToFGWordSetInUse = &fGNetCtrlsWordSet;
76  }
77  else{
78  silent_cerr("invalid FG data structure "
79  "at line " << HP.GetLineData() << std::endl);
81  }
82 
83  if(HP.IsKeyWord("print" "options")){
84  printOptionsOnTextFile("FGNetFDM_options.txt", fieldsDescriptionFGNetFDM);
85  printOptionsOnTextFile("FGNetCtrls_options.txt", fieldsDescriptionFGNetCtrls);
86  silent_cout("Flight Gear options correctly printed on files." << std::endl);
87  }
88 
89  /*map containing descriptions and pointers to ScalarValue specified by user for every field he wants to use*/
90  FlightGearUserChannels flightGearUserChannels;
91  this->ReadFlightGearScalarValues(pDM, HP, flightGearUserChannels, fieldsDescriptionFGInUse);
92 
93  /*bulding of buffer that will be sent to Flight gear*/
94  std::vector<ScalarValue*> allValues(totChannels);
95  std::vector<bool> assignedByUser(totChannels, false);
96 
97  /*modifies user-specified fields in the buffer*/
98  for(FlightGearUserChannels::iterator i = flightGearUserChannels.begin() ; i != flightGearUserChannels.end() ; i++){
99  FieldsDescriptionFG::iterator el = fieldsDescriptionFGInUse->find(i->first);
100  int pos = (el->second.position) -1;
101  allValues.at(pos) = i->second;
102  assignedByUser.at(pos) = true;
103  }
104 
105  /*initializes each remaining field with its default value*/
106  this->setDefaultValues(allValues, assignedByUser, *fieldsDescriptionFGInUse);
107 
108  StreamContent::Modifier *pMod(0);
109  pMod = this->buildFGStreamContentModifier(*fieldsDescriptionFGInUse);
110 
112 
113  return pSC;
114 }
115 
117  FlightGearUserChannels &flightGearUserChannels, FieldsDescriptionFG *fieldsDescriptionFGInUse){
118  const char *fieldDescription;//field the user has written in MBDyn input file
119  ScalarValue *scalarValue;
120  std::string *s;
121 
122  while((fieldDescription = HP.IsWord(*sendToFGWordSetInUse)) != NULL){
123  s = new std::string(fieldDescription);
124  if(this->fieldAlreadyUsed(std::string(fieldDescription), flightGearUserChannels)){
125  silent_cerr("field of FGNetFDM already used "
126  "at line " << HP.GetLineData() << std::endl);
128  }
129 
130  scalarValue = ReadScalarValue(pDM, HP);
131  flightGearUserChannels.insert(FlightGearUserChannels::value_type(*s, scalarValue));
132  }
133 }
134 
135 void FlightGearStreamOutputReader::setDefaultValues(std::vector<ScalarValue *> &allValues, std::vector<bool> &assignedByUser, FieldsDescriptionFG &fieldsDescriptionFGInUse){
136  DriveCaller *pDC = 0;
137 
138  if(!assignedByUser.at(fieldsDescriptionFGInUse.find("version")->second.position-1)){
139  if(sendToFGWordSetInUse == &fGNetFDMWordSet){
141  allValues.at(fieldsDescriptionFGInUse.find("version")->second.position-1) = new ScalarDriveValue(pDC); //version
142  }else if(sendToFGWordSetInUse == &fGNetCtrlsWordSet){
144  allValues.at(fieldsDescriptionFGInUse.find("version")->second.position-1) = new ScalarDriveValue(pDC); //version
145  }
146  }
147 
148  for (int i=1 ; i < allValues.size() ; i++){
149  if(!assignedByUser.at(i)){
150  SAFENEW(pDC, NullDriveCaller);
151  allValues.at(i) = new ScalarDriveValue(pDC);
152  }
153  }
154 }
155 
156 bool FlightGearStreamOutputReader::fieldAlreadyUsed(std::string fieldDescription, FlightGearUserChannels &flightGearUserChannels){
157  if(flightGearUserChannels.find(fieldDescription) != flightGearUserChannels.end()){
158  return true;
159  }
160 
161  return false;
162 }
163 
165  StreamContent::Modifier *pSCM(0);
166 
167  std::vector<BufCast *> data(fieldsDescriptionFGInUse.size());
168  buildFGBufCast(data, fieldsDescriptionFGInUse);
169 
170  size_t minsize = data[data.size() - 1]->offset() + data[data.size() - 1]->size();
171  size_t size = minsize;
172 
173  pSCM = new StreamContentCopyCast(0, 0, size, data);
174 
175  return pSCM;
176 }
HighParser::WordSet * sendToFGWordSetInUse
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual const char * IsWord(const HighParser::WordSet &ws)
Definition: parser.cc:977
void printOptionsOnTextFile(const char *fileName, FieldsDescriptionFG &fieldsDescriptionFG)
void buildFGBufCast(std::vector< BufCast * > &data, FieldsDescriptionFG &fieldsDescriptionFGInUse)
virtual bool IsWord(const std::string &s) const
FGNetCtrlsWordSet fGNetCtrlsWordSet
int FGNetCtrlsCurVersion
void ReadFlightGearScalarValues(DataManager *pDM, MBDynParser &HP, FlightGearUserChannels &flightGearUserChannels, FieldsDescriptionFG *dataStructureInUse)
std::map< std::string, ScalarValue * > FlightGearUserChannels
int FGNetFDMCurVersion
#define SAFENEW(pnt, item)
Definition: mynewmem.h:695
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
ScalarValue * ReadScalarValue(DataManager *pDM, MBDynParser &HP)
Definition: scalarvalue.cc:80
bool fieldAlreadyUsed(std::string fieldDescription, FlightGearUserChannels &flightGearUserChannels)
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
StreamContent::Modifier * buildFGStreamContentModifier(FieldsDescriptionFG &fieldsDescriptionFGInUse)
void setDefaultValues(std::vector< ScalarValue * > &allValues, std::vector< bool > &assignedByUser, FieldsDescriptionFG &fieldsDescriptionFGInUse)
virtual StreamContent * Read(DataManager *pDM, MBDynParser &HP)
FieldsDescriptionFG fieldsDescriptionFGNetCtrls
virtual bool IsWord(const std::string &s) const
FGNetFDMWordSet fGNetFDMWordSet
FieldsDescriptionFG fieldsDescriptionFGNetFDM
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
std::map< std::string, FieldInfo > FieldsDescriptionFG