]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/submodel.cxx
Melchior FRANZ:
[flightgear.git] / src / AIModel / submodel.cxx
1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include "submodel.hxx"
7
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
10
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
14
15
16 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
17
18 FGSubmodelMgr::FGSubmodelMgr ()
19 {
20     
21   x_offset = y_offset = 0.0;
22   z_offset = -4.0;
23   pitch_offset = 2.0;
24   yaw_offset = 0.0;
25   
26   out[0] = out[1] = out[2] = 0;
27   in[3] = out[3] = 1;
28   string contents_node;
29   contrail_altitude = 30000.0;
30 }
31
32 FGSubmodelMgr::~FGSubmodelMgr ()
33 {
34 }
35
36 void
37 FGSubmodelMgr::init ()
38 {
39     load();
40     _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
41
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);
45
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);
51
52     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
53
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);
56
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);
60
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);
65
66     ai = (FGAIManager*)globals->get_subsystem("ai_model");
67
68 }
69
70 void
71 FGSubmodelMgr::bind ()
72 {
73 }
74
75 void
76 FGSubmodelMgr::unbind ()
77 {
78   submodel_iterator = submodels.begin();
79   while(submodel_iterator != submodels.end()) {
80     (*submodel_iterator)->prop->untie("count");
81     ++submodel_iterator;
82   }
83 }
84
85 void
86 FGSubmodelMgr::update (double dt)
87 {
88   if (!(_serviceable_node->getBoolValue())) return;
89   int i=-1;
90
91   if (_user_alt_node->getDoubleValue() > contrail_altitude) {
92      _contrail_trigger->setBoolValue(true);
93   }   
94
95   submodel_iterator = submodels.begin();
96   while(submodel_iterator != submodels.end()) {
97     i++;
98     if ((*submodel_iterator)->trigger->getBoolValue()) {
99         if ((*submodel_iterator)->count != 0) {
100           release( (*submodel_iterator), dt);
101         } 
102     } else {
103       (*submodel_iterator)->first_time = true;
104     }  
105     ++submodel_iterator;
106   }
107    
108 }
109
110 bool
111 FGSubmodelMgr::release (submodel* sm, double dt)
112 {
113   sm->timer += dt;
114   if (sm->timer < sm->delay) return false;
115   sm->timer = 0.0;
116
117   if (sm->first_time) {
118     dt = 0.0;
119     sm->first_time = false;
120   }
121
122   transform(sm);  // calculate submodel's initial conditions in world-coordinates
123
124   FGAIModelEntity entity;
125
126   entity.path = sm->model.c_str();
127   entity.latitude = IC.lat;
128   entity.longitude = IC.lon;
129   entity.altitude = IC.alt;
130   entity.azimuth = IC.azimuth;
131   entity.elevation = IC.elevation;
132   entity.roll = IC.roll;
133   entity.speed = IC.speed;
134   entity.eda = sm->drag_area;
135   entity.life = sm->life;
136   entity.buoyancy = sm->buoyancy;
137   entity.wind_from_east = IC.wind_from_east;
138   entity.wind_from_north = IC.wind_from_north;
139   entity.wind = sm->wind;
140   entity.cd = sm->cd;
141   entity.mass = IC.mass;
142   entity.aero_stabilised = sm->aero_stabilised;
143   ai->createBallistic( &entity );
144  
145   if (sm->count > 0) (sm->count)--; 
146
147   return true;                    
148 }
149
150 void
151 FGSubmodelMgr::load ()
152 {
153
154     int i;
155     SGPropertyNode *path = fgGetNode("/sim/submodels/path");
156     SGPropertyNode root;
157
158     if (path) {
159       SGPath config( globals->get_fg_root() );
160       config.append( path->getStringValue() );
161
162       try {
163         readProperties(config.str(), &root);
164       } catch (const sg_exception &e) {
165         SG_LOG(SG_GENERAL, SG_ALERT,
166         "Unable to read submodels file: ");
167         cout << config.str() << endl;
168         return;
169       }
170     }
171
172    int count = root.nChildren();
173    for (i = 0; i < count; i++) { 
174      // cout << "Reading submodel " << i << endl;        
175      SGPropertyNode *prop;
176      submodel* sm = new submodel;
177      SGPropertyNode * entry_node = root.getChild(i);
178      sm->trigger        = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
179      sm->name           = entry_node->getStringValue("name", "none_defined");
180      sm->model          = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
181      sm->speed          = entry_node->getDoubleValue("speed", 2329.4 );
182      sm->repeat         = entry_node->getBoolValue  ("repeat", false); 
183      sm->delay          = entry_node->getDoubleValue("delay", 0.25); 
184      sm->count          = entry_node->getIntValue   ("count", 1); 
185      sm->slaved         = entry_node->getBoolValue  ("slaved", false); 
186      sm->x_offset       = entry_node->getDoubleValue("x-offset", 0.0); 
187      sm->y_offset       = entry_node->getDoubleValue("y-offset", 0.0); 
188      sm->z_offset       = entry_node->getDoubleValue("z-offset", 0.0); 
189      sm->yaw_offset     = entry_node->getDoubleValue("yaw-offset", 0.0); 
190      sm->pitch_offset   = entry_node->getDoubleValue("pitch-offset", 0.0);
191      sm->drag_area      = entry_node->getDoubleValue("eda", 0.034);
192      sm->life           = entry_node->getDoubleValue("life", 900.0);
193      sm->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
194      sm->wind           = entry_node->getBoolValue  ("wind", false); 
195      sm->first_time     = false;
196      sm->cd             = entry_node->getDoubleValue("cd", 0.193);
197      sm->weight         = entry_node->getDoubleValue("weight", 0.25);
198      sm->aero_stabilised = entry_node->getBoolValue  ("aero-stabilised", true);
199      sm->contents_node  = fgGetNode(entry_node->getStringValue("contents", "none"), true);
200
201      sm->trigger->setBoolValue(false);
202      sm->timer = sm->delay;
203  
204      sm->contents = sm->contents_node->getDoubleValue();
205  
206      sm->prop = fgGetNode("/ai/submodels/submodel", i, true);
207      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
208
209 //   sm->prop->tie("contents", SGRawValuePointer<double>(&(sm->contents)));
210 //   sm->prop->tie("contents path", SGRawValuePointer<const char *>(&(sm->contents_node)));
211      submodels.push_back( sm );
212    }
213
214   submodel_iterator = submodels.begin();
215   
216 }
217
218
219 void
220 FGSubmodelMgr::transform( submodel* sm) 
221 {
222
223 // get initial conditions 
224  
225 // get the weight of the contents (lbs) and convert to mass (slugs)
226   sm->contents = sm->contents_node->getDoubleValue();
227   
228   IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;;
229 //  cout << IC.mass << endl;
230     
231 // set contents to 0 in the parent
232    sm->contents_node->setDoubleValue(0); 
233     
234   IC.lat =        _user_lat_node->getDoubleValue();    
235   IC.lon =        _user_lon_node->getDoubleValue();   
236   IC.alt =        _user_alt_node->getDoubleValue();    
237   IC.roll =       _user_roll_node->getDoubleValue();    // rotation about x axis 
238   IC.elevation =  _user_pitch_node->getDoubleValue();   // rotation about y axis 
239   IC.azimuth =    _user_heading_node->getDoubleValue(); // rotation about z axis
240   
241   IC.speed =           _user_speed_node->getDoubleValue();
242   IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
243   IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
244   
245   IC.speed_down_fps =   _user_speed_down_fps_node->getDoubleValue();
246   IC.speed_east_fps =   _user_speed_east_fps_node->getDoubleValue();
247   IC.speed_north_fps =  _user_speed_north_fps_node->getDoubleValue();
248
249   
250   in[0] = sm->x_offset;
251   in[1] = sm->y_offset;
252   in[2] = sm->z_offset; 
253  
254   
255 // pre-process the trig functions
256
257     cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
258     sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
259     cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
260     sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
261     cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
262     sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
263
264 // set up the transform matrix
265
266     trans[0][0] =  cosRy * cosRz;
267     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
268     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
269
270     trans[1][0] =  cosRy * sinRz;
271     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
272     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
273
274     trans[2][0] =  -1 * sinRy;
275     trans[2][1] =  sinRx * cosRy;
276     trans[2][2] =  cosRx * cosRy;
277
278
279 // multiply the input and transform matrices
280
281    out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
282    out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
283    out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
284
285    // convert ft to degrees of latitude
286    out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
287
288    // convert ft to degrees of longitude
289    out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
290
291    // set submodel initial position
292    IC.lat += out[0];
293    IC.lon += out[1];
294    IC.alt += out[2];
295
296    // get aircraft velocity vector angles in XZ and XY planes
297     //double alpha = _user_alpha_node->getDoubleValue();
298     //double velXZ = IC.elevation - alpha * cosRx;
299     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
300    
301    // Get submodel initial velocity vector angles in XZ and XY planes.
302    // This needs to be fixed. This vector should be added to aircraft's vector. 
303    IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
304    IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
305  
306    // For now assume vector is close to airplane's vector.  This needs to be fixed.
307    //IC.speed += ; 
308
309    // calcuate the total speed north
310
311    IC.total_speed_north = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
312                     cos(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
313
314    // calculate the total speed east
315
316    IC.total_speed_east = sm->speed * cos(IC.elevation*SG_DEGREES_TO_RADIANS)*
317                      sin(IC.azimuth*SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
318
319    // calculate the total speed down
320   
321    IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
322                         IC.speed_down_fps;
323
324   // re-calculate speed, elevation and azimuth
325
326    IC.speed = sqrt( IC.total_speed_north * IC.total_speed_north + 
327                     IC.total_speed_east * IC.total_speed_east +
328                     IC.total_speed_down * IC.total_speed_down);
329
330    IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
331    
332    // rationalise the output
333    
334    if (IC.total_speed_north <= 0){
335     IC.azimuth = 180 + IC.azimuth;
336    }
337    else{
338      if(IC.total_speed_east <= 0){
339      IC.azimuth = 360 + IC.azimuth;
340    }
341   }
342  
343     IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north * 
344                        IC.total_speed_north + 
345                        IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
346
347 }
348
349 void 
350 FGSubmodelMgr::updatelat(double lat) 
351 {
352     double latitude = lat;
353     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
354     ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
355 }
356
357 // end of submodel.cxx
358
359
360
361