]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_dg.cxx
Clean-up some SGMath dependencies.
[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
67     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
68     _offset_node = node->getChild("offset-deg", 0, true);
69     _serviceable_node = node->getChild("serviceable", 0, true);
70     _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
71     _error_node = node->getChild("error-deg", 0, true);
72     _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
73     _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
74     _align_node = node->getChild("align-deg", 0, true);
75
76     _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
77
78     _align_node->setDoubleValue(0);
79     _error_node->setDoubleValue(0);
80
81     _last_heading_deg = (_heading_in_node->getDoubleValue() +
82         _offset_node->getDoubleValue() + _align_node->getDoubleValue());
83 }
84
85 void
86 HeadingIndicatorDG::bind ()
87 {
88     std::ostringstream temp;
89     std::string branch;
90     temp << num;
91     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
92
93     fgTie((branch + "/serviceable").c_str(),
94           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
95     fgTie((branch + "/spin").c_str(),
96           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
97 }
98
99 void
100 HeadingIndicatorDG::unbind ()
101 {
102     std::ostringstream temp;
103     std::string branch;
104     temp << num;
105     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
106
107     fgUntie((branch + "/serviceable").c_str());
108     fgUntie((branch + "/spin").c_str());
109 }
110
111 void
112 HeadingIndicatorDG::update (double dt)
113 {
114                                 // Get the spin from the gyro
115     _gyro.set_power_norm(_electrical_node->getDoubleValue());
116
117     _gyro.update(dt);
118     double spin = _gyro.get_spin_norm();
119
120                                 // Next, calculate time-based precession
121     double offset = _offset_node->getDoubleValue();
122     offset -= dt * (0.25 / 60.0); // 360deg/day
123     offset = SGMiscd::normalizePeriodic(-360.0,360.0,offset);
124     _offset_node->setDoubleValue(offset);
125
126                                 // No magvar - set the alignment manually
127     double align = _align_node->getDoubleValue();
128
129                                 // Movement-induced error
130     double yaw_rate = _yaw_rate_node->getDoubleValue();
131     double error = _error_node->getDoubleValue();
132     double g = _g_node->getDoubleValue();
133
134     if ( fabs ( yaw_rate ) > 5 ) {
135         error += 0.033 * -yaw_rate * dt ;
136     }
137
138     if ( g > 1.5 || g < -0.5){
139         error += 0.033 * g * dt;
140     }
141
142     _error_node->setDoubleValue(error);
143
144                                 // Next, calculate the indicated heading,
145                                 // introducing errors.
146     double factor = 100 * (spin * spin * spin * spin * spin * spin);
147     double heading = _heading_in_node->getDoubleValue();
148
149                                 // Now, we have to get the current
150                                 // heading and the last heading into
151                                 // the same range.
152     while ((heading - _last_heading_deg) > 180)
153         _last_heading_deg += 360;
154     while ((heading - _last_heading_deg) < -180)
155         _last_heading_deg -= 360;
156
157     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
158     _last_heading_deg = heading;
159
160     heading += offset + align + error;
161     heading = SGMiscd::normalizePeriodic(0.0,360.0,heading);
162
163     _heading_out_node->setDoubleValue(heading);
164
165                                  // calculate the difference between the indicacted heading
166                                  // and the selected heading for use with an autopilot
167     static SGPropertyNode *bnode
168         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
169     if ( bnode ) {
170         double diff = bnode->getDoubleValue() - heading;
171         if ( diff < -180.0 ) { diff += 360.0; }
172         if ( diff > 180.0 ) { diff -= 360.0; }
173             _heading_bug_error_node->setDoubleValue( diff );
174     }
175                                  // calculate the difference between the indicated heading
176                                  // and the selected nav1 radial for use with an autopilot
177     SGPropertyNode *nnode
178         = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
179     if ( nnode ) {
180         double diff = nnode->getDoubleValue() - heading;
181         if ( diff < -180.0 ) { diff += 360.0; }
182         if ( diff > 180.0 ) { diff -= 360.0; }
183         _nav1_error_node->setDoubleValue( diff );
184     }
185 }
186
187 // end of heading_indicator_dg.cxx