customHome.cpp

Homing routine that captures the hardware position, sets the originand then moves back to home.

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


 home.c allows the user to trigger his home off an input or index
 pulse, capture the hardware position, set the origin and then move back
 to that home position.

 CaptureConfigSet(...) has been used in this sample application. Please look
 at our sample application 'capture1.c' for information.

 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            RSICaptureSourceHOME
#define CAPTURE_TRIGGER_EDGE        RSICaptureEdgeFALLING
#define CAPTURE_FEEDBACK_AXIS_NUMBER    AXIS_NUMBER
#define CAPTURE_ENCODER           RSIMotorEncoderPRIMARY
#define CAPTURE_GLOBAL_TRIGGER        false

/* Motion Parameters */
#define VELOCITY              5000  
#define ACCELERATION            80000 
#define DECELERATION                        80000
#define POSITION              0


//Method prints captured state
void PrintCaptureState(Axis *axis)
{
  char* text;
  long captureState = axis->CaptureStateGet();
  switch(captureState)
  {
    case RSICaptureStateIDLE:
      text = "IDLE";
      break;
    case RSICaptureStateARMED:
      text = "ARMED";
      break;
    case RSICaptureStateCAPTURED:
      text = "CAPTURED";
      break;
    default:
      text = "UNKNOWN";
      break;
  }
  printf("Capture State is: %s\n",text );
}


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

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

    // 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->Abort();
    axis->ClearFaults();
    axis->AmpEnableSet(true);
    axis->CaptureArm(true);
  
    PrintCaptureState(axis);
    printf("Looking for Home...\n\n");

    // setup Home Action (the home action will trigger when CaptureStatus == Captured)
    axis->HomeActionSet(RSIActionSTOP);

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

    // commanding a velocity move
    axis->MoveVelocity(VELOCITY, ACCELERATION);

    // wait (sleep) until motion done interrupt occurs
    while (axis->InterruptWait(RSIWaitFOREVER) != RSIEventTypeMOTION_DONE)
    {
    }
    // check the CaptureState
    PrintCaptureState(axis);
    if(axis->CaptureStateGet() == RSICaptureStateCAPTURED)
    {
      // subtract Origin Position from Captured Position
      capturedPosition =  ( axis->CapturePositionGet() - axis->OriginPositionGet());
      printf("\nCaptured Position: %lf\n\n", capturedPosition);
      
      // set the Origin to Captured Position (captured position becomes new zero position)
      axis->OriginPositionSet(axis->CapturePositionGet());
    }
    else
    {
      printf("The Capture never triggered.\n");
    }
      
    // setup Home Action (the home action will not trigger)
    axis->HomeActionSet(RSIActionNONE);
      
    axis->ClearFaults();
    axis->AmpEnableSet(true);

    printf("Moving back to origin...\n\n");
    //Moving back to home
    axis->MoveTrapezoidal(POSITION, VELOCITY, ACCELERATION, DECELERATION);
  }
  catch (RsiError *err)
  {
    printf("%s\n", err->text);
  }
}