]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_dg.cxx
52fe89834e6dd27d2a68ff32939251f785d60235
[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 #ifdef HAVE_CONFIG_H
10 #  include "config.h"
11 #endif
12
13 #include <simgear/compiler.h>
14 #include <simgear/sg_inlines.h>
15 #include <simgear/math/SGMath.hxx>
16 #include <iostream>
17 #include <string>
18 #include <sstream>
19
20 #include <Main/fg_props.hxx>
21 #include <Main/util.hxx>
22
23 #include "heading_indicator_dg.hxx"
24
25
26 HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
27     name("heading-indicator-dg"),
28     num(0)
29 {
30     int i;
31     for ( i = 0; i < node->nChildren(); ++i ) {
32         SGPropertyNode *child = node->getChild(i);
33         std::string cname = child->getName();
34         std::string cval = child->getStringValue();
35         if ( cname == "name" ) {
36             name = cval;
37         } else if ( cname == "number" ) {
38             num = child->getIntValue();
39         } else {
40             SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
41             if ( name.length() ) {
42                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
43             }
44         }
45     }
46 }
47
48 HeadingIndicatorDG::HeadingIndicatorDG ()
49 {
50 }
51
52 HeadingIndicatorDG::~HeadingIndicatorDG ()
53 {
54 }
55
56 void
57 HeadingIndicatorDG::init ()
58 {
59     std::string branch;
60     branch = "/instrumentation/" + name;
61
62     _heading_in_node = fgGetNode("/orientation/heading-deg", true);
63     _yaw_rate_node   = fgGetNode("/orientation/yaw-rate-degps", true);
64      _g_node         = fgGetNode("/accelerations/pilot-g", true);
65
66     SGPropertyNode *node    = fgGetNode(branch.c_str(), num, true );
67     _offset_node            = node->getChild("offset-deg", 0, true);
68     _serviceable_node       = node->getChild("serviceable", 0, true);
69     _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
70     _error_node             = node->getChild("error-deg", 0, true);
71     _nav1_error_node        = node->getChild("nav1-course-error-deg", 0, true);
72     _heading_out_node       = node->getChild("indicated-heading-deg", 0, true);
73     _align_node             = node->getChild("align-deg", 0, true);
74
75     _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
76
77     reinit();
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::reinit (void)
108 {
109     // reset errors/drift values
110     _align_node->setDoubleValue(0.0);
111     _error_node->setDoubleValue(0.0);
112     _offset_node->setDoubleValue(0.0);
113
114     _last_heading_deg = (_heading_in_node->getDoubleValue() +
115         _offset_node->getDoubleValue() + _align_node->getDoubleValue());
116
117     _gyro.reinit();
118 }
119
120 void
121 HeadingIndicatorDG::update (double dt)
122 {
123                                 // Get the spin from the gyro
124     _gyro.set_power_norm(_electrical_node->getDoubleValue());
125
126     _gyro.update(dt);
127     double spin = _gyro.get_spin_norm();
128
129                                 // Next, calculate time-based precession
130     double offset = _offset_node->getDoubleValue();
131     offset -= dt * (0.25 / 60.0); // 360deg/day
132     offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
133     _offset_node->setDoubleValue(offset);
134
135                                 // No magvar - set the alignment manually
136     double align = _align_node->getDoubleValue();
137
138                                 // Movement-induced error
139     double yaw_rate = _yaw_rate_node->getDoubleValue();
140     double error = _error_node->getDoubleValue();
141     double g = _g_node->getDoubleValue();
142
143     if ( fabs ( yaw_rate ) > 5 ) {
144         error += 0.033 * -yaw_rate * dt ;
145     }
146
147     if ( g > 1.5 || g < -0.5){
148         error += 0.033 * g * dt;
149     }
150
151                                 // Next, calculate the indicated heading,
152                                 // introducing errors.
153     double factor = spin * spin * spin * spin * spin * spin;
154     double heading = _heading_in_node->getDoubleValue();
155     if (spin < 0.9)
156     {
157         // when gyro spin is low, then any heading change results in
158         // increasing the error (spin=0 => indicator is stuck)
159         error += (_last_heading_deg - heading)*(1-factor);
160     }
161     _error_node->setDoubleValue(error);
162
163                                 // Now, we have to get the current
164                                 // heading and the last heading into
165                                 // the same range.
166     while ((heading - _last_heading_deg) > 180)
167         _last_heading_deg += 360;
168     while ((heading - _last_heading_deg) < -180)
169         _last_heading_deg -= 360;
170     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor * 100);
171     _last_heading_deg = heading;
172
173     heading += offset + align + error;
174     heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
175
176     _heading_out_node->setDoubleValue(heading);
177
178                                  // calculate the difference between the indicated heading
179                                  // and the selected heading for use with an autopilot
180     static SGPropertyNode *bnode
181         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
182     if ( bnode ) {
183         double diff = bnode->getDoubleValue() - heading;
184         if ( diff < -180.0 ) { diff += 360.0; }
185         if ( diff > 180.0 ) { diff -= 360.0; }
186             _heading_bug_error_node->setDoubleValue( diff );
187     }
188                                  // calculate the difference between the indicated heading
189                                  // and the selected nav1 radial for use with an autopilot
190     SGPropertyNode *nnode
191         = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
192     if ( nnode ) {
193         double diff = nnode->getDoubleValue() - heading;
194         if ( diff < -180.0 ) { diff += 360.0; }
195         if ( diff > 180.0 ) { diff -= 360.0; }
196         _nav1_error_node->setDoubleValue( diff );
197     }
198 }
199
200 // end of heading_indicator_dg.cxx