Skip to content

Commit 020d192

Browse files
committed
updated/fixed/cleaned up/documented the get/set MagOffset[1,2]() methods
1 parent 9f5412c commit 020d192

File tree

2 files changed

+69
-72
lines changed

2 files changed

+69
-72
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -574,18 +574,25 @@ int8_t TMAG5273::setMagneticGain(uint8_t magneticGain)
574574
/// range of possible offset valid entrees can be +/-128. The offset value
575575
/// is calculated by multiplying bit resolution with the entered value.
576576
/// TMAG5273_REG_MAG_OFFSET_CONFIG_1
577-
/// @param offset1 Value within the range +/-128
577+
/// @param offset1 Value within the range +/-128 in mT
578578
/// @return Error code (0 is success, negative is failure, positive is warning)
579-
int8_t TMAG5273::setMagneticOffset1(float offset1)
579+
int8_t TMAG5273::setMagneticOffset1(int8_t offset1)
580580
{
581581
if (offset1 < -128.f || offset1 > 128.f)
582582
return -1; // invalid offset1
583583

584584
uint8_t rangeValXY = getXYAxisRange();
585585
float range = rangeValXY == 0 ? 40.f : 80.f;
586586

587-
// Multiply bit resolution with entered value from datasheet
588-
int8_t magOffset = (int8_t)((2048.f * offset1) / range); // 2048 = 2^12
587+
// From the datasheet, equation to calculate offset value (17)
588+
//
589+
// OFFSET_REG = (2^12 * OFFSET_mT) / 2*|RANGE_mT|
590+
// Where RANGE_mT is the full scale range of the selected axis
591+
// OFFSET_mT is the desired offset in mT - passed to this method
592+
//
593+
// factoring out the 2 in the denominator gives us:
594+
// OFFSET_REG = (2^11 * OFFSET_mT) / |RANGE_mT|
595+
int8_t magOffset = (int8_t)((2048.f * (float)offset1) / range); // 2048 = 2^11
589596

590597
// deal with int/uint conversion
591598
uint8_t tempWrite = *(uint8_t *)&magOffset;
@@ -602,7 +609,7 @@ int8_t TMAG5273::setMagneticOffset1(float offset1)
602609
/// TMAG5273_REG_MAG_OFFSET_CONFIG_2
603610
/// @param offset2 Value within the range +/-128
604611
/// @return Error code (0 is success, negative is failure, positive is warning)
605-
int8_t TMAG5273::setMagneticOffset2(float offset2)
612+
int8_t TMAG5273::setMagneticOffset2(int8_t offset2)
606613
{
607614
if (offset2 < -128.f || offset2 > 128.f)
608615
return -1; // invalid offset2
@@ -613,15 +620,22 @@ int8_t TMAG5273::setMagneticOffset2(float offset2)
613620
return -1; // invalid channel selection
614621

615622
// See which XY axis range is currently set
616-
float rangeXY = getXYAxisRange() == 0 ? 40.f : 80.f;
617-
618-
// See which Z axis range is currently set
619-
float rangeZ = getZAxisRange() == 0 ? 40.f : 80.f;
620623

621-
float range = channelSelect == 1 ? rangeXY : rangeZ;
624+
float range;
625+
if (channelSelect == 1)
626+
range = getXYAxisRange() == 0 ? 40.f : 80.f;
627+
else
628+
range = getZAxisRange() == 0 ? 40.f : 80.f;
622629

623-
// Multiply bit resolution with entered value
624-
int8_t magOffset = (int8_t)((2048.f * offset2) / range); // 2048 = 2^12
630+
// From the datasheet, equation to calculate offset value (17)
631+
//
632+
// OFFSET_REG = (2^12 * OFFSET_mT) / 2*|RANGE_mT|
633+
// Where RANGE_mT is the full scale range of the selected axis
634+
// OFFSET_mT is the desired offset in mT - passed to this method
635+
//
636+
// factoring out the 2 in the denominator gives us:
637+
// OFFSET_REG = (2^11 * OFFSET_mT) / |RANGE_mT|
638+
int8_t magOffset = (int8_t)((2048.f * (float)offset2) / range); // 2048 = 2^11
625639

626640
// deal with int/uint conversion
627641
uint8_t tempWrite = *(uint8_t *)&magOffset;
@@ -1302,22 +1316,23 @@ int8_t TMAG5273::getMagneticOffset1()
13021316
if (_theI2CBus.readRegister(TMAG5273_REG_MAG_OFFSET_CONFIG_1, tmpRead) != ksfTkErrOk)
13031317
return 0;
13041318
int8_t magOffset1 = *(int8_t *)&tmpRead;
1305-
// KDB 12/25
1306-
// rangeVal is always 0, so range is always 40? Not sure the logic here, but
1307-
// leaving it for now -- can re-visit later if needed
1308-
uint8_t rangeVal = 0;
1309-
uint8_t range = 0;
1310-
if (rangeVal == 0)
1311-
{
1312-
range = 40;
1313-
}
1314-
else
1315-
{
1316-
range = 80;
1317-
}
13181319

1319-
// Divide by resoltuion (2^12 = 2048)
1320-
return (int8_t)((magOffset1 * range) / 2048);
1320+
// To calculate the offset register value from the desired offset in mT,
1321+
// use the following equation from the datasheet:
1322+
// OFFSET_REG = (2^12 * OFFSET_mT) / 2*|RANGE_mT|
1323+
// Where RANGE_mT is the full scale range of the selected axis
1324+
// OFFSET_mT is the desired offset in mT - passed to this method
1325+
//
1326+
// factoring out the 2 in the denominator gives us:
1327+
// OFFSET_REG = (2^11 * OFFSET_mT) / |RANGE_mT|
1328+
//
1329+
// Now to determine offset_mfT from OFFSET_REG, we rearrange to:
1330+
// OFFSET_mT = (OFFSET_REG * |RANGE_mT|) / 2^11
1331+
1332+
uint8_t rangeValXY = getXYAxisRange();
1333+
float range = rangeValXY == 0 ? 40.f : 80.f;
1334+
1335+
return (int8_t)((magOffset1 * range) / 2048.f); // 2^11 = 2048
13211336
}
13221337

13231338
/// @brief This function will return an 8-bit, 2's complement offset value
@@ -1335,56 +1350,38 @@ int8_t TMAG5273::getMagneticOffset2()
13351350
return 0;
13361351

13371352
int magOffset2 = *(int8_t *)&tempRead;
1338-
// Choose the XY axis range
1339-
uint8_t channelSelect = getAngleEn(); // 1, 2, or 3
1340-
uint8_t rangeValXY = getXYAxisRange();
1341-
uint8_t rangeXY = 0;
1342-
if (rangeValXY == 0)
1343-
{
1344-
rangeXY = 40;
1345-
}
1346-
else if (rangeValXY == 1)
1347-
{
1348-
rangeXY = 80;
1349-
}
13501353

1351-
// Choose the Z axis range
1352-
uint8_t rangeValZ = getZAxisRange();
1353-
uint8_t rangeZ = 0;
1354-
if (rangeValZ == 0)
1355-
{
1356-
rangeZ = 40;
1357-
}
1358-
else if (rangeValZ == 1)
1359-
{
1360-
rangeZ = 80;
1361-
}
1354+
// Determine whether the magnetic offset channel is Y or Z for the range selection
1355+
uint8_t channelSelect = getAngleEn(); // 1, 2, or 3
1356+
if (channelSelect < 1 || channelSelect > 3)
1357+
return -1; // invalid channel selection
13621358

1363-
// Range value for caluclation
1364-
uint8_t range;
1359+
// See which XY axis range is currently set
13651360

1366-
// Select Channel
1367-
if (channelSelect == 1) // Y
1368-
{
1369-
range = rangeXY; // 40 or 80
1370-
}
1371-
else if (channelSelect == 2) // Z
1372-
{
1373-
range = rangeZ; // 40 or 80
1374-
}
1375-
else if (channelSelect == 3) // Z
1376-
{
1377-
range = rangeZ; // 40 or 80
1378-
}
1361+
float range;
1362+
if (channelSelect == 1)
1363+
range = getXYAxisRange() == 0 ? 40.f : 80.f;
13791364
else
1380-
{
1381-
return -1; // Returns error
1382-
// Error could also be the ranges being disabled
1383-
}
1365+
range = getZAxisRange() == 0 ? 40.f : 80.f;
1366+
1367+
// To calculate the offset register value from the desired offset in mT,
1368+
// use the following equation from the datasheet:
1369+
// OFFSET_REG = (2^12 * OFFSET_mT) / 2*|RANGE_mT|
1370+
// Where RANGE_mT is the full scale range of the selected axis
1371+
// OFFSET_mT is the desired offset in mT - passed to this method
1372+
//
1373+
// factoring out the 2 in the denominator gives us:
1374+
// OFFSET_REG = (2^11 * OFFSET_mT) / |RANGE_mT|
1375+
//
1376+
// Now to determine offset_mfT from OFFSET_REG, we rearrange to:
1377+
// OFFSET_mT = (OFFSET_REG * |RANGE_mT|) / 2^11
13841378

13851379
// Use resolution equation in datasheet
1386-
return (int8_t)((magOffset2 * range) / 2048); // 2^11 = 2048
1380+
return (int8_t)((magOffset2 * range) / 2048.f); // 2^11 = 2048
13871381
}
1382+
// KDB ************************************************************************************************/
1383+
// STOP STOP STOP
1384+
// KDB ************************************************************************************************/
13881385

13891386
/// @brief Returns angle calculation, magnetic gain, and offset
13901387
/// corrections between two selected magnetic channels.

src/SparkFun_TMAG5273_Arduino_Library.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class TMAG5273
5353
int8_t setMagDir(uint8_t threshDir); // Sets the direction of threshold check
5454
int8_t setMagnitudeGain(uint8_t gainAdjust); // Sets the axis for magnitude gain correction value
5555
int8_t setMagneticGain(uint8_t magneticGain); // Sets the 8-bit gain value to adjust a Hall axis gain
56-
int8_t setMagneticOffset1(float offset1); // Sets the offset value determined by a primary for the first axis
57-
int8_t setMagneticOffset2(float offset2); // Sets the offset value determined by a primary for the second axis
56+
int8_t setMagneticOffset1(int8_t offset1); // Sets the offset value determined by a primary for the first axis
57+
int8_t setMagneticOffset2(int8_t offset2); // Sets the offset value determined by a primary for the second axis
5858
int8_t setAngleEn(uint8_t angleEnable); // Sets the angle caluclation, mag gain, and offset corections
5959
int8_t setXYAxisRange(uint8_t xyAxisRange); // Sets the X and Y axes magnetic range from ±40mT or ±80mT
6060
int8_t setZAxisRange(uint8_t zAxisRange); // Sets the Z axis magnetic range from ±40mT or ±80mT

0 commit comments

Comments
 (0)