]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/submodel.cxx
2cf28b1470acd18275f2f5eb780d4c265aa785b7
[flightgear.git] / src / AIModel / submodel.cxx
1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 // With major additions by Vivian Meaaza 2004 - 2007
4 //
5 // This file is in the Public Domain and comes with no warranty.
6
7 #ifdef HAVE_CONFIG_H
8 #  include "config.h"
9 #endif
10
11 #include "submodel.hxx"
12
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/misc/sg_path.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
16
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
19
20 #include "AIBase.hxx"
21 #include "AIManager.hxx"
22 #include "AIBallistic.hxx"
23
24
25 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
26
27 FGSubmodelMgr::FGSubmodelMgr()
28 {
29     x_offset = y_offset = 0.0;
30     z_offset = -4.0;
31     pitch_offset = 2.0;
32     yaw_offset = 0.0;
33
34     out[0] = out[1] = out[2] = 0;
35     in[3] = out[3] = 1;
36     string contents_node;
37     contrail_altitude = 30000;
38 }
39
40 FGSubmodelMgr::~FGSubmodelMgr()
41 {}
42
43 void FGSubmodelMgr::init()
44 {
45     index = 0;
46
47     _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
48     _serviceable_node->setBoolValue(true);
49
50     _user_lat_node = fgGetNode("/position/latitude-deg", true);
51     _user_lon_node = fgGetNode("/position/longitude-deg", true);
52     _user_alt_node = fgGetNode("/position/altitude-ft", true);
53
54     _user_heading_node = fgGetNode("/orientation/heading-deg", true);
55     _user_pitch_node =   fgGetNode("/orientation/pitch-deg", true);
56     _user_roll_node =    fgGetNode("/orientation/roll-deg", true);
57     _user_yaw_node =     fgGetNode("/orientation/yaw-deg", true);
58     _user_alpha_node =   fgGetNode("/orientation/alpha-deg", true);
59
60     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
61
62     _user_wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps", true);
63     _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
64
65     _user_speed_down_fps_node   = fgGetNode("/velocities/speed-down-fps", true);
66     _user_speed_east_fps_node   = fgGetNode("/velocities/speed-east-fps", true);
67     _user_speed_north_fps_node  = fgGetNode("/velocities/speed-north-fps", true);
68
69     _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
70     contrail_altitude       = _contrail_altitude_node->getDoubleValue();
71     _contrail_trigger       = fgGetNode("ai/submodels/contrails", true);
72     _contrail_trigger->setBoolValue(false);
73
74     ai = (FGAIManager*)globals->get_subsystem("ai_model");
75
76     load();
77 }
78
79 void FGSubmodelMgr::postinit() {
80     // postinit, so that the AI list is populated
81     loadAI();
82 }
83
84 void FGSubmodelMgr::bind()
85 {}
86
87 void FGSubmodelMgr::unbind()
88 {
89     submodel_iterator = submodels.begin();
90
91     while (submodel_iterator != submodels.end()) {
92         (*submodel_iterator)->prop->untie("count");
93         ++submodel_iterator;
94     }
95
96 }
97
98 void FGSubmodelMgr::update(double dt)
99 {
100     if (!(_serviceable_node->getBoolValue()))
101         return;
102
103     int i = -1;
104     bool in_range = true;
105     bool trigger = false;
106
107     _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
108
109     submodel_iterator = submodels.begin();
110
111     while (submodel_iterator != submodels.end()) {
112         i++;
113
114         if ((*submodel_iterator)->trigger_node != 0) {
115             trigger = (*submodel_iterator)->trigger_node->getBoolValue();
116             //cout << (*submodel_iterator)->name << "trigger node found" << trigger << endl;
117         } else {
118             trigger = true;
119             //cout << (*submodel_iterator)->name << "trigger node not found" << trigger << endl;
120         }
121
122         if (trigger) {
123             int id = (*submodel_iterator)->id;
124
125             // don't release submodels from AI Objects if they are
126             // too far away to be seen. id 0 is not an AI model,
127             // so we can skip the whole process
128             sm_list_iterator sm_list_itr = sm_list.begin();
129             sm_list_iterator end = sm_list.end();
130
131             while (sm_list_itr != end) {
132
133                 if (id == 0) {
134                     SG_LOG(SG_GENERAL, SG_DEBUG,
135                         "Submodels: continuing: " << id);
136                     ++sm_list_itr;
137                     continue;
138                 }
139
140                 int parent_id = (*sm_list_itr)->getID();
141
142                 if (parent_id == id) {
143                     double parent_lat = (*sm_list_itr)->_getLatitude();
144                     double parent_lon = (*sm_list_itr)->_getLongitude();
145                     double own_lat    = _user_lat_node->getDoubleValue();
146                     double own_lon    = _user_lon_node->getDoubleValue();
147                     double range_nm   = getRange(parent_lat, parent_lon, own_lat, own_lon);
148                     /* cout << "parent " << parent_id << ", "<< parent_lat << ", " << parent_lon << endl;
149                     cout << "own " << own_lat << ", " << own_lon << " range " << range_nm << endl;*/
150
151                     if (range_nm > 15) {
152                         SG_LOG(SG_GENERAL, SG_DEBUG,
153                             "Submodels: skipping release: " << id);
154                         in_range = false;
155                     }
156
157                 }
158
159                 ++sm_list_itr;
160             } // end while
161
162             if ((*submodel_iterator)->count != 0 && in_range)
163                 release((*submodel_iterator), dt);
164
165         } else
166             (*submodel_iterator)->first_time = true;
167
168         ++submodel_iterator;
169     } // end while
170
171 }
172
173 bool FGSubmodelMgr::release(submodel* sm, double dt)
174 {
175     // only run if first time or repeat is set to true
176     if (!sm->first_time && !sm->repeat)
177         return false;
178
179     sm->timer += dt;
180
181     if (sm->timer < sm->delay)
182         return false;
183
184     sm->timer = 0.0;
185
186     if (sm->first_time) {
187         dt = 0.0;
188         sm->first_time = false;
189     }
190
191     transform(sm);  // calculate submodel's initial conditions in world-coordinates
192
193     FGAIBallistic* ballist = new FGAIBallistic;
194     ballist->setPath(sm->model.c_str());
195     ballist->setLatitude(IC.lat);
196     ballist->setLongitude(IC.lon);
197     ballist->setAltitude(IC.alt);
198     ballist->setAzimuth(IC.azimuth);
199     ballist->setElevation(IC.elevation);
200     ballist->setRoll(IC.roll);
201     ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
202     ballist->setWind_from_east(IC.wind_from_east);
203     ballist->setWind_from_north(IC.wind_from_north);
204     ballist->setMass(IC.mass);
205     ballist->setDragArea(sm->drag_area);
206     ballist->setLife(sm->life);
207     ballist->setBuoyancy(sm->buoyancy);
208     ballist->setWind(sm->wind);
209     ballist->setCd(sm->cd);
210     ballist->setStabilisation(sm->aero_stabilised);
211     ballist->setNoRoll(sm->no_roll);
212     ballist->setName(sm->name);
213     ai->attach(ballist);
214
215     if (sm->count > 0)
216         (sm->count)--;
217
218     return true;
219 }
220
221 void FGSubmodelMgr::load()
222 {
223     const int id = 0;
224
225     SGPropertyNode *path = fgGetNode("/sim/submodels/path");
226     SGPropertyNode root;
227
228     if (path) {
229         string Path = path->getStringValue();
230         bool Seviceable =_serviceable_node->getBoolValue();
231         setData(id, Path, Seviceable);
232     }
233
234 }
235
236 void FGSubmodelMgr::transform(submodel* sm)
237 {
238     // get initial conditions
239     if (sm->contents_node != 0) {
240         // get the weight of the contents (lbs) and convert to mass (slugs)
241         sm->contents = sm->contents_node->getDoubleValue();
242         IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
243
244         // set contents to 0 in the parent
245         sm->contents_node->setDoubleValue(0);
246     } else
247         IC.mass = sm->weight * lbs_to_slugs;
248
249     //cout << "mass "  << IC.mass << endl;
250
251     if (sm->speed_node != 0)
252         sm->speed = sm->speed_node->getDoubleValue();
253
254     int ind = sm->id;
255
256     if (ind == 0) {
257         // set the data for a submodel tied to the main model
258         IC.lat             = _user_lat_node->getDoubleValue();
259         IC.lon             = _user_lon_node->getDoubleValue();
260         IC.alt             = _user_alt_node->getDoubleValue();
261         IC.roll            = _user_roll_node->getDoubleValue();    // rotation about x axis
262         IC.elevation       = _user_pitch_node->getDoubleValue();   // rotation about y axis
263         IC.azimuth         = _user_heading_node->getDoubleValue(); // rotation about z axis
264         IC.speed           = _user_speed_node->getDoubleValue();
265         IC.speed_down_fps  = _user_speed_down_fps_node->getDoubleValue();
266         IC.speed_east_fps  = _user_speed_east_fps_node->getDoubleValue();
267         IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
268
269     } else {
270         // set the data for a submodel tied to an AI Object
271         sm_list_iterator sm_list_itr = sm_list.begin();
272         sm_list_iterator end = sm_list.end();
273
274         while (sm_list_itr != end) {
275             int id = (*sm_list_itr)->getID();
276
277             if (ind != id) {
278                 ++sm_list_itr;
279                 continue;
280             }
281
282             //cout << "found id " << id << endl;
283             IC.lat             = (*sm_list_itr)->_getLatitude();
284             IC.lon             = (*sm_list_itr)->_getLongitude();
285             IC.alt             = (*sm_list_itr)->_getAltitude();
286             IC.roll            = (*sm_list_itr)->_getRoll();
287             IC.elevation       = (*sm_list_itr)->_getPitch();
288             IC.azimuth         = (*sm_list_itr)->_getHeading();
289             IC.alt             = (*sm_list_itr)->_getAltitude();
290             IC.speed           = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
291             IC.speed_down_fps  = -(*sm_list_itr)->_getVS_fps();
292             IC.speed_east_fps  = (*sm_list_itr)->_get_speed_east_fps();
293             IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
294
295             ++sm_list_itr;
296         }
297
298     }
299
300     /*cout << "heading " << IC.azimuth << endl ;
301     cout << "speed down " << IC.speed_down_fps << endl ;
302     cout << "speed east " << IC.speed_east_fps << endl ;
303     cout << "speed north " << IC.speed_north_fps << endl ;
304     cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
305
306     IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
307     IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
308
309     in[0] = sm->x_offset;
310     in[1] = sm->y_offset;
311     in[2] = sm->z_offset;
312
313     // pre-process the trig functions
314     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
315     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
316     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
317     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
318     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
319     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
320
321     // set up the transform matrix
322     trans[0][0] =  cosRy * cosRz;
323     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
324     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
325
326     trans[1][0] =  cosRy * sinRz;
327     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
328     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
329
330     trans[2][0] =  -1 * sinRy;
331     trans[2][1] =  sinRx * cosRy;
332     trans[2][2] =  cosRx * cosRy;
333
334
335     // multiply the input and transform matrices
336     out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
337     out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
338     out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
339
340     // convert ft to degrees of latitude
341     out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
342
343     // convert ft to degrees of longitude
344     out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
345
346     // set submodel initial position
347     IC.lat += out[0];
348     IC.lon += out[1];
349     IC.alt += out[2];
350
351     // get aircraft velocity vector angles in XZ and XY planes
352     //double alpha = _user_alpha_node->getDoubleValue();
353     //double velXZ = IC.elevation - alpha * cosRx;
354     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
355
356     // Get submodel initial velocity vector angles in XZ and XY planes.
357     // This needs to be fixed. This vector should be added to aircraft's vector.
358     IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
359     IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
360
361     // calcuate the total speed north
362     IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
363                             * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
364
365     // calculate the total speed east
366     IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
367                             * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
368
369     // calculate the total speed down
370     IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
371                             + IC.speed_down_fps;
372
373     // re-calculate speed, elevation and azimuth
374     IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
375         + IC.total_speed_east * IC.total_speed_east
376         + IC.total_speed_down * IC.total_speed_down);
377
378     //cout << " speed fps out" << IC.speed << endl ;
379     IC.azimuth = atan(IC.total_speed_east / IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
380
381     // rationalise the output
382     if (IC.total_speed_north <= 0) {
383         IC.azimuth = 180 + IC.azimuth;
384     } else {
385
386         if (IC.total_speed_east <= 0)
387             IC.azimuth = 360 + IC.azimuth;
388
389     }
390
391     IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
392                     * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
393                     * SG_RADIANS_TO_DEGREES;
394
395 }
396
397 void FGSubmodelMgr::updatelat(double lat)
398 {
399     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
400     ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
401 }
402
403 void FGSubmodelMgr::loadAI()
404 {
405     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
406
407     sm_list = ai->get_ai_list();
408
409     if (sm_list.empty()) {
410         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
411         return;
412     }
413
414     sm_list_iterator sm_list_itr = sm_list.begin();
415     sm_list_iterator end = sm_list.end();
416
417     while (sm_list_itr != end) {
418         string path = (*sm_list_itr)->_getPath();
419
420         if (path.empty()) {
421             ++sm_list_itr;
422             continue;
423         }
424
425         int id = (*sm_list_itr)->getID();
426         bool serviceable = (*sm_list_itr)->_getServiceable();
427         setData(id, path, serviceable);
428         ++sm_list_itr;
429     }
430
431 }
432
433
434 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
435 {
436
437     double course, distance, az2;
438
439     //calculate the bearing and range of the second pos from the first
440     geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
441     distance *= SG_METER_TO_NM;
442     return distance;
443 }
444
445 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
446 {
447     SGPropertyNode root;
448
449     SGPath config(globals->get_fg_root());
450     config.append(path);
451
452     try {
453         SG_LOG(SG_GENERAL, SG_DEBUG,
454             "Submodels: Trying to read AI submodels file: " << config.str());
455         readProperties(config.str(), &root);
456     } catch (const sg_exception &e) {
457         SG_LOG(SG_GENERAL, SG_DEBUG,
458             "Submodels: Unable to read AI submodels file: " << config.str());
459         return;
460     }
461
462     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
463     vector<SGPropertyNode_ptr>::iterator it = children.begin();
464     vector<SGPropertyNode_ptr>::iterator end = children.end();
465
466     for (int i = 0; it != end; ++it, i++) {
467         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
468         submodel* sm = new submodel;
469         SGPropertyNode * entry_node = *it;
470         sm->name            = entry_node->getStringValue("name", "none_defined");
471         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
472         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
473         sm->repeat          = entry_node->getBoolValue("repeat", false);
474         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
475         sm->count           = entry_node->getIntValue("count", 1);
476         sm->slaved          = entry_node->getBoolValue("slaved", false);
477         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
478         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
479         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
480         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
481         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
482         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
483         sm->life            = entry_node->getDoubleValue("life", 900.0);
484         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
485         sm->wind            = entry_node->getBoolValue("wind", false);
486         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
487         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
488         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
489         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
490         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
491         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
492
493         //cout <<  "sm->contents_node " << sm->contents_node << endl;
494         if (sm->contents_node != 0)
495             sm->contents = sm->contents_node->getDoubleValue();
496
497         const char *trigger_path = entry_node->getStringValue("trigger", 0);
498         if (trigger_path) {
499             sm->trigger_node = fgGetNode(trigger_path, true);
500             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
501         } else {
502             sm->trigger_node = 0;
503         }
504
505         if (sm->speed_node != 0)
506             sm->speed = sm->speed_node->getDoubleValue();
507
508         sm->timer = sm->delay;
509         sm->id = id;
510         sm->first_time = false;
511         sm->serviceable = serviceable;
512
513         sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
514         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
515         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
516         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
517         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
518         string name = sm->name;
519         sm->prop->setStringValue("name", name.c_str());
520
521         if (sm->contents_node != 0)
522             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
523
524         index++;
525         submodels.push_back(sm);
526     }
527
528     submodel_iterator = submodels.begin();
529
530 }
531
532 // end of submodel.cxx