Read accelerometer from raspberry pi

4 visualizzazioni (ultimi 30 giorni)
Student88
Student88 il 12 Apr 2016
Commentato: Student88 il 22 Apr 2016
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 Commenti
Joachim Schlosser
Joachim Schlosser il 15 Apr 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.
Student88
Student88 il 22 Apr 2016
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

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Raspberry Pi Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by