-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSLPB_Test_5_IMU-magnetometer.ino
127 lines (95 loc) · 2.7 KB
/
TSLPB_Test_5_IMU-magnetometer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* IMU Test
* Temp, Acc_X, Acc_Y, Acc_Z, Gyr_X, Gyr_Y, Gyr_Z, Mag_X, Mag_Y, Mag_Z
*/
#include <Wire.h>
#define MPU9250_ADDRESS 0x69
#define MAG_ADDRESS 0x0C
void setup() {
Wire.begin();
Serial.begin(9600);
I2CwriteByte(MPU9250_ADDRESS,0x37,0x02); // Set by pass mode for the magnetometers
I2CwriteByte(MAG_ADDRESS,0x0A,0x16); // Request continuous magnetometer measurements in 16 bits
}
void loop() {
=
// ::: Magnetometer :::
// Read register Status 1 and wait for the DRDY: Data Ready
uint8_t ST1;
do
{
I2Cread(MAG_ADDRESS,0x02,1,&ST1);
}
while (!(ST1&0x01));
// Read magnetometer data
uint8_t Mag[7];
I2Cread(MAG_ADDRESS,0x03,7,Mag);
// Create 16 bits values from 8 bits data
// Magnetometer
int16_t mx=(Mag[1]<<8 | Mag[0]);
int16_t my=(Mag[3]<<8 | Mag[2]);
int16_t mz=(Mag[5]<<8 | Mag[4]);
/*
int16_t mx=-(Mag[3]<<8 | Mag[2]);
int16_t my=-(Mag[1]<<8 | Mag[0]);
int16_t mz=-(Mag[5]<<8 | Mag[4]);
*/
// real magnetometer NO declination adjust value in uT
//float conv_m=1;
float conv_m=0.1465; // convertion factor for +-4,800uT range 0.6uT???, 16bits>0.1465, 14bits>0.2929
float mx_r=(mx)*conv_m;
float my_r=(my)*conv_m;
float mz_r=(mz)*conv_m;
// Magnetometer
/*
Serial.print (mx+200,DEC);
Serial.print ("\t");
Serial.print (my-70,DEC);
Serial.print ("\t");
Serial.print (mz-700,DEC);
Serial.print ("\t");
*/
/*
Serial.print (mx,DEC);
Serial.print ("\t");
Serial.print (my,DEC);
Serial.print ("\t");
Serial.print (mz,DEC);
Serial.print ("\t");
*/
//Display the results (magnetic vector values are in micro-Tesla (uT))
Serial.print (mx_r,1);
Serial.print ("\t");
Serial.print (my_r,1);
Serial.print ("\t");
Serial.print (mz_r,1);
Serial.print ("\t");
// End of line
Serial.println("");
// delay(100);
}
//**************************I2CReadByte()
// This function read Nbytes bytes from I2C device at address Address.
// Put read bytes starting at register Register in the Data array.
void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data)
{
// Set register address
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.endTransmission();
// Read Nbytes
Wire.requestFrom(Address, Nbytes);
uint8_t index=0;
while (Wire.available())
Data[index++]=Wire.read();
}
//******************************I2CwriteByte()
//Write a byte (Data) in device (Address) at register (Register)
void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data)
{
// Set register address
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.write(Data);
Wire.endTransmission();
}