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>
23 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
25 name("heading-indicator-fg"),
29 for ( i = 0; i < node->nChildren(); ++i ) {
30 SGPropertyNode *child = node->getChild(i);
31 string cname = child->getName();
32 string cval = child->getStringValue();
33 if ( cname == "name" ) {
35 } else if ( cname == "number" ) {
36 num = child->getIntValue();
38 SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
39 if ( name.length() ) {
40 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
46 HeadingIndicatorFG::HeadingIndicatorFG ()
50 HeadingIndicatorFG::~HeadingIndicatorFG ()
55 HeadingIndicatorFG::init ()
58 branch = "/instrumentation/" + name;
60 _heading_in_node = fgGetNode("/orientation/heading-deg", 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 _error_node = node->getChild("heading-bug-error-deg", 0, true);
66 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
67 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
68 _off_node = node->getChild("off-flag", 0, true);
70 _last_heading_deg = (_heading_in_node->getDoubleValue() +
71 _offset_node->getDoubleValue());
72 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
76 HeadingIndicatorFG::bind ()
78 std::ostringstream temp;
81 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
83 fgTie((branch + "/serviceable").c_str(),
84 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
85 fgTie((branch + "/spin").c_str(),
86 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
91 HeadingIndicatorFG::unbind ()
93 std::ostringstream temp;
96 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
98 fgUntie((branch + "/serviceable").c_str());
99 fgUntie((branch + "/spin").c_str());
103 HeadingIndicatorFG::update (double dt)
105 // Get the spin from the gyro
106 _gyro.set_power_norm(_electrical_node->getDoubleValue());
108 double spin = _gyro.get_spin_norm();
110 if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
111 _off_node->setBoolValue(false);
113 _off_node->setBoolValue(true);
117 // No time-based precession for a flux gate compass
118 // We just use offset to get the magvar
119 double offset = _offset_node->getDoubleValue();
121 // TODO: movement-induced error
123 // Next, calculate the indicated heading,
124 // introducing errors.
125 double factor = 100 * (spin * spin * spin * spin * spin * spin);
126 double heading = _heading_in_node->getDoubleValue();
128 // Now, we have to get the current
129 // heading and the last heading into
131 if ((heading - _last_heading_deg) > 180)
132 _last_heading_deg += 360;
133 if ((heading - _last_heading_deg) < -180)
134 _last_heading_deg -= 360;
136 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
137 _last_heading_deg = heading;
146 _heading_out_node->setDoubleValue(heading);
148 // calculate the difference between the indicated heading
149 // and the selected heading for use with an autopilot
150 static SGPropertyNode *bnode
151 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
154 diff = bnode->getDoubleValue() - heading;
155 if ( diff < -180.0 ) { diff += 360.0; }
156 if ( diff > 180.0 ) { diff -= 360.0; }
157 _error_node->setDoubleValue( diff );
159 // calculate the difference between the indicated heading
160 // and the selected nav1 radial for use with an autopilot
161 SGPropertyNode *nnode
162 = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
165 ndiff = nnode->getDoubleValue() - heading;
166 if ( ndiff < -180.0 ) { ndiff += 360.0; }
167 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
168 _nav1_error_node->setDoubleValue( ndiff );
174 // end of heading_indicator_fg.cxx