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