public new void Initialize() { _scanningTool = _entity.GetBehavior <ResourceScanner>(); _toolSwitch = _entity.GetSwitch <ResourceScanner>(); base.Initialize(); }
private static async Task ConfigureRover(CSharpKernel csharpKernel) { using var _ = Log.OnEnterAndExit(); await LoadAssemblyAndAddNamespace <RoverRobot>(csharpKernel); await LoadAssemblyAndAddNamespace <ResourceScanner>(csharpKernel); await AddNamespace(csharpKernel, typeof(ImageProcessing.ImageExtensions)); var roverBody = new RoverRobot(PiTop4Board.Instance.GetOrCreateExpansionPlate(), PiTop4Board.Instance.GetOrCreateCamera <StreamingCamera>(0), RoverRobotConfiguration.Default); var roverBrain = new RoverRobotStrategies(); var resourceScanner = new ResourceScanner(); roverBody.BlinkAllLights(); await csharpKernel.SetVariableAsync(nameof(roverBody), roverBody); await csharpKernel.SetVariableAsync(nameof(roverBrain), roverBrain); await csharpKernel.SetVariableAsync(nameof(resourceScanner), resourceScanner); var source = new CancellationTokenSource(); var robotLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainLoop"); while (!source.IsCancellationRequested) { var localCancellationSource = new CancellationTokenSource(); if (!source.IsCancellationRequested && !localCancellationSource.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("Perceive"); try { roverBrain.Perceive?.Invoke(roverBody, DateTime.Now, localCancellationSource.Token); } catch (Exception e) { __.Error(e); } } if (!source.IsCancellationRequested && !localCancellationSource.IsCancellationRequested) { var planResult = PlanningResult.NoPlan; using var ___ = operation.OnEnterAndExit("Plan"); try { planResult = roverBrain.Plan?.Invoke(roverBody, DateTime.Now, localCancellationSource.Token) ?? PlanningResult.NoPlan; } catch (Exception e) { ___.Error(e); planResult = PlanningResult.NoPlan; } if (!source.IsCancellationRequested && planResult != PlanningResult.NoPlan && !localCancellationSource.IsCancellationRequested) { using var ____ = operation.OnEnterAndExit("Act"); roverBrain.Act?.Invoke(roverBody, DateTime.Now, localCancellationSource.Token); } } } roverBody.MotionComponent.Stop(); }, source.Token); var reactLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainReactLoop"); while (!source.IsCancellationRequested) { var localCancellationSource = new CancellationTokenSource(); if (!source.IsCancellationRequested && !localCancellationSource.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("React"); try { roverBrain.React?.Invoke(roverBody, DateTime.Now, localCancellationSource.Token); } catch (Exception e) { __.Error(e); } } } roverBody.MotionComponent.Stop(); }, source.Token); csharpKernel.RegisterForDisposal(() => { source.Cancel(false); Task.WaitAll(new[] { robotLoop, reactLoop }, TimeSpan.FromSeconds(10)); roverBody.Dispose(); }); }
private static async Task ConfigureRover(CSharpKernel csharpKernel) { Microsoft.DotNet.Interactive.Formatting.Formatter.ListExpansionLimit = 42; using var _ = Log.OnEnterAndExit(); await LoadAssemblyAndAddNamespace <RoverRobot>(csharpKernel); await LoadAssemblyAndAddNamespace <ResourceScanner>(csharpKernel); await AddNamespace(csharpKernel, typeof(ImageProcessing.ImageExtensions)); var RoverBody = new RoverRobot(PiTop4Board.Instance.GetOrCreateExpansionPlate(), PiTop4Board.Instance.GetOrCreateCamera <StreamingCamera>(0), RoverRobotConfiguration.Default); var RoverBrain = new RoverRobotAgent(); var ResourceScanner = new ResourceScanner(); RoverBody.BlinkAllLights(); await csharpKernel.SetVariableAsync(nameof(RoverBody), RoverBody); await csharpKernel.SetVariableAsync(nameof(RoverBrain), RoverBrain); await csharpKernel.SetVariableAsync(nameof(ResourceScanner), ResourceScanner); var command = new Command("#!reset", "Reset RoverBody, RoverBrain and BrainState") { new Option <bool>("--body", description: "Resets the rover body"), new Option <bool>("--brain", description: "Resets the rover brain"), new Option <bool>("--state", description: "Resets the rover brain state"), new Option <bool>("--all", description: "Resets the entire rover"), }; command.Handler = CommandHandler.Create <bool, bool, bool, bool, KernelInvocationContext>(async(body, brain, state, all, context) => { if (body || brain || state || all) { var code = new StringBuilder(); var resetTarget = new List <string>(); if (brain || all) { code.AppendLine($"{nameof(RoverBrain)}.Reset();"); resetTarget.Add("brain"); } if (state || all) { code.AppendLine($"{nameof(RoverBrain)}.ClearState();"); resetTarget.Add("state"); } if (body || all) { code.AppendLine($"{nameof(RoverBody)}.Reset();"); resetTarget.Add("body"); } var value = context.Display($"Reset for {string.Join(", ", resetTarget)} in progress", PlainTextFormatter.MimeType); await csharpKernel.SendAsync(new SubmitCode(code.ToString())); value.Update($"Reset for {string.Join(", ", resetTarget)} done!"); } }); csharpKernel.AddDirective(command); var source = new CancellationTokenSource(); var robotLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainLoop"); while (!source.IsCancellationRequested) { if (!source.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("Perceive"); try { RoverBrain.Perceive(); } catch (Exception e) { __.Error(e); } } if (!source.IsCancellationRequested) { var planResult = PlanningResult.NoPlan; using var ___ = operation.OnEnterAndExit("Plan"); try { planResult = RoverBrain.Plan(); } catch (Exception e) { ___.Error(e); planResult = PlanningResult.NoPlan; } if (!source.IsCancellationRequested && planResult != PlanningResult.NoPlan) { using var ____ = operation.OnEnterAndExit("Act"); RoverBrain.Act(); } } } RoverBody.MotionComponent.Stop(); }, source.Token); var reactLoop = Task.Run(() => { using var operation = Log.OnEnterAndExit("roverBrainReactLoop"); while (!source.IsCancellationRequested) { if (!source.IsCancellationRequested) { using var __ = operation.OnEnterAndExit("React"); try { RoverBrain.React(); } catch (Exception e) { __.Error(e); } } } RoverBody.MotionComponent.Stop(); }, source.Token); csharpKernel.RegisterForDisposal(() => { source.Cancel(false); Task.WaitAll(new[] { robotLoop, reactLoop }, TimeSpan.FromSeconds(10)); RoverBody.Dispose(); }); }
static void Init() { ResourceScanner window = (ResourceScanner)EditorWindow.GetWindow(typeof(ResourceScanner)); window.Show(); }