Пример #1
0
        private async void DoWork3(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 (_worker2 != null && !_worker2.CancellationPending)
            {
                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 Task ProcessMessage(string message)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    return;
                }

                var json = JsonObject.Parse(message);

                var highestInent = GetHighestInent(json);

                if (string.IsNullOrWhiteSpace(highestInent))
                {
                    return;
                }

                switch (highestInent.ToLower())
                {
                case "stop":
                    _twoMotorsDriver.Stop();
                    _isMovingForward = false;
                    break;

                case "moveforward":
                    _twoMotorsDriver.MoveForward();
                    _isMovingForward = true;
                    break;

                case "movebackward":
                    _twoMotorsDriver.MoveBackward();
                    _isMovingForward = false;
                    break;

                case "turn":

                    var direction = GetHighestEntity(json);
                    if (!string.IsNullOrWhiteSpace(direction))
                    {
                        if (direction == "right")
                        {
                            await _twoMotorsDriver.TurnRightAsync();
                        }
                        else
                        {
                            await _twoMotorsDriver.TurnLeftAsync();
                        }
                    }
                    _isMovingForward = false;
                    break;
                }
            }
            catch (Exception ex)
            {
                await WriteLog(ex.Message);
            }
        }
Пример #3
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            while (_worker != null && !_worker.CancellationPending)
            {
                var result = await ServiceBusHelper.ReceiveAndDeleteMessage();

                switch (result)
                {
                case "SMR":
                    await _uln2003Driver.TurnAsync(90, TurnDirection.Right);

                    break;

                case "SML":
                    await _uln2003Driver.TurnAsync(90, TurnDirection.Left);

                    break;

                case "F":
                    _twoMotorsDriver.MoveForward();
                    isMovingForward = true;
                    break;

                case "S":
                    _twoMotorsDriver.Stop();
                    isMovingForward = false;
                    break;

                case "R":
                    await _twoMotorsDriver.TurnRightAsync();

                    isMovingForward = false;
                    break;

                case "L":
                    await _twoMotorsDriver.TurnLeftAsync();

                    isMovingForward = false;
                    break;

                case "B":
                    _twoMotorsDriver.MoveBackward();
                    isMovingForward = false;
                    break;

                case "TR":
                    await _twoMotorsDriver.TurnRightAsync(45);

                    isMovingForward = false;
                    break;

                case "TL":
                    await _twoMotorsDriver.TurnLeftAsync(45);

                    isMovingForward = false;
                    break;

                case "BR":
                    await _twoMotorsDriver.TurnRightAsync(100);

                    isMovingForward = false;
                    break;

                case "BL":
                    await _twoMotorsDriver.TurnLeftAsync(100);

                    isMovingForward = false;
                    break;
                }

                await Task.Delay(1000);
            }
        }
Пример #4
0
        private async void DoWork3(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 (_worker2 != null && !_worker2.CancellationPending)
            {
                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");
            }
        }