1 // heading_indicator_dg.cxx - a Directional Gyro (DG) compass.
\r
2 // Based on the vacuum driven Heading Indicator Written by David Megginson,
\r
5 // Written by Vivian Meazza, started 2005.
\r
7 // This file is in the Public Domain and comes with no warranty.
\r
9 #include <simgear/compiler.h>
\r
10 #include <simgear/sg_inlines.h>
\r
15 #include <Main/fg_props.hxx>
\r
16 #include <Main/util.hxx>
\r
18 #include "heading_indicator_dg.hxx"
\r
21 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
\r
22 name("heading-indicator-dg"),
\r
26 for ( i = 0; i < node->nChildren(); ++i ) {
\r
27 SGPropertyNode *child = node->getChild(i);
\r
28 string cname = child->getName();
\r
29 string cval = child->getStringValue();
\r
30 if ( cname == "name" ) {
\r
32 } else if ( cname == "number" ) {
\r
33 num = child->getIntValue();
\r
35 SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
\r
36 if ( name.length() ) {
\r
37 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
\r
43 HeadingIndicatorDG::HeadingIndicatorDG ()
\r
47 HeadingIndicatorDG::~HeadingIndicatorDG ()
\r
52 HeadingIndicatorDG::init ()
\r
55 branch = "/instrumentation/" + name;
\r
57 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
58 _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
59 _g_node = fgGetNode("/accelerations/pilot-g", true);
62 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
\r
63 _offset_node = node->getChild("offset-deg", 0, true);
\r
64 _serviceable_node = node->getChild("serviceable", 0, true);
\r
65 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
\r
66 _error_node = node->getChild("error-deg", 0, true);
\r
67 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
\r
68 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
\r
69 _align_node = node->getChild("align-deg", 0, true);
\r
71 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
\r
73 _align_node->setDoubleValue(0);
\r
74 _error_node->setDoubleValue(0);
\r
76 _last_heading_deg = (_heading_in_node->getDoubleValue() +
\r
77 _offset_node->getDoubleValue() + _align_node->getDoubleValue());
\r
81 HeadingIndicatorDG::bind ()
\r
83 std::ostringstream temp;
\r
86 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
\r
88 fgTie((branch + "/serviceable").c_str(),
\r
89 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
\r
90 fgTie((branch + "/spin").c_str(),
\r
91 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
\r
95 HeadingIndicatorDG::unbind ()
\r
97 std::ostringstream temp;
\r
100 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
\r
102 fgUntie((branch + "/serviceable").c_str());
\r
103 fgUntie((branch + "/spin").c_str());
\r
107 HeadingIndicatorDG::update (double dt)
\r
109 // Get the spin from the gyro
\r
110 _gyro.set_power_norm(_electrical_node->getDoubleValue());
\r
113 double spin = _gyro.get_spin_norm();
\r
115 // Next, calculate time-based precession
\r
116 double offset = _offset_node->getDoubleValue();
\r
117 offset -= dt * (0.25 / 60.0); // 360deg/day
\r
118 SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
\r
120 // No magvar - set the alignment manually
\r
121 double align = _align_node->getDoubleValue();
\r
123 // Movement-induced error
\r
124 double yaw_rate = _yaw_rate_node->getDoubleValue();
\r
125 double error = _error_node->getDoubleValue();
\r
126 double g = _g_node->getDoubleValue();
129 if ( fabs ( yaw_rate ) > 5 ) {
130 sign = (yaw_rate < 0 ? -1 : 1);
131 error += 0.033 * -yaw_rate * dt ;
134 if ( g > 1.5 || g < -0.5){
135 sign = (g < 0 ? -1 : 1);
136 error += 0.033 * g * dt;
139 _error_node->setDoubleValue(error);
141 // Next, calculate the indicated heading,
\r
142 // introducing errors.
\r
143 double factor = 100 * (spin * spin * spin * spin * spin * spin);
\r
144 double heading = _heading_in_node->getDoubleValue();
\r
146 // Now, we have to get the current
\r
147 // heading and the last heading into
\r
149 while ((heading - _last_heading_deg) > 180)
\r
150 _last_heading_deg += 360;
\r
151 while ((heading - _last_heading_deg) < -180)
\r
152 _last_heading_deg -= 360;
\r
154 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
\r
155 _last_heading_deg = heading;
\r
157 heading += offset + align + error;
\r
158 SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
\r
160 _heading_out_node->setDoubleValue(heading);
\r
162 // calculate the difference between the indicacted heading
\r
163 // and the selected heading for use with an autopilot
\r
164 static SGPropertyNode *bnode
\r
165 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
\r
167 double diff = bnode->getDoubleValue() - heading;
\r
168 if ( diff < -180.0 ) { diff += 360.0; }
\r
169 if ( diff > 180.0 ) { diff -= 360.0; }
\r
170 _heading_bug_error_node->setDoubleValue( diff );
\r
172 // calculate the difference between the indicated heading
\r
173 // and the selected nav1 radial for use with an autopilot
\r
174 SGPropertyNode *nnode
\r
175 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
\r
177 double diff = nnode->getDoubleValue() - heading;
\r
178 if ( diff < -180.0 ) { diff += 360.0; }
\r
179 if ( diff > 180.0 ) { diff -= 360.0; }
\r
180 _nav1_error_node->setDoubleValue( diff );
\r
184 // end of heading_indicator_dg.cxx
\r