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 // Modified 03/05/03 - Modified to reduce need to search property tree for each node per frame
16 // This program is free software; you can redistribute it and/or
17 // modify it under the terms of the GNU General Public License as
18 // published by the Free Software Foundation; either version 2 of the
19 // License, or (at your option) any later version.
21 // This program is distributed in the hope that it will be useful, but
22 // WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 // General Public License for more details.
26 // You should have received a copy of the GNU General Public License
27 // along with this program; if not, write to the Free Software
28 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/io/iochannel.hxx>
37 #include <FDM/flight.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
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 mag_var_node(fgGetNode("/environment/magnetic-variation-deg", true)),
49 p_latitude(fgGetNode("/position/latitude-deg", true)),
50 p_longitude(fgGetNode("/position/longitude-deg", true)),
51 p_alt_node(fgGetNode("/position/altitude-ft", true)),
52 p_altitude(fgGetNode("/steam/altitude-ft", true)),
53 p_altitude_agl(fgGetNode("/position/altitude-agl-ft", true)),
54 egt0_node(fgGetNode("/engines/engine/egt-degf", true)),
55 egt1_node(fgGetNode("/engines/engine[1]/egt-degf", true)),
56 egt2_node(fgGetNode("/engines/engine[2]/egt-degf", true)),
57 egt3_node(fgGetNode("/engines/engine[3]/egt-degf", true)),
58 p_left_aileron(fgGetNode("surface-positions/left-aileron-pos-norm", true)),
59 p_right_aileron(fgGetNode("surface-positions/right-aileron-pos-norm", true)),
60 p_elevator(fgGetNode("surface-positions/elevator-pos-norm", true)),
61 p_elevator_trim(fgGetNode("surface-positions/elevator_trim-pos-norm", true)),
62 p_rudder(fgGetNode("surface-positions/rudder-pos-norm", true)),
63 p_flaps(fgGetNode("surface-positions/flap-pos-norm", true)),
64 p_flaps_cmd(fgGetNode("/controls/flaps", true)),
65 p_alphadot(fgGetNode("/fdm/jsbsim/aero/alphadot-radsec", true)),
66 p_betadot(fgGetNode("/fdm/jsbsim/aero/betadot-radsec", true))
71 FGOpenGC::~FGOpenGC() {
74 // open hailing frequencies
75 bool FGOpenGC::open() {
77 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
78 << "is already in use, ignoring" );
82 SGIOChannel *io = get_io_channel();
84 if ( ! io->open( get_direction() ) ) {
85 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
94 //static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
95 void FGOpenGC::collect_data( const FGInterface *fdm, ogcFGData *data ) {
97 data->version_id = OGC_VERSION;
99 data->longitude = p_longitude->getDoubleValue();
100 data->latitude = p_latitude->getDoubleValue();
101 data->elevation = p_alt_node->getDoubleValue();
102 data->magvar = mag_var_node->getDoubleValue();
104 //cout << "ID: " << OGC_VERSION << " Lon: " << data->longitude << " Lat: " << data->latitude << endl;
106 data->pitch = cur_fdm_state->get_Theta_deg();
107 data->bank = cur_fdm_state->get_Phi_deg();
108 data->heading = cur_fdm_state->get_Psi_deg();
109 data->altitude = p_altitude->getDoubleValue();
110 data->altitude_agl = p_altitude_agl->getDoubleValue();
111 data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
112 data->vvi = cur_fdm_state->get_Climb_Rate();
113 data->mach = cur_fdm_state->get_Mach_number();
114 data->groundspeed = cur_fdm_state->get_V_ground_speed();
115 data->v_keas = cur_fdm_state->get_V_equiv_kts();
117 data->phi_dot = cur_fdm_state->get_Phi_dot();
118 data->theta_dot = cur_fdm_state->get_Theta_dot();
119 data->psi_dot = cur_fdm_state->get_Psi_dot();
121 data->alpha = cur_fdm_state->get_Alpha();
122 data->beta = cur_fdm_state->get_Beta();
123 data->alpha_dot = p_alphadot->getDoubleValue();
124 data->beta_dot = p_betadot->getDoubleValue();
126 //data->rudder_trim = p_Controls->get_rudder_trim();
128 data->left_aileron = p_left_aileron->getDoubleValue();
129 data->right_aileron = p_right_aileron->getDoubleValue();
130 data->elevator = p_elevator->getDoubleValue();
131 data->elevator_trim = p_elevator_trim->getDoubleValue();
132 data->rudder = p_rudder->getDoubleValue();
133 data->flaps = p_flaps->getDoubleValue();
134 data->flaps_cmd = p_flaps_cmd->getDoubleValue();
136 data->gear_nose = fgGetDouble("gear/gear[0]/position-norm");
137 data->gear_left = fgGetDouble("gear/gear[1]/position-norm");
138 data->gear_right = fgGetDouble("gear/gear[2]/position-norm");
139 data->gear_left_rear = fgGetDouble("gear/gear[3]/position-norm");
140 data->gear_right_rear = fgGetDouble("gear/gear[4]/position-norm");
142 data->rpm[0] = fgGetDouble("/engines/engine[0]/rpm");
143 data->rpm[1] = fgGetDouble("/engines/engine[1]/rpm");
145 data->epr[0] = fgGetDouble("/engines/engine[0]/epr");
146 data->epr[1] = fgGetDouble("/engines/engine[1]/epr");
147 data->epr[2] = fgGetDouble("/engines/engine[2]/epr");
148 data->epr[3] = fgGetDouble("/engines/engine[3]/epr");
150 data->egt[0] = (egt0_node->getDoubleValue() - 32.0) * 0.555;
151 data->egt[1] = (egt1_node->getDoubleValue() - 32.0) * 0.555;
152 data->egt[2] = (egt2_node->getDoubleValue() - 32.0) * 0.555;
153 data->egt[3] = (egt3_node->getDoubleValue() - 32.0) * 0.555;
155 data->n2_turbine[0] = fgGetDouble("/engines/engine[0]/n2");
156 data->n2_turbine[1] = fgGetDouble("/engines/engine[1]/n2");
157 data->n2_turbine[2] = fgGetDouble("/engines/engine[2]/n2");
158 data->n2_turbine[3] = fgGetDouble("/engines/engine[3]/n2");
160 data->n1_turbine[0] = fgGetDouble("/engines/engine[0]/n1");
161 data->n1_turbine[1] = fgGetDouble("/engines/engine[1]/n1");
162 data->n1_turbine[2] = fgGetDouble("/engines/engine[2]/n1");
163 data->n1_turbine[3] = fgGetDouble("/engines/engine[3]/n1");
164 // the units for turbine engines is lbs/hr divide by 6.5 to convert to gph if piston type
165 data->fuel_flow[0] = fgGetDouble("/engines/engine[0]/fuel-flow-gph");
166 data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph");
167 data->fuel_flow[2] = fgGetDouble("/engines/engine[2]/fuel-flow-gph");
168 data->fuel_flow[3] = fgGetDouble("/engines/engine[3]/fuel-flow-gph");
170 data->oil_pressure[0] = fgGetDouble("/engines/engine[0]/oil-pressure-psi");
171 data->oil_pressure[1] = fgGetDouble("/engines/engine[1]/oil-pressure-psi");
172 data->oil_pressure[2] = fgGetDouble("/engines/engine[2]/oil-pressure-psi");
173 data->oil_pressure[3] = fgGetDouble("/engines/engine[3]/oil-pressure-psi");
175 data->oil_temp[0] = fgGetDouble("/engines/engine[0]/OilTemp_degK");
176 data->oil_temp[1] = fgGetDouble("/engines/engine[1]/OilTemp_degK");
177 data->oil_temp[2] = fgGetDouble("/engines/engine[2]/OilTemp_degK");
178 data->oil_temp[3] = fgGetDouble("/engines/engine[3]/OilTemp_degK");
179 // dummy value to fill data packet
180 data->oil_quantity[0] = fgGetDouble("/engines/engine[0]/oil-quantity");
181 data->oil_quantity[1] = fgGetDouble("/engines/engine[1]/oil-quantity");
182 data->oil_quantity[2] = fgGetDouble("/engines/engine[2]/oil-quantity");
183 data->oil_quantity[3] = fgGetDouble("/engines/engine[3]/oil-quantity");
184 data->oil_quantity[0] = 40.3; data->oil_quantity[1] = 44.6;
186 data->hyd_pressure[0] = fgGetDouble("/engines/engine[0]/hyd-pressure-psi");
187 data->hyd_pressure[1] = fgGetDouble("/engines/engine[1]/hyd-pressure-psi");
188 data->hyd_pressure[2] = fgGetDouble("/engines/engine[2]/hyd-pressure-psi");
189 data->hyd_pressure[3] = fgGetDouble("/engines/engine[3]/hyd-pressure-psi");
190 data->hyd_pressure[0] = 3215.0; data->hyd_pressure[1] = 3756.0;
192 data->man_pressure[0] = fgGetDouble("/engines/engine[0]/mp-osi");
193 data->man_pressure[1] = fgGetDouble("/engines/engine[1]/mp-osi");
195 data->tank[0] = fgGetDouble("/consumables/fuel/tank[2]/level-gal_us") * 6.6;
196 data->tank[1] = fgGetDouble("/consumables/fuel/tank/level-gal_us") * 6.6;
197 data->tank[2] = fgGetDouble("/consumables/fuel/tank[1]/level-gal_us") * 6.6;
198 //data->tank[0] = 3000.0;
199 //data->tank[1] = 9800.0;
200 //data->tank[2] = 9800.0;
202 data->total_temperature = cur_fdm_state->get_Total_temperature();
203 data->total_pressure = cur_fdm_state->get_Total_pressure();
204 data->dynamic_pressure = cur_fdm_state->get_Dynamic_pressure();
206 data->static_pressure = press_node->getDoubleValue();
207 data->static_temperature = temp_node->getDoubleValue();
208 data->wind = wind_speed_node->getDoubleValue();
209 data->wind_dir = wind_dir_node->getDoubleValue();
212 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
213 // just a place holder until the CDU is developed
217 // process work for this port
218 bool FGOpenGC::process() {
219 SGIOChannel *io = get_io_channel();
220 int length = sizeof(buf);
222 if ( get_direction() == SG_IO_OUT ) {
223 collect_data( cur_fdm_state, &buf );
224 //collect_data( &buf );
225 if ( ! io->write( (char *)(& buf), length ) ) {
226 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
229 } else if ( get_direction() == SG_IO_IN ) {
230 if ( io->get_type() == sgFileType ) {
231 if ( io->read( (char *)(& buf), length ) == length ) {
232 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
233 distribute_data( &buf, cur_fdm_state );
236 while ( io->read( (char *)(& buf), length ) == length ) {
237 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
238 distribute_data( &buf, cur_fdm_state );
248 bool FGOpenGC::close() {
249 SGIOChannel *io = get_io_channel();
251 set_enabled( false );
253 if ( ! io->close() ) {