CaptureAndMove.cs

Capture a position from input, then move some offset.

/*  
 Copyright(c) 1998-2009 by Robotic Systems Integration, Inc. All rights reserved.
 This software contains proprietary and confidential information of Robotic 
 Systems Integration, Inc. (RSI) and its suppliers. Except as may be set forth 
 in the license agreement under which this software is supplied, disclosure, 
 reproduction, or use with controls other than those provided by RSI or suppliers
 for RSI is strictly prohibited without the prior express written consent of 
 Robotic Systems Integration.

 Warning!  This is a sample program to assist in the integration of your motion 
 controller with your application.  It may not contain all of the logic and safety
 features that your application requires.

 For any questions regarding this sample code please visit www.roboticsys.com.
 ==================================================================================
*/

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using RSI.RapidCode.SynqNet.dotNET;
using RSI.RapidCode.SynqNet.dotNET.Enums;

namespace SampleApplications
{
    [TestFixture]
    public class CaptureAndMove
    {

        const double VELOCITY = 1000.0;
        const double ACCEL = 10000.0;
        const double JERK_PCT = 50.0;

        const double INITIAL_TARGET = 5000;   // start the original move to this position
        
        const double TARGET_OFFSET = 400;  // how far to move from the edge of the input sensor

        const int A_FEW_MILLISECONDS = 7;   // how long we'll sleep while wating in our loop

        MotionController controller;
        Axis axis;



        private void SetupDefaults()
        {
            axis.DefaultVelocitySet(VELOCITY);
            axis.DefaultAccelerationSet(ACCEL);
            axis.DefaultDecelerationSet(ACCEL);
            axis.DefaultJerkPercentSet(JERK_PCT);
        }

        [Test]
        public void Main()
        {
            try
            {
                controller = MotionController.Create();
                axis = controller.AxisGet(0);
                SetupDefaults();

                axis.Abort();
                axis.PositionSet(0);  // only do this so we can run the sample over and over
                axis.ClearFaults();
                axis.AmpEnableSet(true);

                axis.CaptureConfigSet(RSICaptureType.RSICaptureTypeTIME,
                                        RSICaptureSource.RSICaptureSourceINDEX,
                                        RSICaptureEdge.RSICaptureEdgeEITHER,
                                        axis.NumberGet(),
                                        RSIMotorEncoder.RSIMotorEncoderPRIMARY, false);
                axis.CaptureArm(true);

                axis.MoveSCurve(INITIAL_TARGET);

                while (axis.CaptureStateGet() != RSICaptureState.RSICaptureStateCAPTURED)
                {
                    controller.OS.Sleep(A_FEW_MILLISECONDS);
                }

                double capturedPosition = (axis.CapturePositionGet() - axis.OriginPositionGet());
                Console.WriteLine("Captured Position: " + capturedPosition);

                double targetPosition = capturedPosition + TARGET_OFFSET;

                Console.WriteLine("Adjusting move to: " + targetPosition);
                axis.MoveSCurve(targetPosition);
            }
            catch (RsiError e)
            {
                Console.WriteLine(e.Message);
            }

        }
    }
}