Skip to content

Commit 9baad04

Browse files
authored
Merge pull request #53 from arduino-libraries/fix-run-through-all-servos
Fix: Do not require all servos to be connected when running `Factory_Set_Initial_Servo_Position`
2 parents 17d85ad + 05af48b commit 9baad04

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

examples/Tools/Factory_Set_Initial_Servo_Position/Factory_Set_Initial_Servo_Position.ino

Lines changed: 45 additions & 16 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);
@@ -82,13 +93,21 @@ bool set_initial_servo_position(int const id, float const target_angle)
8293
Serial.println(msg);
8394
}
8495

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();
87102
return false;
88103
}
89104

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();
92111
return false;
93112
}
94113

@@ -104,29 +123,39 @@ void setup()
104123
Serial.begin(115200);
105124
while(!Serial) { }
106125

107-
if (!Braccio.begin()) {
126+
if (!Braccio.begin(nullptr, false)) {
108127
Serial.println("Braccio.begin() failed.");
109128
for(;;) { }
110129
}
111130

112131
Braccio.disengage();
113132

133+
int success_cnt = 0;
114134
for (auto & servo : INITIAL_SERVO_POSITION)
115135
{
116136
Serial.print("Servo ");
117137
Serial.print(servo.id());
118138
Serial.print(": Target Angle = ");
119139
Serial.print(servo.angle());
120-
Serial.print(" °");
121140
Serial.println();
122141

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++;
127144
}
128145

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+
}
130159
}
131160

132161
void loop()

src/Braccio++.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ BraccioClass::BraccioClass()
9393
* PUBLIC MEMBER FUNCTIONS
9494
**************************************************************************************/
9595

96-
bool BraccioClass::begin(voidFuncPtr custom_menu)
96+
bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected)
9797
{
9898
static int constexpr RS485_RX_PIN = 1;
9999

@@ -140,13 +140,16 @@ 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++)
143+
if (wait_for_all_motor_connected)
145144
{
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;
145+
/* Wait for all motors to be actually connected. */
146+
for (int id = SmartServoClass::MIN_MOTOR_ID; id <= SmartServoClass::MAX_MOTOR_ID; id++)
147+
{
148+
auto const start = millis();
149+
auto isTimeout = [start]() -> bool { return ((millis() - start) > 5000); };
150+
for(; !isTimeout() && !connected(id); delay(100)) { }
151+
if (isTimeout()) return false;
152+
}
150153
}
151154

152155
return true;

src/Braccio++.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class BraccioClass
6565
BraccioClass();
6666

6767
inline bool begin() { return begin(nullptr); }
68-
bool begin(voidFuncPtr custom_menu);
69-
68+
bool begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected = true);
7069

7170
void pingOn();
7271
void pingOff();

0 commit comments

Comments
 (0)