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 ?
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,
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!
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…
thanks for these information
this will be great
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 ?
No problem!
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,
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.
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!
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
Hey @khaled_9797!
Every project starts with an idea… quickly followed by requirements!
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,
thank you @scharette for you efforts to help me in this
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.
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.
of course, that’s what i hope to do
Thanks for your help, and i will wait for these updates
man you are great !
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.
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.
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.
This is a great start
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
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: