コード例 #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
ファイル: MainPage.xaml.cs プロジェクト: peejster/Rover
        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");
            }
        }
コード例 #3
0
ファイル: MainPage.xaml.cs プロジェクト: jmservera/Rover
        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);
                }
            }
        }
コード例 #4
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");
            }
        }
コード例 #5
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(
                                                            ultrasonicTrigPin,
                                                            ultrasonicEchoPin);
            await ultrasonicDistanceSensor.InitAsync();

            var previousState = _roverState.Clone();
            var state = _roverState.Clone();
            var currentTimer = _timer;
            while (!_finish)
            {
                try
                {
                    lock(_stateLock)
                    {
                        currentTimer = _timer;
                        previousState = state;
                        state = _roverState.Clone();
                    }

                    if (state.TurnState == RoverTurnState.Right)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnRightAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Left)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnLeftAsync();
                    }
                    else if (state.TurnState == RoverTurnState.TurnAround)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnAroundAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Play)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.PlayAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Attack)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.AttackAsync();
                    }
                    else
                    {
                        if (previousState.MoveState != state.MoveState)
                        {
                            switch (state.MoveState)
                            {
                                case RoverMoveState.Backward:
                                    ResetTimer(currentTimer, _roverState.NewTimer(), 2000);
                                    _driver.MoveBackward();
                                    break;

                                case RoverMoveState.Forward:
                                    ResetTimer(currentTimer, _roverState.NewTimer(), 2000);
                                    _driver.MoveForward();
                                    break;

                                case RoverMoveState.Stopped:
                                    ResetTimer(currentTimer);
                                    _driver.Stop();
                                    break;
                            }
                        }
                    }
                
                    await Task.Delay(20);

                    /*
                    var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000);
                    WriteData("Forward", distance);

                    if (distance <= 35.0)
                    {
                        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);
                }
            }
        }
コード例 #6
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            var ultrasonicDistanceSensor = new UltrasonicDistanceSensor(
                ultrasonicTrigPin,
                ultrasonicEchoPin);
            await ultrasonicDistanceSensor.InitAsync();

            var previousState = _roverState.Clone();
            var state         = _roverState.Clone();
            var currentTimer  = _timer;

            while (!_finish)
            {
                try
                {
                    lock (_stateLock)
                    {
                        currentTimer  = _timer;
                        previousState = state;
                        state         = _roverState.Clone();
                    }

                    if (state.TurnState == RoverTurnState.Right)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnRightAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Left)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnLeftAsync();
                    }
                    else if (state.TurnState == RoverTurnState.TurnAround)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.TurnAroundAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Play)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.PlayAsync();
                    }
                    else if (state.TurnState == RoverTurnState.Attack)
                    {
                        _roverState.TurnState = RoverTurnState.Straight;
                        await _driver.AttackAsync();
                    }
                    else
                    {
                        if (previousState.MoveState != state.MoveState)
                        {
                            switch (state.MoveState)
                            {
                            case RoverMoveState.Backward:
                                ResetTimer(currentTimer, _roverState.NewTimer(), 2000);
                                _driver.MoveBackward();
                                break;

                            case RoverMoveState.Forward:
                                ResetTimer(currentTimer, _roverState.NewTimer(), 2000);
                                _driver.MoveForward();
                                break;

                            case RoverMoveState.Stopped:
                                ResetTimer(currentTimer);
                                _driver.Stop();
                                break;
                            }
                        }
                    }

                    await Task.Delay(20);

                    /*
                     * var distance = await ultrasonicDistanceSensor.GetDistanceInCmAsync(1000);
                     * WriteData("Forward", distance);
                     *
                     * if (distance <= 35.0)
                     * {
                     *  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);
                }
            }
        }