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 SubmodelSystem::SubmodelSystem ()
19 x_offset = y_offset = 0.0;
24 out[0] = out[1] = out[2] = 0;
28 SubmodelSystem::~SubmodelSystem ()
33 SubmodelSystem::init ()
36 _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
38 _user_lat_node = fgGetNode("/position/latitude-deg", true);
39 _user_lon_node = fgGetNode("/position/longitude-deg", true);
40 _user_alt_node = fgGetNode("/position/altitude-ft", true);
42 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
43 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
44 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
45 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
46 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
48 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
50 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
51 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
53 ai = (FGAIManager*)globals->get_subsystem("ai_model");
59 SubmodelSystem::bind ()
64 SubmodelSystem::unbind ()
66 submodel_iterator = submodels.begin();
67 while(submodel_iterator != submodels.end()) {
68 (*submodel_iterator)->prop->untie("count");
74 SubmodelSystem::update (double dt)
76 if (!(_serviceable_node->getBoolValue())) return;
78 submodel_iterator = submodels.begin();
79 while(submodel_iterator != submodels.end()) {
81 if ((*submodel_iterator)->trigger->getBoolValue()) {
82 if ((*submodel_iterator)->count != 0) {
83 release( (*submodel_iterator), dt);
86 (*submodel_iterator)->first_time = true;
94 SubmodelSystem::release (submodel* sm, double dt)
97 if (sm->timer < sm->delay) return false;
100 if (sm->first_time) {
102 sm->first_time = false;
105 transform(sm); // calculate submodel's initial conditions in world-coordinates
107 FGAIModelEntity entity;
109 entity.path = sm->model.c_str();
110 entity.latitude = IC.lat;
111 entity.longitude = IC.lon;
112 entity.altitude = IC.alt;
113 entity.azimuth = IC.azimuth;
114 entity.elevation = IC.elevation;
115 entity.speed = IC.speed;
116 entity.eda = sm->drag_area;
117 entity.life = sm->life;
118 entity.buoyancy = sm->buoyancy;
119 entity.wind_from_east = IC.wind_from_east;
120 entity.wind_from_north = IC.wind_from_north;
121 entity.wind = sm->wind;
122 ai->createBallistic( &entity );
124 if (sm->count > 0) (sm->count)--;
130 SubmodelSystem::load ()
133 SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
137 SGPath config( globals->get_fg_root() );
138 config.append( path->getStringValue() );
141 readProperties(config.str(), &root);
142 } catch (const sg_exception &e) {
143 SG_LOG(SG_GENERAL, SG_ALERT,
144 "Unable to read submodels file: ");
145 cout << config.str() << endl;
150 int count = root.nChildren();
151 for (i = 0; i < count; i++) {
152 // cout << "Reading submodel " << i << endl;
153 SGPropertyNode *prop;
154 submodel* sm = new submodel;
155 SGPropertyNode * entry_node = root.getChild(i);
156 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
157 sm->name = entry_node->getStringValue("name", "none_defined");
158 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
159 sm->speed = entry_node->getDoubleValue("speed", 0.0);
160 sm->repeat = entry_node->getBoolValue ("repeat", false);
161 sm->delay = entry_node->getDoubleValue("delay", 0.25);
162 sm->count = entry_node->getIntValue ("count", 1);
163 sm->slaved = entry_node->getBoolValue ("slaved", false);
164 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
165 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
166 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
167 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
168 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
169 sm->drag_area = entry_node->getDoubleValue("eda", 0.007);
170 sm->life = entry_node->getDoubleValue("life", 900.0);
171 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
172 sm->wind = entry_node->getBoolValue ("wind", false);
173 sm->first_time = false;
175 sm->trigger->setBoolValue(false);
176 sm->timer = sm->delay;
178 sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
179 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
181 submodels.push_back( sm );
184 submodel_iterator = submodels.begin();
190 SubmodelSystem::transform( submodel* sm)
193 // get initial conditions
195 IC.lat = _user_lat_node->getDoubleValue();
196 IC.lon = _user_lon_node->getDoubleValue();
197 IC.alt = _user_alt_node->getDoubleValue();
198 IC.roll = - _user_roll_node->getDoubleValue(); // rotation about x axis
199 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
200 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
202 IC.speed = _user_speed_node->getDoubleValue();
203 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
204 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
206 in[0] = sm->x_offset;
207 in[1] = sm->y_offset;
208 in[2] = sm->z_offset;
210 // pre-process the trig functions
212 cosRx = cos(IC.roll * SG_DEGREES_TO_RADIANS);
213 sinRx = sin(IC.roll * SG_DEGREES_TO_RADIANS);
214 cosRy = cos(IC.elevation * SG_DEGREES_TO_RADIANS);
215 sinRy = sin(IC.elevation * SG_DEGREES_TO_RADIANS);
216 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
217 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
219 // set up the transform matrix
221 trans[0][0] = cosRy * cosRz;
222 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
223 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
225 trans[1][0] = cosRy * sinRz;
226 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
227 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
229 trans[2][0] = -1 * sinRy;
230 trans[2][1] = sinRx * cosRy;
231 trans[2][2] = cosRx * cosRy;
234 // multiply the input and transform matrices
236 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
237 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
238 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
240 // convert ft to degrees of latitude
241 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
243 // convert ft to degrees of longitude
244 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
246 // set submodel initial position
251 // get aircraft velocity vector angles in XZ and XY planes
252 //double alpha = _user_alpha_node->getDoubleValue();
253 //double velXZ = IC.elevation - alpha * cosRx;
254 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
256 // Get submodel initial velocity vector angles in XZ and XY planes.
257 // This needs to be fixed. This vector should be added to aircraft's vector.
258 IC.elevation += (sm->pitch_offset * cosRx) + (sm->yaw_offset * sinRx);
259 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
261 // For now assume vector is close to airplane's vector. This needs to be fixed.
262 IC.speed += sm->speed;
267 SubmodelSystem::updatelat(double lat)
269 double latitude = lat;
270 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
271 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
274 // end of submodel.cxx