Periodic Breakpoint C/C++ Code

NI-Motion

Periodic Breakpoint C/C++ Code

The following section includes C/C++ code for executing a high-speed capture, as well as using RTSI to execute a high-speed capture. 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
   u16 csr   = 0;             // Communication status Register
   u8  profileStatus;         // Profile Complete Status
   u8  bpStatus;              // Breakpoint found Status
   i32 bpPos;                 // Breakpoint Position
   i32 bpPer;                 // Breakpoint Period
   i32 targetPos;             // Target Position
   i32 currentPos;            // Current Position
   u16 axisStatus;            // Status of the axis

   // 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("%u", &boardID);

   // Get the axis number
   printf("Enter a axis number: ");
   scanf("%u",&axis);

   // Get the Target Position
   printf("Enter a target position: ");
   scanf("%ld",&targetPos);

   // Get the Breakpoint Position
   printf("Enter a breakpoint position: ");
   scanf("%ld",&bpPos);

   // Get the Breakpoint Period
   printf("Enter a breakpoint period: ");
   scanf("%ld",&bpPer);

   // Configure the breakpoint to be absolute
   err = flex_configure_breakpoint(boardID,axis,NIMC_PERIODIC_BREAKPOINT,NIMC_NO_CHANGE,0);
   CheckError;

   // Load the position to start breakpoints
   err = flex_load_pos_bp(boardID,axis,bpPos,0xFF);
   CheckError;

   // Set the Period
   err = flex_load_bp_modulus(boardID,axis,bpPer,0xFF);
   CheckError;

   // Enable the breakpoint
   err = flex_enable_breakpoint(boardID,axis,NIMC_TRUE);
   CheckError;

   // Load a target position
   err = flex_load_target_pos(boardID,axis,targetPos,0xFF);
   CheckError;

   // Start the motion
   err = flex_start(boardID,axis,0);
   CheckError;

   printf("\n");

   do
   {
      // Read the axis status
      err = flex_read_axis_status_rtn(boardID,axis,&axisStatus);
      CheckError;

      err = flex_read_pos_rtn(boardID,axis,&currentPos);
      CheckError;

      // Check the breakpoint bit
      bpStatus = !((axisStatus & NIMC_POS_BREAKPOINT_BIT)==0);

      // Check the profile complete bit
      profileStatus = !((axisStatus & NIMC_PROFILE_COMPLETE_BIT)==0);

      printf("Current Position=%10d Breakpoint Status=%d Profile Complete=%d\r",currentPos,bpStatus,profileStatus);

      // Check for modal errors
      err = flex_read_csr_rtn(boardID,&csr);
      CheckError;
      // Check the modal errors
      if (csr & NIMC_MODAL_ERROR_MSG)
      {
         flex_stop_motion(boardID,NIMC_VECTOR_SPACE1, NIMC_DECEL_STOP, 0);// Stop the Motion
         err = csr & NIMC_MODAL_ERROR_MSG;
         CheckError;
      }

   }while(!profileStatus);

   printf("\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
}