ESP32 - AirTag Documentation
Loading...
Searching...
No Matches
distance.cpp File Reference

Implementation of RSSI averaging and distance estimation utilities. More...

#include <math.h>
#include "distance.h"

Functions

void updateRssiAvg (int rssi)
 Updates the running average of the received signal strength indicator (RSSI).
float estimateDistanceMeters (float rssi, float txPower, float n)
 Estimates the distance (in meters) from RSSI using the log-distance path loss model.

Variables

bool hasAvg = false
 Flag to indicate whether the RSSI average has been initialized.
float rssiAvg = 0.0f
 Stores the current smoothed RSSI average.
float txPower = -52.0f
 Transmitter power at 1 meter (in dBm).
float nFactor = 2.5f
 Path-loss exponent (environmental factor).

Detailed Description

Implementation of RSSI averaging and distance estimation utilities.

This file provides definitions for global parameters and functions that help smooth RSSI (Received Signal Strength Indicator) values and estimate distance based on the log-distance path loss model.

The algorithm uses exponential smoothing for RSSI and applies the following formula for distance estimation:

\[  d = 10^{\frac{(txPower - rssi)}{10 \cdot n}}
\]

where:

  • $d$ is the estimated distance in meters,
  • $rssi$ is the measured signal strength in dBm,
  • $txPower$ is the RSSI at 1 meter reference distance,
  • $n$ is the path-loss exponent.

Function Documentation

◆ estimateDistanceMeters()

float estimateDistanceMeters ( float rssi,
float txPower,
float n )

Estimates the distance (in meters) from RSSI using the log-distance path loss model.

Estimate distance from RSSI using the log-distance path loss model.

Computes an approximate distance to the transmitter based on the difference between the measured RSSI and the known transmit power at 1 meter.

Parameters
[in]rssiReceived signal strength indicator (in dBm).
[in]txPowerTransmitter power at 1 meter (in dBm).
[in]nPath-loss exponent (environmental factor).
Returns
Estimated distance in meters.

◆ updateRssiAvg()

void updateRssiAvg ( int rssi)

Updates the running average of the received signal strength indicator (RSSI).

Update the exponential moving average of the RSSI.

Applies exponential smoothing with a fixed factor $\alpha = 0.2$. This reduces noise in instantaneous RSSI readings.

Parameters
[in]rssiCurrent measured RSSI value (in dBm).

Variable Documentation

◆ hasAvg

bool hasAvg = false

Flag to indicate whether the RSSI average has been initialized.

Flag indicating whether an RSSI average has been initialized.

◆ nFactor

float nFactor = 2.5f

Path-loss exponent (environmental factor).

Typical values:

  • ~2.0 in free space
  • 2.7–4.0 in indoor/obstructed environments

Should be tuned experimentally for best accuracy.

◆ rssiAvg

float rssiAvg = 0.0f

Stores the current smoothed RSSI average.

Exponential moving average of the RSSI value.

◆ txPower

float txPower = -52.0f

Transmitter power at 1 meter (in dBm).

Reference transmit power (measured RSSI at 1 meter distance).

This value should be tuned based on calibration measurements.