### Filter Potentiometer Noise in C++ Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt A complete example demonstrating how to filter noisy analog readings from a potentiometer. It adds synthetic noise to a signal and uses the Kalman filter to recover the original value. ```cpp #include SimpleKalmanFilter simpleKalmanFilter(2, 2, 0.01); const long SERIAL_REFRESH_TIME = 100; long refresh_time; void setup() { Serial.begin(115200); } void loop() { float real_value = analogRead(A0) / 1024.0 * 100.0; float measured_value = real_value + random(-100, 100) / 100.0; float estimated_value = simpleKalmanFilter.updateEstimate(measured_value); if (millis() > refresh_time) { Serial.print(real_value, 4); Serial.print(","); Serial.print(measured_value, 4); Serial.print(","); Serial.print(estimated_value, 4); Serial.println(); refresh_time = millis() + SERIAL_REFRESH_TIME; } } ``` -------------------------------- ### Update Kalman Filter Estimate with New Measurement (Arduino) Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Shows the core usage of the SimpleKalmanFilter library, where a new sensor reading is processed using the updateEstimate() method. The example reads an analog value, filters it, and then prints both the raw and filtered values to the serial monitor, demonstrating noise reduction. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); void setup() { Serial.begin(115200); } void loop() { // Read raw sensor value float raw_value = analogRead(A0) / 1024.0 * 100.0; // Get filtered estimate float filtered_value = kf.updateEstimate(raw_value); // Output: raw noisy value vs smooth filtered value Serial.print("Raw: "); Serial.print(raw_value); Serial.print(" | Filtered: "); Serial.println(filtered_value); delay(100); } ``` -------------------------------- ### Smooth Altitude Estimation with BMP180 in C++ Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Integrates the Kalman filter with a BMP180 barometric pressure sensor. This example shows how to produce stable altitude readings by filtering noisy pressure data. ```cpp #include #include SimpleKalmanFilter pressureKalmanFilter(1, 1, 0.01); SFE_BMP180 pressure; float baseline; const long SERIAL_REFRESH_TIME = 100; long refresh_time; double getPressure() { char status; double T, P; status = pressure.startTemperature(); if (status != 0) { delay(status); status = pressure.getTemperature(T); if (status != 0) { status = pressure.startPressure(3); if (status != 0) { delay(status); status = pressure.getPressure(P, T); if (status != 0) return P; } } } return 0; } void setup() { Serial.begin(115200); if (!pressure.begin()) while(1); baseline = getPressure(); } void loop() { float p = getPressure(); float altitude = pressure.altitude(p, baseline); float estimated_altitude = pressureKalmanFilter.updateEstimate(altitude); if (millis() > refresh_time) { Serial.print(altitude, 6); Serial.print(","); Serial.print(estimated_altitude, 6); Serial.println(); refresh_time = millis() + SERIAL_REFRESH_TIME; } } ``` -------------------------------- ### Initialize Simple Kalman Filter Instances (Arduino) Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Demonstrates how to create and initialize SimpleKalmanFilter objects with different parameters for measurement uncertainty, estimation uncertainty, and process noise. This allows tuning the filter for various signal characteristics, such as fast-changing or slow-changing data streams. ```cpp #include // Create filter with measurement uncertainty=2, estimation uncertainty=2, process noise=0.01 SimpleKalmanFilter temperatureFilter(2, 2, 0.01); // For fast-changing signals, use higher process noise SimpleKalmanFilter accelerometerFilter(1, 1, 0.1); // For slow-changing signals, use lower process noise SimpleKalmanFilter pressureFilter(1, 1, 0.001); ``` -------------------------------- ### Dynamically Adjust Process Noise (Arduino) Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Shows how to modify the process noise parameter (q) using setProcessNoise() during runtime. This allows switching between modes for smooth filtering of stable signals or faster tracking of rapidly changing signals. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); bool fastMode = false; void setup() { Serial.begin(115200); pinMode(2, INPUT_PULLUP); // Mode switch button } void loop() { // Toggle between fast and slow tracking modes if (digitalRead(2) == LOW) { fastMode = !fastMode; if (fastMode) { kf.setProcessNoise(0.1); // Fast tracking mode Serial.println("Fast tracking enabled"); } else { kf.setProcessNoise(0.01); // Smooth filtering mode Serial.println("Smooth filtering enabled"); } delay(300); // Debounce } float estimate = kf.updateEstimate(analogRead(A0)); Serial.println(estimate); delay(50); } ``` -------------------------------- ### Initialize and Update Simple Kalman Filter Source: https://github.com/denyssene/simplekalmanfilter/blob/master/README.md Demonstrates how to instantiate the SimpleKalmanFilter class and update the estimate with new sensor readings in an Arduino loop. The updateEstimate method takes a raw sensor value and returns the filtered estimate. ```c++ SimpleKalmanFilter kf = SimpleKalmanFilter(e_mea, e_est, q); while (1) { float x = analogRead(A0); float estimated_x = kf.updateEstimate(x); // ... } ``` -------------------------------- ### Retrieve Kalman Gain in C++ Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Demonstrates how to fetch the current Kalman gain using getKalmanGain(). This value indicates the filter's trust in new measurements versus predictions, ranging from 0 to 1. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); void setup() { Serial.begin(115200); } void loop() { float measurement = analogRead(A0); float estimate = kf.updateEstimate(measurement); float gain = kf.getKalmanGain(); Serial.print("Measurement: "); Serial.print(measurement); Serial.print(" | Estimate: "); Serial.print(estimate); Serial.print(" | Kalman Gain: "); Serial.println(gain); delay(100); } ``` -------------------------------- ### Set Kalman Filter Estimation Error (Arduino) Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Demonstrates the use of the setEstimateError() method to configure the estimation uncertainty parameter. This setting influences how quickly the filter adapts to new measurements, with higher values allowing for faster response. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); void setup() { Serial.begin(115200); // Reset estimation error to high value when starting fresh // This makes the filter quickly adapt to initial measurements kf.setEstimateError(10); } void loop() { float measurement = analogRead(A0); float estimate = kf.updateEstimate(measurement); Serial.print("Measurement: "); Serial.print(measurement); Serial.print(" | Estimate: "); Serial.println(estimate); delay(100); } ``` -------------------------------- ### Retrieve Estimation Error in C++ Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Shows how to monitor the filter's confidence using getEstimateError(). Lower values indicate higher confidence in the current estimate, which is useful for debugging convergence. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); void setup() { Serial.begin(115200); } void loop() { float measurement = analogRead(A0); float estimate = kf.updateEstimate(measurement); float error = kf.getEstimateError(); Serial.print("Estimate: "); Serial.print(estimate); Serial.print(" | Confidence Error: "); Serial.println(error); delay(100); } ``` -------------------------------- ### Dynamically Adjust Measurement Error (Arduino) Source: https://context7.com/denyssene/simplekalmanfilter/llms.txt Illustrates how to use the setMeasurementError() method to dynamically adjust the measurement uncertainty parameter at runtime. This is useful for adapting the filter's trust in sensor readings based on changing conditions, such as signal strength. ```cpp #include SimpleKalmanFilter kf(2, 2, 0.01); void setup() { Serial.begin(115200); } void loop() { float measurement = analogRead(A0); // Increase measurement error when signal is weak (low values) if (measurement < 100) { kf.setMeasurementError(5); // Less trust in measurements } else { kf.setMeasurementError(2); // Normal trust level } float estimate = kf.updateEstimate(measurement); Serial.println(estimate); delay(50); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.