Velocity-Based Straight-Line Move 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 u16 axisStatus; // Axis status i32 moveTime1; // Time for the 1st segment i32 moveTime2; // Time for the 2nd segment i32 initialTime; i32 currentTime; // 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 = 3; // Set the axis number axis = 1; // Move time for the first segment moveTime1 = 5000; //milliseconds // Move time for the second segment moveTime2 = 10000; //milliseconds //////////////////////////////// //------------------------------------------------------------ // First segment //------------------------------------------------------------ // 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 value) for the move (in sample periods) err = flex_load_scurve_time(boardID, axis, 100, 0xFF); CheckError; // Set the operation mode to velocity err = flex_set_op_mode(boardID, axis, NIMC_VELOCITY); CheckError; // Start the move err = flex_start(boardID, axis, 0); CheckError; // Wait for the time for first segment initialTime = timeGetTime(); do { // Check the move complete status/following error/axis off status 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; } // Get the current time and check if time is over for the first segment currentTime = timeGetTime(); if((currentTime - initialTime) >= moveTime1) break; Sleep (50); // Check every 50 ms }while (!(axisStatus & NIMC_MOVE_COMPLETE_BIT) && !(axisStatus & NIMC_FOLLOWING_ERROR_BIT) && !(axisStatus & NIMC_AXIS_OFF_BIT)); // Exit on move complete/following error/axis off //------------------------------------------------------------ // Second segment //------------------------------------------------------------ // Set the velocity for the move (in counts/sec) err = flex_load_velocity(boardID, axis, 6568, 0xFF); CheckError; // Start the move - to update the velocity err = flex_start(boardID, axis, 0); CheckError; // Wait for the time for second segment initialTime = timeGetTime(); do { // Check the move complete status/following error/axis off status 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; } // Get the current time and check if time is over for the second segment currentTime = timeGetTime(); if((currentTime - initialTime) >= moveTime2) break; Sleep (50); // Check every 50 ms }while (!(axisStatus & NIMC_MOVE_COMPLETE_BIT) && !(axisStatus & NIMC_FOLLOWING_ERROR_BIT) && !(axisStatus & NIMC_AXIS_OFF_BIT)); // Exit on move complete/following error/axis off // Decelerate the axis to a stop err = flex_stop_motion(boardID, axis, NIMC_DECEL_STOP, 0); 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 }