danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Tue Dec 24, 2013 2:17 pm

Check the second github then.

The I2c function is not the same but it will read the register the same way.

Check the header file for the MPU6050.

Also check this site about MPU6050. http://www.i2cdevlib.com/docs/html/class_m_p_u6050.html

Daniel

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Tue Dec 24, 2013 3:29 pm

The C wrapper could read up to 31 registers in one shot. You just need to increase the number of register to read to include the temperature . But first set the range.

P.S. Be sure that your structure array is big enough to hold the data

andariel
Posts: 3
Joined: Mon Dec 23, 2013 8:20 pm

Re: Yet more MPU6050 demo code

Wed Dec 25, 2013 1:17 pm

Hi all!
Please can somebody help how can I change the code.
I need to change INT0 connected to GPIO27 to GPIO24 .
I have done layout and do not want to do redesign.
Thanks

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 11:10 am

I think the problem is the use of C
In fact,when I try to compile the example in C++,it work but when I try to compile the same command in C,the compilator say :
I2Cdev.h error:unknown type name "class"
MPU6050.h error:unknown type name "class"

I have a little study C++ and it seems to me that "classes" are specific to C++ language and not for C ...
I think that's the only problem and if i solve it,i would use the MPU6050 without any problem :)
But do you know how can I solve the problem ? ^^

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 3:07 pm

Hi megaguigui,

It took me an hour to do that little c program.


I imported stuff from my egg and A2d converter from my git hub.


this is a new github with C code to read the mpu6050. Please change the I2C address to correspond to your accelerometer.

https://github.com/danjperron/mpu6050Te ... ree/master

You will notice that I'm using only one command to read the mpu6050. I read them in block which is way faster.


Daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 6:42 pm

I have try you prog but it always say "Unable to read " and now I can't try to execut because I lost the execution command in geany,so i can't execut a prog ...

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 6:52 pm

The fail means that it didn't see the I2C slave device on the i2c bus.

Ok which I2c bus are you using.

this is for bus 1

What i2cdetect -y 1 gives you

Did you change the I2C address to 0x68 if you are using that address.

Did you compile like this

Code: Select all

gcc -o AccTest AccTest.c mpu6050.c I2CWrapper.c 
And run it with

Code: Select all

sudo ./AccTest
Last edited by danjperron on Thu Dec 26, 2013 7:10 pm, edited 2 times in total.

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 6:58 pm

When i launch i2cdetect,I found my MPU6050 on the 0x68 adress
But i don't use the command line for launch but the IDE Geany and I can't run a programm without run command,command I've delete and I don't refind them ...

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 7:07 pm

This is normal it is compile for the mpu6050 at 0x69.

change the line 55 in AccTest.c to

int I2C_Current_Slave_Adress=0x68;


P.S. I did specify to change the i2c slave address on first post.

Daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 7:14 pm

Yes,I have already change the adress.
But for the moment,I Can't run any program with out the C run command

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 7:45 pm

Ok since it seems that you are not able to compile, I change the github and put the mpu6050 i2c address to 0x68.

Just reload the github.

https://github.com/danjperron/mpu6050TestInC

daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 7:52 pm

I'm able to compile and build,but i can't run (execut) the program :mrgreen:
Didn't you use Geany,just for give me the run command ? ^^

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:06 pm

Never use geany, I even don't know what it is.

And I know C , C++, C#, python etc....

I never ran a raspberry PI in gui mode , except in VNC, since I don't have any monitor.

I do all my stuff directly from the console. I'm a old style programmer. If I do gui , I will use eclipse!

Mind you that I'm working a lot with visual studio and arduino GUI but this is another story.


So you need a terminal console and just compile with gcc. I can't image that you are not able to run stuff on console.

B.T.W. I have a small bug in the code . The accelerometer range is 16G and not 20G. I will change a.s.a.p.


Daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:12 pm

I dont know Linux before I begin this programm,so I prefer use a GUI ^^
What's the line to run a programm in a console ?
5thank you a lot for your programm and you work,and sorry for my english too) :lol: :lol:

AlexandervD
Posts: 17
Joined: Tue Oct 08, 2013 10:19 am

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:22 pm

(I was considering a new topic, but I guess my question fits well enough in this topic.)
I got all the examples to work, but I want to do something similar to demo_3D in PI3D, is there an easy way to get dmp to work in python, or do I basically have to start from scratch. I'm quite lost at the moment and very unfamiliar with c/c++ whatever is used for the demos.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:28 pm

Ok first open a console

1 - install git

Code: Select all

sudo apt-get install git
2 - get the github repository

Code: Select all

cd
git clone https://github.com/danjperron/mpu6050TestInC.git
3 - Go into the new folder

Code: Select all

cd mpu6050TestInC
4 - compile it

Code: Select all

gcc -o AccTest AccTest.c mpu6050.c I2CWrapper.c
5 - And run it

Code: Select all

sudo ./AccTest
B.T.W. I change the current git hub and fix the 16G range. I also create a loop that will refresh the value every .1 sec . Just hit ctrl-c to quit.

Daniel
Last edited by danjperron on Thu Dec 26, 2013 8:38 pm, edited 1 time in total.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:37 pm

Hi AlexandervD,

You could do the same thing in python. To access I2c use the smbus function.

Daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:39 pm

Ok,It's working but now,I must understand the programm,and apply all of that to my programm ^^
Just a question for the moment: what it is given by the programm rotation or acceleration,and how find the another information ? (It seems that it Rotation ^^)

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 8:57 pm

You will have to read the mpu6050 pdf specification to figure out which are the range you want for your acceleration et gyro rotation.

This is a simple program and this is the step it does,

1- Open an handle to i2c device at 0x68.
2- Check if the device is there.
3- Initialize the device with correct settings.
4- Read a block of registers and store accelerometer, temperature and gyro data.
5 - Print out the information from the GForceStruct Data.
6 - loop back to step 4

If a ctrl-c is detected , just exit from the loop and close i2c.

P.S. Instead of creating a function to read Accelerometer, temperature and gyro. it is way faster to read the device in block. The advantage is that you just specify the first register to read and how many byte.

The I2CWrapper is just a way to convert the ioctl function to a more lisible and simple method.

And the mpu6050 contains all information about the device.

B.T.W. the GForceStruct hold all the information with the function Get_Accel_Values(). The rotation info is also there but I didn't print them.
Daniel

AlexandervD
Posts: 17
Joined: Tue Oct 08, 2013 10:19 am

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:13 pm

Thanks for the reply Daniel. "Do the same thing" is a pretty broad concept, but I guess you mean that I have to look at the addresses and calculations used in the examples and find a way to copy them using smbus? (If so I'm already stuck, as I can't find an equivalent to writeBit, all I can find is writeByte or something like that.)

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:21 pm

Check this github,

I know it is not about the mpu6050 but it has a python code on how to implements smbus.

check AdTest.py. it is using smbus method. (read_byte_data, write_byte_dat, etc...)

https://github.com/danjperron/A2D_PIC_RPI.git


B.T.W. I2c minimum size is BYTE so write bit is not possible unless you read the byte , mask it , set it, and write the byte.

Daniel

Megaguigui
Posts: 33
Joined: Sun Dec 08, 2013 2:11 pm

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:26 pm

In my case,I just need to give to variable the value,can I do this like that:
Acceleration_top_bottom=(float) Data.Gx *20.0 / 32767,
Acceleration_left_right=(float) Data.Gy * 20.0 /32767,
Acceleration_front_back = (float) Data.Gz *20.0 /32767,
Temperature= (float) Data.Temperature /340.0 +36.53,
Rotation_yaw = (float) Data.Gyrox * 20.0 /32767,
Rotation_Pitch = (float) Data.Gyroy * 20.0 /32767,
Rotation_roulis = (float) Data.Gyroz * 20.0 / 32767,

?

AlexandervD
Posts: 17
Joined: Tue Oct 08, 2013 10:19 am

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:34 pm

Thanks again. Seems like I will have to spend plenty of time to get this to work then, as far as I understand MPU6050.setIntDMPEnabled() uses write bit for example. I'll take another look at it when I'm a bit less tired, plenty of stuff I see in the mpu/demo code goes way over my head at first glance.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:51 pm

Hi Megaguigui,

yes this will work.
Last edited by danjperron on Thu Dec 26, 2013 10:07 pm, edited 2 times in total.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more MPU6050 demo code

Thu Dec 26, 2013 9:59 pm

you could create your own write bit function.

There is 2 methods That I know. If the value are static you could mirror it and write once into the MPU6050. A lot of code work that way. They hold a mirror copy of the register , register that are not changing by the device alone. This way you could just change the bit and overwrite the register. No need to read before.


And of course the second method is to read the byte , set/clear the needed bit and overwrite the byte.


def write_bit_data(add,cmd,bit_channel,bit_value):

Daniel

Return to “Automation, sensing and robotics”