All Projects → leomariga → Simulink-Arduino-Serial

leomariga / Simulink-Arduino-Serial

Licence: other
How to connect Arduino and Simulink

Projects that are alternatives of or similar to Simulink-Arduino-Serial

ArduRTOS
Real Time Operating System Lessons using Arduino and The FreeRTOS Kernel
Stars: ✭ 46 (-14.81%)
Mutual labels:  arduino-boards, arduino-platform
Bits
A bite sized library for dealing with bytes.
Stars: ✭ 16 (-70.37%)
Mutual labels:  binary, bytes
KY-040-Encoder-Library---Arduino
Arduino library for the KY-040 Encoder
Stars: ✭ 37 (-31.48%)
Mutual labels:  arduino-boards, arduino-platform
Binarykit
💾🔍🧮 BinaryKit helps you to break down binary data into bits and bytes, easily access specific parts and write data to binary.
Stars: ✭ 92 (+70.37%)
Mutual labels:  binary, bytes
Arduino
🚀 Proyectos de todo tipo para arduino utilizando sus sensores y actuadores. 🤖
Stars: ✭ 27 (-50%)
Mutual labels:  arduino-boards, arduino-platform
Arduino Boards Index
Electronic Cats Package Lists for the Arduino Board Manager
Stars: ✭ 14 (-74.07%)
Mutual labels:  arduino-boards, arduino-platform
duino-coin
ᕲ Duino-Coin is a coin that can be mined with almost everything, including Arduino boards.
Stars: ✭ 626 (+1059.26%)
Mutual labels:  arduino-boards, arduino-platform
Bytearray.js
An equivalent to Actionscript 3's ByteArray for Javascript with AMF0 and AMF3 support.
Stars: ✭ 100 (+85.19%)
Mutual labels:  binary, bytes
SerialTransfer
Arduino library to transfer dynamic, packetized data fast and reliably via Serial, I2C, or SPI
Stars: ✭ 273 (+405.56%)
Mutual labels:  serial, bytes
interfax-ruby
Fax send and receive in Ruby with the InterFAX REST API
Stars: ✭ 16 (-70.37%)
Mutual labels:  receive, send
quartz-mailer
A library to get started in sending and receiving emails from and to your Crystal application
Stars: ✭ 23 (-57.41%)
Mutual labels:  receive, send
fixed-wing-sim
Matlab implementation to simulate the non-linear dynamics of a fixed-wing unmanned areal glider. Includes tools to calculate aerodynamic coefficients using a vortex lattice method implementation, and to extract longitudinal and lateral linear systems around the trimmed gliding state.
Stars: ✭ 72 (+33.33%)
Mutual labels:  simulink
Apex.Serialization
High performance contract-less binary serializer for .NET
Stars: ✭ 82 (+51.85%)
Mutual labels:  binary
muxnect
Send input to just about any interactive command-line tool through a local web server
Stars: ✭ 23 (-57.41%)
Mutual labels:  send
arduino
required (and optional) source files for the Arduino development environment, specifically the hardware/arduino sub-directory, to support xmega processors
Stars: ✭ 18 (-66.67%)
Mutual labels:  arduino-platform
Arduino-Beginners-EN
Code repository for the "Arduino for beginners" YouTube videos
Stars: ✭ 27 (-50%)
Mutual labels:  arduino-platform
BinaryStream
BinaryStream - a writer and reader for binary data. Best replacement for pack()/unpack().
Stars: ✭ 44 (-18.52%)
Mutual labels:  binary
nason
🗜 Ultra tiny serializer / encoder with plugin-support. Useful to build binary files containing images, strings, numbers and more!
Stars: ✭ 30 (-44.44%)
Mutual labels:  binary
gba-remote-play
Stream Raspberry Pi games to a GBA via Link Cable
Stars: ✭ 356 (+559.26%)
Mutual labels:  serial
gen-cisco
🧨 Generates Cisco scripts based on YAML files
Stars: ✭ 29 (-46.3%)
Mutual labels:  serial

cover

Simulink and Arduino connection

Welcome! You are here because you are trying to connect your Arduino board to Simulink to transmit some data (maybe some sensor output) but everything is going wrong and your world is falling apart (drama).

In this tutorial, we are NOT using the official Simulink Arduino Package, which enables you to access directly Arduino pins and many other features. What's the problem with this library? It uses a specific Arduino code, so you can't customize the code inside the board (if you want to use the Arduino as a control device for a "Hardware in the loop" project, as example).

If you don't want to read everything, just select what you want to learn:

Topics

The solution

To overcome the problem we can send and receive data using the serial blocks from Simulink without downloading any extra packages:

Instrument Control Toolbox

Using this blocks you can send and receive bytes in Arduino and interpret it as ASCII, floats, ints or whatever you want!! \o/.

In this example we are sending and receiving binary data, more specifically, an Arduino float (4 bytes) or Simulink single. Though, you can use this same code to send other types of variables.

Configuring your Serial

Well, first of all, you need to configure the Serial Port you are using for this communication. To do that, simply put the Serial Configuration block anywhere you want in your Simulink project.

configserial

Make sure your Arduino is connected to your computer and select its Communication port (COM15 in my case). If you don't know which communication port the Arduino is using, open your Arduino IDE > Tools > Ports.

Next, select the Baud rate to communicate with Arduino. Some standard values are 9600 and 115200. This is usually configured in the setup() function in your Arduino code. The other parameters in the block you can simply use the same as the image above or change it if you know what you are doing.

Sending data from Arduino to Simulink

IMPORTANT: You need to configure your serial port BEFORE sending or receiving data, so remember to use the configuration block before continuing.

Simulink setup - Receive

Use the Serial Receive block to receive serial data in your Simulink project. You need to configure this block to make the communication correctly. You can see how to configure this block in the figure below.

seriareceiveblock

Configuring your Serial Receive block:

  • Communication port: Use the same one you configured in the step above.
  • Header: Makes Simulink understand when the message is starting. This is not strictly necessary to your communication, but I highly recommend using it because avoids all types of synchronization issues (Simulink crashes after some time receiving data). In this example I used the byte 'A', but you can use whatever you want.
  • Terminator: Same as the header, but indicates the package end. I recommend using the end-line '\n'.
  • Data size: If you are sending only one float from Arduino, use [1 1] but you can change it to [1 2] or [1 N] where N is the number of float you are receiving from Serial.
  • Data type If you are sending float from Arduino, make sure to select single in Simulink, since both type of variables are intrinsically the same (floating point number with 4 bytes of length).

Remember: The Data type and Data size are correlated, so if you set Data size to [1 3] using Data type single Simulink will expect to receive 3 * 4 bytes = 12 bytes every step.

  • Enable blocking mode: Make sure to check this option.
  • Select Output last received value in the selection box.
  • Block sample time: Period in which the Arduino is sending data (20 Hz rate in my case).

Well done! You can now receive the Arduino data. The only step left is to convert it to the variable you want to work, in my case, a double.

Serial receive final block diagram:

seriareceivediagramblock

Arduino setup - Send

To send binary data from Arduino you have to convert your float to an array of bytes uint8_t. An interesting strategy is to use an union type like the code below:

typedef union{
  float number;
  uint8_t bytes[4];
} FLOATUNION_t;

FLOATUNION_t myValue;

To attribute a value for your float, you simply call:

myValue.number = 1.2;

To send this number just call Serial.write() for each myValue.bytes[i]. Remember to also send the header and the terminator. The code below illustrates this process:

Serial.write('A'); 

for (int i=0; i<4; i++){
	Serial.write(myValue.bytes[i]); 
}

Serial.print('\n');

You need to setup the Serial in the setup() function and create a loop with the same time delay as configured in Simulink. A simple example can be found here, where the Arduino sends a sinoid which is plotted in a scope.

seriareceivesignal

Sending data from Simulink to Arduino

IMPORTANT: As for the receive block, you need to configure your serial port BEFORE sending data, so remember to use the configuration block before continuing.

Simulink setup - Send

To send data from Simulink create a project similar to the image below.

serialsendsimulink

Block Function
Step Generate the signal you want to send.
Zero-Order Hold Set the simulation send rate.
Single Convert this signal to a single (4 bytes).
Byte Pack Convert single signal to byte. Use Byte alignment 4 and Data Type uint32.
Serial Send Send the bytes. You can add a Header and a Terminator if you want, but I had no problem sending this data without them.

Arduino Setup - Receive

To receive a float in Arduino, first use the same union as in Arduino Send code. Then, just create a function called getFloat() as follows:

float getFloat(){
    int cont = 0;
    FLOATUNION_t f;
    while (cont < 4 ){
        f.bytes[cont] = Serial.read() ;
        cont = cont +1;
    }
    return f.number;
}

Then, in your loop, just call

FLOATUNION_t myValue;

myValue.number = getFloat();

If you are sending more then one variable, remember to call getFloat() as many time as the number of floats you are sending. A receive example can be found here

Send and receive (Hardware in the loop)

Now you have both the send and receive blocks working you can join everything into a single project. The idea here is to create a simulation where the Arduino receive some data, process it, and sends back to Simulink. To do that, the Simulink block diagram is shown below:

sendreceive

The Arduino code consists in joining both receive and send codes. A fully working example of this code can be found here.. In this example, Simulink sends a signal which passes through Arduino and sends back to Simulink.

Array of floats

The blocks used in this simulation enables transmitting more then one variable in each simulation step. To do so, you have to make some minor adjustments in your simulation. The figure below shows this communication.

sendreceivemultiples

Notice that nothing has changed in the sender diagram. The block byte pack allow converting an array of float into an array of bytes. When the array arrives in the Serial Send, it concatenates the array in order and sends the bytes in the serial.

In the receiver diagram, the main change is in the Serial Receive block. You have to set the number of floats the Arduino is sending in order receive the data correctly. The figure bellow illustrates how to configure the block to receive 3 floats.

sendreceivemultiples

This example shows the Simulink transmitting 6 different values to Arduino, and receiving back 3 values (Sum of each pair). The Arduino code is right here..

outputmultiple

Conclusion

Hope I could help you in your work. If you have questions just create an issue so we can solve it together 😀!

[email protected]

Leonardo Mariga

As the legend says:

Struggle with your heart, not with your code.

Thank you!

Did I help you? Remember to click on 🌟 button. 😊

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].