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 SubmodelSystem::lbs_to_slugs = 0.031080950172;
18 SubmodelSystem::SubmodelSystem ()
21 x_offset = y_offset = 0.0;
26 out[0] = out[1] = out[2] = 0;
31 SubmodelSystem::~SubmodelSystem ()
36 SubmodelSystem::init ()
39 _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
41 _user_lat_node = fgGetNode("/position/latitude-deg", true);
42 _user_lon_node = fgGetNode("/position/longitude-deg", true);
43 _user_alt_node = fgGetNode("/position/altitude-ft", true);
45 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
46 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
47 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
48 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
49 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
51 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
53 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
54 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
56 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps",true);
57 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps",true);
58 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps",true);
60 ai = (FGAIManager*)globals->get_subsystem("ai_model");
66 SubmodelSystem::bind ()
71 SubmodelSystem::unbind ()
73 submodel_iterator = submodels.begin();
74 while(submodel_iterator != submodels.end()) {
75 (*submodel_iterator)->prop->untie("count");
81 SubmodelSystem::update (double dt)
83 if (!(_serviceable_node->getBoolValue())) return;
85 submodel_iterator = submodels.begin();
86 while(submodel_iterator != submodels.end()) {
88 if ((*submodel_iterator)->trigger->getBoolValue()) {
89 if ((*submodel_iterator)->count != 0) {
90 release( (*submodel_iterator), dt);
93 (*submodel_iterator)->first_time = true;
101 SubmodelSystem::release (submodel* sm, double dt)
104 if (sm->timer < sm->delay) return false;
107 if (sm->first_time) {
109 sm->first_time = false;
112 transform(sm); // calculate submodel's initial conditions in world-coordinates
114 FGAIModelEntity entity;
116 entity.path = sm->model.c_str();
117 entity.latitude = IC.lat;
118 entity.longitude = IC.lon;
119 entity.altitude = IC.alt;
120 entity.azimuth = IC.azimuth;
121 entity.elevation = IC.elevation;
122 entity.roll = IC.roll;
123 entity.speed = IC.speed;
124 entity.eda = sm->drag_area;
125 entity.life = sm->life;
126 entity.buoyancy = sm->buoyancy;
127 entity.wind_from_east = IC.wind_from_east;
128 entity.wind_from_north = IC.wind_from_north;
129 entity.wind = sm->wind;
131 entity.mass = IC.mass;
132 ai->createBallistic( &entity );
134 if (sm->count > 0) (sm->count)--;
140 SubmodelSystem::load ()
144 SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
148 SGPath config( globals->get_fg_root() );
149 config.append( path->getStringValue() );
152 readProperties(config.str(), &root);
153 } catch (const sg_exception &e) {
154 SG_LOG(SG_GENERAL, SG_ALERT,
155 "Unable to read submodels file: ");
156 cout << config.str() << endl;
161 int count = root.nChildren();
162 for (i = 0; i < count; i++) {
163 // cout << "Reading submodel " << i << endl;
164 SGPropertyNode *prop;
165 submodel* sm = new submodel;
166 SGPropertyNode * entry_node = root.getChild(i);
167 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
168 sm->name = entry_node->getStringValue("name", "none_defined");
169 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
170 sm->speed = entry_node->getDoubleValue("speed", 2329.4 );
171 sm->repeat = entry_node->getBoolValue ("repeat", false);
172 sm->delay = entry_node->getDoubleValue("delay", 0.25);
173 sm->count = entry_node->getIntValue ("count", 1);
174 sm->slaved = entry_node->getBoolValue ("slaved", false);
175 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
176 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
177 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
178 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
179 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
180 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
181 sm->life = entry_node->getDoubleValue("life", 900.0);
182 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
183 sm->wind = entry_node->getBoolValue ("wind", false);
184 sm->first_time = false;
185 sm->cd = entry_node->getDoubleValue("cd", 0.193);
186 sm->weight = entry_node->getDoubleValue("weight", 0.25);
187 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), true);
189 sm->trigger->setBoolValue(false);
190 sm->timer = sm->delay;
192 sm->contents = sm->contents_node->getDoubleValue();
194 sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
195 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
197 // sm->prop->tie("contents", SGRawValuePointer<double>(&(sm->contents)));
198 // sm->prop->tie("contents path", SGRawValuePointer<const char *>(&(sm->contents_node)));
199 submodels.push_back( sm );
202 submodel_iterator = submodels.begin();
208 SubmodelSystem::transform( submodel* sm)
211 // get initial conditions
213 // get the weight of the contents (lbs) and convert to mass (slugs)
214 sm->contents = sm->contents_node->getDoubleValue();
216 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;;
217 // cout << IC.mass << endl;
219 // set contents to 0 in the parent
220 sm->contents_node->setDoubleValue(0);
222 IC.lat = _user_lat_node->getDoubleValue();
223 IC.lon = _user_lon_node->getDoubleValue();
224 IC.alt = _user_alt_node->getDoubleValue();
225 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
226 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
227 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
229 IC.speed = _user_speed_node->getDoubleValue();
230 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
231 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
233 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
234 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
235 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
238 in[0] = sm->x_offset;
239 in[1] = sm->y_offset;
240 in[2] = sm->z_offset;
243 // pre-process the trig functions
245 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
246 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
247 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
248 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
249 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
250 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
252 // set up the transform matrix
254 trans[0][0] = cosRy * cosRz;
255 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
256 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
258 trans[1][0] = cosRy * sinRz;
259 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
260 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
262 trans[2][0] = -1 * sinRy;
263 trans[2][1] = sinRx * cosRy;
264 trans[2][2] = cosRx * cosRy;
267 // multiply the input and transform matrices
269 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
270 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
271 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
273 // convert ft to degrees of latitude
274 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
276 // convert ft to degrees of longitude
277 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
279 // set submodel initial position
284 // get aircraft velocity vector angles in XZ and XY planes
285 //double alpha = _user_alpha_node->getDoubleValue();
286 //double velXZ = IC.elevation - alpha * cosRx;
287 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
289 // Get submodel initial velocity vector angles in XZ and XY planes.
290 // This needs to be fixed. This vector should be added to aircraft's vector.
291 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
292 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
294 // For now assume vector is close to airplane's vector. This needs to be fixed.
297 // calcuate the total speed north
299 IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
300 cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
302 // calculate the total speed east
304 IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
305 sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
307 // calculate the total speed down
309 IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
312 // re-calculate speed, elevation and azimuth
314 IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north +
315 IC.total_speed_east * IC.total_speed_east +
316 IC.total_speed_down * IC.total_speed_down);
318 IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
320 // rationalise the output
322 if (IC.total_speed_north <= 0){
323 IC.azimuth = 180 + IC.azimuth;
326 if(IC.total_speed_east <= 0){
327 IC.azimuth = 360 + IC.azimuth;
331 IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north *
332 IC.total_speed_north +
333 IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
338 SubmodelSystem::updatelat(double lat)
340 double latitude = lat;
341 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
342 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
345 // end of submodel.cxx