Skip to content

Commit 7e956ae

Browse files
committed
1.1.0
Magnetometer measurement errors have been corrected.
1 parent d9685e1 commit 7e956ae

File tree

3 files changed

+71
-125
lines changed

3 files changed

+71
-125
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Deneyap 9-Eksen Ataletsel Olcum Birimi
2-
version=1.0.2
2+
version=1.1.0
33
author=Turkish Technnology Team Foundation (T3) <[email protected]>
44
maintainer=Turkish Technnology Team Foundation (T3) <[email protected]>
55
sentence=Arduino library for Deneyap 9 Dof IMU MMC5603NJ, LSM6DSM

src/Deneyap_9EksenAtaletselOlcumBirimi.cpp

Lines changed: 67 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
********************************************************************************
33
@file Deneyap_9EksenAtaletselOlcumBirimi.cpp
44
@mainpage Deneyap 9 Dof IMU MMC5603NJ Arduino library source file
5-
@maintainer RFtek Electronics <[email protected]>
6-
@version v1.0.0
7-
@date June 23, 2022
5+
@version v1.1.0
6+
@date Dec 03, 2025
87
@brief Includes functions to control Deneyap 9 Dof IMU MMC5603NJ
98
Arduino library
109
@@ -19,35 +18,28 @@ Library includes:
1918
#include <Wire.h>
2019

2120
/**
22-
* @brief I2C initialization and read check
23-
* @param adress: Device adress
24-
* @retval None
21+
* @brief I2C initialization
2522
**/
2623
bool MAGNETOMETER::begin(uint8_t address, TwoWire &wirePort) {
27-
_address = address >> 1;
24+
_address = address >> 1;
25+
2826
Wire.begin();
2927
Wire.beginTransmission(_address);
30-
if(!Wire.endTransmission())
31-
return true;
32-
return false;
28+
return (Wire.endTransmission() == 0);
3329
}
3430

3531
/**
36-
* @brief
37-
* @param
38-
* @retval
32+
* @brief Reset and Set procedure (Degaussing)
3933
**/
4034
void MAGNETOMETER::RegRead() {
4135
writeRegister(Control_Reg_0, 0x20);
42-
writeRegister(Control_Reg_0, 0x08); // SET
43-
writeRegister(Control_Reg_0, 0x10); // RESET
44-
writeRegister(Control_Reg_0, 0x01);
36+
delay(1);
37+
writeRegister(Control_Reg_0, 0x08);
38+
delay(1);
4539
}
4640

4741
/**
48-
* @brief
49-
* @param
50-
* @retval
42+
* @brief Write specific register
5143
**/
5244
uint8_t MAGNETOMETER::writeRegister(uint8_t address, uint8_t value) {
5345
Wire.beginTransmission(_address);
@@ -58,138 +50,91 @@ uint8_t MAGNETOMETER::writeRegister(uint8_t address, uint8_t value) {
5850
}
5951

6052
/**
61-
* @brief
62-
* @param
63-
* @retval
53+
* @brief Read multiple registers
6454
**/
6555
uint8_t MAGNETOMETER::readRegisters(uint8_t address, uint8_t *data, size_t length) {
6656
Wire.beginTransmission(_address);
6757
Wire.write(address);
68-
69-
if (Wire.endTransmission(false) != 0) {
70-
return 0;
71-
}
72-
73-
if (Wire.requestFrom(_address, length) != length) {
74-
return 0;
75-
}
76-
58+
if (Wire.endTransmission(false) != 0) return 0;
59+
if (Wire.requestFrom(_address, length) != length) return 0;
7760
for (size_t i = 0; i < length; i++) {
7861
*data++ = Wire.read();
7962
}
63+
return 1;
64+
}
8065

81-
return 0;
66+
/**
67+
* @brief Helper function to trigger measurement
68+
**/
69+
void MAGNETOMETER::triggerMeasurement() {
70+
writeRegister(0x1B, 0x01);
71+
delay(10);
8272
}
8373

8474
/**
85-
* @brief
86-
* @param
87-
* @retval
75+
* @brief Read X Axis
8876
**/
8977
int MAGNETOMETER::readMagnetometerX() {
90-
int x = 0;
91-
long I2C_ValueX = 0;
78+
triggerMeasurement();
79+
9280
byte data[9];
93-
readRegisters(0x00, (uint8_t *)data, sizeof(data));
94-
for (int i = 7; i >= 0; i--) {
95-
I2C_ValueX += pow(2, 12 + i) * bitRead(data[0], i);
96-
}
97-
for (int i = 7; i >= 0; i--) {
98-
I2C_ValueX += pow(2, 4 + i) * bitRead(data[1], i);
99-
}
100-
for (int i = 7; i >= 4; i--) {
101-
I2C_ValueX += pow(2, i) * bitRead(data[6], i);
102-
}
103-
x = (I2C_ValueX - 524288) * 0.00625;
104-
return x;
81+
if(readRegisters(0x00, (uint8_t *)data, 9) == 0) return 0;
82+
83+
unsigned long rawValue = ((unsigned long)data[0] << 12) | ((unsigned long)data[1] << 4) | ((unsigned long)data[6] >> 4);
84+
85+
float result = ((long)rawValue - 524288) * 0.00625;
86+
return (int)result;
10587
}
10688

10789
/**
108-
* @brief
109-
* @param
110-
* @retval
90+
* @brief Read Y Axis
11191
**/
11292
int MAGNETOMETER::readMagnetometerY() {
113-
int y = 0;
114-
long I2C_ValueY = 0;
93+
triggerMeasurement();
94+
11595
byte data[9];
116-
readRegisters(0x00, (uint8_t *)data, sizeof(data));
117-
for (int i = 7; i >= 0; i--) {
118-
I2C_ValueY += pow(2, 12 + i) * bitRead(data[2], i);
119-
}
120-
for (int i = 7; i >= 0; i--) {
121-
I2C_ValueY += pow(2, 4 + i) * bitRead(data[3], i);
122-
}
123-
for (int i = 7; i >= 4; i--) {
124-
I2C_ValueY += pow(2, i) * bitRead(data[7], i);
125-
}
126-
y = (I2C_ValueY - 524288) * 0.00625;
127-
return y;
96+
if(readRegisters(0x00, (uint8_t *)data, 9) == 0) return 0;
97+
98+
unsigned long rawValue = ((unsigned long)data[2] << 12) | ((unsigned long)data[3] << 4) | ((unsigned long)data[7] >> 4);
99+
100+
float result = ((long)rawValue - 524288) * 0.00625;
101+
return (int)result;
128102
}
129103

130104
/**
131-
* @brief
132-
* @param
133-
* @retval
105+
* @brief Read Z Axis
134106
**/
135107
int MAGNETOMETER::readMagnetometerZ() {
136-
int z = 0;
137-
long I2C_ValueZ = 0;
108+
triggerMeasurement();
109+
138110
byte data[9];
139-
readRegisters(0x00, (uint8_t *)data, sizeof(data));
140-
for (int i = 7; i >= 0; i--) {
141-
I2C_ValueZ += pow(2, 12 + i) * bitRead(data[4], i);
142-
}
143-
for (int i = 7; i >= 0; i--) {
144-
I2C_ValueZ += pow(2, 4 + i) * bitRead(data[5], i);
145-
}
146-
for (int i = 7; i >= 4; i--) {
147-
I2C_ValueZ += pow(2, i) * bitRead(data[8], i);
148-
}
149-
z = (I2C_ValueZ - 524288) * 0.00625;
150-
return z;
111+
if(readRegisters(0x00, (uint8_t *)data, 9) == 0) return 0;
112+
113+
unsigned long rawValue = ((unsigned long)data[4] << 12) | ((unsigned long)data[5] << 4) | ((unsigned long)data[8] >> 4);
114+
115+
float result = ((long)rawValue - 524288) * 0.00625;
116+
return (int)result;
151117
}
152118

153119
/**
154-
* @brief
155-
* @param
156-
* @retval
120+
* @brief Read All Data and Print
157121
**/
158122
int MAGNETOMETER::readData() {
159-
int x = 0;
160-
int y = 0;
161-
int z = 0;
162-
163-
long I2C_ValueX, I2C_ValueY, I2C_ValueZ;
123+
triggerMeasurement();
164124
byte data[9];
165-
readRegisters(0x00, (uint8_t *)data, sizeof(data));
166-
I2C_ValueX = 0;
167-
I2C_ValueY = 0;
168-
I2C_ValueZ = 0;
169-
for (int i = 7; i >= 0; i--) {
170-
I2C_ValueX += pow(2, 12 + i) * bitRead(data[0], i);
171-
I2C_ValueY += pow(2, 12 + i) * bitRead(data[2], i);
172-
I2C_ValueZ += pow(2, 12 + i) * bitRead(data[4], i);
173-
}
174-
for (int i = 7; i >= 0; i--) {
175-
I2C_ValueX += pow(2, 4 + i) * bitRead(data[1], i);
176-
I2C_ValueY += pow(2, 4 + i) * bitRead(data[3], i);
177-
I2C_ValueZ += pow(2, 4 + i) * bitRead(data[5], i);
178-
}
179-
for (int i = 7; i >= 4; i--) {
180-
I2C_ValueX += pow(2, i) * bitRead(data[6], i);
181-
I2C_ValueY += pow(2, i) * bitRead(data[7], i);
182-
I2C_ValueZ += pow(2, i) * bitRead(data[8], i);
183-
}
184-
x = (I2C_ValueX - 524288) * 0.00625;
185-
y = (I2C_ValueY - 524288) * 0.00625;
186-
z = (I2C_ValueZ - 524288) * 0.00625;
187-
Serial.print("X ekseni = ");
188-
Serial.println(x);
189-
Serial.print("Y ekseni = ");
190-
Serial.println(y);
191-
Serial.print("Z ekseni = ");
192-
Serial.println(z);
193-
194-
return true;
195-
}
125+
if(readRegisters(0x00, (uint8_t *)data, 9) == 0) return 0;
126+
127+
unsigned long rawX = ((unsigned long)data[0] << 12) | ((unsigned long)data[1] << 4) | ((unsigned long)data[6] >> 4);
128+
unsigned long rawY = ((unsigned long)data[2] << 12) | ((unsigned long)data[3] << 4) | ((unsigned long)data[7] >> 4);
129+
unsigned long rawZ = ((unsigned long)data[4] << 12) | ((unsigned long)data[5] << 4) | ((unsigned long)data[8] >> 4);
130+
131+
float x = ((long)rawX - 524288) * 0.00625;
132+
float y = ((long)rawY - 524288) * 0.00625;
133+
float z = ((long)rawZ - 524288) * 0.00625;
134+
135+
Serial.print("X ekseni = "); Serial.println(x);
136+
Serial.print("Y ekseni = "); Serial.println(y);
137+
Serial.print("Z ekseni = "); Serial.println(z);
138+
139+
return 1;
140+
}

src/Deneyap_9EksenAtaletselOlcumBirimi.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
*****************************************************************************
33
@file Deneyap_9EksenAtaletselOlcumBirimi.h
44
@mainpage Deneyap 9 Dof IMU MMC5603NJ Arduino library header file
5-
@version v1.0.0
6-
@date June 23, 2022
5+
@version v1.1.0
6+
@date Dec 03, 2025
77
@brief This file contains all function prototypes and macros
88
for Deneyap 9 Dof IMU MMC5603NJ Arduino library
99
*****************************************************************************
@@ -29,6 +29,7 @@ class MAGNETOMETER {
2929

3030
private:
3131
uint8_t _address;
32+
void triggerMeasurement();
3233
uint8_t writeRegister(uint8_t address, uint8_t value);
3334
uint8_t readRegisters(uint8_t address, uint8_t *data, size_t length);
3435
};

0 commit comments

Comments
 (0)