Capture.cpp

Perform a velocity move and capture position based on encoder Index pulse.

/* 
Capture.cpp

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.

 This sample code presumes that the user has set the tuning paramters(PID, PIV, etc.) 
 prior to running this program so that the motor can rotate in a stable manner.


 In this sample code, we firstly initialize the controller and axis. Then we disable the capture arm 
 before calling CaptureConfigSet in which we set the paramters for capture. 
 
 The imputs are discussed below:
 CaptureConfigSet(CAPTURE_TYPE, CAPTURE_SOURCE, CAPTURE_TRIGGER_EDGE,
             CAPTURE_FEEDBACK, CAPTURE_ENCODER, CAPTURE_GLOBAL_TRIGGER)
 
 CAPTURE_TYPE: User can decide whether he wants to capture based on Position or Time.
 CAPTURE_SOURCE: Which output trigger is desired to capture position. capture1.cpp uses INDEX.
 CAPTURE_TRIGGER_EDGE: Choose from three option: Rising, Falling or Either.
 CAPTURE_FEEDBACK: Axis number being used.
 CAPTURE_ENCODER: User can choose from motor Primary or some other Secondary encoder.
 CAPTURE_GLOBAL_TRIGGER will always be set to 0 in Rapid Code.
 To understand the above inputs graphically please use our software 'Rapid Setup' and visit Homing Tab.

Once CaptureConfigSet has been called we can set function CaptureArm() back to true. We then perform a 
Velocity Move until the encoder's index pulse occurs.

Finally when the capture occurs, the position is displayed.

For any questions regarding this sample code please visit our documentation at www.roboticsys.com


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.
*/

#include "rsi.h"

using namespace RSI::RapidCode::SynqNet;

// user defined options
#define AXIS_NUMBER             0

#define CAPTURE_TYPE            RSICaptureTypePOSITION
#define CAPTURE_SOURCE            RSICaptureSourceINDEX
#define CAPTURE_TRIGGER_EDGE        RSICaptureEdgeRISING
#define CAPTURE_FEEDBACK_AXIS_NUMBER    AXIS_NUMBER
#define CAPTURE_ENCODER           RSIMotorEncoderPRIMARY
#define CAPTURE_GLOBAL_TRIGGER        false

#define VELOCITY              2000
#define ACCEL               5000


void captureMain()
{
  try
  {
    MotionController    *controller ;
    Axis        *axis ;

    double        capturedPosition;

    // initialize MotionController class
    controller = MotionController::CreateFromBoard(0);  
    
    // initialize Axis class  
    axis = controller->AxisGet( AXIS_NUMBER);

    // enable interrupts
    axis->InterruptEnableSet(true);

    // make sure Capture is not armed
    axis->CaptureArm(false);

    // setting the Capture Configurations, parameters discussed above
    axis->CaptureConfigSet(CAPTURE_TYPE, CAPTURE_SOURCE, CAPTURE_TRIGGER_EDGE,
                 CAPTURE_FEEDBACK_AXIS_NUMBER, CAPTURE_ENCODER,
                 CAPTURE_GLOBAL_TRIGGER);

    // arming Capture before commanding a velocity move
    axis->CaptureArm(true);
    axis->ClearFaults();
    axis->AmpEnableSet(true);

    // setup Home Action (the home action will trigger when CaptureStatus == Captured)
    axis->HomeActionSet(RSIActionE_STOP);
    
    // commanding a velocity move
    axis->MoveVelocity(VELOCITY, ACCEL);
    
    printf("\n Moving...Waiting for Capture Source (Encoder Index pulse) edge to Capture a position... \n");

    // wait (sleep) until motion done interrupt occurs
    while (axis->InterruptWait(RSIWaitFOREVER) != RSIEventTypeMOTION_DONE)
    {
    }

    // check the CaptureState
    if(axis->CaptureStateGet() == RSICaptureStateCAPTURED)
    {
      // subtract Origin Position from Captured Position
      capturedPosition =  ( axis->CapturePositionGet() - axis->OriginPositionGet());
      printf("\n Captured Position: %lf\n\n ", capturedPosition);
    }
    else
    {
      printf(" The Capture never triggered.\n");
    }
  }
  catch (RsiError *err)
  {
    printf("%s\n", err->text);
  }
}