Skip to content

Commit dea7437

Browse files
committed
Implement function to verify if a given angle is within the allowed range of motion for the motor.
1 parent 92a2ad0 commit dea7437

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/lib/motors/SmartServo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ int SmartServoClass::begin() {
180180
}
181181
}
182182

183-
void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t const speed) {
183+
void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t const speed)
184+
{
185+
if (!isValidAngle(angle))
186+
return;
187+
184188
mutex.lock();
185189
if (isValidId(id))
186190
{

src/lib/motors/SmartServo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class SmartServoClass
8989
static int constexpr MIN_MOTOR_ID = 1;
9090
static int constexpr MAX_MOTOR_ID = 6;
9191

92+
inline bool isValidAngle(float const angle) { return ((angle >= 0.0f) && (angle <= MAX_ANGLE)); }
9293
inline bool isValidId(int const id) const { return ((id >= MIN_MOTOR_ID) && (id <= MAX_MOTOR_ID)); }
9394
inline int idToArrayIndex(int const id) const { return (id - 1); }
9495

0 commit comments

Comments
 (0)