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 <simgear/magvar/magvar.hxx>
18 #include <simgear/math/SGMath.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Main/util.hxx>
23 #include "heading_indicator_fg.hxx"
27 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
29 name("heading-indicator-fg"),
33 for ( i = 0; i < node->nChildren(); ++i ) {
34 SGPropertyNode *child = node->getChild(i);
35 string cname = child->getName();
36 string cval = child->getStringValue();
37 if ( cname == "name" ) {
39 } else if ( cname == "number" ) {
40 num = child->getIntValue();
42 SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
43 if ( name.length() ) {
44 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
50 HeadingIndicatorFG::HeadingIndicatorFG ()
54 HeadingIndicatorFG::~HeadingIndicatorFG ()
59 HeadingIndicatorFG::init ()
62 branch = "/instrumentation/" + name;
64 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
66 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
67 if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
68 _offset_node = node->getChild("offset-deg", 0, true);
69 _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
71 _serviceable_node = node->getChild("serviceable", 0, true);
72 _error_node = node->getChild("heading-bug-error-deg", 0, true);
73 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
74 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
75 _off_node = node->getChild("off-flag", 0, true);
77 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
83 HeadingIndicatorFG::reinit ()
85 _last_heading_deg = (_heading_in_node->getDoubleValue() +
86 _offset_node->getDoubleValue());
91 HeadingIndicatorFG::bind ()
93 std::ostringstream temp;
96 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
98 fgTie((branch + "/serviceable").c_str(),
99 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
100 fgTie((branch + "/spin").c_str(),
101 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
105 HeadingIndicatorFG::unbind ()
107 std::ostringstream temp;
110 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
112 fgUntie((branch + "/serviceable").c_str());
113 fgUntie((branch + "/spin").c_str());
117 HeadingIndicatorFG::update (double dt)
119 // Get the spin from the gyro
120 _gyro.set_power_norm(_electrical_node->getDoubleValue());
122 double spin = _gyro.get_spin_norm();
124 if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
125 _off_node->setBoolValue(false);
127 _off_node->setBoolValue(true);
131 // No time-based precession for a flux gate compass
132 // We just use offset to get the magvar
133 double offset = _offset_node->getDoubleValue();
135 // TODO: movement-induced error
137 // Next, calculate the indicated heading,
138 // introducing errors.
139 double factor = 100 * (spin * spin * spin * spin * spin * spin);
140 double heading = _heading_in_node->getDoubleValue();
142 // Now, we have to get the current
143 // heading and the last heading into
145 if ((heading - _last_heading_deg) > 180)
146 _last_heading_deg += 360;
147 if ((heading - _last_heading_deg) < -180)
148 _last_heading_deg -= 360;
150 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
151 _last_heading_deg = heading;
160 _heading_out_node->setDoubleValue(heading);
162 // calculate the difference between the indicated heading
163 // and the selected heading for use with an autopilot
164 static SGPropertyNode *bnode
165 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
168 diff = bnode->getDoubleValue() - heading;
169 if ( diff < -180.0 ) { diff += 360.0; }
170 if ( diff > 180.0 ) { diff -= 360.0; }
171 _error_node->setDoubleValue( diff );
173 // calculate the difference between the indicated heading
174 // and the selected nav1 radial for use with an autopilot
175 SGPropertyNode *nnode
176 = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
179 ndiff = nnode->getDoubleValue() - heading;
180 if ( ndiff < -180.0 ) { ndiff += 360.0; }
181 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
182 _nav1_error_node->setDoubleValue( ndiff );
188 // end of heading_indicator_fg.cxx