@@ -29,7 +29,20 @@ ServoController::ServoController() {
29
29
@brief Destructor
30
30
*/
31
31
/* *************************************************************************/
32
- ServoController::~ServoController () {}
32
+ ServoController::~ServoController () {
33
+ // De-initialize all servos
34
+ for (int i = 0 ; i < _active_servo_pins; i++) {
35
+ if (_servo_hardware[i] != nullptr ) {
36
+ delete _servo_hardware[i];
37
+ _servo_hardware[i] = nullptr ;
38
+ }
39
+ }
40
+
41
+ if (_servo_model != nullptr ) {
42
+ delete _servo_model;
43
+ _servo_model = nullptr ;
44
+ }
45
+ }
33
46
34
47
/* *************************************************************************/
35
48
/* !
@@ -45,6 +58,11 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
45
58
return false ;
46
59
}
47
60
61
+ if (_active_servo_pins >= MAX_SERVOS) {
62
+ WS_DEBUG_PRINTLN (" [servo] Error: Maximum number of servos reached!" );
63
+ return false ;
64
+ }
65
+
48
66
wippersnapper_servo_ServoAdd *msg_add = _servo_model->GetServoAddMsg ();
49
67
uint8_t pin = atoi (msg_add->servo_pin + 1 );
50
68
_servo_hardware[_active_servo_pins] = new ServoHardware (
@@ -55,7 +73,7 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
55
73
did_attach = _servo_hardware[_active_servo_pins]->ServoAttach ();
56
74
57
75
// Write the default minimum to a servo
58
- if (! did_attach) {
76
+ if (did_attach) {
59
77
_servo_hardware[_active_servo_pins]->ServoWrite (MIN_SERVO_PULSE_WIDTH);
60
78
WS_DEBUG_PRINT (" [servo] Servo attached to pin: " );
61
79
WS_DEBUG_PRINT (msg_add->servo_pin );
@@ -91,7 +109,6 @@ bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {
91
109
}
92
110
wippersnapper_servo_ServoWrite *msg_write = _servo_model->GetServoWriteMsg ();
93
111
uint8_t pin = atoi (msg_write->servo_pin + 1 );
94
- // find the pin by using getpin()
95
112
int servo_idx = -1 ;
96
113
for (int i = 0 ; i < _active_servo_pins; i++) {
97
114
if (_servo_hardware[i]->GetPin () == pin) {
@@ -107,7 +124,7 @@ bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {
107
124
_servo_hardware[servo_idx]->ServoWrite (msg_write->pulse_width );
108
125
WS_DEBUG_PRINT (" [servo] Set Pulse Width: " );
109
126
WS_DEBUG_PRINT (msg_write->pulse_width );
110
- WS_DEBUG_PRINT (" mS on pin: " );
127
+ WS_DEBUG_PRINT (" µs on pin: " );
111
128
WS_DEBUG_PRINT (msg_write->servo_pin );
112
129
return true ;
113
130
}
0 commit comments