From b31eee9266f0fbc2b5ffc308d30f76347e728a68 Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Wed, 11 May 2022 17:15:48 +0200
Subject: [PATCH] [Servo] Fixed reset when getting timer from PWM class

The problem was due to returning a copy of type GeneralPurposeTimer, in its constructor the timer is reset
---
 src/shared/actuators/Buzzer.h          | 9 ---------
 src/shared/actuators/Servo/ServoData.h | 4 ++--
 src/shared/drivers/timer/PWM.cpp       | 6 +++---
 src/shared/drivers/timer/PWM.h         | 4 ++--
 4 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/src/shared/actuators/Buzzer.h b/src/shared/actuators/Buzzer.h
index 50c6b3340..ad3539d87 100644
--- a/src/shared/actuators/Buzzer.h
+++ b/src/shared/actuators/Buzzer.h
@@ -83,15 +83,6 @@ private:
 inline Buzzer::Buzzer(TIM_TypeDef *timer, TimerUtils::Channel channel)
     : timer(timer), channel(channel)
 {
-    // this->timer.setPrescaler(
-    //     TimerUtils::computePrescalerValue(timer, frequency * 2));
-    // this->timer.setAutoReloadRegister(1);
-    // this->timer.setOutputCompareMode(channel,
-    //                                  TimerUtils::OutputCompareMode::TOGGLE);
-    // this->timer.setCaptureCompareRegister(channel, 1);
-    // this->timer.generateUpdate();
-    // this->timer.enableCaptureCompareOutput(channel);
-    // this->timer.enableCaptureCompareComplementaryOutput(channel);
 }
 
 inline void Buzzer::on()
diff --git a/src/shared/actuators/Servo/ServoData.h b/src/shared/actuators/Servo/ServoData.h
index b4ff14d35..5676c0c4b 100644
--- a/src/shared/actuators/Servo/ServoData.h
+++ b/src/shared/actuators/Servo/ServoData.h
@@ -38,8 +38,8 @@ struct ServoData
 
     void print(std::ostream& os) const
     {
-        os << timestamp << "," << timer << "," << channel << "," << position
-           << "\n";
+        os << timestamp << "," << static_cast<int>(timer) << ","
+           << static_cast<int>(channel) << "," << position << "\n";
     }
 };
 
diff --git a/src/shared/drivers/timer/PWM.cpp b/src/shared/drivers/timer/PWM.cpp
index f8e2faa10..e8b156b96 100644
--- a/src/shared/drivers/timer/PWM.cpp
+++ b/src/shared/drivers/timer/PWM.cpp
@@ -78,8 +78,8 @@ void PWM::setDutyCycle(TimerUtils::Channel channel, float dutyCycle)
 {
     if (dutyCycle >= 0 && dutyCycle <= 1)
         timer.setCaptureCompareRegister(
-            channel,
-            static_cast<uint16_t>(dutyCycle * timer.readAutoReloadRegister()));
+            channel, static_cast<uint16_t>(
+                         dutyCycle * timer.readAutoReloadRegister() + 0.5));
 }
 
 float PWM::getDutyCycle(TimerUtils::Channel channel)
@@ -88,7 +88,7 @@ float PWM::getDutyCycle(TimerUtils::Channel channel)
            static_cast<float>(timer.readAutoReloadRegister());
 }
 
-GP16bitTimer PWM::getTimer() { return timer; }
+GP16bitTimer& PWM::getTimer() { return timer; }
 
 void PWM::setTimerConfiguration()
 {
diff --git a/src/shared/drivers/timer/PWM.h b/src/shared/drivers/timer/PWM.h
index d30e5d45c..085be48f6 100644
--- a/src/shared/drivers/timer/PWM.h
+++ b/src/shared/drivers/timer/PWM.h
@@ -65,7 +65,7 @@ public:
      * @param dutyCycleResolution Duty cycle levels.
      */
     explicit PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency = 50,
-                 unsigned int dutyCycleResolution = 1000);
+                 unsigned int dutyCycleResolution = 20000);
 
     ~PWM();
 
@@ -98,7 +98,7 @@ public:
     /**
      * @brief Returns the timer used to generate the pwm signal.
      */
-    GP16bitTimer getTimer();
+    GP16bitTimer& getTimer();
 
 private:
     // This class is not copyable!
-- 
GitLab