Electronic Gearing C/C++ Code

NI-Motion

Electronic Gearing 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
   u16 moveComplete;

   // 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 3:2
   err =  flex_load_gear_ratio(boardID, slaveAxis, NIMC_RELATIVE_GEARING,
                              3/* ratioNumerator*/, 2/* ratioDenominator*/, 0xFF);
   CheckError;

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

   // Wait for 5 seconds
   Sleep(5000);
   
   //--------------------------------------------------------
   // Set up the move parameters for the superimposed move
   //--------------------------------------------------------
   
   // Set the operation mode to relative
   err = flex_set_op_mode(boardID, slaveAxis, NIMC_RELATIVE_TO_CAPTURE);
   CheckError;

   // Load Velocity in counts/s
   err = flex_load_velocity(boardID, slaveAxis, 10000, 0xFF);
   CheckError;

   // Load Acceleration and Deceleration in counts.sec^2
   err = flex_load_acceleration(boardID, slaveAxis, NIMC_BOTH, 100000, 0xFF);
   CheckError;

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

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


   err = flex_wait_for_move_complete (boardID, slaveAxis, 0, 
                                      1000/*ms timeout*/, 20/*ms pollInterval*/, &moveComplete);
   CheckError;

   //--------------------------------------------------------
   // Disable Gearing on slave axis
   //--------------------------------------------------------
   err = flex_enable_gearing_single_axis (boardID, slaveAxis, NIMC_FALSE);
   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
}