MPU6050 sensor read with the Netduino VB - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

MPU6050 sensor read with the Netduino VB

NP2 N+2 MPU6050 VB.net Netduino Plus 2 Gyro senssor Accerlertion Sensor I2C Orientation wheelie

  • Please log in to reply
No replies to this topic

#1 MrGringoPy

MrGringoPy

    New Member

  • Members
  • Pip
  • 8 posts

Posted 01 July 2013 - 08:27 PM

Hi.

 

I am using the NP2 with MPU6050 to make a system for a wheelie controller for my motorbike. 

 

Below is the VB code for the MPU6050 sensor. 

 

If anyone has the code for Orientation or code that tells me the position of the IMU I would really love to have it.  If you have code with a different sensor, no problem, I will buy the sensor required. I think a 9DOF would of been better than my 6DOF. (I am new to this micro controller world)

 

If you code is in C# , no problem as I will use online code converter to get in to VB.net.

 

The code bellow is thanks to Johannes Paul Langer in Germany.  It was in C# and I converted it to VB.net and translated all the German parts to English.

 

If you want the C# version it is here at the bottom of there page: http://meineweltinme...m-netduino.html

 

Here is a link to VB.net code. http://www.laspalmer...oPlus2 Gyro 01/

It is the same code as I have pasted in this post.

 

I sure hope some one can pass me the code for Orientation as there is not much on the internet for Netduino.

 

Thanks and hope to see a lot more Code for the Netduino users posted here.  :)

 

 

Posted Image

 

 

File structure looks like this:

 

[color=#daa520;]I2C Hardware[/color] ___ [color=#0000cd;]I2CConnector.vb[/color]

 

[color=#daa520;]Sensor[/color] ________ [color=#0000cd;]AccelerationAndGyroData.vb[/color]

 

      l___  [color=#0000cd;]Module1.vb[/color]

 

          I___ [color=#0000cd;]MPU6050.vb[/color]

 

[color=#0000cd;]File:  Module1.vb[/color]

  •  
  • Imports Microsoft.SPOT
  • Imports NetduinoApplication1.ExampleAccelGyroSensor.Sensor
  • Namespace NetduinoApplication1
  •   Public Class Program
  •  
  •   Public Shared Sub Main()
  •   ' Initializing the Sensor
  •   Dim mpu6050 As New MPU6050()
  •  
  •   ' Creating object for gyro and accelerometer
  •   Dim senorResult As AccelerationAndGyroData = mpu6050.GetSensorData()
  •  
  •   While True
  •   ' retrieve data
  •   senorResult = mpu6050.GetSensorData()
  •  
  •   ' issue
  •   ' Here it prints Data to screen
  •   Debug.Print(senorResult.ToString())
  •  
  •   Thread.Sleep(100)
  •   End While
  •   End Sub
  •  
  •   End Class
  • End Namespace

 

[color=#0000cd;]File :  MPU6050.vb[/color]

 

  • Imports Microsoft.SPOT
  • Imports NetduinoApplication1.ExampleAccelGyroSensor.I2C_Hardware
  •  
  • Namespace ExampleAccelGyroSensor.Sensor
  •   Public Class MPU6050
  •   ' <summary>
  •   ' Classes for the connection via the I ² C bus
  •   ' </summary>
  •   Private _I2C As I2C_Connector
  •   ' <summary>
  •   ' Class with the initializing Appropriate addresses
  •   ' </summary>
  •   Public Sub New()
  •   Debug.Print("Initialize the accelerometer and gyro sensor MPU-6050")
  •   _I2C = New I2C_Connector(MPU6050_Registers.I2C_ADDRESS, 100)
  •  
  •   Initialize()
  •  
  •   ' test connection
  •   Debug.Print("testing the Connection:")
  •   ErrorStatus(_I2C.Read(New Byte() {MPU6050_Registers.WHO_AM_I}))
  •   Debug.Print("-----------------------------------------------------------------------")
  •   End Sub
  •   ' <summary>
  •   ' Initialize the sensor with the default address
  •   ' </summary>
  •   Public Sub Initialize()
  •   ' PWR_MGMT_1 = Power Management 1 (11111001)
  •   ' 0xF9 = Device Reset (1) Sleep (1) Cycle (1) nothing (1) temperature (1), set clock source (001)
  •   ErrorStatus(_I2C.Write(New Byte() {MPU6050_Registers.PWR_MGMT_1, &HF9}))
  •   ' FullScaleGyroRange
  •   ErrorStatus(_I2C.Write(New Byte() {MPU6050_Registers.GYRO_CONFIG, &HE7}))
  •   ' FullScaleAccelRange
  •   ErrorStatus(_I2C.Write(New Byte() {MPU6050_Registers.ACCEL_CONFIG, &HE7}))
  •   ' Exit sleep mode
  •   ErrorStatus(_I2C.Write(New Byte() {MPU6050_Registers.PWR_MGMT_1, &H1}))
  •   End Sub
  •   ' <summary>
  •   ' Returns an error status via Debug
  •   ' </summary>
  •   ' <param name="error"></param>
  •   Private Sub ErrorStatus(ByVal [error] As Object)
  •   If [error] IsNot Nothing Then
  •   If CInt([error]) = 0 Then
  •   Debug.Print("Status: Error")
  •   Else
  •   Debug.Print("Status: OK")
  •   End If
  •   End If
  •   End Sub
  •   ' <summary>
  •   ' Retrieves the data from the sensor from
  •   ' </summary>
  •   Public Function GetSensorData() As AccelerationAndGyroData
  •   Dim registerList As Byte() = New Byte(13) {}
  •  
  •   registerList(0) = MPU6050_Registers.ACCEL_XOUT_H
  •   registerList(1) = MPU6050_Registers.ACCEL_XOUT_L
  •   registerList(2) = MPU6050_Registers.ACCEL_YOUT_H
  •   registerList(3) = MPU6050_Registers.ACCEL_YOUT_L
  •   registerList(4) = MPU6050_Registers.ACCEL_ZOUT_H
  •   registerList(5) = MPU6050_Registers.ACCEL_ZOUT_L
  •   registerList(6) = MPU6050_Registers.TEMP_OUT_H
  •   registerList(7) = MPU6050_Registers.TEMP_OUT_L
  •   registerList(8) = MPU6050_Registers.GYRO_XOUT_H
  •   registerList(9) = MPU6050_Registers.GYRO_XOUT_L
  •   registerList(10) = MPU6050_Registers.GYRO_YOUT_H
  •   registerList(11) = MPU6050_Registers.GYRO_YOUT_L
  •   registerList(12) = MPU6050_Registers.GYRO_ZOUT_H
  •   registerList(13) = MPU6050_Registers.GYRO_ZOUT_L
  •  
  •   _I2C.Write(New Byte() {MPU6050_Registers.ACCEL_XOUT_H})
  •   _I2C.Read(registerList)
  •  
  •   Return New AccelerationAndGyroData(registerList)
  •   End Function
  •   End Class
  • End Namespace

 

File: [color=#0000cd;]AccelerationAndGyroData.vb[/color]

 

  • Namespace ExampleAccelGyroSensor.Sensor
  •  
  •   ' <summary>
  •   ' Object to evaluate the results and provide the measured values.
  •   ' </summary>
  •   Public Structure AccelerationAndGyroData
  •   ' <summary>
  •   ' X Axis of the acceleration sensor
  •   ' </summary>
  •   Public Acceleration_X As Integer
  •   ' <summary>
  •   ' Y Axis of the acceleration sensor
  •   ' </summary>
  •   Public Acceleration_Y As Integer
  •   ' <summary>
  •   ' Z  Axis of the acceleration sensor
  •   ' </summary>
  •   Public Acceleration_Z As Integer
  •   ' <summary>
  •   ' Temperatur Wert
  •   ' </summary>
  •   Public Temperatur As Integer
  •   ' <summary>
  •   ' X Axis of the gyroscope
  •   ' </summary>
  •   Public Gyro_X As Integer
  •   ' <summary>
  •   ' Y Axis of the gyroscope
  •   ' </summary>
  •   Public Gyro_Y As Integer
  •   ' <summary>
  •   ' Z Axis of the gyroscope
  •   ' </summary>
  •   Public Gyro_Z As Integer
  •   ' <summary>
  •   ' Creates the object with the results
  •   ' </summary>
  •   ' <param name="results"></param>
  •   Public Sub New(ByVal results As Byte())
  •   ' Result for the acceleration sensor put together by bit shifting
  •   Acceleration_X = (CInt(results(0)) << 8) Or results(1)
  •   Acceleration_Y = (CInt(results(2)) << 8) Or results(3)
  •   Acceleration_Z = (CInt(results(4)) << 8) Or results(5)
  •  
  •   ' Results for temperature (date not yet tested)
  •   Temperatur = (CInt(results(6)) << 8) Or results(7)
  •  
  •   ' Result for the gyroscope sensor put together by bit shifting
  •   Gyro_X = (CInt(results(8)) << 8) Or results(9)
  •   Gyro_Y = (CInt(results(10)) << 8) Or results(11)
  •   Gyro_Z = (CInt(results(12)) << 8) Or results(13)
  •   End Sub
  •  
  •   Private Property vbTab As String
  •  
  •   ' <summary>
  •   ' Override the ToString () method
  •   ' Are all values ??in a string
  •   ' </summary>
  •   ' <returns></returns>
  •   Public Overrides Function ToString() As String
  •   Return "Acceleration: X: " & GetProzent(Acceleration_X) & vbTab & "Y:" & GetProzent(Acceleration_Y) & vbTab & "Z:" & GetProzent(Acceleration_Z) & " " & vbTab & " Temp: " & GetProzent(Temperatur) & " " & vbTab & " Gyro: X: " & GetProzent(Gyro_X) & vbTab & "Y: " & GetProzent(Gyro_Y) & vbTab & "Z: " & GetProzent(Gyro_Z)
  •   End Function
  •   ' <summary>
  •   ' Returns the value as a percentage value in order to keep the number small.
  •   ' </summary>
  •   ' <param name="value"></param>
  •   ' <returns></returns>
  •   Private Function GetProzent(ByVal value As Integer) As String
  •   Dim d As Double = CDbl(value)
  •   Dim result As Double = System.Math.Round((d / 65535) * 100)
  •  
  •   Return GetToThreeStellingerWorth(result.ToString())
  •   End Function
  •   ' <summary>
  •   ' Makes the value of a three-digit number
  •   ' </summary>
  •   ' <param name="content"></param>
  •   ' <returns></returns>
  •   Private Function GetToThreeStellingerWorth(ByVal content As String) As String
  •   If content.Length = 1 Then
  •   Return "__" & content
  •   ElseIf content.Length = 2 Then
  •   Return "_" & content
  •   End If
  •  
  •   Return content
  •   End Function
  •   End Structure
  • End Namespace

 

File : [color=#0000cd;]I2CConnector.vb[/color]

 

  • Imports Microsoft.SPOT.Hardware
  • Imports Microsoft.SPOT
  •  
  • Namespace ExampleAccelGyroSensor.I2C_Hardware
  •   ''' <summary>
  •   '''Simple class to connect a module to the I ² C bus.
  •   ''' Is from an example in the forum of Netduino.com derived.
  •   ''' </summary>
  •   Public Class I2C_Connector
  •   ''' <summary>
  •   ''' Hold configurations
  •   ''' </summary>
  •   Private _Config As I2CDevice.Configuration
  •   ''' <summary>
  •   ''' Main class for the connection
  •   ''' </summary>
  •   Private _Device As I2CDevice
  •   ''' <summary>
  •   ''' Initializes the class
  •   ''' Configuration is created and
  •   ''' the corresponding class I2C device is initialized
  •   ''' </summary>
  •   ''' <param name="address"> address byte transferred from the module. </ param>
  •   ''' <param name="clockRateKHz"> clock rate in KHz set </ param>
  •   Public Sub New(ByVal address As Byte, ByVal clockRateKHz As Integer)
  •   ' Transfer the address and clock frequency
  •   _Config = New I2CDevice.Configuration(address, clockRateKHz)
  •   _Device = New I2CDevice(_Config)
  •   End Sub
  •   ''' <summary>
  •   ''' Sends the contents of the byte array to the hardware
  •   ''' </summary>
  •   ''' <param name="writeBuffer">Byte Array</param>
  •   Public Function Write(ByVal writeBuffer As Byte()) As Integer
  •   ' Byte array passed to the ertellen a Transaction
  •   Dim writeTransaction As I2CDevice.I2CTransaction() = New I2CDevice.I2CTransaction() {I2CDevice.CreateWriteTransaction(writeBuffer)}
  •   ' Send the data to the hardware. Timeout at 1 second
  •   Dim written As Integer = Me._Device.Execute(writeTransaction, 1000)
  •  
  •   ' Check if all data have been sent otherwise perform exception  
  •   If written <> writeBuffer.Length Then
  •   Debug.Print("Data can not be sent to the module.")
  •   End If
  •  
  •   Return written
  •   End Function
  •   ''' <summary>
  •   ''' Gets the addresses in the buffer, the values ??from
  •   ''' </summary>
  •   ''' <param name="read Buffer"> byte array containing addresses for the retrieval of relevant data </ param>
  •   Public Function Read(ByVal readBuffer As Byte()) As Integer
  •   ' Create a transaction for reading with handover of the byte array  
  •   Dim readTransaction As I2CDevice.I2CTransaction() = New I2CDevice.I2CTransaction() {I2CDevice.CreateReadTransaction(readBuffer)}
  •   ' Reading the data from the hardware. Timeout of one second    
  •   Dim read__1 As Integer = Me._Device.Execute(readTransaction, 1000)
  •  
  •   ' Check if the data were sent  
  •   If read__1 <> readBuffer.Length Then
  •   'throw new Exception("It could not be read from the module.");
  •   Debug.Print("It could not be read from the module.")
  •   End If
  •  
  •   Return read__1
  •   End Function
  •   End Class
  • End Namespace






Also tagged with one or more of these keywords: NP2, N+2, MPU6050, VB.net, Netduino Plus 2, Gyro senssor, Accerlertion Sensor, I2C, Orientation, wheelie

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.