]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
Boris Koenig:
[flightgear.git] / src / Systems / submodel.cxx
1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include "submodel.hxx"
7
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
10
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
14
15
16 const double SubmodelSystem::lbs_to_slugs = 0.031080950172;
17
18 SubmodelSystem::SubmodelSystem ()
19 {
20     
21   x_offset = y_offset = 0.0;
22   z_offset = -4.0;
23   pitch_offset = 2.0;
24   yaw_offset = 0.0;
25   
26   out[0] = out[1] = out[2] = 0;
27   in[3] = out[3] = 1;
28   string contents_node;
29 }
30
31 SubmodelSystem::~SubmodelSystem ()
32 {
33 }
34
35 void
36 SubmodelSystem::init ()
37 {
38     load();
39     _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
40
41     _user_lat_node = fgGetNode("/position/latitude-deg", true);
42     _user_lon_node = fgGetNode("/position/longitude-deg", true);
43     _user_alt_node = fgGetNode("/position/altitude-ft", true);
44
45     _user_heading_node = fgGetNode("/orientation/heading-deg", true);
46     _user_pitch_node =   fgGetNode("/orientation/pitch-deg", true);
47     _user_roll_node =    fgGetNode("/orientation/roll-deg", true);
48     _user_yaw_node =     fgGetNode("/orientation/yaw-deg", true);
49     _user_alpha_node =   fgGetNode("/orientation/alpha-deg", true);
50
51     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
52
53     _user_wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps",true);
54     _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
55
56     _user_speed_down_fps_node   = fgGetNode("/velocities/speed-down-fps",true);
57     _user_speed_east_fps_node   = fgGetNode("/velocities/speed-east-fps",true);
58     _user_speed_north_fps_node  = fgGetNode("/velocities/speed-north-fps",true);
59
60     ai = (FGAIManager*)globals->get_subsystem("ai_model");
61
62
63 }
64
65 void
66 SubmodelSystem::bind ()
67 {
68 }
69
70 void
71 SubmodelSystem::unbind ()
72 {
73   submodel_iterator = submodels.begin();
74   while(submodel_iterator != submodels.end()) {
75     (*submodel_iterator)->prop->untie("count");
76     ++submodel_iterator;
77   }
78 }
79
80 void
81 SubmodelSystem::update (double dt)
82 {
83   if (!(_serviceable_node->getBoolValue())) return;
84   int i=-1;
85   submodel_iterator = submodels.begin();
86   while(submodel_iterator != submodels.end()) {
87     i++;
88     if ((*submodel_iterator)->trigger->getBoolValue()) {
89         if ((*submodel_iterator)->count != 0) {
90           release( (*submodel_iterator), dt);
91         } 
92     } else {
93       (*submodel_iterator)->first_time = true;
94     }  
95     ++submodel_iterator;
96   }
97    
98 }
99
100 bool
101 SubmodelSystem::release (submodel* sm, double dt)
102 {
103   sm->timer += dt;
104   if (sm->timer < sm->delay) return false;
105   sm->timer = 0.0;
106
107   if (sm->first_time) {
108     dt = 0.0;
109     sm->first_time = false;
110   }
111
112   transform(sm);  // calculate submodel's initial conditions in world-coordinates
113
114   FGAIModelEntity entity;
115
116   entity.path = sm->model.c_str();
117   entity.latitude = IC.lat;
118   entity.longitude = IC.lon;
119   entity.altitude = IC.alt;
120   entity.azimuth = IC.azimuth;
121   entity.elevation = IC.elevation;
122   entity.roll = IC.roll;
123   entity.speed = IC.speed;
124   entity.eda = sm->drag_area;
125   entity.life = sm->life;
126   entity.buoyancy = sm->buoyancy;
127   entity.wind_from_east = IC.wind_from_east;
128   entity.wind_from_north = IC.wind_from_north;
129   entity.wind = sm->wind;
130   entity.cd = sm->cd;
131   entity.mass = IC.mass;
132   ai->createBallistic( &entity );
133  
134   if (sm->count > 0) (sm->count)--; 
135
136   return true;                    
137 }
138
139 void
140 SubmodelSystem::load ()
141 {
142
143     int i;
144     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
145     SGPropertyNode root;
146
147     if (path) {
148       SGPath config( globals->get_fg_root() );
149       config.append( path->getStringValue() );
150
151       try {
152         readProperties(config.str(), &root);
153       } catch (const sg_exception &e) {
154         SG_LOG(SG_GENERAL, SG_ALERT,
155         "Unable to read submodels file: ");
156         cout << config.str() << endl;
157         return;
158       }
159     }
160
161    int count = root.nChildren();
162    for (i = 0; i < count; i++) { 
163      // cout << "Reading submodel " << i << endl;        
164      SGPropertyNode *prop;
165      submodel* sm = new submodel;
166      SGPropertyNode * entry_node = root.getChild(i);
167      sm->trigger        = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
168      sm->name           = entry_node->getStringValue("name", "none_defined");
169      sm->model          = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
170      sm->speed          = entry_node->getDoubleValue("speed", 2329.4 );
171      sm->repeat         = entry_node->getBoolValue  ("repeat", false); 
172      sm->delay          = entry_node->getDoubleValue("delay", 0.25); 
173      sm->count          = entry_node->getIntValue   ("count", 1); 
174      sm->slaved         = entry_node->getBoolValue  ("slaved", false); 
175      sm->x_offset       = entry_node->getDoubleValue("x-offset", 0.0); 
176      sm->y_offset       = entry_node->getDoubleValue("y-offset", 0.0); 
177      sm->z_offset       = entry_node->getDoubleValue("z-offset", 0.0); 
178      sm->yaw_offset     = entry_node->getDoubleValue("yaw-offset", 0.0); 
179      sm->pitch_offset   = entry_node->getDoubleValue("pitch-offset", 0.0);
180      sm->drag_area      = entry_node->getDoubleValue("eda", 0.034);
181      sm->life           = entry_node->getDoubleValue("life", 900.0);
182      sm->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
183      sm->wind           = entry_node->getBoolValue  ("wind", false); 
184      sm->first_time     = false;
185      sm->cd             = entry_node->getDoubleValue("cd", 0.193);
186      sm->weight         = entry_node->getDoubleValue("weight", 0.25);
187      sm->contents_node  = fgGetNode(entry_node->getStringValue("contents", "none"), true);
188
189      sm->trigger->setBoolValue(false);
190      sm->timer = sm->delay;
191  
192      sm->contents = sm->contents_node->getDoubleValue();
193  
194      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
195      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
196
197 //   sm->prop->tie("contents", SGRawValuePointer<double>(&(sm->contents)));
198 //   sm->prop->tie("contents path", SGRawValuePointer<const char *>(&(sm->contents_node)));
199      submodels.push_back( sm );
200    }
201
202   submodel_iterator = submodels.begin();
203   
204 }
205
206
207 void
208 SubmodelSystem::transform( submodel* sm) 
209 {
210
211 // get initial conditions 
212  
213 // get the weight of the contents (lbs) and convert to mass (slugs)
214   sm->contents = sm->contents_node->getDoubleValue();
215   
216   IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;;
217 //  cout << IC.mass << endl;
218     
219 // set contents to 0 in the parent
220    sm->contents_node->setDoubleValue(0); 
221     
222   IC.lat =        _user_lat_node->getDoubleValue();    
223   IC.lon =        _user_lon_node->getDoubleValue();   
224   IC.alt =        _user_alt_node->getDoubleValue();    
225   IC.roll =       _user_roll_node->getDoubleValue();    // rotation about x axis 
226   IC.elevation =  _user_pitch_node->getDoubleValue();   // rotation about y axis 
227   IC.azimuth =    _user_heading_node->getDoubleValue(); // rotation about z axis
228   
229   IC.speed =           _user_speed_node->getDoubleValue();
230   IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
231   IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
232   
233   IC.speed_down_fps =   _user_speed_down_fps_node->getDoubleValue();
234   IC.speed_east_fps =   _user_speed_east_fps_node->getDoubleValue();
235   IC.speed_north_fps =  _user_speed_north_fps_node->getDoubleValue();
236
237   
238   in[0] = sm->x_offset;
239   in[1] = sm->y_offset;
240   in[2] = sm->z_offset; 
241  
242   
243 // pre-process the trig functions
244
245     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
246     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
247     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
248     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
249     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
250     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
251
252 // set up the transform matrix
253
254     trans[0][0] =  cosRy * cosRz;
255     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
256     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
257
258     trans[1][0] =  cosRy * sinRz;
259     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
260     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
261
262     trans[2][0] =  -1 * sinRy;
263     trans[2][1] =  sinRx * cosRy;
264     trans[2][2] =  cosRx * cosRy;
265
266
267 // multiply the input and transform matrices
268
269    out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
270    out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
271    out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
272
273    // convert ft to degrees of latitude
274    out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
275
276    // convert ft to degrees of longitude
277    out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
278
279    // set submodel initial position
280    IC.lat += out[0];
281    IC.lon += out[1];
282    IC.alt += out[2];
283
284    // get aircraft velocity vector angles in XZ and XY planes
285     //double alpha = _user_alpha_node->getDoubleValue();
286     //double velXZ = IC.elevation - alpha * cosRx;
287     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
288    
289    // Get submodel initial velocity vector angles in XZ and XY planes.
290    // This needs to be fixed. This vector should be added to aircraft's vector. 
291    IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
292    IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
293  
294    // For now assume vector is close to airplane's vector.  This needs to be fixed.
295    //IC.speed += ; 
296
297    // calcuate the total speed north
298
299    IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
300                     cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
301
302    // calculate the total speed east
303
304    IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
305                      sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
306
307    // calculate the total speed down
308   
309    IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
310                         IC.speed_down_fps;
311
312   // re-calculate speed, elevation and azimuth
313
314    IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north + 
315                     IC.total_speed_east * IC.total_speed_east +
316                     IC.total_speed_down * IC.total_speed_down);
317
318    IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
319    
320    // rationalise the output
321    
322    if (IC.total_speed_north <= 0){
323     IC.azimuth = 180 + IC.azimuth;
324    }
325    else{
326      if(IC.total_speed_east <= 0){
327      IC.azimuth = 360 + IC.azimuth;
328    }
329   }
330  
331     IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north * 
332                        IC.total_speed_north + 
333                        IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
334
335 }
336
337 void 
338 SubmodelSystem::updatelat(double lat) 
339 {
340     double latitude = lat;
341     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
342     ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
343 }
344
345 // end of submodel.cxx
346
347
348
349