MATLAB Support Package for Arduino® Hardware baud rate change - matlab

MATLAB released a Support Package for Arduino® Hardware this September and I am using it with MATLAB r2014a. I am having problems concerning the speed of the communication between MATLAB and Arduino. Does anyone know how can I increase the baud rate? I tried changing it in the arduino.m file but I get the following error :
Updating server code on Arduino Uno (COM3). Please wait.
Error using arduino_real_time_plotter (line 1)
Internal error: The initialization of the server code is
incorrect.

According to Mathworks this is no longer possible in the new MATLAB Support Package for Arduino® Hardware!

Related

How do I force MATLAB to run deep learning code on the CPU instead of the GPU?

I don't have CUDA-enabled Nvidia GPU, and I want to force MATLAB to run the code on CPU instead of GPU (yes, I know, it will be very very slow). How can I do it?
As an example, let’s try to run this code on my PC without CUDA. Here is the error given by MATLAB:
There is a problem with the CUDA driver or with this GPU device. Be sure that you have a supported GPU and that the latest driver is installed.
Error in nnet.internal.cnn.SeriesNetwork/activations (line 48)
output = gpuArray(data);
Error in SeriesNetwork/activations (line 269)
YChannelFormat = predictNetwork.activations(X, layerID);
Error in DeepLearningImageClassificationExample (line 262)
trainingFeatures = activations(convnet, trainingSet, featureLayer, ...
Caused by:
The CUDA driver could not be loaded. The library name used was 'nvcuda.dll'. The error was:
The specified module could not be found.
With R2016a, the ConvNet "functionality requires the Parallel Computing Toolbox™ and a CUDA®-enabled NVIDIA® GPU with compute capability 3.0 or higher."
See: http://uk.mathworks.com/help/nnet/convolutional-neural-networks.html
The code example that you link to requires a GPU. As such the solution is very simple:
You need to use different code.
In your question it is not mentioned specifically what you are trying to achieve, so it is hard to say whether you would need to create something your self or will be able to pick up an exisiting solution, but this CPU vs GPU deep learning benchmark may be an inspiration.

MATLAB and Arudino, set registers (PWM)

I am using an Arduino MEGA with MATLAB for the first time to try and control a servo motor, the issue is that the default PWM frequency for the pin I am trying to use is at 976Hz, while I need it to be around 50Hz. I found code for changing the timer 0 B register that affects this frequency but only for use in the Arudino IDE.
So basically, I need to send the following command to the Arduino:
TCCR0B = TCCR0B & B11111000 | B00000101;
Does anyone know how to do this while using the Arduino MEGA through MATLAB? Is there not a way to have straight up Arudino code in the .m file through some function/syntax? Or is there another way to manipulate the TCCR0B register via MATLAB? I know I could do this on the Arduino IDE but I really need to use MATLAB for the rest of this project.
Thank you for your time.

Call Arduino SPI functions with MATLAB

I have successfully connected MATLAB with my Arduino. So far, I have only sent simple tasks to the Arduino such as digitalWrite and such.
The code I have been using is as follows:
%-- connect to the board
a = arduino('COM9')
%-- specify pin mode
a.pinMode(9,'output');
%-- write 0 (off) to pin 9
a.digitalWrite(9,0);
%-- dummy variable
on = false;
%-- simple loop to make LED flash 5 times
for m in 1:5
if on
a.digitalWrite(9,0); % turn LED off
on = false;
else
a.digitalWrite(9,1); % turn LED on
on = true;
end
%-- close session
delete(a)
Now that this basic test successfully passed, I wanted to get the SPI Arduino library to work with MATLAB. Is it possible to call a function from the Arduino SPI library in my MATLAB code? Specifically, I want to get SPI.begin(); and SPI.end(); to work from MATLAB, but a.SPI.begin() is not working. Is there some step I am missing?
To get the SPI library into an Arduino program, one must use #include <SPI.h>, but how can we make sure MATLAB knows all of the functions available in the SPI library? Hopefully it is not a problem that the Arduino SPI Library code is written in a different language than what MATLAB files are written in.
References:
MATLAB Support Package for Arduino (aka ArduinoIO Package)
Arduino SPI Library
The library "ArduinoIO" does not support SPI.
That library is just a serial port listener, and every matlab/arduino instruction send a code by serial, that is readed in the sketch on the arduino, translated in the corresponding arduino's instruction, and then executed.
You can create your own block that send some your-choise-spi-command, you'll also have to edit the arduino's sketch to execure the corrisponding SPI command. But you'll have to understand how the library works, change it's code, and so on.
It is way more faster (in execution speed, as serial comunication really is slow, and coding time) to code a "specialized" arduino sketch, that send back to Serial just the value you need, and then read serial and to pc-side computation.
To communicate with SPI device using Matlab support package you can use the following code:
a = arduino();
Spi_Device = spidev(a, 'D5'); % D5 is the pin number that you want to use for chip select
writeRead(Spi_Device,[hex2dec('00'), 100]); % 100 is the value that you want to send to the device
% When you done clear the spi object
clear Spi_Device
The Legacy MATLAB and Simulink Support for Arduino is no longer supported. I would recommend using the MATLAB Support Package for Arduino Hardware as that has built-in support for basic SPI communication.
There are getting started type of examples that comes with the support package and one of them shows how to use SPI.
Disclaimer: Even though I work for MathWorks, these posts are based on my experience with the software as a user. For actual Technical Support, please contact Mathworks' TS.

Simulink external mode and Arduino Mega 2560

I'm stuck with Simulink Arduino Target in MATLAB 2012a. Simulink can compile my model and download it to the hardware. It seems to work, but when I do have a scope in my simulation, activating the external mode (for tuning my model and tracing the values) causes Simulink to crash ("Segmentation Violation", but the most time it just hangs without any error). It crashes the same way if I disconnect the usb-cable during external mode.
I'm using the Arduino Mega 2560 R3 Hardware, Matlab 2012b on Win 7 64 bit and Win XP.
Any help is welcome, the matlab forum doesn't give any useful answer (at least so far).
I could reproduce the same error you've described in your post when using a sample time in the Analog Input block which was different to the sample time in the Simulation Settings pane. In other words, there is a good chance that one of the Arduino blocks has a different sample time to the global sample timing --- and this is an issue for the Arduino support package.
Normally (in xPC Target) you just get an error message if the sample time does not make sense (non multiple of the global or faster) but a slower multiple is not an issue. Here I've found that the input block needed the same sampling as the global model to work.
(Matlab 2012b, Win 7 x64, Arduino Mega 2560 R3, external mode)
Gergely

running old mex file on new matlab releases

I'm trying to run a program originally tested on Matlab 6.5 on a new release (R2009a)
The program uses some mex files, and I get the following error when trying to run it:
??? Invalid MEX-file '/normalizedCut/common_files/sparsifyc.mexglx':
normalizedCut/common_files/sparsifyc.mexglx: symbol mxGetIr, version
libmx.INTERNAL not defined in file libmx.so with link time reference.
(the code I'm trying to tun is Normalized cut by Shi & Malic, and can be found here:
http://www.cis.upenn.edu/~jshi/software/files/NcutClustering_7.zip)
If I try to run the code on the same system but Matlab 2007a it runs ok.
Is there some problem with backwards compatibility for 2009a?
Are there any flags somewhere in the system I can change to help it work?
When I googled it I saw some references to the LD_LIBRARY_PATH env variable, but what exactly should be added to it I could not find out.
Thanks,
Yair
The source code for those mex functions appears to be available in the "Image segmentation with normalized cuts" source on this page: http://www.cis.upenn.edu/~jshi/software/ (in the specific_NcutImage_files subdirectory in the unpacked .zip)
It's pretty common for there to be problems running mex functions with different versions of Matlab. The errors you're getting look like they're due to API changes in Matlab (though that surprises me a little). I've had the most trouble because of binary incompatibilities induced by changes in gcc. I'd suggest contacting Jiambo and asking him if he can build a new version or release the source.
Worst case, you could try re-implementing those mex functions. The normalized cut algorithm is pretty straightforward in Matlab (see the Shi and Malik paper). By the names of the mex functions, they look like they're mostly duplicating existing matlab functionality (matrix multiplication, matrix sparsification). There's a non-zero chance that if you re-implemented them as regular m-code functions they'd be faster anyway due to the multicore support that's been added to Matlab.