|
| 1 | +#include "Braccio++.h" |
| 2 | +#include "FlashIAPBlockDevice.h" |
| 3 | +#include "FATFileSystem.h" |
| 4 | + |
| 5 | +enum states { |
| 6 | + LEARN, |
| 7 | + REPEAT, |
| 8 | + IDLE |
| 9 | +}; |
| 10 | + |
| 11 | +int state = IDLE; |
| 12 | + |
| 13 | +float values[10000]; |
| 14 | +float* idx = values; |
| 15 | +float* final_idx = 0; |
| 16 | + |
| 17 | +static void event_handler(lv_event_t * e) { |
| 18 | + lv_event_code_t code = lv_event_get_code(e); |
| 19 | + lv_obj_t * obj = lv_event_get_target(e); |
| 20 | + |
| 21 | + if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) { |
| 22 | + state = IDLE; |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (code == LV_EVENT_CLICKED) { |
| 27 | + uint32_t id = lv_btnmatrix_get_selected_btn(obj); |
| 28 | + const char * txt = lv_btnmatrix_get_btn_text(obj, id); |
| 29 | + |
| 30 | + if (state == LEARN) { |
| 31 | + final_idx = idx; |
| 32 | + } |
| 33 | + |
| 34 | + idx = values; |
| 35 | + |
| 36 | + FILE* f; |
| 37 | + switch (id) { |
| 38 | + case 0: |
| 39 | + state = LEARN; |
| 40 | + Braccio.disengage(); |
| 41 | + Serial.println("LEARN"); |
| 42 | + break; |
| 43 | + case 1: |
| 44 | + state = REPEAT; |
| 45 | + Braccio.engage(); |
| 46 | + Serial.println("REPEAT"); |
| 47 | + break; |
| 48 | + case 2: |
| 49 | + state = IDLE; |
| 50 | + f = fopen("/fs/movements", "wb"); |
| 51 | + fwrite(values, 1, (final_idx - values) * sizeof(float), f); |
| 52 | + fflush(f); |
| 53 | + fclose(f); |
| 54 | + Serial.print("SAVED "); |
| 55 | + Serial.print((final_idx - values) * sizeof(float)); |
| 56 | + Serial.println(" bytes"); |
| 57 | + break; |
| 58 | + case 3: |
| 59 | + state = IDLE; |
| 60 | + f = fopen("/fs/movements", "rb"); |
| 61 | + final_idx = values + fread(values, 1, sizeof(values), f) / sizeof(float); |
| 62 | + fclose(f); |
| 63 | + Serial.print("LOADED "); |
| 64 | + Serial.print((final_idx - values) * sizeof(float)); |
| 65 | + Serial.println(" bytes"); |
| 66 | + break; |
| 67 | + default: |
| 68 | + state = IDLE; |
| 69 | + Serial.println("IDLE"); |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +static lv_obj_t *counter; |
| 76 | + |
| 77 | +static const char * btnm_map[] = { "Learn", "\n", "Replay", "\n", "Save in flash", "\n", "Load from flash", "\n", "Idle", "\n", "\0" }; |
| 78 | + |
| 79 | +void customMenu() { |
| 80 | + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); |
| 81 | + lv_btnmatrix_set_map(btnm1, btnm_map); |
| 82 | + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); |
| 83 | + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); |
| 84 | + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); |
| 85 | + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); |
| 86 | + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); |
| 87 | + lv_obj_align(btnm1, LV_ALIGN_CENTER, 0, 0); |
| 88 | + counter = lv_label_create(btnm1); |
| 89 | + lv_label_set_text_fmt(counter, "%d" , 0); |
| 90 | + lv_obj_align(counter, LV_ALIGN_CENTER, 0, 50); |
| 91 | + lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL); |
| 92 | + Braccio.connectJoystickTo(btnm1); |
| 93 | +} |
| 94 | + |
| 95 | +static FlashIAPBlockDevice bd(XIP_BASE + 0x100000, 0x100000); |
| 96 | +static mbed::FATFileSystem fs("fs"); |
| 97 | + |
| 98 | +void setup() { |
| 99 | + |
| 100 | + // Call Braccio.begin() for default menu or pass a function for custom menu |
| 101 | + Serial.begin(115200); |
| 102 | + |
| 103 | + // Mount file system for load/store movements |
| 104 | + int err = fs.mount(&bd); |
| 105 | + if (err) { |
| 106 | + err = fs.reformat(&bd); |
| 107 | + } |
| 108 | + |
| 109 | + Braccio.begin(customMenu); |
| 110 | + Serial.println("Replicate a movement"); |
| 111 | +} |
| 112 | + |
| 113 | +void loop() { |
| 114 | + if (state == LEARN) { |
| 115 | + Braccio.positions(idx); |
| 116 | + idx += 6; |
| 117 | + } |
| 118 | + if (state == REPEAT) { |
| 119 | + Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]); |
| 120 | + idx += 6; |
| 121 | + if (idx >= final_idx) { |
| 122 | + Serial.println("Repeat done"); |
| 123 | + state = IDLE; |
| 124 | + } |
| 125 | + } |
| 126 | + if (idx - values >= sizeof(values)) { |
| 127 | + Serial.println("IDLE"); |
| 128 | + state = IDLE; |
| 129 | + } |
| 130 | + delay(100); |
| 131 | + if (state != IDLE) { |
| 132 | + lv_label_set_text_fmt(counter, "%d" , idx - values); |
| 133 | + } |
| 134 | +} |
0 commit comments