1

I'm working on data acquisition from a sensor attached to the Arduino : MPU6050 using a model in Ssimulink. I have installed the hardware support in Simulink but I don't know how to start building the model. The model must read the data from the Arduino by deploying it into the Arduino.

My connection Arduino <=> MPU6050 is :

Arduino    MPU 9150
3.3V       VCC
GND        GND
A5         SCL
A4         SDA
DIGITAL 2  INT
GND        AD0

update : i have use a s-function but I get the following error: _The call to realtime_make_rtw_hook, during the after_make hook generated the following error: Could not download the model to the target hardware. Verify the connection between the computer and target hardware.

The build process will terminate as a result.

Caused by: Could not download the model to the target hardware. Verify the connection between the computer and target hardware._

But the target is definitely connected, and downloading others module (like led blinking ) with simulink is working fine. I've also tested this on a different PC with Windows 8 (64bits) with the same result. Can anyone give me a hint please?

3

I just made my GY-521(MPU6050) work with Simulink using s-function. I wish this would be helpful for you.

I am using an Arduino MEGA 2650 board. The sensor data is read in in real time. Note that currently only Arduino MEGA 2650, Arduino MEGA 2650 ADK, and Arduino DUE support the real time windows target. If you are using other types of board, this answer may not applied.

Your hardware connection looks correct. But if you have some troubles you can refer to mine for checking:

5v VCC
GND GND
SCL SCL(check the register map of your board)
SDA SDA(check the register map of your board)

Above four connections are adequate if you just read raw sensor data. INT is needed if you are going to read fused data. But I did not try that.

Next you may write a small program in Arduino IDE to ensure everything works. You can refer to the Arduino MPU6050 examples. The code written here will be used to construct your Simulink s-function later.

Now you can start to build your Simulink block using s-function builder. If you did not do this before, here is a good tutorial:

https://m.youtube.com/watch?v=_OLctOFjjYQ

In our case, you need to copy Wire.h, twi.h and their .c counterparts in your current working directory where your s-function resides.

And do not forget to rename generated .c file to .cpp, and add external "C" declaration. All of these are illustrated in above video tutorial.

After all of above are done, you turn your simulation mode to external, and run. You can view your sensor data in scope if everything goes right.


Source code and configuration used in my function: I include all headers in the Libraries panel of S-Function Builder,

#ifndef MATLAB_MEX_FILE
#define ARDUINO 100
#include <Arduino.h>
#include <Wire.h>
#include <Wire.cpp>
#include <twi.h>
#include <twi.c>
#define MPU 0x68
#endif

Wire.* and twi.* files are copied from Arduino IDE's directory. I put these files along with my S-Function block file.

The address of I2C is 0x68 here.

Next, the code for sensor outputs are placed in Outputs panel. And do not forget to add ports in Data Properties panel.

#ifndef MATLAB_MEX_FILE
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);

int raw = Wire.read() << 8;
raw = raw | Wire.read();
ax[0] = (float)raw / 8192.0f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
ay[0] = (float)raw / 8192.0f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
az[0] = (float)raw / 8192.0f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
temp[0] = (float)raw / 340.0f + 36.53f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
gx[0] = (float)raw / 131.0f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
gy[0] = (float)raw / 131.0f;

raw = Wire.read() << 8;
raw = raw | Wire.read();
gz[0] = (float)raw / 131.0f;
#endif

Finally, initialization and configuration are done in Discrete Update panel.

#ifndef MATLAB_MEX_FILE
if (xD[0] != 1) {
    Wire.begin();

    /* Do not sleep */
    Wire.beginTransmission(MPU);
    Wire.write(0x6B);
    Wire.write(0);
    Wire.endTransmission(true);

    /* Gyroscope full scale range: 250 degs/s */
    Wire.beginTransmission(MPU);
    Wire.write(0x1B);
    Wire.write(0);
    Wire.endTransmission(true);

    /* Accelerometer full scale range: 4g */
    Wire.beginTransmission(MPU);
    Wire.write(0x1C);
    Wire.write(8);
    Wire.endTransmission(true);

    xD[0] = 1;
}
#endif

Please refer to your board's datasheet for specific address and LSB scales. I am using GY-521 here.

  • Thanks a lot :) , i'm actually using an ARDUINO uno but it give an error when i try to deploy the s-function i will try with an ARDUINO MEGA but please can you send me the s-function that you have built to chfakht@gmail.com , it will be very helpful ;) – The Beast Feb 21 '15 at 1:21
  • @user3530803 Does that error look like "you should enable external mode in parameter configuration"? I had this kind of error before and went to MEGA then. You can supply your error message to your question, maybe someone can help you solve that using just UNO. I am on vacation these days, about 5 days later I will share the detail source code in my answer. – TheBusyTypist Feb 21 '15 at 14:44
  • Thanks , i have update the post : you can see the error in the now , have a good vacation i will be waiting you – The Beast Feb 22 '15 at 23:33
  • @user3530803 I just updated my answer with source code of s-function. – TheBusyTypist Feb 26 '15 at 4:52
  • thanks a lot , i have just done it with the s-function but this time with an ARDUINO MEGA ... please can you tell me for what kind of project you have used the s-function ... here is my email for further contact : chfakht@gmail.com – The Beast Feb 27 '15 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.