1 // heading_indicator_dg.cxx - a Directional Gyro (DG) compass.
2 // Based on the vacuum driven Heading Indicator Written by David Megginson,
5 // Written by Vivian Meazza, started 2005.
7 // This file is in the Public Domain and comes with no warranty.
13 #include <simgear/compiler.h>
14 #include <simgear/sg_inlines.h>
15 #include <simgear/math/SGMath.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Main/util.hxx>
23 #include "heading_indicator_dg.hxx"
25 /** Macro calculating x^6 (faster than super-slow math/pow). */
26 #define POW6(x) (x*x*x*x*x*x)
28 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
29 name("heading-indicator-dg"),
33 for ( i = 0; i < node->nChildren(); ++i ) {
34 SGPropertyNode *child = node->getChild(i);
35 std::string cname = child->getName();
36 std::string cval = child->getStringValue();
37 if ( cname == "name" ) {
39 } else if ( cname == "number" ) {
40 num = child->getIntValue();
42 SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
43 if ( name.length() ) {
44 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
50 HeadingIndicatorDG::~HeadingIndicatorDG ()
55 HeadingIndicatorDG::init ()
58 branch = "/instrumentation/" + name;
60 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
61 _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
62 _g_node = fgGetNode("/accelerations/pilot-g", true);
64 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
65 _offset_node = node->getChild("offset-deg", 0, true);
66 _serviceable_node = node->getChild("serviceable", 0, true);
67 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
68 _error_node = node->getChild("error-deg", 0, true);
69 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
70 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
71 _align_node = node->getChild("align-deg", 0, true);
73 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
79 HeadingIndicatorDG::bind ()
81 std::ostringstream temp;
84 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
86 fgTie((branch + "/serviceable").c_str(),
87 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
88 fgTie((branch + "/spin").c_str(),
89 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
93 HeadingIndicatorDG::unbind ()
95 std::ostringstream temp;
98 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
100 fgUntie((branch + "/serviceable").c_str());
101 fgUntie((branch + "/spin").c_str());
105 HeadingIndicatorDG::reinit (void)
107 // reset errors/drift values
108 _align_node->setDoubleValue(0.0);
109 _error_node->setDoubleValue(0.0);
110 _offset_node->setDoubleValue(0.0);
112 _last_heading_deg = _heading_in_node->getDoubleValue();
113 _last_indicated_heading_dg = _last_heading_deg;
119 HeadingIndicatorDG::update (double dt)
121 // Get the spin from the gyro
122 _gyro.set_power_norm(_electrical_node->getDoubleValue());
127 double spin = _gyro.get_spin_norm();
128 double heading = _heading_in_node->getDoubleValue();
129 double offset = _offset_node->getDoubleValue();
131 // calculate scaling factor
132 double factor = POW6(spin);
134 // calculate time-based precession (scaled by spin factor, since
135 // there is no precession when the gyro is stuck).
136 offset -= dt * (0.25 / 60.0) * factor; // 360deg/day
138 // indication should get more and more stuck at low gyro spins
141 // when gyro spin is low, then any heading change results in
142 // increasing the offset
143 double diff = SGMiscd::normalizePeriodic(-180.0, 180.0, _last_heading_deg - heading);
144 // scaled by 1-factor, so indication is fully stuck at spin==0 (offset compensates
145 // any heading change)
146 offset += diff * (1.0-factor);
148 _last_heading_deg = heading;
151 offset = SGMiscd::normalizePeriodic(-180.0,180.0,offset);
152 _offset_node->setDoubleValue(offset);
154 // No magvar - set the alignment manually
155 double align = _align_node->getDoubleValue();
157 // Movement-induced error
158 double yaw_rate = _yaw_rate_node->getDoubleValue();
159 double error = _error_node->getDoubleValue();
160 double g = _g_node->getDoubleValue();
161 if ( fabs ( yaw_rate ) > 5 ) {
162 error += 0.033 * -yaw_rate * dt * factor;
165 if ( g > 1.5 || g < -0.5){
166 error += 0.033 * g * dt * factor;
168 _error_node->setDoubleValue(error);
170 // Now, we have to get the current
171 // heading and the last heading into
173 while ((heading - _last_indicated_heading_dg) > 180)
174 _last_indicated_heading_dg += 360;
175 while ((heading - _last_indicated_heading_dg) < -180)
176 _last_indicated_heading_dg -= 360;
177 heading = fgGetLowPass(_last_indicated_heading_dg, heading, dt * 100);
178 _last_indicated_heading_dg = heading;
180 heading += offset + align + error;
181 heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
183 _heading_out_node->setDoubleValue(heading);
185 // calculate the difference between the indicated heading
186 // and the selected heading for use with an autopilot
187 static SGPropertyNode *bnode
188 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
190 double diff = bnode->getDoubleValue() - heading;
191 if ( diff < -180.0 ) { diff += 360.0; }
192 if ( diff > 180.0 ) { diff -= 360.0; }
193 _heading_bug_error_node->setDoubleValue( diff );
195 // calculate the difference between the indicated heading
196 // and the selected nav1 radial for use with an autopilot
197 SGPropertyNode *nnode
198 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
200 double diff = nnode->getDoubleValue() - heading;
201 if ( diff < -180.0 ) { diff += 360.0; }
202 if ( diff > 180.0 ) { diff -= 360.0; }
203 _nav1_error_node->setDoubleValue( diff );
207 // end of heading_indicator_dg.cxx