1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 // With major additions by Vivian Meaaza 2004 - 2007
5 // This file is in the Public Domain and comes with no warranty.
11 #include "submodel.hxx"
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/misc/sg_path.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
21 #include "AIManager.hxx"
22 #include "AIBallistic.hxx"
25 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
27 FGSubmodelMgr::FGSubmodelMgr()
30 x_offset = y_offset = 0.0;
35 out[0] = out[1] = out[2] = 0;
38 contrail_altitude = 30000;
41 FGSubmodelMgr::~FGSubmodelMgr()
44 void FGSubmodelMgr::init()
50 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
51 _serviceable_node->setBoolValue(true);
53 _user_lat_node = fgGetNode("/position/latitude-deg", true);
54 _user_lon_node = fgGetNode("/position/longitude-deg", true);
55 _user_alt_node = fgGetNode("/position/altitude-ft", true);
57 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
58 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
59 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
60 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
61 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
63 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
65 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
66 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
68 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
69 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
70 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
72 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
73 contrail_altitude = _contrail_altitude_node->getDoubleValue();
74 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
75 _contrail_trigger->setBoolValue(false);
77 ai = (FGAIManager*)globals->get_subsystem("ai_model");
82 void FGSubmodelMgr::bind()
85 void FGSubmodelMgr::unbind()
87 submodel_iterator = submodels.begin();
89 while (submodel_iterator != submodels.end()) {
90 (*submodel_iterator)->prop->untie("count");
96 void FGSubmodelMgr::update(double dt)
99 if (!(_serviceable_node->getBoolValue()))
103 bool in_range = true;
104 bool trigger = false;
106 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
108 submodel_iterator = submodels.begin();
109 while (submodel_iterator != submodels.end()) {
112 if ((*submodel_iterator)->trigger_node != 0) {
113 trigger = (*submodel_iterator)->trigger_node->getBoolValue();
114 //cout << (*submodel_iterator)->name << "trigger node found" << trigger << endl;
117 //cout << (*submodel_iterator)->name << "trigger node not found" << trigger << endl;
121 int id = (*submodel_iterator)->id;
123 // don't release submodels from AI Objects if they are
124 // too far away to be seen. id 0 is not an AI model,
125 // so we can skip the whole process
126 sm_list_iterator sm_list_itr = sm_list.begin();
127 sm_list_iterator end = sm_list.end();
129 while (sm_list_itr != end) {
132 SG_LOG(SG_GENERAL, SG_DEBUG,
133 "Submodels: continuing: " << id);
138 int parent_id = (*sm_list_itr)->getID();
140 if (parent_id == id) {
141 double parent_lat = (*sm_list_itr)->_getLatitude();
142 double parent_lon = (*sm_list_itr)->_getLongitude();
143 double own_lat = _user_lat_node->getDoubleValue();
144 double own_lon = _user_lon_node->getDoubleValue();
145 double range_nm = getRange(parent_lat, parent_lon, own_lat, own_lon);
146 /* cout << "parent " << parent_id << ", "<< parent_lat << ", " << parent_lon << endl;
147 cout << "own " << own_lat << ", " << own_lon << " range " << range_nm << endl;*/
150 SG_LOG(SG_GENERAL, SG_DEBUG,
151 "Submodels: skipping release: " << id);
160 if ((*submodel_iterator)->count != 0 && in_range)
161 release((*submodel_iterator), dt);
164 (*submodel_iterator)->first_time = true;
171 bool FGSubmodelMgr::release(submodel* sm, double dt)
173 // only run if first time or repeat is set to true
174 if (!sm->first_time && !sm->repeat)
179 if (sm->timer < sm->delay)
184 if (sm->first_time) {
186 sm->first_time = false;
189 transform(sm); // calculate submodel's initial conditions in world-coordinates
191 FGAIBallistic* ballist = new FGAIBallistic;
192 ballist->setPath(sm->model.c_str());
193 ballist->setLatitude(IC.lat);
194 ballist->setLongitude(IC.lon);
195 ballist->setAltitude(IC.alt);
196 ballist->setAzimuth(IC.azimuth);
197 ballist->setElevation(IC.elevation);
198 ballist->setRoll(IC.roll);
199 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
200 ballist->setWind_from_east(IC.wind_from_east);
201 ballist->setWind_from_north(IC.wind_from_north);
202 ballist->setMass(IC.mass);
203 ballist->setDragArea(sm->drag_area);
204 ballist->setLife(sm->life);
205 ballist->setBuoyancy(sm->buoyancy);
206 ballist->setWind(sm->wind);
207 ballist->setCd(sm->cd);
208 ballist->setStabilisation(sm->aero_stabilised);
209 ballist->setNoRoll(sm->no_roll);
210 ballist->setName(sm->name);
219 void FGSubmodelMgr::load()
221 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
225 SGPath config(globals->get_fg_root());
226 config.append(path->getStringValue());
229 readProperties(config.str(), &root);
230 } catch (const sg_exception &e) {
231 SG_LOG(SG_GENERAL, SG_INFO,
232 "Submodels: unable to read submodels file: " << config.str());
237 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
238 vector<SGPropertyNode_ptr>::iterator it = children.begin();
239 vector<SGPropertyNode_ptr>::iterator end = children.end();
241 for (int i = 0; it != end; ++it, i++) {
242 // cout << "Reading submodel " << (*it)->getPath() << endl;
243 submodel* sm = new submodel;
244 SGPropertyNode * entry_node = *it;
245 sm->name = entry_node->getStringValue("name", "none_defined");
246 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
247 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
248 sm->repeat = entry_node->getBoolValue("repeat", false);
249 sm->delay = entry_node->getDoubleValue("delay", 0.25);
250 sm->count = entry_node->getIntValue("count", 1);
251 sm->slaved = entry_node->getBoolValue("slaved", false);
252 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
253 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
254 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
255 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
256 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
257 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
258 sm->life = entry_node->getDoubleValue("life", 900.0);
259 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
260 sm->wind = entry_node->getBoolValue("wind", false);
261 sm->cd = entry_node->getDoubleValue("cd", 0.193);
262 sm->weight = entry_node->getDoubleValue("weight", 0.25);
263 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
264 sm->no_roll = entry_node->getBoolValue("no-roll", false);
265 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
266 sm->trigger_node = fgGetNode(entry_node->getStringValue("trigger", "none"), false);
267 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
269 //cout << "sm->contents_node " << sm->contents_node << endl;
270 if (sm->contents_node != 0)
271 sm->contents = sm->contents_node->getDoubleValue();
273 //cout << sm->name << " sm->trigger_node " << sm->trigger_node << endl;
274 if (sm->trigger_node != 0)
275 sm->trigger_node->setBoolValue(false);
277 if (sm->speed_node != 0)
278 sm->speed = sm->speed_node->getDoubleValue();
280 sm->timer = sm->delay;
282 sm->first_time = false;
284 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
285 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
286 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
287 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
288 string name = sm->name;
289 sm->prop->setStringValue("name", name.c_str());
291 if (sm->contents_node != 0) {
292 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
296 submodels.push_back(sm);
299 submodel_iterator = submodels.begin();
302 void FGSubmodelMgr::transform(submodel* sm)
304 // get initial conditions
305 if (sm->contents_node != 0) {
306 // get the weight of the contents (lbs) and convert to mass (slugs)
307 sm->contents = sm->contents_node->getDoubleValue();
308 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
310 // set contents to 0 in the parent
311 sm->contents_node->setDoubleValue(0);
313 IC.mass = sm->weight * lbs_to_slugs;
315 //cout << "mass " << IC.mass << endl;
317 if (sm->speed_node != 0)
318 sm->speed = sm->speed_node->getDoubleValue();
323 // set the data for a submodel tied to the main model
324 IC.lat = _user_lat_node->getDoubleValue();
325 IC.lon = _user_lon_node->getDoubleValue();
326 IC.alt = _user_alt_node->getDoubleValue();
327 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
328 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
329 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
330 IC.speed = _user_speed_node->getDoubleValue();
331 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
332 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
333 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
336 // set the data for a submodel tied to an AI Object
337 sm_list_iterator sm_list_itr = sm_list.begin();
338 sm_list_iterator end = sm_list.end();
340 while (sm_list_itr != end) {
341 int id = (*sm_list_itr)->getID();
348 //cout << "found id " << id << endl;
349 IC.lat = (*sm_list_itr)->_getLatitude();
350 IC.lon = (*sm_list_itr)->_getLongitude();
351 IC.alt = (*sm_list_itr)->_getAltitude();
352 IC.roll = (*sm_list_itr)->_getRoll();
353 IC.elevation = (*sm_list_itr)->_getPitch();
354 IC.azimuth = (*sm_list_itr)->_getHeading();
355 IC.alt = (*sm_list_itr)->_getAltitude();
356 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
357 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
358 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
359 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
366 /*cout << "heading " << IC.azimuth << endl ;
367 cout << "speed down " << IC.speed_down_fps << endl ;
368 cout << "speed east " << IC.speed_east_fps << endl ;
369 cout << "speed north " << IC.speed_north_fps << endl ;
370 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
372 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
373 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
375 in[0] = sm->x_offset;
376 in[1] = sm->y_offset;
377 in[2] = sm->z_offset;
379 // pre-process the trig functions
380 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
381 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
382 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
383 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
384 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
385 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
387 // set up the transform matrix
388 trans[0][0] = cosRy * cosRz;
389 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
390 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
392 trans[1][0] = cosRy * sinRz;
393 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
394 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
396 trans[2][0] = -1 * sinRy;
397 trans[2][1] = sinRx * cosRy;
398 trans[2][2] = cosRx * cosRy;
401 // multiply the input and transform matrices
402 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
403 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
404 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
406 // convert ft to degrees of latitude
407 out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
409 // convert ft to degrees of longitude
410 out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
412 // set submodel initial position
417 // get aircraft velocity vector angles in XZ and XY planes
418 //double alpha = _user_alpha_node->getDoubleValue();
419 //double velXZ = IC.elevation - alpha * cosRx;
420 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
422 // Get submodel initial velocity vector angles in XZ and XY planes.
423 // This needs to be fixed. This vector should be added to aircraft's vector.
424 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
425 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
427 // calcuate the total speed north
428 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
429 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
431 // calculate the total speed east
432 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
433 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
435 // calculate the total speed down
436 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
439 // re-calculate speed, elevation and azimuth
440 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
441 + IC.total_speed_east * IC.total_speed_east
442 + IC.total_speed_down * IC.total_speed_down);
444 //cout << " speed fps out" << IC.speed << endl ;
445 IC.azimuth = atan(IC.total_speed_east / IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
447 // rationalise the output
448 if (IC.total_speed_north <= 0) {
449 IC.azimuth = 180 + IC.azimuth;
452 if (IC.total_speed_east <= 0)
453 IC.azimuth = 360 + IC.azimuth;
457 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
458 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
459 * SG_RADIANS_TO_DEGREES;
463 void FGSubmodelMgr::updatelat(double lat)
465 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
466 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
469 void FGSubmodelMgr::loadAI()
471 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
473 sm_list = ai->get_ai_list();
475 if (sm_list.empty()) {
476 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
480 sm_list_iterator sm_list_itr = sm_list.begin();
481 sm_list_iterator end = sm_list.end();
483 while (sm_list_itr != end) {
484 string path = (*sm_list_itr)->_getPath();
485 bool serviceable = (*sm_list_itr)->_getServiceable();
491 //cout << " path " << path << " serviceable " << serviceable << endl;
493 SGPath config(globals->get_fg_root());
495 int id = (*sm_list_itr)->getID();
497 //cout << "id: " << id << endl;
500 SG_LOG(SG_GENERAL, SG_DEBUG,
501 "Submodels: Trying to read AI submodels file: " << config.str());
502 readProperties(config.str(), &root);
503 } catch (const sg_exception &e) {
504 SG_LOG(SG_GENERAL, SG_DEBUG,
505 "Submodels: Unable to read AI submodels file: " << config.str());
509 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
510 vector<SGPropertyNode_ptr>::iterator it = children.begin();
511 vector<SGPropertyNode_ptr>::iterator end = children.end();
513 for (int i = 0; it != end; ++it, i++) {
514 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
515 submodel* sm = new submodel;
516 SGPropertyNode * entry_node = *it;
517 sm->name = entry_node->getStringValue("name", "none_defined");
518 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
519 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
520 sm->repeat = entry_node->getBoolValue("repeat", false);
521 sm->delay = entry_node->getDoubleValue("delay", 0.25);
522 sm->count = entry_node->getIntValue("count", 1);
523 sm->slaved = entry_node->getBoolValue("slaved", false);
524 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
525 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
526 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
527 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
528 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
529 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
530 sm->life = entry_node->getDoubleValue("life", 900.0);
531 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
532 sm->wind = entry_node->getBoolValue("wind", false);
533 sm->cd = entry_node->getDoubleValue("cd", 0.193);
534 sm->weight = entry_node->getDoubleValue("weight", 0.25);
535 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
536 sm->no_roll = entry_node->getBoolValue("no-roll", false);
537 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
538 sm->trigger_node = fgGetNode(entry_node->getStringValue("trigger", "none"), false);
539 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
541 //cout << "sm->contents_node " << sm->contents_node << endl;
542 if (sm->contents_node != 0)
543 sm->contents = sm->contents_node->getDoubleValue();
544 //cout << "sm->trigger_node " << sm->trigger_node << endl;
545 if (sm->trigger_node != 0)
546 sm->trigger_node->setBoolValue(false);
548 if (sm->speed_node != 0)
549 sm->speed = sm->speed_node->getDoubleValue();
551 sm->timer = sm->delay;
553 sm->first_time = false;
555 sm->serviceable = (*sm_list_itr)->_getServiceable();
557 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
558 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
559 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
560 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
561 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
562 string name = sm->name;
563 sm->prop->setStringValue("name", name.c_str());
565 if (sm->contents_node != 0)
566 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
569 submodels.push_back(sm);
572 submodel_iterator = submodels.begin();
578 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
581 double course, distance, az2;
583 //calculate the bearing and range of the second pos from the first
584 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
585 distance *= SG_METER_TO_NM;
588 // end of submodel.cxx