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