diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino b/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino new file mode 100644 index 0000000..1cc8416 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino @@ -0,0 +1,20 @@ + #include "Braccio++.h" + +void customMenu() { + lv_obj_t * btn1 = lv_btn_create(lv_scr_act()); + lv_obj_set_size(btn1, 120, 75); + lv_obj_t * label1 = lv_label_create(btn1); + lv_label_set_text(label1, "BTN 1"); + lv_obj_align(btn1, LV_ALIGN_CENTER, 0, 0); + lv_obj_center(label1); +} + +void setup() { + // put your setup code here, to run once: + Braccio.begin(customMenu); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/sketch.json b/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/sketch.json new file mode 100644 index 0000000..45332f0 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/sketch.json @@ -0,0 +1,7 @@ +{ + "cpu": { + "fqbn": "arduino:mbed_nano:nanorp2040connect", + "name": "Arduino Nano RP2040 Connect", + "port": "serial:///dev/ttyACM0" + } +} \ No newline at end of file diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino new file mode 100644 index 0000000..8818453 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino @@ -0,0 +1,39 @@ +#include "Braccio++.h" + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + + +void customMenu() { + static lv_style_t style; + lv_style_init(&style); + lv_style_set_bg_color(&style, lv_color_hex(COLOR_WHITE)); + lv_style_set_border_color(&style, lv_color_hex(COLOR_TEAL)); + lv_style_set_border_width(&style, 5); + lv_style_set_text_color(&style, lv_color_hex(COLOR_ORANGE)); + + lv_obj_t * btn1 = lv_btn_create(lv_scr_act()); + lv_obj_set_size(btn1, 120, 75); + + lv_obj_t * label1 = lv_label_create(btn1); + lv_label_set_text(label1, "BTN 1"); + + lv_obj_align(btn1, LV_ALIGN_CENTER, 0, 0); + lv_obj_center(label1); + + lv_obj_add_style(btn1, &style, 0); +} + +void setup() { + // put your setup code here, to run once: + Braccio.begin(customMenu); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/btn_style.ino.NANO_RP2040_CONNECT.bin b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/btn_style.ino.NANO_RP2040_CONNECT.bin new file mode 100755 index 0000000..00d92a9 Binary files /dev/null and b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/btn_style.ino.NANO_RP2040_CONNECT.bin differ diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/sketch.json b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/sketch.json new file mode 100644 index 0000000..abdab11 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/sketch.json @@ -0,0 +1,6 @@ +{ + "cpu": { + "fqbn": "arduino:mbed_nano:nanorp2040connect", + "port": "" + } +} \ No newline at end of file diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino b/examples/tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino new file mode 100644 index 0000000..1a4f832 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino @@ -0,0 +1,46 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +static const char * btnm_map[] = {"OPTION 1", "OPTION 2", "\n", + "OPTION 3", "OPTION 4", "\0" + }; + +void customMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_WHITE)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); +} + +void setup() { + // put your setup code here, to run once: + Braccio.begin(customMenu); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino b/examples/tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino new file mode 100644 index 0000000..1d4a705 --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino @@ -0,0 +1,46 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 + +static const char * btnm_map[] = {"OPTION 1", "OPTION 2", "OPTION 3", "\n", + "OPTION 4", "OPTION 5", "\0" + }; + +void customMenu(){ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(0xFFFFFF)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_btnmatrix_set_btn_width(btnm1, 3, 2); // Make "Option 4" twice as wide as "Option 5" + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); +} + +void setup() { + // put your setup code here, to run once: + Braccio.begin(customMenu); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino b/examples/tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino new file mode 100644 index 0000000..50e760c --- /dev/null +++ b/examples/tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino @@ -0,0 +1,46 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +#define COLOR_BG 0xA69F80 +#define COLOR_BTN 0xF2C36B +#define COLOR_BORDER 0x591202 +#define COLOR_TEXT 0xA65221 + +static const char * btnm_map[] = {"BTN 1", "\n", + "BTN 2", "BTN 3", "\n", + "BTN 4", "BTN 5", "\0" + }; + +void customMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_BG)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_BTN)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_BORDER)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEXT)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_btnmatrix_set_btn_width(btnm1, 1, 2); // Make "Button 2" twice as wide as "Button 3" + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); +} + +void setup() { + // put your setup code here, to run once: + Braccio.begin(customMenu); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/examples/tutorials/lessons/02-navigatting-the-display-menu/01_playing_with_the_Joystick/01_playing_with_the_Joystick.ino b/examples/tutorials/lessons/02-navigatting-the-display-menu/01_playing_with_the_Joystick/01_playing_with_the_Joystick.ino new file mode 100644 index 0000000..db53831 --- /dev/null +++ b/examples/tutorials/lessons/02-navigatting-the-display-menu/01_playing_with_the_Joystick/01_playing_with_the_Joystick.ino @@ -0,0 +1,30 @@ +#include "Braccio++.h" + +String message = ""; + +String checkJoystick(int input){ + String joystick[] = { "", + "Joystick left was moved!", + "Joystick right was moved!", + "Joystick select button was pressed!", + "Joystick up was moved!", + "Joystick down was moved!"}; + return joystick[input]; +} + +void setup() { + Serial.begin(115200); + while(!Serial){} + Braccio.begin(); + Serial.println("Press any button or move the joystick."); +} + +void loop() { + int joystickPos = Braccio.getKey(); + message = checkJoystick(joystickPos); + if(message != ""){ + Serial.println(message); + message = ""; + } + delay(500); +} diff --git a/examples/tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino b/examples/tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino new file mode 100644 index 0000000..e1c118c --- /dev/null +++ b/examples/tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino @@ -0,0 +1,70 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +static const char * btnm_map[] = {"Option 1", "Option 2", "\n", + "Option 3", "Option 4", "\n", + "Option 5", "Option 6", "\0" + }; + +static void eventHandler(lv_event_t * e){ + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + if (code == LV_EVENT_PRESSED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + LV_LOG_USER("%s was selected\n", txt); + Serial.println(String(txt) + " was selected."); + } +} + +void customMenu(){ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_WHITE)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm1, true); + + lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); + + Braccio.connectJoystickTo(btnm1); +} + +void setup() { + Braccio.begin(customMenu); +} + +void loop() { + +} diff --git a/examples/tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino b/examples/tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino new file mode 100644 index 0000000..8a25ba3 --- /dev/null +++ b/examples/tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino @@ -0,0 +1,70 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +static const char * btnm_map[] = {"Option 1", "Option 2", "\n", + "Option 3", "Option 4", "\n", + "Option 5", "Option 6", "\0" + }; + +static void eventHandler(lv_event_t * e){ + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + if (code == LV_EVENT_PRESSING) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + LV_LOG_USER("%s is pressed\n", txt); + Serial.println(String(txt) + " is pressed."); + } +} + +void customMenu(){ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_WHITE)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm1, true); + + lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); + + Braccio.connectJoystickTo(btnm1); +} + +void setup() { + Braccio.begin(customMenu); +} + +void loop() { + +} diff --git a/examples/tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino b/examples/tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino new file mode 100644 index 0000000..f3a9ca9 --- /dev/null +++ b/examples/tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino @@ -0,0 +1,70 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +// Arduino Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +static const char * btnm_map[] = {"Option 1", "Option 2", "\n", + "Option 3", "Option 4", "\n", + "Option 5", "Option 6", "\0" + }; + +static void eventHandler(lv_event_t * e){ + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + if (code == LV_EVENT_RELEASED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + LV_LOG_USER("%s was released\n", txt); + Serial.println(String(txt) + " was released."); + } +} + +void customMenu(){ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_WHITE)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + + lv_obj_add_style(btnm1, &style_bg, 0); + lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm1, true); + + lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); + + Braccio.connectJoystickTo(btnm1); +} + +void setup() { + Braccio.begin(customMenu); +} + +void loop() { + +} diff --git a/examples/tutorials/lessons/03-playing-with-the-motors/01_playing_with_the_motors/01_playing_with_the_motors.ino b/examples/tutorials/lessons/03-playing-with-the-motors/01_playing_with_the_motors/01_playing_with_the_motors.ino new file mode 100644 index 0000000..faa1e04 --- /dev/null +++ b/examples/tutorials/lessons/03-playing-with-the-motors/01_playing_with_the_motors/01_playing_with_the_motors.ino @@ -0,0 +1,41 @@ +#include "Braccio++.h" + +int motorID= 0; + +void setup() { + Braccio.begin(); + delay(500); + + Serial.begin(115200); + while(!Serial){} +} + +void loop() { + Serial.println("To start, send any key to the serial port:"); + while((Serial.available() <= 0)){}; + Serial.read(); // Clear the serial buffer + + for(motorID = 1; motorID <=6; motorID++){ + Serial.println("\nMoving the motor " + String(motorID)); + Serial.println("Current angle 0.0 (zero)"); + + Braccio.move(motorID).to(0.0f); + delay(2000); // delay between movements + + Serial.println("Current angle 90.0"); + Braccio.move(motorID).to(90.0f); + delay(2000); // delay between movements + + Serial.println("Current angle 180.0"); + Braccio.move(motorID).to(180.0f); + delay(2000); // delay between movements + + Serial.println("Current angle 270.0"); + Braccio.move(motorID).to(270.0f); + delay(2000); // delay between movements + + Serial.println("Current angle 315.0\n"); + Braccio.move(motorID).to(315.0f); + delay(2000); // delay between movements + } +} diff --git a/examples/tutorials/lessons/03-playing-with-the-motors/02_selecting_the_motor_with_the_enter_button/02_selecting_the_motor_with_the_enter_button.ino b/examples/tutorials/lessons/03-playing-with-the-motors/02_selecting_the_motor_with_the_enter_button/02_selecting_the_motor_with_the_enter_button.ino new file mode 100644 index 0000000..861dd5b --- /dev/null +++ b/examples/tutorials/lessons/03-playing-with-the-motors/02_selecting_the_motor_with_the_enter_button/02_selecting_the_motor_with_the_enter_button.ino @@ -0,0 +1,66 @@ +#include "Braccio++.h" + +#define BUTTON_ENTER 6 + +int motorID = 0; +boolean movement = false; +float initialAngle = 0.0; + +void setup() { + Braccio.begin(); + + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for(int i = 1; i <= 6; i++){ + Braccio.move(i).to(0.0f); + delay(1000); // Necessary to set the motor ID correctly + } + + Serial.begin(115200); + + Serial.println("Press the Enter Button to select the motor 1."); +} + +void loop() { + // Check if the control key pressed is the Enter Button + if(Braccio.getKey() == BUTTON_ENTER){ + + if(motorID <= 6){ + + motorID++; // Increment the ID + + if(motorID > 6){ + motorID = 1; // Restart the ID to motor 1 + } + } + + movement = true; // Flag allows the motor to move + + while(Braccio.getKey() == BUTTON_ENTER); // Avoids more than one increment + Serial.println("Motor " + String(motorID) + " selected."); + } + + if(movement){ + + for (float angle = 0.0; angle <= 315.0; angle+=45.0){ + Braccio.move(motorID).to(angle); + + Serial.println("Motor " + String(motorID) + " - current angle: " + String(angle)); + delay(250); + } + + delay(500); + + for (float angle = 315.0; angle >= 0.0; angle-=45.0){ + Braccio.move(motorID).to(angle); + + Serial.println("Motor " + String(motorID) + " - current angle: " + String(angle)); + delay(250); + } + + movement = false; + + Serial.println("\n\nPress the Enter Button to select the next motor.\n\n"); + } +} diff --git a/examples/tutorials/lessons/03-playing-with-the-motors/03_moving_the_motors_with_the_joystick/03_moving_the_motors_with_the_joystick.ino b/examples/tutorials/lessons/03-playing-with-the-motors/03_moving_the_motors_with_the_joystick/03_moving_the_motors_with_the_joystick.ino new file mode 100644 index 0000000..30ad751 --- /dev/null +++ b/examples/tutorials/lessons/03-playing-with-the-motors/03_moving_the_motors_with_the_joystick/03_moving_the_motors_with_the_joystick.ino @@ -0,0 +1,63 @@ +#include "Braccio++.h" + +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +int motorID = 0; +float initialAngle = SmartServoClass::MAX_ANGLE / 2.0f; // 315 degrees / 2 = 157.5 degrees +float currentAngle = initialAngle; + +void setup() { + Braccio.begin(); // Initialize Braccio + + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for(int i = 1; i <= 6; i++){ + Braccio.move(i).to(initialAngle); + delay(1000); // Necessary to set the motor ID correctly + } + + Serial.begin(115200); + + Serial.println("Press the Enter Button to select the motor 1."); +} + +void loop() { + int pressedKey = Braccio.getKey(); + + // Check if the control key pressed is the Enter Button + if(pressedKey == BUTTON_ENTER){ + if(motorID < 6){ + + motorID++; // Increment the ID + + } else { + motorID = 1; // Restart the ID to motor 1 + } + + currentAngle = initialAngle; + + while(Braccio.getKey() == BUTTON_ENTER); // Avoids more than one increment + + Serial.println("Motor " + String(motorID) + " selected."); + Serial.println("Use the left and right joystick to move the selected motor."); + } + + // Check if the Joystick Left is pressed + if(pressedKey == JOYSTICK_LEFT){ + currentAngle = currentAngle + 5.00; + } + + // Check if the Joystick Right is pressed + if(pressedKey == JOYSTICK_RIGHT){ + currentAngle = currentAngle - 5.00; + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/lessons/03-playing-with-the-motors/04_servo_motors_challenge/04_servo_motors_challenge.ino b/examples/tutorials/lessons/03-playing-with-the-motors/04_servo_motors_challenge/04_servo_motors_challenge.ino new file mode 100644 index 0000000..11302ff --- /dev/null +++ b/examples/tutorials/lessons/03-playing-with-the-motors/04_servo_motors_challenge/04_servo_motors_challenge.ino @@ -0,0 +1,77 @@ +#include "Braccio++.h" + +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +int motorID = 0; +float initialAngle = SmartServoClass::MAX_ANGLE / 2.0f; +float currentAngle = initialAngle; + +void setup() { + Braccio.begin(); + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for (int i = 1; i <= 6; i++) { + Braccio.move(i).to(initialAngle); + delay(1000); // Necessary to set the motor ID correctly + } + + Serial.begin(115200); + while (!Serial) {} + + Serial.println("Press the up and down joystick to select one motor."); +} + +void loop() { + int pressedKey = Braccio.getKey(); + + // Check if the control key pressed is the Joystick Up + if (pressedKey == JOYSTICK_UP) { + + if (motorID < 6) { + motorID++; // Increment the ID + } else { + motorID = 1; // Restart the ID to motor 1 + } + + currentAngle = initialAngle; + + while (Braccio.getKey() == JOYSTICK_UP); // Avoids more than one increment + + Serial.println("Motor " + String(motorID) + " selected."); + Serial.println("Use the left and right joystick to move the selected motor."); + } + + // Check if the control key pressed is the Joystick Down + if (pressedKey == JOYSTICK_DOWN) { + + if (motorID > 1) { + motorID--; // Increment the ID + } else { + motorID = 6; // Select motor 6 + } + + while (Braccio.getKey() == JOYSTICK_DOWN); // Avoids more than one increment + + Serial.println("Motor " + String(motorID) + " selected."); + Serial.println("Use the left and right joystick to move the selected motor."); + } + + // Check if the Joystick Left is pressed + if (pressedKey == JOYSTICK_LEFT) { + currentAngle = currentAngle + 5.00; + } + + // Check if the Joystick Right is pressed + if (pressedKey == JOYSTICK_RIGHT) { + currentAngle = currentAngle - 5.00; + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/lessons/04-integration-of-previous-learnings/01_playing_with_a_joint_angle_gauge/01_playing_with_a_joint_angle_gauge.ino b/examples/tutorials/lessons/04-integration-of-previous-learnings/01_playing_with_a_joint_angle_gauge/01_playing_with_a_joint_angle_gauge.ino new file mode 100644 index 0000000..bad15f8 --- /dev/null +++ b/examples/tutorials/lessons/04-integration-of-previous-learnings/01_playing_with_a_joint_angle_gauge/01_playing_with_a_joint_angle_gauge.ino @@ -0,0 +1,97 @@ +#include "Braccio++.h" + +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 + +int motorID = 1; +float initialAngle = 0.0; +float currentAngle = initialAngle; +float angles[6]; + +static lv_obj_t * meter; +lv_meter_indicator_t * indic; + + +static void eventHandlerMeter(lv_event_t * e) { + /* Set the meter value */ + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); +} + + +void meterScreen(void) +{ + meter = lv_meter_create(lv_scr_act()); + + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Remove the circle from the middle*/ + lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR); + + static lv_style_t style_meter; + lv_style_init(&style_meter); + lv_style_set_text_color(&style_meter, lv_color_hex(COLOR_TEAL)); + lv_obj_add_style(meter, &style_meter, 0); + + /*Add a scale first*/ + lv_meter_scale_t * scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 8, 2, 10, lv_color_hex(COLOR_ORANGE)); + lv_meter_set_scale_major_ticks(meter, scale, 1, 2, 20, lv_color_hex(COLOR_ORANGE), 20); + lv_meter_set_scale_range(meter, scale, 0, 315, 315, 90); + + /*Add a arc indicator*/ + indic = lv_meter_add_arc(meter, scale, 10, lv_color_hex(COLOR_LIGHT_TEAL), 0); + + lv_obj_add_event_cb(meter, eventHandlerMeter, LV_EVENT_KEY, NULL); + + Braccio.connectJoystickTo(meter); +} + +void setup() { + Braccio.begin(meterScreen); + + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for (int i = 1; i <= 6; i++) { + Braccio.move(i).to(initialAngle); + delay(1000); // Necessary to set the motor ID correctly + } +} + +void loop() { + int pressedKey = Braccio.getKey(); + + Braccio.positions(angles); + currentAngle = angles[motorID - 1]; + + // Check if the Joystick Left is pressed + if (pressedKey == JOYSTICK_LEFT) { + if (currentAngle <= 315 ) { + currentAngle += 15.0; + } else { + currentAngle = 315.0; + } + } + + // Check if the Joystick Right is pressed + if (pressedKey == JOYSTICK_RIGHT) { + if (currentAngle > 15 ) { + currentAngle -= 15.0; + } else { + currentAngle = 0.0; + } + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/lessons/04-integration-of-previous-learnings/02_selecting_the_motor_in_the_LCD_menu/02_selecting_the_motor_in_the_LCD_menu.ino b/examples/tutorials/lessons/04-integration-of-previous-learnings/02_selecting_the_motor_in_the_LCD_menu/02_selecting_the_motor_in_the_LCD_menu.ino new file mode 100644 index 0000000..04ad83f --- /dev/null +++ b/examples/tutorials/lessons/04-integration-of-previous-learnings/02_selecting_the_motor_in_the_LCD_menu/02_selecting_the_motor_in_the_LCD_menu.ino @@ -0,0 +1,161 @@ +#include "Braccio++.h" + +// Joystick +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +// Variables +int motorID = 0; // Selected motor ID +float initialAngle = SmartServoClass::MAX_ANGLE / 2.0f; +float currentAngle = initialAngle; // Selected motor current angle +float angles[6]; // All motors current angles + +static const char * btnm_map[] = {"Motor 1", "Motor 2", "\n", + "Motor 3", "Motor 4", "\n", + "Motor 5", "Motor 6", "\0" + }; + +lv_obj_t * btnm; // Motors button matrix +lv_obj_t * meter; // Gauge with selected motor angle +lv_meter_indicator_t * indic; // Indication of selected motor angle + + +// Event Handlers +static void eventHandlerMeter(lv_event_t * e) { + uint32_t pressed_key = Braccio.getKey(); + + if (pressed_key == BUTTON_ENTER) { + motorID = 0; // No motor selected + motorMenu(); // Load motor menu screen + lv_obj_del(meter); // Delete the object + } + else { + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + } +} + +static void eventHandlerMenu(lv_event_t * e) { + lv_obj_t * obj = lv_event_get_target(e); + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + + motorID = id + 1; + currentAngle = angles[motorID - 1]; + meterScreen(); // Load meter screen + lv_obj_del(btnm); // Delete the object +} + +// Screens functions +void meterScreen(void) +{ + meter = lv_meter_create(lv_scr_act()); + + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Remove the circle from the middle*/ + lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR); + + static lv_style_t style_meter; + lv_style_init(&style_meter); + lv_style_set_text_color(&style_meter, lv_color_hex(COLOR_TEAL)); + lv_obj_add_style(meter, &style_meter, 0); + + /*Add a scale first*/ + lv_meter_scale_t * scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 8, 2, 10, lv_color_hex(COLOR_ORANGE)); + lv_meter_set_scale_major_ticks(meter, scale, 1, 2, 20, lv_color_hex(COLOR_ORANGE), 20); + lv_meter_set_scale_range(meter, scale, 0, 315, 315, 90); + + /*Add a arc indicator*/ + indic = lv_meter_add_arc(meter, scale, 10, lv_color_hex(COLOR_LIGHT_TEAL), 0); + + lv_obj_add_event_cb(meter, eventHandlerMeter, LV_EVENT_KEY, NULL); + + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + + Braccio.connectJoystickTo(meter); +} + +void motorMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_WHITE)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_bg, 0); + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_PRESSED, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(motorMenu); + + delay(5000); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for (int i = 1; i <= 6; i++) { + Braccio.move(i).to(initialAngle); + delay(1500); // Necessary to set the motor ID correctly + } +} + +void loop() { + int pressedKey = Braccio.getKey(); + + Braccio.positions(angles); + currentAngle = angles[motorID - 1]; + + // Check if the Joystick Left is pressed + if (pressedKey == JOYSTICK_LEFT) { + if (currentAngle < 315 ) { + currentAngle += 15.00; + } else { + currentAngle = 315; + } + } + + // Check if the Joystick Right is pressed + if (pressedKey == JOYSTICK_RIGHT) { + if (currentAngle > 15 ) { + currentAngle -= 15.00; + } else { + currentAngle = 0; + } + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/lessons/04-integration-of-previous-learnings/03_learnings_challenge_I/03_learnings_challenge_I.ino b/examples/tutorials/lessons/04-integration-of-previous-learnings/03_learnings_challenge_I/03_learnings_challenge_I.ino new file mode 100644 index 0000000..517e3da --- /dev/null +++ b/examples/tutorials/lessons/04-integration-of-previous-learnings/03_learnings_challenge_I/03_learnings_challenge_I.ino @@ -0,0 +1,161 @@ +#include "Braccio++.h" + +// Joystick +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +// Variables +int motorID = 0; // Selected motor ID +float initialAngle = 0.0; +float currentAngle = initialAngle; // Selected motor current angle +float angles[6]; // All motors current angles + +static const char * btnm_map[] = {"Motor 1", "Motor 2", "\n", + "Motor 3", "Motor 4", "\n", + "Motor 5", "Motor 6", "\0" + }; + +lv_obj_t * btnm; // Motors button matrix +lv_obj_t * meter; // Gauge with selected motor angle +lv_meter_indicator_t * indic; // Indication of selected motor angle + + +// Event Handlers +static void eventHandlerMeter(lv_event_t * e) { + uint32_t pressed_key = Braccio.getKey(); + + if (pressed_key == BUTTON_ENTER) { + motorID = 0; // No motor selected + motorMenu(); // Load motor menu screen + lv_obj_del(meter); // Delete the object + } + else { + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + } +} + +static void eventHandlerMenu(lv_event_t * e) { + lv_obj_t * obj = lv_event_get_target(e); + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + + motorID = id + 1; + currentAngle = angles[motorID - 1]; + meterScreen(); + lv_obj_del(btnm); +} + +// Screens functions +void meterScreen(void) +{ + meter = lv_meter_create(lv_scr_act()); + + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Remove the circle from the middle*/ + lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR); + + static lv_style_t style_meter; + lv_style_init(&style_meter); + lv_style_set_text_color(&style_meter, lv_color_hex(COLOR_TEAL)); + lv_obj_add_style(meter, &style_meter, 0); + + /*Add a scale first*/ + lv_meter_scale_t * scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 22, 2, 10, lv_color_hex(COLOR_ORANGE)); + lv_meter_set_scale_major_ticks(meter, scale, 1, 2, 20, lv_color_hex(COLOR_ORANGE), 20); + lv_meter_set_scale_range(meter, scale, 0, 315, 315, 90); + + /*Add a arc indicator*/ + indic = lv_meter_add_arc(meter, scale, 10, lv_color_hex(COLOR_LIGHT_TEAL), 0); + + lv_obj_add_event_cb(meter, eventHandlerMeter, LV_EVENT_KEY, NULL); + + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + + Braccio.connectJoystickTo(meter); +} + +void motorMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_WHITE)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_bg, 0); + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_PRESSED, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(motorMenu); + + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for (int i = 1; i <= 6; i++) { + Braccio.move(i).to(initialAngle); + delay(1000); // Necessary to set the motor ID correctly + } +} + +void loop() { + int pressedKey = Braccio.getKey(); + + Braccio.positions(angles); + currentAngle = angles[motorID - 1]; + + // Check if the Joystick Left is pressed + if (pressedKey == JOYSTICK_LEFT) { + if (currentAngle < 315 ) { + currentAngle += 10.00; + } else { + currentAngle = 315; + } + } + + // Check if the Joystick Right is pressed + if (pressedKey == JOYSTICK_RIGHT) { + if (currentAngle > 15 ) { + currentAngle -= 10.00; + } else { + currentAngle = 0; + } + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/lessons/04-integration-of-previous-learnings/04_learnings_challenge_II/04_learnings_challenge_II.ino b/examples/tutorials/lessons/04-integration-of-previous-learnings/04_learnings_challenge_II/04_learnings_challenge_II.ino new file mode 100644 index 0000000..3593e5b --- /dev/null +++ b/examples/tutorials/lessons/04-integration-of-previous-learnings/04_learnings_challenge_II/04_learnings_challenge_II.ino @@ -0,0 +1,161 @@ +#include "Braccio++.h" + +// Joystick +#define JOYSTICK_LEFT 1 +#define JOYSTICK_RIGHT 2 +#define JOYSTICK_SEL_BUTTON 3 +#define JOYSTICK_UP 4 +#define JOYSTICK_DOWN 5 +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_ORANGE 0xE47128 +#define COLOR_YELLOW 0xE5AD24 +#define COLOR_WHITE 0xFFFFFF + +// Variables +int motorID = 0; // Selected motor ID +float initialAngle = 0.0; +float currentAngle = initialAngle; // Selected motor current angle +float angles[6]; // All motors current angles + +static const char * btnm_map[] = {"Motor 1", "Motor 2", "\n", + "Motor 3", "Motor 4", "\n", + "Motor 5", "Motor 6", "\0" + }; + +lv_obj_t * btnm; // Motors button matrix +lv_obj_t * meter; // Gauge with selected motor angle +lv_meter_indicator_t * indic; // Indication of selected motor angle + + +// Event Handlers +static void eventHandlerMeter(lv_event_t * e) { + uint32_t pressed_key = Braccio.getKey(); + + if (pressed_key == BUTTON_ENTER) { + motorID = 0; // No motor selected + motorMenu(); // Load motor menu screen + lv_obj_del(meter); // Delete the object + } + else { + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + } +} + +static void eventHandlerMenu(lv_event_t * e) { + lv_obj_t * obj = lv_event_get_target(e); + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + + motorID = id + 1; + currentAngle = angles[motorID - 1]; + meterScreen(); + lv_obj_del(btnm); +} + +// Screens functions +void meterScreen(void) +{ + meter = lv_meter_create(lv_scr_act()); + + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Remove the circle from the middle*/ + lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR); + + static lv_style_t style_meter; + lv_style_init(&style_meter); + lv_style_set_text_color(&style_meter, lv_color_hex(COLOR_TEAL)); + lv_obj_add_style(meter, &style_meter, 0); + + /*Add a scale first*/ + lv_meter_scale_t * scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 22, 2, 10, lv_color_hex(COLOR_ORANGE)); + lv_meter_set_scale_major_ticks(meter, scale, 3, 2, 20, lv_color_hex(COLOR_ORANGE), 20); + lv_meter_set_scale_range(meter, scale, 0, 315, 315, 90); + + /*Add a arc indicator*/ + indic = lv_meter_add_arc(meter, scale, 10, lv_color_hex(COLOR_LIGHT_TEAL), 0); + + lv_obj_add_event_cb(meter, eventHandlerMeter, LV_EVENT_KEY, NULL); + + lv_meter_set_indicator_end_value(meter, indic, (int32_t)angles[motorID - 1]); + + Braccio.connectJoystickTo(meter); +} + +void motorMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_WHITE)); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_bg, 0); + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_PRESSED, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(motorMenu); + + delay(500); // Waits for the Braccio initialization + + // Sets the initial angle for the motors + for (int i = 1; i <= 6; i++) { + Braccio.move(i).to(initialAngle); + delay(1000); // Necessary to set the motor ID correctly + } +} + +void loop() { + int pressedKey = Braccio.getKey(); + + Braccio.positions(angles); + currentAngle = angles[motorID - 1]; + + // Check if the Joystick Left is pressed + if (pressedKey == JOYSTICK_LEFT) { + if (currentAngle < 315 ) { + currentAngle += 10.00; + } else { + currentAngle = 315; + } + } + + // Check if the Joystick Right is pressed + if (pressedKey == JOYSTICK_RIGHT) { + if (currentAngle > 15 ) { + currentAngle -= 10.00; + } else { + currentAngle = 0; + } + } + + Braccio.move(motorID).to(currentAngle); // Moves the motor to the new angle + delay(100); +} diff --git a/examples/tutorials/projects/p01-moving-braccio/01_aligning_braccio/01_aligning_braccio.ino b/examples/tutorials/projects/p01-moving-braccio/01_aligning_braccio/01_aligning_braccio.ino new file mode 100644 index 0000000..bf7f178 --- /dev/null +++ b/examples/tutorials/projects/p01-moving-braccio/01_aligning_braccio/01_aligning_braccio.ino @@ -0,0 +1,60 @@ +#include "Braccio++.h" + +// Variables +// Braccio ++ joints +auto gripper = Braccio.get(1); +auto wristRoll = Braccio.get(2); +auto wristPitch = Braccio.get(3); +auto elbow = Braccio.get(4); +auto shoulder = Braccio.get(5); +auto base = Braccio.get(6); + +float initialGripper = 160.0; +float initialBase = 90.0; +float initialAngle = 150.0; +float angles[6]; + +void setup() { + Braccio.begin(); + delay(500); // Waits for the Braccio initialization + + // You can choose the speed beforehand with + Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW + + // Send motors initial angle + gripper.move().to(initialGripper); + delay(100); + wristRoll.move().to(initialAngle); + delay(100); + wristPitch.move().to(initialAngle); + delay(100); + elbow.move().to(initialAngle); + delay(100); + shoulder.move().to(initialAngle); + delay(100); + base.move().to(initialBase); + delay(100); + + Serial.begin(115200); + while(!Serial){} +} + +void loop() { + // Fetch the joints positions + Braccio.positions(angles); + + // Print the joint angles + Serial.println("************* Joints Angles *************"); + Serial.println("|\tMotor ID\t|\tAngle\t|"); + Serial.println("----------------------------------------"); + Serial.print("| 1 - Gripper\t\t|\t" + String(angles[0]) + "\t|\n" + + "| 2 - Wrist Rotation\t|\t" + String(angles[1]) + "\t|\n" + + "| 3 - Wrist Vertical\t|\t" + String(angles[2]) + "\t|\n" + + "| 4 - Elbow\t\t|\t" + String(angles[3]) + "\t|\n" + + "| 5 - Shoulder\t\t|\t" + String(angles[4]) + "\t|\n" + + "| 6 - Base\t\t|\t" + String(angles[5]) + "\t|\n"); + Serial.println("*****************************************\n\n\n\n\n"); + Serial.println("\n\n\n\n"); + + delay(1000); +} diff --git a/examples/tutorials/projects/p01-moving-braccio/02_waving_with_Braccio/02_waving_with_Braccio.ino b/examples/tutorials/projects/p01-moving-braccio/02_waving_with_Braccio/02_waving_with_Braccio.ino new file mode 100644 index 0000000..57e081a --- /dev/null +++ b/examples/tutorials/projects/p01-moving-braccio/02_waving_with_Braccio/02_waving_with_Braccio.ino @@ -0,0 +1,55 @@ +#include "Braccio++.h" + +#define BUTTON_ENTER 6 + +// Braccio ++ joints +auto gripper = Braccio.get(1); +auto wristRoll = Braccio.get(2); +auto wristPitch = Braccio.get(3); +auto elbow = Braccio.get(4); +auto shoulder = Braccio.get(5); +auto base = Braccio.get(6); + +/* Variables */ +// initialAngles[6] = {gripper, wristRoll, wristPitch, elbow, shoulder, base} +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; +float wavePos[6] = {180.0, 250.0, 145.0, 150.0, 150.0, 90.0}; + +bool movement = false; // Flag to initialize joints' movements + +void setup() { + Braccio.begin(); + delay(500); // Waits for the Braccio initialization + + // You can choose the speed beforehand with + Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW + + // Set motors initial angle + // Should move all the motors at once + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + delay(500); +} + +// Waving whit Wrist pitch +void loop() { + int pressedKey = Braccio.getKey(); + + if (pressedKey == BUTTON_ENTER) + movement = true; // Trigger joints' movements + + if (movement) { + Braccio.moveTo(wavePos[0], wavePos[1], wavePos[2], wavePos[3], wavePos[4], wavePos[5]); + delay(1000); + + for (int i = 1; i <= 10; i++) { + wristPitch.move().to(100.0f); + delay(300); + wristPitch.move().to(190.0f); + delay(600); + wristPitch.move().to(145.0f); + delay(300); + } + + movement = false; // Stop joints' movements + } +} diff --git a/examples/tutorials/projects/p01-moving-braccio/03_moving_challenge/03_moving_challenge.ino b/examples/tutorials/projects/p01-moving-braccio/03_moving_challenge/03_moving_challenge.ino new file mode 100644 index 0000000..5667946 --- /dev/null +++ b/examples/tutorials/projects/p01-moving-braccio/03_moving_challenge/03_moving_challenge.ino @@ -0,0 +1,53 @@ +#include "Braccio++.h" + +#define BUTTON_ENTER 6 + +// Braccio ++ joints +auto gripper = Braccio.get(1); +auto wristRoll = Braccio.get(2); +auto wristPitch = Braccio.get(3); +auto elbow = Braccio.get(4); +auto shoulder = Braccio.get(5); +auto base = Braccio.get(6); + +/* Variables */ +// initialAngles[6] = {gripper, wristRoll, wristPitch, elbow, shoulder, base} +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; + +bool movement = false; // Flag to initialize joints' movements + +void setup() { + Braccio.begin(); + delay(500); // Waits for the Braccio initialization + + // You can choose the speed beforehand with + Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW + + // Set motors initial angle + // Should move all the motors at once + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + delay(500); +} + +void loop() { + int pressedKey = Braccio.getKey(); + + if (pressedKey == BUTTON_ENTER) + movement = true; // Trigger joints' movements + + if (movement) { + Braccio.moveTo(160.0, 150.0, 220.0, 220.0, 100.0, 270.0); + delay(1000); + + for (int i = 1; i <= 10; i++) { + wristPitch.move().to(190.0f); + delay(200); + wristPitch.move().to(250.0f); + delay(400); + wristPitch.move().to(220.0f); + delay(200); + } + + movement = false; // Stop joints' moviments + } +} diff --git a/examples/tutorials/projects/p02-controlling-braccio-manually/01_controlling_manually_Braccio/01_controlling_manually_Braccio.ino b/examples/tutorials/projects/p02-controlling-braccio-manually/01_controlling_manually_Braccio/01_controlling_manually_Braccio.ino new file mode 100644 index 0000000..a3adb85 --- /dev/null +++ b/examples/tutorials/projects/p02-controlling-braccio-manually/01_controlling_manually_Braccio/01_controlling_manually_Braccio.ino @@ -0,0 +1,191 @@ +#include "Braccio++.h" + +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_YELLOW 0xE5AD24 + +// Variables +String selectedJoints; +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; +float angles[6]; // All motors current angles + +// Braccio ++ joints +auto gripper = Braccio.get(1); +auto wristRoll = Braccio.get(2); +auto wristPitch = Braccio.get(3); +auto elbow = Braccio.get(4); +auto shoulder = Braccio.get(5); +auto base = Braccio.get(6); + +String jointsPair[] = {"Shoulder", "Elbow", "Wrist"}; + +static const char * btnm_map[] = {"Shoulder", "\n", + "Elbow", "\n", + "Wrist", "\0" + }; + +static const char * directional_map[] = {" ", LV_SYMBOL_UP, " ", "\n", + LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, "\n", + " ", LV_SYMBOL_DOWN, " ", "\0" + }; + +lv_obj_t * btnm; // Joints button matrix +lv_obj_t * directional; // Direction button matrix + + +// Function +void moveJoints(uint32_t btnID) { + if (selectedJoints == "Shoulder") { + switch (btnID) { + case 1: shoulder.move().to(angles[4] - 10.0); break; + case 3: base.move().to(angles[5] - 10.0); break; + case 5: base.move().to(angles[5] + 10.0); break; + case 7: shoulder.move().to(angles[4] + 10.0); break; + default: break; + } + } + + if (selectedJoints == "Elbow") { + switch (btnID) { + case 1: elbow.move().to(angles[3] - 10.0); break; + case 7: elbow.move().to(angles[3] + 10.0); break; + default: break; + } + } + + if (selectedJoints == "Wrist") { + switch (btnID) { + case 1: wristPitch.move().to(angles[2] - 10.0); break; + case 3: wristRoll.move().to(angles[1] - 10.0); break; + case 5: wristRoll.move().to(angles[1] + 10.0); break; + case 7: wristPitch.move().to(angles[2] + 10.0); break; + default: break; + } + } +} + +// Event Handlers +static void eventHandlerMenu(lv_event_t * e) { + lv_obj_t * obj = lv_event_get_target(e); + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + + selectedJoints = jointsPair[id]; + + directionScreen(); + lv_obj_del(btnm); +} + +static void eventHandlerDirectional(lv_event_t * e) { + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + + if (code == LV_EVENT_KEY) { + uint32_t pressed_key = Braccio.getKey(); + + if (pressed_key == BUTTON_ENTER) { + mainMenu(); // Load motor menu screen + lv_obj_del(directional); // Delete the object + } + } + if (code == LV_EVENT_PRESSING) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + Braccio.positions(angles); + delay(5); + moveJoints(id); + } +} + +// Screens functions +void mainMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_white()); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + lv_style_set_text_letter_space(&style_btn, 8); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(btnm, 240, 240); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_bg, 0); + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + lv_btnmatrix_set_selected_btn(btnm, 0); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_PRESSED, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void directionScreen(void) +{ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_white()); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_text_color(&style_btn, lv_color_white()); + + directional = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(directional, 240, 240); + lv_btnmatrix_set_map(directional, directional_map); + lv_obj_align(directional, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(directional, &style_bg, 0); + lv_obj_add_style(directional, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(directional, 0, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 2, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 4, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 6, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 7, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 8, LV_BTNMATRIX_CTRL_HIDDEN); + + if (selectedJoints == "Elbow") { + lv_btnmatrix_set_btn_ctrl(directional, 3, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 5, LV_BTNMATRIX_CTRL_HIDDEN); + } + + lv_btnmatrix_set_one_checked(directional, true); + lv_btnmatrix_set_selected_btn(directional, 1); + + lv_obj_add_event_cb(directional, eventHandlerDirectional, LV_EVENT_ALL, NULL); + + delay(50); + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(mainMenu); + delay(500); // Waits for the Braccio initialization + + Braccio.speed(SLOW); + + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + delay(500); +} + +void loop() { + +} diff --git a/examples/tutorials/projects/p02-controlling-braccio-manually/02_manual_control_challenge/02_manual_control_challenge.ino b/examples/tutorials/projects/p02-controlling-braccio-manually/02_manual_control_challenge/02_manual_control_challenge.ino new file mode 100644 index 0000000..557afb6 --- /dev/null +++ b/examples/tutorials/projects/p02-controlling-braccio-manually/02_manual_control_challenge/02_manual_control_challenge.ino @@ -0,0 +1,203 @@ +#include "Braccio++.h" + +#define BUTTON_ENTER 6 + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 +#define COLOR_YELLOW 0xE5AD24 + +// Variables +String selectedJoints; +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; +float angles[6]; // All motors current angles + +// Braccio ++ joints +auto gripper = Braccio.get(1); +auto wristRoll = Braccio.get(2); +auto wristPitch = Braccio.get(3); +auto elbow = Braccio.get(4); +auto shoulder = Braccio.get(5); +auto base = Braccio.get(6); + +String jointsPair[] = {"Shoulder", "Elbow", "Wrist", "Gripper"}; + +static const char * btnm_map[] = {"Shoulder", "\n", + "Elbow", "\n", + "Wrist", "\n", + "Gripper", "\0" + }; + +static const char * directional_map[] = {" ", LV_SYMBOL_UP, " ", "\n", + LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, "\n", + " ", LV_SYMBOL_DOWN, " ", "\0" + }; + +lv_obj_t * btnm; // Joints button matrix +lv_obj_t * directional; // Direction button matrix + + +// Function +void moveJoints(uint32_t btnID) { + if (selectedJoints == "Shoulder") { + switch (btnID) { + case 1: shoulder.move().to(angles[4] - 10.0); break; + case 3: base.move().to(angles[5] - 10.0); break; + case 5: base.move().to(angles[5] + 10.0); break; + case 7: shoulder.move().to(angles[4] + 10.0); break; + default: break; + } + } + + if (selectedJoints == "Elbow") { + switch (btnID) { + case 1: elbow.move().to(angles[3] - 10.0); break; + case 7: elbow.move().to(angles[3] + 10.0); break; + default: break; + } + } + + if (selectedJoints == "Wrist") { + switch (btnID) { + case 1: wristPitch.move().to(angles[2] - 10.0); break; + case 3: wristRoll.move().to(angles[1] - 10.0); break; + case 5: wristRoll.move().to(angles[1] + 10.0); break; + case 7: wristPitch.move().to(angles[2] + 10.0); break; + default: break; + } + } + + if (selectedJoints == "Gripper") { + switch (btnID) { + case 3: gripper.move().to(angles[0] - 5.0); break; + case 5: gripper.move().to(angles[0] + 5.0); break; + default: break; + } + } +} + +// Event Handlers +static void eventHandlerMenu(lv_event_t * e) { + lv_obj_t * obj = lv_event_get_target(e); + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + + selectedJoints = jointsPair[id]; + + directionScreen(); + lv_obj_del(btnm); +} + +static void eventHandlerDirectional(lv_event_t * e) { + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + + if (code == LV_EVENT_KEY) { + uint32_t pressed_key = Braccio.getKey(); + + if (pressed_key == BUTTON_ENTER) { + mainMenu(); // Load motor menu screen + lv_obj_del(directional); // Delete the object + } + } + if (code == LV_EVENT_PRESSING) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + Braccio.positions(angles); + delay(5); + moveJoints(id); + } +} + +// Screens functions +void mainMenu() { + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_white()); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_YELLOW)); + lv_style_set_border_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_border_width(&style_btn, 2); + lv_style_set_text_color(&style_btn, lv_color_hex(COLOR_TEAL)); + lv_style_set_text_letter_space(&style_btn, 8); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(btnm, 240, 240); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_bg, 0); + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + lv_btnmatrix_set_selected_btn(btnm, 0); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_PRESSED, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void directionScreen(void) +{ + static lv_style_t style_bg; + lv_style_init(&style_bg); + lv_style_set_bg_color(&style_bg, lv_color_white()); + + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_text_color(&style_btn, lv_color_white()); + + directional = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(directional, 240, 240); + lv_btnmatrix_set_map(directional, directional_map); + lv_obj_align(directional, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(directional, &style_bg, 0); + lv_obj_add_style(directional, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(directional, 0, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 2, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 4, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 6, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 7, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(directional, 8, LV_BTNMATRIX_CTRL_HIDDEN); + + if (selectedJoints == "Elbow") { + lv_btnmatrix_set_btn_ctrl(directional, 3, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 5, LV_BTNMATRIX_CTRL_HIDDEN); + } + + if (selectedJoints == "Gripper") { + lv_btnmatrix_set_btn_ctrl(directional, 1, LV_BTNMATRIX_CTRL_HIDDEN); + lv_btnmatrix_set_btn_ctrl(directional, 7, LV_BTNMATRIX_CTRL_HIDDEN); + } + + lv_btnmatrix_set_one_checked(directional, true); + lv_btnmatrix_set_selected_btn(directional, 1); + + lv_obj_add_event_cb(directional, eventHandlerDirectional, LV_EVENT_ALL, NULL); + + delay(150); + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(mainMenu); + delay(500); // Waits for the Braccio initialization + + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + delay(500); +} + +void loop() { + +} diff --git a/examples/tutorials/projects/p03-learning-mode/01_Braccio_learning_mode/01_Braccio_learning_mode.ino b/examples/tutorials/projects/p03-learning-mode/01_Braccio_learning_mode/01_Braccio_learning_mode.ino new file mode 100644 index 0000000..c2b8ab3 --- /dev/null +++ b/examples/tutorials/projects/p03-learning-mode/01_Braccio_learning_mode/01_Braccio_learning_mode.ino @@ -0,0 +1,134 @@ +#include "Braccio++.h" + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 + +enum states { + LEARN, + REPEAT, + IDLE +}; + +int state = IDLE; + +float values[10000]; +float* idx = values; +float* final_idx = 0; +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; + +static lv_obj_t * counter; +static lv_obj_t * btnm; + +static const char * btnm_map[] = { "Learn", "\n", "Replay", "\n", "Idle", "\n", "\0" }; + + +static void eventHandlerMenu(lv_event_t * e) { + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + + if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) { + state = IDLE; + return; + } + + if (code == LV_EVENT_CLICKED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + if (state == LEARN) { + final_idx = idx; + } + + idx = values; + + FILE* f; + switch (id) { + case 0: + state = LEARN; + Braccio.disengage(); + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("LEARN"); + break; + case 1: + state = REPEAT; + Braccio.engage(); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("REPEAT"); + break; + default: + state = IDLE; + delay(500); + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("IDLE"); + break; + } + } +} + +void mainMenu() { + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_text_color(&style_btn, lv_color_white()); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(btnm, 240, 240); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + lv_btnmatrix_set_selected_btn(btnm, 2); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + + counter = lv_label_create(btnm); + lv_label_set_text_fmt(counter, "Counter: %d" , 0); + lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(mainMenu); + delay(500); + + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + delay(500); + + Serial.begin(115200); + Serial.println("Replicate a movement"); +} + +void loop() { + if (state == LEARN) { + Braccio.positions(idx); + idx += 6; + } + if (state == REPEAT) { + Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]); + idx += 6; + if (idx >= final_idx) { + Serial.println("Repeat done"); + state = IDLE; + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + } + } + if (idx - values >= sizeof(values)) { + Serial.println("IDLE"); + state = IDLE; + } + delay(100); + if (state != IDLE) { + lv_label_set_text_fmt(counter, "Counter: %d" , idx - values); + } +} diff --git a/examples/tutorials/projects/p03-learning-mode/02_learning_challenge/02_learning_challenge.ino b/examples/tutorials/projects/p03-learning-mode/02_learning_challenge/02_learning_challenge.ino new file mode 100644 index 0000000..d1eef79 --- /dev/null +++ b/examples/tutorials/projects/p03-learning-mode/02_learning_challenge/02_learning_challenge.ino @@ -0,0 +1,162 @@ +#include "Braccio++.h" + +// Colors +#define COLOR_TEAL 0x00878F +#define COLOR_LIGHT_TEAL 0x62AEB2 + +auto wristPitch = Braccio.get(3); + +enum states { + LEARN, + REPEAT, + IDLE, + DEMO +}; + +int state = IDLE; + +float values[1000]; +float* idx = values; +float* final_idx = 0; +float homePos[6] = {160.0, 150.0, 220.0, 220.0, 100.0, 180.0}; +float wavePos[6] = {180.0, 250.0, 145.0, 150.0, 150.0, 90.0}; + +static lv_obj_t * counter; +static lv_obj_t * btnm; + +static const char * btnm_map[] = { "Learn", "\n", "Replay", "\n", "Idle", "\n", "Demo", "\n", "\0" }; + + +void ciaoMovement() { + for (int i = 1; i <= 10; i++) { + wristPitch.move().to(100.0f); + delay(300); + wristPitch.move().to(190.0f); + delay(600); + wristPitch.move().to(145.0f); + delay(300); + } + + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); +} + + +static void eventHandlerMenu(lv_event_t * e) { + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + + if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) { + state = IDLE; + return; + } + + if (code == LV_EVENT_CLICKED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + if (state == LEARN) { + final_idx = idx; + } + + idx = values; + + FILE* f; + switch (id) { + case 0: + state = LEARN; + Braccio.disengage(); + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("LEARN"); + break; + case 1: + state = REPEAT; + Braccio.engage(); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("REPEAT"); + break; + case 3: + state = DEMO; + Braccio.moveTo(wavePos[0], wavePos[1], wavePos[2], wavePos[3], wavePos[4], wavePos[5]); + lv_btnmatrix_set_btn_ctrl(btnm, 3, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("DEMO"); + break; + default: + state = IDLE; + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + Serial.println("IDLE"); + break; + } + } +} + +void mainMenu() { + static lv_style_t style_btn; + lv_style_init(&style_btn); + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); + lv_style_set_text_color(&style_btn, lv_color_white()); + + btnm = lv_btnmatrix_create(lv_scr_act()); + lv_obj_set_size(btnm, 240, 240); + lv_btnmatrix_set_map(btnm, btnm_map); + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0); + + lv_obj_add_style(btnm, &style_btn, LV_PART_ITEMS); + + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + + lv_btnmatrix_set_one_checked(btnm, true); + lv_btnmatrix_set_selected_btn(btnm, 2); + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + + counter = lv_label_create(btnm); + lv_label_set_text_fmt(counter, "Counter: %d" , 0); + lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80); + + lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL); + + Braccio.connectJoystickTo(btnm); +} + +void setup() { + Braccio.begin(mainMenu); + delay(500); + + Serial.begin(115200); + Serial.println("Replicate a movement"); + + Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]); +} + +void loop() { + if (state == LEARN) { + Braccio.positions(idx); + idx += 6; + } + if (state == REPEAT) { + Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]); + idx += 6; + if (idx >= final_idx) { + Serial.println("Repeat done"); + state = IDLE; + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); + } + } + + if (state == DEMO) { + ciaoMovement(); + state = IDLE; + } + + if (idx - values >= sizeof(values)) { + Serial.println("IDLE"); + state = IDLE; + } + delay(100); + if (state != IDLE) { + lv_label_set_text_fmt(counter, "Counter: %d" , idx - values); + } +}