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);
67 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
68 _offset_node = node->getChild("offset-deg", 0, true);
69 _serviceable_node = node->getChild("serviceable", 0, true);
70 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
71 _error_node = node->getChild("error-deg", 0, true);
72 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
73 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
74 _align_node = node->getChild("align-deg", 0, true);
76 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
78 _align_node->setDoubleValue(0);
79 _error_node->setDoubleValue(0);
81 _last_heading_deg = (_heading_in_node->getDoubleValue() +
82 _offset_node->getDoubleValue() + _align_node->getDoubleValue());
86 HeadingIndicatorDG::bind ()
88 std::ostringstream temp;
91 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
93 fgTie((branch + "/serviceable").c_str(),
94 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
95 fgTie((branch + "/spin").c_str(),
96 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
100 HeadingIndicatorDG::unbind ()
102 std::ostringstream temp;
105 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
107 fgUntie((branch + "/serviceable").c_str());
108 fgUntie((branch + "/spin").c_str());
112 HeadingIndicatorDG::update (double dt)
114 // Get the spin from the gyro
115 _gyro.set_power_norm(_electrical_node->getDoubleValue());
118 double spin = _gyro.get_spin_norm();
120 // Next, calculate time-based precession
121 double offset = _offset_node->getDoubleValue();
122 offset -= dt * (0.25 / 60.0); // 360deg/day
123 offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
124 _offset_node->setDoubleValue(offset);
126 // No magvar - set the alignment manually
127 double align = _align_node->getDoubleValue();
129 // Movement-induced error
130 double yaw_rate = _yaw_rate_node->getDoubleValue();
131 double error = _error_node->getDoubleValue();
132 double g = _g_node->getDoubleValue();
134 if ( fabs ( yaw_rate ) > 5 ) {
135 error += 0.033 * -yaw_rate * dt ;
138 if ( g > 1.5 || g < -0.5){
139 error += 0.033 * g * dt;
142 _error_node->setDoubleValue(error);
144 // Next, calculate the indicated heading,
145 // introducing errors.
146 double factor = 100 * (spin * spin * spin * spin * spin * spin);
147 double heading = _heading_in_node->getDoubleValue();
149 // Now, we have to get the current
150 // heading and the last heading into
152 while ((heading - _last_heading_deg) > 180)
153 _last_heading_deg += 360;
154 while ((heading - _last_heading_deg) < -180)
155 _last_heading_deg -= 360;
157 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
158 _last_heading_deg = heading;
160 heading += offset + align + error;
161 heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
163 _heading_out_node->setDoubleValue(heading);
165 // calculate the difference between the indicacted heading
166 // and the selected heading for use with an autopilot
167 static SGPropertyNode *bnode
168 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
170 double diff = bnode->getDoubleValue() - heading;
171 if ( diff < -180.0 ) { diff += 360.0; }
172 if ( diff > 180.0 ) { diff -= 360.0; }
173 _heading_bug_error_node->setDoubleValue( diff );
175 // calculate the difference between the indicated heading
176 // and the selected nav1 radial for use with an autopilot
177 SGPropertyNode *nnode
178 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
180 double diff = nnode->getDoubleValue() - heading;
181 if ( diff < -180.0 ) { diff += 360.0; }
182 if ( diff > 180.0 ) { diff -= 360.0; }
183 _nav1_error_node->setDoubleValue( diff );
187 // end of heading_indicator_dg.cxx