Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Set heading and VS
Browse files Browse the repository at this point in the history
  • Loading branch information
David committed Jun 23, 2018
1 parent e3811a3 commit 39e23d6
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ You need ExtPlane Plugin installed ([Download](https://github.com/vranki/ExtPlan
* __[select / engage / turn on / arm / de-select / disengage / turn off / disarm / toggle] auto throttle__
* __[select / egnage / turn on / de-select / disengage / turn off] / toggle] v-nav__
* __[select / engage / turn on / de-select / disengage / turn off] / toggle] l-nav__
* Heading set
* set __heading__ to __XXX__
* Vertical speed set
* set __vertical speed__ to negative V thousand /& X hundred [fifty] [fpm / feet per minute / feet]
* __eg: set vertical speed to six thousand five hundred fifty; set vertical speed to six hundred; set vertical speed fifty__


__Cheat sheet:__
* __Bold text is Mandatory__
* Normal text is Optional
* __X => [zero / one / two / three / four / fiver / six / seven / eight / niner]__
* __T => [zero / one / two / three / four / fiver / six / seven]__
* __V => [zero / one / two / three / four / five / six]__

## Hard time speaking - train Windows to understand you better?
* Open Speech Recognition by clicking the Start button, clicking Control Panel, clicking Ease of Access, and then clicking Speech Recognition.
Expand Down
80 changes: 80 additions & 0 deletions X-Plane Voice Control/Commands/HeadingMCPControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
using System.Threading.Tasks;
using ExtPlaneNet;

namespace X_Plane_Voice_Control.Commands
{
class HeadingMCPControl : ControlTemplate
{

public HeadingMCPControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer)
{
var headingGrammar = new GrammarBuilder();
headingGrammar.Append("please", 0, 1);
headingGrammar.Append("set", 0, 1);
headingGrammar.Append("heading");
headingGrammar.Append("to", 0, 1);
headingGrammar.Append(Constants.NumberChoices, 3, 3);
headingGrammar.Append("please", 0, 1);
Grammar = new Grammar(headingGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(headingGrammar.DebugShowPhrases);
}
public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<double>("laminar/B738/autopilot/mcp_hdg_dial");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var presentHeading = (int)XPlaneInterface.GetDataRef<double>("laminar/B738/autopilot/mcp_hdg_dial").Value;
var stringHeading = Constants.StringNumbersToDigits(phrase);
stringHeading = stringHeading.Substring(stringHeading.Length - 5, 5);
var requestedHeading = int.Parse(stringHeading.Replace(" ", ""));
if (requestedHeading > 360)
{
SpeechSynthesizer.SpeakAsync("Cannot set heading bigger than 360");
return;
}

if (requestedHeading == 360)
requestedHeading = 0;

int wayToRotate;
if (presentHeading < requestedHeading)
{
var x = requestedHeading - presentHeading;
var y = presentHeading + (360 - requestedHeading);
wayToRotate = x <= y ? 1 : -1;
}
else
{
var x = (360 - presentHeading) + requestedHeading;
var y = presentHeading - requestedHeading;
wayToRotate = x <= y ? 1 : -1;
}

Task.Run(() =>
{
while (presentHeading != requestedHeading)
{
presentHeading += wayToRotate;
if (presentHeading == 360)
presentHeading = 0;
if (presentHeading == -1)
presentHeading = 359;
XPlaneInterface.SetDataRef("laminar/B738/autopilot/mcp_hdg_dial", presentHeading);
Thread.Sleep(2);
}
});
SpeechSynthesizer.SpeakAsync($"Setting heading to {stringHeading}");
}

}
}
88 changes: 88 additions & 0 deletions X-Plane Voice Control/Commands/VerticalSpeedMCPControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
using System.Threading.Tasks;
using ExtPlaneNet;

namespace X_Plane_Voice_Control.Commands
{
class VerticalSpeedMCPControl : ControlTemplate
{

public VerticalSpeedMCPControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer)
{
var headingGrammar = new GrammarBuilder();

var thousandGrammar = new GrammarBuilder();
thousandGrammar.Append(Constants.VerticalSpeedNumberChoices);
thousandGrammar.Append("thousand");

var hundredGrammar = new GrammarBuilder();
hundredGrammar.Append(Constants.ClassicNumberChoices);
hundredGrammar.Append("hundred");

var altitudeGrammar = new GrammarBuilder();
altitudeGrammar.Append(thousandGrammar, 0, 1);
altitudeGrammar.Append(hundredGrammar, 0, 1);
headingGrammar.Append("please", 0, 1);
headingGrammar.Append("set", 0, 1);
headingGrammar.Append("vertical speed");
headingGrammar.Append("to", 0, 1);
headingGrammar.Append("negative", 0, 1);
headingGrammar.Append(altitudeGrammar);
headingGrammar.Append("fifty", 0, 1);
headingGrammar.Append(new Choices("fpm", "feet per minute", "feet"), 0, 1);
headingGrammar.Append("please", 0, 1);
Grammar = new Grammar(headingGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(headingGrammar.DebugShowPhrases);
}
public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<float>("sim/cockpit/autopilot/vertical_velocity");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var stringHeading = Constants.StringNumbersToDigits(phrase);
try
{
var startingIndexNumber = Constants.NumbersInDigits.First(stringHeading.Contains);
var startingIndex = stringHeading.IndexOf(startingIndexNumber, StringComparison.Ordinal);
stringHeading = stringHeading.Substring(startingIndex, stringHeading.Length - startingIndex);
}
catch { }

var splittedString = stringHeading.Split(' ');
float verticalSpeedToSet = 0;
for (var i = 0; i < splittedString.Length; i += 2)
{
var nextIndex = i + 1;
if (nextIndex > splittedString.Length - 1)
break;
if (splittedString[i + 1] == "thousand")
{
verticalSpeedToSet += int.Parse(splittedString[i]) * 1000;
}
if (splittedString[i + 1] == "hundred")
{
verticalSpeedToSet += int.Parse(splittedString[i]) * 100;
}
}

if (phrase.Contains("fifty") && verticalSpeedToSet < 1000 && verticalSpeedToSet > -1000)
verticalSpeedToSet += 50;

if (phrase.Contains("negative"))
verticalSpeedToSet *= -1;
XPlaneInterface.SetDataRef("sim/cockpit/autopilot/vertical_velocity", verticalSpeedToSet);


}

}
}
14 changes: 12 additions & 2 deletions X-Plane Voice Control/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ namespace X_Plane_Voice_Control
{
internal static class Constants
{
private static readonly string[] ClassicNumbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

private static readonly string[] Numbers = { "zero", "one", "two", "three", "four", "fiver", "six", "seven", "eight", "niner" };

private static readonly string[] TransponderNumbers = { "zero", "one", "two", "three", "four", "fiver", "six", "seven"};
private static readonly string[] TransponderNumbers = { "zero", "one", "two", "three", "four", "fiver", "six", "seven" };

private static readonly string[] VerticalSpeedNumbers = { "zero", "one", "two", "three", "four", "five", "six" };

public static readonly string[] NumbersInDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

public static readonly Choices NumberChoices = new Choices(Numbers);

public static readonly Choices ClassicNumberChoices = new Choices(ClassicNumbers);

public static readonly Choices TransponderNumberChoices = new Choices(TransponderNumbers);

public static readonly Choices VerticalSpeedNumberChoices = new Choices(VerticalSpeedNumbers);

public const int ButtonReleaseDelay = 700;

public const int PushButtonReleaseDelay = 300;
Expand All @@ -20,7 +30,7 @@ public static string StringNumbersToDigits(string input)
{
return input.Replace("zero", "0").Replace("one", "1").Replace("two", "2").Replace("three", "3")
.Replace("four", "4").Replace("fiver", "5").Replace("six", "6").Replace("seven", "7")
.Replace("eight", "8").Replace("niner", "9").Replace("decimal", "").Replace("point", "").Trim();
.Replace("eight", "8").Replace("niner", "9").Replace("decimal", "").Replace("point", "").Replace("five", "5").Replace("nine", "9").Trim();
}

public static bool IsValidComFreq(int input)
Expand Down
20 changes: 17 additions & 3 deletions X-Plane Voice Control/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion X-Plane Voice Control/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -36,7 +37,8 @@ private void ButtonListen_Click(object sender, EventArgs e)
try
{
_extPlaneInterface.Connect();
MessageBox.Show("Successfully connected! You can now speak after clicking OK", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
labelConnectionStatus.Text = "Connected";
labelConnectionStatus.ForeColor = Color.Green;
}
catch (Exception)
{
Expand Down
4 changes: 2 additions & 2 deletions X-Plane Voice Control/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.7.0.0")]
[assembly: AssemblyFileVersion("0.7.0.0")]
[assembly: AssemblyVersion("0.8.0.0")]
[assembly: AssemblyFileVersion("0.8.0.0")]
2 changes: 2 additions & 0 deletions X-Plane Voice Control/X-Plane Voice Control.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
<ItemGroup>
<Compile Include="Commands\AutoPilotControl.cs" />
<Compile Include="Commands\FlightDirectorControl.cs" />
<Compile Include="Commands\VerticalSpeedMCPControl.cs" />
<Compile Include="Commands\HeadingSelControl.cs" />
<Compile Include="Commands\AutoThrottleControl.cs" />
<Compile Include="Commands\HeadingMCPControl.cs" />
<Compile Include="Commands\VnavControl.cs" />
<Compile Include="Commands\LnavControl.cs" />
<Compile Include="Commands\TransponderModeControl.cs" />
Expand Down

0 comments on commit 39e23d6

Please sign in to comment.