Home › Forums › Forums › Technical Support for BerryIMU › BerryIMU and Matlab
- This topic has 17 replies, 3 voices, and was last updated 5 years, 5 months ago by Sylvain.
- AuthorPosts
- February 21, 2017 at 4:53 am #6110nightpoisonParticipant
hello,
So I’m new to both Matlab and the Raspberry Pi. I’m attempting a project that uses both, is there any suggestions on how I can access the data that the berryIMU will generate from Matlab?
Michael
March 1, 2017 at 7:26 am #6124nightpoisonParticipantHi,
So im still having issues regarding matlab and the BerryIMU. But I’ve done some more research and narrowed it down to what I am specifically needing help with.
Matlab has the ability to directly connect to I2C devices by creating an object using the devices address. here is two functions that I can use to read data from the device read() & readRegister()
read(device, count, precision) –
device = object connection with the IMU
count = the number of elements I can expect
precision = specified as a string. Match the data precision to the size of the register on the device. Optional.readRegister(device, register, precision) –
device = object connection with the IMU
register = register #
Precision = specified as a string. Match the data precision to the size of the register on the device. Optional.can I use the LSM9DS0 to identify the registers on the IMU and then use readRegister to bring in the data? If that’s the case, which registers do I need to read in order to get the data I need.
for instance in regards to the write function to enable the gyro. I know I need to send 0b00001111 to CTRL_REG1_G. But the register access is a scaler, so I can’t use the register name such as in python. how are the registers numbered?
March 8, 2017 at 8:13 am #6133nightpoisonParticipantIts taken some time, but I’ve figured out how to read and write to the device. However, I would very much like some feedback as I’m getting static output values from my device. There isn’t any drift at all.
for the Gyroscope, could you supply a sample output for register’s 0x28, 0x29, 0x2A, 0x2B, 0x2C, & 0x2D. Before combining the values. I’m just looking for what the Gyro reads when you ready from those specific registers. For instance here is a method I’m currently using to read for the X value of the Gyro.
function [ gyr_combined ] = readGYRx(IMU)
% Reads data from IMU for x axis
gyr_low = readRegister(IMU, hex2dec(’28’));
gyr_high = readRegister(IMU, hex2dec(’29’));gyr_combined = bitor(gyr_low, gyr_high);
gyr_combined = bitshift(gyr_combined, 8);if gyr_combined >= 32768
gyr_combined = gyr_combined -65536;
endreturn
endno matter the angle of the sensor it always returns the same thing:
gyr_low = 92.00
gyr_high = 5.00first I’m sure these numbers should be moving, as even when a gyro is fixed, there should still be drift.
March 8, 2017 at 6:14 pm #6135Mark WilliamsKeymasteryou are right, even if it isn’t moving, the raw values should be changing.
Have you enabled the gyro at the start of the code?Mark --OzzMaker.com --
March 8, 2017 at 10:28 pm #6136nightpoisonParticipantFirst, thank you for helping. I do really appreciate it.
I believe I am enabling correctly, here is the write function
function [] = writeGYR(IMU, reg, val)
%GRYO Summary of this function goes herewriteRegister(IMU, reg, val)
end
and here is the script that I’m using. I used the Python code you supplied via git, and made changes where appropriate for Matlab.
IMU = i2cdev(rpi, ‘i2c-1’, ‘0x6A’);
G_GAIN = .07;
gyroXAngle = 0;
gyroYAngle = 0;
gyroZAngle = 0;%initialize Gyroscope
writeGYR(IMU, hex2dec(’20’), 00001111);
writeGYR(IMU, hex2dec(’21’), 00110000);%start timer
tic;while true
%read gyroscope valuess
GYRx = readGYRx(IMU);
GYRy = readGYRy(IMU);
GYRz = readGYRz(IMU);%calculate loop period(LP). time between reads
loopTime = toc;
tic;
LP = (loopTime/1000000)/(1000000*1);%convert gyro raw to degrees per second
rate_gyr_x = GYRx * G_GAIN;
rate_gyr_y = GYRy * G_GAIN;
rate_gyr_z = GYRz * G_GAIN;%Calculate angles from gyro
gyroXangle = gyroXAngle + (rate_gyr_x * LP);
gyroYangle = gyroYAngle + (rate_gyr_y * LP);
gyroZangle = gyroZAngle + (rate_gyr_z * LP);% fprintf(‘Gyro X angle: %5.2f Gyro Y angle: %5.2f Gyro Z angle %5.2f\n’, gyroXAngle, gyroYAngle, gyroZAngle);
pause(1);
end
March 8, 2017 at 10:51 pm #6137Mark WilliamsKeymasterI haven’t used Matlab before, so I am taking a guess here.
Maybe you need to convert the binary to decimalwriteGYR(IMU, hex2dec(’20’),bin2dec(‘00001111`));
writeGYR(IMU, hex2dec(’21’), bin2dec(‘00110000`));Mark --OzzMaker.com --
March 8, 2017 at 11:12 pm #6140nightpoisonParticipantbin2dec() doesn’t work. I get an error “Input must be a character vector”. I have a resource for matlab support.
I just want to clarify, in the Python code (im using that for reference) you supply a GNU binary literal for the value to enable correct? Looking through the data sheet it seems that that is what is needed.
Also, what is the type for the return value when I’m reading from the gyro for the high low registers? should they be decimal as I’m getting now?
I haven’t used Matlab before, so I am taking a guess here.
Maybe you need to convert the binary to decimalwriteGYR(IMU, hex2dec(’20’),bin2dec(‘00001111`));
writeGYR(IMU, hex2dec(’21’), bin2dec(‘00110000`));March 9, 2017 at 11:53 am #6141Mark WilliamsKeymasterWhen you pass 00110000, dis Matlab seeing this as a binary value or a decimal? E.g. one hundred and then thousand.
What about if you try a decimal value when enabling the sensor?
00001111 = 15
00110000 = 48writeGYR(IMU, hex2dec(’20’), 15);
writeGYR(IMU, hex2dec(’21’), 48 );One byte is read from High register
One byte is read from Low register
Then they are combined, into a 16 bit signed value, MSB for the sign and then 15 bits for the value.Mark --OzzMaker.com --
March 11, 2017 at 12:45 am #6145nightpoisonParticipantMark,
Thank you for clarifying. That helped me resolve and I am now getting what looks to be appropriate results for the individual register output. I found some errors, misleading information, in the Matlab documentation that was the source of my errors. Thank you. I will problem have more questions as I’m going. I’m running in to other issues. but I’m going to trouble shoot it for a bit first. Thank you again.
Michael
March 16, 2017 at 4:06 am #6151nightpoisonParticipantMark,
so I believe I have gotten the read functions to perform correctly. can you confirm the data I’m receiving makes sense?
after combining the low with the high, performing the bitwise or, shifting left 8 bits, and then finally adjusting by -655536, if the value is greater than 32768 here is the return values I’m receiving.
-30976
25856
29952
-28160
28672
-28928
26880
-32000
32000
21504
-30208
-26880
-24576
etcis this the results I should be expecting?
March 16, 2017 at 2:42 pm #6155Mark WilliamsKeymasterNo, the don’t look correct.
Can you show me what the raw values are for gyr_low and gyr_high. make sure the IMU isnt moving.
Mark --OzzMaker.com --
March 16, 2017 at 11:15 pm #6156nightpoisonParticipantmark,
here is the output I’m getting directly from the high lows.
ACC X——-ACC Y ACC Z Gyro X Gyro Y Gyro Z
low high—low high low high low high Low High Low High
240 255—-240 255 32 4 5 0 158 255 67 254
238 255—-246 255 16 5 113 0 1 255 97 254
244 255—-249 0 1 4 148 0 204 255 92 254
2 255—-252 0 43 5 84 0 154 255 85 254
240 255—-3 255 254 4 116 0 200 255 127 254
252 255—-241 255 27 5 179 0 10 255 67 254
253 255—-236 255 247 4 156 0 203 255 29 254
231 255—-250 255 21 5 100 0 207 255 37 254
250 255—-255 255 5 5 83 0 189 255 87 254
245 255—-252 255 5 5 243 0 174 255 63 254March 16, 2017 at 11:46 pm #6160nightpoisonParticipantACC X——- ACC Y—— ACC Z—— Gyro X—– Gyro Y——- Gyro Z
low high— low high— low high— low high— Low High—– Low High
240 255—- 240 255—- 32 4—— 5 0——— 158 255—— 67 254
238 255—- 246 255—- 16 5—— 113 0——— 1 255—— 97 254
244 255—- 249 0———–1 4—— 148 0——— 204 255—— 92 254
2 255———252 0——— 43 5—— 84 0——— 154 255—— 85 254
240 255—- 3 255——- 254 4—— 116 0——— 200 255—— 127 254
252 255—- 241 255—- 27 5—— 179 0——— 10 255—— 67 254
253 255—- 236 255—- 247 4—— 156 0——— 203 255—— 29 254
231 255—- 250 255—- 21 5—— 100 0——— 207 255—— 37 254
250 255—- 255 255—- 5 5—— 83 0——— 189 255—— 87 254
245 255—- 252 255—- 5 5—— 243 0——— 174 255—— 63 254March 16, 2017 at 11:48 pm #6163Mark WilliamsKeymasterthats fine… i can use what is above
Mark --OzzMaker.com --
March 16, 2017 at 11:58 pm #6168Mark WilliamsKeymasterHave a look at the attached.
I placed your raw values into Excel and then used excel to calculate the final value. The raw looks good to me.I think this is the problem;
gyr_combined = bitor(gyr_low, gyr_high); gyr_combined = bitshift(gyr_combined, 8);
You need to shift at the same time you do the bitor.
gyr_combined = bitor(gyr_low, bitshift(gyr_high, 8));
Attachments:
Mark --OzzMaker.com --
- AuthorPosts
- You must be logged in to reply to this topic.