]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_fg.cxx
Fix unused private vars.
[flightgear.git] / src / Instrumentation / heading_indicator_fg.cxx
1 // heading_indicator_fg.cxx - a flux_gate compass.
2 // Based on the vacuum driven Heading Indicator Written by David Megginson, started 2002.
3 //
4 // Written by Vivian Meazza, started 2005.
5 //
6 // This file is in the Public Domain and comes with no warranty.
7
8 #ifdef HAVE_CONFIG_H
9 #  include "config.h"
10 #endif
11
12 #include <simgear/compiler.h>
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16
17 #include <simgear/math/SGMath.hxx>
18
19 #include <Main/fg_props.hxx>
20 #include <Main/util.hxx>
21
22 #include "heading_indicator_fg.hxx"
23
24 using std::string;
25
26 HeadingIndicatorFG::HeadingIndicatorFG ( SGPropertyNode *node )
27     :
28     name("heading-indicator-fg"),        
29     num(0)
30 {
31     int i;
32     for ( i = 0; i < node->nChildren(); ++i ) {
33         SGPropertyNode *child = node->getChild(i);
34         string cname = child->getName();
35         string cval = child->getStringValue();
36         if ( cname == "name" ) {
37             name = cval;
38         } else if ( cname == "number" ) {
39             num = child->getIntValue();
40         } else {
41             SG_LOG( SG_INSTR, SG_WARN, "Error in flux-gate heading-indicator config logic" );
42             if ( name.length() ) {
43                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
44             }
45         }
46     }
47 }
48
49 HeadingIndicatorFG::HeadingIndicatorFG ()
50 {
51 }
52
53 HeadingIndicatorFG::~HeadingIndicatorFG ()
54 {
55 }
56
57 void
58 HeadingIndicatorFG::init ()
59 {
60     string branch;
61     branch = "/instrumentation/" + name;
62     
63     _heading_in_node = fgGetNode("/orientation/heading-deg", true);
64
65     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
66     if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
67       _offset_node = node->getChild("offset-deg", 0, true);
68       _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") );
69     }
70     _serviceable_node = node->getChild("serviceable", 0, true);
71     _error_node = node->getChild("heading-bug-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     _off_node         = node->getChild("off-flag", 0, true);
75
76     _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
77
78     reinit();
79 }
80
81 void
82 HeadingIndicatorFG::reinit ()
83 {
84     _last_heading_deg = (_heading_in_node->getDoubleValue() +
85                          _offset_node->getDoubleValue());
86     _gyro.reinit();
87 }
88
89 void
90 HeadingIndicatorFG::bind ()
91 {
92     std::ostringstream temp;
93     string branch;
94     temp << num;
95     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
96
97     fgTie((branch + "/serviceable").c_str(),
98           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
99     fgTie((branch + "/spin").c_str(),
100           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
101 }
102
103 void
104 HeadingIndicatorFG::unbind ()
105 {
106     std::ostringstream temp;
107     string branch;
108     temp << num;
109     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
110
111     fgUntie((branch + "/serviceable").c_str());
112     fgUntie((branch + "/spin").c_str());
113 }
114
115 void
116 HeadingIndicatorFG::update (double dt)
117 {
118                                 // Get the spin from the gyro
119          _gyro.set_power_norm(_electrical_node->getDoubleValue());
120         _gyro.update(dt);
121     double spin = _gyro.get_spin_norm();
122
123     if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
124         _off_node->setBoolValue(false);
125     } else {
126         _off_node->setBoolValue(true);
127         return;
128     }
129
130                                 // No time-based precession     for a flux gate compass
131                                     // We just use offset to get the magvar
132         double offset = _offset_node->getDoubleValue();
133            
134                                 // TODO: movement-induced error
135
136                                 // Next, calculate the indicated heading,
137                                 // introducing errors.
138     double factor = 100 * (spin * spin * spin * spin * spin * spin);
139     double heading = _heading_in_node->getDoubleValue();
140
141                                 // Now, we have to get the current
142                                 // heading and the last heading into
143                                 // the same range.
144     if ((heading - _last_heading_deg) > 180)
145         _last_heading_deg += 360;
146     if ((heading - _last_heading_deg) < -180)
147         _last_heading_deg -= 360;
148
149     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
150     _last_heading_deg = heading;
151
152         heading += offset;
153
154     if (heading < 0)
155         heading += 360;
156     if (heading > 360)
157         heading -= 360;
158
159     _heading_out_node->setDoubleValue(heading);
160
161                                      // calculate the difference between the indicated heading
162                                      // and the selected heading for use with an autopilot
163         SGPropertyNode *bnode
164         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
165         double diff = 0;
166         if ( bnode ){
167                 diff = bnode->getDoubleValue() - heading;
168                 if ( diff < -180.0 ) { diff += 360.0; }
169                 if ( diff > 180.0 ) { diff -= 360.0; }
170                 _error_node->setDoubleValue( diff );
171         }   
172                                      // calculate the difference between the indicated heading
173                                      // and the selected nav1 radial for use with an autopilot
174         SGPropertyNode *nnode
175         = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
176         double ndiff = 0;
177         if ( nnode ){
178                 ndiff = nnode->getDoubleValue() - heading;
179                 if ( ndiff < -180.0 ) { ndiff += 360.0; }
180                 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
181                 _nav1_error_node->setDoubleValue( ndiff );
182         }   
183
184
185 }
186
187 // end of heading_indicator_fg.cxx