Simulink external mode and Arduino Mega 2560 - matlab

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

Related

MultiCore Programming on Raspberry Pi via Simulink

i am currently developing a model in simulink with three different main functions (let's call them A,B,C for now), where one of them is running at a different sample time as the other ones. However, I tried to simulate this system on the Raspberry Pi via external mode but got a lot of overruns and a high cpu load. Now, I am trying to split the model, so that for example functions A and B are executed on one core and function C is executed on another core.
For this, I used this article from Mathworks, but I think that you can't actually assign one task to a core but just specify the periodic execution. As a result I could reduce the cpu load to a maximum of 40% but still get a lot of overruns (imo, this also contradicts itself).
As a second approach, I tried this article but I think this is not possible for Raspberry Pis since I can not add and assign cores in the concurrent execution tab.
My goal is to assign each task to a core on the raspberry and see the cpu load on the raspberry pi.
Many thanks in advance!

How to enable multithreading in MATLAB?

Some of MATLAB's functions support multithreading and will make use of your multi-core architecture when available. Hence, I'm not referring to MATLAB support for parallel execution when you explicitly invoke it, e.g. using parfor.
In my code I'm running imregtform. My issue with using this function is that on one device (Win 8, x64, MATLAB 2014b) the function (called thousands of times) is maxing all my CPUs but whereas on another device (Win 7, x64, MATLAB 2014a) it is barely using half of my CPUs and only about 20%. Why is that? Is there a switch somewhere?
If tried some of the suggestions found in:
Checking if MATLAB is running in multithread mode and Matlab 2011a Use all Cores Available on 64 bit Linux?.
Any other suggestions?

MATLAB Support Package for Arduino® Hardware baud rate change

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!

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.

profiling, How to avoid FPU (hardware) in VS pro 2008 or 2012RC and use emulated FPU codes

I need to optimise some codes for Cortex-M3 processor which doesn't have FP unit. I'm completely new to domain of optimisation.anyways,I use VS 2012 Release Candidate for native compiling of codes on my pc(Intel Core i5, windows 7 as os)and then porting them to Cortex_M3.I tried to write my codes in a way that it uses as little as possible the floating point arithmetics.but I still have a few. so i know that when i embedd it in Cortex_M3, it will take advantage of emulated FPU codes instead (Software FPU). Since i'm not able to do profiling for cortex_m3, i did it on my PC using VS2012 (Instrumentation method) to verify which functions take more time and have to be more optimized.
I think that profiling results on my PC can be proportional to that of COrtex_M3 if i don't use FP unit of my PC.
Is there a keyword or way in Visual Studio (2008 pro. or 2012 RC) which allows me to skip the (hardware) FP unit?
your insights are very appreciated
Your PC is soooooo different to a Cortex M3 that optimisations performed there are unlikely to be of any relevance. Some of the differences:
PC can issue more than one instruction per cycle
PC has some billion of those cycles per second vs some tens of millions
PC likely has more cache than your M3 has RAM
As you observe - the floating point unit
The M3 is an embedded processor - if you can't profile in the traditional way either get a better toolset, so that you can, or do it by hand, by using the hardware timers in the device to time your functions. Or toggle some port pins and hang an oscilloscope off it - that's proper embedded :)
EDIT:
You can profile without an OS - higher-end embedded toolchains can instrument the code, run it and pull the results back for post-processing
There are other hardware timers than the watchdog. At the simplest level, write some functions to read the value before you perform some task, read the value afterwards, subtract the result and print it out. More complex schemes can also be done, logging many iterations and keeping track of statistics etc.
If you have a few port pins, just set one before the function(s) you want to profile, clear it when it completes.
With a 4 channel scope you can see the execution times (and when they happen relative to each other, which can be useful if one interrupts another) of 4 sections of code at a time. If you have more, get a logic analyser and you can do loads of them!
You can also see the jitter or variation in execution time which can be instructive. Try it on the libc trig functions as the angle varies, you'll see that at some angles the sin/cos functions (for example) take way longer to run than at other angles. This can be a significant problem in a real-time system.