2 // submodel.cxx - models a releasable submodel.
3 // Written by Dave Culp, started Aug 2004
5 // This file is in the Public Domain and comes with no warranty.
7 #include "submodel.hxx"
9 #include <simgear/structure/exception.hxx>
10 #include <simgear/misc/sg_path.hxx>
12 #include <Main/fg_props.hxx>
13 #include <Main/util.hxx>
14 #include <AIModel/AIManager.hxx>
17 SubmodelSystem::SubmodelSystem ()
20 x_offset = y_offset = 0.0;
25 out[0] = out[1] = out[2] = 0;
29 SubmodelSystem::~SubmodelSystem ()
34 SubmodelSystem::init ()
37 _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
39 _user_lat_node = fgGetNode("/position/latitude-deg", true);
40 _user_lon_node = fgGetNode("/position/longitude-deg", true);
41 _user_alt_node = fgGetNode("/position/altitude-ft", true);
43 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
44 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
45 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
46 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
47 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
49 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
51 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
52 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
54 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps",true);
55 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps",true);
56 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps",true);
58 ai = (FGAIManager*)globals->get_subsystem("ai_model");
64 SubmodelSystem::bind ()
69 SubmodelSystem::unbind ()
71 submodel_iterator = submodels.begin();
72 while(submodel_iterator != submodels.end()) {
73 (*submodel_iterator)->prop->untie("count");
79 SubmodelSystem::update (double dt)
81 if (!(_serviceable_node->getBoolValue())) return;
83 submodel_iterator = submodels.begin();
84 while(submodel_iterator != submodels.end()) {
86 if ((*submodel_iterator)->trigger->getBoolValue()) {
87 if ((*submodel_iterator)->count != 0) {
88 release( (*submodel_iterator), dt);
91 (*submodel_iterator)->first_time = true;
99 SubmodelSystem::release (submodel* sm, double dt)
102 if (sm->timer < sm->delay) return false;
105 if (sm->first_time) {
107 sm->first_time = false;
110 transform(sm); // calculate submodel's initial conditions in world-coordinates
112 FGAIModelEntity entity;
114 entity.path = sm->model.c_str();
115 entity.latitude = IC.lat;
116 entity.longitude = IC.lon;
117 entity.altitude = IC.alt;
118 entity.azimuth = IC.azimuth;
119 entity.elevation = IC.elevation;
120 entity.speed = IC.speed;
121 entity.eda = sm->drag_area;
122 entity.life = sm->life;
123 entity.buoyancy = sm->buoyancy;
124 entity.wind_from_east = IC.wind_from_east;
125 entity.wind_from_north = IC.wind_from_north;
126 entity.wind = sm->wind;
127 ai->createBallistic( &entity );
129 if (sm->count > 0) (sm->count)--;
135 SubmodelSystem::load ()
138 SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
142 SGPath config( globals->get_fg_root() );
143 config.append( path->getStringValue() );
146 readProperties(config.str(), &root);
147 } catch (const sg_exception &e) {
148 SG_LOG(SG_GENERAL, SG_ALERT,
149 "Unable to read submodels file: ");
150 cout << config.str() << endl;
155 int count = root.nChildren();
156 for (i = 0; i < count; i++) {
157 // cout << "Reading submodel " << i << endl;
158 SGPropertyNode *prop;
159 submodel* sm = new submodel;
160 SGPropertyNode * entry_node = root.getChild(i);
161 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
162 sm->name = entry_node->getStringValue("name", "none_defined");
163 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
164 sm->speed = entry_node->getDoubleValue("speed", 0.0);
165 sm->repeat = entry_node->getBoolValue ("repeat", false);
166 sm->delay = entry_node->getDoubleValue("delay", 0.25);
167 sm->count = entry_node->getIntValue ("count", 1);
168 sm->slaved = entry_node->getBoolValue ("slaved", false);
169 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
170 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
171 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
172 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
173 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
174 sm->drag_area = entry_node->getDoubleValue("eda", 0.007);
175 sm->life = entry_node->getDoubleValue("life", 900.0);
176 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
177 sm->wind = entry_node->getBoolValue ("wind", false);
178 sm->first_time = false;
180 sm->trigger->setBoolValue(false);
181 sm->timer = sm->delay;
183 sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
184 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
186 submodels.push_back( sm );
189 submodel_iterator = submodels.begin();
195 SubmodelSystem::transform( submodel* sm)
198 // get initial conditions
200 IC.lat = _user_lat_node->getDoubleValue();
201 IC.lon = _user_lon_node->getDoubleValue();
202 IC.alt = _user_alt_node->getDoubleValue();
203 IC.roll = - _user_roll_node->getDoubleValue(); // rotation about x axis (-ve)
204 IC.elevation = - _user_pitch_node->getDoubleValue(); // rotation about y axis (-ve)
205 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
207 IC.speed = _user_speed_node->getDoubleValue();
208 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
209 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
211 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
212 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
213 IC.speed_north_fps = _user_speed_north_fps_node ->getDoubleValue();
216 in[0] = sm->x_offset;
217 in[1] = sm->y_offset;
218 in[2] = sm->z_offset;
220 // pre-process the trig functions
222 cosRx = cos(IC.roll * SG_DEGREES_TO_RADIANS);
223 sinRx = sin(IC.roll * SG_DEGREES_TO_RADIANS);
224 cosRy = cos(IC.elevation * SG_DEGREES_TO_RADIANS);
225 sinRy = sin(IC.elevation * SG_DEGREES_TO_RADIANS);
226 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
227 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
229 // set up the transform matrix
231 trans[0][0] = cosRy * cosRz;
232 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
233 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
235 trans[1][0] = cosRy * sinRz;
236 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
237 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
239 trans[2][0] = -1 * sinRy;
240 trans[2][1] = sinRx * cosRy;
241 trans[2][2] = cosRx * cosRy;
244 // multiply the input and transform matrices
246 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
247 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
248 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
250 // convert ft to degrees of latitude
251 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
253 // convert ft to degrees of longitude
254 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
256 // set submodel initial position
261 // get aircraft velocity vector angles in XZ and XY planes
262 //double alpha = _user_alpha_node->getDoubleValue();
263 //double velXZ = IC.elevation - alpha * cosRx;
264 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
266 // Get submodel initial velocity vector angles in XZ and XY planes.
267 // This needs to be fixed. This vector should be added to aircraft's vector.
268 IC.elevation += (sm->yaw_offset * sinRx) - (sm->pitch_offset * cosRx);
269 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
271 // For now assume vector is close to airplane's vector. This needs to be fixed.
274 // calcuate the total speed north
276 IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
277 cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
279 // calculate the total speed east
281 IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
282 sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
284 // calculate the total speed down
286 IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
289 // re-calculate speed, elevation and azimuth
291 IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north +
292 IC.total_speed_east * IC.total_speed_east +
293 IC.total_speed_down * IC.total_speed_down);
295 IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
297 // rationalise the output
299 if (IC.total_speed_north <= 0){
300 IC.azimuth = 180 + IC.azimuth;
303 if(IC.total_speed_east <= 0){
304 IC.azimuth = 360 + IC.azimuth;
310 IC.elevation = atan(IC.total_speed_down/sqrt(IC.total_speed_north * IC.total_speed_north +
311 IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
316 SubmodelSystem::updatelat(double lat)
318 double latitude = lat;
319 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
320 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
323 // end of submodel.cxx