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>
21 HeadingIndicator::HeadingIndicator ( SGPropertyNode *node )
23 _name(node->getStringValue("name", "heading-indicator")),
24 _num(node->getIntValue("number", 0)),
25 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg"))
29 HeadingIndicator::~HeadingIndicator ()
34 HeadingIndicator::init ()
37 branch = "/instrumentation/" + _name;
39 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
40 _offset_node = node->getChild("offset-deg", 0, true);
41 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
42 _suction_node = fgGetNode(_suction.c_str(), true);
43 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
44 _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
45 _heading_bug_node = node->getChild("heading-bug-deg", 0, true);
46 _last_heading_deg = (_heading_in_node->getDoubleValue() +
47 _offset_node->getDoubleValue());
51 HeadingIndicator::bind ()
53 std::ostringstream temp;
56 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
58 fgTie((branch + "/serviceable").c_str(),
59 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
60 fgTie((branch + "/spin").c_str(),
61 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
65 HeadingIndicator::unbind ()
67 std::ostringstream temp;
70 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
72 fgUntie((branch + "/serviceable").c_str());
73 fgUntie((branch + "/spin").c_str());
77 HeadingIndicator::update (double dt)
79 // Get the spin from the gyro
80 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
82 double spin = _gyro.get_spin_norm();
84 // Next, calculate time-based precession
85 double offset = _offset_node->getDoubleValue();
86 offset -= dt * (0.25 / 60.0); // 360deg/day
87 SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
89 // TODO: movement-induced error
91 // Next, calculate the indicated heading,
92 // introducing errors.
93 double factor = 100 * (spin * spin * spin * spin * spin * spin);
94 double heading = _heading_in_node->getDoubleValue();
96 // Now, we have to get the current
97 // heading and the last heading into
99 while ((heading - _last_heading_deg) > 180)
100 _last_heading_deg += 360;
101 while ((heading - _last_heading_deg) < -180)
102 _last_heading_deg -= 360;
104 heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
105 _last_heading_deg = heading;
108 SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
110 _heading_out_node->setDoubleValue(heading);
112 // Calculate heading bug error normalized to +/- 180.0
113 double heading_bug = _heading_bug_node->getDoubleValue();
114 double diff = heading_bug - heading;
116 SG_NORMALIZE_RANGE(diff, -180.0, 180.0);
117 _heading_bug_error_node->setDoubleValue( diff );
120 // end of heading_indicator.cxx