public MirroringMode(PiCarConnection picar) { Picar = picar; InitializeComponent(); //pressing enter key starts mirroring var submit = new RoutedCommand(); submit.InputGestures.Add(new KeyGesture(Key.Enter)); CommandBindings.Add(new CommandBinding(submit, StartMirroring_Click)); }
/** * Given two different car connections and a list of inputs, * start replaying to the first car and then replay to the second car after a delay. */ public static Tuple <Replay, Replay> StartTwoWithDelay(PiCarConnection first, PiCarConnection second, List <Direction> savedInputs, TimeSpan delay) { var firstReplay = new Replay(first, savedInputs, 0); var secondReplay = new Replay(second, savedInputs, 0); firstReplay.Start(); Thread.Sleep(delay); secondReplay.Start(); return(Tuple.Create(firstReplay, secondReplay)); }
/** * Function that tries to connect to a given IP address */ private async Task IPConnect(string selectedIP, string selectedName) { //Handle the dummy connection if (selectedIP == "DummyIP") { var dummyConnection = new DummyConnection(selectedName, selectedIP); _mainWindow.deviceListMain.Add(dummyConnection); _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Added " + selectedName + " for testing\n"); LogFieldReg.AppendText("Added " + selectedName + " for testing\n"); } else if (!CheckIfValidIP(selectedIP)) { LogFieldReg.AppendText("Invalid IP used, try again!\n"); _mainWindow.LogField.AppendText(DateTime.Now + ":\tInvalid IP used, try again!\n"); } else { PiCarConnection newConnection = null; var canConnect = false; try { newConnection = new PiCarConnection(selectedName, selectedIP); var connectResponse = newConnection.RequestConnect(); Console.Write(connectResponse.Item2); LogFieldReg.AppendText(connectResponse.Item2); _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + connectResponse.Item2); canConnect = connectResponse.Item1; } catch (RpcException rpcE) { _mainWindow.LogField.AppendText(DateTime.Now + ":\tError! " + rpcE + "\n"); } catch (Exception exception) { _mainWindow.LogField.AppendText(DateTime.Now + ":\tError! " + exception + "\n"); } if (canConnect) { _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Connected to " + selectedName + " with IP: " + selectedIP + "\n"); LogFieldReg.AppendText("Connected to " + selectedName + " with IP: " + selectedIP + "\n"); _mainWindow.deviceListMain.Add(newConnection); } else { _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Failed to connect to " + selectedName + " with IP: " + selectedIP + "\n"); LogFieldReg.AppendText("Failed to connect to " + selectedName + " with IP: " + selectedIP + "\n"); } } }
private void DisconnectCar(PiCarConnection picar) { if (picar.GetType() == typeof(DummyConnection)) { return; } LogField.AppendText(DateTime.Now + ":\t" + picar + " stopped responding, disconnecting.\n"); LogField.ScrollToEnd(); deviceListMain.Remove(picar); DeviceListMn.ItemsSource = null; DeviceListMn.ItemsSource = deviceListMain; }
private void SetVehicleMode(PiCarConnection picar, ModeRequest.Types.Mode mode) { try { picar.SetMode(mode); DeviceStatus.Text = picar.Mode.ToString(); LogField.AppendText(DateTime.Now + ":\tSetting " + picar + "to " + picar.Mode.ToString() + "\n"); LogField.ScrollToEnd(); } catch (Exception e) { DisconnectCar(picar); Console.WriteLine(e); } }
/** * Create a replay which will send the SavedInputs to the piCar after .Start() is called. */ public Replay(PiCarConnection piCarConnection, List <Direction> savedInputs, int initDelay) { this.InitialDelay = initDelay; this.piCar = piCarConnection; // Map Direction -> <Offset to next instruction, Direction> var priorTime = savedInputs[0].time; this.savedOffsetInputs = savedInputs.Select(x => { var t = Tuple.Create(x.time - priorTime, x); if (t.Item1.Duration() != t.Item1) { throw new ArgumentException("Replay inputs must be in chronological order"); } priorTime = x.time; return(t); }).ToList(); this.replayThread = new Thread(DoReplay); }
/** * Given two different car connections and a list of inputs, * start replaying to the first car and then replay to the second car after giving an initial forward acceleration. * This is done in order to catch up to the starting position of the first car. */ public static Tuple <Replay, Replay> StartTwoWithCatchup(PiCarConnection first, PiCarConnection second, List <Direction> savedInputs, double catchupDistance) { TimeSpan CatchupDuration = TimeSpan.FromSeconds(catchupDistance / PiCarConnection.SPEED_AT_MAX_THROTTLE); var secondInputs = new List <Direction>(savedInputs); Direction catchupInput = new Direction(secondInputs[0].time - CatchupDuration, 1.0, 0.0); secondInputs.Insert(0, catchupInput); secondInputs.Add(new Direction(secondInputs.Last().time, 0.0, 0.0)); // Ensure we stop at the end Direction avoidCrashInput = new Direction(savedInputs.Last().time, 1.0, 0.0); savedInputs.Add(avoidCrashInput); savedInputs.Add(new Direction(savedInputs.Last().time + CatchupDuration, 0.0, 0.0)); // Stop after avoiding crash var firstReplay = new Replay(first, savedInputs, 0); var secondReplay = new Replay(second, secondInputs, 0); firstReplay.Start(); secondReplay.Start(); return(Tuple.Create(firstReplay, secondReplay)); }