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