@@ -182,9 +182,10 @@ int SmartServoClass::begin() {
182
182
183
183
void SmartServoClass::setPosition (uint8_t const id, float const angle, uint16_t const speed) {
184
184
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;
188
189
if (_positionMode==PositionMode::IMMEDIATE) {
189
190
writeWordCmd (id, REG (SmartServoRegister::TARGET_POSITION_H), angleToPosition (angle));
190
191
}
@@ -195,9 +196,8 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
195
196
float SmartServoClass::getPosition (uint8_t const id) {
196
197
mutex.lock ();
197
198
float ret = -1 ;
198
- if (id<MAX_MOTORS) {
199
+ if (isValidId (id))
199
200
ret = positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
200
- }
201
201
mutex.unlock ();
202
202
return ret;
203
203
}
@@ -211,18 +211,18 @@ void SmartServoClass::center(uint8_t const id, uint16_t const position) {
211
211
void SmartServoClass::synchronize () {
212
212
mutex.lock ();
213
213
_txPacket.id = 0xFE ;
214
- _txPacket.length = ( 4 + 1 )*MAX_MOTORS + 4 ;
214
+ _txPacket.length = MAX_TX_PAYLOAD_LEN ;
215
215
_txPacket.instruction = CMD (SmartServoOperation::SYNC_WRITE);
216
216
_txPacket.payload [0 ] = REG (SmartServoRegister::TARGET_POSITION_H);
217
217
_txPacket.payload [1 ] = 4 ;
218
218
int index = 2 ;
219
219
220
- for (int i= 1 ; i < MAX_MOTORS; i++) {
220
+ for (int i = MIN_MOTOR_ID; i <= MAX_MOTOR_ID; i++) {
221
221
_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[i- 1 ] >>8 ;
223
+ _txPacket.payload [index++] = _targetPosition[i- 1 ];
224
+ _txPacket.payload [index++] = _targetSpeed[i- 1 ]>>8 ;
225
+ _txPacket.payload [index++] = _targetSpeed[i- 1 ];
226
226
}
227
227
sendPacket ();
228
228
mutex.unlock ();
0 commit comments