1 // heading_indicator_fg.cxx - a flux_gate compass.
2 // Based on the vacuum driven Heading Indicator Written by David Megginson, started 2002.
4 // Written by Vivian Meazza, started 2005.
6 // This file is in the Public Domain and comes with no warranty.
12 #include <simgear/compiler.h>
17 #include "heading_indicator_fg.hxx"
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
21 #include <simgear/magvar/magvar.hxx>
25 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
27 name("heading-indicator-fg"),
31 for ( i = 0; i < node->nChildren(); ++i ) {
32 SGPropertyNode *child = node->getChild(i);
33 string cname = child->getName();
34 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 flux-gate heading-indicator config logic" );
41 if ( name.length() ) {
42 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
48 HeadingIndicatorFG::HeadingIndicatorFG ()
52 HeadingIndicatorFG::~HeadingIndicatorFG ()
57 HeadingIndicatorFG::init ()
60 branch = "/instrumentation/" + name;
62 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
64 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
65 if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
66 _offset_node = node->getChild("offset-deg", 0, true);
67 _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
69 _serviceable_node = node->getChild("serviceable", 0, true);
70 _error_node = node->getChild("heading-bug-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 _off_node = node->getChild("off-flag", 0, true);
75 _last_heading_deg = (_heading_in_node->getDoubleValue() +
76 _offset_node->getDoubleValue());
77 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
81 HeadingIndicatorFG::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);
96 HeadingIndicatorFG::unbind ()
98 std::ostringstream temp;
101 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
103 fgUntie((branch + "/serviceable").c_str());
104 fgUntie((branch + "/spin").c_str());
108 HeadingIndicatorFG::update (double dt)
110 // Get the spin from the gyro
111 _gyro.set_power_norm(_electrical_node->getDoubleValue());
113 double spin = _gyro.get_spin_norm();
115 if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
116 _off_node->setBoolValue(false);
118 _off_node->setBoolValue(true);
122 // No time-based precession for a flux gate compass
123 // We just use offset to get the magvar
124 double offset = _offset_node->getDoubleValue();
126 // TODO: movement-induced error
128 // Next, calculate the indicated heading,
129 // introducing errors.
130 double factor = 100 * (spin * spin * spin * spin * spin * spin);
131 double heading = _heading_in_node->getDoubleValue();
133 // Now, we have to get the current
134 // heading and the last heading into
136 if ((heading - _last_heading_deg) > 180)
137 _last_heading_deg += 360;
138 if ((heading - _last_heading_deg) < -180)
139 _last_heading_deg -= 360;
141 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
142 _last_heading_deg = heading;
151 _heading_out_node->setDoubleValue(heading);
153 // calculate the difference between the indicated heading
154 // and the selected heading for use with an autopilot
155 static SGPropertyNode *bnode
156 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
159 diff = bnode->getDoubleValue() - heading;
160 if ( diff < -180.0 ) { diff += 360.0; }
161 if ( diff > 180.0 ) { diff -= 360.0; }
162 _error_node->setDoubleValue( diff );
164 // calculate the difference between the indicated heading
165 // and the selected nav1 radial for use with an autopilot
166 SGPropertyNode *nnode
167 = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
170 ndiff = nnode->getDoubleValue() - heading;
171 if ( ndiff < -180.0 ) { ndiff += 360.0; }
172 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
173 _nav1_error_node->setDoubleValue( ndiff );
179 // end of heading_indicator_fg.cxx