Onboard Subroutine C/C++ Code

NI-Motion

Onboard Subroutine 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  axis;                 // Axis number
   u16 csr   = 0;            // Communication status register

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

   //--------------------------------------------------------
   // Onboard program 2.  This onboard program moves axis one
   // clockwise 5000 counts (steps).  This onboard program
   // is executed by onboard program one.
   //--------------------------------------------------------

   // Begin onboard program storage - program number 2
   err = flex_begin_store(boardID, 2);
   CheckError;

   // Set the operation mode to relative
   err = flex_set_op_mode(boardID, axis, NIMC_RELATIVE_POSITION);
   CheckError;

   // Load Target Position to move clockwise 5000 counts(steps)
   err = flex_load_target_pos(boardID, axis, 5000, 0xFF);
   CheckError;

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

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

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

   // Wait for move to complete
   err =  flex_wait_on_condition(boardID, 0, NIMC_WAIT, NIMC_CONDITION_MOVE_COMPLETE, 2/*Indicates axis 1*/,
                                    0, NIMC_MATCH_ALL, 1000 /*time out*/, 0);
   CheckError;

   // End Program Storage
   err = flex_end_store(boardID, 2);
   CheckError;


   //--------------------------------------------------------
   // Onboard program 3.  This onboard program moves axis one
   // counter clockwise 5000 counts (steps).  This onboard program
   // is executed by onboard program one.
   //--------------------------------------------------------

   // Begin onboard program storage - program number 3
   err = flex_begin_store(boardID, 3);
   CheckError;

   // Set the operation mode to relative
   err = flex_set_op_mode(boardID, axis, NIMC_RELATIVE_POSITION);
   CheckError;

   // Load Target Position to move counter clockwise 5000 counts(steps)
   err = flex_load_target_pos(boardID, axis, -5000, 0xFF);
   CheckError;

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

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

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

   // Wait for move to complete
   err =  flex_wait_on_condition(boardID, 0, NIMC_WAIT, NIMC_CONDITION_MOVE_COMPLETE, 2/*Indicates axis 1*/,
                                    0, NIMC_MATCH_ALL, 1000 /*time out*/, 0);
   CheckError;

   // End Program Storage
   err = flex_end_store(boardID, 3);
   CheckError;


   //--------------------------------------------------------
   // Onboard program 1.  The main onboard program monitors
   // an IO line and based on state of the IO line executes
   // onboard program 2 or onboard program 3
   //--------------------------------------------------------

   // Begin onboard program storage - program number 1
   err = flex_begin_store(boardID, 1);
   CheckError;

   // Insert Label number 1
   err = flex_insert_program_label(boardID, 1);
   CheckError;

   // Jump to label 2 if the line 1 on port one is active
   err =  flex_jump_label_on_condition (boardID, NIMC_IO_PORT1, NIMC_CONDITION_IO_PORT_MATCH, 2/*Indicates line 1*/,  0, NIMC_MATCH_ALL, 2/*label number*/);
   CheckError;

   // If the above jump failed i.e. the IO line is not active
   // execute program #3
   err =  flex_run_prog(boardID, 3);
   CheckError;

   // Wait for program #3 to finish executing
   err =  flex_wait_on_condition(boardID, 3 /*program #*/, NIMC_WAIT, NIMC_CONDITION_PROGRAM_COMPLETE, 0, 0, NIMC_MATCH_ALL, 1000 /*time out*/, 0);
   CheckError;

   // Jump unconditionally to label 1 and check IO line again
   err =  flex_jump_label_on_condition (boardID, 0, NIMC_CONDITION_TRUE, 0, 0, NIMC_MATCH_ALL, 1/*label number*/);
   CheckError;


   // Insert Label number 2
   err = flex_insert_program_label(boardID, 2);
   CheckError;

   // Execute program #2
   err =  flex_run_prog(boardID, 2);
   CheckError;

   // Wait for program #2 to finish executing
   err =  flex_wait_on_condition(boardID, 2 /*program #*/, NIMC_WAIT, NIMC_CONDITION_PROGRAM_COMPLETE, 0,
                                    0, NIMC_MATCH_ALL, 1000 /*time out*/, 0);
   CheckError;

   // Jump unconditionally to label 1 and check IO line again
   err =  flex_jump_label_on_condition (boardID, 0, NIMC_CONDITION_TRUE, 0, 0, NIMC_MATCH_ALL, 1/*label number*/);
   CheckError;
//
//
   // End Program Storage
   err = flex_end_store(boardID, 1);
   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
}