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()
29 x_offset = y_offset = 0.0;
34 out[0] = out[1] = out[2] = 0;
37 contrail_altitude = 30000;
40 FGSubmodelMgr::~FGSubmodelMgr()
43 void FGSubmodelMgr::init()
49 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
50 _serviceable_node->setBoolValue(true);
52 _user_lat_node = fgGetNode("/position/latitude-deg", true);
53 _user_lon_node = fgGetNode("/position/longitude-deg", true);
54 _user_alt_node = fgGetNode("/position/altitude-ft", true);
56 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
57 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
58 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
59 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
60 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
62 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
64 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
65 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
67 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
68 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
69 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
71 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
72 contrail_altitude = _contrail_altitude_node->getDoubleValue();
73 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
74 _contrail_trigger->setBoolValue(false);
76 ai = (FGAIManager*)globals->get_subsystem("ai_model");
79 void FGSubmodelMgr::postinit() {
80 // postinit, so that the AI list is populated
84 void FGSubmodelMgr::bind()
87 void FGSubmodelMgr::unbind()
89 submodel_iterator = submodels.begin();
91 while (submodel_iterator != submodels.end()) {
92 (*submodel_iterator)->prop->untie("count");
98 void FGSubmodelMgr::update(double dt)
100 if (!(_serviceable_node->getBoolValue()))
104 bool in_range = true;
105 bool trigger = false;
107 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
109 submodel_iterator = submodels.begin();
111 while (submodel_iterator != submodels.end()) {
114 if ((*submodel_iterator)->trigger_node != 0) {
115 trigger = (*submodel_iterator)->trigger_node->getBoolValue();
116 //cout << (*submodel_iterator)->name << "trigger node found" << trigger << endl;
119 //cout << (*submodel_iterator)->name << "trigger node not found" << trigger << endl;
123 int id = (*submodel_iterator)->id;
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();
131 while (sm_list_itr != end) {
134 SG_LOG(SG_GENERAL, SG_DEBUG,
135 "Submodels: continuing: " << id);
140 int parent_id = (*sm_list_itr)->getID();
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;*/
152 SG_LOG(SG_GENERAL, SG_DEBUG,
153 "Submodels: skipping release: " << id);
162 if ((*submodel_iterator)->count != 0 && in_range)
163 release((*submodel_iterator), dt);
166 (*submodel_iterator)->first_time = true;
173 bool FGSubmodelMgr::release(submodel* sm, double dt)
175 // only run if first time or repeat is set to true
176 if (!sm->first_time && !sm->repeat)
181 if (sm->timer < sm->delay)
186 if (sm->first_time) {
188 sm->first_time = false;
191 transform(sm); // calculate submodel's initial conditions in world-coordinates
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);
221 void FGSubmodelMgr::load()
223 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
227 SGPath config(globals->get_fg_root());
228 config.append(path->getStringValue());
231 readProperties(config.str(), &root);
232 } catch (const sg_exception &e) {
233 SG_LOG(SG_GENERAL, SG_INFO,
234 "Submodels: unable to read submodels file: " << config.str());
239 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
240 vector<SGPropertyNode_ptr>::iterator it = children.begin();
241 vector<SGPropertyNode_ptr>::iterator end = children.end();
243 for (int i = 0; it != end; ++it, i++) {
244 // cout << "Reading submodel " << (*it)->getPath() << endl;
245 submodel* sm = new submodel;
246 SGPropertyNode * entry_node = *it;
247 sm->name = entry_node->getStringValue("name", "none_defined");
248 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
249 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
250 sm->repeat = entry_node->getBoolValue("repeat", false);
251 sm->delay = entry_node->getDoubleValue("delay", 0.25);
252 sm->count = entry_node->getIntValue("count", 1);
253 sm->slaved = entry_node->getBoolValue("slaved", false);
254 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
255 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
256 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
257 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
258 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
259 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
260 sm->life = entry_node->getDoubleValue("life", 900.0);
261 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
262 sm->wind = entry_node->getBoolValue("wind", false);
263 sm->cd = entry_node->getDoubleValue("cd", 0.193);
264 sm->weight = entry_node->getDoubleValue("weight", 0.25);
265 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
266 sm->no_roll = entry_node->getBoolValue("no-roll", false);
267 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
268 sm->trigger_node = fgGetNode(entry_node->getStringValue("trigger", "none"), false);
269 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
271 //cout << "sm->contents_node " << sm->contents_node << endl;
272 if (sm->contents_node != 0)
273 sm->contents = sm->contents_node->getDoubleValue();
275 //cout << sm->name << " sm->trigger_node " << sm->trigger_node << endl;
276 if (sm->trigger_node != 0)
277 sm->trigger_node->setBoolValue(false);
279 if (sm->speed_node != 0)
280 sm->speed = sm->speed_node->getDoubleValue();
282 sm->timer = sm->delay;
284 sm->first_time = false;
286 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
287 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
288 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
289 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
290 string name = sm->name;
291 sm->prop->setStringValue("name", name.c_str());
293 if (sm->contents_node != 0) {
294 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
298 submodels.push_back(sm);
301 submodel_iterator = submodels.begin();
304 void FGSubmodelMgr::transform(submodel* sm)
306 // get initial conditions
307 if (sm->contents_node != 0) {
308 // get the weight of the contents (lbs) and convert to mass (slugs)
309 sm->contents = sm->contents_node->getDoubleValue();
310 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
312 // set contents to 0 in the parent
313 sm->contents_node->setDoubleValue(0);
315 IC.mass = sm->weight * lbs_to_slugs;
317 //cout << "mass " << IC.mass << endl;
319 if (sm->speed_node != 0)
320 sm->speed = sm->speed_node->getDoubleValue();
325 // set the data for a submodel tied to the main model
326 IC.lat = _user_lat_node->getDoubleValue();
327 IC.lon = _user_lon_node->getDoubleValue();
328 IC.alt = _user_alt_node->getDoubleValue();
329 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
330 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
331 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
332 IC.speed = _user_speed_node->getDoubleValue();
333 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
334 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
335 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
338 // set the data for a submodel tied to an AI Object
339 sm_list_iterator sm_list_itr = sm_list.begin();
340 sm_list_iterator end = sm_list.end();
342 while (sm_list_itr != end) {
343 int id = (*sm_list_itr)->getID();
350 //cout << "found id " << id << endl;
351 IC.lat = (*sm_list_itr)->_getLatitude();
352 IC.lon = (*sm_list_itr)->_getLongitude();
353 IC.alt = (*sm_list_itr)->_getAltitude();
354 IC.roll = (*sm_list_itr)->_getRoll();
355 IC.elevation = (*sm_list_itr)->_getPitch();
356 IC.azimuth = (*sm_list_itr)->_getHeading();
357 IC.alt = (*sm_list_itr)->_getAltitude();
358 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
359 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
360 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
361 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
368 /*cout << "heading " << IC.azimuth << endl ;
369 cout << "speed down " << IC.speed_down_fps << endl ;
370 cout << "speed east " << IC.speed_east_fps << endl ;
371 cout << "speed north " << IC.speed_north_fps << endl ;
372 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
374 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
375 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
377 in[0] = sm->x_offset;
378 in[1] = sm->y_offset;
379 in[2] = sm->z_offset;
381 // pre-process the trig functions
382 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
383 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
384 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
385 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
386 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
387 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
389 // set up the transform matrix
390 trans[0][0] = cosRy * cosRz;
391 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
392 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
394 trans[1][0] = cosRy * sinRz;
395 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
396 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
398 trans[2][0] = -1 * sinRy;
399 trans[2][1] = sinRx * cosRy;
400 trans[2][2] = cosRx * cosRy;
403 // multiply the input and transform matrices
404 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
405 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
406 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
408 // convert ft to degrees of latitude
409 out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
411 // convert ft to degrees of longitude
412 out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
414 // set submodel initial position
419 // get aircraft velocity vector angles in XZ and XY planes
420 //double alpha = _user_alpha_node->getDoubleValue();
421 //double velXZ = IC.elevation - alpha * cosRx;
422 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
424 // Get submodel initial velocity vector angles in XZ and XY planes.
425 // This needs to be fixed. This vector should be added to aircraft's vector.
426 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
427 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
429 // calcuate the total speed north
430 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
431 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
433 // calculate the total speed east
434 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
435 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
437 // calculate the total speed down
438 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
441 // re-calculate speed, elevation and azimuth
442 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
443 + IC.total_speed_east * IC.total_speed_east
444 + IC.total_speed_down * IC.total_speed_down);
446 //cout << " speed fps out" << IC.speed << endl ;
447 IC.azimuth = atan(IC.total_speed_east / IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
449 // rationalise the output
450 if (IC.total_speed_north <= 0) {
451 IC.azimuth = 180 + IC.azimuth;
454 if (IC.total_speed_east <= 0)
455 IC.azimuth = 360 + IC.azimuth;
459 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
460 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
461 * SG_RADIANS_TO_DEGREES;
465 void FGSubmodelMgr::updatelat(double lat)
467 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
468 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
471 void FGSubmodelMgr::loadAI()
473 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
475 sm_list = ai->get_ai_list();
477 if (sm_list.empty()) {
478 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
482 sm_list_iterator sm_list_itr = sm_list.begin();
483 sm_list_iterator end = sm_list.end();
485 while (sm_list_itr != end) {
486 string path = (*sm_list_itr)->_getPath();
487 bool serviceable = (*sm_list_itr)->_getServiceable();
494 //cout << " path " << path << " serviceable " << serviceable << endl;
496 SGPath config(globals->get_fg_root());
498 int id = (*sm_list_itr)->getID();
500 //cout << "id: " << id << endl;
503 SG_LOG(SG_GENERAL, SG_DEBUG,
504 "Submodels: Trying to read AI submodels file: " << config.str());
505 readProperties(config.str(), &root);
506 } catch (const sg_exception &e) {
507 SG_LOG(SG_GENERAL, SG_DEBUG,
508 "Submodels: Unable to read AI submodels file: " << config.str());
512 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
513 vector<SGPropertyNode_ptr>::iterator it = children.begin();
514 vector<SGPropertyNode_ptr>::iterator end = children.end();
516 for (int i = 0; it != end; ++it, i++) {
517 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
518 submodel* sm = new submodel;
519 SGPropertyNode * entry_node = *it;
520 sm->name = entry_node->getStringValue("name", "none_defined");
521 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
522 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
523 sm->repeat = entry_node->getBoolValue("repeat", false);
524 sm->delay = entry_node->getDoubleValue("delay", 0.25);
525 sm->count = entry_node->getIntValue("count", 1);
526 sm->slaved = entry_node->getBoolValue("slaved", false);
527 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
528 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
529 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
530 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
531 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
532 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
533 sm->life = entry_node->getDoubleValue("life", 900.0);
534 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
535 sm->wind = entry_node->getBoolValue("wind", false);
536 sm->cd = entry_node->getDoubleValue("cd", 0.193);
537 sm->weight = entry_node->getDoubleValue("weight", 0.25);
538 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
539 sm->no_roll = entry_node->getBoolValue("no-roll", false);
540 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
541 sm->trigger_node = fgGetNode(entry_node->getStringValue("trigger", "none"), false);
542 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
544 //cout << "sm->contents_node " << sm->contents_node << endl;
545 if (sm->contents_node != 0)
546 sm->contents = sm->contents_node->getDoubleValue();
547 //cout << "sm->trigger_node " << sm->trigger_node << endl;
548 if (sm->trigger_node != 0)
549 sm->trigger_node->setBoolValue(false);
551 if (sm->speed_node != 0)
552 sm->speed = sm->speed_node->getDoubleValue();
554 sm->timer = sm->delay;
556 sm->first_time = false;
558 sm->serviceable = (*sm_list_itr)->_getServiceable();
560 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
561 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
562 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
563 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
564 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
565 string name = sm->name;
566 sm->prop->setStringValue("name", name.c_str());
568 if (sm->contents_node != 0)
569 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
572 submodels.push_back(sm);
575 submodel_iterator = submodels.begin();
581 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
584 double course, distance, az2;
586 //calculate the bearing and range of the second pos from the first
587 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
588 distance *= SG_METER_TO_NM;
591 // end of submodel.cxx