Read accelerometer from raspberry pi

Hello there :)
I am facing problems with reading values from a Bosch BMA280 Accelerometer connected to a raspberry pi via Matlab.
What is working so far:
  • Raspberry Pi Support Package is installed
  • Raspi is addressable via Matlab
  • BMA280 Sensor is connected to Raspberry Pi via SPI 4-wire
  • I can read several register via writeRead-Function (SPI) in Matlab (for example Chip ID, X-, Y-, Z-Data)
What I want to do:
  • Use the BMA280 connected to the Raspi in Simulink
My problem is, I don't know where to start. I found device drivers on the bosch homepage in c-Code. Should I try to build my own device driver in matlab? Are there any better ways?
The other problem I am facing: The bosch sensor sends the X-, Y- and Z-Data in two's complement format. The matlab function "writeRead" (Raspberry support package) returns a row vector, which looks like this:
The technical documentation tells the user to first read the LSB-Register (here 0x04, which has to be 0x84 for reading values) and afterwards read the MSB-Register (0x05 -> 0x85). I don't know why I, but when I'm not including "hex2dec('0')", matlab reads only one register. Can anyone explain that to me? This would look like (0x84 should not be "0"!):
How do I convert the row vector returned by matlab to useful values?
Thanks so far.
Kind regards,
Niko

4 commentaires

Joachim Schlosser
Joachim Schlosser le 12 Avr 2016
You are doing good. What else would you need than reading and writing registers?
You probably found this already – The Raspberry Pi SPI doc for MATLAB is in the Online Doc on Raspberry Pi SPI in MATLAB, with a page on General Usage and the Function Reference.
Student88
Student88 le 13 Avr 2016
Modifié(e) : Student88 le 13 Avr 2016
Thanks for your reply :) I guess reading and writing the registers is the main deal. I am just unsure about the device driver from bosch, which is in c-code.
My idea now is the following: I found a way to develop device drivers for simulink: Simulink device driver
This one has a good documentation, so I am hoping to get a functional simulink block for reading the bma280 sensor. I will try to use the wiringpi library for the spi connection on the raspberry pi (as there is no spi block available in simulink).
---- new approach:
While reading through the documentation of "device drivers for simulink", I found out, that there are different ways to write a device driver for simulink:
  • S-Function (via S-Function Builder Block)
  • Legacy Code Tool
  • Matlab Function
  • System Object
As I need to be able to change the sample time of the device driver and would like to achieve a high reading frequency of the bma280 sensor (about 2000 Hz), I guess matlab function is the best way to start.
---- end of new approach :)
The next step would be to implement the given functions from the bosch device driver into that simulink device driver.
If you have any further suggestions, let me know :)
Thanks for the link by the way (and yes, I already found that:)) The online doc for the writeRead function tells the user to:
"To read data from SPI, send dummy values". Is that the reason, why I have to send hex2dec('0') when I only want to read some registers (see first picture above)?
Joachim Schlosser
Joachim Schlosser le 15 Avr 2016
Yes, if you want to write the code yourself, go MATLAB Function. If you just want to re-use existing C functions, use Legacy Code tool.
On writeread: yes, this is how the writeread function seems to be proposed by the SPI standard.
Hello there :)
Some news and new questions from me :D
I got the BMA280 Sensor to work with the Raspberry Pi (C Code). It returns correct acceleration values (in G).
Next step would be to implement this c code in a matlab function block. I've tested this with a LED before (running the code on the raspberry pi), and it worked.
But... It doesn't work with my c code (which is working perfec on the raspberry though).
First problem: Printf does not work the way I would expect it to work (printf command in c-code). At least Matlab only prints out the messages, when a printf-command in the matlab script is called. I found solutions on the internet using "ssprintf", but when I'm including "simstruc.h" in my c-code (which is needed for ssprintf), I'm getting errors saying:
In file included from ./SPI_Raspi.h:5:0, from ./SPI_Raspi.c:3: ./simstruc.h:1076:5: error: unknown type name ÔÇÿmxArrayÔÇÖ ./simstruc.h:1879:6: error: unknown type name ÔÇÿmxArrayÔÇÖ ./simstruc.h:1883:3: error: unknown type name ÔÇÿ_ResolveVarFcnÔÇÖ In file included from ./SPI_Raspi.h:5:0, from ./SPI_Raspi.c:3: ./simstruc.h:11996:5: error: unknown type name ÔÇÿmxArrayÔÇÖ _bma280_device_driver.mk:125: recipe for target 'linuxUDP.o' failed make: * [linuxUDP.o] Error 1
Is there any workaround or other way, to solve this?
My second problem is the following: The matlab code, using my c-code, looks like this:
% code
function [x, y, z]=BMA280()
persistent initflag;
x=0.0;
y=0.0;
z=0.0;
temp = 0;
if strcmp(coder.target, 'rtw'),
if isempty(initflag),
% Initialize step
temp = coder.ceval('bma280Init');
if (temp),
initflag = 1;
else
fprintf('Failed to initialise BMA280!\n');
% End function because of error
return;
end
else
% output Step
coder.ceval('updateAccelData', coder.wref(x), coder.ref(y), coder.ref(z));
%fprintf('X is %10.5f Y is %10.5f Z is %10.5f \n', x, y, z);
end
end
(sorry about formatting, I don't know how to insert code the right way here)
I guess the problem has got something to do with the arguments for the function "updateAccelData". This c-function needs pointers to float values, which contain the x, y and z accel data after calling the function. I found out, that I need to use "coder.ref" to pass the reference (pointer) to my c-function. But it does not work (as you can see, I also tried "coder.wref", which only offers write access, but that did not change anything). Is there a problem with the data type "float" in c, regarding matlab? By the way, the c-Function prints the correct values, when I am running my modell in simulink on external mode (raspberry). But Matlab does not contain anything in the values x, y and z.
The last question from my side would be: is there any way to debug matlab code, which uses my c-code, and runs on the raspberry pi? I couldn't find a solution until now.
Have a nice weekend,
best regards,
niko

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Raspberry Pi Hardware dans Centre d'aide et File Exchange

Question posée :

le 12 Avr 2016

Commenté :

le 22 Avr 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by