Electronic Camming C/C++ Code

NI-Motion

Electronic Camming 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)
{
   // Locals
   u8 boardID;             // Board ID as assigned by MAX
   f64 bufferInterval = 0; // Ignored

   // Master axis information  
   u8 masterAxis;          // Master axis ID
   i32 targetPos;          // Position to move to
   f64 velocity;           // Velocity limit for this move
   f64 camCycle = 70000;   // Position cycle to repeat the camming process
   NIMC_DATA data;         // Generic data structure
   i32 masterPosition;     // Current master position
   u16 masterStatus;       // Current master status

   // Slave axis information
   u8 slaveAxis;           // Slave axis ID
   u8 buffer;              // Buffer to contain the cam table
   i32 positionArr[] = {0, 10000, 40000, 45000, 45000, 40000, 10000, 0}; // Position array
   u32 positionSize = sizeof(positionArr) / sizeof(i32);   // Number of positions in the array
   i32 slavePosition;      // Current slave position

   // For error handling
   u16 csr;                // Communication status
   u16 commandID;          // Command ID that causes the error
   u16 resourceID;         // Resource ID that is set on the failed command
   i32 errorCode;          // Error code from the controller
   i32 scanVar;            // Scan variable to read in values not supported
                           // by the scanf function
   // Get the board ID
   printf("Enter the board ID: ");
   scanf("%d", &scanVar);
   boardID=(u8)scanVar;

   // Get master axis information
   printf("\nEnter information about the master axis... \n");
   printf("Axis ID: ");
   scanf("%d", &scanVar);
   masterAxis=(u8)scanVar;

   printf("Target position: ");
   scanf("%d", &targetPos);

   printf("Velocity limit: ");
   scanf("%lf", &velocity);

   // Get the slave Axis
   printf("\nEnter information about the slave axis... \n");
   printf("Axis ID: ");
   scanf("%d", &scanVar);
   slaveAxis=(u8)scanVar;

   printf("Buffer ID: ");
   scanf("%d", &scanVar);
   buffer=(u8)scanVar;

   // Configure the cam master & master cycle
   err = flex_configure_camming_master(boardID, slaveAxis, masterAxis, camCycle);
   CheckError;

   // Configure the cam table
   err = flex_configure_buffer(boardID, buffer, slaveAxis, 
                               NIMC_CAMMING_POSITION,
                               positionSize, positionSize, 
                               TRUE, bufferInterval, &bufferInterval);
   CheckError;

   // Write the data to the buffer
   err = flex_write_buffer(boardID, buffer, positionSize, 
                           NIMC_REGENERATION_NO_CHANGE, 
                           positionArr, 0xFF);
   CheckError;

   // Enable camming immediately
   err = flex_enable_camming_single_axis(boardID, slaveAxis, TRUE, -1.0);
   CheckError;

   // Set to absolute mode
   err = flex_set_op_mode(boardID, masterAxis, NIMC_ABSOLUTE_POSITION);
   CheckError;

   // Set the maximum velocity
   data.doubleData = velocity;
   err = flex_load_move_constraint(boardID, masterAxis, 
                                   TnimcMoveConstraintVelocity, &data);
   CheckError;

   // Set the target position
   err = flex_load_target_pos(boardID, masterAxis, targetPos, 0xFF);
   CheckError;

   // Start the master axis movement
   err = flex_start(boardID, masterAxis, 0x0);
   CheckError;

   // Keep running until the master completes or there's a modal error
   do
   {
      Sleep (100);

      // Get both the master and the slave current positions.
      err = flex_read_pos_rtn(boardID, masterAxis, &masterPosition);
      CheckError;

      err = flex_read_pos_rtn(boardID, slaveAxis, &slavePosition);
      CheckError;

      // Display the master and the slave current positions.
      printf("Axis %d (master) position is %d, Axis %d (slave) position is %d.\r", 
         masterAxis, masterPosition, slaveAxis, slavePosition);
      
      // Read current master trajectory status
      err = flex_read_axis_status_rtn(boardID, masterAxis, &masterStatus);
      CheckError;

      // Check for modal error
      err = flex_read_csr_rtn(boardID, &csr);
      CheckError;

   } while (!(masterStatus & NIMC_MOVE_COMPLETE_BIT) && 
            !(csr & NIMC_MODAL_ERROR_MSG));

   /////////////////////////////////////////////////////////////////////////
   // Error Handling
   //
   nimcHandleError; // NIMCCATCHTHIS:

   // Disable camming   
   flex_enable_camming_single_axis(boardID, slaveAxis, FALSE, -1.0);

   // Clear (delete) the buffer
   flex_clear_buffer(boardID, buffer);

   // Make sure the master is not running
   flex_stop(boardID, masterAxis, 0x0); 

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