Skip to content

Commit 05af48b

Browse files
committed
Stop trying to connect after a given time limit.
1 parent a9b4f92 commit 05af48b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

examples/Tools/Factory_Set_Initial_Servo_Position/Factory_Set_Initial_Servo_Position.ino

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,22 @@ static std::array<InitialServoPosition, 6> const INITIAL_SERVO_POSITION =
4949

5050
bool set_initial_servo_position(int const id, float const target_angle)
5151
{
52+
auto isTimeout = [](unsigned long const start) -> bool { return ((millis() - start) > 2000); };
53+
54+
auto start = millis();
55+
5256
Serial.print("Connecting .... ");
53-
for (; !Braccio.get(id).connected(); delay(10)) { }
54-
Serial.println("OK.");
57+
for (; !Braccio.get(id).connected() && !isTimeout(start); delay(10)) { }
58+
if (!isTimeout(start))
59+
Serial.println("OK.");
60+
else
61+
{
62+
Serial.print("Error: Can not connect to servo ");
63+
Serial.print(id);
64+
Serial.println(" within time limit.");
65+
Serial.println();
66+
return false;
67+
}
5568
delay(500);
5669

5770
Serial.print("Disengaging ... ");
@@ -65,14 +78,12 @@ bool set_initial_servo_position(int const id, float const target_angle)
6578
delay(500);
6679

6780
/* Drive to the position for assembling the servo horn. */
68-
auto const start = millis();
69-
auto isTimeout = [start]() -> bool { return ((millis() - start) > 5000); };
7081
auto isTargetAngleReached = [target_angle, id](float const epsilon) -> bool { return (fabs(Braccio.get(id).position() - target_angle) <= epsilon); };
7182

7283
static float constexpr EPSILON = 2.0f;
7384

7485
for ( float current_angle = Braccio.get(id).position();
75-
!isTargetAngleReached(EPSILON) && !isTimeout();)
86+
!isTargetAngleReached(EPSILON) && !isTimeout(start);)
7687
{
7788
Braccio.get(id).move().to(target_angle).in(200ms);
7889
delay(250);
@@ -91,7 +102,7 @@ bool set_initial_servo_position(int const id, float const target_angle)
91102
return false;
92103
}
93104

94-
if (isTimeout())
105+
if (isTimeout(start))
95106
{
96107
Serial.print("Error: Servo ");
97108
Serial.print(id);
@@ -132,7 +143,7 @@ void setup()
132143
success_cnt++;
133144
}
134145

135-
if (success_cnt != (SmartServoClass::NUM_MOTORS - 1))
146+
if (success_cnt == SmartServoClass::NUM_MOTORS)
136147
{
137148
Serial.println("SUCCESS : all servos are set to their initial position.");
138149
}

0 commit comments

Comments
 (0)