Raw magnetometer readings contain significant noise that causes erratic robot behaviour. Even small heading errors compound over distance in an open field, making reliable row traversal impossible without pre-processing. AGRIBOT addresses this by applying a Moving Median filter to collect and smooth magnetometer samples — implemented inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Dhruv2012/Autonomous-Farm-Robot/llms.txt
Use this file to discover all available pages before exploring further.
data_manipulation.py.
Moving Median Filter
The Moving Median filter accumulates raw heading samples in a list. Every 8 samples it computes the median value, publishes the result onFilteredheading, and resets the accumulator. Using the median rather than the mean makes the filter robust to short-duration spikes (e.g. from magnetic interference) because outliers do not pull the median the way they pull the mean.
Rawheading via mag_data.publish(RawHeading), so the unfiltered signal is available for real-time visualization in plot.py alongside the filtered output.
The sign inversion (-1 *) applied to the median result aligns the magnetometer’s sensor frame convention with the navigation frame used by the rest of the stack. Without this correction, the angular error fed to the PD controller would have the wrong sign, causing the robot to steer away from — rather than toward — the goal.
Angle Error Computation
Once a filtered heading is available,data_manipulation.py computes the corrected angle the robot must turn through to face the goal. This combines the GPS-derived bearing from gps_converter (received on angleOG) with the filtered magnetometer heading.
if statement handle the sign of the GPS bearing separately so that the subtraction always produces an error in the correct rotational direction. The normalization step at the end ensures the result stays within [-180°, 180°], which prevents the PD controller from commanding a 350° rotation when a −10° correction would achieve the same result.
The corrected angle is published on angle and consumed directly by the autonomousdrive node via its /angle subscriber.
Visualization with plot.py
Theplot.py node subscribes to Rawheading and renders a live Matplotlib chart so developers can observe the raw heading in real time during a simulation run: