]> git.mxchange.org Git - flightgear.git/blob - src/Network/opengc.cxx
MSVC fix from Frederic Bouvier
[flightgear.git] / src / Network / opengc.cxx
1 // opengc.cxx - Network interface program to send data to display processor over LAN
2 //
3 // Created by:  J. Wojnaroski  -- castle@mminternet.com
4 // Date:                21 Nov 2001 
5 //
6 // Extended from original network code developed by C. Olson
7 //
8 //  Modified 12/02/01 - Update engine structure for multi-engine models
9 //                        - Added data preamble to id msg types
10 //
11 //  Modified 01/23/02 - Converted portions of the Engine and Gear accesssors to properties
12 //                          - Removed data from navigation functions. OpenGC provides own nav functions
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License as
16 // published by the Free Software Foundation; either version 2 of the
17 // License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful, but
20 // WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 // General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 //
28
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/io/iochannel.hxx>
31
32 #include <vector>
33
34 #include "opengc.hxx"
35 #include <FDM/flight.hxx>
36 #include <Main/globals.hxx>
37 #include <Cockpit/radiostack.hxx>
38 #include <Controls/controls.hxx>
39 #include <Main/fg_props.hxx>
40
41 SG_USING_STD(vector);
42
43 FGOpenGC::FGOpenGC() : 
44         press_node(fgGetNode("/environment/pressure-inhg", true)),
45         temp_node(fgGetNode("/environment/temperature-degc", true)),
46         wind_dir_node(fgGetNode("/environment/wind-from-heading-deg", true)),
47         wind_speed_node(fgGetNode("/environment/wind-speed-kt", true)),
48         p_left_aileron(fgGetNode("surface-positions/left-aileron-pos-norm", true)),
49         p_right_aileron(fgGetNode("surface-positions/right-aileron-pos-norm", true)),
50         p_elevator(fgGetNode("surface-positions/elevator-pos-norm", true)),
51         p_elevator_trim(fgGetNode("surface-positions/elevator_trim-pos-norm", true)),
52         p_rudder(fgGetNode("surface-positions/rudder-pos-norm", true)),
53         p_flaps(fgGetNode("surface-positions/flap-pos-norm", true)),
54         p_flaps_cmd(fgGetNode("/controls/flaps", true)),
55         p_alphadot(fgGetNode("/fdm/jsbsim/aero/alphadot-radsec", true)),
56         p_betadot(fgGetNode("/fdm/jsbsim/aero/betadot-radsec", true))
57 {
58 }
59
60 FGOpenGC::~FGOpenGC() {
61 }
62
63 // open hailing frequencies
64 bool FGOpenGC::open() {
65     if ( is_enabled() ) {
66         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
67                 << "is already in use, ignoring" );
68         return false;
69     }
70
71     SGIOChannel *io = get_io_channel();
72
73     if ( ! io->open( get_direction() ) ) {
74         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
75         return false;
76     }
77
78     set_enabled( true );
79
80     return true;
81 }
82
83 //static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
84 void FGOpenGC::collect_data( const FGInterface *fdm, ogcFGData *data ) {        
85                                                                                         
86     data->version_id = OGC_VERSION;
87
88     data->longitude = cur_fdm_state->get_Longitude_deg();   
89     data->latitude = cur_fdm_state->get_Latitude_deg();
90     data->magvar = globals->get_mag()->get_magvar();
91    
92     data->pitch = cur_fdm_state->get_Theta_deg();
93     data->bank = cur_fdm_state->get_Phi_deg();
94     data->heading = cur_fdm_state->get_Psi_deg();
95     data->altitude = cur_fdm_state->get_Altitude();
96     data->altitude_agl = cur_fdm_state->get_Altitude_AGL();
97     data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
98     data->vvi = cur_fdm_state->get_Climb_Rate();
99     data->mach = cur_fdm_state->get_Mach_number();
100     data->groundspeed = cur_fdm_state->get_V_ground_speed();
101     data->v_keas = cur_fdm_state->get_V_equiv_kts();
102
103     data->phi_dot = cur_fdm_state->get_Phi_dot();
104     data->theta_dot = cur_fdm_state->get_Theta_dot();
105     data->psi_dot = cur_fdm_state->get_Psi_dot();
106
107     data->alpha = cur_fdm_state->get_Alpha();
108     data->beta = cur_fdm_state->get_Beta();
109     data->alpha_dot = p_alphadot->getDoubleValue();
110     data->beta_dot = p_betadot->getDoubleValue();
111     
112     //data->rudder_trim = p_Controls->get_rudder_trim();
113     
114     data->left_aileron = p_left_aileron->getDoubleValue();
115     data->right_aileron = p_right_aileron->getDoubleValue();
116     data->elevator = p_elevator->getDoubleValue();
117     data->elevator_trim = p_elevator_trim->getDoubleValue();
118     data->rudder = p_rudder->getDoubleValue();
119     data->flaps = p_flaps->getDoubleValue();
120     data->flaps_cmd = p_flaps_cmd->getDoubleValue();
121
122     data->gear_nose = fgGetDouble("gear/gear[0]/position-norm");
123     data->gear_nose = fgGetDouble("gear/gear[1]/position-norm");
124     data->gear_nose = fgGetDouble("gear/gear[2]/position-norm");
125           data->gear_nose = fgGetDouble("gear/gear[3]/position-norm");
126           data->gear_nose = fgGetDouble("gear/gear[4]/position-norm");
127           
128     data->rpm[0] = fgGetDouble("/engines/engine[0]/rpm");
129     data->rpm[1] = fgGetDouble("/engines/engine[1]/rpm");   
130
131     data->epr[0] = fgGetDouble("/engines/engine[0]/epr");
132     data->epr[1] = fgGetDouble("/engines/engine[1]/epr");
133     data->epr[2] = fgGetDouble("/engines/engine[2]/epr");
134     data->epr[3] = fgGetDouble("/engines/engine[3]/epr");
135     
136     data->egt[0] = (fgGetDouble("/engines/engine[0]/egt-degf") - 32.0) * 0.555;
137     data->egt[1] = (fgGetDouble("/engines/engine[1]/egt-degf") - 32.0) * 0.555;
138     data->egt[2] = (fgGetDouble("/engines/engine[2]/egt-degf") - 32.0) * 0.555;
139     data->egt[3] = (fgGetDouble("/engines/engine[3]/egt-degf") - 32.0) * 0.555;
140
141     data->n2_turbine[0] = fgGetDouble("/engines/engine[0]/n2");
142     data->n2_turbine[1] = fgGetDouble("/engines/engine[1]/n2");
143     data->n2_turbine[2] = fgGetDouble("/engines/engine[2]/n2");
144     data->n2_turbine[3] = fgGetDouble("/engines/engine[3]/n2");
145
146     data->n1_turbine[0] = fgGetDouble("/engines/engine[0]/n1");
147     data->n1_turbine[1] = fgGetDouble("/engines/engine[1]/n1");
148     data->n1_turbine[2] = fgGetDouble("/engines/engine[2]/n1");
149     data->n1_turbine[3] = fgGetDouble("/engines/engine[3]/n1");
150 // Convert gph to pph for turbine engines
151     data->fuel_flow[0] = fgGetDouble("/engines/engine[0]/fuel-flow-gph") * 6.5;
152     data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph") * 6.5;
153     data->fuel_flow[2] = fgGetDouble("/engines/engine[2]/fuel-flow-gph") * 6.5;
154     data->fuel_flow[3] = fgGetDouble("/engines/engine[3]/fuel-flow-gph") * 6.5;
155
156     data->oil_pressure[0] = fgGetDouble("/engines/engine[0]/oil-pressure-psi");
157     data->oil_pressure[1] = fgGetDouble("/engines/engine[1]/oil-pressure-psi");
158     data->oil_pressure[2] = fgGetDouble("/engines/engine[2]/oil-pressure-psi");
159     data->oil_pressure[3] = fgGetDouble("/engines/engine[3]/oil-pressure-psi");
160 // Just a few test numbers to test interface and drive EICAS displayss    
161     for (int j=0; j<4; j++) data->oil_pressure[j] = 100.4 + 2.3*j;
162
163     data->man_pressure[0] = fgGetDouble("/engines/engine[0]/mp-osi");
164     data->man_pressure[1] = fgGetDouble("/engines/engine[1]/mp-osi");    
165
166     data->total_temperature = cur_fdm_state->get_Total_temperature();
167     data->total_pressure = cur_fdm_state->get_Total_pressure();
168     data->dynamic_pressure = cur_fdm_state->get_Dynamic_pressure();
169     
170     data->static_pressure = press_node->getDoubleValue();
171     data->static_temperature = temp_node->getDoubleValue();
172     data->wind = wind_speed_node->getDoubleValue();
173     data->wind_dir =  wind_dir_node->getDoubleValue();
174 }
175
176 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
177     // just a place holder until the CDU is developed
178         
179 }
180
181 // process work for this port
182 bool FGOpenGC::process() {
183     SGIOChannel *io = get_io_channel();
184     int length = sizeof(buf);
185
186     if ( get_direction() == SG_IO_OUT ) {
187         collect_data( cur_fdm_state, &buf );
188         //collect_data( &buf );
189         if ( ! io->write( (char *)(& buf), length ) ) {
190             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
191             return false;
192         }
193     } else if ( get_direction() == SG_IO_IN ) {
194         if ( io->get_type() == sgFileType ) {
195             if ( io->read( (char *)(& buf), length ) == length ) {
196                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
197                 distribute_data( &buf, cur_fdm_state );
198             }
199         } else {
200             while ( io->read( (char *)(& buf), length ) == length ) {
201                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
202                 distribute_data( &buf, cur_fdm_state );
203             }
204         }
205     }
206
207     return true;
208 }
209
210
211 // close the channel
212 bool FGOpenGC::close() {
213     SGIOChannel *io = get_io_channel();
214
215     set_enabled( false );
216
217     if ( ! io->close() ) {
218         return false;
219     }
220
221     return true;
222 }
223