Пример #1
0
        private void StopWorker()
        {
            try
            {
                if (isWorkerRunning && plucky != null && !plucky.isCommError)
                {
                    //plucky.Dispose();
                    Task.Factory.StartNew(plucky.Dispose).Wait();
                    //await Task.Factory.StartNew(plucky.Dispose);

                    //await Task.Delay(2000); // let PWM commands reach motors
                    Task.Delay(4000).Wait(); // let PWM commands reach motors
                }

                IsDeviceOpen = false;
                StopTickers().Wait();   // triggers tokenSource.Cancel()
            }
            catch (AggregateException exc)
            {
                Debug.WriteLine("Error: StopWorker(): AggregateException - " + exc);
            }
            catch (CommunicationException exc)
            {
                Debug.WriteLine("Error: StopWorker(): CommunicationException - " + exc);
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: StopWorker(): " + exc);
            }

            if (plucky != null)
            {
                plucky.Dispose();
                plucky = null;
            }

            if (httpServer != null)
                httpServer.robot = plucky;

            isWorkerRunning = false;
        }
Пример #2
0
        private async Task<bool> doOpenDevice(string deviceId)
        {
            try
            {
                // start the worker process:
                await StartWorker();

                if (!isWorkerRunning || plucky == null || plucky.isCommError)
                {
                    Speak("Hardware brick does not connect");
                    if (plucky != null)
                    {
                        plucky.Close();
                        plucky = null;
                    }
                }
                else
                {
                    // plucky is connected to hardware brick.
                }

                if (httpServer != null)
                    httpServer.robot = plucky;

                return true;
            }
            catch (Exception exc)
            {
                Speak("could not connect");
            }
            return false;
        }
Пример #3
0
        private async Task StartWorker()
        {
            try
            {
                IsDeviceOpen = false;

                tokenSource = new CancellationTokenSource();
                isWorkerRunning = true;

                plucky = new PluckyTheRobot(this, joystick, desiredLoopTimeMs);
                await plucky.Init(tokenSource, new string[] { currentSerialPort });  // may throw exceptions

                if (plucky.isCommError)
                {
                    isWorkerRunning = false;
                }
                else
                {
                    await InitializeTickers();
                    LastConnectionTime = DateTime.Now;
                    IsDeviceOpen = true;
                }
            }
            catch (AggregateException exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): AggregateException - " + exc);
                throw;
            }
            catch (CommunicationException exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): CommunicationException - " + exc);
                throw;
            }
            catch (Exception exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): " + exc);
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// click on the button that selects the serial port and then starts backround worker, thus starting the robot.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void OpenCloseButton_Click(object sender, RoutedEventArgs e)
        {
            try {
                OpenCloseButton.IsEnabled = false;

                if (isWorkerRunning)
                {
                    Speak("disconnecting");
                    await Task.Delay(100);

                    // cancel the worker process, dispose of plucky - but don't wait for it:
                    Task task = Task.Factory.StartNew(StopWorker);
                    await Task.Delay(1000);

                    ResetOpenCloseButton("");
                }
                else
                {
                    currentSerialPort = "" + SerialPortComboBox.SelectedValue;

                    if (!string.IsNullOrWhiteSpace(currentSerialPort))
                    {
                        Speak("connecting");
                        await Task.Delay(1000);

                        // start the worker process:
                        await StartWorker();

                        if (!isWorkerRunning || plucky.isCommError)
                        {
                            StatusLabel.Text = plucky.ToString() + " cannot connect to hardware brick";
                            ResetOpenCloseButton("");
                            Speak("Hardware brick does not connect");
                            if (plucky != null)
                            {
                                plucky.Close();
                                plucky = null;
                            }
                        }
                        else
                        {
                            // plucky is connected to hardware brick.
                            SerialPortComboBox.IsEnabled = false;
                            OpenCloseButton.Content = "Close";
                            EnableOpenCloseButton("");
                        }

                        if (httpServer != null)
                            httpServer.robot = plucky;
                    }
                    else
                    {
                        Speak("Select serial port");
                        StatusLabel.Text = "Please select serial port to connect to " + plucky + " hardware brick";
                        ResetOpenCloseButton("");
                    }
                }
            }
            catch (Exception exc)
            {
                Speak("Oops");
                StatusLabel.Text = exc.Message;
                Debug.WriteLine("Exception: " + exc);
                ResetOpenCloseButton("");
                plucky = null;
                if (httpServer != null)
                    httpServer.robot = plucky;
            }
        }