MATLAB: Do I get an out of memory warning when running External mode with Arduino Uno

simulink

When running my model in External mode on the Arduino Uno, I am getting incorrect readings from my sensor and a warning in the Diagnostic Viewer that says:
Not enough memory on the target to process the packet: EXT_SELECT_SIGNALS
Why is this warning occurring and how do I get correct readings from my sensor?

Best Answer

This warning occurs because there is not enough memory on the target to properly execute the model in External mode. There are a number of factors that influence the memory usage on the Arduino Uno. Ultimately, the memory constraints of the Arduino are causing the issue with bad sensor readings.
Note that the Arduino Uno has 2KB of SRAM for data, stack, and heap combined:
External mode is using heap memory for logging signals. In the Diagnostic Viewer, we can see the amount of space that is taken for data when we generate code. For example:
AVR Memory Usage
----------------------------
Device: atmega328p
Program: 22464 bytes (68.6% Full)
(.text + .data + .bootloader)
*Data: 1319 bytes (64.4% Full)*
(.data + .bss + .noinit)
In this example, we see that there is < 1KB of SRAM left for stack and heap combined. The following are some rough numbers for how much memory External mode requires:
- Basic External mode connection without logging requires ~150 bytes
- Logging one double signal (with Duration = 2) requires an additional ~550 bytes
- Each additional double signal logged (with Duration = 2) requires another ~50 bytes
Therefore, it is easy to run into memory limits on the Uno in External mode. The available workarounds are to change the model to use less memory, or to upgrade to a microcontroller with more memory. For example, the Arduino Due has 96KB of SRAM.
If you decide to change the model, listed below are several factors that can influence the SRAM used. Note that you can monitor the code generation output to see the amount of memory being used for data, as shown above. Some of the factors listed will affect the heap memory used too.
1) Number of signals being logged: Increasing the number of Display and Scope blocks increases the amount of memory needed by the buffer on the Arduino that stores the data collected in External mode. Having too many signal logging blocks can therefore overload the memory on the Arduino.
2) Size of the External Mode buffer: This value determines how many samples are collected in the External mode buffer before transmitting them back to Simulink. Collecting more samples at a time can affect memory usage on the Arduino. The number of samples collected can be changed with "Duration" value from the Code menu, under External Mode Control Panel > Signal & Triggering > Duration. See this page for further details:
3) Choice of solver: Continuous solvers use more memory than discrete solvers. Additionally, there are several continuous solver options, which you view under Configuration Parameters > Solver. The higher order (i.e. more precise) solvers use more memory on the Arduino.
4) Complexity of the model: Increasing the complexity of the model uses more memory on the Arduino.
In summary, there are several strategies you can use to address the External Mode memory constraints, by referring to each of the points above. The following will likely have the greatest affect:
- Reduce the number of signals being logged at once
- If possible, use a discrete instead of a continuous solver. Note that this requires discretizing the model (for example, you must use a discrete PID controller instead of a continuous one)
- If you must use a continuous solver, choose the lowest order solver that meets your requirements. Note that the higher order solvers have higher numbers (e.g. ode8 is a higher order solver than ode5).
- Reduce the complexity of the model as much as possible
By implementing these suggestions, you maybe able to reduce the memory requirements of your model to fit under the constraints of the Arduino. If this is not possible, it is recommended to upgrade to a target with more memory.