HexapiBackground.IK.IkController.BehaviorBounceAsync C# (CSharp) Method

BehaviorBounceAsync() private method

private BehaviorBounceAsync ( CancellationToken cancellationToken ) : Task
cancellationToken System.Threading.CancellationToken
return Task
        private async Task BehaviorBounceAsync(CancellationToken cancellationToken)
        {
            if (_behaviorRunning)
                return;

            _behaviorRunning = true;

            var randomNumber = new Random(DateTime.Now.Millisecond);

            await _display.WriteAsync("Bounce started", 2);

            RequestSetMovement(true);
            RequestSetGaitType(GaitType.Tripod8);

            double travelLengthZ = 40;
            double travelLengthX = 0;
            double travelRotationY = 0;
            double gaitSpeed = 50;

            while (!cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(100, cancellationToken);

                if (_leftInches > _perimeterInInches && _centerInches > _perimeterInInches && _rightInches > _perimeterInInches)
                {
                    await _display.WriteAsync("Forward", 2);

                    travelLengthZ = -50;
                    travelLengthX = 0;
                    travelRotationY = 0;
                }

                if (_leftInches <= _perimeterInInches && _rightInches > _perimeterInInches)
                {
                    await _display.WriteAsync("Left", 2);

                    travelLengthZ = _centerInches > _perimeterInInches ? -20 : 0;
                    travelLengthX = 0;
                    travelRotationY = -30;
                }

                if (_leftInches > _perimeterInInches && _rightInches <= _perimeterInInches)
                {
                    await _display.WriteAsync("Right", 2);

                    travelLengthZ = _centerInches > _perimeterInInches ? -20 : 0;
                    travelLengthX = 0;
                    travelRotationY = 30;
                }
                else if (_leftInches > _perimeterInInches && _rightInches <= _perimeterInInches)
                {
                    await _display.WriteAsync("Right", 2);

                    travelLengthZ = _centerInches > _perimeterInInches ? -20 : 0;
                    travelLengthX = 0;
                    travelRotationY = 30;
                }

                if (_leftInches <= _perimeterInInches && _rightInches <= _perimeterInInches)
                {
                    travelLengthX = 0;
                    travelRotationY = 0;

                    if (cancellationToken.IsCancellationRequested)
                    {
                        RequestMovement(50, 0, 0, 0);
                        _behaviorRunning = false;
                        return;
                    }

                    if (_centerInches < _perimeterInInches)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            RequestMovement(50, 0, 0, 0);
                            _behaviorRunning = false;
                            return;
                        }

                        await _display.WriteAsync("Reverse", 2);

                        travelLengthZ = 30; //Reverse
                        RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);

                        await Task.Delay(6000, cancellationToken);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            RequestMovement(50, 0, 0, 0);
                            _behaviorRunning = false;
                            return;
                        }

                        travelLengthZ = 0;

                        if (randomNumber.Next(0, 5) >= 3)
                        {
                            await _display.WriteAsync("Turn Right", 2);
                            travelRotationY = 30;

                            RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);

                            var targetYaw = _yaw + 5;

                            if (targetYaw > 359)
                                targetYaw = targetYaw - 359;
                            
                            while (_yaw < targetYaw)
                            {
                                
                            }
                        }
                        else
                        {
                            await _display.WriteAsync("Turn Left", 2);
                            travelRotationY = -30;

                            RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);

                            var targetYaw = _yaw - 5;

                            if (targetYaw < 0)
                                targetYaw = targetYaw + 359;

                            while (_yaw > targetYaw)
                            {

                            }
                        }

                        //RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);
                        //await Task.Delay(2000);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            RequestMovement(50, 0, 0, 0);
                            _behaviorRunning = false;
                            return;
                        }

                        await _display.WriteAsync("Stop", 2);

                        travelLengthX = 0;
                        travelRotationY = 0;

                        RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);

                        continue;
                    }
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    RequestMovement(50, 0, 0, 0);
                    _behaviorRunning = false;
                    return;
                }

                RequestMovement(gaitSpeed, travelLengthX, travelLengthZ, travelRotationY);
            }

            _behaviorRunning = false;
        }