-// heading_indicator_dg.cxx - a Directional Gyro (DG) compass.
-// Based on the vacuum driven Heading Indicator Written by David Megginson,
-// started 2002.
-//
-// Written by Vivian Meazza, started 2005.
-//
-// This file is in the Public Domain and comes with no warranty.
-
-#include <simgear/compiler.h>
-#include <iostream>
-#include <string>
-#include <sstream>
-
-#include <Main/fg_props.hxx>
-#include <Main/util.hxx>
-
-#include "heading_indicator_dg.hxx"
-
-
-HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :
- name("heading-indicator-dg"),
- num(0)
-{
- int i;
- for ( i = 0; i < node->nChildren(); ++i ) {
- SGPropertyNode *child = node->getChild(i);
- string cname = child->getName();
- string cval = child->getStringValue();
- if ( cname == "name" ) {
- name = cval;
- } else if ( cname == "number" ) {
- num = child->getIntValue();
- } else {
- SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );
- if ( name.length() ) {
- SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
- }
- }
- }
-}
-
-HeadingIndicatorDG::HeadingIndicatorDG ()
-{
-}
-
-HeadingIndicatorDG::~HeadingIndicatorDG ()
-{
-}
-
-void
-HeadingIndicatorDG::init ()
-{
- string branch;
- branch = "/instrumentation/" + name;
-
+// heading_indicator_dg.cxx - a Directional Gyro (DG) compass.\r
+// Based on the vacuum driven Heading Indicator Written by David Megginson,\r
+// started 2002.\r
+//\r
+// Written by Vivian Meazza, started 2005.\r
+//\r
+// This file is in the Public Domain and comes with no warranty.\r
+\r
+#include <simgear/compiler.h>\r
+#include <simgear/sg_inlines.h>\r
+#include <iostream>\r
+#include <string>\r
+#include <sstream>\r
+\r
+#include <Main/fg_props.hxx>\r
+#include <Main/util.hxx>\r
+\r
+#include "heading_indicator_dg.hxx"\r
+\r
+\r
+HeadingIndicatorDG::HeadingIndicatorDG ( SGPropertyNode *node ) :\r
+ name("heading-indicator-dg"),\r
+ num(0)\r
+{\r
+ int i;\r
+ for ( i = 0; i < node->nChildren(); ++i ) {\r
+ SGPropertyNode *child = node->getChild(i);\r
+ string cname = child->getName();\r
+ string cval = child->getStringValue();\r
+ if ( cname == "name" ) {\r
+ name = cval;\r
+ } else if ( cname == "number" ) {\r
+ num = child->getIntValue();\r
+ } else {\r
+ SG_LOG( SG_INSTR, SG_WARN, "Error in DG heading-indicator config logic" );\r
+ if ( name.length() ) {\r
+ SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+HeadingIndicatorDG::HeadingIndicatorDG ()\r
+{\r
+}\r
+\r
+HeadingIndicatorDG::~HeadingIndicatorDG ()\r
+{\r
+}\r
+\r
+void\r
+HeadingIndicatorDG::init ()\r
+{\r
+ string branch;\r
+ branch = "/instrumentation/" + name;\r
+\r
_heading_in_node = fgGetNode("/orientation/heading-deg", true);
- SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
- _offset_node = node->getChild("offset-deg", 0, true);
- _serviceable_node = node->getChild("serviceable", 0, true);
- _error_node = node->getChild("heading-bug-error-deg", 0, true);
- _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);
- _heading_out_node = node->getChild("indicated-heading-deg", 0, true);
- _last_heading_deg = (_heading_in_node->getDoubleValue() +
- _offset_node->getDoubleValue());
- _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);
-}
-
-void
-HeadingIndicatorDG::bind ()
-{
- std::ostringstream temp;
- string branch;
- temp << num;
- branch = "/instrumentation/" + name + "[" + temp.str() + "]";
-
- fgTie((branch + "/serviceable").c_str(),
- &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
- fgTie((branch + "/spin").c_str(),
- &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
-}
-
-void
-HeadingIndicatorDG::unbind ()
-{
- std::ostringstream temp;
- string branch;
- temp << num;
- branch = "/instrumentation/" + name + "[" + temp.str() + "]";
-
- fgUntie((branch + "/serviceable").c_str());
- fgUntie((branch + "/spin").c_str());
-}
-
-void
-HeadingIndicatorDG::update (double dt)
-{
- // Get the spin from the gyro
- _gyro.set_power_norm(_electrical_node->getDoubleValue());
-
- _gyro.update(dt);
- double spin = _gyro.get_spin_norm();
-
- // No time-based precession for a flux gate compass
- // No magvar
- double offset = 0;
-
- // TODO: movement-induced error
-
- // Next, calculate the indicated heading,
- // introducing errors.
- double factor = 100 * (spin * spin * spin * spin * spin * spin);
- double heading = _heading_in_node->getDoubleValue();
-
- // Now, we have to get the current
- // heading and the last heading into
- // the same range.
- while ((heading - _last_heading_deg) > 180)
- _last_heading_deg += 360;
- while ((heading - _last_heading_deg) < -180)
- _last_heading_deg -= 360;
-
- heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);
- _last_heading_deg = heading;
-
- heading += offset;
- while (heading < 0)
- heading += 360;
- while (heading > 360)
- heading -= 360;
-
- _heading_out_node->setDoubleValue(heading);
-
- // calculate the difference between the indicacted heading
- // and the selected heading for use with an autopilot
- static SGPropertyNode *bnode
- = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
- if ( bnode ) {
- double diff = bnode->getDoubleValue() - heading;
- if ( diff < -180.0 ) { diff += 360.0; }
- if ( diff > 180.0 ) { diff -= 360.0; }
- _error_node->setDoubleValue( diff );
+ _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
+ _g_node = fgGetNode("/accelerations/pilot-g", true);
+
+\r
+ SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );\r
+ _offset_node = node->getChild("offset-deg", 0, true);\r
+ _serviceable_node = node->getChild("serviceable", 0, true);\r
+ _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);\r
+ _error_node = node->getChild("error-deg", 0, true);\r
+ _nav1_error_node = node->getChild("nav1-course-error-deg", 0, true);\r
+ _heading_out_node = node->getChild("indicated-heading-deg", 0, true);\r
+ _align_node = node->getChild("align-deg", 0, true);\r
+\r
+ _electrical_node = fgGetNode("/systems/electrical/outputs/DG", true);\r
+\r
+ _align_node->setDoubleValue(0);\r
+ _error_node->setDoubleValue(0);\r
+\r
+ _last_heading_deg = (_heading_in_node->getDoubleValue() +\r
+ _offset_node->getDoubleValue() + _align_node->getDoubleValue());\r
+}\r
+\r
+void\r
+HeadingIndicatorDG::bind ()\r
+{\r
+ std::ostringstream temp;\r
+ string branch;\r
+ temp << num;\r
+ branch = "/instrumentation/" + name + "[" + temp.str() + "]";\r
+\r
+ fgTie((branch + "/serviceable").c_str(),\r
+ &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);\r
+ fgTie((branch + "/spin").c_str(),\r
+ &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);\r
+}\r
+\r
+void\r
+HeadingIndicatorDG::unbind ()\r
+{\r
+ std::ostringstream temp;\r
+ string branch;\r
+ temp << num;\r
+ branch = "/instrumentation/" + name + "[" + temp.str() + "]";\r
+\r
+ fgUntie((branch + "/serviceable").c_str());\r
+ fgUntie((branch + "/spin").c_str());\r
+}\r
+\r
+void\r
+HeadingIndicatorDG::update (double dt)\r
+{\r
+ // Get the spin from the gyro\r
+ _gyro.set_power_norm(_electrical_node->getDoubleValue());\r
+\r
+ _gyro.update(dt);\r
+ double spin = _gyro.get_spin_norm();\r
+\r
+ // Next, calculate time-based precession\r
+ double offset = _offset_node->getDoubleValue();\r
+ offset -= dt * (0.25 / 60.0); // 360deg/day\r
+ SG_NORMALIZE_RANGE(offset, -360.0, 360.0);\r
+\r
+ // No magvar - set the alignment manually\r
+ double align = _align_node->getDoubleValue();\r
+\r
+ // Movement-induced error\r
+ double yaw_rate = _yaw_rate_node->getDoubleValue();\r
+ double error = _error_node->getDoubleValue();\r
+ double g = _g_node->getDoubleValue();
+ int sign = 0;
+
+ if ( fabs ( yaw_rate ) > 5 ) {
+ sign = (yaw_rate < 0 ? -1 : 1);
+ error += 0.033 * -yaw_rate * dt ;
}
- // calculate the difference between the indicated heading
- // and the selected nav1 radial for use with an autopilot
- SGPropertyNode *nnode
- = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );
- if ( nnode ) {
- double diff = nnode->getDoubleValue() - heading;
- if ( diff < -180.0 ) { diff += 360.0; }
- if ( diff > 180.0 ) { diff -= 360.0; }
- _nav1_error_node->setDoubleValue( diff );
+
+ if ( g > 1.5 || g < -0.5){
+ sign = (g < 0 ? -1 : 1);
+ error += 0.033 * g * dt;
}
-}
-// end of heading_indicator_fg.cxx
+ _error_node->setDoubleValue(error);
+
+ // Next, calculate the indicated heading,\r
+ // introducing errors.\r
+ double factor = 100 * (spin * spin * spin * spin * spin * spin);\r
+ double heading = _heading_in_node->getDoubleValue();\r
+\r
+ // Now, we have to get the current\r
+ // heading and the last heading into\r
+ // the same range.\r
+ while ((heading - _last_heading_deg) > 180)\r
+ _last_heading_deg += 360;\r
+ while ((heading - _last_heading_deg) < -180)\r
+ _last_heading_deg -= 360;\r
+\r
+ heading = fgGetLowPass(_last_heading_deg, heading, dt * factor);\r
+ _last_heading_deg = heading;\r
+\r
+ heading += offset + align + error;\r
+ SG_NORMALIZE_RANGE(heading, 0.0, 360.0);\r
+\r
+ _heading_out_node->setDoubleValue(heading);\r
+\r
+ // calculate the difference between the indicacted heading\r
+ // and the selected heading for use with an autopilot\r
+ static SGPropertyNode *bnode\r
+ = fgGetNode( "/autopilot/settings/heading-bug-deg", false );\r
+ if ( bnode ) {\r
+ double diff = bnode->getDoubleValue() - heading;\r
+ if ( diff < -180.0 ) { diff += 360.0; }\r
+ if ( diff > 180.0 ) { diff -= 360.0; }\r
+ _heading_bug_error_node->setDoubleValue( diff );\r
+ }\r
+ // calculate the difference between the indicated heading\r
+ // and the selected nav1 radial for use with an autopilot\r
+ SGPropertyNode *nnode\r
+ = fgGetNode( "/instrumentation/nav/radials/selected-deg", false );\r
+ if ( nnode ) {\r
+ double diff = nnode->getDoubleValue() - heading;\r
+ if ( diff < -180.0 ) { diff += 360.0; }\r
+ if ( diff > 180.0 ) { diff -= 360.0; }\r
+ _nav1_error_node->setDoubleValue( diff );\r
+ }\r
+}\r
+\r
+// end of heading_indicator_dg.cxx\r
-// heading_indicator.hxx - a vacuum-powered heading indicator.
-// Written by David Megginson, started 2002.
-//
-// This file is in the Public Domain and comes with no warranty.
-
-
-#ifndef __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX
-#define __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX 1
-
-
-#include <simgear/props/props.hxx>
-#include <simgear/structure/subsystem_mgr.hxx>
-
-#include "gyro.hxx"
-
-
-/**
- * Model an electrically-powered heading indicator.
- *
- * Input properties:
- *
- * /instrumentation/"name"/serviceable
- * /instrumentation/"name"/spin
- * /instrumentation/"name"/offset-deg
- * /orientation/heading-deg
- * /systems/electrical/outputs/DG
- *
- * Output properties:
- *
- * /instrumentation/"name"/indicated-heading-deg
- */
-class HeadingIndicatorDG : public SGSubsystem
-{
-
-public:
-
- HeadingIndicatorDG ( SGPropertyNode *node );
- HeadingIndicatorDG ();
- virtual ~HeadingIndicatorDG ();
-
- virtual void init ();
- virtual void bind ();
- virtual void unbind ();
- virtual void update (double dt);
-
-private:
-
- Gyro _gyro;
- double _last_heading_deg;
-
- string name;
- int num;
- //string vacuum_system;
-
- SGPropertyNode_ptr _offset_node;
- SGPropertyNode_ptr _heading_in_node;
- SGPropertyNode_ptr _serviceable_node;
- SGPropertyNode_ptr _heading_out_node;
- SGPropertyNode_ptr _electrical_node;
- SGPropertyNode_ptr _error_node;
- SGPropertyNode_ptr _nav1_error_node;
-};
-
-#endif // __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX
+// heading_indicator.hxx - a vacuum-powered heading indicator.\r
+// Written by David Megginson, started 2002.\r
+//\r
+// This file is in the Public Domain and comes with no warranty.\r
+\r
+\r
+#ifndef __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX\r
+#define __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX 1\r
+\r
+\r
+#include <simgear/props/props.hxx>\r
+#include <simgear/structure/subsystem_mgr.hxx>\r
+#include <simgear/math/sg_random.h>\r
+\r
+#include "gyro.hxx"\r
+\r
+\r
+/**\r
+ * Model an electrically-powered heading indicator.\r
+ *\r
+ * Input properties:\r
+ *\r
+ * /instrumentation/"name"/serviceable\r
+ * /instrumentation/"name"/spin\r
+ * /instrumentation/"name"/offset-deg\r
+ * /orientation/heading-deg\r
+ * /systems/electrical/outputs/DG\r
+ *\r
+ * Output properties:\r
+ *\r
+ * /instrumentation/"name"/indicated-heading-deg\r
+ */\r
+class HeadingIndicatorDG : public SGSubsystem\r
+{\r
+\r
+public:\r
+\r
+ HeadingIndicatorDG ( SGPropertyNode *node );\r
+ HeadingIndicatorDG ();\r
+ virtual ~HeadingIndicatorDG ();\r
+\r
+ virtual void init ();\r
+ virtual void bind ();\r
+ virtual void unbind ();\r
+ virtual void update (double dt);\r
+\r
+private:\r
+\r
+ Gyro _gyro;\r
+ double _last_heading_deg;\r
+\r
+ string name;\r
+ int num;\r
+ //string vacuum_system;\r
+\r
+ SGPropertyNode_ptr _offset_node;\r
+ SGPropertyNode_ptr _heading_in_node;\r
+ SGPropertyNode_ptr _serviceable_node;\r
+ SGPropertyNode_ptr _heading_out_node;\r
+ SGPropertyNode_ptr _electrical_node;\r
+ SGPropertyNode_ptr _error_node;\r
+ SGPropertyNode_ptr _nav1_error_node;\r
+ SGPropertyNode_ptr _align_node;\r
+ SGPropertyNode_ptr _yaw_rate_node;\r
+ SGPropertyNode_ptr _heading_bug_error_node;\r
+ SGPropertyNode_ptr _g_node;\r
+};\r
+\r
+#endif // __INSTRUMENTS_HEADING_INDICATOR_ELEC_HXX\r