]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/submodel.cxx
20e33528122b15878ffab39b039420d08b387217
[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     ballist->setImpact(sm->impact);
214     ballist->setImpactReportNode(sm->impact_reports);
215     ai->attach(ballist);
216
217     if (sm->count > 0)
218         (sm->count)--;
219
220     return true;
221 }
222
223 void FGSubmodelMgr::load()
224 {
225     const int id = 0;
226
227     SGPropertyNode *path = fgGetNode("/sim/submodels/path");
228     SGPropertyNode root;
229
230     if (path) {
231         string Path = path->getStringValue();
232         bool Seviceable =_serviceable_node->getBoolValue();
233         setData(id, Path, Seviceable);
234     }
235
236 }
237
238 void FGSubmodelMgr::transform(submodel* sm)
239 {
240     // get initial conditions
241     if (sm->contents_node != 0) {
242         // get the weight of the contents (lbs) and convert to mass (slugs)
243         sm->contents = sm->contents_node->getDoubleValue();
244         IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
245
246         // set contents to 0 in the parent
247         sm->contents_node->setDoubleValue(0);
248     } else
249         IC.mass = sm->weight * lbs_to_slugs;
250
251     //cout << "mass "  << IC.mass << endl;
252
253     if (sm->speed_node != 0)
254         sm->speed = sm->speed_node->getDoubleValue();
255
256     int ind = sm->id;
257
258     if (ind == 0) {
259         // set the data for a submodel tied to the main model
260         IC.lat             = _user_lat_node->getDoubleValue();
261         IC.lon             = _user_lon_node->getDoubleValue();
262         IC.alt             = _user_alt_node->getDoubleValue();
263         IC.roll            = _user_roll_node->getDoubleValue();    // rotation about x axis
264         IC.elevation       = _user_pitch_node->getDoubleValue();   // rotation about y axis
265         IC.azimuth         = _user_heading_node->getDoubleValue(); // rotation about z axis
266         IC.speed           = _user_speed_node->getDoubleValue();
267         IC.speed_down_fps  = _user_speed_down_fps_node->getDoubleValue();
268         IC.speed_east_fps  = _user_speed_east_fps_node->getDoubleValue();
269         IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
270
271     } else {
272         // set the data for a submodel tied to an AI Object
273         sm_list_iterator sm_list_itr = sm_list.begin();
274         sm_list_iterator end = sm_list.end();
275
276         while (sm_list_itr != end) {
277             int id = (*sm_list_itr)->getID();
278
279             if (ind != id) {
280                 ++sm_list_itr;
281                 continue;
282             }
283
284             //cout << "found id " << id << endl;
285             IC.lat             = (*sm_list_itr)->_getLatitude();
286             IC.lon             = (*sm_list_itr)->_getLongitude();
287             IC.alt             = (*sm_list_itr)->_getAltitude();
288             IC.roll            = (*sm_list_itr)->_getRoll();
289             IC.elevation       = (*sm_list_itr)->_getPitch();
290             IC.azimuth         = (*sm_list_itr)->_getHeading();
291             IC.alt             = (*sm_list_itr)->_getAltitude();
292             IC.speed           = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
293             IC.speed_down_fps  = -(*sm_list_itr)->_getVS_fps();
294             IC.speed_east_fps  = (*sm_list_itr)->_get_speed_east_fps();
295             IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
296
297             ++sm_list_itr;
298         }
299
300     }
301
302     /*cout << "heading " << IC.azimuth << endl ;
303     cout << "speed down " << IC.speed_down_fps << endl ;
304     cout << "speed east " << IC.speed_east_fps << endl ;
305     cout << "speed north " << IC.speed_north_fps << endl ;
306     cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
307
308     IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
309     IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
310
311     in[0] = sm->x_offset;
312     in[1] = sm->y_offset;
313     in[2] = sm->z_offset;
314
315     // pre-process the trig functions
316     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
317     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
318     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
319     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
320     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
321     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
322
323     // set up the transform matrix
324     trans[0][0] =  cosRy * cosRz;
325     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
326     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
327
328     trans[1][0] =  cosRy * sinRz;
329     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
330     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
331
332     trans[2][0] =  -1 * sinRy;
333     trans[2][1] =  sinRx * cosRy;
334     trans[2][2] =  cosRx * cosRy;
335
336
337     // multiply the input and transform matrices
338     out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
339     out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
340     out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
341
342     // convert ft to degrees of latitude
343     out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
344
345     // convert ft to degrees of longitude
346     out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
347
348     // set submodel initial position
349     IC.lat += out[0];
350     IC.lon += out[1];
351     IC.alt += out[2];
352
353     // get aircraft velocity vector angles in XZ and XY planes
354     //double alpha = _user_alpha_node->getDoubleValue();
355     //double velXZ = IC.elevation - alpha * cosRx;
356     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
357
358     // Get submodel initial velocity vector angles in XZ and XY planes.
359     // This needs to be fixed. This vector should be added to aircraft's vector.
360     IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
361     IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
362
363     // calcuate the total speed north
364     IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
365                             * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
366
367     // calculate the total speed east
368     IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
369                             * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
370
371     // calculate the total speed down
372     IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
373                             + IC.speed_down_fps;
374
375     // re-calculate speed, elevation and azimuth
376     IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
377         + IC.total_speed_east * IC.total_speed_east
378         + IC.total_speed_down * IC.total_speed_down);
379
380     //cout << " speed fps out" << IC.speed << endl ;
381     IC.azimuth = atan(IC.total_speed_east / IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
382
383     // rationalise the output
384     if (IC.total_speed_north <= 0) {
385         IC.azimuth = 180 + IC.azimuth;
386     } else {
387
388         if (IC.total_speed_east <= 0)
389             IC.azimuth = 360 + IC.azimuth;
390
391     }
392
393     IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
394                     * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
395                     * SG_RADIANS_TO_DEGREES;
396
397 }
398
399 void FGSubmodelMgr::updatelat(double lat)
400 {
401     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
402     ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
403 }
404
405 void FGSubmodelMgr::loadAI()
406 {
407     SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
408
409     sm_list = ai->get_ai_list();
410
411     if (sm_list.empty()) {
412         SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
413         return;
414     }
415
416     sm_list_iterator sm_list_itr = sm_list.begin();
417     sm_list_iterator end = sm_list.end();
418
419     while (sm_list_itr != end) {
420         string path = (*sm_list_itr)->_getPath();
421
422         if (path.empty()) {
423             ++sm_list_itr;
424             continue;
425         }
426
427         int id = (*sm_list_itr)->getID();
428         bool serviceable = (*sm_list_itr)->_getServiceable();
429         setData(id, path, serviceable);
430         ++sm_list_itr;
431     }
432
433 }
434
435
436 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
437 {
438
439     double course, distance, az2;
440
441     //calculate the bearing and range of the second pos from the first
442     geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
443     distance *= SG_METER_TO_NM;
444     return distance;
445 }
446
447 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
448 {
449     SGPropertyNode root;
450
451     SGPath config(globals->get_fg_root());
452     config.append(path);
453
454     try {
455         SG_LOG(SG_GENERAL, SG_DEBUG,
456             "Submodels: Trying to read AI submodels file: " << config.str());
457         readProperties(config.str(), &root);
458     } catch (const sg_exception &e) {
459         SG_LOG(SG_GENERAL, SG_DEBUG,
460             "Submodels: Unable to read AI submodels file: " << config.str());
461         return;
462     }
463
464     vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
465     vector<SGPropertyNode_ptr>::iterator it = children.begin();
466     vector<SGPropertyNode_ptr>::iterator end = children.end();
467
468     for (int i = 0; it != end; ++it, i++) {
469         //cout << "Reading AI submodel " << (*it)->getPath() << endl;
470         submodel* sm = new submodel;
471         SGPropertyNode * entry_node = *it;
472         sm->name            = entry_node->getStringValue("name", "none_defined");
473         sm->model           = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
474         sm->speed           = entry_node->getDoubleValue("speed", 2329.4);
475         sm->repeat          = entry_node->getBoolValue("repeat", false);
476         sm->delay           = entry_node->getDoubleValue("delay", 0.25);
477         sm->count           = entry_node->getIntValue("count", 1);
478         sm->slaved          = entry_node->getBoolValue("slaved", false);
479         sm->x_offset        = entry_node->getDoubleValue("x-offset", 0.0);
480         sm->y_offset        = entry_node->getDoubleValue("y-offset", 0.0);
481         sm->z_offset        = entry_node->getDoubleValue("z-offset", 0.0);
482         sm->yaw_offset      = entry_node->getDoubleValue("yaw-offset", 0.0);
483         sm->pitch_offset    = entry_node->getDoubleValue("pitch-offset", 0.0);
484         sm->drag_area       = entry_node->getDoubleValue("eda", 0.034);
485         sm->life            = entry_node->getDoubleValue("life", 900.0);
486         sm->buoyancy        = entry_node->getDoubleValue("buoyancy", 0);
487         sm->wind            = entry_node->getBoolValue("wind", false);
488         sm->cd              = entry_node->getDoubleValue("cd", 0.193);
489         sm->weight          = entry_node->getDoubleValue("weight", 0.25);
490         sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
491         sm->no_roll         = entry_node->getBoolValue("no-roll", false);
492         sm->impact          = entry_node->getBoolValue("impact", false);
493         sm->impact_reports  = entry_node->getStringValue("impact-reports");
494         sm->contents_node   = fgGetNode(entry_node->getStringValue("contents", "none"), false);
495         sm->speed_node      = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
496
497         //cout <<  "sm->contents_node " << sm->contents_node << endl;
498         if (sm->contents_node != 0)
499             sm->contents = sm->contents_node->getDoubleValue();
500
501         const char *trigger_path = entry_node->getStringValue("trigger", 0);
502         if (trigger_path) {
503             sm->trigger_node = fgGetNode(trigger_path, true);
504             sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
505         } else {
506             sm->trigger_node = 0;
507         }
508
509         if (sm->speed_node != 0)
510             sm->speed = sm->speed_node->getDoubleValue();
511
512         sm->timer = sm->delay;
513         sm->id = id;
514         sm->first_time = false;
515         sm->serviceable = serviceable;
516
517         sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
518         sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
519         sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
520         sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
521         sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
522         string name = sm->name;
523         sm->prop->setStringValue("name", name.c_str());
524
525         if (sm->contents_node != 0)
526             sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
527
528         index++;
529         submodels.push_back(sm);
530     }
531
532     submodel_iterator = submodels.begin();
533
534 }
535
536 // end of submodel.cxx