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"
26 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
27 name("heading-indicator-dg"),
31 for ( i = 0; i < node->nChildren(); ++i ) {
32 SGPropertyNode *child = node->getChild(i);
33 std::string cname = child->getName();
34 std::string cval = child->getStringValue();
35 if ( cname == "name" ) {
37 } else if ( cname == "number" ) {
38 num = child->getIntValue();
40 SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
41 if ( name.length() ) {
42 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
48 HeadingIndicatorDG::HeadingIndicatorDG ()
52 HeadingIndicatorDG::~HeadingIndicatorDG ()
57 HeadingIndicatorDG::init ()
60 branch = "/instrumentation/" + name;
62 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
63 _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
64 _g_node = fgGetNode("/accelerations/pilot-g", true);
66 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
67 _offset_node = node->getChild("offset-deg", 0, true);
68 _serviceable_node = node->getChild("serviceable", 0, true);
69 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
70 _error_node = node->getChild("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 _align_node = node->getChild("align-deg", 0, true);
75 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
81 HeadingIndicatorDG::bind ()
83 std::ostringstream temp;
86 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
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);
95 HeadingIndicatorDG::unbind ()
97 std::ostringstream temp;
100 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
102 fgUntie((branch + "/serviceable").c_str());
103 fgUntie((branch + "/spin").c_str());
107 HeadingIndicatorDG::reinit (void)
109 // reset errors/drift values
110 _align_node->setDoubleValue(0.0);
111 _error_node->setDoubleValue(0.0);
112 _offset_node->setDoubleValue(0.0);
114 _last_heading_deg = (_heading_in_node->getDoubleValue() +
115 _offset_node->getDoubleValue() + _align_node->getDoubleValue());
121 HeadingIndicatorDG::update (double dt)
123 // Get the spin from the gyro
124 _gyro.set_power_norm(_electrical_node->getDoubleValue());
127 double spin = _gyro.get_spin_norm();
129 // Next, calculate time-based precession
130 double offset = _offset_node->getDoubleValue();
131 offset -= dt * (0.25 / 60.0); // 360deg/day
132 offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
133 _offset_node->setDoubleValue(offset);
135 // No magvar - set the alignment manually
136 double align = _align_node->getDoubleValue();
138 // Movement-induced error
139 double yaw_rate = _yaw_rate_node->getDoubleValue();
140 double error = _error_node->getDoubleValue();
141 double g = _g_node->getDoubleValue();
143 if ( fabs ( yaw_rate ) > 5 ) {
144 error += 0.033 * -yaw_rate * dt ;
147 if ( g > 1.5 || g < -0.5){
148 error += 0.033 * g * dt;
151 _error_node->setDoubleValue(error);
153 // Next, calculate the indicated heading,
154 // introducing errors.
155 double factor = 100 * (spin * spin * spin * spin * spin * spin);
156 double heading = _heading_in_node->getDoubleValue();
158 // Now, we have to get the current
159 // heading and the last heading into
161 while ((heading - _last_heading_deg) > 180)
162 _last_heading_deg += 360;
163 while ((heading - _last_heading_deg) < -180)
164 _last_heading_deg -= 360;
166 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
167 _last_heading_deg = heading;
169 heading += offset + align + error;
170 heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
172 _heading_out_node->setDoubleValue(heading);
174 // calculate the difference between the indicated heading
175 // and the selected heading for use with an autopilot
176 static SGPropertyNode *bnode
177 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
179 double diff = bnode->getDoubleValue() - heading;
180 if ( diff < -180.0 ) { diff += 360.0; }
181 if ( diff > 180.0 ) { diff -= 360.0; }
182 _heading_bug_error_node->setDoubleValue( diff );
184 // calculate the difference between the indicated heading
185 // and the selected nav1 radial for use with an autopilot
186 SGPropertyNode *nnode
187 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
189 double diff = nnode->getDoubleValue() - heading;
190 if ( diff < -180.0 ) { diff += 360.0; }
191 if ( diff > 180.0 ) { diff -= 360.0; }
192 _nav1_error_node->setDoubleValue( diff );
196 // end of heading_indicator_dg.cxx