1 // heading_indicator.cxx - a vacuum-powered heading indicator.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain and comes with no warranty.
10 #include <simgear/compiler.h>
11 #include <simgear/sg_inlines.h>
12 #include <simgear/math/SGMath.hxx>
17 #include "heading_indicator.hxx"
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
21 #include <simgear/magvar/magvar.hxx>
23 HeadingIndicator::HeadingIndicator ( SGPropertyNode *node )
25 _name(node->getStringValue("name", "heading-indicator")),
26 _num(node->getIntValue("number", 0)),
27 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg"))
31 HeadingIndicator::~HeadingIndicator ()
36 HeadingIndicator::init ()
39 branch = "/instrumentation/" + _name;
41 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
42 if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
43 _offset_node = node->getChild("offset-deg", 0, true);
44 _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
46 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
47 _suction_node = fgGetNode(_suction.c_str(), true);
48 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
49 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
50 _heading_bug_node = node->getChild("heading-bug-deg", 0, true);
56 HeadingIndicator::reinit ()
58 _last_heading_deg = (_heading_in_node->getDoubleValue() +
59 _offset_node->getDoubleValue());
64 HeadingIndicator::bind ()
66 std::ostringstream temp;
69 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
71 fgTie((branch + "/serviceable").c_str(),
72 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
73 fgTie((branch + "/spin").c_str(),
74 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
78 HeadingIndicator::unbind ()
80 std::ostringstream temp;
83 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
85 fgUntie((branch + "/serviceable").c_str());
86 fgUntie((branch + "/spin").c_str());
90 HeadingIndicator::update (double dt)
92 // Get the spin from the gyro
93 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
95 double spin = _gyro.get_spin_norm();
97 // Next, calculate time-based precession
98 double offset = _offset_node->getDoubleValue();
99 offset -= dt * (0.25 / 60.0); // 360deg/day
100 SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
102 // TODO: movement-induced error
104 // Next, calculate the indicated heading,
105 // introducing errors.
106 double factor = 100 * (spin * spin * spin * spin * spin * spin);
107 double heading = _heading_in_node->getDoubleValue();
109 // Now, we have to get the current
110 // heading and the last heading into
112 while ((heading - _last_heading_deg) > 180)
113 _last_heading_deg += 360;
114 while ((heading - _last_heading_deg) < -180)
115 _last_heading_deg -= 360;
117 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
118 _last_heading_deg = heading;
121 SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
123 _heading_out_node->setDoubleValue(heading);
125 // Calculate heading bug error normalized to +/- 180.0
126 double heading_bug = _heading_bug_node->getDoubleValue();
127 double diff = heading_bug - heading;
129 SG_NORMALIZE_RANGE(diff, -180.0, 180.0);
130 _heading_bug_error_node->setDoubleValue( diff );
133 // end of heading_indicator.cxx