Skip to content

Commit dee6b44

Browse files
authored
Fix: Wait for all motors to be connected before allowing the program flow to continue. Otherwise we risk sending commands when servos are not yet conected. If a timeout occurs, begin returns false. (#47)
1 parent 71bd4bd commit dee6b44

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Braccio++.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ bool BraccioClass::begin(voidFuncPtr custom_menu)
140140

141141
_motors_connected_thd.start(mbed::callback(this, &BraccioClass::motorConnectedThreadFunc));
142142

143+
/* Wait for all motors to be actually connected. */
144+
for (int id = SmartServoClass::MIN_MOTOR_ID; id <= SmartServoClass::MAX_MOTOR_ID; id++)
145+
{
146+
auto const start = millis();
147+
auto isTimeout = [start]() -> bool { return ((millis() - start) > 5000); };
148+
for(; !isTimeout() && !connected(id); delay(100)) { }
149+
if (isTimeout()) return false;
150+
}
151+
143152
return true;
144153
}
145154

0 commit comments

Comments
 (0)