]> git.mxchange.org Git - flightgear.git/blob - src/FDM/Balloon.cxx
I tested:
[flightgear.git] / src / FDM / Balloon.cxx
1 /*****************************************************************************
2
3  Module:       BalloonSimInterface.cxx
4  Author:       Christian Mayer
5  Date started: 07.10.99
6  Called by:    
7
8  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 Interface to the hot air balloon simulator
30
31 HISTORY
32 ------------------------------------------------------------------------------
33 01.09.1999 Christian Mayer      Created
34 *****************************************************************************/
35
36 /****************************************************************************/
37 /* INCLUDES                                                                 */
38 /****************************************************************************/
39
40 #include <simgear/compiler.h>
41
42 #ifdef FG_MATH_EXCEPTION_CLASH
43 #  include <math.h>
44 #endif
45
46 #include STL_STRING
47
48 #include <simgear/constants.h>
49 #include <simgear/debug/logstream.hxx>
50 #include <simgear/math/sg_geodesy.hxx>
51 #include <simgear/misc/fgpath.hxx>
52
53 #include <Aircraft/aircraft.hxx>
54 #include <Controls/controls.hxx>
55 #include <Main/globals.hxx>
56
57 #include "Balloon.h"
58
59 /****************************************************************************/
60 /********************************** CODE ************************************/
61 /****************************************************************************/
62
63
64 // Initialize the BalloonSim flight model, dt is the time increment for
65 // each subsequent iteration through the EOM
66 bool FGBalloonSim::init( double dt ) {
67     sgVec3 temp;
68
69     FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing BalloonSim" );
70
71     FG_LOG( FG_FLIGHT, FG_INFO, "  created a balloon" );
72
73     //set the dt of the model
74     current_balloon.set_dt(dt);
75
76     //set position
77     sgSetVec3( temp,
78         get_Latitude(), 
79         get_Longitude(), 
80         get_Altitude() * FEET_TO_METER);
81     current_balloon.setPosition( temp );
82
83     //set Euler angles (?)
84     sgSetVec3( temp,
85         get_Phi(), 
86         get_Theta(), 
87         get_Psi() );
88     current_balloon.setHPR( temp );
89
90     //set velocities
91     sgSetVec3( temp,
92         globals->get_options()->get_uBody(), 
93         globals->get_options()->get_vBody(), 
94         globals->get_options()->get_wBody() );
95     current_balloon.setVelocity( temp );
96
97     FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing BalloonSim" );
98
99     return true;
100 }
101
102
103 // Run an iteration of the EOM (equations of motion)
104 bool FGBalloonSim::update( int multiloop ) {
105     double save_alt = 0.0;
106
107     // lets try to avoid really screwing up the BalloonSim model
108     if ( get_Altitude() < -9000 ) {
109         save_alt = get_Altitude();
110         set_Altitude( 0.0 );
111     }
112
113     // set control positions
114     current_balloon.set_burner_strength ( controls.get_throttle(0) );
115     //not more implemented yet
116
117     // Inform BalloonSim of the local terrain altitude
118     current_balloon.setGroundLevel ( get_Runway_altitude() * FEET_TO_METER);
119
120     // old -- FGInterface_2_JSBsim() not needed except for Init()
121     // translate FG to JSBsim structure
122     // FGInterface_2_JSBsim(f);
123     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
124     // printf("Altitude = %.2f\n", Altitude * 0.3048);
125     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
126
127     /* FDMExec.GetState()->Setsim_time(State->Getsim_time() 
128                                     + State->Getdt() * multiloop); */
129
130     for ( int i = 0; i < multiloop; i++ ) {
131         current_balloon.update(); 
132     }
133
134     // translate BalloonSim back to FG structure so that the
135     // autopilot (and the rest of the sim can use the updated
136     // values
137
138     copy_from_BalloonSim();
139     
140     /*sgVec3 temp, temp2;
141     current_balloon.getPosition( temp );
142     current_balloon.getVelocity( temp2 );
143     FG_LOG( FG_FLIGHT, FG_INFO, "T: " << current_balloon.getTemperature() <<
144                                 " alt: " << temp[2] <<
145                                 " gr_alt: " << get_Runway_altitude() << 
146                                 " burner: " << controls.get_elevator() <<
147                                 " v[2]: " << temp2[2]); */                      
148
149     // but lets restore our original bogus altitude when we are done
150     if ( save_alt < -9000.0 ) {
151         set_Altitude( save_alt );
152     }
153
154     return true;
155 }
156
157
158 // Convert from the FGInterface struct to the BalloonSim
159 bool FGBalloonSim::copy_to_BalloonSim() {
160     return true;
161 }
162
163
164 // Convert from the BalloonSim to the FGInterface struct
165 bool FGBalloonSim::copy_from_BalloonSim() {
166
167     sgVec3 temp;
168
169     // Velocities
170     current_balloon.getVelocity( temp );
171     _set_Velocities_Local( temp[0], temp[1], temp[2] );
172
173     /* ***FIXME*** */ _set_V_equiv_kts( sgLengthVec3 ( temp ) );
174
175     _set_Omega_Body( 0.0, 0.0, 0.0 );
176
177     // Positions
178     current_balloon.getPosition( temp );
179     double lat_geoc = temp[0];
180     double lon      = temp[1];
181     double alt      = temp[2] * METER_TO_FEET;
182
183     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
184     sgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
185                   &lat_geod, &tmp_alt, &sl_radius1 );
186     sgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
187
188     FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
189             << " lat_geoc = " << lat_geoc
190             << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
191             << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
192             << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
193             << " Equator = " << EQUATORIAL_RADIUS_FT );
194             
195     _set_Geocentric_Position( lat_geoc, lon, 
196                                sl_radius2 * METER_TO_FEET + alt );
197     _set_Geodetic_Position( lat_geod, lon, alt );
198
199     current_balloon.getHPR( temp );
200     /* **FIXME*** */ _set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
201     /* **FIXME*** */ _set_Earth_position_angle( 0.0 );
202
203     /* ***FIXME*** */ _set_Runway_altitude( 0.0 );
204
205     _set_sin_lat_geocentric( lat_geoc );
206     _set_cos_lat_geocentric( lat_geoc );
207   
208     _set_sin_cos_longitude( lon );
209     _set_sin_cos_latitude( lat_geod );
210     
211     return true;
212 }
213
214