1 // opengc.cxx - Network interface program to send data to display processor over LAN
3 // Created by: J. Wojnaroski -- castle@mminternet.com
6 // Extended from original network code developed by C. Olson
8 // Modified 12/02/01 - Update engine structure for multi-engine models
9 // - Added data preamble to id msg types
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
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.
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.
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.
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/io/iochannel.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>
43 FGOpenGC::FGOpenGC() {
46 FGOpenGC::~FGOpenGC() {
49 // open hailing frequencies
50 bool FGOpenGC::open() {
52 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
53 << "is already in use, ignoring" );
57 SGIOChannel *io = get_io_channel();
59 if ( ! io->open( get_direction() ) ) {
60 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
70 static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
72 data->version_id = 0x0012;
74 data->longitude = fdm->get_Longitude_deg();
75 data->latitude = cur_fdm_state->get_Latitude_deg();
76 data->magvar = globals->get_mag()->get_magvar();
78 data->pitch = cur_fdm_state->get_Theta_deg();
79 data->bank = cur_fdm_state->get_Phi_deg();
80 data->heading = cur_fdm_state->get_Psi_deg();
81 data->altitude = cur_fdm_state->get_Altitude();
82 data->altitude_agl = cur_fdm_state->get_Altitude_AGL();
83 data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
84 data->vvi = cur_fdm_state->get_Climb_Rate();
85 data->mach = cur_fdm_state->get_Mach_number();
86 data->groundspeed = cur_fdm_state->get_V_ground_speed();
87 data->v_tas = cur_fdm_state->get_V_equiv_kts();
89 data->alpha = cur_fdm_state->get_Alpha();
90 data->beta = cur_fdm_state->get_Beta();
91 data->alpha_dot = cur_fdm_state->get_Alpha_dot();
92 data->beta_dot = cur_fdm_state->get_Beta_dot();
94 data->static_temperature = cur_fdm_state->get_Static_temperature();
95 data->total_temperature = cur_fdm_state->get_Total_temperature();
97 data->static_pressure = cur_fdm_state->get_Static_pressure();
98 data->total_pressure = cur_fdm_state->get_Total_pressure();
99 data->dynamic_pressure = cur_fdm_state->get_Dynamic_pressure();
101 data->rpm[0] = fgGetDouble("/engines/engine[0]/rpm");
102 data->rpm[1] = fgGetDouble("/engines/engine[1]/rpm");
104 data->epr[0] = fgGetDouble("/engines/engine[0]/epr");
105 data->epr[1] = fgGetDouble("/engines/engine[1]/epr");
106 data->epr[2] = fgGetDouble("/engines/engine[2]/epr");
107 data->epr[3] = fgGetDouble("/engines/engine[3]/epr");
109 data->egt[0] = fgGetDouble("/engines/engine[0]/egt-degf");
110 data->egt[1] = fgGetDouble("/engines/engine[1]/egt-degf");
111 data->egt[2] = fgGetDouble("/engines/engine[2]/egt-degf");
112 data->egt[3] = fgGetDouble("/engines/engine[3]/egt-degf");
114 data->n2_turbine[0] = fgGetDouble("/engines/engine[0]/n2");
115 data->n2_turbine[1] = fgGetDouble("/engines/engine[1]/n2");
116 data->n2_turbine[2] = fgGetDouble("/engines/engine[2]/n2");
117 data->n2_turbine[3] = fgGetDouble("/engines/engine[3]/n2");
119 data->n1_turbine[0] = fgGetDouble("/engines/engine[0]/n1");
120 data->n1_turbine[1] = fgGetDouble("/engines/engine[1]/n1");
121 data->n1_turbine[2] = fgGetDouble("/engines/engine[2]/n1");
122 data->n1_turbine[3] = fgGetDouble("/engines/engine[3]/n1");
124 data->fuel_flow[0] = fgGetDouble("/engines/engine[0]/fuel-flow-gph");
125 data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph");
126 data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph");
127 data->fuel_flow[2] = fgGetDouble("/engines/engine[2]/fuel-flow-gph");
129 data->oil_pressure[0] = fgGetDouble("/engines/engine[0]/oil-pressure-psi");
130 data->oil_pressure[1] = fgGetDouble("/engines/engine[1]/oil-pressure-psi");
131 data->oil_pressure[2] = fgGetDouble("/engines/engine[2]/oil-pressure-psi");
132 data->oil_pressure[3] = fgGetDouble("/engines/engine[3]/oil-pressure-psi");
134 data->man_pressure[0] = fgGetDouble("/engines/engine[0]/mp-osi");
135 data->man_pressure[1] = fgGetDouble("/engines/engine[1]/mp-osi");
137 //data->gear_nose = p_gear[0]->GetPosition();
138 //data->gear_left = p_gear[1]->GetPosition();
139 //data->gear_right = p_gear[2]->GetPosition();
141 data->aileron = globals->get_controls()->get_aileron();
142 //data->aileron_trim = p_Controls->get_aileron_trim();
143 data->elevator = globals->get_controls()->get_elevator();
144 data->elevator_trim = globals->get_controls()->get_elevator_trim();
145 data->rudder = globals->get_controls()->get_rudder();
146 //data->rudder_trim = p_Controls->get_rudder_trim();
147 //data->flaps = fgGetDouble("/controls/flaps");
148 data->flaps = globals->get_controls()->get_flaps();
151 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
152 // just a place holder until the CDU is developed
156 // process work for this port
157 bool FGOpenGC::process() {
158 SGIOChannel *io = get_io_channel();
159 int length = sizeof(buf);
161 if ( get_direction() == SG_IO_OUT ) {
162 collect_data( cur_fdm_state, &buf );
163 //collect_data( &buf );
164 if ( ! io->write( (char *)(& buf), length ) ) {
165 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
168 } else if ( get_direction() == SG_IO_IN ) {
169 if ( io->get_type() == sgFileType ) {
170 if ( io->read( (char *)(& buf), length ) == length ) {
171 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
172 distribute_data( &buf, cur_fdm_state );
175 while ( io->read( (char *)(& buf), length ) == length ) {
176 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
177 distribute_data( &buf, cur_fdm_state );
187 bool FGOpenGC::close() {
188 SGIOChannel *io = get_io_channel();
190 set_enabled( false );
192 if ( ! io->close() ) {