Skip to content

Commit 0f3d34c

Browse files
authored
Merge pull request #24 from bcmi-labs/fix-wrong-id-limit
Bugfix: Invalid comparison and assignment due to motor id's ranging from 1-6.
2 parents cb988f4 + 6e00d45 commit 0f3d34c

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-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[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();

src/lib/motors/SmartServo.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,17 @@ 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+
inline int idToArrayIndex(int const id) const { return (id - 1); }
93+
8894
int calcChecksum ();
8995
void sendPacket ();
9096
void writeCmd (uint8_t const id, SmartServoOperation const instruction);
@@ -116,8 +122,8 @@ class SmartServoClass
116122

117123
uint8_t _rxBuf[MAX_RX_PAYLOAD_LEN];
118124
uint8_t _rxLen;
119-
uint16_t _targetPosition[MAX_MOTORS];
120-
uint16_t _targetSpeed[MAX_MOTORS];
125+
uint16_t _targetPosition[NUM_MOTORS];
126+
uint16_t _targetSpeed[NUM_MOTORS];
121127
PositionMode _positionMode;
122128

123129
rtos::Mutex mutex;

0 commit comments

Comments
 (0)