Buffered High-Speed Capture C/C++ Code

NI-Motion

Buffered High-Speed Capture 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
   u16 csr   = 0;                // Communication status register
   i32 bufferSize = 100;         // The size of the buffer to allocate on the motion controller
   u32 totalPoints = 100;        // The number of high speed capture to acquire
   i32 capturedPositions[100];   // Array to store the captured positions
   f64 actualInterval;           // The interval the controller can really contour at
   u32 backlog;                  // Indicates the available space for captured positions
   u32 pointsDone;               // Indicates the number of points that have been consumed
   u16 bufferState;              // Indicates the state of the onboard buffer
   u32 currentDataPoint = 0;     // Indicates the next points to be read from the buffer
   i32* readBuffer = NULL;       // The temporary array that is created to read captured positions

   u32 i;

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

   ///////////////////////////////
   // Set the board ID
   boardID = 1;
   // Set the axis number
   axis = NIMC_AXIS1;
   ////////////////////////////////

   // Configure buffer on motion controller memory (RAM)
   // Note requested time interval is hardcoded to 10 milliseconds
   err = flex_configure_buffer(boardID, 1 /*buffer number*/, axis, NIMC_HS_CAPTURE_READBACK, bufferSize,
                                      totalPoints, NIMC_TRUE, 10, &actualInterval);
   CheckError;

   // Configure High Speed Capture
   err = flex_configure_hs_capture(boardID, axis, NIMC_HS_LOW_TO_HIGH_EDGE, NIMC_OPERATION_BUFFERED);
   CheckError;

   // Enable the high speed capture on axis
   err = flex_enable_hs_capture(boardID, axis, NIMC_TRUE);
   CheckError;

   do
   {
      err = flex_check_buffer_rtn(boardID, 1 /*buffer number*/, &backlog, &bufferState, &pointsDone);
      CheckError;

      // Check backlog for captured position in buffer
      if (backlog > 0)
      {
         readBuffer = (i32*)malloc(sizeof(i32)*backlog);

         // If captured position available in the buffer, read the captured position from the buffer
         err = flex_read_buffer_rtn(boardID, 1/*buffer number*/, backlog, readBuffer);

         for(i=0;i<backlog;i++){
            if(currentDataPoint > totalPoints) break;
            capturedPositions[currentDataPoint] = readBuffer[i];
            printf("capture pos %d\n",capturedPositions[currentDataPoint]);
            currentDataPoint++;
         }
         free(readBuffer);
         readBuffer = NULL;
         CheckError;
      }

      // Check for axis off status/following error or any modal errors
      // Read the communication status register and check the modal errors
      err = flex_read_csr_rtn(boardID, &csr);
      CheckError;
      // Check the modal errors
      if (csr & NIMC_MODAL_ERROR_MSG){
         err = csr & NIMC_MODAL_ERROR_MSG;
         CheckError;
      }
   Sleep(60);   // Check every 60 ms
   } while (bufferState != NIMC_BUFFER_DONE);

   // Free the buffer allocated on the controller memory
   err = flex_clear_buffer(boardID, 1/*buffer number*/);
   CheckError;

   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
}