@@ -182,9 +182,10 @@ int SmartServoClass::begin() {
182182
183183void SmartServoClass::setPosition (uint8_t const id, float const angle, uint16_t const speed) {
184184 mutex.lock ();
185- if (id<MAX_MOTORS) {
186- _targetPosition[id] = angleToPosition (angle);
187- _targetSpeed[id] = speed;
185+ if (isValidId (id))
186+ {
187+ _targetPosition[id-1 ] = angleToPosition (angle);
188+ _targetSpeed[id-1 ] = speed;
188189 if (_positionMode==PositionMode::IMMEDIATE) {
189190 writeWordCmd (id, REG (SmartServoRegister::TARGET_POSITION_H), angleToPosition (angle));
190191 }
@@ -195,9 +196,8 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
195196float SmartServoClass::getPosition (uint8_t const id) {
196197 mutex.lock ();
197198 float ret = -1 ;
198- if (id<MAX_MOTORS) {
199+ if (isValidId (id))
199200 ret = positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
200- }
201201 mutex.unlock ();
202202 return ret;
203203}
@@ -211,18 +211,18 @@ void SmartServoClass::center(uint8_t const id, uint16_t const position) {
211211void SmartServoClass::synchronize () {
212212 mutex.lock ();
213213 _txPacket.id = 0xFE ;
214- _txPacket.length = ( 4 + 1 )*MAX_MOTORS + 4 ;
214+ _txPacket.length = MAX_TX_PAYLOAD_LEN ;
215215 _txPacket.instruction = CMD (SmartServoOperation::SYNC_WRITE);
216216 _txPacket.payload [0 ] = REG (SmartServoRegister::TARGET_POSITION_H);
217217 _txPacket.payload [1 ] = 4 ;
218218 int index = 2 ;
219219
220- for (int i= 1 ; i < MAX_MOTORS; i++) {
220+ for (int i = MIN_MOTOR_ID; i <= MAX_MOTOR_ID; i++) {
221221 _txPacket.payload [index++] = i;
222- _txPacket.payload [index++] = _targetPosition[i ] >>8 ;
223- _txPacket.payload [index++] = _targetPosition[i ];
224- _txPacket.payload [index++] = _targetSpeed[i ]>>8 ;
225- _txPacket.payload [index++] = _targetSpeed[i ];
222+ _txPacket.payload [index++] = _targetPosition[idToArrayIndex (i) ] >>8 ;
223+ _txPacket.payload [index++] = _targetPosition[idToArrayIndex (i) ];
224+ _txPacket.payload [index++] = _targetSpeed[idToArrayIndex (i) ]>>8 ;
225+ _txPacket.payload [index++] = _targetSpeed[idToArrayIndex (i) ];
226226 }
227227 sendPacket ();
228228 mutex.unlock ();
0 commit comments