]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_dg.cxx
fix NAV receiver vs GPS bugs
[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         string cname = child->getName();
29         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     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     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     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     SG_NORMALIZE_RANGE(offset, -360.0, 360.0);
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     int sign = 0;
129
130     if ( fabs ( yaw_rate ) > 5 ) {
131         error += 0.033 * -yaw_rate * dt ;
132     }
133
134     if ( g > 1.5 || g < -0.5){
135         error += 0.033 * g * dt;
136     }
137
138     _error_node->setDoubleValue(error);
139
140                                 // Next, calculate the indicated heading,
141                                 // introducing errors.
142     double factor = 100 * (spin * spin * spin * spin * spin * spin);
143     double heading = _heading_in_node->getDoubleValue();
144
145                                 // Now, we have to get the current
146                                 // heading and the last heading into
147                                 // the same range.
148     while ((heading - _last_heading_deg) > 180)
149         _last_heading_deg += 360;
150     while ((heading - _last_heading_deg) < -180)
151         _last_heading_deg -= 360;
152
153     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
154     _last_heading_deg = heading;
155
156     heading += offset + align + error;
157     SG_NORMALIZE_RANGE(heading, 0.0, 360.0);
158
159     _heading_out_node->setDoubleValue(heading);
160
161                                  // calculate the difference between the indicacted heading
162                                  // and the selected heading for use with an autopilot
163     static SGPropertyNode *bnode
164         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
165     if ( bnode ) {
166         double diff = bnode->getDoubleValue() - heading;
167         if ( diff < -180.0 ) { diff += 360.0; }
168         if ( diff > 180.0 ) { diff -= 360.0; }
169             _heading_bug_error_node->setDoubleValue( diff );
170     }
171                                  // calculate the difference between the indicated heading
172                                  // and the selected nav1 radial for use with an autopilot
173     SGPropertyNode *nnode
174         = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
175     if ( nnode ) {
176         double diff = nnode->getDoubleValue() - heading;
177         if ( diff < -180.0 ) { diff += 360.0; }
178         if ( diff > 180.0 ) { diff -= 360.0; }
179         _nav1_error_node->setDoubleValue( diff );
180     }
181 }
182
183 // end of heading_indicator_dg.cxx