Tutorial 3-3: Blocking motion using Wait function
Functions in the WMX3 library can be blocking or non-blocking. A blocking function will stop the execution of the thread until a particular condition has been met. For example, the CreateDevice function will stop execution until the engine process has started and has finished initializing. A non-blocking function will immediately return execution to the calling thread after the function has been processed, typically in the order of microseconds or less.
Unless noted otherwise, functions in the WMX3 library are non-blocking. Also see Function Calls That Are Blocking for additional information regarding blocking functions.
The Wait function will block execution of the calling thread until the motion of an axis is finished. Other overloaded Wait functions will block until a specified condition is met.
Also see Wait Operation for additional information regarding Wait functions.
The following code executes three position commands with blocking Wait function calls between them to wait until each motion has finished before continuing execution. Without the Wait function, all three position commands will be executed rapidly in sequence, and the axis will only move to the position of the last position command.
Motion::PosCommand pos;
//Set position command parameters
pos.axis = 0;
pos.profile.type = ProfileType::Trapezoidal;
pos.profile.velocity = 10000;
pos.profile.acc = 10000;
pos.profile.dec = 10000;
//Execute motion to position 10000
pos.target = 10000;
err = wmxlib_cm.motion->StartPos(&pos);
if (err != ErrorCode::None) {
wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
printf("Failed to execute motion. Error=%d (%s)\n", err, errString);
goto exit;
}
//Block execution until motion is finished
err = wmxlib_cm.motion->Wait(0);
//Execute motion to position 20000
pos.target = 20000;
err = wmxlib_cm.motion->StartPos(&pos);
if (err != ErrorCode::None) {
wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
printf("Failed to execute motion. Error=%d (%s)\n", err, errString);
goto exit;
}
//Block execution until motion is finished
err = wmxlib_cm.motion->Wait(0);
//Execute motion to position 10000
pos.target = 10000;
err = wmxlib_cm.motion->StartPos(&pos);
if (err != ErrorCode::None) {
wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
printf("Failed to execute motion. Error=%d (%s)\n", err, errString);
goto exit;
}
//Block execution until motion is finished
err = wmxlib_cm.motion->Wait(0);
The position and velocity plots of the axis during this motion are shown below.
