Rotating Knife C/C++ Code

NI-Motion

Rotating Knife 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 slaveAxis;                     // Slave axis number
   u8 master;                        // Gear master
   u16 csr   = 0;                    // Communication status register
   i32 synchronizationPosition = 0;  // Synchronization position
   i32 correctionPoint = 500;        // Point where the correction can be applied
   i32 cyclePosition = 2000;         // One revolution is 2000 counts
   i32 currentPosition;              // The current slave position
   i32 capturedPosition;             // The position at which the trigger happens
   u16 axisStatus;

   // 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
   slaveAxis = 1;
   // Master is encoder 4
   master = NIMC_ENCODER4;
   ////////////////////////////////
   //--------------------------------------------------------
   // Set up the gearing configuration for the slave axis
   //--------------------------------------------------------
   // Configure Gear Master
   err = flex_config_gear_master(boardID, slaveAxis, master);
   CheckError;

   // Load Gear Ratio 1:1
   err =  flex_load_gear_ratio(boardID, slaveAxis, NIMC_ABSOLUTE_GEARING,
                              1/* ratioNumerator*/, 1/* ratioDenominator*/, 0xFF);
   CheckError;

   //--------------------------------------------------------
   // Set up the move parameters for the superimposed move
   // to be done on registration
   //--------------------------------------------------------
   // Set the operation mode to relative
   err = flex_set_op_mode(boardID, slaveAxis, NIMC_RELATIVE_TO_CAPTURE);
   CheckError;

   // Load Velocity in RPM
   err = flex_load_rpm(boardID, slaveAxis, 100.00, 0xFF);
   CheckError;

   // Load Acceleration and Deceleration in RPS/sec
   err = flex_load_rpsps(boardID, slaveAxis, NIMC_BOTH, 50.00, 0xFF);
   CheckError;


   //--------------------------------------------------------
   // Enable Gearing on slave axis
   //--------------------------------------------------------
   err = flex_enable_gearing_single_axis (boardID, slaveAxis, NIMC_TRUE);
   CheckError;


   //--------------------------------------------------------
   // Wait for trigger to do the registration move
   //--------------------------------------------------------
   for(;;){
      // Enable High speed capture for slave axis
      err = flex_enable_hs_capture(boardID, slaveAxis, NIMC_TRUE);
      CheckError;

      do
      {
         // Check the high speed capture status/following error/axis off status
         err = flex_read_axis_status_rtn(boardID, slaveAxis, &axisStatus);
         CheckError;

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



      }while (!(axisStatus & NIMC_HIGH_SPEED_CAPTURE_BIT) && !(axisStatus & NIMC_FOLLOWING_ERROR_BIT) && !(axisStatus & NIMC_AXIS_OFF_BIT)); // Exit on following error/axis off

      if((axisStatus & NIMC_FOLLOWING_ERROR_BIT) || (axisStatus & NIMC_AXIS_OFF_BIT)){
         break; // Break out of the for loop
      }

      // Update the variables for this cycle
      synchronizationPosition += cyclePosition;
      correctionPoint += cyclePosition;

      // Read the captured position
      err = flex_read_cap_pos_rtn(boardID, slaveAxis, &capturedPosition);
      CheckError;

      // Load the target position for the registration (superimposed) move
      err = flex_load_target_pos(boardID, slaveAxis, (synchronizationPosition-capturedPosition), 0xFF);
      CheckError;

      // Wait until we have passed the correction point before we apply the correction
      currentPosition = 0;
      while (currentPosition < correctionPoint){
         err = flex_read_pos_rtn(boardID, slaveAxis, &currentPosition);
         CheckError;
      }

      // Start registration move on the slave
      err = flex_start(boardID, slaveAxis, 0);
      CheckError;
   }// For loop


   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
}