From 97e7250e8c468dbd5bdf75fbb6b61e03a397b472 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <nicolo.caruso@skywarder.eu>
Date: Thu, 5 Jun 2025 21:00:44 +0200
Subject: [PATCH] [ARP] Fixed Follower missing mutex for set gain

- Fix: For the set gain function missing mutex lock.
  Also moved yaw and pitch angle computation to mutex-protected
  area
---
 src/shared/algorithms/Follower/Follower.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/shared/algorithms/Follower/Follower.cpp b/src/shared/algorithms/Follower/Follower.cpp
index d4551e5bc..e7a8d4e74 100644
--- a/src/shared/algorithms/Follower/Follower.cpp
+++ b/src/shared/algorithms/Follower/Follower.cpp
@@ -117,6 +117,8 @@ AntennaAngles Follower::getTargetAngles()
 
 bool Follower::setMaxGain(float yawGainNew, float pitchGainNew)
 {
+    Lock<FastMutex> lock(followerMutex);
+
     // In case of negative or over the limit values, do not set the gains
     if (yawGainNew < 0 || yawGainNew > YAW_GAIN_LIMIT || pitchGainNew < 0 ||
         pitchGainNew > PITCH_GAIN_LIMIT)
@@ -170,13 +172,13 @@ void Follower::step()
         // Calculate the amount to move from the current position
         diffAngles = {targetAngles.timestamp, targetAngles.yaw - vn300.yaw,
                       targetAngles.pitch - vn300.pitch};
-    }
 
-    // Rotate in the shortest direction
-    diffAngles.yaw =
-        std::min(yawGain, YAW_GAIN_LIMIT) * minimizeRotation(diffAngles.yaw);
-    diffAngles.pitch = std::min(pitchGain, PITCH_GAIN_LIMIT) *
-                       minimizeRotation(diffAngles.pitch);
+        // Rotate in the shortest direction
+        diffAngles.yaw = std::min(yawGain, YAW_GAIN_LIMIT) *
+                         minimizeRotation(diffAngles.yaw);
+        diffAngles.pitch = std::min(pitchGain, PITCH_GAIN_LIMIT) *
+                           minimizeRotation(diffAngles.pitch);
+    }
 
     // Calculate angular velocity for moving the antennas toward position
     float horizontalSpeed =
-- 
GitLab