Acquire Data C/C++ Code

NI-Motion

Acquire Data 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
   u16 csr   = 0;                // Communication status register
   i32 i;
   u16 axisMap;                  // Bitmap of axes for which data is requested
   i32 axis1Positions[200];      // Array to store the positions (1)
   i32 axis1Velocities[200];     // Array to store velocities   (1)
   i32 axis2Positions[200];      // Array to store the positions (2)
   i32 axis2Velocities[200];     // Array to store velocities   (2)
   u16 numSamples = 200;         // Number of samples
   i32 returnData[4];            // Need size of 4 for 2 axes worth of data

   // 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;
   // Axes whose data needs to be acquired
   axisMap = ((1<<1) | (1<<2)); // Axis 1 and axis 2
   ////////////////////////////////


   err =  flex_acquire_trajectory_data(boardID, axisMap, numSamples, 3/* ms time period*/);
   CheckError;
   
   for(i=0; i<numSamples; i++){
         
      Sleep (10); // Check every 10 ms and give time for the data
                  // to be copied to onboard FIFO
   

      // Read the trajectory data
      err =  flex_read_trajectory_data_rtn(boardID, returnData);
      CheckError;
      
      // Two axes worth of data is read every sample
      axis1Positions[i] = returnData[0];
      axis1Velocities[i] = returnData[1];
      axis2Positions[i] = returnData[2];
      axis2Velocities[i] = returnData[3];


   }
   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
}