Example #1
0
        private async Task goToPositionAsync(int position)
        {
            if (isAtPosition(position))
            {
                // Cancel a move in case the motor is moving.
                System.Diagnostics.Debug.WriteLine($"Motor is at desired position within error (target: {position}, actual: {mover.Locator.Position}).");
                mover.CancelMove();
                return;
            }

            var drivePosition = calcDrivePosition(position);
            await mover.GoToPositionAsync(drivePosition);
        }
Example #2
0
        private void topInterruptDetected(object sender, GpioValueChangedEventArgs e)
        {
            var pos = locator.Position;

            switch (State)
            {
            case CalibrationState.PreparingToCalibrate:
                // Check if can enter the Calibrating state
                if (e.Edge == GpioEdge.FallingEdge)
                {
                    if (motorInfo.Direction == MoveDirection.Backward)
                    {
                        State       = CalibrationState.Calibrating;
                        upperTopPos = pos;
                    }
                }
                else
                {
                    if (motorInfo.Direction == MoveDirection.Forward)
                    {
                        mover.CancelMove();
                    }
                }
                break;

            case CalibrationState.Calibrating:
                // Update nullable positions
                if (e.Edge == GpioEdge.FallingEdge)
                {
                    if (motorInfo.Direction == MoveDirection.Backward)
                    {
                        upperTopPos = pos;
                    }
                    else
                    {
                        lowerTopPos = pos;
                    }
                }
                else
                {
                    if (motorInfo.Direction == MoveDirection.Forward)
                    {
                        upperTopPos = pos;
                        mover.CancelMove();
                    }
                    else
                    {
                        lowerTopPos = pos;
                    }
                }
                break;

            case CalibrationState.Ready:
                // Check we're still calibrated
                var offset    = Math.Abs(pos - top.StepPosition);
                var tolerance = (upperTopPos.Value - lowerTopPos.Value) / 2 + 0.25 * StepsPerGridUnit;
                if (offset > tolerance)
                {
                    System.Diagnostics.Debug.WriteLine($"Need to calibrate {motorInfo.Axis} motor.");
                    State = CalibrationState.NeedsCalibrating;
                }
                break;
            }
        }