]> git.mxchange.org Git - flightgear.git/blob - src/FDM/Balloon.cxx
Sync. w. JSB CVS as of 15/01/2007
[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
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #ifdef HAVE_CONFIG_H
41 #  include <config.h>
42 #endif
43
44 #include <simgear/compiler.h>
45
46 #ifdef SG_MATH_EXCEPTION_CLASH
47 #  include <math.h>
48 #endif
49
50 #include STL_STRING
51
52 #include <simgear/constants.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/math/sg_geodesy.hxx>
55 #include <simgear/misc/sg_path.hxx>
56
57 #include <Aircraft/aircraft.hxx>
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60
61 #include "Balloon.h"
62
63 /****************************************************************************/
64 /********************************** CODE ************************************/
65 /****************************************************************************/
66
67
68 FGBalloonSim::FGBalloonSim( double dt ) {
69     //set the dt of the model
70     current_balloon.set_dt(dt);
71 }
72
73
74 FGBalloonSim::~FGBalloonSim() {
75 }
76
77
78 // Initialize the BalloonSim flight model, dt is the time increment for
79 // each subsequent iteration through the EOM
80 void FGBalloonSim::init() {
81                 
82     //do init common to all the FDM's           
83     common_init();
84     
85     //now do init specific to the Balloon
86
87     sgVec3 temp;
88
89     SG_LOG( SG_FLIGHT, SG_INFO, "Starting initializing BalloonSim" );
90
91     SG_LOG( SG_FLIGHT, SG_INFO, "  created a balloon" );
92
93     //set position
94     sgSetVec3( temp,
95         get_Latitude(), 
96         get_Longitude(), 
97         get_Altitude() * SG_FEET_TO_METER);
98     current_balloon.setPosition( temp );
99
100     //set Euler angles (?)
101     sgSetVec3( temp,
102         get_Phi(), 
103         get_Theta(), 
104         get_Psi() );
105     current_balloon.setHPR( temp );
106
107     //set velocities
108     sgSetVec3( temp,
109                fgGetDouble("/sim/presets/uBody-fps"),
110                fgGetDouble("/sim/presets/vBody-fps"),
111                fgGetDouble("/sim/presets/wBody-fps") );
112     current_balloon.setVelocity( temp );
113
114     SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing BalloonSim" );
115 }
116
117
118 // Run an iteration of the EOM (equations of motion)
119 void FGBalloonSim::update( double dt ) {
120     double save_alt = 0.0;
121
122     if (is_suspended())
123       return;
124
125     int multiloop = _calc_multiloop(dt);
126
127     // lets try to avoid really screwing up the BalloonSim model
128     if ( get_Altitude() < -9000 ) {
129         save_alt = get_Altitude();
130         set_Altitude( 0.0 );
131     }
132
133     // set control positions
134     current_balloon.set_burner_strength ( globals->get_controls()->get_throttle(0) );
135     //not more implemented yet
136
137     // Inform BalloonSim of the local terrain altitude
138     current_balloon.setGroundLevel ( get_Runway_altitude() * SG_FEET_TO_METER);
139
140     // old -- FGInterface_2_JSBsim() not needed except for Init()
141     // translate FG to JSBsim structure
142     // FGInterface_2_JSBsim(f);
143     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
144     // printf("Altitude = %.2f\n", Altitude * 0.3048);
145     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
146
147     /* FDMExec.GetState()->Setsim_time(State->Getsim_time() 
148                                     + State->Getdt() * multiloop); */
149
150     for ( int i = 0; i < multiloop; i++ ) {
151         current_balloon.update(); 
152     }
153
154     // translate BalloonSim back to FG structure so that the
155     // autopilot (and the rest of the sim can use the updated
156     // values
157
158     copy_from_BalloonSim();
159     
160     /*sgVec3 temp, temp2;
161     current_balloon.getPosition( temp );
162     current_balloon.getVelocity( temp2 );
163     SG_LOG( SG_FLIGHT, SG_INFO, "T: " << current_balloon.getTemperature() <<
164                                 " alt: " << temp[2] <<
165                                 " gr_alt: " << get_Runway_altitude() << 
166                                 " burner: " << controls.get_elevator() <<
167                                 " v[2]: " << temp2[2]); */                      
168
169     // but lets restore our original bogus altitude when we are done
170     if ( save_alt < -9000.0 ) {
171         set_Altitude( save_alt );
172     }
173 }
174
175
176 // Convert from the FGInterface struct to the BalloonSim
177 bool FGBalloonSim::copy_to_BalloonSim() {
178     return true;
179 }
180
181
182 // Convert from the BalloonSim to the FGInterface struct
183 bool FGBalloonSim::copy_from_BalloonSim() {
184
185     sgVec3 temp;
186
187     // Velocities
188     current_balloon.getVelocity( temp );
189     _set_Velocities_Local( temp[0], temp[1], temp[2] );
190
191     /* ***FIXME*** */ _set_V_equiv_kts( sgLengthVec3 ( temp ) );
192
193     _set_Omega_Body( 0.0, 0.0, 0.0 );
194
195     // Positions
196     current_balloon.getPosition( temp );
197     //temp[0]: geocentric latitude
198     //temp[1]: longitude
199     //temp[2]: altitude (meters)
200
201     _updateGeocentricPosition( temp[0], temp[1], temp[2] * SG_METER_TO_FEET );
202     
203     current_balloon.getHPR( temp );
204     set_Euler_Angles( temp[0], temp[1], temp[2] );
205     
206     return true;
207 }
208
209