From 2e87933a1e80ce999117a1835ddc5eac5c1e9e6a Mon Sep 17 00:00:00 2001 From: Thomas Hargrove Date: Wed, 16 Oct 2024 14:17:43 -0700 Subject: [PATCH 1/2] Support angle and microseconds for nrf52, renesasm stn32f4 --- src/nrf52/Servo.cpp | 13 +++++++------ src/renesas/Servo.cpp | 9 ++++++--- src/stm32f4/Servo.cpp | 9 ++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/nrf52/Servo.cpp b/src/nrf52/Servo.cpp index fe9876a..5305a84 100644 --- a/src/nrf52/Servo.cpp +++ b/src/nrf52/Servo.cpp @@ -74,12 +74,13 @@ void Servo::detach() void Servo::write(int value) { - if (value < 0) - value = 0; - else if (value > 180) - value = 180; - value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); - + if(value < MIN_PULSE_WIDTH) { + if (value < 0) + value = 0; + else if (value > 180) + value = 180; + value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); + } writeMicroseconds(value); } diff --git a/src/renesas/Servo.cpp b/src/renesas/Servo.cpp index fba4210..7ed268c 100644 --- a/src/renesas/Servo.cpp +++ b/src/renesas/Servo.cpp @@ -232,12 +232,15 @@ void Servo::detach() } } -void Servo::write(int angle) +void Servo::write(int value) { if (servoIndex != SERVO_INVALID_INDEX) { ra_servo_t *servo = &ra_servos[servoIndex]; - angle = constrain(angle, 0, 180); - writeMicroseconds(map(angle, 0, 180, servo->period_min, servo->period_max)); + if(value < MIN_PULSE_WIDTH) { + value = constrain(value, 0, 180); + value = map(value, 0, 180, servo->period_min, servo->period_max); + } + this->writeMicroseconds(value); } } diff --git a/src/stm32f4/Servo.cpp b/src/stm32f4/Servo.cpp index 8687c51..2149f2e 100644 --- a/src/stm32f4/Servo.cpp +++ b/src/stm32f4/Servo.cpp @@ -148,9 +148,12 @@ bool Servo::detach() { return true; } -void Servo::write(int degrees) { - degrees = constrain(degrees, this->minAngle, this->maxAngle); - this->writeMicroseconds(ANGLE_TO_US(degrees)); +void Servo::write(int value) { + if(value < MIN_PULSE_WIDTH) { + value = constrain(value, this->minAngle, this->maxAngle); + value = ANGLE_TO_US(value); + } + this->writeMicroseconds(value); } int Servo::read() const { From db471fdd4c047d191e3f01daca1ad7549c376d49 Mon Sep 17 00:00:00 2001 From: Thomas Hargrove Date: Wed, 16 Oct 2024 14:21:06 -0700 Subject: [PATCH 2/2] More consistent use of this depending on the file --- src/nrf52/Servo.cpp | 2 +- src/renesas/Servo.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nrf52/Servo.cpp b/src/nrf52/Servo.cpp index 5305a84..5f6d2d0 100644 --- a/src/nrf52/Servo.cpp +++ b/src/nrf52/Servo.cpp @@ -81,7 +81,7 @@ void Servo::write(int value) value = 180; value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); } - writeMicroseconds(value); + this->writeMicroseconds(value); } diff --git a/src/renesas/Servo.cpp b/src/renesas/Servo.cpp index 7ed268c..141d91f 100644 --- a/src/renesas/Servo.cpp +++ b/src/renesas/Servo.cpp @@ -240,7 +240,7 @@ void Servo::write(int value) value = constrain(value, 0, 180); value = map(value, 0, 180, servo->period_min, servo->period_max); } - this->writeMicroseconds(value); + writeMicroseconds(value); } }