Skip to content

Commit dac4af5

Browse files
committed
Fix - Controller writes the pulse width from the message upon Add, variable timer width for ESP32x instead of hardcode
1 parent 7458980 commit dac4af5

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/components/servo/controller.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
7474

7575
// Write the default minimum to a servo
7676
if (did_attach) {
77-
_servo_hardware[_active_servo_pins]->ServoWrite(MIN_SERVO_PULSE_WIDTH);
77+
_servo_hardware[_active_servo_pins]->ServoWrite(
78+
(int)msg_add->min_pulse_width);
7879
WS_DEBUG_PRINT("[servo] Servo attached to pin: ");
7980
WS_DEBUG_PRINTLN(msg_add->servo_pin);
8081
_active_servo_pins++;
@@ -116,10 +117,6 @@ bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {
116117
}
117118
// Write the pulse width to the servo
118119
_servo_hardware[servo_idx]->ServoWrite(msg_write->pulse_width);
119-
WS_DEBUG_PRINT("[servo] Set Pulse Width: ");
120-
WS_DEBUG_PRINT(msg_write->pulse_width);
121-
WS_DEBUG_PRINT(" µs on pin: ");
122-
WS_DEBUG_PRINT(msg_write->servo_pin);
123120
return true;
124121
}
125122

src/components/servo/hardware.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ void ServoHardware::ServoWrite(int value) {
147147
if (value > _max_pulse_width)
148148
value = _max_pulse_width;
149149
_servo->writeMicroseconds(value);
150+
WS_DEBUG_PRINT("[servo] Set Pulse Width: ");
151+
WS_DEBUG_PRINT(value);
152+
WS_DEBUG_PRINT(" µs on pin: ");
153+
WS_DEBUG_PRINT(_pin);
150154
#endif
151155
}
152156

@@ -174,6 +178,11 @@ void ServoHardware::writeMicroseconds(int value) {
174178
((double)value / ((double)20000 / (double)pow(2, LEDC_TIMER_WIDTH)));
175179
if (!ledcWrite(_pin, count))
176180
WS_DEBUG_PRINTLN("[servo] Error: Failed to write to servo pin!");
181+
182+
WS_DEBUG_PRINT("[servo] Set Pulse Width: ");
183+
WS_DEBUG_PRINT(value);
184+
WS_DEBUG_PRINT(" uS on pin: ");
185+
WS_DEBUG_PRINT(_pin);
177186
}
178187

179188
/**************************************************************************/

src/components/servo/hardware.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
#ifdef ARDUINO_ARCH_ESP32
2020
#include "esp32-hal-ledc.h"
2121
#include "esp_err.h"
22-
2322
#define LEDC_TIMER_WIDTH \
24-
12 ///< timer width to request from LEDC manager component, in bits (NOTE:
25-
///< While ESP32x can go up to 16 bit timer width, ESP32-S2 does not work
26-
///< at this resolution. So, for the purposes of keeping this library
27-
///< compatible with multiple ESP32x platforms, the timer width has been
28-
///< scaled down to 10 bits and the calculation adjusted accordingly)
23+
SOC_LEDC_TIMER_BIT_WIDTH ///< Dynamically scale bit width for each ESP32-x
24+
///< Arch.
2925
#else
3026
#include <Servo.h>
3127
#endif

0 commit comments

Comments
 (0)