@@ -49,9 +49,22 @@ static std::array<InitialServoPosition, 6> const INITIAL_SERVO_POSITION =
49
49
50
50
bool set_initial_servo_position (int const id, float const target_angle)
51
51
{
52
+ auto isTimeout = [](unsigned long const start) -> bool { return ((millis () - start) > 2000 ); };
53
+
54
+ auto start = millis ();
55
+
52
56
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
+ }
55
68
delay (500 );
56
69
57
70
Serial.print (" Disengaging ... " );
@@ -65,14 +78,12 @@ bool set_initial_servo_position(int const id, float const target_angle)
65
78
delay (500 );
66
79
67
80
/* Drive to the position for assembling the servo horn. */
68
- auto const start = millis ();
69
- auto isTimeout = [start]() -> bool { return ((millis () - start) > 5000 ); };
70
81
auto isTargetAngleReached = [target_angle, id](float const epsilon) -> bool { return (fabs (Braccio.get (id).position () - target_angle) <= epsilon); };
71
82
72
83
static float constexpr EPSILON = 2 .0f ;
73
84
74
85
for ( float current_angle = Braccio.get (id).position ();
75
- !isTargetAngleReached (EPSILON) && !isTimeout ();)
86
+ !isTargetAngleReached (EPSILON) && !isTimeout (start );)
76
87
{
77
88
Braccio.get (id).move ().to (target_angle).in (200ms);
78
89
delay (250 );
@@ -91,7 +102,7 @@ bool set_initial_servo_position(int const id, float const target_angle)
91
102
return false ;
92
103
}
93
104
94
- if (isTimeout ())
105
+ if (isTimeout (start ))
95
106
{
96
107
Serial.print (" Error: Servo " );
97
108
Serial.print (id);
@@ -132,7 +143,7 @@ void setup()
132
143
success_cnt++;
133
144
}
134
145
135
- if (success_cnt != ( SmartServoClass::NUM_MOTORS - 1 ) )
146
+ if (success_cnt == SmartServoClass::NUM_MOTORS)
136
147
{
137
148
Serial.println (" SUCCESS : all servos are set to their initial position." );
138
149
}
0 commit comments