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.
6 #include <simgear/compiler.h>
11 #include "heading_indicator.hxx"
12 #include <Main/fg_props.hxx>
13 #include <Main/util.hxx>
16 HeadingIndicator::HeadingIndicator ( SGPropertyNode *node )
18 name("heading-indicator"),
20 vacuum_system("/systems/vacuum")
23 for ( i = 0; i < node->nChildren(); ++i ) {
24 SGPropertyNode *child = node->getChild(i);
25 string cname = child->getName();
26 string cval = child->getStringValue();
27 if ( cname == "name" ) {
29 } else if ( cname == "number" ) {
30 num = child->getIntValue();
31 } else if ( cname == "vacuum-system" ) {
34 SG_LOG( SG_INSTR, SG_WARN, "Error in heading-indicator config logic" );
35 if ( name.length() ) {
36 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
42 HeadingIndicator::HeadingIndicator ()
46 HeadingIndicator::~HeadingIndicator ()
51 HeadingIndicator::init ()
54 branch = "/instrumentation/" + name;
55 vacuum_system += "/suction-inhg";
57 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
58 _offset_node = node->getChild("offset-deg", 0, true);
59 _heading_in_node = fgGetNode("/orientation/heading-deg", true);
60 _suction_node = fgGetNode(vacuum_system.c_str(), true);
61 _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
62 _last_heading_deg = (_heading_in_node->getDoubleValue() +
63 _offset_node->getDoubleValue());
67 HeadingIndicator::bind ()
69 std::ostringstream temp;
72 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
74 fgTie((branch + "/serviceable").c_str(),
75 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
76 fgTie((branch + "/spin").c_str(),
77 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
81 HeadingIndicator::unbind ()
83 std::ostringstream temp;
86 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
88 fgUntie((branch + "/serviceable").c_str());
89 fgUntie((branch + "/spin").c_str());
93 HeadingIndicator::update (double dt)
95 // Get the spin from the gyro
96 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
98 double spin = _gyro.get_spin_norm();
100 // Next, calculate time-based precession
101 double offset = _offset_node->getDoubleValue();
102 offset -= dt * (0.25 / 60.0); // 360deg/day
103 while (offset < -360)
107 _offset_node->setDoubleValue(offset);
109 // TODO: movement-induced error
111 // Next, calculate the indicated heading,
112 // introducing errors.
113 double factor = 0.01 / (spin * spin * spin * spin * spin * spin);
114 double heading = _heading_in_node->getDoubleValue();
116 // Now, we have to get the current
117 // heading and the last heading into
119 while ((heading - _last_heading_deg) > 180)
120 _last_heading_deg += 360;
121 while ((heading - _last_heading_deg) < -180)
122 _last_heading_deg -= 360;
124 heading = fgGetLowPass(_last_heading_deg, heading, dt/factor);
125 _last_heading_deg = heading;
130 while (heading > 360)
133 _heading_out_node->setDoubleValue(heading);
136 // end of heading_indicator.cxx