예제 #1
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6));
            var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24);

            await WriteLog("Moving forward");

            while (!_finish)
            {
                driver.MoveForward();

                await Task.Delay(200);

                var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000);

                if (distance > 35.0)
                {
                    continue;
                }

                await WriteLog($"Obstacle found at {distance} cm or less. Turning right");

                await driver.TurnRightAsync();

                await WriteLog("Moving forward");
            }
        }
예제 #2
0
        private async void MainPage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            _dispatcher   = CoreWindow.GetForCurrentThread().Dispatcher;
            _blinkyLed    = new BlinkyDriver(blinkyPin);
            _speechDriver = new SpeechDriver();
            await _speechDriver.InitializeAsync();

            _speechDriver.Activated += SpeechDriver_Activated;
            _speechDriver.SpeechCommandTriggered += SpeechCommandTriggered;

            _driver = new TwoMotorsDriver(
                new Motor(leftMotorPin1, leftMotorPin2),
                new Motor(rightMotorPin1, rightMotorPin2));
            await _speechDriver.StartAsync();

            _worker         = new BackgroundWorker();
            _worker.DoWork += DoWork;
            _worker.RunWorkerAsync();
        }
예제 #3
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6));
            var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24);

            await WriteLog("Moving forward");

            while (!_finish)
            {
                driver.MoveForward();

                await Task.Delay(200);

                var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000);
                if (distance > 35.0)
                    continue;

                await WriteLog($"Obstacle found at {distance} cm or less. Turning right");

                await driver.TurnRightAsync();

                await WriteLog("Moving forward");
            }
        }
예제 #4
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            var driver = new TwoMotorsDriver(new Motor(27, 22), new Motor(5, 6));
            var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(23, 24);
            await ultrasonicDistanceSensor.InitAsync();

            WriteLog("Moving forward");

            while (!_finish)
            {
                try
                {
                    driver.MoveForward();

                    await Task.Delay(200);

                    var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000);
                    WriteData("Forward", distance);
                    if (distance > 35.0)
                        continue;

                    WriteLog($"Obstacle found at {distance:F2} cm or less. Turning right");
                    WriteData("Turn Right", distance);

                    await driver.TurnRightAsync();

                    WriteLog("Moving forward");
                }
                catch (Exception ex)
                {
                    WriteLog(ex.Message);
                    driver.Stop();
                    WriteData("Stop", -1);
                }
            }
        }
예제 #5
0
        public static void Main(string[] args)
        {
            Configuration rover = Configuration.Read();

            if (args.Length <= 0)
            {
                return;
            }
            try {
                switch (args[0])
                {
                case "led":
                    if (args.Length > 2)
                    {
                        int  numled = 999;
                        bool result = int.TryParse(args[1], out numled);
                        if (result)
                        {
                            if (rover.led.Length > numled)
                            {
                                Led led = new Led(numled, rover.led[numled]);
                                if ((args[2]) == "on")
                                {
                                    led.on();
                                }
                                else
                                {
                                    led.off();
                                }
                                Console.Write("OK");
                            }
                        }
                    }
                    break;

                case "button":
                    if (args.Length > 1)
                    {
                        int  numbtn = 999;
                        bool result = int.TryParse(args[1], out numbtn);
                        if (result)
                        {
                            if (rover.button.Length > numbtn)
                            {
                                Button btn =
                                    new Button(rover.button[numbtn]);
                                Console.Write(btn.read()); btn.dispose();
                            }
                        }
                    }
                    break;

                case "motor":
                    TwoMotorsDriver motors = new TwoMotorsDriver(rover.motor);
                    if (args.Length > 1)
                    {
                        switch (args[1])
                        {
                        case "left": motors.TurnLeft(); break;

                        case "right": motors.TurnRight(); break;

                        case "forward": motors.MoveForward(); break;

                        case "backward": motors.MoveBackward(); break;

                        case "stop": motors.Stop(); break;

                        default: { } break;
                        }
                        System.Threading.Thread.Sleep(
                            TimeSpan.FromMilliseconds(1000 * rover.timestep));
                        motors.Stop(); Console.Write("OK"); motors.dispose();
                    }
                    break;

                case "uds":
                    UltrasonicDistanceSensor uds = new UltrasonicDistanceSensor(rover.uds);
                    uds.getDistance(); Console.Write(uds.getDistance()); uds.Close();
                    break;
                }
            }
            catch (Exception) {
                Console.Write("exception: the application only works on raspian with 'sudo' command");
            }
        }
예제 #6
0
        private async void MainPage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
            _blinkyLed = new BlinkyDriver(blinkyPin);
            _speechDriver = new SpeechDriver();
            await _speechDriver.InitializeAsync();
            _speechDriver.Activated += SpeechDriver_Activated;
            _speechDriver.SpeechCommandTriggered += SpeechCommandTriggered;

            _driver = new TwoMotorsDriver(
                    new Motor(leftMotorPin1, leftMotorPin2),
                    new Motor(rightMotorPin1, rightMotorPin2));
            await _speechDriver.StartAsync();

            _worker = new BackgroundWorker();
            _worker.DoWork += DoWork;
            _worker.RunWorkerAsync();
        }