B4R Question rWire

derez

Expert
Licensed User
Longtime User
I see that B4R has rWire library.
Please explain how to use it in order to work like the following Arduino sketch :
B4X:
// Connect A4 to SDA and A5 to SCL,
// VCC to 3.3v , and Ground.

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;

double bax, bay, baz, bgx, bgy, bgz, gx, gy, gz ;
double fxg = 0 ;
double fyg = 0 ;
double fzg = 0 ;
double apitch , aroll, gpitch, groll, yaw ;
const float alfa = 0.7 ;
const float alfa1 = 1 - alfa ;

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);

}
void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
  AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
....

Thanks
 
Last edited:

derez

Expert
Licensed User
Longtime User
The following is what I understood, but it is not working of course
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public WM As WireMaster
    Public WS As WireSlave
    Dim bc As ByteConverter
    Dim data(14) As Byte
    Dim zero() As Byte = Array As Byte(0)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    WM.Initialize
   
    WS.Initialize(0x68,"NewData")
    WM.WriteTo(0x6B,zero)
    WS.SetDataForMaster(data)
    WM.RequestFrom(0x3B,14)   
End Sub

Sub NewData
    Dim buf(2) As Byte
    Dim dbl() As Double
    bc.ArrayCopy(data,0,buf,0,2)  ' trying to get the first variable AcX
    dbl = bc.DoublesFromBytes(buf)
    Log(dbl(0))
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public WM As WireMaster
    Dim bc As ByteConverter
    Dim zero() As Byte = Array As Byte(0)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    WM.Initialize

    WM.WriteTo(0x6B,zero)

    Dim data() As Byte = WM.RequestFrom(0x3B,14)
    Dim buf(2) As Byte
    Dim dbl(2) As Double
    For i = 0 To 6
        bc.ArrayCopy(data,i*2,buf,0,2)
        dbl = bc.DoublesFromBytes(buf)
        Log(dbl(0))
    Next
End Sub

But I get 7 "-0.0000"
Edit: With Arduino sketch I get:
AcX = 7128 | AcY = -364 | AcZ = -14992 | Tmp = 30.08 | GyX = -348 | GyY = 322 | GyZ = -100
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I think the commands like the following which set the board in a specific mode are not handled correctly in the above code, but I don't understand how to set it right.
B4X:
  Wire.beginTransmission(MPU);
Wire.write(0x6B);  // PWR_MGMT_1 register
Wire.write(0);     // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
...
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);

I get length of zero for data buffer which is the return of
B4X:
Dim data() As Byte = WM.RequestFrom(0x3B,14)
Log(data.Length)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private wire As WireMaster
   Private raf As RandomAccessFile
   Private const MPU As Int = 0x68
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   wire.Initialize
   wire.WriteTo(MPU, Array As Byte(0x6b, 0))
   AddLooper("Looper1")
End Sub

Sub Looper1
   wire.WriteTo(MPU, Array As Byte(0x3b))
   Dim b() As Byte = wire.RequestFrom(MPU, 14)
   If b.Length = 14 Then
     raf.Initialize(b, True)
     Dim acx As Double = raf.ReadDouble32(raf.CurrentPosition)
     Dim acy As Double = raf.ReadDouble32(raf.CurrentPosition)
     Log("acx: ", acx, ", acy: ", acy)
   Else
     Log("Missing data...")
   End If
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Data from Arduino sketch:
AcX = 908 | AcY = -10072 | AcZ = -12836 | Tmp = 26.88 | GyX = -329 | GyY = 287 | GyZ = -82
full scale for the accelerations is ~ 17000
Data from your code:
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The mpu sends 7*2 bytes for the 7 parameters.
Reading with the above code , the current position is advanced by 4 bytes for each line !
I have tried to change it to read int16 and Uint16 but the data is still inconsistent.
in the sketch the variables are int16_t.
The 7 pairs of bytes are:
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Thanks Erel.
Found the problem - The order of the bytes in each pair should be reversed !
Changed the true to false for the LittleEndian in raf initialization.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…