Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino MPU6050 #239

Closed
sgilani opened this issue Oct 25, 2015 · 9 comments
Closed

Arduino MPU6050 #239

sgilani opened this issue Oct 25, 2015 · 9 comments

Comments

@sgilani
Copy link

sgilani commented Oct 25, 2015

I get only back this when i run the Go Prog. on Windows, i checked the Adress with I2C Scanner for Adruino and i get back 0x68, some where the communication is broken.

Plattform Windows 8.1 & Adruino Mega 2560 and the RX/TX Light are On on the Board when i run "go run test_mpu.go", COM Port is set to 57600.

Connection: Adruino to MPU6050 (Not Sparkfun Version)

ADRUINO - 5V to MPU6050 VCC
ADRUINO - GND to MPU6050 GND
ADRUINO - SCL 21 to MPU6050 SCL
ADRUINO - SDA 20 to MPU6050 SDA

Any ideas?

Accelerometer {0 0 0}
Gyroscope {0 0 0}
Temperature 0

package main

import (
"fmt"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/i2c"
)

func main() {
gbot := gobot.NewGobot()

firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "COM4")
mpu6050 := i2c.NewMPU6050Driver(firmataAdaptor, "mpu6050")

work := func() {
gobot.Every(300*time.Millisecond, func() {
fmt.Println("Accelerometer", mpu6050.Accelerometer)
fmt.Println("Gyroscope", mpu6050.Gyroscope)
fmt.Println("Temperature", mpu6050.Temperature)
})
}

robot := gobot.NewRobot("mpu6050Bot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{mpu6050},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}

@sgilani
Copy link
Author

sgilani commented Oct 26, 2015

By the end i solved my problem in another way for get the values in go:

1.: Adruino Uploaded the MPU6050 RAW Example
2.: Done a conection with serial to get the values

package main

import (
serial "github.com/tarm/serial"
"log"

)

func main() {
conf := new(serial.Config)
conf.Name = "COM4"
conf.Baud = 57600
conf.ReadTimeout = 0

     sc, err := serial.OpenPort(conf)
     if err != nil {
             log.Fatal(err)
     }

     buf := make([]byte, 1024)

     for {
             n, err := sc.Read(buf)
             if err != nil {
                     log.Fatal(err)
             }
             log.Printf("%s\n", buf[:n])
     }

}

OUTPUT:

cX = 16824 | AcY = -164 | AcZ = -1284 | Tmp = 24.11 | GyX = -669 | GyY = -91 | GyZ = -150
AcX = 16744 | AcY = -120 | AcZ = -968 | Tmp = 24.01 | GyX = -686 | GyY = -74 | GyZ = -143

@sgilani
Copy link
Author

sgilani commented Oct 26, 2015

UPDATE:

In Release 8.2 the MPU6050 Driver is Broken. In Version 8.1 it works more or less, the Values are not refreshed, after removing in github.com/hybridgroup/gobot/platforms/i2c/mpu6050_driver.go this part:

// setSleepEnabled
if err = h.connection.I2cWrite([]byte{MPU6050_RA_PWR_MGMT_1,
    MPU6050_PWR1_SLEEP_BIT,
    0}); err != nil {
    return
}

All looks fine and i have all my values. Later i will make a diff between 8.2 and 8.1 to see what is broken and why.

Maybe for the moment it is helpfull for other users.

@deadprogram
Copy link
Member

Hi, @sgilani

Can you please try this code in substitute for the initialize method?

func (h *MPU6050Driver) initialize() (err error) {
    if err = h.connection.I2cStart(mpu6050Address); err != nil {
        return
    }

    // setClockSource
    if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_PWR_MGMT_1,
        MPU6050_PWR1_CLKSEL_BIT,
        MPU6050_PWR1_CLKSEL_LENGTH,
        MPU6050_CLOCK_PLL_XGYRO}); err != nil {
        return
    }

    // setFullScaleGyroRange
    if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_GYRO_CONFIG,
        MPU6050_GCONFIG_FS_SEL_BIT,
        MPU6050_GCONFIG_FS_SEL_LENGTH,
        MPU6050_GYRO_FS_250}); err != nil {
        return
    }

    // setFullScaleAccelRange
    if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_CONFIG,
        MPU6050_ACONFIG_AFS_SEL_BIT,
        MPU6050_ACONFIG_AFS_SEL_LENGTH,
        MPU6050_ACCEL_FS_2}); err != nil {
        return
    }

    return nil
}

Please let me know if that works for you!

@sgilani
Copy link
Author

sgilani commented Oct 26, 2015

HI deadprogramm,

thanks for help, this is the result after implentation, no values.

Accelerometer {0 0 0}
Gyroscope {0 0 0}
Temperature 0

@sgilani
Copy link
Author

sgilani commented Oct 27, 2015

Maybe also helpful for debuging:

This freezes the values:

h.adaptor().I2cWrite([]byte{MPU6050_RA_PWR_MGMT_1,
MPU6050_PWR1_SLEEP_BIT,
0})

but when i remove just MPU6050_RA_PWR_MGMT_1 so the part looks like this:

h.adaptor().I2cWrite([]byte{
MPU6050_PWR1_SLEEP_BIT,
0})

I get again refreshed values, but some are missing:

Accelerometer {20268 -120 14648}
Gyroscope {-5008 -747 0}
Temperature 0

Gyro part 3 and Temp are lost somewhere... really strange

@sgilani
Copy link
Author

sgilani commented Oct 27, 2015

Solved for v.8.1, change this

const MPU6050_PWR1_SLEEP_BIT = 6

to this

const MPU6050_PWR1_SLEEP_BIT = 0

then we can also use the setSleepEnabled part without troubles, to get the right values:

Accelerometer {16472 -680 1772}
Gyroscope {-3872 406 -13133}
Temperature 35
Accelerometer {17472 408 4456}
Gyroscope {-3888 -3898 -3417}
Temperature 33
Accelerometer {16836 -120 856}
Gyroscope {-3824 -625 238}
Temperature 35

@deadprogram
Copy link
Member

According to the MPU6050 docs I could find:

Get sleep mode status. Setting the SLEEP bit in the register puts the device into very low power sleep mode. In this mode, only the serial interface and internal registers remain active, allowing for a very low standby current. Clearing this bit puts the device back into normal mode. To save power, the individual standby selections for each of the gyros should be used if any gyro axis is not used by the application.

So perhaps, a better thing would be to add a new constant const MPU6050_PWR1_ENABLE_BIT = 0 and use that. I'd love a a PR that did that! :)

@sgilani
Copy link
Author

sgilani commented Oct 27, 2015

You are right and i like your idea :) i will change it and share it.

@deadprogram
Copy link
Member

Hi, @sgilani did you ever have a chance to work up a PR with this? :)

deadprogram added a commit that referenced this issue Feb 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants