B4R Question rWire

derez

Expert
Licensed 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
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
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
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
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
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:
acx: ovf, acy: ovf
acx: 0.0000, acy: 7.6427
acx: -0.0000, acy: 0.0000
acx: 113958928, acy: 0.0000
acx: 0.0000, acy: 0.0000
acx: 0.0000, acy: ovf
acx: 6.7969, acy: 0.0000
acx: ovf, acy: 7.6305
acx: -0.0000, acy: ovf
acx: -0.0000, acy: 0.0000
acx: -0.0000, acy: 0.0000
acx: -113647632, acy: ovf
acx: -0.0000, acy: 0.0000
acx: ovf, acy: 0.0000
acx: ovf, acy: 7.6324
acx: 0.0000, acy: 7.6393
acx: 0.0000, acy: 0.0000
acx: 0.0000, acy: 7.6417
acx: ovf, acy: 7.6285
acx: 0.0000, acy: 0.0000
acx: -444096.0625, acy: ovf
acx: ovf, acy: 7.6397
acx: ovf, acy: ovf
acx: 0.0000, acy: 0.0000
acx: ovf, acy: 7.6349
acx: -0.0000, acy: 0.0000
acx: -442816.0625, acy: 0.0000
acx: ovf, acy: 0.0000
acx: ovf, acy: ovf
acx: ovf, acy: ovf
acx: -0.0000, acy: 7.6432
acx: -0.0000, acy: ovf
acx: -1733.6252, acy: 7.6344
acx: 0.0000, acy: ovf
acx: 0.0000, acy: 0.0000
acx: ovf, acy: 7.6412
acx: ovf, acy: 0.0000
acx: -0.0000, acy: 7.6412
acx: ovf, acy: 0.0000
acx: ovf, acy: ovf
acx: ovf, acy: 7.6358
acx: ovf, acy: 0.0000
acx: ovf, acy: 0.0000
...
acx: -0.0000, acy: 0.0000
acx: -0.0000, acy: 0.0000
acx: -6.7651, acy: 0.0000
acx: -1735.0002, acy: 0.0000
acx: ovf, acy: 7.6422
acx: -0.0000, acy: 0.0000
acx: 0.0000, acy: 7.6388
acx: -0.0000, acy: ovf
acx: 0.0000, acy: 0.0000
acx: ovf, acy: 7.6319
acx: -0.0264, acy: 0.0000
acx: 0.0000, acy: 7.6500
acx: 0.0000, acy: 0.0000
acx: 0.0000, acy: 7.6559
acx: ovf, acy: 7.6461
 
Upvote 0

derez

Expert
Licensed 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:
10 92, 218 132, 205 8, 243 224, 254 203, 1 68, 255 157

10 128, 218 108, 205 84, 243 208, 254 208, 1 68, 255 180

10 136, 218 108, 204 212, 244 0, 254 170, 1 107, 255 176

10 88, 218 252, 205 0, 243 224, 254 200, 1 65, 255 152

10 160, 218 44, 205 84, 243 224, 254 221, 1 67, 255 128

10 136, 218 44, 205 172, 244 0, 254 163, 1 71, 255 166

10 160, 218 184, 205 0, 243 224, 254 196, 1 57, 255 215

10 176, 218 164, 205 188, 243 208, 255 13, 1 85, 255 79

10 176, 218 156, 205 44, 243 224, 254 236, 1 90, 255 133

10 228, 219 52, 204 200, 243 240, 254 247, 1 49, 255 64
 
Last edited:
Upvote 0

derez

Expert
Licensed 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
Top