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.
8 #include <simgear/compiler.h>
13 #include "heading_indicator_fg.hxx"
14 #include <Main/fg_props.hxx>
15 #include <Main/util.hxx>
18 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
20 name("heading-indicator-fg"),
24 for ( i = 0; i < node->nChildren(); ++i ) {
25 SGPropertyNode *child = node->getChild(i);
26 string cname = child->getName();
27 string cval = child->getStringValue();
28 if ( cname == "name" ) {
30 } else if ( cname == "number" ) {
31 num = child->getIntValue();
33 SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
34 if ( name.length() ) {
35 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
41 HeadingIndicatorFG::HeadingIndicatorFG ()
45 HeadingIndicatorFG::~HeadingIndicatorFG ()
50 HeadingIndicatorFG::init ()
53 branch = "/instrumentation/" + name;
55 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
57 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
58 _offset_node = node->getChild("offset-deg", 0, true);
59 _serviceable_node = node->getChild("serviceable", 0, true);
60 _error_node = node->getChild("heading-bug-error-deg", 0, true);
61 _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
62 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
63 _off_node = node->getChild("off-flag", 0, true);
65 _last_heading_deg = (_heading_in_node->getDoubleValue() +
66 _offset_node->getDoubleValue());
67 _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
71 HeadingIndicatorFG::bind ()
73 std::ostringstream temp;
76 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
78 fgTie((branch + "/serviceable").c_str(),
79 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
80 fgTie((branch + "/spin").c_str(),
81 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
86 HeadingIndicatorFG::unbind ()
88 std::ostringstream temp;
91 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
93 fgUntie((branch + "/serviceable").c_str());
94 fgUntie((branch + "/spin").c_str());
98 HeadingIndicatorFG::update (double dt)
100 // Get the spin from the gyro
101 _gyro.set_power_norm(_electrical_node->getDoubleValue());
103 double spin = _gyro.get_spin_norm();
105 if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
106 _off_node->setBoolValue(false);
108 _off_node->setBoolValue(true);
112 // No time-based precession for a flux gate compass
113 // We just use offset to get the magvar
114 double offset = _offset_node->getDoubleValue();
116 // TODO: movement-induced error
118 // Next, calculate the indicated heading,
119 // introducing errors.
120 double factor = 100 * (spin * spin * spin * spin * spin * spin);
121 double heading = _heading_in_node->getDoubleValue();
123 // Now, we have to get the current
124 // heading and the last heading into
126 if ((heading - _last_heading_deg) > 180)
127 _last_heading_deg += 360;
128 if ((heading - _last_heading_deg) < -180)
129 _last_heading_deg -= 360;
131 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
132 _last_heading_deg = heading;
141 _heading_out_node->setDoubleValue(heading);
143 // calculate the difference between the indicated heading
144 // and the selected heading for use with an autopilot
145 static SGPropertyNode *bnode
146 = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
149 diff = bnode->getDoubleValue() - heading;
150 if ( diff < -180.0 ) { diff += 360.0; }
151 if ( diff > 180.0 ) { diff -= 360.0; }
152 _error_node->setDoubleValue( diff );
154 // calculate the difference between the indicated heading
155 // and the selected nav1 radial for use with an autopilot
156 SGPropertyNode *nnode
157 = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
160 ndiff = nnode->getDoubleValue() - heading;
161 if ( ndiff < -180.0 ) { ndiff += 360.0; }
162 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
163 _nav1_error_node->setDoubleValue( ndiff );
169 // end of heading_indicator_fg.cxx