<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://www.fsdeveloper.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dragonflightdesign</id>
	<title>FSDeveloper Wiki - User contributions [en-gb]</title>
	<link rel="self" type="application/atom+xml" href="http://www.fsdeveloper.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dragonflightdesign"/>
	<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php/Special:Contributions/Dragonflightdesign"/>
	<updated>2026-05-27T10:28:31Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=11392</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=11392"/>
		<updated>2023-11-05T11:28:37Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code, but check that you haven&#039;t already declared the module_vars under different names.&lt;br /&gt;
&lt;br /&gt;
Edited 22 August 2020 to replace the menu time change with a more reliable trap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 MODULE_VAR tick18 = { TICK18 };&lt;br /&gt;
 &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
   int min_check = 0; &lt;br /&gt;
   int force_update_period = 5;  // Number of minutes difference between menu and clock that will force a clock update&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     // Apply the current minutes to the check variable&lt;br /&gt;
     min_check = mins;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 24 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust for needle positioning&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     x = localseconds.var_value.n;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       // Apply the actual current minutes to the check variable&lt;br /&gt;
       min_check = (int)mins/60;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Trap a time change by menu. &lt;br /&gt;
   // A difference of five minutes or more will trigger a clock update&lt;br /&gt;
   x = (int)localminutes.var_value.n;&lt;br /&gt;
   if (abs(x - min_check) &amp;gt; force_update_period)set_clock = 0;&lt;br /&gt;
  &lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=11391</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=11391"/>
		<updated>2023-11-05T11:27:13Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Made time differential into a variable (force_update_period) instead of a hard-coded figure 5 minutes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code, but check that you haven&#039;t already declared the module_vars under different names.&lt;br /&gt;
&lt;br /&gt;
Edited 22 August 2020 to replace the menu time change with a more reliable trap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 MODULE_VAR tick18 = { TICK18 };&lt;br /&gt;
 &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
   int min_check = 0; &lt;br /&gt;
   int force_update_period = 5;  // Number of minutes difference between menu and clock that will force a clock update&lt;br /&gt;
&lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     // Apply the current minutes to the check variable&lt;br /&gt;
     min_check = mins;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 24 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust for needle positioning&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     x = localseconds.var_value.n;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       // Apply the actual current minutes to the check variable&lt;br /&gt;
       min_check = (int)mins/60;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Trap a time change by menu. &lt;br /&gt;
   // A difference of five minutes or more will trigger a clock update&lt;br /&gt;
   x = (int)localminutes.var_value.n;&lt;br /&gt;
   if (abs(x - min_check) &amp;gt; force_update_period)set_clock = 0;&lt;br /&gt;
&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11382</id>
		<title>C: Battery Discharge Rate</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11382"/>
		<updated>2023-04-30T11:12:28Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Updated 30/04/2023 to use a percentage charge/drain rate per hour as I found an edge case that would cause failure if a specific voltage charge/drain was used.&lt;br /&gt;
&lt;br /&gt;
//----------------------&lt;br /&gt;
&lt;br /&gt;
There&#039;s a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_1SEC event. By habit I prefix all SimConnect functions with &#039;sc_&#039;.&lt;br /&gt;
&lt;br /&gt;
 hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, &amp;quot;ELECTRICAL BATTERY VOLTAGE&amp;quot;, &amp;quot;Volts&amp;quot;, SIMCONNECT_DATATYPE_FLOAT64, 0);&lt;br /&gt;
&lt;br /&gt;
 case CLIENT_1SEC:&lt;br /&gt;
 {&lt;br /&gt;
   // Battery charger.&lt;br /&gt;
   // - dis_charge_rate = charge or drain rate in percentage of system_voltage per hour e.g. 2.4 will give a 10% charge, -3.6 will give a 15% drain&lt;br /&gt;
   // - battery_voltage should contain the current battery voltage obtained from the sim&lt;br /&gt;
   // - system_voltage is the battery max value e.g. 24 (volts)&lt;br /&gt;
   sc_battery_charger(dis_charge_rate, battery_voltage, system_voltage);&lt;br /&gt;
 }&lt;br /&gt;
 break;&lt;br /&gt;
&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 // This is called from the CLIENT_1SEC event.&lt;br /&gt;
 // Set the battery voltage to avoid a long-standing bug with a sub-twenty minute drain time&lt;br /&gt;
 // Setting charge_discharge_rate to one hundred percent prevents any battery drain at all&lt;br /&gt;
 // Setting charge_discharge_rate to zero will discharge at the default rate&lt;br /&gt;
 // Setting between one and 99 will charge/discharge at that percentage per hour&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 void sc_battery_charger(double charge_discharge_rate, double current_volts, double target_volts)&lt;br /&gt;
 {&lt;br /&gt;
   double volts_out = 0;&lt;br /&gt;
   double rate = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Rate setting&lt;br /&gt;
   if (charge_discharge_rate &amp;gt;= 100.0)volts_out = target_volts;		// No discharge&lt;br /&gt;
   else if (charge_discharge_rate)&lt;br /&gt;
   {&lt;br /&gt;
     rate = charge_discharge_rate / 3600; // 3600 = 1 hour&lt;br /&gt;
     //Add the charge to the current voltage (negative for discharge)&lt;br /&gt;
     volts_out = current_volts + rate;&lt;br /&gt;
     // Sanity check&lt;br /&gt;
     if (volts_out &amp;gt; target_volts) volts_out = target_volts;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   // Set the voltage on the sim&lt;br /&gt;
   if (rate)SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &amp;amp;volts_out);&lt;br /&gt;
&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11381</id>
		<title>C: Battery Discharge Rate</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11381"/>
		<updated>2023-04-30T11:10:28Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Updated 30/04/2023 to use a percentage charge/drain rate per hour as I found an edge case that would cause failure if a specific voltage charge/drain was used.&lt;br /&gt;
&lt;br /&gt;
//----------------------&lt;br /&gt;
&lt;br /&gt;
There&#039;s a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_1SEC event. By habit I prefix all SimConnect functions with &#039;sc_&#039;.&lt;br /&gt;
&lt;br /&gt;
 hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, &amp;quot;ELECTRICAL BATTERY VOLTAGE&amp;quot;, &amp;quot;Volts&amp;quot;, SIMCONNECT_DATATYPE_FLOAT64, 0);&lt;br /&gt;
&lt;br /&gt;
 case CLIENT_1SEC:&lt;br /&gt;
 {&lt;br /&gt;
   // Battery charger.&lt;br /&gt;
   // - dis_charge_rate = charge or drain rate in percentage of system_voltage per hour&lt;br /&gt;
   // - battery_voltage should contain the current battery voltage obtained from the sim&lt;br /&gt;
   // - system_voltage is the battery max value e.g. 24 (volts)&lt;br /&gt;
   sc_battery_charger(dis_charge_rate, battery_voltage, system_voltage);&lt;br /&gt;
 }&lt;br /&gt;
 break;&lt;br /&gt;
&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 // This is called from the CLIENT_1SEC event.&lt;br /&gt;
 // Set the battery voltage to avoid a long-standing bug with a sub-twenty minute drain time&lt;br /&gt;
 // Setting charge_discharge_rate to one hundred percent prevents any battery drain at all&lt;br /&gt;
 // Setting charge_discharge_rate to zero will discharge at the default rate&lt;br /&gt;
 // Setting between one and 99 will charge/discharge at that percentage per hour&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 void sc_battery_charger(double charge_discharge_rate, double current_volts, double target_volts)&lt;br /&gt;
 {&lt;br /&gt;
   double volts_out = 0;&lt;br /&gt;
   double rate = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Rate setting&lt;br /&gt;
   if (charge_discharge_rate &amp;gt;= 100.0)volts_out = target_volts;		// No discharge&lt;br /&gt;
   else if (charge_discharge_rate)&lt;br /&gt;
   {&lt;br /&gt;
     rate = charge_discharge_rate / 3600; // 3600 = 1 hour&lt;br /&gt;
     //Add the charge to the current voltage (negative for discharge)&lt;br /&gt;
     volts_out = current_volts + rate;&lt;br /&gt;
     // Sanity check&lt;br /&gt;
     if (volts_out &amp;gt; target_volts) volts_out = target_volts;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   // Set the voltage on the sim&lt;br /&gt;
   if (rate)SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &amp;amp;volts_out);&lt;br /&gt;
&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11380</id>
		<title>C: Battery Discharge Rate</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=11380"/>
		<updated>2023-04-30T11:06:39Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Updated 30/04/2023 to use a percentage charge/drain rate per hour as I found an edge case that would cause failure if a specific voltage charge/drain was used.&lt;br /&gt;
&lt;br /&gt;
//----------------------&lt;br /&gt;
&lt;br /&gt;
There&#039;s a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_1SEC event. By habit I prefix all SimConnect functions with &#039;sc_&#039;.&lt;br /&gt;
&lt;br /&gt;
 hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, &amp;quot;ELECTRICAL BATTERY VOLTAGE&amp;quot;, &amp;quot;Volts&amp;quot;, SIMCONNECT_DATATYPE_FLOAT64, 0);&lt;br /&gt;
&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 // This is called from the CLIENT_1SEC event.&lt;br /&gt;
 // Set the battery voltage to avoid a long-standing bug with a sub-twenty minute drain time&lt;br /&gt;
 // Setting charge_discharge_rate to one hundred percent prevents any battery drain at all&lt;br /&gt;
 // Setting charge_discharge_rate to zero will discharge at the default rate&lt;br /&gt;
 // Setting between one and 99 will charge/discharge at that percentage per hour&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 void sc_battery_charger(double charge_discharge_rate, double current_volts, double target_volts)&lt;br /&gt;
 {&lt;br /&gt;
   double volts_out = 0;&lt;br /&gt;
   double rate = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Rate setting&lt;br /&gt;
   if (charge_discharge_rate &amp;gt;= 100.0)volts_out = target_volts;		// No discharge&lt;br /&gt;
   else if (charge_discharge_rate)&lt;br /&gt;
   {&lt;br /&gt;
     rate = charge_discharge_rate / 3600; // 3600 = 1 hour&lt;br /&gt;
     //Add the charge to the current voltage (negative for discharge)&lt;br /&gt;
     volts_out = current_volts + rate;&lt;br /&gt;
     // Sanity check&lt;br /&gt;
     if (volts_out &amp;gt; target_volts) volts_out = target_volts;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   // Set the voltage on the sim&lt;br /&gt;
   if (rate)SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &amp;amp;volts_out);&lt;br /&gt;
&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_FX_driver_using_aircraft.cfg_smoke_entries&amp;diff=10975</id>
		<title>C: FX driver using aircraft.cfg smoke entries</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_FX_driver_using_aircraft.cfg_smoke_entries&amp;diff=10975"/>
		<updated>2021-04-22T09:55:16Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Using the [smoke] entries to run fx&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are 99 available [smoke] entries available in the aircraft.cfg file and they don&#039;t have to be smoke-style fx; they can be any type of fx you like. One of the more common usages is to drive fx that are being used for aircraft lighting. The following function is a simple method of having a one-line fx driver:&lt;br /&gt;
&lt;br /&gt;
 void setFX(bool action, int number)&lt;br /&gt;
 {&lt;br /&gt;
   // Assign a character string&lt;br /&gt;
   char fx_number[20];&lt;br /&gt;
   // Ensure the string is clean&lt;br /&gt;
   memset(fx_number,0,20);&lt;br /&gt;
 &lt;br /&gt;
   // Set the fx to &#039;on&#039;&lt;br /&gt;
   if (action == true)&lt;br /&gt;
   {&lt;br /&gt;
     sprintf_s(fx_number, 20, &amp;quot;%d (&amp;gt;K:SMOKE_ON)&amp;quot;, number);&lt;br /&gt;
     execute_calculator_code(fx_number, NULL, NULL, NULL);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Set the fx to &#039;off&#039;&lt;br /&gt;
   if (action == false)&lt;br /&gt;
   {&lt;br /&gt;
     sprintf_s(fx_number, 20, &amp;quot;%d (&amp;gt;K:SMOKE_OFF)&amp;quot;, number);&lt;br /&gt;
     execute_calculator_code(fx_number, NULL, NULL, NULL);&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In you code you call it by&lt;br /&gt;
&lt;br /&gt;
 setFX(true, 14)&lt;br /&gt;
 &lt;br /&gt;
where &#039;action&#039; is true (on) or false (off) and the number is the corresponding entry in the aircraft.cfg file. To make your code easier to read you can assign a description to the fx number e.g.&lt;br /&gt;
&lt;br /&gt;
 const int landing_lights = 14;&lt;br /&gt;
 &lt;br /&gt;
so the call then becomes&lt;br /&gt;
&lt;br /&gt;
 setFX(true, landing_lights)  // Landing lights on&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]] [[Category: FSX]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=10974</id>
		<title>C: Battery Discharge Rate</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Battery_Discharge_Rate&amp;diff=10974"/>
		<updated>2021-04-19T10:23:51Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: SimConnect code to fix the battery discharge rate bug&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There&#039;s a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_4SEC event. By habit I prefix all SimConnect functions with &#039;sc_&#039;.&lt;br /&gt;
&lt;br /&gt;
 hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, &amp;quot;ELECTRICAL BATTERY VOLTAGE&amp;quot;, &amp;quot;Volts&amp;quot;, SIMCONNECT_DATATYPE_FLOAT64, 0);&lt;br /&gt;
&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 // Set the battery voltage to avoid a long-standing bug&lt;br /&gt;
 // with a sub-twenty minute drain time&lt;br /&gt;
 // Setting rate to one prevents any battery drain at all&lt;br /&gt;
 // Setting rate to zero will discharge at the default rate&lt;br /&gt;
 // Any other value between zero and one will add/subtract from the current volts&lt;br /&gt;
 // *******************************************************&lt;br /&gt;
 void sc_battery_charger(HANDLE hSimConnect, double charge_discharge_rate, double current_volts)&lt;br /&gt;
 {&lt;br /&gt;
   double volts_out = 0;&lt;br /&gt;
   double rate = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Sanity check&lt;br /&gt;
   if (charge_discharge_rate &amp;lt; 0 || charge_discharge_rate &amp;gt; 1.0)rate = 1.0;&lt;br /&gt;
   else rate = charge_discharge_rate;&lt;br /&gt;
   //Add the charge rate&lt;br /&gt;
   volts_out = current_volts + rate;&lt;br /&gt;
   // Sanity check&lt;br /&gt;
   if (volts_out &amp;gt; 24.0) volts_out = 24.0;&lt;br /&gt;
   // Set the voltage on the sim&lt;br /&gt;
   SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &amp;amp;volts_out);&lt;br /&gt;
 &lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10769</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10769"/>
		<updated>2020-08-22T14:51:02Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code, but check that you haven&#039;t already declared the module_vars under different names.&lt;br /&gt;
&lt;br /&gt;
Edited 22 August 2020 to replace the menu time change with a more reliable trap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 MODULE_VAR tick18 = { TICK18 };&lt;br /&gt;
 &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     // Apply the current minutes to the check variable&lt;br /&gt;
     min_check = mins;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 24 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust for needle positioning&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     x = localseconds.var_value.n;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       // Apply the actual current minutes to the check variable&lt;br /&gt;
       min_check = (int)mins/60;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Trap a time change by menu. &lt;br /&gt;
   // A difference of five minutes or more will trigger a clock update&lt;br /&gt;
   x = (int)localminutes.var_value.n;&lt;br /&gt;
   if (abs(x - min_check) &amp;gt; 5)set_clock = 0;&lt;br /&gt;
&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Vertical_Speed_Hold&amp;diff=10618</id>
		<title>Vertical Speed Hold</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Vertical_Speed_Hold&amp;diff=10618"/>
		<updated>2020-02-07T11:54:12Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Vertical speed hold has not worked since FS2K/FS2K2. The workround to get this to function is to set altitude hold to some unfeasible height for the aircraft if wanting a positive vertical speed and to zero feet if wanting a negative vertical speed. The following code snip then sets the required vertical speed.&lt;br /&gt;
&lt;br /&gt;
 if(vsi_target&amp;gt;0)send_key_event(KEY_AP_ALT_VAR_SET_ENGLISH,50000); 	//Positive VS&lt;br /&gt;
 else send_key_event(KEY_AP_ALT_VAR_SET_ENGLISH,0);			//Negative VS&lt;br /&gt;
 send_key_event(KEY_AP_VS_VAR_SET_ENGLISH,(int)vsi_target);		//Set required VS&lt;br /&gt;
 send_key_event(KEY_AP_ALT_HOLD_ON,0);					//Set alt hold on&lt;br /&gt;
&lt;br /&gt;
From information received from the Prepar3D team at Lockheed-Martin, the bug for this one is buried so deep that it can’t be fixed. They’ve removed it from the list of KEY_EVENTS in the later P3D SDKs.&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Vertical_Speed_Hold&amp;diff=10617</id>
		<title>Vertical Speed Hold</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Vertical_Speed_Hold&amp;diff=10617"/>
		<updated>2020-02-07T11:53:22Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Created page with &amp;quot;Vertical speed hold has not worked since FS2K/FS2K2. The workround to get this to function is to set altitude hold to some unfeasible height for the aircraft if wanting a posi...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Vertical speed hold has not worked since FS2K/FS2K2. The workround to get this to function is to set altitude hold to some unfeasible height for the aircraft if wanting a positive vertical speed and to zero feet if wanting a negative vertical speed. The following code snip then sets the required vertical speed.&lt;br /&gt;
&lt;br /&gt;
 if(vsi_target&amp;gt;0)send_key_event(KEY_AP_ALT_VAR_SET_ENGLISH,50000); 	//Positive VS&lt;br /&gt;
 else send_key_event(KEY_AP_ALT_VAR_SET_ENGLISH,0);			//Negative VS&lt;br /&gt;
 send_key_event(KEY_AP_VS_VAR_SET_ENGLISH,(int)vsi_target);		//Set required VS&lt;br /&gt;
 send_key_event(KEY_AP_ALT_HOLD_ON,0);					//Set alt hold on&lt;br /&gt;
&lt;br /&gt;
From information received from the Prepar3D team at Lockheed-Martin, the bug for this one is buried so deep that it can’t be fixed. They’ve removed it from the list of KEY_EVENTS in the later P3D SDKs.&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10616</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10616"/>
		<updated>2020-01-07T20:57:19Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. This is copy&#039;n&#039;paste code, but check that you haven&#039;t already declared the module_vars under different names; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0; // Travelling east travel_dir = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob (0 - 359 degrees)&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 double latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 double latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10615</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10615"/>
		<updated>2020-01-07T20:56:23Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code, but check that you haven&#039;t already declared the module_vars under different names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 MODULE_VAR tick18 = { TICK18 };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video and gauge processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = tick18.var_value.n;&lt;br /&gt;
   if (prev_tick != tick18.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
     if (tick18.var_value.n &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = tick18.var_value.n;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10614</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10614"/>
		<updated>2020-01-05T13:14:56Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0; // Travelling east travel_dir = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob (0 - 359 degrees)&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 double latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 double latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10613</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10613"/>
		<updated>2020-01-05T13:13:12Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0; // Travelling east travel_dir = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 double latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 double latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10612</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10612"/>
		<updated>2020-01-05T13:07:22Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: A directional gyro drift class for FSX and all versions of P3D&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 double latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 double latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10611</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10611"/>
		<updated>2020-01-05T12:25:58Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 double latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 double latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10610</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10610"/>
		<updated>2020-01-05T12:21:05Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double dgCard = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  dgCard = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (dgCard &amp;lt; 0)dgCard += 359;&lt;br /&gt;
  if (dgCard &amp;gt; 359)dgCard -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return dgCard;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10609</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10609"/>
		<updated>2020-01-05T12:18:43Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: A directional gyro drift class for FSX and all versions of P3D&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The alternative to global variables and a global update function is to have a local update function per directional gyro.&lt;br /&gt;
&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the DG gauge&lt;br /&gt;
 // &#039;knob&#039; is the current position of the adjustment knob in degrees (0 - 359)&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 double gyro1Update(double knob)&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  x = gyro1.get_hdg(latitude_nut1, knob);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (x &amp;lt; 0)x += 359;&lt;br /&gt;
  if (x &amp;gt; 359)x -= 359;&lt;br /&gt;
 &lt;br /&gt;
  return x;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10608</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10608"/>
		<updated>2020-01-05T12:10:42Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10607</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10607"/>
		<updated>2020-01-05T12:10:27Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // FS module_vars&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 MODULE_VAR elapsedsecs = { ELAPSED_SECONDS };&lt;br /&gt;
 MODULE_VAR acft_on_gnd = { AIRCRAFT_ON_GROUND };&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10606</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10606"/>
		<updated>2020-01-05T12:07:36Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   // Get the current magnetic heading&lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   // Instantiate a temporary drift calculation class&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   // Calculate the current drift&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   // Calculate the current drift offset from the magnetic heading&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10605</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10605"/>
		<updated>2020-01-05T12:02:41Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates which match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight. It has also been tested using figures from Ed William&#039;s Aviation Formulary ([http://edwilliams.org/avform.htm])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // &#039;&#039;&#039;Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&#039;&#039;&#039;&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10604</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10604"/>
		<updated>2020-01-05T11:57:40Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs. All will drift at marginally different rates but match real world standards. The code is copy&#039;n&#039;paste; the only section you need to change is below the &#039;Implementation&#039; header. This has been tested on an aircraft with three directional gyros (captain, first officer and flight engineer) on a four hour flight.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10603</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10603"/>
		<updated>2020-01-05T11:56:20Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs and all will drift at marginally different rates. The code is copy&#039;n&#039;paste; the only section you need to change is below the Implementation header. This has been tested on an aircarft with three directional gyros (captain, first officer and flight engineer).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //         |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10602</id>
		<title>C: Directional Gyro drift</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Directional_Gyro_drift&amp;diff=10602"/>
		<updated>2020-01-05T11:56:07Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Created page with &amp;quot;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There is only a single directional gyro class in FSX and P3D (PLANE HEADING DEGREES GYRO), so if you have multiple directional gyros they will all drift at the same rate. The following code implements a directional gyro class that can be applied to as many directional gyros as the aircraft needs and all will drift at marginally different rates. The code is copy&#039;n&#039;paste; the only section you need to change is below the Implementation header. This has been tested on an aircarft with three directional gyros (captain, first officer and flight engineer).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Helper functions&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 #define PI_LARGE                 3.1415926535897932384626433832795&lt;br /&gt;
 #define RADIANS_TO_DEGREE_FACTOR (180.0/PI_LARGE)&lt;br /&gt;
 #define RAD_TO_DEG(val)          (val)*RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 #define DEG_TO_RAD(val)          (val)/RADIANS_TO_DEGREE_FACTOR&lt;br /&gt;
 &lt;br /&gt;
 //General to get the current latitude and longitude of the aircraft&lt;br /&gt;
 double dblAcftCurrLat=0;&lt;br /&gt;
 double dblAcftCurrLon=0;&lt;br /&gt;
 &lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in radians&lt;br /&gt;
 double AcftCurrLonRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLon,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLon;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in radians&lt;br /&gt;
 double AcftCurrLatRad()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;,&amp;amp;dblAcftCurrLat,NULL,NULL);&lt;br /&gt;
   return dblAcftCurrLat;&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft longitude in degrees&lt;br /&gt;
 double AcftCurrLonDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LONGITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLon, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLon);&lt;br /&gt;
 }&lt;br /&gt;
 // -------------------------------------------------&lt;br /&gt;
 // Aircraft latitude in degrres&lt;br /&gt;
 double AcftCurrLatDeg()&lt;br /&gt;
 {&lt;br /&gt;
   execute_calculator_code(&amp;quot;(A:PLANE LATITUDE,Radians)&amp;quot;, &amp;amp;dblAcftCurrLat, NULL, NULL);&lt;br /&gt;
   return RAD_TO_DEG(dblAcftCurrLat);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Drift class explanation&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Definitions of variables&lt;br /&gt;
 // Real wander (RW)&lt;br /&gt;
 // Earth rotation (ER)&lt;br /&gt;
 // Latitude nut correction (LN)&lt;br /&gt;
 // Transport wander easterly or westerly (TW)&lt;br /&gt;
 //&lt;br /&gt;
 // Quadrant sign Table&lt;br /&gt;
 //     |N. Hem |S. Hem |&lt;br /&gt;
 //         |-------|-------|&lt;br /&gt;
 // ER      |   -   |   +   |&lt;br /&gt;
 // LN      |   +   |   -   |&lt;br /&gt;
 // TW east |   -   |   +   |&lt;br /&gt;
 // TW west |   +   |   -   |&lt;br /&gt;
 //&lt;br /&gt;
 // Total Drift (TD) = RW + ER + LN + TW&lt;br /&gt;
 // BUT: if we assume that we&#039;re dealing with a perfect gyro then RW = 0, so&lt;br /&gt;
 // TD = ER + LN + TW&lt;br /&gt;
 // Period must be in fractions of an hour e.g. 90 mins = 1.5 hours&lt;br /&gt;
 // Assumption: the latitude nut is always set in the northern hemisphere (positive sign)&lt;br /&gt;
 // Note also that in this implementation when calculating TW, (x) is already a sine from calculating ER, so TW = y*sin(x) becomes TW = y*x.&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class gyro_drift&lt;br /&gt;
 {&lt;br /&gt;
   double current_drift = 0;&lt;br /&gt;
 &lt;br /&gt;
 public:&lt;br /&gt;
 &lt;br /&gt;
   double calc(double lat_nut)&lt;br /&gt;
  {&lt;br /&gt;
    static double flight_start = 0, prev_lat = 0, prev_lon = 0, prev_lat_deg = 0, prev_lon_deg = 0, flight_progress = 0;&lt;br /&gt;
   double x = 0, y = 0, er = 0, ln = 0, tw = 0, curr_lat = 0, curr_lon = 0, curr_lat_deg = 0, curr_lon_deg = 0, flight_time = 0, curr_ew = 0, curr_ns = 0,  travel_dir = 0;&lt;br /&gt;
   // Travelling east = 1&lt;br /&gt;
   static double prev_ew = 0; // Previous longitude point was east (prev_ew = 1) or west (prev_ew = 0). See also curr_ew&lt;br /&gt;
   static double prev_ns = 0; // Previous longitude point was north (prev_ns = 1) or south (prev_ns = 0). See also curr_ns&lt;br /&gt;
 &lt;br /&gt;
   // Only update if we are flying&lt;br /&gt;
   if (!acft_on_gnd.var_value.n)&lt;br /&gt;
   {&lt;br /&gt;
    if (!flight_start)&lt;br /&gt;
    {&lt;br /&gt;
     flight_start = elapsedsecs.var_value.n + 30;&lt;br /&gt;
     flight_progress = flight_start;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
     // Calculate drift every thirty seconds&lt;br /&gt;
    if (elapsedsecs.var_value.n &amp;gt; flight_progress + 30)&lt;br /&gt;
    {&lt;br /&gt;
     flight_progress = elapsedsecs.var_value.n;&lt;br /&gt;
     // Flight time running total in seconds&lt;br /&gt;
     flight_time = flight_progress - flight_start;&lt;br /&gt;
     // Delay calculation start until the aircraft has been flying for two minutes&lt;br /&gt;
     if (flight_time &amp;gt; 120)&lt;br /&gt;
     {&lt;br /&gt;
      // ----------&lt;br /&gt;
      // Current position in radians&lt;br /&gt;
      curr_lat = AcftCurrLatRad();&lt;br /&gt;
      curr_lon = AcftCurrLonRad();&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Get the quadrant we are in&lt;br /&gt;
      curr_ns = AcftCurrLatDeg();&lt;br /&gt;
      curr_lat_deg = curr_ns;    // Needed in degrees for ER calculation&lt;br /&gt;
      if (curr_ns &amp;gt; 0)curr_ns = 1;&lt;br /&gt;
      curr_ew = AcftCurrLonDeg();&lt;br /&gt;
      curr_lon_deg = curr_ew;    // Needed in degrees for TW calculation&lt;br /&gt;
      if (curr_ew &amp;gt; 0)curr_ew = 1;&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate earth rotation - use the mean of the two known points&lt;br /&gt;
      // er = 15 x sin of latitude per hour&lt;br /&gt;
 &lt;br /&gt;
      x = int(abs(curr_lat_deg) + abs(prev_lat_deg)) / 2;&lt;br /&gt;
      x = sin(DEG_TO_RAD(x));&lt;br /&gt;
      // Convert flight time to fractions of an hour&lt;br /&gt;
      y = minsToDec((int)flight_time / 60);&lt;br /&gt;
      // x is already a sin so don&#039;t convert it&lt;br /&gt;
      er = (15 * x) * y;&lt;br /&gt;
 &lt;br /&gt;
      // Negative if in the northern hemisphere&lt;br /&gt;
      if (curr_ns)er = -abs(er);&lt;br /&gt;
 &lt;br /&gt;
       // ----------&lt;br /&gt;
      // Latitude nut error&lt;br /&gt;
      // ln = 15 x Sin (Latitude) in degrees per hour&lt;br /&gt;
      ln = (15 * sin(lat_nut)*flight_time);&lt;br /&gt;
      // Negative if in the southern hemisphere&lt;br /&gt;
      if (!curr_ns)ln = -abs(ln);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Transport wander&lt;br /&gt;
      // Get direction of travel - default travel_dir = 0 i.e. travelling west&lt;br /&gt;
      prev_lon_deg = abs(prev_lon_deg);&lt;br /&gt;
      curr_lon_deg = abs(curr_lon_deg);&lt;br /&gt;
      // Heading east in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading east from the western hemisphere to the eastern hemisphere&lt;br /&gt;
      if (!prev_ew &amp;amp;&amp;amp; curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       travel_dir = 1;&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the eastern hemisphere&lt;br /&gt;
      if ((prev_ew &amp;amp;&amp;amp; curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;gt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg - curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west in the western hemisphere&lt;br /&gt;
      if ((!prev_ew &amp;amp;&amp;amp; !curr_ew) &amp;amp;&amp;amp; (curr_lon_deg &amp;lt; prev_lon_deg))&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(curr_lon_deg - prev_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
      // Heading west from the eastern hemisphere to the western hemisphere&lt;br /&gt;
      if (prev_ew &amp;amp;&amp;amp; !curr_ew)&lt;br /&gt;
      {&lt;br /&gt;
       y = DEG_TO_RAD(prev_lon_deg + curr_lon_deg);&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      // Normal calculation is tw = y*sin(x), but x is already a sine (see calculation for er)&lt;br /&gt;
      tw = y*x;&lt;br /&gt;
      tw = RAD_TO_DEG(tw);&lt;br /&gt;
      // Negative if the direction of travel is west to east&lt;br /&gt;
      if (travel_dir)tw = -abs(tw);&lt;br /&gt;
 &lt;br /&gt;
      // ----------&lt;br /&gt;
      // Calculate the current drift&lt;br /&gt;
      current_drift = (er)+(ln)+(tw);&lt;br /&gt;
 &lt;br /&gt;
      // Done with the calculations, so set curr into prev&lt;br /&gt;
      prev_lat = curr_lat;&lt;br /&gt;
      prev_lon = curr_lon;&lt;br /&gt;
      prev_ns = curr_ns;&lt;br /&gt;
      prev_ew = curr_ew;&lt;br /&gt;
      prev_lat_deg = curr_lat_deg;&lt;br /&gt;
      prev_lon_deg = curr_lon_deg;&lt;br /&gt;
     }&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
    // Reset on landing / flight reload&lt;br /&gt;
    flight_start = 0;&lt;br /&gt;
    flight_progress = 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // Return drift&lt;br /&gt;
   return current_drift;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 // Directional gyro.&lt;br /&gt;
 // Calculate the drift to update the display&lt;br /&gt;
 // Offset is the value assigned by the adjustment knob&lt;br /&gt;
 // -----------------------------------------------------------------------&lt;br /&gt;
 class directional_gyro&lt;br /&gt;
 {&lt;br /&gt;
  double dgi_hdg;&lt;br /&gt;
 &lt;br /&gt;
 public: &lt;br /&gt;
 &lt;br /&gt;
  double get_hdg(double lat_nut, double offset)&lt;br /&gt;
  {&lt;br /&gt;
   double curr_hdg = 0;&lt;br /&gt;
 &lt;br /&gt;
   curr_hdg = hdg_mag.var_value.n;&lt;br /&gt;
   gyro_drift drift;&lt;br /&gt;
   dgi_hdg = drift.calc(lat_nut);&lt;br /&gt;
   dgi_hdg = dgi_hdg + offset + curr_hdg;&lt;br /&gt;
 &lt;br /&gt;
   return dgi_hdg;&lt;br /&gt;
  }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 // Implementation&lt;br /&gt;
 // *************************************************************************&lt;br /&gt;
 &lt;br /&gt;
 // Instantiate a class per directional gyro&lt;br /&gt;
 directional_gyro gyro1;&lt;br /&gt;
 directional_gyro gyro2;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // Randomise a latitude nut setting between 39 degrees north and 56 degrees north per gyro&lt;br /&gt;
 // Make sure this is only done once during aircraft load, otherwise you&#039;ll never get a stable setting&lt;br /&gt;
 latitude_nut1 = getRand(39, 56);&lt;br /&gt;
 latitude_nut2 = getRand(39, 56);&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Directional gyro indicators - &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
 // These variables must be global&lt;br /&gt;
 double gyro1DGAdjustment = 0;  // Gyro1 adjustment knob in degrees 0 - 359&lt;br /&gt;
 double gyro1DGCard = 0;        // Gyro1 heading display card 0 - 359&lt;br /&gt;
 &lt;br /&gt;
 double gyro2DGAdjustment = 0;&lt;br /&gt;
 double gyro2DGCard = 0;&lt;br /&gt;
 (etc.)&lt;br /&gt;
 &lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 // Update the directional gyro drift and display&lt;br /&gt;
 // Called from the _update section of the relevant DG gauge&lt;br /&gt;
 // ----------------------------------------------------------&lt;br /&gt;
 void dgUpdate()&lt;br /&gt;
 {&lt;br /&gt;
  double x = 0, y = 0;&lt;br /&gt;
 &lt;br /&gt;
  // Captain&#039;s directional gyro&lt;br /&gt;
  // Current degrees of the directional gyro adjustment knob&lt;br /&gt;
  x = gyro1DGAdjustment;&lt;br /&gt;
  // Calculate the offset and drift&lt;br /&gt;
  y = gyro1.get_hdg(latitude_nut1, x);&lt;br /&gt;
  // Correct any overshoot or undershoot in degrees&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  // Set the card to the corrected heading&lt;br /&gt;
  gyro1DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  // First Officer&lt;br /&gt;
  x = gyro2DGAdjustment;&lt;br /&gt;
  y = gyro2.get_hdg(latitude_nut2, x);&lt;br /&gt;
  if (y &amp;lt; 0)y += 359;&lt;br /&gt;
  if (y &amp;gt; 359)y -= 359;&lt;br /&gt;
  gyro2DGcard = y;&lt;br /&gt;
 &lt;br /&gt;
  return;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10601</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10601"/>
		<updated>2020-01-05T10:32:54Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video and gauge processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = getTick();&lt;br /&gt;
   if (prev_tick != getTick())&lt;br /&gt;
   {&lt;br /&gt;
     if (getTick() &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = getTick();&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10600</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10600"/>
		<updated>2020-01-05T10:28:52Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = getTick();&lt;br /&gt;
   if (prev_tick != getTick())&lt;br /&gt;
   {&lt;br /&gt;
     if (getTick() &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = getTick();&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10599</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10599"/>
		<updated>2020-01-05T10:27:52Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge update -&lt;br /&gt;
 // Can be used in any project without having to change anything&lt;br /&gt;
 // Can also be called from multiple clocks in the same aircraft&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = getTick();&lt;br /&gt;
   if (prev_tick != getTick())&lt;br /&gt;
   {&lt;br /&gt;
     if (getTick() &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = getTick();&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10598</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10598"/>
		<updated>2020-01-05T10:27:17Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Function to be called from the gauge - can be used in any project without having to change anything&lt;br /&gt;
 // Can also be called from multiple clocks in the same aircraft&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = getTick();&lt;br /&gt;
   if (prev_tick != getTick())&lt;br /&gt;
   {&lt;br /&gt;
     if (getTick() &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = getTick();&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10597</id>
		<title>Analog clock</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=Analog_clock&amp;diff=10597"/>
		<updated>2020-01-05T10:26:26Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: C: 12-hour analog clock&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they &#039;flick over&#039; - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy&#039;n&#039;paste code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
 // Time - 12 hour analog clock&lt;br /&gt;
 // Driven from seconds only to get better movement of the hands&lt;br /&gt;
 // 3,600 seconds to an hour&lt;br /&gt;
 // 43,200 seconds to twelve hours&lt;br /&gt;
 //----------------------------------------------------------&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 MODULE_VAR localsecond = { CLOCK_SECOND };&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Global clock hand variables for the gauge display &lt;br /&gt;
 double second_hand = 0;&lt;br /&gt;
 double minute_hand = 0;&lt;br /&gt;
 double hour_hand = 0;&lt;br /&gt;
 &lt;br /&gt;
  &lt;br /&gt;
 // Function to be called from the gauge - can be used in any project without having to change anything&lt;br /&gt;
 // Can also be called from multiple clocks in the same aircraft&lt;br /&gt;
 void analogClock()&lt;br /&gt;
 {&lt;br /&gt;
   int x = 0;&lt;br /&gt;
  &lt;br /&gt;
   static double mins = -1;&lt;br /&gt;
   static double hours = -1;&lt;br /&gt;
   static double prev_secs = -1;&lt;br /&gt;
   static double set_clock = 0;&lt;br /&gt;
   static double prev_tick = -1;&lt;br /&gt;
  &lt;br /&gt;
   // Trap a time change by menu. Video processing is suspended while the menu is active&lt;br /&gt;
   // Use this to force a clock update (set_clock = 0) when video processing resumes&lt;br /&gt;
   if (prev_tick == -1)prev_tick = getTick();&lt;br /&gt;
   if (prev_tick != getTick())&lt;br /&gt;
   {&lt;br /&gt;
     if (getTick() &amp;gt; prev_tick + 1)set_clock = 0;&lt;br /&gt;
     prev_tick = getTick();&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   // Set clock to the current simulator time&lt;br /&gt;
   if (set_clock == 0)&lt;br /&gt;
   {&lt;br /&gt;
     set_clock = 1;&lt;br /&gt;
     mins = localminutes.var_value.n;&lt;br /&gt;
     hours = localhours.var_value.n;&lt;br /&gt;
     // 12 hour trap&lt;br /&gt;
     if (hours &amp;gt; 12) hours -= 12;&lt;br /&gt;
     // Adjust needle positioning for when normal updates resume&lt;br /&gt;
     hours *= 3600;&lt;br /&gt;
     mins *= 60;&lt;br /&gt;
     hours += mins;&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
     // Resume normal updates&lt;br /&gt;
     second_hand = localsecond.var_value.n;&lt;br /&gt;
  &lt;br /&gt;
     x = (int)second_hand;&lt;br /&gt;
     if (x != prev_secs)&lt;br /&gt;
     {&lt;br /&gt;
       prev_secs = x;&lt;br /&gt;
       // Minute hand&lt;br /&gt;
       mins += 1;&lt;br /&gt;
       if (mins &amp;gt;= 3599)mins = 0;&lt;br /&gt;
       minute_hand = mins;&lt;br /&gt;
       // Hour hand&lt;br /&gt;
       hours += 1;&lt;br /&gt;
       // Reset if passing through 00.00 or 12.00&lt;br /&gt;
       if (hours &amp;gt;= 43199)hours = 0;&lt;br /&gt;
       hour_hand = hours;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Launching_an_external_.exe_from_within_a_gauge&amp;diff=10591</id>
		<title>C: Launching an external .exe from within a gauge</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Launching_an_external_.exe_from_within_a_gauge&amp;diff=10591"/>
		<updated>2018-08-20T12:16:21Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Created page with &amp;quot;This short piece of code allows you to launch an external executable (.exe file) and detect when the program has been started. Place the code inside the gauge callback.    //-...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This short piece of code allows you to launch an external executable (.exe file) and detect when the program has been started. Place the code inside the gauge callback.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //---------------------------------------------------------------------------&lt;br /&gt;
 // Gauge callback&lt;br /&gt;
 //---------------------------------------------------------------------------&lt;br /&gt;
 void FSAPI  aircraft_config_update(PGAUGEHDR pgauge, int service_id, PUSERDATA extra_data)&lt;br /&gt;
 {&lt;br /&gt;
   HINSTANCE ret = 0;&lt;br /&gt;
   static int retSuccess = 0;&lt;br /&gt;
  &lt;br /&gt;
   switch (service_id)&lt;br /&gt;
   {&lt;br /&gt;
    case PANEL_SERVICE_PRE_UPDATE:&lt;br /&gt;
 &lt;br /&gt;
      if (!retSuccess)&lt;br /&gt;
      {&lt;br /&gt;
        // Try to open NotePad&lt;br /&gt;
        ret = ShellExecute(NULL, &amp;quot;open&amp;quot;, &amp;quot;c:\\windows\\notepad.exe&amp;quot;, NULL, NULL, SW_SHOWNORMAL);&lt;br /&gt;
        // If ret &amp;gt; 32 i.e. success then set retSuccess to 1&lt;br /&gt;
        if (int(ret) &amp;gt; 32)retSuccess = 1;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
Note the use of PUSERDATA in the method header. This allows the code to be FSX / P3D agnostic but you must have the correct #ifdef somewhere in your project.&lt;br /&gt;
&lt;br /&gt;
 #ifdef _64BIT&lt;br /&gt;
   #define PUSERDATA UINT_PTR&lt;br /&gt;
  (etc.)&lt;br /&gt;
 #else&lt;br /&gt;
   #define PUSERDATA UINT32&lt;br /&gt;
   (etc.)&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Trapping_two_mouse_buttons_at_the_same_time&amp;diff=10559</id>
		<title>C: Trapping two mouse buttons at the same time</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Trapping_two_mouse_buttons_at_the_same_time&amp;diff=10559"/>
		<updated>2018-05-24T10:29:06Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Detecting multiple mouse actions isn’t quite as easy as it would appear. The natural thing would be to do this:&lt;br /&gt;
 &lt;br /&gt;
  if((mouse_flags &amp;amp; MOUSE_LEFTSINGLE)&amp;amp;&amp;amp;(mouse_flags &amp;amp; MOUSE_RIGHTSINGLE))&lt;br /&gt;
 &lt;br /&gt;
but the value of mouse_flags is a reflection of action, not state. One answer is to use variables to detect the mouse button actions.&lt;br /&gt;
&lt;br /&gt;
  // Add these to a global variable file&lt;br /&gt;
  bool bBtnLeft=false;&lt;br /&gt;
  bool bBtnRight=false;&lt;br /&gt;
  bool bBtnMid=false;&lt;br /&gt;
  int iBtnStatus=0;&lt;br /&gt;
&lt;br /&gt;
 // Inside the mouse callback&lt;br /&gt;
 BOOL FSAPI myMouse_mcb( PPIXPOINT relative_point, FLAGS32 mouse_flags )&lt;br /&gt;
 {&lt;br /&gt;
   static int initbtn = 0; // Used to detect which button is pressed first&lt;br /&gt;
  &lt;br /&gt;
   if (mouse_flags &amp;amp; MOUSE_LEFTSINGLE)&lt;br /&gt;
   {&lt;br /&gt;
     bBtnLeft = true;&lt;br /&gt;
     if (!initbtn)initbtn = 1;	// First button pressed - left&lt;br /&gt;
   }&lt;br /&gt;
   else if (mouse_flags &amp;amp; MOUSE_LEFTRELEASE)bBtnLeft = false;&lt;br /&gt;
  &lt;br /&gt;
   if (mouse_flags &amp;amp; MOUSE_RIGHTSINGLE)&lt;br /&gt;
   {&lt;br /&gt;
     bBtnRight = true;&lt;br /&gt;
     if (!initbtn)initbtn = 2;	// First button pressed - right&lt;br /&gt;
   }&lt;br /&gt;
   else if (mouse_flags &amp;amp; MOUSE_RIGHTRELEASE)bBtnRight = false;&lt;br /&gt;
  &lt;br /&gt;
   // Button down status&lt;br /&gt;
   if (bBtnLeft == false &amp;amp;&amp;amp; bBtnRight == false)iBtnStatus = 0, initbtn = 0;  // Neither&lt;br /&gt;
   if (bBtnLeft == true &amp;amp;&amp;amp; bBtnRight == false)iBtnStatus = 1;                // Left&lt;br /&gt;
   if (bBtnLeft == false &amp;amp;&amp;amp; bBtnRight == true)iBtnStatus = 2;                // Right&lt;br /&gt;
   if (bBtnLeft == true &amp;amp;&amp;amp; bBtnRight == true)iBtnStatus = 3;                 // Both&lt;br /&gt;
  &lt;br /&gt;
   // Action code here&lt;br /&gt;
   // Mouse buttons released&lt;br /&gt;
   if(!iBtnStatus)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Left mouse button only&lt;br /&gt;
   if(iBtnStatus==1)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Right mouse button only&lt;br /&gt;
   if(iBtnStatus==2)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Both mouse buttons pressed&lt;br /&gt;
   if (iBtnStatus==3)&lt;br /&gt;
   {&lt;br /&gt;
     if(initbtn==1)	// Left mouse button was pressed first&lt;br /&gt;
     {&lt;br /&gt;
     }&lt;br /&gt;
     if(initbtn==2)	// Right mouse button was pressed first&lt;br /&gt;
     {&lt;br /&gt;
     }		&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[category:Aircraft Design]] [[category:Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_Trapping_two_mouse_buttons_at_the_same_time&amp;diff=10558</id>
		<title>C: Trapping two mouse buttons at the same time</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_Trapping_two_mouse_buttons_at_the_same_time&amp;diff=10558"/>
		<updated>2018-05-24T10:27:59Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Created page with &amp;quot;Detecting multiple mouse actions isn’t quite as easy as it would appear. The natural thing would be to do this:     if((mouse_flags &amp;amp; MOUSE_LEFTSINGLE)&amp;amp;&amp;amp;(mouse_flags &amp;amp; MOUSE...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Detecting multiple mouse actions isn’t quite as easy as it would appear. The natural thing would be to do this:&lt;br /&gt;
 &lt;br /&gt;
  if((mouse_flags &amp;amp; MOUSE_LEFTSINGLE)&amp;amp;&amp;amp;(mouse_flags &amp;amp; MOUSE_RIGHTSINGLE))&lt;br /&gt;
 &lt;br /&gt;
but the value of mouse_flags is a reflection of action, not state. One answer is to use variables to detect the mouse button actions.&lt;br /&gt;
&lt;br /&gt;
  // Add these to a global variable file&lt;br /&gt;
  bool bBtnLeft=false;&lt;br /&gt;
  bool bBtnRight=false;&lt;br /&gt;
  bool bBtnMid=false;&lt;br /&gt;
  int iBtnStatus=0;&lt;br /&gt;
&lt;br /&gt;
 // Inside the mouse callback&lt;br /&gt;
 BOOL FSAPI myMouse_mcb( PPIXPOINT relative_point, FLAGS32 mouse_flags )&lt;br /&gt;
 {&lt;br /&gt;
   static int initbtn = 0; // Used to detect which button is pressed first&lt;br /&gt;
  &lt;br /&gt;
   if (mouse_flags &amp;amp; MOUSE_LEFTSINGLE)&lt;br /&gt;
   {&lt;br /&gt;
     bBtnLeft = true;&lt;br /&gt;
     if (!initbtn)initbtn = 1;	// First button pressed - left&lt;br /&gt;
   }&lt;br /&gt;
   else if (mouse_flags &amp;amp; MOUSE_LEFTRELEASE)bBtnLeft = false;&lt;br /&gt;
  &lt;br /&gt;
   if (mouse_flags &amp;amp; MOUSE_RIGHTSINGLE)&lt;br /&gt;
   {&lt;br /&gt;
     bBtnRight = true;&lt;br /&gt;
     if (!initbtn)initbtn = 2;	// First button pressed - right&lt;br /&gt;
   }&lt;br /&gt;
   else if (mouse_flags &amp;amp; MOUSE_RIGHTRELEASE)bBtnRight = false;&lt;br /&gt;
  &lt;br /&gt;
   // Button down status&lt;br /&gt;
   if (bBtnLeft == false &amp;amp;&amp;amp; bBtnRight == false)iBtnStatus = 0, initbtn = 0;  // Neither&lt;br /&gt;
   if (bBtnLeft == true &amp;amp;&amp;amp; bBtnRight == false)iBtnStatus = 1;                // Left&lt;br /&gt;
   if (bBtnLeft == false &amp;amp;&amp;amp; bBtnRight == true)iBtnStatus = 2;                // Right&lt;br /&gt;
   if (bBtnLeft == true &amp;amp;&amp;amp; bBtnRight == true)iBtnStatus = 3;                 // Both&lt;br /&gt;
  &lt;br /&gt;
   // Action code here&lt;br /&gt;
   // Mouse buttons released&lt;br /&gt;
   if(!iBtnStatus)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Left mouse button only&lt;br /&gt;
   if(iBtnStatus==1)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Right mouse button only&lt;br /&gt;
   if(iBtnStatus==2)&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
   else&lt;br /&gt;
   // Both mouse buttons pressed&lt;br /&gt;
   if (iBtnStatus==3)&lt;br /&gt;
   {&lt;br /&gt;
     if(initbtn==1)	// Left mouse button was pressed first&lt;br /&gt;
     {&lt;br /&gt;
     }&lt;br /&gt;
     if(initbtn==2)	// Right mouse button was pressed first&lt;br /&gt;
     {&lt;br /&gt;
     }		&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10557</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10557"/>
		<updated>2018-04-30T20:37:45Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed. The downside is that intrin.h may not be available in a non-Microsoft compiler. The advantage of using this over the more usual time-seeded random number generator is that this can be used on succeeding code lines in a gauge and will return a different number from each line. A time-seeded random number generator will not have had time (!) to update and will give you the same &#039;random&#039; number for a number of successive calls during a gauge update cycle.&lt;br /&gt;
&lt;br /&gt;
 iMin, iMax = the range of numbers to be randomised&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
 // ---------------------------&lt;br /&gt;
 // Random number generator&lt;br /&gt;
 // ---------------------------&lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;&lt;br /&gt;
 &lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
 &lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=P3Dv4_-_Updated_gauges.h_file&amp;diff=10556</id>
		<title>P3Dv4 - Updated gauges.h file</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=P3Dv4_-_Updated_gauges.h_file&amp;diff=10556"/>
		<updated>2018-04-30T18:04:37Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Link and file removed as the method of updating the gauges.h file was changed with the release of sd2gau37. All of the changes that were incorporated within the gauges.h file have now been moved to an external header.&lt;br /&gt;
&lt;br /&gt;
-Dai&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10555</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10555"/>
		<updated>2018-04-30T17:55:04Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed. The downside is that intrin.h may not be available in a non-Microsoft compiler.&lt;br /&gt;
&lt;br /&gt;
 iMin, iMax = the range of numbers to be randomised&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
 // ---------------------------&lt;br /&gt;
 // Random number generator&lt;br /&gt;
 // ---------------------------&lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;&lt;br /&gt;
 &lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
 &lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10554</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10554"/>
		<updated>2018-04-30T17:49:43Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
 &lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;&lt;br /&gt;
 &lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
 &lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10553</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10553"/>
		<updated>2018-04-30T11:11:02Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
 &lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;&lt;br /&gt;
 &lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
 &lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10552</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10552"/>
		<updated>2018-04-30T11:09:55Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;  &lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10551</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10551"/>
		<updated>2018-04-30T11:09:40Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
 int getRand(int iMin,int iMax)&lt;br /&gt;
 {&lt;br /&gt;
   unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
   i = __rdtsc();&lt;br /&gt;
   do&lt;br /&gt;
     i=(int)rand()%iMax;&lt;br /&gt;
   while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
   return (int)i;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10550</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10550"/>
		<updated>2018-04-30T11:09:04Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10549</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10549"/>
		<updated>2018-04-30T11:08:48Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
 #pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
// ------------------------------&lt;br /&gt;
//Randomiser - seed by CPU cycles&lt;br /&gt;
// ------------------------------&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10548</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10548"/>
		<updated>2018-04-30T11:08:29Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
[code]#include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
#pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// ------------------------------&lt;br /&gt;
//Randomiser - seed by CPU cycles&lt;br /&gt;
// ------------------------------&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10547</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10547"/>
		<updated>2018-04-30T11:07:59Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
[code]&lt;br /&gt;
#include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
#pragma intrinsic(__rdtsc)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// ------------------------------&lt;br /&gt;
//Randomiser - seed by CPU cycles&lt;br /&gt;
// ------------------------------&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;br /&gt;
[/code]&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10546</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10546"/>
		<updated>2018-04-30T11:05:22Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;#include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
#pragma intrinsic(__rdtsc)  &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
// ------------------------------&lt;br /&gt;
//Randomiser - seed by CPU cycles&lt;br /&gt;
// ------------------------------&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
	<entry>
		<id>http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10545</id>
		<title>C: A true random number generator</title>
		<link rel="alternate" type="text/html" href="http://www.fsdeveloper.com/wiki/index.php?title=C:_A_true_random_number_generator&amp;diff=10545"/>
		<updated>2018-04-30T11:04:51Z</updated>

		<summary type="html">&lt;p&gt;Dragonflightdesign: Random number generator&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This random number generator is about as close as it gets to being truly random. It works by using the number of CPU cycles from startup as the seed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;intrin.h&amp;gt;&lt;br /&gt;
#pragma intrinsic(__rdtsc)  &lt;br /&gt;
&lt;br /&gt;
// ------------------------------&lt;br /&gt;
//Randomiser - seed by CPU cycles&lt;br /&gt;
// ------------------------------&lt;br /&gt;
&lt;br /&gt;
int getRand(int iMin,int iMax)&lt;br /&gt;
{&lt;br /&gt;
  unsigned __int64 i;  &lt;br /&gt;
&lt;br /&gt;
  i = __rdtsc();&lt;br /&gt;
  do&lt;br /&gt;
    i=(int)rand()%iMax;&lt;br /&gt;
  while (i &amp;lt; iMin);&lt;br /&gt;
&lt;br /&gt;
  return (int)i;&lt;br /&gt;
}&lt;/div&gt;</summary>
		<author><name>Dragonflightdesign</name></author>
	</entry>
</feed>