protected async Task <PlaybackResult> PlayWavStreamAsync(IVectorControllerPlus controller, Stream stream, uint volume = 50) { var reader = new BinaryReader(stream); var header = reader.ReadBytes(44); return(await PlaySoundAsync(controller, header, stream, volume)); }
private async Task <PlaybackResult> PlaySoundAsync(IVectorControllerPlus controller, byte[] header, Stream stream, uint volume) { // WAV format guide: http://www.topherlee.com/software/pcm-tut-wavformat.html var channels = BitConverter.ToUInt16(header, 22); if (channels != 1) { throw new ArgumentException("Vector requires a 1 channel WAV."); } var bitdepth = BitConverter.ToUInt16(header, 34); if (bitdepth != 16) { throw new ArgumentException("Vector requires a WAV with 16-bit samples."); } var sampleRate = BitConverter.ToUInt32(header, 24); uint frameRate = sampleRate; // not right I think if (frameRate < 8000 || frameRate > 16025) { throw new ArgumentException("Frame rate must bet between 8000 and 16025 hz."); } return(await controller.Robot.Audio.PlayStream(stream, frameRate, volume)); }
protected override async Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { if (ReadyToInteract(controller.Robot)) { await controller.Robot.Behavior.SetLiftHeight(1.0f); } bool spoke = false; if (await WaitUntilReadyToSpeakAsync(controller.Robot, TimeSpan.FromSeconds(5))) { if (name != null) { await controller.Robot.Behavior.SayText("Would you like a cup of tea, " + name + "?", true); } else { await controller.Robot.Behavior.SayText("Would you like a cup of tea?", true); } spoke = true; } if (ReadyToInteract(controller.Robot)) { await controller.Robot.Behavior.SetLiftHeight(0.0f); } return(spoke); }
public async Task <ActionState> ExecuteAsync(IVectorControllerPlus controller) { Started = DateTime.Now; if (IsTimedOut) { State = ActionState.Timeout; } else if (Killed) { State = ActionState.Dead; } else { try { Attempted = true; State = ActionState.Running; bool ok = await ExecuteImplementationAsync(controller); Duration = DateTime.Now - Started; State = ok ? ActionState.Completed : ActionState.Failed; } catch (Exception e) { Exception = e; // TODO: log the exception? State = ActionState.Failed; } } return(State); }
protected override async Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { // turn to face someone - allow 5 seconds to be ready to roll if (await WaitUntilReadyToInteractAsync(controller.Robot, TimeSpan.FromSeconds(5))) { var status = await controller.Robot.Behavior.FindFaces(); var faces = controller.Robot.World.Objects.Where(o => o is Face).Select(o => o as Face); var recentSpan = TimeSpan.FromSeconds(20); var recentFaces = faces.Where(f => DateTime.Now - f.LastObservedTime < recentSpan); if (recentFaces.Count() > 0) { int index = random.Next(0, faces.Count()); var face = faces.ElementAt(index); var turned = await controller.Robot.Behavior.TurnTowardsFace(face, 2); return (turned.Result == Anki.Vector.Types.ActionResultCode.ActionResultSuccess); } else { return(false); // no faces } } else { return(false); // not ready to interact } }
protected async Task <PlaybackResult> PlayEmbeddedSoundAsync(IVectorControllerPlus controller, string resourceId, uint volume = 50) { //var thisAssembly = Assembly.GetExecutingAssembly(); var thisAssembly = GetType().GetTypeInfo().Assembly; var stream = thisAssembly.GetManifestResourceStream(resourceId); return(await PlayWavStreamAsync(controller, stream, volume)); }
protected async Task <PlaybackResult> PlayWavByteArrayAsync(IVectorControllerPlus controller, byte[] wav, uint volume = 50) { // Audio starts at offset 44... var header = new byte[44]; var audio = new byte[wav.Length - 44]; wav.CopyTo(header, 0); wav.CopyTo(audio, 44); var stream = new MemoryStream(audio); return(await PlaySoundAsync(controller, header, stream, volume)); }
public async Task SetControllerAsync(IVectorControllerPlus controller) { if (this.controller != controller && this.controller != null) { this.controller.OnConnectionChanged -= Controller_OnConnectionChanged; } if (this.controller != controller && controller != null) { controller.OnConnectionChanged += Controller_OnConnectionChanged; } this.controller = controller; await HandleConnectionStateAsync(); }
protected override async Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { // initial search for the cube - allow up to 5 seconds to be ready to roll await WaitUntilReadyToInteractAsync(controller.Robot, TimeSpan.FromSeconds(5)); if (ReadyToInteract(controller.Robot)) { await controller.Robot.World.ConnectCube(); if (controller.Robot.World.LightCube.IsConnected) { // there's a cube in sight, so exterminate it await controller.Robot.Behavior.SetLiftHeight(0.5f); await controller.Robot.Behavior.GoToCube(100.0f, 2); await controller.Robot.Behavior.SayText("Extermin8! Extermin8!", true); await controller.Robot.Behavior.GoToCube(50.0f); await PlayEmbeddedSoundAsync(controller, "VectorPlus.Demo.Behaviour.Audio.gun16.wav"); await controller.Robot.Behavior.SetLiftHeight(0.0f); } else { // turn towards any face var status = await controller.Robot.Behavior.FindFaces(); var faces = controller.Robot.World.Objects.Where(o => o is Face).Select(o => o as Face); var recentSpan = TimeSpan.FromSeconds(20); var recentFaces = faces.Where(f => DateTime.Now - f.LastObservedTime < recentSpan); if (recentFaces.Count() > 0) { int index = random.Next(0, faces.Count()); var face = faces.ElementAt(index); var turned = await controller.Robot.Behavior.TurnTowardsFace(face, 2); } // catch phrase instead of extermination await controller.Robot.Behavior.SayText("You would make a good dalek.", true); } } else { // unable to turn towards a face, just say the line await controller.Robot.Behavior.SayText("You would make a good dalek.", true); } return(true); }
protected override async Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { await controller.Robot.Behavior.SetLiftHeight(92f); // this is in BehaviorComponent.MaxLiftHeight, but it's private await controller.Robot.Behavior.SayText("Happy birthday to you!", true); await controller.Robot.Behavior.SayText("Happy birthday to you!", true); await controller.Robot.Behavior.SayText("Happy birthday dear Sheila.", true); await controller.Robot.Behavior.SayText("Happy birthday to you!", true); // await controller.Robot.Behavior.SetLiftHeight(32f); // this is in BehaviorComponent.MinLiftHeight, but it's private return(true); }
protected override async Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { if (face == null) { return(false); } // allow 5 seconds to be ready to roll if (await WaitUntilReadyToInteractAsync(controller.Robot, TimeSpan.FromSeconds(5))) { var turned = await controller.Robot.Behavior.TurnTowardsFace(face, 2); return (turned.Result == Anki.Vector.Types.ActionResultCode.ActionResultSuccess); } else { return(false); // not ready to interact } }
protected async override Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { await controller.Robot.Motors.StopAllMotors(); return(true); }
protected async override Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller) { await controller.Robot.Behavior.SayText(speech, true); return(true); }
protected void StopPlayback(IVectorControllerPlus controller) { controller.Robot.Audio.CancelPlayback(); }
public static VectorBehaviourPlusReport FromException(Exception e, Robot robot = null, IVectorControllerPlus controller = null, IVectorBehaviourPlus behaviour = null) { return(new VectorBehaviourPlusReport() { Description = e.Message, Controller = controller, Robot = robot, Behaviour = behaviour }); }
protected abstract Task <bool> ExecuteImplementationAsync(IVectorControllerPlus controller);
protected async Task <PlaybackResult> PlayWavFileAsync(IVectorControllerPlus controller, string wavPath, uint volume = 50) { return(await PlayWavByteArrayAsync(controller, File.ReadAllBytes(wavPath), volume)); }