]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/heading_indicator_fg.cxx
#346 related: missing status message for property server
[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
57     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
58     _offset_node = node->getChild("offset-deg", 0, true);
59     _serviceable_node = node->getChild("serviceable", 0, true);
60         _error_node = node->getChild("heading-bug-error-deg", 0, true);
61         _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
62     _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
63     _off_node         = node->getChild("off-flag", 0, true);
64
65     _last_heading_deg = (_heading_in_node->getDoubleValue() +
66                          _offset_node->getDoubleValue());
67         _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
68 }
69
70 void
71 HeadingIndicatorFG::bind ()
72 {
73     std::ostringstream temp;
74     string branch;
75     temp << num;
76     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
77
78     fgTie((branch + "/serviceable").c_str(),
79           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
80     fgTie((branch + "/spin").c_str(),
81           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
82         
83 }
84
85 void
86 HeadingIndicatorFG::unbind ()
87 {
88     std::ostringstream temp;
89     string branch;
90     temp << num;
91     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
92
93     fgUntie((branch + "/serviceable").c_str());
94     fgUntie((branch + "/spin").c_str());
95 }
96
97 void
98 HeadingIndicatorFG::update (double dt)
99 {
100                                 // Get the spin from the gyro
101          _gyro.set_power_norm(_electrical_node->getDoubleValue());
102         _gyro.update(dt);
103     double spin = _gyro.get_spin_norm();
104
105     if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
106         _off_node->setBoolValue(false);
107     } else {
108         _off_node->setBoolValue(true);
109         return;
110     }
111
112                                 // No time-based precession     for a flux gate compass
113                                     // We just use offset to get the magvar
114         double offset = _offset_node->getDoubleValue();
115            
116                                 // TODO: movement-induced error
117
118                                 // Next, calculate the indicated heading,
119                                 // introducing errors.
120     double factor = 100 * (spin * spin * spin * spin * spin * spin);
121     double heading = _heading_in_node->getDoubleValue();
122
123                                 // Now, we have to get the current
124                                 // heading and the last heading into
125                                 // the same range.
126     if ((heading - _last_heading_deg) > 180)
127         _last_heading_deg += 360;
128     if ((heading - _last_heading_deg) < -180)
129         _last_heading_deg -= 360;
130
131     heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
132     _last_heading_deg = heading;
133
134         heading += offset;
135
136     if (heading < 0)
137         heading += 360;
138     if (heading > 360)
139         heading -= 360;
140
141     _heading_out_node->setDoubleValue(heading);
142
143                                      // calculate the difference between the indicated heading
144                                      // and the selected heading for use with an autopilot
145         static SGPropertyNode *bnode
146         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
147         double diff = 0;
148         if ( bnode ){
149                 diff = bnode->getDoubleValue() - heading;
150                 if ( diff < -180.0 ) { diff += 360.0; }
151                 if ( diff > 180.0 ) { diff -= 360.0; }
152                 _error_node->setDoubleValue( diff );
153         }   
154                                      // calculate the difference between the indicated heading
155                                      // and the selected nav1 radial for use with an autopilot
156         SGPropertyNode *nnode
157         = fgGetNode( "/instrumentation/nav/radials/selected-deg", true );
158         double ndiff = 0;
159         if ( nnode ){
160                 ndiff = nnode->getDoubleValue() - heading;
161                 if ( ndiff < -180.0 ) { ndiff += 360.0; }
162                 if ( ndiff > 180.0 ) { ndiff -= 360.0; }
163                 _nav1_error_node->setDoubleValue( ndiff );
164         }   
165
166
167 }
168
169 // end of heading_indicator_fg.cxx