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>
19 #include <Main/fg_props.hxx>
20 #include <Main/util.hxx>
22 #include "heading_indicator_dg.hxx"
25 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
26 name("heading-indicator-dg"),
30 for ( i = 0; i < node->nChildren(); ++i ) {
31 SGPropertyNode *child = node->getChild(i);
32 std::string cname = child->getName();
33 std::string cval = child->getStringValue();
34 if ( cname == "name" ) {
36 } else if ( cname == "number" ) {
37 num = child->getIntValue();
39 SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
40 if ( name.length() ) {
41 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
47 HeadingIndicatorDG::HeadingIndicatorDG ()
51 HeadingIndicatorDG::~HeadingIndicatorDG ()
56 HeadingIndicatorDG::init ()
59 branch = "/instrumentation/" + name;
61 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
62 _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
63 _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);
77 _align_node->setDoubleValue(0);
78 _error_node->setDoubleValue(0);
80 _last_heading_deg = (_heading_in_node->getDoubleValue() +
81 _offset_node->getDoubleValue() + _align_node->getDoubleValue());
85 HeadingIndicatorDG::bind ()
87 std::ostringstream temp;
90 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
92 fgTie((branch + "/serviceable").c_str(),
93 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
94 fgTie((branch + "/spin").c_str(),
95 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
99 HeadingIndicatorDG::unbind ()
101 std::ostringstream temp;
104 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
106 fgUntie((branch + "/serviceable").c_str());
107 fgUntie((branch + "/spin").c_str());
111 HeadingIndicatorDG::update (double dt)
113 // Get the spin from the gyro
114 _gyro.set_power_norm(_electrical_node->getDoubleValue());
117 double spin = _gyro.get_spin_norm();
119 // Next, calculate time-based precession
120 double offset = _offset_node->getDoubleValue();
121 offset -= dt * (0.25 / 60.0); // 360deg/day
122 offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
123 _offset_node->setDoubleValue(offset);
125 // No magvar - set the alignment manually
126 double align = _align_node->getDoubleValue();
128 // Movement-induced error
129 double yaw_rate = _yaw_rate_node->getDoubleValue();
130 double error = _error_node->getDoubleValue();
131 double g = _g_node->getDoubleValue();
133 if ( fabs ( yaw_rate ) > 5 ) {
134 error += 0.033 * -yaw_rate * dt ;
137 if ( g > 1.5 || g < -0.5){
138 error += 0.033 * g * dt;
141 _error_node->setDoubleValue(error);
143 // Next, calculate the indicated heading,
144 // introducing errors.
145 double factor = 100 * (spin * spin * spin * spin * spin * spin);
146 double heading = _heading_in_node->getDoubleValue();
148 // Now, we have to get the current
149 // heading and the last heading into
151 while ((heading - _last_heading_deg) > 180)
152 _last_heading_deg += 360;
153 while ((heading - _last_heading_deg) < -180)
154 _last_heading_deg -= 360;
156 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
157 _last_heading_deg = heading;
159 heading += offset + align + error;
160 heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
162 _heading_out_node->setDoubleValue(heading);
164 // calculate the difference between the indicacted heading
165 // and the selected heading for use with an autopilot
166 static SGPropertyNode *bnode
167 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
169 double diff = bnode->getDoubleValue() - heading;
170 if ( diff < -180.0 ) { diff += 360.0; }
171 if ( diff > 180.0 ) { diff -= 360.0; }
172 _heading_bug_error_node->setDoubleValue( diff );
174 // calculate the difference between the indicated heading
175 // and the selected nav1 radial for use with an autopilot
176 SGPropertyNode *nnode
177 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
179 double diff = nnode->getDoubleValue() - heading;
180 if ( diff < -180.0 ) { diff += 360.0; }
181 if ( diff > 180.0 ) { diff -= 360.0; }
182 _nav1_error_node->setDoubleValue( diff );
186 // end of heading_indicator_dg.cxx