From 49ddb3e02d6a363318a5505db299d69499d49950 Mon Sep 17 00:00:00 2001 From: Matthew Lloyd <github@matthewlloyd.net> Date: Thu, 20 Mar 2014 05:27:32 +0000 Subject: [PATCH] AP_Compass: avoid division by zero if we haven't received any mag reports Otherwise, get_field() will return NaNs after once every few calls to read() during compassmot on PX4 platforms, which causes compassmot to fail. This is a quick hack around the deeper issue, which could be something like the PX4 mag driver experiencing starvation and skipping mag reports, buffer overrun or something else that causes mag reports to be dropped. Or perhaps we should never expect in the first place that we will always receive at least one mag report between calls to read(). --- libraries/AP_Compass/AP_Compass_PX4.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/AP_Compass/AP_Compass_PX4.cpp b/libraries/AP_Compass/AP_Compass_PX4.cpp index 541a53bde..61ca2a30f 100644 --- a/libraries/AP_Compass/AP_Compass_PX4.cpp +++ b/libraries/AP_Compass/AP_Compass_PX4.cpp @@ -92,6 +92,9 @@ bool AP_Compass_PX4::read(void) } for (uint8_t i=0; i<_num_instances; i++) { + // avoid division by zero if we haven't received any mag reports + if (_count[i] == 0) continue; + _sum[i] /= _count[i]; _sum[i] *= 1000; -- GitLab