LSS arduino code to matlab code

hi, i write an arduino code using LSS library to control lynxmotion motors. i need to know how can i convert this code to matlab ?

1 Like

Hi @khaled_9797,

Welcome to the RobotShop community!

I am not aware of any simple way to convert Arduino code to MATLAB. As mentioned here, your best bet is most likely to install the Arduino support package for MATLAB and redo your code in there.

Alternatively, you could code in MATLAB natively and simply control your LSS as a generic serial port (see MATLAB serial devices info here). The LSS bus is effectively a TTL UART interface and if you are using a LSS Adapter Board then you get a convenient serial port (COM port under Windows) by connecting it by USB to your computer.

Whatever you end up choosing, youā€™ll most likely have to redo your code in some form that is compatible with MATLAB.

Good luck with your project and let us know how it goes!

Sincerely,

1 Like

thanks for your fast reply.
I couldnā€™t find a way to declare the lss library on matlab so i will connect the adapter board using USB and try the matlab serial solution as you mentioned.
for the arduino code i need to update the position, velocity and acceleration command for the motor. i found the (move , moveRelative) to control the position so what about the velocity and acceleration ?

If you are able to use C++ as a module or something in matlab, then you could just copy code from the library itself. Though I think that is not something you can do, as far as I know.

You can find all the commands available on the protocol page (Wiki) right here. The page is getting a major update since weā€™ve just released a big update for the firmware recently (last week), so that it will be easier to read for the users and easier to update for us in a consistent manner, too! :slight_smile:

The full command list is here. So, you can change velocity using SD/CSD or SR/CSR and acceleration/deceleration with AA/CAA and AD/CAD.

I havenā€™t used matlab in yearsā€¦ but if I end up having access to a reasonably modern copy I may be tempted to make a module for it. To be seenā€¦ :open_mouth:

1 Like

thanks for these information

this will be great :blush:
I want to show you this idea:
https://petercorke.com/matlab/calling-c-from-matlab/
do you think it may help to convert the lss library into matlab code ?

1 Like

No problem! :slight_smile:

Just got my hands on a copy of MATLAB 2016b. Iā€™ll install it later this week and have a deeper look in the near future.

Well, that certainly is useful if you wish to access code created in C or C++. That being said, youā€™ll still need to code for COM PORT access under Windows to use with MATLAB. The interface specific code (UART via Arduino) in the LSS library is not the same at all that you would use for a computer running Windows. The rest of the code could be used most likely verbatim, though. You should only need to replace the UART code with COM PORT c++ code (there should be plenty of example of that online).

Sincerely,

1 Like

you want to say that after declaring the lss library on matlab it will need the usb connection between the motor and the arduino (with the adapter board) to send the matlab output to the motor ?
iā€™m not sure but i think that will be easier to write the code on matlab and then send the commands to the motors directly and this what i want to do. and this will be useful for anyone using these motors because matlab is very common in the robotics field.

1 Like

Yes, the code or modules required to access to the COM port in MATLAB (or in C++ code) will be different than what is used in the LSS library for Arduino to access the UART peripheral. Thatā€™s the main difference. May need some other small tweaks but that should be the main difference.

Thatā€™s what Iā€™ll be trying out too - making a native module(s) for MATLAB. Iā€™ve looked at other examples firstā€¦ Iā€™m quite rusty with MATLAB! :smiley:

1 Like

Thank you for your effort and i hope you will make this modules because it will help me and solve a big problem in my project so i will wait for it :slightly_smiling_face:

1 Like

Hey @khaled_9797!

Every project starts with an ideaā€¦ quickly followed by requirements! :slight_smile:
What kind of features would be required (according to you / your own needs) for this module to be helpful? Simply a collection of functions to initialize, send commands and read query results from a LSS bus (COM port)? Some extra function ā€œshortcutsā€ to do common commands/actions?

Let me know what you think. This should give me a starting point on what is needed.

Sincerely,

1 Like

thank you @scharette for you efforts to help me in this :slightly_smiling_face:
I want to control the position, velocity and acceleration to follow a specific trajectory (generated on matlab) and i need to read query result about these quantities.
All my work is done on matlab and i have the adapter board and arduino uno but controlling the motors via arduino IDE required to send huge amount of data from matlab to arduino and then send the sensors readings from arduino to matlab again to plot the results. so this will be difficult and may make many problems.

so if i could connect the adapter board to PC using USB cable and controlling the motors via matlab directly(so my results go directly to motor functions) this will be awesome and very helpful.

1 Like

Yeah, it is best to remove the middleman here (extra overhead!) and do it directly from MATLAB to the LSS Adapter Board!

I think I have a pretty good idea of what you may require, but weā€™ll see how it goes and post an update here once I have something useful!

Sincerely,

N.B.: If you want to know more about how the LSS, the LSS Adapter Board and Windows (or other similar non-real time OS) interact together with serial ports, check out this post I made earlier. It digs into the details of how the communication works and what can affect it. This will also apply to MATLAB, so it is most likely useful information.

1 Like

of course, thatā€™s what i hope to do :+1:

Thanks for your help, and i will wait for these updates :blush:

man you are great ! :grinning:
you always response by a very useful information.
i read this post and itā€™s very helpful to know more about the lss protocol but the serial communication details seam difficult to me and i always hope not to dig into these details. until you make the matlab - lss adapter board communication i will try to use the protocol command with the matlab result to make the command format and try to send it.

1 Like

Do you have a simple example project of how you intend to use MATLAB and the LSS together? If you prefer, you can share in private (direct message), too.

1 Like

Oh, as a tease, hereā€™s a test I ran quickly yesterday evening to get some motion:

% Create port and add terminator (\r)
s = serial('COM3', 'BaudRate', 115200);
s.Terminator = 'CR';
fopen(s);

% Send command
%cmd = sprintf('#%d%s', 254, 'MD');
cmd = sprintf('#%d%s%d', 254, 'MD', 900);
%cmd = sprintf('#%d%s%d%s%d', 254, 'D', 900, 'T', 3000);
fprintf(s, cmd);

% Close port
fclose(s);

Note: Since I am using 2016b, I do not have the newer serialport functions (from 2018+ or 2019 versions), so hopefully that is still useful for you.

1 Like

This is a great start :blush:
i made this for the query commands :
%%%%% Query command
% positionā€¦
Qmove = sprintf(ā€™#%d%sā€™, 3 , ā€˜QDā€™);
fprintf(s, Qmove);
getposition = fscanf(s , ā€˜*3QD%uā€™);

% velocityā€¦
Qspeed = sprintf(ā€™#%d%sā€™, 3 , ā€˜QSDā€™);
fprintf(s, Qspeed);
getspeed = fscanf(s , ā€˜*3QSD%uā€™);

please check these commands. and is QSD is what should i use to get a feedback for the current velocity at the run time (not the max) ?
and is the S modifier is used as this :
moveS = sprintf(ā€™#%d%s%d%s%dā€™, ID , ā€˜Dā€™, value , ā€˜SDā€™, speed); % 0.1 degree , 1 degree/second
and is itā€™s units is (deg/s) or (0.1deg/s) ?

itā€™s not recommended starting in R2019b and iā€™m using 2019a. and serial is working well :slightly_smiling_face:

1 Like

You should check the wiki page for the LSS protocol and see all the queries available. I recommend using QWD.
If you want to use QSD, your could use itā€™s third output type using #[id]QSD2\r.

Check the details on the SD modifier here. As you can read in the description, it is in degrees per second:

1 Like