Skip to content

Commit e2f96f2

Browse files
committed
Fix: Motor IDs go from 1 to 6, but values are stored in arrays with a max size of 6.
This error was not observable while having configured for 7 motors (because '<' comparison was used) but is now obviously a problem.
1 parent 038f15a commit e2f96f2

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/lib/motors/SmartServo.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ int SmartServoClass::begin() {
182182

183183
void 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
195196
float 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) {
211211
void 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[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];
226226
}
227227
sendPacket();
228228
mutex.unlock();

src/lib/motors/SmartServo.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ class SmartServoClass
8080

8181
private:
8282

83-
static int constexpr MAX_MOTORS = 6;
84-
static int constexpr MAX_TX_PAYLOAD_LEN = (5*MAX_MOTORS+4);
83+
static int constexpr NUM_MOTORS = 6;
84+
static int constexpr MAX_TX_PAYLOAD_LEN = (5*NUM_MOTORS+4);
8585
static int constexpr MAX_RX_PAYLOAD_LEN = 10;
8686
static int constexpr MAX_POSITION = 4000;
8787

88+
static int constexpr MIN_MOTOR_ID = 1;
89+
static int constexpr MAX_MOTOR_ID = 6;
90+
91+
inline bool isValidId(int const id) const { return ((id >= MIN_MOTOR_ID) && (id <= MAX_MOTOR_ID)); }
92+
8893
int calcChecksum ();
8994
void sendPacket ();
9095
void writeCmd (uint8_t const id, SmartServoOperation const instruction);
@@ -116,8 +121,8 @@ class SmartServoClass
116121

117122
uint8_t _rxBuf[MAX_RX_PAYLOAD_LEN];
118123
uint8_t _rxLen;
119-
uint16_t _targetPosition[MAX_MOTORS];
120-
uint16_t _targetSpeed[MAX_MOTORS];
124+
uint16_t _targetPosition[NUM_MOTORS];
125+
uint16_t _targetSpeed[NUM_MOTORS];
121126
PositionMode _positionMode;
122127

123128
rtos::Mutex mutex;

0 commit comments

Comments
 (0)