diff --git a/src/lib/motors/SmartServo.cpp b/src/lib/motors/SmartServo.cpp index 4d618f1..491e89c 100644 --- a/src/lib/motors/SmartServo.cpp +++ b/src/lib/motors/SmartServo.cpp @@ -182,9 +182,10 @@ int SmartServoClass::begin() { void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t const speed) { mutex.lock(); - if (id>8; - _txPacket.payload[index++] = _targetPosition[i]; - _txPacket.payload[index++] = _targetSpeed[i]>>8; - _txPacket.payload[index++] = _targetSpeed[i]; + _txPacket.payload[index++] = _targetPosition[idToArrayIndex(i)] >>8; + _txPacket.payload[index++] = _targetPosition[idToArrayIndex(i)]; + _txPacket.payload[index++] = _targetSpeed[idToArrayIndex(i)]>>8; + _txPacket.payload[index++] = _targetSpeed[idToArrayIndex(i)]; } sendPacket(); mutex.unlock(); diff --git a/src/lib/motors/SmartServo.h b/src/lib/motors/SmartServo.h index 4e01ad2..3e7c5ae 100644 --- a/src/lib/motors/SmartServo.h +++ b/src/lib/motors/SmartServo.h @@ -80,11 +80,17 @@ class SmartServoClass private: - static int constexpr MAX_MOTORS = 6; - static int constexpr MAX_TX_PAYLOAD_LEN = (5*MAX_MOTORS+4); + static int constexpr NUM_MOTORS = 6; + static int constexpr MAX_TX_PAYLOAD_LEN = (5*NUM_MOTORS+4); static int constexpr MAX_RX_PAYLOAD_LEN = 10; static int constexpr MAX_POSITION = 4000; + static int constexpr MIN_MOTOR_ID = 1; + static int constexpr MAX_MOTOR_ID = 6; + + inline bool isValidId(int const id) const { return ((id >= MIN_MOTOR_ID) && (id <= MAX_MOTOR_ID)); } + inline int idToArrayIndex(int const id) const { return (id - 1); } + int calcChecksum (); void sendPacket (); void writeCmd (uint8_t const id, SmartServoOperation const instruction); @@ -116,8 +122,8 @@ class SmartServoClass uint8_t _rxBuf[MAX_RX_PAYLOAD_LEN]; uint8_t _rxLen; - uint16_t _targetPosition[MAX_MOTORS]; - uint16_t _targetSpeed[MAX_MOTORS]; + uint16_t _targetPosition[NUM_MOTORS]; + uint16_t _targetSpeed[NUM_MOTORS]; PositionMode _positionMode; rtos::Mutex mutex;