Skip to content

Commit

Permalink
Review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrawehr committed Jan 27, 2025
1 parent 0c5c8f4 commit 2508e25
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 89 deletions.
6 changes: 6 additions & 0 deletions src/devices/Arduino/ArduinoBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public ArduinoBoard(Stream serialPortStream, bool usesHardwareFlowControl)
/// <remarks>
/// The device is initialized when the first command is sent. The constructor always succeeds.
/// </remarks>
/// <remarks>
/// The stream must have a blocking read operation, or the connection might fail. Some serial port drivers incorrectly
/// return immediately when no data is available and the <code>ReadTimeout</code> is set to infinite (the default). In such a case, set the
/// ReadTimeout to a large value (such as <code>Int.Max - 10</code>), which will simulate a blocking call.
/// </remarks>
/// <param name="serialPortStream">A stream to an Arduino/Firmata device</param>
public ArduinoBoard(Stream serialPortStream)
: this(serialPortStream, false)
Expand All @@ -96,6 +101,7 @@ public ArduinoBoard(string portName, int baudRate)
{
_dataStream = null;
_serialPort = new SerialPort(portName, baudRate);
// Set the timeout to a long time, but not infinite. See the note for the constructor above.
_serialPort.ReadTimeout = int.MaxValue - 10;
StreamUsesHardwareFlowControl = false; // Would need to configure the serial port externally for this to work
_logger = this.GetCurrentClassLogger();
Expand Down
85 changes: 0 additions & 85 deletions src/devices/Bmm150/Bmm150.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,91 +133,6 @@ private void Initialize()
WriteRegister(Bmp180Register.OP_MODE_ADDR, 0x00);
}

/// <summary>
/// Calibrate the magnetometer.
/// Please make sure you are not close to any magnetic field like magnet or phone
/// Please make sure you are moving the magnetometer all over space, rotating it.
/// </summary>
/// <param name="numberOfMeasurements">Number of measurement for the calibration, default is 100</param>
// https://platformio.org/lib/show/12697/M5_BMM150
[Obsolete("Not a reliable way of calibration. Use a calibration determined with MagneticDeviationCorrection instead")]
public void CalibrateMagnetometer(int numberOfMeasurements = 100)
{
CalibrateMagnetometer(null, numberOfMeasurements);
}

/// <summary>
/// Calibrate the magnetometer.
/// Please make sure you are not close to any magnetic field like magnet or phone
/// Please make sure you are moving the magnetometer all over space, rotating it.
/// </summary>
/// <param name="progress">A progress provider (returns a value in percent)</param>
/// <param name="numberOfMeasurements">Number of measurement for the calibration, default is 100</param>
// https://platformio.org/lib/show/12697/M5_BMM150
[Obsolete("Not a reliable way of calibration. Use a calibration determined with MagneticDeviationCorrection instead")]
public void CalibrateMagnetometer(IProgress<double>? progress, int numberOfMeasurements)
{
Vector3 mag_min = new Vector3() { X = 9000, Y = 9000, Z = 30000 };
Vector3 mag_max = new Vector3() { X = -9000, Y = -9000, Z = -30000 };
Vector3 rawMagnetometerData;
if (numberOfMeasurements <= 0)
{
throw new ArgumentOutOfRangeException(nameof(numberOfMeasurements), "The number of measurements must be > 0");
}

for (int i = 0; i < numberOfMeasurements; i++)
{
try
{
rawMagnetometerData = ReadMagnetometerWithoutCorrection();

if (rawMagnetometerData.X != 0)
{
mag_min.X = (rawMagnetometerData.X < mag_min.X) ? rawMagnetometerData.X : mag_min.X;
mag_max.X = (rawMagnetometerData.X > mag_max.X) ? rawMagnetometerData.X : mag_max.X;
}

if (rawMagnetometerData.Y != 0)
{
mag_max.Y = (rawMagnetometerData.Y > mag_max.Y) ? rawMagnetometerData.Y : mag_max.Y;
mag_min.Y = (rawMagnetometerData.Y < mag_min.Y) ? rawMagnetometerData.Y : mag_min.Y;
}

if (rawMagnetometerData.Z != 0)
{
mag_min.Z = (rawMagnetometerData.Z < mag_min.Z) ? rawMagnetometerData.Z : mag_min.Z;
mag_max.Z = (rawMagnetometerData.Z > mag_max.Z) ? rawMagnetometerData.Z : mag_max.Z;
}

// Wait for 100ms until next reading
Wait(100);
}
catch
{
// skip this reading
}

if (progress != null)
{
double percentDone = ((double)i / numberOfMeasurements) * 100.0;
progress.Report(percentDone);
}
}

if (progress != null)
{
progress.Report(100.0);
}

// Refresh CalibrationCompensation vector
CalibrationCompensation = new Vector3()
{
X = (mag_max.X + mag_min.X) / 2,
Y = (mag_max.Y + mag_min.Y) / 2,
Z = (mag_max.Z + mag_min.Z) / 2
};
}

/// <summary>
/// True if there is a data to read
/// </summary>
Expand Down
6 changes: 2 additions & 4 deletions src/devices/Bmm150/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ Documentation for the Bmm150 can be [found here](https://www.bosch-sensortec.com

## Usage

You can find an example in the [sample](./samples/Program.cs) directory. Usage is straight forward. The "Calibration" provided
needs conceptual review and is therefore currently not recommended. Also, when using the magnetometer in a real-world application,
it is not reasonable to turn it around all axes at every startup (try that with a car!).
You can find an example in the [sample](./samples/Program.cs) directory. Usage is straight forward. The previous "Calibration" method
was removed, as it would need to be completely rewritten to do something useful.

```csharp
I2cConnectionSettings mpui2CConnectionSettingmpus = new(1, Bmm150.DefaultI2cAddress);

using Bmm150 bmm150 = new Bmm150(I2cDevice.Create(mpui2CConnectionSettingmpus));


while (!Console.KeyAvailable)
{
MagnetometerData magne;
Expand Down
Binary file removed src/devices/Bmm150/corrcalib.png
Binary file not shown.
Binary file removed src/devices/Bmm150/rawcalib.png
Binary file not shown.

0 comments on commit 2508e25

Please sign in to comment.