Skip to content

Commit 8fc941e

Browse files
committed
Implement - ServoWrite, boundary checks around handling the servohardware objects
1 parent de50089 commit 8fc941e

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/components/servo/controller.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,33 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
8484
@returns True if successful, False otherwise
8585
*/
8686
/**************************************************************************/
87-
bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {}
87+
bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {
88+
if (!_servo_model->DecodeServoWrite(stream)) {
89+
WS_DEBUG_PRINTLN("[servo] Error: Failed to decode ServoWrite message!");
90+
return false;
91+
}
92+
wippersnapper_servo_ServoWrite *msg_write = _servo_model->GetServoWriteMsg();
93+
uint8_t pin = atoi(msg_write->servo_pin + 1);
94+
// find the pin by using getpin()
95+
int servo_idx = -1;
96+
for (int i = 0; i < _active_servo_pins; i++) {
97+
if (_servo_hardware[i]->GetPin() == pin) {
98+
servo_idx = i;
99+
break;
100+
}
101+
}
102+
if (servo_idx == -1) {
103+
WS_DEBUG_PRINTLN("[servo] Error: Servo pin not found!");
104+
return false;
105+
}
106+
// Write the pulse width to the servo
107+
_servo_hardware[servo_idx]->ServoWrite(msg_write->pulse_width);
108+
WS_DEBUG_PRINT("[servo] Set Pulse Width: ");
109+
WS_DEBUG_PRINT(msg_write->pulse_width);
110+
WS_DEBUG_PRINT(" mS on pin: ");
111+
WS_DEBUG_PRINT(msg_write->servo_pin);
112+
return true;
113+
}
88114

89115
/**************************************************************************/
90116
/*!
@@ -94,4 +120,6 @@ bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {}
94120
@returns True if successful, False otherwise
95121
*/
96122
/**************************************************************************/
97-
bool ServoController::Handle_Servo_Remove(pb_istream_t *stream) {}
123+
bool ServoController::Handle_Servo_Remove(pb_istream_t *stream) {
124+
// just delete the ServoHardware object, it'll deinit itself!
125+
}

src/components/servo/hardware.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,38 @@ ServoHardware::ServoHardware(int pin, int min_pulse_width, int max_pulse_width,
4343

4444
/**************************************************************************/
4545
/*!
46-
@brief Destructor
46+
@brief Detaches the servo from the pin and frees the pin for
47+
other uses.
4748
*/
4849
/**************************************************************************/
49-
ServoHardware::~ServoHardware() {}
50+
ServoHardware::~ServoHardware() {
51+
#ifdef ARDUINO_ARCH_ESP32
52+
if (!_is_attached) {
53+
WS_DEBUG_PRINTLN("[servo] Error: Failed to detach, servo not attached!");
54+
return;
55+
}
56+
if (!ledcDetach(_pin)) {
57+
WS_DEBUG_PRINTLN("[servo] Error: Failed to detach servo from pin!");
58+
return;
59+
}
60+
_is_attached = false;
61+
#else
62+
if (_servo == nullptr) {
63+
WS_DEBUG_PRINTLN("[servo] Error: Failed to detach, servo not created!");
64+
return;
65+
}
66+
if (!_servo->attached()) {
67+
WS_DEBUG_PRINTLN("[servo] Error: Failed to detach, servo not attached!");
68+
return;
69+
}
70+
_servo->detach();
71+
delete _servo;
72+
_servo = nullptr;
73+
#endif
74+
75+
WS_DEBUG_PRINT("[servo] Servo detached from pin ");
76+
WS_DEBUG_PRINTLN(_pin);
77+
}
5078

5179
/**************************************************************************/
5280
/*!
@@ -66,6 +94,10 @@ bool ServoHardware::ServoAttach() {
6694
_is_attached = true;
6795
}
6896
#else
97+
if (_servo == nullptr) {
98+
WS_DEBUG_PRINTLN("[servo] Error: Failed to detach, servo not created!");
99+
return false;
100+
}
69101
rc = _servo.attach(_pin, _min_pulse_width, _max_pulse_width);
70102
#endif
71103

@@ -87,8 +119,16 @@ bool ServoHardware::ServoAttach() {
87119
/**************************************************************************/
88120
void ServoHardware::ServoWrite(int value) {
89121
#ifdef ARDUINO_ARCH_ESP32
122+
if (!_is_attached) {
123+
WS_DEBUG_PRINTLN("[servo] Error: Servo not attached!");
124+
return;
125+
}
90126
writeMicroseconds(value);
91127
#else
128+
if (_servo == nullptr || !_servo->attached()) {
129+
WS_DEBUG_PRINTLN("[servo] Error: Servo not attached!");
130+
return;
131+
}
92132
_servo.writeMicroseconds(value);
93133
#endif
94134
}
@@ -103,11 +143,6 @@ void ServoHardware::ServoWrite(int value) {
103143
*/
104144
/**************************************************************************/
105145
void ServoHardware::writeMicroseconds(int value) {
106-
if (!_is_attached) {
107-
WS_DEBUG_PRINTLN("[servo] Error: Servo not attached!");
108-
return;
109-
}
110-
111146
// Clamp value to a valid pulse_width range
112147
if (value < _min_pulse_width)
113148
value = _min_pulse_width;

src/components/servo/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ServoHardware {
4444
~ServoHardware();
4545
bool ServoAttach();
4646
void ServoWrite(int value);
47+
uint8_t GetPin() { return _pin; }
4748

4849
private:
4950
#ifdef ARDUINO_ARCH_ESP32

0 commit comments

Comments
 (0)