Using Arduino libraries in MATLAB
32 views (last 30 days)
Patrick Flanigan on 22 Oct 2018
I know this question has been asked before, but I'm very new to this and I don't understand the answers (like here and here and here and here), to be honest.
I have an Arduino Uno connected to a custom-made circuit board. The Arduino code for controlling it has already been written, and the necessary .h and .cpp files are all included and working. I want to see if I can operate the device with MATLAB, but I need to tell Arduino to include these extra libraries (one is for a real-time clock, one is for a pressure / temperature / humidity sensor, etc.). Is there are very simple explanation for how to do this? When I read this, I don't understand what parts I'm supposed to change for my specific case, what I need to keep, and what I can throw away.
Siddharth Bhutiya on 25 Oct 2018
As mentioned here, the add on library interface functions as a server client model.
You send a command using the "sendCommand" function from MATLAB and provide a commandID. This is sent to the Arduino which reads the commandID, runs the appropriate function from the C++ file and then uses the "sendResponseMsg" command to return any data that needs to be sent back.
The basic interface for these functionalities are provided in the arduinoio.LibraryBase class in MATLAB and librarybase.h in C++. In your add on you will build on top of these so that you can run your commands.
If we consider the simple HelloWorld example provided over here
First, we create a folder structure as mentioned in the link.
Second inside the C++ code we include LibraryBase.h and any other libraries used by our code. Then we create a class that inherits from the base class and inside the constructor we provide a name for the library and we register it so that the Arduino server knows that such a library exists.
After this, inside the command handler, we use switch case which will decide what piece of code will run when "sendCommand" sends a given commandID. So for each commandID you call the appropriate functions from your file and do whatever you want (get data from sensor, turn on a LED, etc.) This will complete the Arduino part of the add on.
After this, we set up the MATLAB interface that will provide the actual functions that we will call from MATLAB, to run our code on the Arduino.
To do this we create a class that inherits from the "arduinoio.LibraryBase" class. Inside it we define the commandIDs for each command, define any constants and specify the location of the header files.
The methods of this class will send a commandID to the arduino server, which in turn will run the appropriate function on the Arduino.
In the HelloWorld example, the read() function in HelloWorld class sends commandID 0x01 to the server. The server checks the commandID (inside the command handler) and then sends "Hello World" back to the read() function which in turn prints it.
The basic architecture for your code will be the same just the functions in the MATLAB Class will correspond to the functions that you want to execute and you will have to add the corresponding code inside the command handler.
Hope this helps.