Reference Move C/C++ Code

NI-Motion

Reference Move C/C++ Code

The following example code is not necessarily complete, and may not compile if copied exactly. Refer to the examples folder on the NI-Motion CD for files that are complete and compile as is.

// Main function
void main(void){

   u8  boardID;                // Board identification number
   u8  axis;                   // Axis number
   f64 acceleration   =100;    // Acceleration value in RPS/S
   f64 velocity      =200;     // Velocity value in RPM
   u16 found, finding;         // Check Reference Statuses
   u16 axisStatus;             // Axis status
   u16 csr=0;                  // Communication status Register
   i32 position;               // Current position of axis
   i32 scanVar;                // Scan variable to read in values not supported
                                                // by the scanf function

   // Variables for modal error handling
   u16 commandID;              // The commandID of the function
   u16 resourceID;             // The resource ID
   i32 errorCode;              // Error code

   // Get the board ID
   printf("Enter the Board ID: ");
   scanf("%d", &scanVar);
   boardID=(u8)scanVar;


   // Check if the board is at power up reset condition
   err = flex_read_csr_rtn(boardID, &csr);
   CheckError;

    if (csr & NIMC_POWER_UP_RESET ){
      printf("\nThe FlexMotion board is in the reset condition. Please initialize the board before ");
      printf("running this example.  The \"flex_initialize_controller\" function will initialize the ");
      printf("board with settings selected through Measurement and Automation Explorer\n");
      return;
   }

   // Get the axis number
   printf("Enter the axis: ");
   scanf("%d",&scanVar);
   axis=(u8)scanVar;

   // Flush the Stdin
   fflush(stdin);

   // Load acceleration and deceleration to the axis selected
   err = flex_load_rpsps(boardID, axis, NIMC_BOTH, acceleration, 0xFF);
   CheckError;

   // Load velocity to the axis selected
   err = flex_load_rpm(boardID, axis, velocity, 0xFF);
   CheckError;

   // Start the Find Reference move
   err = flex_find_reference(boardID, axis, 0, NIMC_FIND_HOME_REFERENCE);
   CheckError;

   // Wait for find reference to complete on the axis AND
   // also check for modal errors at the same time
   do{
      // Read the current position of axis
      err = flex_read_pos_rtn(boardID, axis, &position);
      CheckError;

      // Display the current position of axis
      printf("\rAxis %d position: %10d", axis, position);

      // Check if the axis has stopped b/c of axis off or following error
      err = flex_read_axis_status_rtn(boardID, axis, &axisStatus);

      // Check if the reference has finished finding
      err = flex_check_reference(boardID, axis, 0, &found, &finding);
      CheckError;

      // Read the Communication Status Register - check the
      // modal error bit
      err = flex_read_csr_rtn(boardID, &csr);
      CheckError;

      if (csr & NIMC_MODAL_ERROR_MSG)
      {
         flex_stop_motion(boardID,NIMC_AXIS1, NIMC_DECEL_STOP, 0);//Stop the Motion
         err = csr & NIMC_MODAL_ERROR_MSG;
         CheckError;
      }

   // test for find reference complete, following error, or axis off status
   }while ( !(axisStatus & (NIMC_FOLLOWING_ERROR_BIT | NIMC_AXIS_OFF_BIT)) && finding);
   printf("\nAxis %d position: %10d", axis, position);
   if (found)
      printf("\rAxis found reference");
   else
      printf("\rAxis did not find reference");
   printf("\n\nFinished.\n");

   return;      // Exit the Application



   /////////////////////////////////////////////////////////////////////////
   // Error Handling
   //
   nimcHandleError; //NIMCCATCHTHIS:
   
   // Check to see if there were any Modal Errors
   if (csr & NIMC_MODAL_ERROR_MSG){
      do{
         // Get the command ID, resource and the error code of the modal
         // error from the error stack on the board
         flex_read_error_msg_rtn(boardID,&commandID,&resourceID,&errorCode);
         nimcDisplayError(errorCode,commandID,resourceID);

         // Read the Communication Status Register
         flex_read_csr_rtn(boardID,&csr);

      }while(csr & NIMC_MODAL_ERROR_MSG);
   }
   else       // Display regular error
      nimcDisplayError(err,0,0);
   return;    // Exit the Application
}