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

Commit

Permalink
Added APU and Engine control
Browse files Browse the repository at this point in the history
  • Loading branch information
David committed Jun 20, 2018
1 parent 91256e3 commit b7a094e
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ You need ExtPlane Plugin installed ([Download](https://github.com/vranki/ExtPlan
* __set [landing lights on / landing lights off]__ please
* Taxi lights control
* __set [taxi lights on / taxi lights off]__ please
* Engine control
* __[start / light up / kill / shut down] the engine number [one / two]]__ please
* __introduce fuel [into / to] number engine [one / two]__ please
* APU control
* __[start / light up / stop / shutdown] APU__ please
* __APU [on / off]__ please

__Cheat sheet:__
* __Needed__
Expand Down
55 changes: 55 additions & 0 deletions X-Plane Voice Control/Commands/ApuControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExtPlaneNet;

namespace X_Plane_Voice_Control.Commands
{
class ApuControl : ControlTemplate
{
private readonly string[] _apuOnStrings = { "start", "light up" };
private readonly string[] _apuOffString = { "stop", "shutdown" };
private readonly string[] _apuStatus = { "on", "off" };

public ApuControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer)
{
var apuGrammar = new GrammarBuilder();
apuGrammar.Append("please", 0, 1);
apuGrammar.Append(new Choices(_apuOnStrings.Concat(_apuOffString).ToArray()), 0, 1);
apuGrammar.Append("APU");
apuGrammar.Append(new Choices(_apuStatus), 0, 1);
apuGrammar.Append("please", 0, 1);
Grammar = new Grammar(apuGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(apuGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

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

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var apuStatus = XPlaneInterface.GetDataRef<double>("laminar/B738/switches/apu_start");
if (_apuOnStrings.Any(phrase.Contains) || phrase.Contains(_apuStatus[0]))
{
XPlaneInterface.SetDataRef(apuStatus.Name, 0);
SpeechSynthesizer.SpeakAsync("APU is starting up");
}
else if (_apuOffString.Any(phrase.Contains) || phrase.Contains(_apuStatus[1]))
{
XPlaneInterface.SetDataRef(apuStatus.Name, 2);
SpeechSynthesizer.SpeakAsync("APU is shutting down");
}
}
}
}
6 changes: 5 additions & 1 deletion X-Plane Voice Control/Commands/BrakeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public BrakeControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer)
brakeGrammar.Append("please", 0, 1);
Grammar = new Grammar(brakeGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(brakeGrammar.DebugShowPhrases);
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/parking_brake");
}

public sealed override Grammar Grammar { get; }

public override string RecognitionPattern { get; }

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

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var value = XPlaneInterface.GetDataRef<double>("laminar/B738/annunciator/parking_brake").Value;
Expand Down
8 changes: 6 additions & 2 deletions X-Plane Voice Control/Commands/ComFrequencyControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ public ComFrequencyControl(ExtPlaneInterface interface_, SpeechSynthesizer synth
frequencyGrammar.Append(Constants.NumberChoices, 1, 3);
Grammar = new Grammar(frequencyGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(frequencyGrammar.DebugShowPhrases);
XPlaneInterface.Subscribe<int>("sim/cockpit2/radios/actuators/com1_standby_frequency_hz");
XPlaneInterface.Subscribe<int>("sim/cockpit2/radios/actuators/com2_standby_frequency_hz");
}
public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<int>("sim/cockpit2/radios/actuators/com1_standby_frequency_hz");
XPlaneInterface.Subscribe<int>("sim/cockpit2/radios/actuators/com2_standby_frequency_hz");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var index = phrase.IndexOf("com", StringComparison.InvariantCulture);
Expand Down
5 changes: 5 additions & 0 deletions X-Plane Voice Control/Commands/ComSwapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public ComSwapControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesize
public override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{

}

public override void OnTrigger(RecognitionResult result, string phrase)
{
var index = phrase.IndexOf("com", StringComparison.InvariantCulture);
Expand Down
2 changes: 2 additions & 0 deletions X-Plane Voice Control/Commands/ControlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected ControlTemplate(ExtPlaneInterface interface_, SpeechSynthesizer synthe

public abstract string RecognitionPattern { get; }

public abstract void DataRefSubscribe();

public abstract void OnTrigger(RecognitionResult result, string phrase);
}
}
59 changes: 59 additions & 0 deletions X-Plane Voice Control/Commands/EngineFuelControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExtPlaneNet;

namespace X_Plane_Voice_Control.Commands
{
class EngineFuelControl : ControlTemplate
{
private readonly string[] _engineNumbersStrings = { "one", "two" };

public EngineFuelControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer)
{
var engineGrammar = new GrammarBuilder();
engineGrammar.Append("please", 0, 1);
engineGrammar.Append("introduce fuel");
engineGrammar.Append(new Choices("into", "to"));
engineGrammar.Append("number", 0, 1);
engineGrammar.Append("engine");
engineGrammar.Append(new Choices(_engineNumbersStrings));
engineGrammar.Append("please", 0, 1);
Grammar = new Grammar(engineGrammar);

RecognitionPattern = Constants.DeserializeRecognitionPattern(engineGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<double>("laminar/B738/engine/mixture_ratio2");
XPlaneInterface.Subscribe<double>("laminar/B738/engine/mixture_ratio1");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var valueOne = XPlaneInterface.GetDataRef<double>("laminar/B738/engine/mixture_ratio1");
var valueTwo = XPlaneInterface.GetDataRef<double>("laminar/B738/engine/mixture_ratio2");
if (phrase.Contains(_engineNumbersStrings[0]))
{
XPlaneInterface.SetDataRef(valueOne.Name, 1);
SpeechSynthesizer.SpeakAsync("Introducing fuel into engine number one");
}
else if (phrase.Contains(_engineNumbersStrings[1]))
{
XPlaneInterface.SetDataRef(valueTwo.Name, 1);
SpeechSynthesizer.SpeakAsync("Introducing fuel into engine number two");
}

}
}
}
78 changes: 78 additions & 0 deletions X-Plane Voice Control/Commands/EngineStartUp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExtPlaneNet;

namespace X_Plane_Voice_Control.Commands
{
class EngineStartUpControl : ControlTemplate
{
private readonly string[] _engineNumbersStrings = { "one", "two" };
private readonly string[] _engineStartStrings = { "start", "light up" };
private readonly string[] _engineShutdownStrings = { "kill", "shut down" };

public EngineStartUpControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer)
{
var engineGrammar = new GrammarBuilder();
engineGrammar.Append("please", 0, 1);
engineGrammar.Append(new Choices(_engineStartStrings.Concat(_engineShutdownStrings).ToArray()));
engineGrammar.Append("the", 0, 1);
engineGrammar.Append("engine");
engineGrammar.Append("number", 0, 1);
engineGrammar.Append(new Choices(_engineNumbersStrings));
engineGrammar.Append("please", 0, 1);
Grammar = new Grammar(engineGrammar);
RecognitionPattern = Constants.DeserializeRecognitionPattern(engineGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<double>("laminar/B738/engine/starter1_pos");
XPlaneInterface.Subscribe<double>("laminar/B738/engine/starter2_pos");
XPlaneInterface.Subscribe<double>("laminar/B738/engine/mixture_ratio2");
XPlaneInterface.Subscribe<double>("laminar/B738/engine/mixture_ratio1");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var valueOne = XPlaneInterface.GetDataRef<double>("laminar/B738/engine/starter1_pos");
var valueTwo = XPlaneInterface.GetDataRef<double>("laminar/B738/engine/starter2_pos");
if (phrase.Contains(_engineNumbersStrings[0]))
{
if (_engineStartStrings.Any(phrase.Contains))
{
XPlaneInterface.SetDataRef(valueOne.Name, 0);
SpeechSynthesizer.SpeakAsync("Starting engine number one");
}
else if (_engineShutdownStrings.Any(phrase.Contains))
{
XPlaneInterface.SetDataRef("laminar/B738/engine/mixture_ratio1", 0);
SpeechSynthesizer.SpeakAsync("Shutting down engine number one");
}
}
else if (phrase.Contains(_engineNumbersStrings[1]))
{
if (_engineStartStrings.Any(phrase.Contains))
{
XPlaneInterface.SetDataRef(valueTwo.Name, 0);
SpeechSynthesizer.SpeakAsync("Starting engine number two");
}
else if (_engineShutdownStrings.Any(phrase.Contains))
{
XPlaneInterface.SetDataRef("laminar/B738/engine/mixture_ratio2", 0);
SpeechSynthesizer.SpeakAsync("Shutting down engine number two");
}
}

}
}
}
6 changes: 5 additions & 1 deletion X-Plane Voice Control/Commands/FlapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public FlapControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer)
flapGrammar.Append(_aNotch, 0, 1);
flapGrammar.Append("please", 0, 1);
Grammar = new Grammar(flapGrammar);
XPlaneInterface.Subscribe<float>("sim/flightmodel/controls/flaprqst");
RecognitionPattern = Constants.DeserializeRecognitionPattern(flapGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<float>("sim/flightmodel/controls/flaprqst");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var value = XPlaneInterface.GetDataRef<float>("sim/flightmodel/controls/flaprqst").Value;
Expand Down
13 changes: 8 additions & 5 deletions X-Plane Voice Control/Commands/GearControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ public GearControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer)
gearGrammar.Append(new Choices("gear up", "gear down"));
gearGrammar.Append("please", 0, 1);
Grammar = new Grammar(gearGrammar);
XPlaneInterface.Subscribe<double>("laminar/B738/switches/landing_gear");
RecognitionPattern = Constants.DeserializeRecognitionPattern(gearGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<double>("laminar/B738/switches/landing_gear");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/nose_gear_transit");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/nose_gear_safe");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/left_gear_transit");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/left_gear_safe");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/right_gear_transit");
XPlaneInterface.Subscribe<double>("laminar/B738/annunciator/right_gear_safe");
RecognitionPattern = Constants.DeserializeRecognitionPattern(gearGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
double valueToSet = phrase.Contains("up") ? 0 : 2;
Expand Down
6 changes: 5 additions & 1 deletion X-Plane Voice Control/Commands/LandingLights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public LandingLights(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer
landingLightsGrammar.Append(new Choices(_landingLightsOnStrings.Concat(_landingLiggOffStrings).ToArray()));
landingLightsGrammar.Append("please", 0, 1);
Grammar = new Grammar(landingLightsGrammar);
XPlaneInterface.Subscribe<float[]>("sim/cockpit2/switches/landing_lights_switch");
RecognitionPattern = Constants.DeserializeRecognitionPattern(landingLightsGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

public override void DataRefSubscribe()
{
XPlaneInterface.Subscribe<float[]>("sim/cockpit2/switches/landing_lights_switch");
}

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var dataref = XPlaneInterface.GetDataRef<float[]>("sim/cockpit2/switches/landing_lights_switch");
Expand Down
6 changes: 5 additions & 1 deletion X-Plane Voice Control/Commands/TaxiLights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public TaxiLights(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) :
brakeGrammar.Append(new Choices(_taxiLightsOnStrings.Concat(_taxiLightsOffStrings).ToArray()));
brakeGrammar.Append("please", 0, 1);
Grammar = new Grammar(brakeGrammar);
XPlaneInterface.Subscribe<double>("laminar/B738/toggle_switch/taxi_light_brightness_pos");
RecognitionPattern = Constants.DeserializeRecognitionPattern(brakeGrammar.DebugShowPhrases);
}

public sealed override Grammar Grammar { get; }
public override string RecognitionPattern { get; }

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

public override void OnTrigger(RecognitionResult rResult, string phrase)
{
var value = XPlaneInterface.GetDataRef<double>("laminar/B738/toggle_switch/taxi_light_brightness_pos").Value;
Expand Down
11 changes: 8 additions & 3 deletions X-Plane Voice Control/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ private void ButtonListen_Click(object sender, EventArgs e)
continue;

var instance = (ControlTemplate)Activator.CreateInstance(type, _extPlaneInterface, _synthesizer);
try { instance.DataRefSubscribe(); }
catch
{
// ignored
}

Console.WriteLine(instance.RecognitionPattern);
_abstractCommands.Add(instance);
_speechRecognitionEngine.LoadGrammarAsync(instance.Grammar);
Expand All @@ -67,10 +73,9 @@ private void SpeechRecognize_SpeechRecognized(object sender, SpeechRecognizedEve
{
var class_ = _abstractCommands.First(x => x.Grammar == e.Result.Grammar);
class_.OnTrigger(e.Result, e.Result.Text);
SetLabelText(e.Result.Text);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Recognized text: {e.Result.Text}. Calling Class {class_.GetType().Name}");
Console.ForegroundColor = ConsoleColor.White;
}

public void SetLabelText(string text) => label1.Text = text;
}
}
3 changes: 3 additions & 0 deletions X-Plane Voice Control/X-Plane Voice Control.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\ComSwapControl.cs" />
<Compile Include="Commands\EngineFuelControl.cs" />
<Compile Include="Commands\FlapControl.cs" />
<Compile Include="Commands\LandingLights.cs" />
<Compile Include="Commands\EngineStartUp.cs" />
<Compile Include="Commands\ApuControl.cs" />
<Compile Include="Commands\TaxiLights.cs" />
<Compile Include="Commands\GearControl.cs" />
<Compile Include="Commands\ControlTemplate.cs" />
Expand Down

0 comments on commit b7a094e

Please sign in to comment.