Skip to content

Commit c4182dd

Browse files
committed
Instead of a blanket delay regularly check if the home position has been reached.
This way we can be sure that the arm has indeed reached the home position.
1 parent 2c828d1 commit c4182dd

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

examples/Braccio_Record_and_Replay/AppState.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,36 @@ void ZeroState::onEnter()
271271
Braccio.engage();
272272
delay(100);
273273
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
274-
delay(500);
274+
275+
auto isInHomePos = []() -> bool
276+
{
277+
float current_angles[SmartServoClass::NUM_MOTORS] = {0};
278+
Braccio.positions(current_angles);
279+
280+
float total_angle_err = 0.0;
281+
for (size_t i = 0; i < SmartServoClass::NUM_MOTORS; i++)
282+
total_angle_err += fabs(current_angles[i] - HOME_POS[i]);
283+
284+
static float const TOTAL_EPSILON = 10.0f;
285+
bool const is_in_home_pos = (total_angle_err < TOTAL_EPSILON);
286+
return is_in_home_pos;
287+
};
288+
auto isTimeout = [](unsigned long const start) -> bool
289+
{
290+
/* Timeout of one second. */
291+
auto const now = millis();
292+
if ((now - start) > 1000)
293+
return true;
294+
else
295+
return false;
296+
};
297+
298+
/* Wait until we've returned to the home position
299+
* with a timeout (i.e. we leave this function)
300+
* after one second even if we can't fully reach
301+
* the home position.
302+
*/
303+
for(auto start = millis(); !isInHomePos() && !isTimeout(start); delay(100)) { }
275304
}
276305

277306
void ZeroState::onExit()

0 commit comments

Comments
 (0)