]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_dg.cxx
Allow instruments/systems to reset on sim reset
[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     _error_node->setDoubleValue(error);
152
153                                 // Next, calculate the indicated heading,
154                                 // introducing errors.
155     double factor = 100 * (spin * spin * spin * spin * spin * spin);
156     double heading = _heading_in_node->getDoubleValue();
157
158                                 // Now, we have to get the current
159                                 // heading and the last heading into
160                                 // the same range.
161     while ((heading - _last_heading_deg) > 180)
162         _last_heading_deg += 360;
163     while ((heading - _last_heading_deg) < -180)
164         _last_heading_deg -= 360;
165
166     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
167     _last_heading_deg = heading;
168
169     heading += offset + align + error;
170     heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
171
172     _heading_out_node->setDoubleValue(heading);
173
174                                  // calculate the difference between the indicated heading
175                                  // and the selected heading for use with an autopilot
176     static SGPropertyNode *bnode
177         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
178     if ( bnode ) {
179         double diff = bnode->getDoubleValue() - heading;
180         if ( diff < -180.0 ) { diff += 360.0; }
181         if ( diff > 180.0 ) { diff -= 360.0; }
182             _heading_bug_error_node->setDoubleValue( diff );
183     }
184                                  // calculate the difference between the indicated heading
185                                  // and the selected nav1 radial for use with an autopilot
186     SGPropertyNode *nnode
187         = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
188     if ( nnode ) {
189         double diff = nnode->getDoubleValue() - heading;
190         if ( diff < -180.0 ) { diff += 360.0; }
191         if ( diff > 180.0 ) { diff -= 360.0; }
192         _nav1_error_node->setDoubleValue( diff );
193     }
194 }
195
196 // end of heading_indicator_dg.cxx