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.
9 #include <simgear/compiler.h>
10 #include <simgear/sg_inlines.h>
15 #include <Main/fg_props.hxx>
16 #include <Main/util.hxx>
18 #include "heading_indicator_dg.hxx"
21 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
22 name("heading-indicator-dg"),
26 for ( i = 0; i < node->nChildren(); ++i ) {
27 SGPropertyNode *child = node->getChild(i);
28 string cname = child->getName();
29 string cval = child->getStringValue();
30 if ( cname == "name" ) {
32 } else if ( cname == "number" ) {
33 num = child->getIntValue();
35 SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
36 if ( name.length() ) {
37 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
43 HeadingIndicatorDG::HeadingIndicatorDG ()
47 HeadingIndicatorDG::~HeadingIndicatorDG ()
52 HeadingIndicatorDG::init ()
55 branch = "/instrumentation/" + name;
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 );
63 _offset_node = node->getChild("offset-deg", 0, true);
64 _serviceable_node = node->getChild("serviceable", 0, true);
65 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
66 _error_node = node->getChild("error-deg", 0, true);
67 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
68 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
69 _align_node = node->getChild("align-deg", 0, true);
71 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
73 _align_node->setDoubleValue(0);
74 _error_node->setDoubleValue(0);
76 _last_heading_deg = (_heading_in_node->getDoubleValue() +
77 _offset_node->getDoubleValue() + _align_node->getDoubleValue());
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::update (double dt)
109 // Get the spin from the gyro
110 _gyro.set_power_norm(_electrical_node->getDoubleValue());
113 double spin = _gyro.get_spin_norm();
115 // Next, calculate time-based precession
116 double offset = _offset_node->getDoubleValue();
117 offset -= dt * (0.25 / 60.0); // 360deg/day
118 SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
119 _offset_node->setDoubleValue(offset);
121 // No magvar - set the alignment manually
122 double align = _align_node->getDoubleValue();
124 // Movement-induced error
125 double yaw_rate = _yaw_rate_node->getDoubleValue();
126 double error = _error_node->getDoubleValue();
127 double g = _g_node->getDoubleValue();
130 if ( fabs ( yaw_rate ) > 5 ) {
131 error += 0.033 * -yaw_rate * dt ;
134 if ( g > 1.5 || g < -0.5){
135 error += 0.033 * g * dt;
138 _error_node->setDoubleValue(error);
140 // Next, calculate the indicated heading,
141 // introducing errors.
142 double factor = 100 * (spin * spin * spin * spin * spin * spin);
143 double heading = _heading_in_node->getDoubleValue();
145 // Now, we have to get the current
146 // heading and the last heading into
148 while ((heading - _last_heading_deg) > 180)
149 _last_heading_deg += 360;
150 while ((heading - _last_heading_deg) < -180)
151 _last_heading_deg -= 360;
153 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
154 _last_heading_deg = heading;
156 heading += offset + align + error;
157 SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
159 _heading_out_node->setDoubleValue(heading);
161 // calculate the difference between the indicacted heading
162 // and the selected heading for use with an autopilot
163 static SGPropertyNode *bnode
164 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
166 double diff = bnode->getDoubleValue() - heading;
167 if ( diff < -180.0 ) { diff += 360.0; }
168 if ( diff > 180.0 ) { diff -= 360.0; }
169 _heading_bug_error_node->setDoubleValue( diff );
171 // calculate the difference between the indicated heading
172 // and the selected nav1 radial for use with an autopilot
173 SGPropertyNode *nnode
174 = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
176 double diff = nnode->getDoubleValue() - heading;
177 if ( diff < -180.0 ) { diff += 360.0; }
178 if ( diff > 180.0 ) { diff -= 360.0; }
179 _nav1_error_node->setDoubleValue( diff );
183 // end of heading_indicator_dg.cxx