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