@@ -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 );
@@ -82,13 +93,21 @@ bool set_initial_servo_position(int const id, float const target_angle)
82
93
Serial.println (msg);
83
94
}
84
95
85
- if (!isTargetAngleReached (EPSILON)) {
86
- Serial.println (" Servo did not reach target angle." );
96
+ if (!isTargetAngleReached (EPSILON))
97
+ {
98
+ Serial.print (" Error: Servo " );
99
+ Serial.print (id);
100
+ Serial.print (" did not reach target angle." );
101
+ Serial.println ();
87
102
return false ;
88
103
}
89
104
90
- if (isTimeout ()) {
91
- Serial.println (" Timeout error." );
105
+ if (isTimeout (start))
106
+ {
107
+ Serial.print (" Error: Servo " );
108
+ Serial.print (id);
109
+ Serial.println (" did not reach target angle within time limit." );
110
+ Serial.println ();
92
111
return false ;
93
112
}
94
113
@@ -104,29 +123,39 @@ void setup()
104
123
Serial.begin (115200 );
105
124
while (!Serial) { }
106
125
107
- if (!Braccio.begin ()) {
126
+ if (!Braccio.begin (nullptr , false )) {
108
127
Serial.println (" Braccio.begin() failed." );
109
128
for (;;) { }
110
129
}
111
130
112
131
Braccio.disengage ();
113
132
133
+ int success_cnt = 0 ;
114
134
for (auto & servo : INITIAL_SERVO_POSITION)
115
135
{
116
136
Serial.print (" Servo " );
117
137
Serial.print (servo.id ());
118
138
Serial.print (" : Target Angle = " );
119
139
Serial.print (servo.angle ());
120
- Serial.print (" °" );
121
140
Serial.println ();
122
141
123
- if (!set_initial_servo_position (servo.id (), servo.angle ())) {
124
- Serial.println (" ERROR." );
125
- return ;
126
- }
142
+ if (set_initial_servo_position (servo.id (), servo.angle ()))
143
+ success_cnt++;
127
144
}
128
145
129
- Serial.println (" SUCCESS : all servos are set to their initial position." );
146
+ if (success_cnt == SmartServoClass::NUM_MOTORS)
147
+ {
148
+ Serial.println (" SUCCESS : all servos are set to their initial position." );
149
+ }
150
+ else
151
+ {
152
+ Serial.print (" ERROR: only " );
153
+ Serial.print (success_cnt);
154
+ Serial.print (" of " );
155
+ Serial.print (SmartServoClass::NUM_MOTORS);
156
+ Serial.print (" could be set to the desired initial position." );
157
+ Serial.println ();
158
+ }
130
159
}
131
160
132
161
void loop ()
0 commit comments