Pages: [1]   Go Down
Author Topic: MPU-6050, Attempting to understand Krodal's code  (Read 1071 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm quite new to electronics in general / arduino and I am trying to implement a simple set of code to just read the raw values of the MPU-6050 (specifically a GY-521). I want to output them as an array or something similar to be used to adjust motor speeds of a quadcopter

I am trying to understand Krodal's code and removing any part I don't require (it seems to have lots of definitions) and was wondering if anyone could give me a run down.

I have started by just coding the read function but I do not know what the part involving unit8_t*buffer does and which part of this code actually outputs the values (surely the return function should return the buffer not 0?).

I have also removed the if statements as they seem overcautious, is this wise or should they be included?

Thanks heaps
Hayden


code so far:
//function to read MPU data
int readMPUData(register, UNKNOWN BIT , int size)
{
  Wire.beginTransmission(address); //start sequence
  Wire.write(register); //sends I2C address
  Wire.endTransmission(false); //holds I2C for restart

Wire.requestFrom(address, size, true); //requests data then releases I2C line
int i=0;
while(Wire.available())
{
int buffer[i++]=Wire.read()
}
return(buffer)
}
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This feels weird.
I tried to make a single sketch to get started fast in an easy way.
But I do want to return any error that could occur.
A few (hundred) more or less defines doesn't hurt. Why not let them stay ?

You can read the datasheets and make the same code as I did.
You could even remove all the error checking.
But why would you want to do that ?

http://playground.arduino.cc/Main/MPU-6050

Some explaining:
The read function should return the read values and also return an error.
The error is in the return variable.
The one or more bytes that are read are placed in a memory location.
That memory location is pointed to by 'buffer'.
The 'uint8_t *' indicated that the buffer points to a memory location for unsigned 8-bit bytes.

So you declare an array: uint8_t buffer[20];
You give the read function a pointer to it : buffer
And the read function fills the buffer.
« Last Edit: February 04, 2013, 10:34:48 am by Krodal » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry but I don't follow the unit_8 bit,

working from the top down, I assume that all the definitions are just straight from the register map (defining all the variables in the first table?). then you have this function which I have never seen before:
// Declaring an union for the registers and the axis values.
// The byte order does not match the byte order of
// the compiler and AVR chip.
// The AVR chip (on the Arduino board) has the Low Byte
// at the lower address.
// But the MPU-6050 has a different order: High Byte at
// lower address, so that has to be corrected.
// The register part "reg" is only used internally,
// and are swapped in code.
typedef union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct
  {
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
  } value;
};

unfortunately the only bit of code i understand is the integer variables. I also do not know what the byte order is.

If you get bored of my VERY lacking knowledge I would understand as this may take a while to fully get.

Thanks again
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I managed to find into on uint8_t and its just basically a byte right? but I still don't know how struct works, its like an array right?
Logged

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2757
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you get a book about elementary C++ programming.

We are happy to help explain difficult and tricky problems to you,   and help you to solve them,
but if you expect people to sit here and manually transcribe
pages and pages of elementary explanations for you,  because you don't know how to use google or wikipedia, or
read any of the multiple online tutorials, which are easily located.
And if you are unable to read,  try youtube.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am more than happy to look for resources, however a google of the struct command did not yield anything overly useful, though thank you for pointing out that it is a C command not an arduino one. I should be able to find it now (was looking it up with arduino included in the search). I did not know that arduino used C++ I was under the impression that is used C?

to krodal: I think I understand what you were saying before, however does filling the buffer inside a function make the new values global variables?
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The "uint8_t" is a "byte". It is a variable of 8 bits without sign. So you may also call it an "unsigned char".
The "uint8_t" is from the AVR GCC compiler and it looks ugly, perhaps I should change them into "byte".

You have to step up on your programming knownledge.
My MPU-6050 example is not a good way to learn 'C' or 'C++'.
So perhaps you should start with the Led Blink example, and try to make functions, and global and local variables and so on. Use functions with return value and a few parameters, and ... use functions with parameters that are pointers to variables.
If you use a single day for that, you should be able to learn the basics.

The Arduino uses C++, but also 'C'. You can use both and mix it in every way you like.

An 'union' is a definition of two or more variables that share the same memory location. This way it is possible to make an 'alias' for a variable. In my code I use two structures.
For example: "reg.x_accel_h" together with "reg.x_accel_l" is the same as "value.x_accel". I did this to make the code easier to read (easier for myself at least).

The 'read' function reads values from the sensor and fills the location of 'buffer' with data. That function doesn't know if the buffer is local or global, it only writes the data to the location the buffer is pointing to.
You may declare that buffer local or global, whatever seems best to you.
« Last Edit: February 05, 2013, 09:30:14 am by Krodal » Logged

Pages: [1]   Go Up
Jump to: