]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
Vivian Meazza:
[flightgear.git] / src / Systems / 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 SubmodelSystem::SubmodelSystem ()
17 {
18     
19   x_offset = y_offset = 0.0;
20   z_offset = -4.0;
21   pitch_offset = 2.0;
22   yaw_offset = 0.0;
23   
24   out[0] = out[1] = out[2] = 0;
25   in[3] = out[3] = 1;
26 }
27
28 SubmodelSystem::~SubmodelSystem ()
29 {
30 }
31
32 void
33 SubmodelSystem::init ()
34 {
35     load();
36     _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
37
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);
41
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);
47
48     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
49
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);
52
53     _user_speed_down_fps_node   = fgGetNode("/velocities/speed-down-fps",true);
54     _user_speed_east_fps_node   = fgGetNode("/velocities/speed-east-fps",true);
55         _user_speed_north_fps_node  = fgGetNode("/velocities/speed-north-fps",true);
56         
57     ai = (FGAIManager*)globals->get_subsystem("ai_model");
58
59
60 }
61
62 void
63 SubmodelSystem::bind ()
64 {
65 }
66
67 void
68 SubmodelSystem::unbind ()
69 {
70   submodel_iterator = submodels.begin();
71   while(submodel_iterator != submodels.end()) {
72     (*submodel_iterator)->prop->untie("count");
73     ++submodel_iterator;
74   }
75 }
76
77 void
78 SubmodelSystem::update (double dt)
79 {
80   if (!(_serviceable_node->getBoolValue())) return;
81   int i=-1;
82   submodel_iterator = submodels.begin();
83   while(submodel_iterator != submodels.end()) {
84     i++;
85     if ((*submodel_iterator)->trigger->getBoolValue()) {
86         if ((*submodel_iterator)->count != 0) {
87           release( (*submodel_iterator), dt);
88         } 
89     } else {
90       (*submodel_iterator)->first_time = true;
91     }  
92     ++submodel_iterator;
93   }
94    
95 }
96
97 bool
98 SubmodelSystem::release (submodel* sm, double dt)
99 {
100   sm->timer += dt;
101   if (sm->timer < sm->delay) return false;
102   sm->timer = 0.0;
103
104   if (sm->first_time) {
105     dt = 0.0;
106     sm->first_time = false;
107   }
108
109   transform(sm);  // calculate submodel's initial conditions in world-coordinates
110
111   FGAIModelEntity entity;
112
113   entity.path = sm->model.c_str();
114   entity.latitude = IC.lat;
115   entity.longitude = IC.lon;
116   entity.altitude = IC.alt;
117   entity.azimuth = IC.azimuth;
118   entity.elevation = IC.elevation;
119   entity.roll = IC.roll;
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 );
128  
129   if (sm->count > 0) (sm->count)--; 
130
131   return true;                    
132 }
133
134 void
135 SubmodelSystem::load ()
136 {
137     int i;
138     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
139     SGPropertyNode root;
140
141     if (path) {
142       SGPath config( globals->get_fg_root() );
143       config.append( path->getStringValue() );
144
145       try {
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;
151         return;
152       }
153     }
154
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;
179
180      sm->trigger->setBoolValue(false);
181      sm->timer = sm->delay;
182
183      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
184      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
185
186      submodels.push_back( sm );
187    }
188
189   submodel_iterator = submodels.begin();
190   
191 }
192
193
194 void
195 SubmodelSystem::transform( submodel* sm) 
196 {
197
198  // get initial conditions 
199     
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 
204   IC.elevation =  _user_pitch_node->getDoubleValue();   // rotation about y axis 
205   IC.azimuth =    _user_heading_node->getDoubleValue(); // rotation about z axis
206   
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();
210   
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();
214
215   
216   in[0] = sm->x_offset;
217   in[1] = sm->y_offset;
218   in[2] = sm->z_offset; 
219
220 // pre-process the trig functions
221
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);
228
229 // set up the transform matrix
230
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;
234
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;
238
239     trans[2][0] =  -1 * sinRy;
240     trans[2][1] =  sinRx * cosRy;
241     trans[2][2] =  cosRx * cosRy;
242
243
244 // multiply the input and transform matrices
245
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];
249
250    // convert ft to degrees of latitude
251    out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
252
253    // convert ft to degrees of longitude
254    out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
255
256    // set submodel initial position
257    IC.lat += out[0];
258    IC.lon += out[1];
259    IC.alt += out[2];
260
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);
265    
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);
270  
271    // For now assume vector is close to airplane's vector.  This needs to be fixed.
272    //IC.speed += ; 
273
274    // calcuate the total speed north
275
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;
278
279    // calculate the total speed east
280
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;
283
284    // calculate the total speed down
285   
286    IC.total_speed_down = sm->speed * -sin(IC.elevation*SG_DEGREES_TO_RADIANS) +
287                         IC.speed_down_fps;
288
289   // re-calculate speed, elevation and azimuth
290
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);
294                           
295    IC.azimuth = atan(IC.total_speed_east/IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
296    
297    // rationalise the output
298    
299    if (IC.total_speed_north <= 0){
300     IC.azimuth = 180 + IC.azimuth;
301         }
302    else{
303      if(IC.total_speed_east <= 0){
304      IC.azimuth = 360 + IC.azimuth;
305          }
306   }
307          
308     IC.elevation = -atan(IC.total_speed_down/sqrt(IC.total_speed_north * 
309                        IC.total_speed_north + 
310                        IC.total_speed_east * IC.total_speed_east)) * SG_RADIANS_TO_DEGREES;
311
312 }
313
314 void 
315 SubmodelSystem::updatelat(double lat) 
316 {
317     double latitude = lat;
318     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
319     ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
320 }
321
322 // end of submodel.cxx
323
324
325