Skip to content

[Braccio_Record_and_Replay] Polishing user flow and issues uncovered during testing. #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions examples/Braccio_Record_and_Replay/AppState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,6 @@ void custom_main_menu()
Braccio.connectJoystickTo(btnm);
}

/**************************************************************************************
* State
**************************************************************************************/

State * State::handle_OnZeroPosition()
{
return new ZeroState();
}

/**************************************************************************************
* IdleState
**************************************************************************************/
Expand All @@ -141,6 +132,11 @@ State * IdleState::handle_OnReplay()
return new ReplayState();
}

State * IdleState::handle_OnZeroPosition()
{
return new ZeroState();
}

/**************************************************************************************
* RecordState
**************************************************************************************/
Expand All @@ -150,8 +146,11 @@ void RecordState::onEnter()
btnm_map[0] = "STOP";
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);

delay(100);
Braccio.disengage();
delay(100);
sample_cnt = 0;
}

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

delay(100);
Braccio.engage();
delay(100);
}

State * RecordState::handle_OnRecord()
Expand Down Expand Up @@ -217,13 +219,15 @@ void ReplayState::onEnter()
btnm_map[2] = "STOP";
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED);
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
}

void ReplayState::onExit()
{
btnm_map[2] = "REPLAY";
lv_btnmatrix_clear_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED);
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
lv_btnmatrix_clear_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_DISABLED);
lv_label_set_text_fmt(counter, "Counter: %d" , 0);
}

Expand Down Expand Up @@ -267,10 +271,40 @@ void ZeroState::onEnter()
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);

delay(100);
Braccio.engage();
delay(100);
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
delay(500);

auto isInHomePos = []() -> bool
{
float current_angles[SmartServoClass::NUM_MOTORS] = {0};
Braccio.positions(current_angles);

float total_angle_err = 0.0;
for (size_t i = 0; i < SmartServoClass::NUM_MOTORS; i++)
total_angle_err += fabs(current_angles[i] - HOME_POS[i]);

static float const TOTAL_EPSILON = 10.0f;
bool const is_in_home_pos = (total_angle_err < TOTAL_EPSILON);
return is_in_home_pos;
};
auto isTimeout = [](unsigned long const start) -> bool
{
/* Timeout of one second. */
auto const now = millis();
if ((now - start) > 1000)
return true;
else
return false;
};

/* Wait until we've returned to the home position
* with a timeout (i.e. we leave this function)
* after one second even if we can't fully reach
* the home position.
*/
for(auto start = millis(); !isInHomePos() && !isTimeout(start); delay(100)) { }
}

void ZeroState::onExit()
Expand Down
7 changes: 4 additions & 3 deletions examples/Braccio_Record_and_Replay/AppState.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class State
protected:
virtual State * handle_OnRecord() { return this; }
virtual State * handle_OnReplay() { return this; }
virtual State * handle_OnZeroPosition();
virtual State * handle_OnZeroPosition() { return this; }
virtual State * handle_OnTimerTick() { return this; }
};

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

protected:
virtual State * handle_OnRecord() override;
virtual State * handle_OnReplay() override;
virtual State * handle_OnRecord () override;
virtual State * handle_OnReplay () override;
virtual State * handle_OnZeroPosition() override;
};

class RecordState : public State
Expand Down