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()
47 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
48 _serviceable_node->setBoolValue(true);
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);
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);
60 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
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);
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);
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);
74 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()
225 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
229 string Path = path->getStringValue();
230 bool Seviceable =_serviceable_node->getBoolValue();
231 setData(id, Path, Seviceable);
236 void FGSubmodelMgr::transform(submodel* sm)
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;
244 // set contents to 0 in the parent
245 sm->contents_node->setDoubleValue(0);
247 IC.mass = sm->weight * lbs_to_slugs;
249 //cout << "mass " << IC.mass << endl;
251 if (sm->speed_node != 0)
252 sm->speed = sm->speed_node->getDoubleValue();
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();
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();
274 while (sm_list_itr != end) {
275 int id = (*sm_list_itr)->getID();
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();
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 ;*/
306 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
307 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
309 in[0] = sm->x_offset;
310 in[1] = sm->y_offset;
311 in[2] = sm->z_offset;
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);
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;
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;
330 trans[2][0] = -1 * sinRy;
331 trans[2][1] = sinRx * cosRy;
332 trans[2][2] = cosRx * cosRy;
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];
340 // convert ft to degrees of latitude
341 out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
343 // convert ft to degrees of longitude
344 out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
346 // set submodel initial position
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);
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);
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;
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;
369 // calculate the total speed down
370 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
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);
378 //cout << " speed fps out" << IC.speed << endl ;
379 IC.azimuth = atan(IC.total_speed_east / IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
381 // rationalise the output
382 if (IC.total_speed_north <= 0) {
383 IC.azimuth = 180 + IC.azimuth;
386 if (IC.total_speed_east <= 0)
387 IC.azimuth = 360 + IC.azimuth;
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;
397 void FGSubmodelMgr::updatelat(double lat)
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);
403 void FGSubmodelMgr::loadAI()
405 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
407 sm_list = ai->get_ai_list();
409 if (sm_list.empty()) {
410 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
414 sm_list_iterator sm_list_itr = sm_list.begin();
415 sm_list_iterator end = sm_list.end();
417 while (sm_list_itr != end) {
418 string path = (*sm_list_itr)->_getPath();
425 int id = (*sm_list_itr)->getID();
426 bool serviceable = (*sm_list_itr)->_getServiceable();
427 setData(id, path, serviceable);
434 double FGSubmodelMgr::getRange(double lat, double lon, double lat2, double lon2) const
437 double course, distance, az2;
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;
445 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
449 SGPath config(globals->get_fg_root());
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());
462 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
463 vector<SGPropertyNode_ptr>::iterator it = children.begin();
464 vector<SGPropertyNode_ptr>::iterator end = children.end();
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);
493 //cout << "sm->contents_node " << sm->contents_node << endl;
494 if (sm->contents_node != 0)
495 sm->contents = sm->contents_node->getDoubleValue();
497 const char *trigger_path = entry_node->getStringValue("trigger", 0);
499 sm->trigger_node = fgGetNode(trigger_path, true);
500 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
502 sm->trigger_node = 0;
505 if (sm->speed_node != 0)
506 sm->speed = sm->speed_node->getDoubleValue();
508 sm->timer = sm->delay;
510 sm->first_time = false;
511 sm->serviceable = serviceable;
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());
521 if (sm->contents_node != 0)
522 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
525 submodels.push_back(sm);
528 submodel_iterator = submodels.begin();
532 // end of submodel.cxx