LSS Smart Servo Query Code on MATLAB format incorrect

Hi,

I have been trying to program the LSS Smart servo motors directly from MATLAB and was successfully sending commands/ position/motion to the servo with the following code:

fprintf(s, sprintf(’#%d%s%d’, 1, ‘D’, 0)); %Position Motor1 to 0
fprintf(s, sprintf(’#%d%s%d’, 254, ‘LED’, 1)); % Turn LED to Red
fprintf(s, sprintf(’#%d%s%d’, 254, ‘CSD’, 30)); %Configure max speed to 30deg/sec

However, I have tried multiple attempts to request a query from the motor to check its Current but the error says format incorrect.

I have also tried the code provided in this thread

provided by khaled but still would not work.

Any help provided would be appreciated, this is a project for my final year dissertation. Thank you.

Hello @Zayn2211 and welcome to the RobotShop community,

Can you share the code you used to read the feedback? Also which MATLAB version are you using?

Perhaps this could help you:

https://www.mathworks.com/help/matlab/ref/serialport.readline.html

Regards

fscanf(s , ‘*3QSD%u’); % to check speed

My MATLAB version was 2020a, and I was using serial instead of serial port

Thanks

This is the entire code:

%% Create port and add terminator (\r)

s = serial('COM5', 'BaudRate', 115200);

s.Terminator = 'CR';

fopen(s);

%% Configuration

% Configure Max Speed to 30 deg/s

fprintf(s, sprintf('#%d%s%d', 254, 'CSD', 30));

%% Open gripper

fprintf(s, sprintf('#%d%s%d', 5, 'D', -155)); % 0 is closed , range to -500 to open

%% Query

fscanf(s , ‘*3QSD%u’); % to check speed

fscanf(s , ‘*3QSD%u’); % to check speed

I’m not sure what this is supposed to do.

According to MATLAB’s documentation:
https://www.mathworks.com/help/instrument/fscanf.html

This is the correct format:

A = fscanf(obj,'format') reads data and converts it according to format .

format is a C language conversion specification. Conversion specifications involve the % character and the conversion characters d, i, o, u, x, X, f, e, E, g, G, c, and s. Refer to the sscanf file I/O format specifications or a C manual for more information.

I think you should send something like:

%% Query
fprintf(s, sprintf(’#%d%s’, 3, ‘QSD’)); % Query session value for maximum speed
speedString = fscanf(s , ‘%s’); % to check speed

*I haven’t tested this, but it is what I believe it should be according to the documentation.
Also, the returned data would be a string similar to “*5QSD1800” and not just the speed value.

Keep in mind that the “QSD” command is to Query Speed in Degrees (Session value for maximum speed), for instantaneous speed you need to use “QSD2”, and as you mentioned “current” in your first post that would be “QC”.

I hope that helps!

1 Like

Thank you so much!!

It works perfectly fine!

Screenshot 2022-09-12 121034

Hope you have a great day ahead

1 Like