While using the Zaber Motors Example Matlab code, the code does not run, or when it runs it does not stop.

7 views (last 30 days)
I am using the code provided by the zaber motor matlab library which is show below. This code throws errors that moveRelative does not exist. If I remove the lines
if ~axis.isHomed()
axis.home();
end
and instead say use the line
device.getAllAxes().home();
before axis is initialized the motor runs, and moveRelative is now a recogonized function. However the motor runs indefinitly and the push to stop button has to be used to stop the motor. I would like assistance figuring out why the motor will not stop running and potential fixes. Thank you!
import zaber.motion.ascii.Connection;
import zaber.motion.Units;
connection = Connection.openSerialPort('COM3');
try
connection.enableAlerts();
deviceList = connection.detectDevices();
fprintf('Found %d devices.\n', deviceList.length);
device = deviceList(1);
axis = device.getAxis(1);
if ~axis.isHomed()
axis.home();
end
% Move to 10mm
axis.moveAbsolute(10, Units.LENGTH_MILLIMETRES);
% Move by an additional 5mm
axis.moveRelative(5, Units.LENGTH_MILLIMETRES);
connection.close();
catch exception
connection.close();
rethrow(exception);
end

Answers (1)

Jack
Jack on 7 Mar 2025
Hey Samantha,
It looks like you're running into two separate issues with the Zaber motion control library:
  1. moveRelative is not recognized – This likely happens because axis is not properly initialized when you first try to use it.
  2. Motor runs indefinitely – This could be due to missing await calls that ensure motion commands complete before proceeding.
Potential Fixes1. Fixing moveRelative Not Found Error
Instead of calling home() on all axes before initializing axis, try:
  • Make sure device is detected properly.
  • Ensure the device is correctly homed before moving.
import zaber.motion.ascii.Connection;
import zaber.motion.Units;
connection = Connection.openSerialPort('COM3');
try
connection.enableAlerts();
deviceList = connection.detectDevices();
if deviceList.isEmpty()
error('No Zaber devices found. Check the connection.');
end
fprintf('Found %d devices.\n', deviceList.length);
device = deviceList(1);
axis = device.getAxis(1);
% Make sure the device is homed
if ~axis.isHomed()
axis.home().await(); % Ensure home completes before proceeding
end
% Move to 10mm
axis.moveAbsolute(10, Units.LENGTH_MILLIMETRES).await();
% Move by an additional 5mm
axis.moveRelative(5, Units.LENGTH_MILLIMETRES).await();
% Close connection after movement is complete
connection.close();
catch exception
connection.close();
rethrow(exception);
end
Why This Should Work
Ensures device is detected properly before proceeding.
Adds .await() after home(), moveAbsolute(), and moveRelative() to wait for each command to finish before the next one starts.
Closes the connection properly so the motor doesn't keep running in an undefined state.
This should prevent the motor from running indefinitely while also ensuring moveRelative works as expected.
If this helps, follow me so you can message me anytime with future MATLAB or Simulink questions! 🚀
  2 Comments
Samantha
Samantha on 17 Mar 2025
Hello, thank you for your comment! I tried your following suggestions and I am still having the constantly running error. Do you have any other debugging suggestions I could try? Thanks again!
Jack
Jack on 17 Mar 2025
Hey Samantha,
Here are a few additional debugging suggestions:
  1. Check Command CompletionAfter each .await() call, try printing or logging the result to see if the device confirms that the command finished. For example, check if the returned object from axis.home().await() indicates successful completion. This might reveal if the device is actually stuck waiting for a response.
  2. Insert a PauseSometimes the device needs a moment to settle between commands. Try adding a brief pause (e.g., pause(0.5)) after each await to see if that helps the commands complete properly.
  3. Query the Device StatusUse functions like axis.getPosition() or any available status query to verify that the movement has finished before sending the next command. This can confirm if the motor is actually in motion or if it's waiting for a completion signal.
  4. Firmware/Library CheckEnsure that your Zaber device firmware and the MATLAB library are up to date and compatible. Sometimes version mismatches can lead to unexpected behavior.
  5. Isolate CommandsTry running just one command sequence at a time (for example, only home or only moveAbsolute) to see if the problem is with a specific command. This helps narrow down whether one particular command is causing the indefinite motion.
  6. Check for Unintended LoopsMake sure that your code or the library isn’t inadvertently reissuing movement commands in a loop, which might make it seem like the motor never stops.
If this helps, please accept the answer and upvote it as well.

Sign in to comment.

Categories

Find more on Instrument Control Toolbox Supported Hardware in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!