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/math/SGMath.hxx>
19 #include <Main/fg_props.hxx>
20 #include <Main/util.hxx>
22 #include "heading_indicator_fg.hxx"
26 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
28 name("heading-indicator-fg"),
32 for ( i = 0; i < node->nChildren(); ++i ) {
33 SGPropertyNode *child = node->getChild(i);
34 string cname = child->getName();
35 string cval = child->getStringValue();
36 if ( cname == "name" ) {
38 } else if ( cname == "number" ) {
39 num = child->getIntValue();
41 SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
42 if ( name.length() ) {
43 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
49 HeadingIndicatorFG::HeadingIndicatorFG ()
53 HeadingIndicatorFG::~HeadingIndicatorFG ()
58 HeadingIndicatorFG::init ()
61 branch = "/instrumentation/" + name;
63 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
65 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
66 if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
67 _offset_node = node->getChild("offset-deg", 0, true);
68 _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") );
70 _serviceable_node = node->getChild("serviceable", 0, true);
71 _error_node = node->getChild("heading-bug-error-deg", 0, true);
72 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
73 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
74 _off_node = node->getChild("off-flag", 0, true);
76 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
82 HeadingIndicatorFG::reinit ()
84 _last_heading_deg = (_heading_in_node->getDoubleValue() +
85 _offset_node->getDoubleValue());
90 HeadingIndicatorFG::bind ()
92 std::ostringstream temp;
95 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
97 fgTie((branch + "/serviceable").c_str(),
98 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
99 fgTie((branch + "/spin").c_str(),
100 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
104 HeadingIndicatorFG::unbind ()
106 std::ostringstream temp;
109 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
111 fgUntie((branch + "/serviceable").c_str());
112 fgUntie((branch + "/spin").c_str());
116 HeadingIndicatorFG::update (double dt)
118 // Get the spin from the gyro
119 _gyro.set_power_norm(_electrical_node->getDoubleValue());
121 double spin = _gyro.get_spin_norm();
123 if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
124 _off_node->setBoolValue(false);
126 _off_node->setBoolValue(true);
130 // No time-based precession for a flux gate compass
131 // We just use offset to get the magvar
132 double offset = _offset_node->getDoubleValue();
134 // TODO: movement-induced error
136 // Next, calculate the indicated heading,
137 // introducing errors.
138 double factor = 100 * (spin * spin * spin * spin * spin * spin);
139 double heading = _heading_in_node->getDoubleValue();
141 // Now, we have to get the current
142 // heading and the last heading into
144 if ((heading - _last_heading_deg) > 180)
145 _last_heading_deg += 360;
146 if ((heading - _last_heading_deg) < -180)
147 _last_heading_deg -= 360;
149 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
150 _last_heading_deg = heading;
159 _heading_out_node->setDoubleValue(heading);
161 // calculate the difference between the indicated heading
162 // and the selected heading for use with an autopilot
163 SGPropertyNode *bnode
164 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
167 diff = bnode->getDoubleValue() - heading;
168 if ( diff < -180.0 ) { diff += 360.0; }
169 if ( diff > 180.0 ) { diff -= 360.0; }
170 _error_node->setDoubleValue( diff );
172 // calculate the difference between the indicated heading
173 // and the selected nav1 radial for use with an autopilot
174 SGPropertyNode *nnode
175 = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
178 ndiff = nnode->getDoubleValue() - heading;
179 if ( ndiff < -180.0 ) { ndiff += 360.0; }
180 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
181 _nav1_error_node->setDoubleValue( ndiff );
187 // end of heading_indicator_fg.cxx