]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_fg.cxx
Fix file access mode for newnavradio.[ch]xx
[flightgear.git] / src / Instrumentation / heading_indicator_fg.cxx
1 // heading_indicator_fg.cxx - a flux_gate compass.
2 // Based on the vacuum driven Heading Indicator Written by David Megginson, started 2002.
3 //
4 // Written by Vivian Meazza, started 2005.
5 //
6 // This file is in the Public Domain and comes with no warranty.
7
8 #ifdef HAVE_CONFIG_H
9 #  include "config.h"
10 #endif
11
12 #include <simgear/compiler.h>
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16
17 #include "heading_indicator_fg.hxx"
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>                            
20
21 #include <simgear/magvar/magvar.hxx>
22
23 using std::string;
24
25 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
26     :
27     name("heading-indicator-fg"),        
28     num(0)
29 {
30     int i;
31     for ( i = 0; i < node->nChildren(); ++i ) {
32         SGPropertyNode *child = node->getChild(i);
33         string cname = child->getName();
34         string cval = child->getStringValue();
35         if ( cname == "name" ) {
36             name = cval;
37         } else if ( cname == "number" ) {
38             num = child->getIntValue();
39         } else {
40             SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
41             if ( name.length() ) {
42                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
43             }
44         }
45     }
46 }
47
48 HeadingIndicatorFG::HeadingIndicatorFG ()
49 {
50 }
51
52 HeadingIndicatorFG::~HeadingIndicatorFG ()
53 {
54 }
55
56 void
57 HeadingIndicatorFG::init ()
58 {
59     string branch;
60     branch = "/instrumentation/" + name;
61     
62         _heading_in_node = fgGetNode("/orientation/heading-deg", true);
63
64     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
65     if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
66       _offset_node = node->getChild("offset-deg", 0, true);
67       _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
68     }
69     _serviceable_node = node->getChild("serviceable", 0, true);
70         _error_node = node->getChild("heading-bug-error-deg", 0, true);
71         _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
72     _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
73     _off_node         = node->getChild("off-flag", 0, true);
74
75     _last_heading_deg = (_heading_in_node->getDoubleValue() +
76                          _offset_node->getDoubleValue());
77         _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
78 }
79
80 void
81 HeadingIndicatorFG::bind ()
82 {
83     std::ostringstream temp;
84     string branch;
85     temp << num;
86     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
87
88     fgTie((branch + "/serviceable").c_str(),
89           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
90     fgTie((branch + "/spin").c_str(),
91           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
92         
93 }
94
95 void
96 HeadingIndicatorFG::unbind ()
97 {
98     std::ostringstream temp;
99     string branch;
100     temp << num;
101     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
102
103     fgUntie((branch + "/serviceable").c_str());
104     fgUntie((branch + "/spin").c_str());
105 }
106
107 void
108 HeadingIndicatorFG::update (double dt)
109 {
110                                 // Get the spin from the gyro
111          _gyro.set_power_norm(_electrical_node->getDoubleValue());
112         _gyro.update(dt);
113     double spin = _gyro.get_spin_norm();
114
115     if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
116         _off_node->setBoolValue(false);
117     } else {
118         _off_node->setBoolValue(true);
119         return;
120     }
121
122                                 // No time-based precession     for a flux gate compass
123                                     // We just use offset to get the magvar
124         double offset = _offset_node->getDoubleValue();
125            
126                                 // TODO: movement-induced error
127
128                                 // Next, calculate the indicated heading,
129                                 // introducing errors.
130     double factor = 100 * (spin * spin * spin * spin * spin * spin);
131     double heading = _heading_in_node->getDoubleValue();
132
133                                 // Now, we have to get the current
134                                 // heading and the last heading into
135                                 // the same range.
136     if ((heading - _last_heading_deg) > 180)
137         _last_heading_deg += 360;
138     if ((heading - _last_heading_deg) < -180)
139         _last_heading_deg -= 360;
140
141     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
142     _last_heading_deg = heading;
143
144         heading += offset;
145
146     if (heading < 0)
147         heading += 360;
148     if (heading > 360)
149         heading -= 360;
150
151     _heading_out_node->setDoubleValue(heading);
152
153                                      // calculate the difference between the indicated heading
154                                      // and the selected heading for use with an autopilot
155         static SGPropertyNode *bnode
156         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
157         double diff = 0;
158         if ( bnode ){
159                 diff = bnode->getDoubleValue() - heading;
160                 if ( diff < -180.0 ) { diff += 360.0; }
161                 if ( diff > 180.0 ) { diff -= 360.0; }
162                 _error_node->setDoubleValue( diff );
163         }   
164                                      // calculate the difference between the indicated heading
165                                      // and the selected nav1 radial for use with an autopilot
166         SGPropertyNode *nnode
167         = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
168         double ndiff = 0;
169         if ( nnode ){
170                 ndiff = nnode->getDoubleValue() - heading;
171                 if ( ndiff < -180.0 ) { ndiff += 360.0; }
172                 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
173                 _nav1_error_node->setDoubleValue( ndiff );
174         }   
175
176
177 }
178
179 // end of heading_indicator_fg.cxx