Straight-Line Move C/C++ Code

NI-Motion

Straight-Line Move C/C++ Code

Tip  The following example code is not necessarily complete, and may not compile if copied exactly. Refer to NI-Motion\Documentation\Examples\NI-Motion User Manual\ for files that are complete and compile as is.

1D Straight-Line Move Code

// Main function
void main(void)
{

   u8 boardID;         // Board identification number
   u8 axis;            // Axis number
   u16 csr   = 0;      // Communication status register
   u16 axisStatus;     // Axis status
   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
   axis = 1;
   ////////////////////////////////

   // Set the velocity for the move (in counts/sec)
   err = flex_load_velocity(boardID, axis, 10000, 0xFF);
   CheckError;
   
   // Set the acceleration for the move (in counts/sec^2)
   err = flex_load_acceleration(boardID, axis, NIMC_ACCELERATION, 100000, 0xFF);
   CheckError;
   
   // Set the deceleration for the move (in counts/sec^2)
   err = flex_load_acceleration(boardID, axis, NIMC_DECELERATION, 100000, 0xFF);
   CheckError;

   // Set the jerk - s-curve time (in sample periods)
   err = flex_load_scurve_time(boardID, axis, 1000, 0xFF);
   CheckError;
   
   // Set the operation mode
   err =  flex_set_op_mode(boardID, axis, NIMC_ABSOLUTE_POSITION);
   CheckError;

   // Load Position
   err = flex_load_target_pos(boardID, axis, 5000, 0xFF);
   CheckError;

   // Start the move
   err = flex_start(boardID, axis, 0);
   CheckError;

   do
   {
      axisStatus = 0;
      // Check the move complete status
      err = flex_check_move_complete_status(boardID, axis, 0, &moveComplete);
      CheckError;
      
      // Check the following error/axis off status for the axis
      err = flex_read_axis_status_rtn(boardID, axis, &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 (!moveComplete && !(axisStatus & NIMC_FOLLOWING_ERROR_BIT) && !(axisStatus & NIMC_AXIS_OFF_BIT)); // Exit on move complete/following error/axis off

   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
}

2D Straight-Line Move Code

// Main function
void main(void)
{

   u8   boardID;           // Board identification number
   u8   vectorSpace;       // Vector space number
   u16 csr   = 0;          // Communication status register
   u16 axisStatus;         // Axis status
   u16 status;
   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
   vectorSpace = NIMC_VECTOR_SPACE1;
   ////////////////////////////////

   // Configure a 2D Vector Space comprising of axes 1 and 2
   err = flex_config_vect_spc(boardID, vectorSpace, 1, 2, 0);
   CheckError;

   // Set the velocity for the move (in counts/sec)
   err = flex_load_velocity(boardID, vectorSpace, 10000, 0xFF);
   CheckError;
   
   // Set the acceleration for the move (in counts/sec^2)
   err = flex_load_acceleration(boardID, vectorSpace, NIMC_ACCELERATION, 100000, 0xFF);
   CheckError;
   
   // Set the deceleration for the move (in counts/sec^2)
   err = flex_load_acceleration(boardID, vectorSpace, NIMC_DECELERATION, 100000, 0xFF);
   CheckError;

   // Set the jerk - s-curve time (in sample periods)
   err = flex_load_scurve_time(boardID, vectorSpace, 1000, 0xFF);
   CheckError;
   
   // Set the operation mode
   err =  flex_set_op_mode(boardID, vectorSpace, NIMC_ABSOLUTE_POSITION);
   CheckError;

   // Load Vector Space Position
   err = flex_load_vs_pos(boardID, vectorSpace, 5000/*x Position*/, 10000/*y Position*/, 0/* z Position*/, 0xFF);
   CheckError;

   // Start the move
   err = flex_start(boardID, vectorSpace, 0);
   CheckError;

   do
   {
      axisStatus = 0;
      // Check the move complete status
      err = flex_check_move_complete_status(boardID, vectorSpace, 0, &moveComplete);
      CheckError;
      
      // Check the following error/axis off status for axis 1
      err = flex_read_axis_status_rtn(boardID, 1, &status);
      CheckError;
      axisStatus |= status;


      // Check the following error/axis off status for axis 2
      err = flex_read_axis_status_rtn(boardID, 2, &status);
      CheckError;
      axisStatus |= status;
      

      // 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 (!moveComplete && !(axisStatus & NIMC_FOLLOWING_ERROR_BIT) && !(axisStatus & NIMC_AXIS_OFF_BIT)); // Exit on move complete/following error/axis off

   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
}