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;
30 SubmodelSystem::~SubmodelSystem ()
35 SubmodelSystem::init ()
38 _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
40 _user_lat_node = fgGetNode("/position/latitude-deg", true);
41 _user_lon_node = fgGetNode("/position/longitude-deg", true);
42 _user_alt_node = fgGetNode("/position/altitude-ft", true);
44 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
45 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
46 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
47 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
48 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
50 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
52 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
53 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
55 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps",true);
56 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps",true);
57 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps",true);
59 ai = (FGAIManager*)globals->get_subsystem("ai_model");
65 SubmodelSystem::bind ()
70 SubmodelSystem::unbind ()
72 submodel_iterator = submodels.begin();
73 while(submodel_iterator != submodels.end()) {
74 (*submodel_iterator)->prop->untie("count");
80 SubmodelSystem::update (double dt)
82 if (!(_serviceable_node->getBoolValue())) return;
84 submodel_iterator = submodels.begin();
85 while(submodel_iterator != submodels.end()) {
87 if ((*submodel_iterator)->trigger->getBoolValue()) {
88 if ((*submodel_iterator)->count != 0) {
89 release( (*submodel_iterator), dt);
92 (*submodel_iterator)->first_time = true;
100 SubmodelSystem::release (submodel* sm, double dt)
103 if (sm->timer < sm->delay) return false;
106 if (sm->first_time) {
108 sm->first_time = false;
111 transform(sm); // calculate submodel's initial conditions in world-coordinates
113 FGAIModelEntity entity;
115 entity.path = sm->model.c_str();
116 entity.latitude = IC.lat;
117 entity.longitude = IC.lon;
118 entity.altitude = IC.alt;
119 entity.azimuth = IC.azimuth;
120 entity.elevation = IC.elevation;
121 entity.roll = IC.roll;
122 entity.speed = IC.speed;
123 entity.eda = sm->drag_area;
124 entity.life = sm->life;
125 entity.buoyancy = sm->buoyancy;
126 entity.wind_from_east = IC.wind_from_east;
127 entity.wind_from_north = IC.wind_from_north;
128 entity.wind = sm->wind;
130 entity.weight = sm->weight;
131 ai->createBallistic( &entity );
133 if (sm->count > 0) (sm->count)--;
139 SubmodelSystem::load ()
142 SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
146 SGPath config( globals->get_fg_root() );
147 config.append( path->getStringValue() );
150 readProperties(config.str(), &root);
151 } catch (const sg_exception &e) {
152 SG_LOG(SG_GENERAL, SG_ALERT,
153 "Unable to read submodels file: ");
154 cout << config.str() << endl;
159 int count = root.nChildren();
160 for (i = 0; i < count; i++) {
161 // cout << "Reading submodel " << i << endl;
162 SGPropertyNode *prop;
163 submodel* sm = new submodel;
164 SGPropertyNode * entry_node = root.getChild(i);
165 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
166 sm->name = entry_node->getStringValue("name", "none_defined");
167 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
168 sm->speed = entry_node->getDoubleValue("speed", 2329.4 );
169 sm->repeat = entry_node->getBoolValue ("repeat", false);
170 sm->delay = entry_node->getDoubleValue("delay", 0.25);
171 sm->count = entry_node->getIntValue ("count", 1);
172 sm->slaved = entry_node->getBoolValue ("slaved", false);
173 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
174 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
175 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
176 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
177 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
178 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
179 sm->life = entry_node->getDoubleValue("life", 900.0);
180 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
181 sm->wind = entry_node->getBoolValue ("wind", false);
182 sm->first_time = false;
183 sm->cd = entry_node->getDoubleValue("cd", 0.295);
184 sm->weight = entry_node->getDoubleValue("weight", 0.25);
186 sm->trigger->setBoolValue(false);
187 sm->timer = sm->delay;
189 sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
190 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
192 submodels.push_back( sm );
195 submodel_iterator = submodels.begin();
201 SubmodelSystem::transform( submodel* sm)
204 // get initial conditions
206 IC.lat = _user_lat_node->getDoubleValue();
207 IC.lon = _user_lon_node->getDoubleValue();
208 IC.alt = _user_alt_node->getDoubleValue();
209 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
210 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
211 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
213 IC.speed = _user_speed_node->getDoubleValue();
214 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
215 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
217 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
218 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
219 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
222 in[0] = sm->x_offset;
223 in[1] = sm->y_offset;
224 in[2] = sm->z_offset;
226 IC.mass = sm->weight * lbs_to_slugs;
228 // pre-process the trig functions
230 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
231 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
232 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
233 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
234 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
235 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
237 // set up the transform matrix
239 trans[0][0] = cosRy * cosRz;
240 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
241 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
243 trans[1][0] = cosRy * sinRz;
244 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
245 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
247 trans[2][0] = -1 * sinRy;
248 trans[2][1] = sinRx * cosRy;
249 trans[2][2] = cosRx * cosRy;
252 // multiply the input and transform matrices
254 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
255 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
256 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
258 // convert ft to degrees of latitude
259 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
261 // convert ft to degrees of longitude
262 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
264 // set submodel initial position
269 // get aircraft velocity vector angles in XZ and XY planes
270 //double alpha = _user_alpha_node->getDoubleValue();
271 //double velXZ = IC.elevation - alpha * cosRx;
272 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
274 // Get submodel initial velocity vector angles in XZ and XY planes.
275 // This needs to be fixed. This vector should be added to aircraft's vector.
276 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
277 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
279 // For now assume vector is close to airplane's vector. This needs to be fixed.
282 // calcuate the total speed north
284 IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
285 cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
287 // calculate the total speed east
289 IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
290 sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
292 // calculate the total speed down
294 IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
297 // re-calculate speed, elevation and azimuth
299 IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north +
300 IC.total_speed_east * IC.total_speed_east +
301 IC.total_speed_down * IC.total_speed_down);
303 IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
305 // rationalise the output
307 if (IC.total_speed_north <= 0){
308 IC.azimuth = 180 + IC.azimuth;
311 if(IC.total_speed_east <= 0){
312 IC.azimuth = 360 + IC.azimuth;
316 IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north *
317 IC.total_speed_north +
318 IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
323 SubmodelSystem::updatelat(double lat)
325 double latitude = lat;
326 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
327 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
330 // end of submodel.cxx