private void Navbar_OnConnectButtonClicked(object sender, RoutedEventArgs e)
        {
            if (_robotConnectionService == null)
            {
                var portName = _selectedPortName;

                if (string.IsNullOrEmpty(portName))
                {
                    Logger.Error("No ports found");
                    MessageBox.Show("Please select serial port first.", "Port not found");
                    return;
                }

                Logger.Info("Starting connection...");
                _mainViewModel.GuiStatusViewModel.ConnectionStatus = $"Connected to: {portName}";
                _mainViewModel.GuiStatusViewModel.IsConnected      = true;
                _serialPort        = _serialPortFactory.GetPort(portName);
                _serialPortManager = new SerialPortManager(_serialPort);
                _serialPortManager.TryOpen();
                _serialPortAdapter      = new SerialPortAdapter(_serialPort);
                _robotConnectionService = new RobotConnectionService(_serialPortAdapter);
                _robotConnectionService.SpeedCurrentFeedbackReceived       += RobotConnection_CurrentSpeedFeedbackReceived;
                _robotConnectionService.VoltageTemperatureFeedbackReceived += RobotConnection_VoltageTemperatureFeedbackReceived;
                _robotConnectionService.ParametersReceived += RobotConnection_ParametersReceived;

                _sender = new ControlsSender(_robotConnectionService, 50);

                _experimentHandler.Sender = _sender;

                RequestParameters();
            }
        }
 public ControlsSender(RobotConnectionService robotConnectionService, int interval) : base(robotConnectionService)
 {
     _controls       = new ControlsModel(0, 0);
     _timer          = new Timer(interval);
     _timer.Elapsed += TimerOnElapsed;
     _timer.Start();
 }
        private void Navbar_OnDisconnectButtonClicked(object sender, RoutedEventArgs e)
        {
            _mainViewModel.GuiStatusViewModel.ConnectionStatus = "Disconnected";
            _mainViewModel.GuiStatusViewModel.IsConnected      = false;

            if (_robotConnectionService != null)
            {
                Logger.Info("Stopping connection...");
                _robotConnectionService.Dispose();
                _robotConnectionService = null;
            }

            if (_serialPort != null)
            {
                _serialPortManager.Close();
                _serialPort.Dispose();
                _serialPort = null;
            }
        }
        private void WindowClose(object sender, CancelEventArgs e)
        {
            _gamepadService.Stop();

            if (_robotConnectionService != null)
            {
                Logger.Info("Stopping connection...");
                _robotConnectionService.Dispose();
                _robotConnectionService = null;
            }

            if (_serialPort != null)
            {
                if (_serialPort.IsOpen)
                {
                    _serialPort.DiscardInBuffer();
                }
                _serialPortManager.Close();
                _serialPort.Dispose();
                _serialPort = null;
            }
        }
        public void SubscribeAndStart(RobotConnectionService robot, IGamepadService gamepad, string parametersHeader)
        {
            _robot   = robot;
            _gamepad = gamepad;

            _writer = new FileWriter(_config);
            _writer.WriteLine(parametersHeader);
            _writer.WriteLine(_formatter.GetHeader());
            _log      = new DatalogModel();
            Setpoints = new ControlsModel(0, 0);

            try
            {
                // _gamepad.RobotControlChanged += GamepadSerrvice_RobotControlChanged;
                _robot.SpeedCurrentFeedbackReceived       += RobotConnection_SpeedCurrentReceived;
                _robot.VoltageTemperatureFeedbackReceived += RobotConnection_VoltageTemperatureReceived;
                _logger.Info($"Started on path {_config.Path}");
                LoggingStarted?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception e)
            {
                _logger.Error($"Cannot subscribe: {e.Message}. Please connect first.");
            }
        }
Esempio n. 6
0
 public Sender(RobotConnectionService robotConnectionService)
 {
     _robotConnectionService = robotConnectionService;
 }