1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
4 // This file is in the Public Domain and comes with no warranty.
6 #include "submodel.hxx"
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
16 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
18 FGSubmodelMgr::FGSubmodelMgr ()
21 x_offset = y_offset = 0.0;
26 out[0] = out[1] = out[2] = 0;
29 contrail_altitude = 30000.0;
32 FGSubmodelMgr::~FGSubmodelMgr ()
37 FGSubmodelMgr::init ()
40 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
42 _user_lat_node = fgGetNode("/position/latitude-deg", true);
43 _user_lon_node = fgGetNode("/position/longitude-deg", true);
44 _user_alt_node = fgGetNode("/position/altitude-ft", true);
46 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
47 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
48 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
49 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
50 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
52 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
54 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
55 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
57 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps",true);
58 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps",true);
59 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps",true);
61 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
62 contrail_altitude = _contrail_altitude_node->getDoubleValue();
63 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
64 _contrail_trigger->setBoolValue(false);
66 ai = (FGAIManager*)globals->get_subsystem("ai_model");
71 FGSubmodelMgr::bind ()
76 FGSubmodelMgr::unbind ()
78 submodel_iterator = submodels.begin();
79 while(submodel_iterator != submodels.end()) {
80 (*submodel_iterator)->prop->untie("count");
86 FGSubmodelMgr::update (double dt)
88 if (!(_serviceable_node->getBoolValue())) return;
91 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
93 submodel_iterator = submodels.begin();
94 while(submodel_iterator != submodels.end()) {
96 if ((*submodel_iterator)->trigger->getBoolValue()) {
97 if ((*submodel_iterator)->count != 0) {
98 release( (*submodel_iterator), dt);
101 (*submodel_iterator)->first_time = true;
109 FGSubmodelMgr::release (submodel* sm, double dt)
112 if (sm->timer < sm->delay) return false;
115 if (sm->first_time) {
117 sm->first_time = false;
120 transform(sm); // calculate submodel's initial conditions in world-coordinates
122 FGAIModelEntity entity;
124 entity.path = sm->model.c_str();
125 entity.latitude = IC.lat;
126 entity.longitude = IC.lon;
127 entity.altitude = IC.alt;
128 entity.azimuth = IC.azimuth;
129 entity.elevation = IC.elevation;
130 entity.roll = IC.roll;
131 entity.speed = IC.speed;
132 entity.eda = sm->drag_area;
133 entity.life = sm->life;
134 entity.buoyancy = sm->buoyancy;
135 entity.wind_from_east = IC.wind_from_east;
136 entity.wind_from_north = IC.wind_from_north;
137 entity.wind = sm->wind;
139 entity.mass = IC.mass;
140 entity.aero_stabilised = sm->aero_stabilised;
141 ai->createBallistic( &entity );
143 if (sm->count > 0) (sm->count)--;
149 FGSubmodelMgr::load ()
151 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
155 SGPath config( globals->get_fg_root() );
156 config.append( path->getStringValue() );
159 readProperties(config.str(), &root);
160 } catch (const sg_exception &e) {
161 SG_LOG(SG_GENERAL, SG_ALERT,
162 "Unable to read submodels file: ");
163 cout << config.str() << endl;
168 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
169 vector<SGPropertyNode_ptr>::iterator it = children.begin();
170 vector<SGPropertyNode_ptr>::iterator end = children.end();
171 for (int i = 0; it < end; ++it, i++) {
173 // cout << "Reading submodel " << (*it)->getPath() << endl;
174 submodel* sm = new submodel;
175 SGPropertyNode * entry_node = *it;
176 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
177 sm->name = entry_node->getStringValue("name", "none_defined");
178 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
179 sm->speed = entry_node->getDoubleValue("speed", 2329.4 );
180 sm->repeat = entry_node->getBoolValue ("repeat", false);
181 sm->delay = entry_node->getDoubleValue("delay", 0.25);
182 sm->count = entry_node->getIntValue ("count", 1);
183 sm->slaved = entry_node->getBoolValue ("slaved", false);
184 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
185 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
186 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
187 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
188 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
189 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
190 sm->life = entry_node->getDoubleValue("life", 900.0);
191 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
192 sm->wind = entry_node->getBoolValue ("wind", false);
193 sm->first_time = false;
194 sm->cd = entry_node->getDoubleValue("cd", 0.193);
195 sm->weight = entry_node->getDoubleValue("weight", 0.25);
196 sm->aero_stabilised = entry_node->getBoolValue ("aero-stabilised", true);
197 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), true);
199 sm->trigger->setBoolValue(false);
200 sm->timer = sm->delay;
202 sm->contents = sm->contents_node->getDoubleValue();
204 sm->prop = fgGetNode("/ai/submodels/submodel", i, true);
205 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
207 // sm->prop->tie("contents", SGRawValuePointer<double>(&(sm->contents)));
208 // sm->prop->tie("contents path", SGRawValuePointer<const char *>(&(sm->contents_node)));
209 submodels.push_back( sm );
212 submodel_iterator = submodels.begin();
218 FGSubmodelMgr::transform( submodel* sm)
221 // get initial conditions
223 // get the weight of the contents (lbs) and convert to mass (slugs)
224 sm->contents = sm->contents_node->getDoubleValue();
226 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;;
227 // cout << IC.mass << endl;
229 // set contents to 0 in the parent
230 sm->contents_node->setDoubleValue(0);
232 IC.lat = _user_lat_node->getDoubleValue();
233 IC.lon = _user_lon_node->getDoubleValue();
234 IC.alt = _user_alt_node->getDoubleValue();
235 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
236 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
237 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
239 IC.speed = _user_speed_node->getDoubleValue();
240 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
241 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
243 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
244 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
245 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
248 in[0] = sm->x_offset;
249 in[1] = sm->y_offset;
250 in[2] = sm->z_offset;
253 // pre-process the trig functions
255 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
256 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
257 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
258 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
259 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
260 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
262 // set up the transform matrix
264 trans[0][0] = cosRy * cosRz;
265 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
266 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
268 trans[1][0] = cosRy * sinRz;
269 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
270 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
272 trans[2][0] = -1 * sinRy;
273 trans[2][1] = sinRx * cosRy;
274 trans[2][2] = cosRx * cosRy;
277 // multiply the input and transform matrices
279 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
280 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
281 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
283 // convert ft to degrees of latitude
284 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
286 // convert ft to degrees of longitude
287 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
289 // set submodel initial position
294 // get aircraft velocity vector angles in XZ and XY planes
295 //double alpha = _user_alpha_node->getDoubleValue();
296 //double velXZ = IC.elevation - alpha * cosRx;
297 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
299 // Get submodel initial velocity vector angles in XZ and XY planes.
300 // This needs to be fixed. This vector should be added to aircraft's vector.
301 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
302 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
304 // For now assume vector is close to airplane's vector. This needs to be fixed.
307 // calcuate the total speed north
309 IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
310 cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
312 // calculate the total speed east
314 IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
315 sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
317 // calculate the total speed down
319 IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
322 // re-calculate speed, elevation and azimuth
324 IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north +
325 IC.total_speed_east * IC.total_speed_east +
326 IC.total_speed_down * IC.total_speed_down);
328 IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
330 // rationalise the output
332 if (IC.total_speed_north <= 0){
333 IC.azimuth = 180 + IC.azimuth;
336 if(IC.total_speed_east <= 0){
337 IC.azimuth = 360 + IC.azimuth;
341 IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north *
342 IC.total_speed_north +
343 IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
348 FGSubmodelMgr::updatelat(double lat)
350 double latitude = lat;
351 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
352 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
355 // end of submodel.cxx