From 0e8417328d9d389abcdf1ece4ecf5a38564c8495 Mon Sep 17 00:00:00 2001 From: njriasan Date: Thu, 14 Nov 2019 14:46:30 -0800 Subject: [PATCH 1/4] Fixed typo in example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bd2b1b5..7d90526 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,6 @@ For the stick push we have following mapping (treating towards buttons as being * NOT PRESSED: 8 * UNUSED: 9-15 -To illustrate this, if the controller currently has the stick pressed in the down direction, and the R, X, and Home buttons are pressed, then the following message in hex will be delivered: +To illustrate this, if the controller currently has the stick pressed in the left direction, and the R, X, and Home buttons are pressed, then the following message in hex will be delivered: 0x4628 From a5b8dd51e41786e6116845bdf9bb04fe2cca801b Mon Sep 17 00:00:00 2001 From: njriasan Date: Sun, 17 Nov 2019 19:50:53 -0800 Subject: [PATCH 2/4] fixed bug in code, now connects but need to test locations --- buckler/buckler_main.c | 27 +++++++++++++++++++-------- joycon/process_manager.c | 4 ++-- rpi_ble/send_pkt.py | 23 ++++++++++++----------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/buckler/buckler_main.c b/buckler/buckler_main.c index e690910..1c24f47 100644 --- a/buckler/buckler_main.c +++ b/buckler/buckler_main.c @@ -42,7 +42,7 @@ states state = OFF; // Intervals for advertising and connections static simple_ble_config_t ble_config = { // c0:98:e5:49:xx:xx - .platform_id = 0x49, // used as 4th octect in device BLE address + .platform_id = 0x00, // used as 4th octect in device BLE address .device_id = 0x11, // TODO: replace with your lab bench number .adv_name = "KOBUKI", // used in advertisements if there is room .adv_interval = MSEC_TO_UNITS(1000, UNIT_0_625_MS), @@ -63,24 +63,35 @@ static uint16_t controller_bytes; simple_ble_app_t* simple_ble_app; // controls ordering: accelerate, decelerate, left, right -static bool controls[NUM_BUTTONS] = {false, false, false, false}; -static uint16_t masks[NUM_BUTTONS] = {0b1, 0b1 << 3, 0b11 << 10, 0b1 << 11}; + +typedef struct { + char* name; + uint16_t mask; + uint8_t shift_amount; + uint8_t value; +} button_info_t; + +static button_info_t x_button = {"X", 0b1 << 3, 3, 0}; +static button_info_t b_button = {"B", 0b1, 0, 0}; +static button_info_t stick_push_button = {"STICK PUSH", 0xb1111 << 8, 8, 8}; + +static button_info_t *buttons[NUM_BUTTONS] = {&x_button, &b_button, &stick_push_button }; void ble_evt_write(ble_evt_t const* p_ble_evt) { // TODO: logic for each characteristic and related state changes for (unsigned int i = 0; i < NUM_BUTTONS; i++) { - controls[i] = controller_bytes & masks[i]; + buttons[i]->value = (buttons[i]->mask & controller_bytes) >> buttons[i]->shift_amount; } - if (controls[0] == true) { - if (controls[2] == true) { + if (x_button.value == 1) { + if (stick_push_button.value == 6) { state = LEFT; - } else if (controls[3] == true) { + } else if (stick_push_button.value == 2) { state = RIGHT; } else { state = ACCELERATE; } - } else if (controls[1] == true) { + } else if (b_button.value == 1) { state = DECELERATE; } } diff --git a/joycon/process_manager.c b/joycon/process_manager.c index 42bf547..de7cf6e 100644 --- a/joycon/process_manager.c +++ b/joycon/process_manager.c @@ -27,7 +27,7 @@ const char *joycon_mac_addrs[NUM_MAC_ADDRS] = {"04:03:d6:7b:59:ca", "04:03:d6:7a:b8:75"}; // List of all MAC addresses for the bucklers (replace me later) -const char *buckler_mac_addrs[NUM_MAC_ADDRS] = {"c0:98:e5:49:49:11", "04:03:d6:7a:b8:75"}; +const char *buckler_mac_addrs[NUM_MAC_ADDRS] = {"c0:98:e5:49:00:11", "c0:98:e5:49:00:11"}; static connection_node_t *unprocessed_macs = NULL; static connection_node_t *processed_macs = NULL; @@ -117,7 +117,7 @@ int main(int argc, char* argv[]) char *python_path = "python3"; char *args[5]; args[0] = python_path; - args[1] = "../send_pkt.py"; + args[1] = "../rpi_ble/send_pkt.py"; args[2] = node->buckler_mac_addr; args[3] = malloc (sizeof(char) * 6); snprintf (args[3], 6, "%d\n", node->server_port); diff --git a/rpi_ble/send_pkt.py b/rpi_ble/send_pkt.py index cfe2efe..dd8e60b 100644 --- a/rpi_ble/send_pkt.py +++ b/rpi_ble/send_pkt.py @@ -30,6 +30,14 @@ def __init__(self, address, server_port): print("connected") + + # Create a new Joycon + self.controller = JoyCon() + self.controller.display_all_pressed_buttons() + + # robot refers to buckler, our peripheral + self.robot = Peripheral(addr) + # get service from robot self.sv = self.robot.getServiceByUUID(SERVICE_UUID) @@ -41,13 +49,6 @@ def __init__(self, address, server_port): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(('localhost', server_port)) - # Create a new Joycon - controller = JoyCon() - controller.display_all_pressed_buttons() - - # robot refers to buckler, our peripheral - self.robot = Peripheral(addr) - while True: pkt = self.sock.recv(12) self.on_pkt_receive(pkt) @@ -55,13 +56,13 @@ def __init__(self, address, server_port): def on_pkt_receive(self, pkt): if (len(pkt) == 0): sys.exit (1) - controller.parse_next_state(pkt) - controller.display_all_pressed_buttons() - #for byte in controller.get_output_message(): + self.controller.parse_next_state(pkt) + self.controller.display_all_pressed_buttons() + #for byte in self.controller.get_output_message(): # print ("{} ".format(hex(byte)), end="") #print() - self.controller_characteristic.write(controller.get_output_message()) + self.controller_characteristic.write(self.controller.get_output_message()) def __enter__(self): return self From be2bb271c72502a8c879455cd7c6cc4b1e82f7bb Mon Sep 17 00:00:00 2001 From: njriasan Date: Sun, 17 Nov 2019 19:58:43 -0800 Subject: [PATCH 3/4] added changed to fix endianess --- buckler/buckler_main.c | 6 +++--- joycon/process_manager.c | 2 +- rpi_ble/buttons.py | 2 +- rpi_ble/send_pkt.py | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/buckler/buckler_main.c b/buckler/buckler_main.c index 1c24f47..9561d24 100644 --- a/buckler/buckler_main.c +++ b/buckler/buckler_main.c @@ -41,9 +41,9 @@ states state = OFF; // Intervals for advertising and connections static simple_ble_config_t ble_config = { - // c0:98:e5:49:xx:xx - .platform_id = 0x00, // used as 4th octect in device BLE address - .device_id = 0x11, // TODO: replace with your lab bench number + // c0:98:e5:yy:xx:xx + .platform_id = 0x00, // used as 4th octect in device BLE address yy + .device_id = 0x11, // TODO: replace with your lab bench number xx .adv_name = "KOBUKI", // used in advertisements if there is room .adv_interval = MSEC_TO_UNITS(1000, UNIT_0_625_MS), .min_conn_interval = MSEC_TO_UNITS(100, UNIT_1_25_MS), diff --git a/joycon/process_manager.c b/joycon/process_manager.c index de7cf6e..0ea7b7a 100644 --- a/joycon/process_manager.c +++ b/joycon/process_manager.c @@ -27,7 +27,7 @@ const char *joycon_mac_addrs[NUM_MAC_ADDRS] = {"04:03:d6:7b:59:ca", "04:03:d6:7a:b8:75"}; // List of all MAC addresses for the bucklers (replace me later) -const char *buckler_mac_addrs[NUM_MAC_ADDRS] = {"c0:98:e5:49:00:11", "c0:98:e5:49:00:11"}; +const char *buckler_mac_addrs[NUM_MAC_ADDRS] = {"c0:98:e5:00:00:11", "c0:98:e5:00:00:11"}; static connection_node_t *unprocessed_macs = NULL; static connection_node_t *processed_macs = NULL; diff --git a/rpi_ble/buttons.py b/rpi_ble/buttons.py index bea6b44..9e887a2 100644 --- a/rpi_ble/buttons.py +++ b/rpi_ble/buttons.py @@ -295,7 +295,7 @@ def get_output_message(self): output_msg = bytes([0, 0]) for button in self.buttons: output_msg = button.append_output(output_msg) - return output_msg + return bytearray([output_msg[1]] + [output_msg[0]]) """ Sample class to contain information for a particular JoyCon diff --git a/rpi_ble/send_pkt.py b/rpi_ble/send_pkt.py index dd8e60b..41ee9ad 100644 --- a/rpi_ble/send_pkt.py +++ b/rpi_ble/send_pkt.py @@ -58,9 +58,9 @@ def on_pkt_receive(self, pkt): sys.exit (1) self.controller.parse_next_state(pkt) self.controller.display_all_pressed_buttons() - #for byte in self.controller.get_output_message(): - # print ("{} ".format(hex(byte)), end="") - #print() + for byte in self.controller.get_output_message(): + print ("{} ".format(hex(byte)), end="") + print() self.controller_characteristic.write(self.controller.get_output_message()) From c21360015a592d483b986c543290ee0e69f7dcd7 Mon Sep 17 00:00:00 2001 From: GrantMeAWish Date: Sun, 17 Nov 2019 21:29:33 -0800 Subject: [PATCH 4/4] working naive driving demo --- buckler/buckler_main.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/buckler/buckler_main.c b/buckler/buckler_main.c index 1c24f47..d88b85f 100644 --- a/buckler/buckler_main.c +++ b/buckler/buckler_main.c @@ -73,15 +73,21 @@ typedef struct { static button_info_t x_button = {"X", 0b1 << 3, 3, 0}; static button_info_t b_button = {"B", 0b1, 0, 0}; -static button_info_t stick_push_button = {"STICK PUSH", 0xb1111 << 8, 8, 8}; +static button_info_t stick_push_button = {"STICK PUSH", 0b1111 << 8, 8, 8}; static button_info_t *buttons[NUM_BUTTONS] = {&x_button, &b_button, &stick_push_button }; void ble_evt_write(ble_evt_t const* p_ble_evt) { // TODO: logic for each characteristic and related state changes + //printf("%x\n", stick_push_button.value); for (unsigned int i = 0; i < NUM_BUTTONS; i++) { buttons[i]->value = (buttons[i]->mask & controller_bytes) >> buttons[i]->shift_amount; } + uint8_t *bytes_look = (uint8_t *) &controller_bytes; + //printf("%x\n", stick_push_button.value); + //printf("%x %x\n", bytes_look[0], bytes_look[1]); + //printf("\n\n"); + if (x_button.value == 1) { if (stick_push_button.value == 6) { @@ -93,6 +99,8 @@ void ble_evt_write(ble_evt_t const* p_ble_evt) { } } else if (b_button.value == 1) { state = DECELERATE; + } else { + state = OFF; } } @@ -208,7 +216,7 @@ int main(void) { state = OFF; } else { // perform state-specific actions here - kobukiDriveDirect(100, 100); + kobukiDriveDirect(700, 700); } break; // each case needs to end with break! } @@ -232,7 +240,7 @@ int main(void) { state = OFF; } else { // perform state-specific actions here - kobukiDriveDirect(-50, 100); + kobukiDriveDirect(650, 700); } break; // each case needs to end with break! } @@ -244,7 +252,7 @@ int main(void) { state = OFF; } else { // perform state-specific actions here - kobukiDriveDirect(100, -50); + kobukiDriveDirect(700, 650); } break; // each case needs to end with break! }