ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
MMC56X3.cpp
Go to the documentation of this file.
1#include "MMC56X3.h"
2
3Adafruit_MMC5603 MMC56X3::mag;
4sensors_event_t MMC56X3::magEvent;
11bool MMC56X3::didBegin = false;
13float MMC56X3::minimum = 0.0f;
14
15bool MMC56X3::Initialize(uint8_t address) {
16 Wire.begin();
17 mag = Adafruit_MMC5603(address);
18 didBegin = mag.begin();
19
20 mag.setDataRate(100);
21 mag.setContinuousMode(true);
22 return didBegin;
23}
24
26 if (didBegin) {
27 mag.getEvent(&magEvent);
28
29 if (timeStep.IsReady()) {
33 }
34
35 if (timeStepCal.IsReady()) {
37 }
38
39 mag.magnetSetReset();
40 }
41}
42
47
49 return GetMagnitude() > minimum + 20.0f;
50}
51
55
57 const float kSurfaceField = 6619.0; // Gauss
58 const float kBrmax = 14800.0; // Gauss
59 const float kBHmax = 52.0; // MGOe
60
61 Vector3D position;
62
63 Update();
64
65 // Calculate magnetic field strength in millimeters
66 float distance = GetMagnitude();
67 float fieldStrength = (kSurfaceField * (kBrmax / sqrtf(kBrmax * kBrmax + distance * distance * 4.0f))) * (kBHmax / sqrtf(kBHmax * kBHmax + distance * distance * 4.0f)) * 8.0f;
68
69 // Estimate position of magnet on three axes
70 position.X = magneticField.X * (fieldStrength / kSurfaceField);
71 position.Y = magneticField.Y * (fieldStrength / kSurfaceField);
72 position.Z = magneticField.Z * (fieldStrength / kSurfaceField);
73
74 return position;
75}
A class for interfacing with the Adafruit MMC56x3 magnetometer sensor.
static Vector3D EstimateMagnetPosition()
Estimates the position of a detected magnet.
Definition MMC56X3.cpp:56
static float minimum
Minimum threshold for detection.
Definition MMC56X3.h:41
static MinFilter< 10 > minF
Minimum filter for noise reduction.
Definition MMC56X3.h:40
static Vector3D GetMagneticField()
Retrieves the current magnetic field vector.
Definition MMC56X3.cpp:43
static RunningAverageFilter< 10 > xFilter
Filter for the X-axis magnetic field data.
Definition MMC56X3.h:33
static bool Initialize(uint8_t address=MMC56X3_DEFAULT_ADDRESS)
Initializes the MMC56x3 sensor.
Definition MMC56X3.cpp:15
static TimeStep timeStepCal
Time utility for calibration intervals.
Definition MMC56X3.h:37
static TimeStep timeStep
Time utility for regular updates.
Definition MMC56X3.h:36
static sensors_event_t magEvent
Sensor event data for magnetic field measurements.
Definition MMC56X3.h:32
static Adafruit_MMC5603 mag
Instance of the Adafruit MMC5603 sensor.
Definition MMC56X3.h:31
static float GetMagnitude()
Calculates the magnitude of the magnetic field.
Definition MMC56X3.cpp:52
static float IsDetected()
Checks if a magnet is detected.
Definition MMC56X3.cpp:48
static RunningAverageFilter< 10 > yFilter
Filter for the Y-axis magnetic field data.
Definition MMC56X3.h:34
static bool didBegin
Flag indicating if the sensor has been initialized.
Definition MMC56X3.h:39
static void Update()
Updates the magnetic field data from the sensor.
Definition MMC56X3.cpp:25
static RunningAverageFilter< 10 > zFilter
Filter for the Z-axis magnetic field data.
Definition MMC56X3.h:35
static Vector3D magneticField
Processed magnetic field vector.
Definition MMC56X3.h:38
Implements a minimum filter over a sliding window.
Definition MinFilter.h:28
float Filter(float value)
Filters the given value, updating the minimum value within the memory window.
Smooths data values using a weighted running average.
float Filter(float value)
Filters the input value using the running average.
Provides a mechanism to trigger actions at a specified frequency.
Definition TimeStep.h:18
bool IsReady()
Checks if the specified time interval has elapsed.
Definition TimeStep.cpp:11
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29