Updating Velocity Based on ADC Channel 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
i32 constant; // Constant multiplier
// 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;
// constant to multiply the ADC value read to calculate the required velocity
constant = 10;
////////////////////////////////
// Initialize onboard variable 4 to 0
err = flex_load_var(boardID, 0, 4);
CheckError;
// Initialize onboard variable 1 to the constant multiplier
err = flex_load_var(boardID, constant, 1);
CheckError;
// Begin onboard program storage - program number 1
err = flex_begin_store(boardID, 1);
// Set the operation mode to velocity
err = flex_set_op_mode(boardID, axis, NIMC_VELOCITY);
CheckError;
// Insert Label number 1
err = flex_insert_program_label(boardID, 1);
CheckError;
// Read ADC channel and store ADC value in variable 2
err = flex_read_adc(boardID, NIMC_ADC1, 2);
CheckError;
// Multiply Variables 2 i.e. the ADC value with 1 i.e. the constant
// Save the result in variable 3
err = flex_mult_vars(boardID, 1, 2, 3);
CheckError;
// Subtract value in variable 3 from variable 4
// We are not interested in the result - just want to
// set the condition on board.
err = flex_sub_vars(boardID, 3, 4, 0);
CheckError;
// Jump to label 1 as the subtraction above set the condition
// to "equal to zero" which implies that the values in variable
// 3 and 4 are the same
err = flex_jump_label_on_condition (boardID, 0, NIMC_CONDITION_EQUAL, 0, 0, NIMC_MATCH_ALL, 1/*label number*/);
// Set the velocity for the move (in counts/sec) by loading the
// value from variable 3 which is (adc value * constant)
err = flex_load_velocity(boardID, axis, 0, 3);
CheckError;
// Start the move to update the velocity
err = flex_start(boardID, axis, 0);
CheckError;
// Save the value in variable 3 to variable 4 for use
// in next cycle
err = flex_read_var(boardID, 3, 4);
CheckError;
// Jump back to label 1 unconditionally
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);
// To execute this program use the Run Program function
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
}