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>
16 #include "heading_indicator.hxx"
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
20 #include <simgear/magvar/magvar.hxx>
22 HeadingIndicator::HeadingIndicator ( SGPropertyNode *node )
24 _name(node->getStringValue("name", "heading-indicator")),
25 _num(node->getIntValue("number", 0)),
26 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg"))
30 HeadingIndicator::~HeadingIndicator ()
35 HeadingIndicator::init ()
38 branch = "/instrumentation/" + _name;
40 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
41 if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
42 _offset_node = node->getChild("offset-deg", 0, true);
43 _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
45 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
46 _suction_node = fgGetNode(_suction.c_str(), true);
47 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
48 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
49 _heading_bug_node = node->getChild("heading-bug-deg", 0, true);
50 _last_heading_deg = (_heading_in_node->getDoubleValue() +
51 _offset_node->getDoubleValue());
55 HeadingIndicator::bind ()
57 std::ostringstream temp;
60 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
62 fgTie((branch + "/serviceable").c_str(),
63 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
64 fgTie((branch + "/spin").c_str(),
65 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
69 HeadingIndicator::unbind ()
71 std::ostringstream temp;
74 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
76 fgUntie((branch + "/serviceable").c_str());
77 fgUntie((branch + "/spin").c_str());
81 HeadingIndicator::update (double dt)
83 // Get the spin from the gyro
84 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
86 double spin = _gyro.get_spin_norm();
88 // Next, calculate time-based precession
89 double offset = _offset_node->getDoubleValue();
90 offset -= dt * (0.25 / 60.0); // 360deg/day
91 SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
93 // TODO: movement-induced error
95 // Next, calculate the indicated heading,
96 // introducing errors.
97 double factor = 100 * (spin * spin * spin * spin * spin * spin);
98 double heading = _heading_in_node->getDoubleValue();
100 // Now, we have to get the current
101 // heading and the last heading into
103 while ((heading - _last_heading_deg) > 180)
104 _last_heading_deg += 360;
105 while ((heading - _last_heading_deg) < -180)
106 _last_heading_deg -= 360;
108 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
109 _last_heading_deg = heading;
112 SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
114 _heading_out_node->setDoubleValue(heading);
116 // Calculate heading bug error normalized to +/- 180.0
117 double heading_bug = _heading_bug_node->getDoubleValue();
118 double diff = heading_bug - heading;
120 SG_NORMALIZE_RANGE(diff, -180.0, 180.0);
121 _heading_bug_error_node->setDoubleValue( diff );
124 // end of heading_indicator.cxx