AverageAngle, a library to average of multiple headings

In a recent project I needed to average multiple angles, sort of heading of a compass. This averaging was needed to minimize noise in the individual samples. The first straightforward code worked quite well until I got to the border case around 360 == 0 degrees. Averaging { 359, 0, 1 } did not gave the expected 0 or 360 but 120, which was a wakeup call to rethink my math.

The result is a AverageAngle library. An AverageAngle object can add multiple angles and give a correct average. The lib works both with DEGREES and RADIANS to be set in the constructor, DEGREES being the default. A simple test sketch completes the library.

The inner working is mapping the angles on a plane in X and Y coordinates and take the average of X and Y. Optionally one can add the length (weight or distance) for every angle so it can be used broader than needed within my project.

Sources on Github - https://github.com/RobTillaart/Arduino/tree/master/libraries/AverageAngle.

As always comments and remarks and improvements are welcome.

void test3(int count)
  AverageAngle AA(AverageAngle::RADIANS);

  for (int i = 0; i < count; i++)
    AA.add( PI / 180 * random(180));
  int cnt = AA.count();
  float avg = AA.getAverage();

  Serial.println(avg, 6);
1 Like