Giter Site home page Giter Site logo

simulink-arduino-serial's Introduction

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. ๐Ÿ˜Š

simulink-arduino-serial's People

Contributors

leomariga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

simulink-arduino-serial's Issues

Implementation of Simulink-Arduino-Serial with Simulink Desktop Real-Time

Hi there,

Work done in this repository is commendable and useful for real-world applications

I am exploring the feasibility of incorporating the data acquisition method proposed in this repository through Simulink Desktop Real-Time. The current approach needs to be revised for higher data rates, such as 1000 Samples/Second.

Please see the thread below:

https://in.mathworks.com/support/search.html/answers/1436729-serial-input-from-com-device-via-stream-input-block-in-simulink-desktop-real-time-external-sldrt.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:slrealtime/development-computer-setup&page=1

Get Float returns inf

I have tried your code to control a stepper motor through matlab, but I have a problem where the motor keeps running away all the time turns out the getFloat() keeps returning a inf number when not connected to anything. Im not sure where my problem lies here are the code that I used to recreate the problem.

`// Create a union to easily convert float to byte
typedef union{
float number;
uint8_t bytes[4];
} FLOATUNION_t;

// Create the variables you want to receive
FLOATUNION_t myValue1;
FLOATUNION_t myValue2;
FLOATUNION_t myValue3;
FLOATUNION_t myValue4;
FLOATUNION_t myValue5;
FLOATUNION_t myValue6;

// Create the variables to send
FLOATUNION_t send1;
FLOATUNION_t send2;
FLOATUNION_t send3;

void setup() {
// initialize serial, use the same boudrate in the Simulink Config block
Serial1.begin(115200);

}
void loop(){
// Get the floats from serial
myValue1.number = getFloat(); // Give your float a value
myValue2.number = getFloat(); // Give your float a value
myValue3.number = getFloat(); // Give your float a value
myValue4.number = getFloat(); // Give your float a value
myValue5.number = getFloat(); // Give your float a value
myValue6.number = getFloat(); // Give your float a value

// Do whatever you want here
Serial1.println(myValue1.number);
Serial1.println("GAE");
//Serial1.println(myValue1);

// Send some variables back
send1.number = myValue1.number+myValue2.number;
send2.number = myValue3.number+myValue4.number;
send3.number = myValue5.number+myValue6.number;

// Print header: Important to avoid sync errors!
Serial1.write('A');

// Print float data
for (int i=0; i<4; i++){
Serial1.write(send1.bytes[i]);
}
for (int i=0; i<4; i++){
Serial1.write(send2.bytes[i]);
}
for (int i=0; i<4; i++){
Serial1.write(send3.bytes[i]);
}
// Print terminator
Serial1.print('\n');

// Use the same delay in the Serial Receive block
delay(50);
}

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

Serial communication problem

when I try to use this code with an arduino which has servo motor connected to it, I got servo motor turning randomly when I try to start or close the simulation!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.