Skip to content

Commit 7070733

Browse files
authored
Merge pull request #69 from arduino-libraries/rework_examples
Rework 01_Braccio_learning_mode example
2 parents 08b17a2 + db8e7f7 commit 7070733

File tree

34 files changed

+330
-134
lines changed

34 files changed

+330
-134
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <Braccio++.h>
2+
3+
// Colors
4+
#define COLOR_TEAL 0x00878F
5+
#define COLOR_LIGHT_TEAL 0x62AEB2
6+
#define COLOR_ORANGE 0xE47128
7+
8+
enum states {
9+
RECORD,
10+
REPLAY,
11+
ZERO_POSITION
12+
};
13+
14+
int state = ZERO_POSITION;
15+
16+
float values[10000];
17+
float* idx = values;
18+
float* final_idx = 0;
19+
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
20+
21+
static lv_obj_t * counter;
22+
static lv_obj_t * btnm;
23+
24+
static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITION", "\n", "\0" };
25+
26+
27+
static void eventHandlerMenu(lv_event_t * e) {
28+
lv_event_code_t code = lv_event_get_code(e);
29+
lv_obj_t * obj = lv_event_get_target(e);
30+
31+
if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) {
32+
state = ZERO_POSITION;
33+
return;
34+
}
35+
36+
if (code == LV_EVENT_CLICKED) {
37+
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
38+
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
39+
40+
if (state == RECORD) {
41+
final_idx = idx;
42+
}
43+
44+
idx = values;
45+
46+
switch (id) {
47+
case 0: // if the button pressed is the first one
48+
if (txt == "RECORD") {
49+
state = RECORD;
50+
Braccio.disengage(); // allow the user to freely move the braccio
51+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
52+
Serial.println("RECORD");
53+
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); // remove disabled state of the replay button
54+
btnm_map[0] = "STOP"; // change the label of the first button to "STOP"
55+
}
56+
else if (txt == "STOP") {
57+
state = ZERO_POSITION;
58+
Braccio.engage(); // enable the steppers so that the braccio stands still
59+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
60+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
61+
}
62+
break;
63+
case 1:
64+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
65+
if (txt == "REPLAY"){
66+
state = REPLAY;
67+
btnm_map[2] = "STOP"; // change the label of the second button to "STOP"
68+
Braccio.engage();
69+
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
70+
Serial.println("REPLAY");
71+
}
72+
else if (txt=="STOP"){
73+
state = ZERO_POSITION;
74+
Braccio.engage(); // enable the steppers so that the braccio stands still
75+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
76+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
77+
}
78+
79+
break;
80+
81+
default:
82+
state = ZERO_POSITION;
83+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
84+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
85+
Braccio.engage();
86+
delay(500);
87+
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
88+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
89+
Serial.println("ZERO_POSITION");
90+
break;
91+
}
92+
}
93+
}
94+
95+
void mainMenu() {
96+
static lv_style_t style_focus;
97+
lv_style_init(&style_focus);
98+
lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE));
99+
lv_style_set_outline_width(&style_focus, 4);
100+
101+
static lv_style_t style_btn;
102+
lv_style_init(&style_btn);
103+
lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL));
104+
lv_style_set_text_color(&style_btn, lv_color_white());
105+
106+
btnm = lv_btnmatrix_create(lv_scr_act());
107+
lv_obj_set_size(btnm, 240, 240);
108+
lv_btnmatrix_set_map(btnm, btnm_map);
109+
lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0);
110+
111+
lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS);
112+
lv_obj_add_style(btnm, &style_focus, LV_PART_ITEMS | LV_STATE_FOCUS_KEY);
113+
114+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
115+
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
116+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE);
117+
118+
lv_btnmatrix_set_one_checked(btnm, true);
119+
lv_btnmatrix_set_selected_btn(btnm, 0);
120+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
121+
122+
counter = lv_label_create(btnm);
123+
lv_label_set_text_fmt(counter, "Counter: %d" , 0);
124+
lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80);
125+
126+
lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL);
127+
128+
Braccio.connectJoystickTo(btnm);
129+
}
130+
131+
void setup() {
132+
Braccio.begin(mainMenu);
133+
delay(500);
134+
135+
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
136+
delay(500);
137+
138+
Serial.begin(115200);
139+
Serial.println("Replicate a movement");
140+
}
141+
142+
void loop() {
143+
if (state == RECORD) {
144+
Braccio.positions(idx);
145+
idx += 6;
146+
}
147+
if (state == REPLAY) {
148+
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
149+
idx += 6;
150+
if (idx >= final_idx) {
151+
Serial.println("REPLAY done");
152+
state = ZERO_POSITION;
153+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
154+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
155+
}
156+
}
157+
if (idx - values >= sizeof(values)) {
158+
Serial.println("ZERO_POSITION");
159+
state = ZERO_POSITION;
160+
}
161+
delay(100);
162+
if (state != ZERO_POSITION) {
163+
lv_label_set_text_fmt(counter, "Counter: %d" , idx - values);
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <Braccio++.h>
2+
3+
// Colors
4+
#define COLOR_TEAL 0x00878F
5+
#define COLOR_LIGHT_TEAL 0x62AEB2
6+
#define COLOR_ORANGE 0xE47128
7+
8+
enum states {
9+
RECORD,
10+
REPLAY,
11+
ZERO_POSITION
12+
};
13+
14+
int state = ZERO_POSITION;
15+
16+
float values[10000];
17+
float* idx = values;
18+
float* final_idx = 0;
19+
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
20+
21+
static lv_obj_t * counter;
22+
static lv_obj_t * btnm;
23+
24+
static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITION", "\n", "\0" };
25+
26+
27+
static void eventHandlerMenu(lv_event_t * e) {
28+
lv_event_code_t code = lv_event_get_code(e);
29+
lv_obj_t * obj = lv_event_get_target(e);
30+
31+
if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) {
32+
state = ZERO_POSITION;
33+
return;
34+
}
35+
36+
if (code == LV_EVENT_CLICKED) {
37+
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
38+
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
39+
40+
if (state == RECORD) {
41+
final_idx = idx;
42+
}
43+
44+
idx = values;
45+
46+
switch (id) {
47+
case 0: // if the button pressed is the first one
48+
if (txt == "RECORD") {
49+
state = RECORD;
50+
Braccio.disengage(); // allow the user to freely move the braccio
51+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED);
52+
Serial.println("RECORD");
53+
lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); // remove disabled state of the replay button
54+
btnm_map[0] = "STOP"; // change the label of the first button to "STOP"
55+
}
56+
else if (txt == "STOP") {
57+
state = ZERO_POSITION;
58+
Braccio.engage(); // enable the steppers so that the braccio stands still
59+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
60+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
61+
}
62+
break;
63+
case 1:
64+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
65+
if (txt == "REPLAY"){
66+
state = REPLAY;
67+
btnm_map[2] = "STOP"; // change the label of the second button to "STOP"
68+
Braccio.engage();
69+
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
70+
Serial.println("REPLAY");
71+
}
72+
else if (txt=="STOP"){
73+
state = ZERO_POSITION;
74+
Braccio.engage(); // enable the steppers so that the braccio stands still
75+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
76+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
77+
}
78+
79+
break;
80+
81+
default:
82+
state = ZERO_POSITION;
83+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
84+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
85+
Braccio.engage();
86+
delay(500);
87+
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
88+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
89+
Serial.println("ZERO_POSITION");
90+
break;
91+
}
92+
}
93+
}
94+
95+
void mainMenu() {
96+
static lv_style_t style_focus;
97+
lv_style_init(&style_focus);
98+
lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE));
99+
lv_style_set_outline_width(&style_focus, 4);
100+
101+
static lv_style_t style_btn;
102+
lv_style_init(&style_btn);
103+
lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL));
104+
lv_style_set_text_color(&style_btn, lv_color_white());
105+
106+
btnm = lv_btnmatrix_create(lv_scr_act());
107+
lv_obj_set_size(btnm, 240, 240);
108+
lv_btnmatrix_set_map(btnm, btnm_map);
109+
lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0);
110+
111+
lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS);
112+
lv_obj_add_style(btnm, &style_focus, LV_PART_ITEMS | LV_STATE_FOCUS_KEY);
113+
114+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
115+
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED);
116+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE);
117+
118+
lv_btnmatrix_set_one_checked(btnm, true);
119+
lv_btnmatrix_set_selected_btn(btnm, 0);
120+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
121+
122+
counter = lv_label_create(btnm);
123+
lv_label_set_text_fmt(counter, "Counter: %d" , 0);
124+
lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80);
125+
126+
lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL);
127+
128+
Braccio.connectJoystickTo(btnm);
129+
}
130+
131+
void setup() {
132+
Braccio.begin(mainMenu);
133+
delay(500);
134+
135+
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
136+
delay(500);
137+
138+
Serial.begin(115200);
139+
Serial.println("Replicate a movement");
140+
}
141+
142+
void loop() {
143+
if (state == RECORD) {
144+
Braccio.positions(idx);
145+
idx += 6;
146+
}
147+
if (state == REPLAY) {
148+
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
149+
idx += 6;
150+
if (idx >= final_idx) {
151+
Serial.println("REPLAY done");
152+
state = ZERO_POSITION;
153+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
154+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
155+
}
156+
}
157+
if (idx - values >= sizeof(values)) {
158+
Serial.println("ZERO_POSITION");
159+
state = ZERO_POSITION;
160+
}
161+
delay(100);
162+
if (state != ZERO_POSITION) {
163+
lv_label_set_text_fmt(counter, "Counter: %d" , idx - values);
164+
}
165+
}

0 commit comments

Comments
 (0)