Skip to content

Commit ecdd09c

Browse files
committed
Simplify state transition logic.
1 parent 4ec7236 commit ecdd09c

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ int state = ZERO_POSITION;
1919
static int const MAX_SAMPLES = 6*100*2; /* 20 seconds. */
2020

2121
float values[MAX_SAMPLES];
22-
float* idx = values;
23-
float* final_idx = 0;
2422
int sample_cnt = 0;
23+
int replay_cnt = 0;
2524
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
2625

2726
static lv_obj_t * counter;
@@ -30,33 +29,32 @@ static lv_obj_t * btnm;
3029
static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITION", "\n", "\0" };
3130

3231

33-
static void eventHandlerMenu(lv_event_t * e) {
32+
static void eventHandlerMenu(lv_event_t * e)
33+
{
3434
Braccio.lvgl_lock();
3535
lv_event_code_t code = lv_event_get_code(e);
3636
lv_obj_t * obj = lv_event_get_target(e);
3737

38-
if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey() == BUTTON_ENTER)) {
38+
if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey() == BUTTON_ENTER))
39+
{
3940
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
4041
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
4142

42-
if (state == RECORD) {
43-
final_idx = idx;
44-
}
45-
46-
idx = values;
47-
sample_cnt = 0;
48-
49-
switch (id) {
43+
switch (id)
44+
{
5045
case 0: // if the button pressed is the first one
51-
if (txt == "RECORD") {
46+
if (txt == "RECORD")
47+
{
5248
state = RECORD;
49+
sample_cnt = 0;
5350
Braccio.disengage(); // allow the user to freely move the braccio
5451
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
5552
Serial.println("RECORD");
5653
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); // remove disabled state of the replay button
5754
btnm_map[0] = "STOP"; // change the label of the first button to "STOP"
5855
}
59-
else if (txt == "STOP") {
56+
else if (txt == "STOP")
57+
{
6058
state = ZERO_POSITION;
6159
Braccio.engage(); // enable the steppers so that the braccio stands still
6260
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
@@ -65,14 +63,16 @@ static void eventHandlerMenu(lv_event_t * e) {
6563
break;
6664
case 1:
6765
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
68-
if (txt == "REPLAY"){
69-
state = REPLAY;
70-
btnm_map[2] = "STOP"; // change the label of the second button to "STOP"
71-
Braccio.engage();
72-
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
73-
Serial.println("REPLAY");
66+
if (txt == "REPLAY")
67+
{
68+
state = REPLAY;
69+
replay_cnt = 0;
70+
btnm_map[2] = "STOP"; // change the label of the second button to "STOP"
71+
Braccio.engage();
72+
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
7473
}
75-
else if (txt=="STOP"){
74+
else if (txt=="STOP")
75+
{
7676
state = ZERO_POSITION;
7777
Braccio.engage(); // enable the steppers so that the braccio stands still
7878
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
@@ -89,7 +89,6 @@ static void eventHandlerMenu(lv_event_t * e) {
8989
delay(500);
9090
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
9191
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
92-
Serial.println("ZERO_POSITION");
9392
break;
9493
}
9594
}
@@ -158,9 +157,10 @@ void loop()
158157
if (state == RECORD)
159158
{
160159
/* Check if we still have space for samples. */
161-
if (sample_cnt >= MAX_SAMPLES) {
160+
if (sample_cnt >= MAX_SAMPLES)
161+
{
162162
state = ZERO_POSITION;
163-
Serial.println("ZERO_POSITION");
163+
replay_cnt = 0;
164164
Braccio.lvgl_lock();
165165
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
166166
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
@@ -169,32 +169,38 @@ void loop()
169169
else
170170
{
171171
/* Capture those samples. */
172-
Braccio.positions(idx);
173-
idx += 6;
172+
Braccio.positions(values + sample_cnt);
174173
sample_cnt += 6;
175174
}
175+
176+
Braccio.lvgl_lock();
177+
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
178+
Braccio.lvgl_unlock();
176179
}
177180

178181
if (state == REPLAY)
179182
{
180-
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
181-
idx += 6;
182-
sample_cnt += 6;
183-
if (idx >= final_idx) {
184-
Serial.println("REPLAY done");
183+
Braccio.moveTo(values[replay_cnt + 0], values[replay_cnt + 1], values[replay_cnt + 2], values[replay_cnt + 3], values[replay_cnt + 4], values[replay_cnt + 5]);
184+
replay_cnt += 6;
185+
186+
if (replay_cnt >= sample_cnt)
187+
{
185188
state = ZERO_POSITION;
186189
Braccio.lvgl_lock();
187190
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
188191
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
189192
Braccio.lvgl_unlock();
190193
}
191-
}
192194

193-
if (state != ZERO_POSITION)
194-
{
195195
Braccio.lvgl_lock();
196-
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
196+
lv_label_set_text_fmt(counter, "Counter: %d" , (replay_cnt / 6));
197197
Braccio.lvgl_unlock();
198198
}
199+
200+
if (state == ZERO_POSITION)
201+
{
202+
Braccio.engage();
203+
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
204+
}
199205
}
200206
}

0 commit comments

Comments
 (0)