Skip to content

Commit 06f1b10

Browse files
authored
Merge pull request #77 from arduino-libraries/patch-up-record-replay
[Braccio_Record_and_Replay] Polishing user flow and issues uncovered during testing.
2 parents 88bdca0 + b346e4d commit 06f1b10

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

examples/Braccio_Record_and_Replay/AppState.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,6 @@ void custom_main_menu()
108108
Braccio.connectJoystickTo(btnm);
109109
}
110110

111-
/**************************************************************************************
112-
* State
113-
**************************************************************************************/
114-
115-
State * State::handle_OnZeroPosition()
116-
{
117-
return new ZeroState();
118-
}
119-
120111
/**************************************************************************************
121112
* IdleState
122113
**************************************************************************************/
@@ -141,6 +132,11 @@ State * IdleState::handle_OnReplay()
141132
return new ReplayState();
142133
}
143134

135+
State * IdleState::handle_OnZeroPosition()
136+
{
137+
return new ZeroState();
138+
}
139+
144140
/**************************************************************************************
145141
* RecordState
146142
**************************************************************************************/
@@ -150,8 +146,11 @@ void RecordState::onEnter()
150146
btnm_map[0] = "STOP";
151147
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
152148
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
149+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
153150

151+
delay(100);
154152
Braccio.disengage();
153+
delay(100);
155154
sample_cnt = 0;
156155
}
157156

@@ -160,9 +159,12 @@ void RecordState::onExit()
160159
btnm_map[0] = "RECORD";
161160
lv_btnmatrix_clear_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
162161
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
162+
lv_btnmatrix_clear_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
163163
lv_label_set_text_fmt(counter, "Counter: %d" , 0);
164164

165+
delay(100);
165166
Braccio.engage();
167+
delay(100);
166168
}
167169

168170
State * RecordState::handle_OnRecord()
@@ -217,13 +219,15 @@ void ReplayState::onEnter()
217219
btnm_map[2] = "STOP";
218220
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED);
219221
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
222+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
220223
}
221224

222225
void ReplayState::onExit()
223226
{
224227
btnm_map[2] = "REPLAY";
225228
lv_btnmatrix_clear_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED);
226229
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
230+
lv_btnmatrix_clear_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
227231
lv_label_set_text_fmt(counter, "Counter: %d" , 0);
228232
}
229233

@@ -267,10 +271,40 @@ void ZeroState::onEnter()
267271
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
268272
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
269273

274+
delay(100);
270275
Braccio.engage();
271276
delay(100);
272277
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
273-
delay(500);
278+
279+
auto isInHomePos = []() -> bool
280+
{
281+
float current_angles[SmartServoClass::NUM_MOTORS] = {0};
282+
Braccio.positions(current_angles);
283+
284+
float total_angle_err = 0.0;
285+
for (size_t i = 0; i < SmartServoClass::NUM_MOTORS; i++)
286+
total_angle_err += fabs(current_angles[i] - HOME_POS[i]);
287+
288+
static float const TOTAL_EPSILON = 10.0f;
289+
bool const is_in_home_pos = (total_angle_err < TOTAL_EPSILON);
290+
return is_in_home_pos;
291+
};
292+
auto isTimeout = [](unsigned long const start) -> bool
293+
{
294+
/* Timeout of one second. */
295+
auto const now = millis();
296+
if ((now - start) > 1000)
297+
return true;
298+
else
299+
return false;
300+
};
301+
302+
/* Wait until we've returned to the home position
303+
* with a timeout (i.e. we leave this function)
304+
* after one second even if we can't fully reach
305+
* the home position.
306+
*/
307+
for(auto start = millis(); !isInHomePos() && !isTimeout(start); delay(100)) { }
274308
}
275309

276310
void ZeroState::onExit()

examples/Braccio_Record_and_Replay/AppState.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class State
5454
protected:
5555
virtual State * handle_OnRecord() { return this; }
5656
virtual State * handle_OnReplay() { return this; }
57-
virtual State * handle_OnZeroPosition();
57+
virtual State * handle_OnZeroPosition() { return this; }
5858
virtual State * handle_OnTimerTick() { return this; }
5959
};
6060

@@ -67,8 +67,9 @@ class IdleState : public State
6767
virtual void onExit() override;
6868

6969
protected:
70-
virtual State * handle_OnRecord() override;
71-
virtual State * handle_OnReplay() override;
70+
virtual State * handle_OnRecord () override;
71+
virtual State * handle_OnReplay () override;
72+
virtual State * handle_OnZeroPosition() override;
7273
};
7374

7475
class RecordState : public State

0 commit comments

Comments
 (0)