]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_dg.cxx
bind the sky disable cutoff distance to a property
[flightgear.git] / src / Instrumentation / heading_indicator_dg.cxx
1 // heading_indicator_dg.cxx - a Directional Gyro (DG) compass.
2 // Based on the vacuum driven Heading Indicator Written by David Megginson,
3 // started 2002.
4 //
5 // Written by Vivian Meazza, started 2005.
6 //
7 // This file is in the Public Domain and comes with no warranty.
8
9 #include <simgear/compiler.h>
10 #include <simgear/sg_inlines.h>
11 #include <iostream>
12 #include <string>
13 #include <sstream>
14
15 #include <Main/fg_props.hxx>
16 #include <Main/util.hxx>
17
18 #include "heading_indicator_dg.hxx"
19
20
21 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
22     name("heading-indicator-dg"),
23     num(0)
24 {
25     int i;
26     for ( i = 0; i < node->nChildren(); ++i ) {
27         SGPropertyNode *child = node->getChild(i);
28         std::string cname = child->getName();
29         std::string cval = child->getStringValue();
30         if ( cname == "name" ) {
31             name = cval;
32         } else if ( cname == "number" ) {
33             num = child->getIntValue();
34         } else {
35             SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
36             if ( name.length() ) {
37                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
38             }
39         }
40     }
41 }
42
43 HeadingIndicatorDG::HeadingIndicatorDG ()
44 {
45 }
46
47 HeadingIndicatorDG::~HeadingIndicatorDG ()
48 {
49 }
50
51 void
52 HeadingIndicatorDG::init ()
53 {
54     std::string branch;
55     branch = "/instrumentation/" + name;
56
57     _heading_in_node = fgGetNode("/orientation/heading-deg", true);
58     _yaw_rate_node   = fgGetNode("/orientation/yaw-rate-degps", true);
59      _g_node         =   fgGetNode("/accelerations/pilot-g", true);
60
61
62     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
63     _offset_node = node->getChild("offset-deg", 0, true);
64     _serviceable_node = node->getChild("serviceable", 0, true);
65     _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
66     _error_node = node->getChild("error-deg", 0, true);
67     _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
68     _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
69     _align_node = node->getChild("align-deg", 0, true);
70
71     _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
72
73     _align_node->setDoubleValue(0);
74     _error_node->setDoubleValue(0);
75
76     _last_heading_deg = (_heading_in_node->getDoubleValue() +
77         _offset_node->getDoubleValue() + _align_node->getDoubleValue());
78 }
79
80 void
81 HeadingIndicatorDG::bind ()
82 {
83     std::ostringstream temp;
84     std::string branch;
85     temp << num;
86     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
87
88     fgTie((branch + "/serviceable").c_str(),
89           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
90     fgTie((branch + "/spin").c_str(),
91           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
92 }
93
94 void
95 HeadingIndicatorDG::unbind ()
96 {
97     std::ostringstream temp;
98     std::string branch;
99     temp << num;
100     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
101
102     fgUntie((branch + "/serviceable").c_str());
103     fgUntie((branch + "/spin").c_str());
104 }
105
106 void
107 HeadingIndicatorDG::update (double dt)
108 {
109                                 // Get the spin from the gyro
110     _gyro.set_power_norm(_electrical_node->getDoubleValue());
111
112     _gyro.update(dt);
113     double spin = _gyro.get_spin_norm();
114
115                                 // Next, calculate time-based precession
116     double offset = _offset_node->getDoubleValue();
117     offset -= dt * (0.25 / 60.0); // 360deg/day
118     offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
119     _offset_node->setDoubleValue(offset);
120
121                                 // No magvar - set the alignment manually
122     double align = _align_node->getDoubleValue();
123
124                                 // Movement-induced error
125     double yaw_rate = _yaw_rate_node->getDoubleValue();
126     double error = _error_node->getDoubleValue();
127     double g = _g_node->getDoubleValue();
128
129     if ( fabs ( yaw_rate ) > 5 ) {
130         error += 0.033 * -yaw_rate * dt ;
131     }
132
133     if ( g > 1.5 || g < -0.5){
134         error += 0.033 * g * dt;
135     }
136
137     _error_node->setDoubleValue(error);
138
139                                 // Next, calculate the indicated heading,
140                                 // introducing errors.
141     double factor = 100 * (spin * spin * spin * spin * spin * spin);
142     double heading = _heading_in_node->getDoubleValue();
143
144                                 // Now, we have to get the current
145                                 // heading and the last heading into
146                                 // the same range.
147     while ((heading - _last_heading_deg) > 180)
148         _last_heading_deg += 360;
149     while ((heading - _last_heading_deg) < -180)
150         _last_heading_deg -= 360;
151
152     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
153     _last_heading_deg = heading;
154
155     heading += offset + align + error;
156     heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
157
158     _heading_out_node->setDoubleValue(heading);
159
160                                  // calculate the difference between the indicacted heading
161                                  // and the selected heading for use with an autopilot
162     static SGPropertyNode *bnode
163         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
164     if ( bnode ) {
165         double diff = bnode->getDoubleValue() - heading;
166         if ( diff < -180.0 ) { diff += 360.0; }
167         if ( diff > 180.0 ) { diff -= 360.0; }
168             _heading_bug_error_node->setDoubleValue( diff );
169     }
170                                  // calculate the difference between the indicated heading
171                                  // and the selected nav1 radial for use with an autopilot
172     SGPropertyNode *nnode
173         = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
174     if ( nnode ) {
175         double diff = nnode->getDoubleValue() - heading;
176         if ( diff < -180.0 ) { diff += 360.0; }
177         if ( diff > 180.0 ) { diff -= 360.0; }
178         _nav1_error_node->setDoubleValue( diff );
179     }
180 }
181
182 // end of heading_indicator_dg.cxx