From da91b813f0b5b8568ead75ee81ed492321ff0368 Mon Sep 17 00:00:00 2001 From: wheaney <42350981+wheaney@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:20:44 -0700 Subject: [PATCH 1/7] Merge device changes from https://github.com/wheaney/xrealAirLinuxDriver --- .gitmodules | 9 ++- CMakeLists.txt | 8 +-- examples/debug_d3/CMakeLists.txt | 4 +- examples/debug_d3/src/debug.c | 2 +- examples/debug_d4/CMakeLists.txt | 4 +- examples/mcu_firmware/CMakeLists.txt | 4 +- interface_lib/CMakeLists.txt | 18 +++-- interface_lib/Fusion | 1 + interface_lib/hidapi | 1 + interface_lib/include/device3.h | 4 +- interface_lib/src/device3.c | 99 +++++++++++++++++----------- interface_lib/src/device4.c | 58 ++++++++-------- modules/Fusion | 1 - src/driver.c | 2 +- 14 files changed, 124 insertions(+), 91 deletions(-) create mode 160000 interface_lib/Fusion create mode 160000 interface_lib/hidapi delete mode 160000 modules/Fusion diff --git a/.gitmodules b/.gitmodules index 5192490..c4c957e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ -[submodule "modules/Fusion"] - path = modules/Fusion - url = https://github.com/TheJackiMonster/Fusion.git +[submodule "interface_lib/Fusion"] + path = interface_lib/Fusion + url = https://github.com/xioTechnologies/Fusion.git +[submodule "interface_lib/hidapi"] + path = interface_lib/hidapi + url = https://github.com/libusb/hidapi.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c072cd7..2a1cdd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,6 @@ project(nrealAirLinuxDriver C) set(CMAKE_C_STANDARD 17) -add_subdirectory(modules/Fusion/Fusion) -set(FUSION_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/modules/Fusion) -set(FUSION_LIBRARY Fusion) - add_subdirectory(interface_lib) add_subdirectory(examples) @@ -19,9 +15,9 @@ target_include_directories(nrealAirLinuxDriver ) target_include_directories(nrealAirLinuxDriver - BEFORE PUBLIC ${NRA_INCLUDE_DIR} + BEFORE PUBLIC ${NREAL_AIR_INCLUDE_DIR} ) target_link_libraries(nrealAirLinuxDriver - ${NRA_LIBRARY} ${FUSION_LIBRARY} + ${NREAL_AIR_LIBRARY} ${FUSION_LIBRARY} ) \ No newline at end of file diff --git a/examples/debug_d3/CMakeLists.txt b/examples/debug_d3/CMakeLists.txt index f293276..d214819 100644 --- a/examples/debug_d3/CMakeLists.txt +++ b/examples/debug_d3/CMakeLists.txt @@ -9,9 +9,9 @@ add_executable( ) target_include_directories(nrealAirDebugD3 - BEFORE PUBLIC ${NRA_INCLUDE_DIR} + BEFORE PUBLIC ${NREAL_AIR_INCLUDE_DIR} ) target_link_libraries(nrealAirDebugD3 - ${NRA_LIBRARY} + ${NREAL_AIR_LIBRARY} ) diff --git a/examples/debug_d3/src/debug.c b/examples/debug_d3/src/debug.c index 0d524f8..6dd22f4 100644 --- a/examples/debug_d3/src/debug.c +++ b/examples/debug_d3/src/debug.c @@ -56,7 +56,7 @@ int main(int argc, const char** argv) { device3_clear(dev3); while (dev3) { - if (device3_read(dev3, -1) < 0) { + if (device3_read(dev3, -1, false) < 0) { break; } } diff --git a/examples/debug_d4/CMakeLists.txt b/examples/debug_d4/CMakeLists.txt index 3b25a0e..d01a63f 100644 --- a/examples/debug_d4/CMakeLists.txt +++ b/examples/debug_d4/CMakeLists.txt @@ -9,9 +9,9 @@ add_executable( ) target_include_directories(nrealAirDebugD4 - BEFORE PUBLIC ${NRA_INCLUDE_DIR} + BEFORE PUBLIC ${NREAL_AIR_INCLUDE_DIR} ) target_link_libraries(nrealAirDebugD4 - ${NRA_LIBRARY} + ${NREAL_AIR_LIBRARY} ) diff --git a/examples/mcu_firmware/CMakeLists.txt b/examples/mcu_firmware/CMakeLists.txt index 37409df..6b5d082 100644 --- a/examples/mcu_firmware/CMakeLists.txt +++ b/examples/mcu_firmware/CMakeLists.txt @@ -9,9 +9,9 @@ add_executable( ) target_include_directories(nrealAirUpgradeMCU - BEFORE PUBLIC ${NRA_INCLUDE_DIR} + BEFORE PUBLIC ${NREAL_AIR_INCLUDE_DIR} ) target_link_libraries(nrealAirUpgradeMCU - ${NRA_LIBRARY} + ${NREAL_AIR_LIBRARY} ) diff --git a/interface_lib/CMakeLists.txt b/interface_lib/CMakeLists.txt index fec5dd4..20ad1c4 100644 --- a/interface_lib/CMakeLists.txt +++ b/interface_lib/CMakeLists.txt @@ -3,8 +3,12 @@ project(nrealAirLibrary C) set(CMAKE_C_STANDARD 17) -find_package(hidapi REQUIRED) -find_package(json-c REQUIRED) +add_subdirectory(hidapi) +find_package(json-c REQUIRED CONFIG) + +add_subdirectory(Fusion/Fusion) +set(FUSION_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/interface_lib/Fusion) +set(FUSION_LIBRARY Fusion) add_library( nrealAirLibrary @@ -13,17 +17,19 @@ add_library( src/device4.c ) +target_compile_options(nrealAirLibrary PRIVATE -fPIC) + target_include_directories(nrealAirLibrary BEFORE PUBLIC include ) target_include_directories(nrealAirLibrary - SYSTEM BEFORE PRIVATE ${FUSION_INCLUDE_DIR} + BEFORE PRIVATE ${FUSION_INCLUDE_DIR} ) target_link_libraries(nrealAirLibrary - hidapi::hidapi json-c::json-c ${FUSION_LIBRARY} m + PRIVATE hidapi::hidapi json-c::json-c ${FUSION_LIBRARY} ) -set(NRA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE) -set(NRA_LIBRARY nrealAirLibrary PARENT_SCOPE) +set(NREAL_AIR_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE) +set(NREAL_AIR_LIBRARY nrealAirLibrary PARENT_SCOPE) diff --git a/interface_lib/Fusion b/interface_lib/Fusion new file mode 160000 index 0000000..0fd6785 --- /dev/null +++ b/interface_lib/Fusion @@ -0,0 +1 @@ +Subproject commit 0fd6785d3e3e2f8c358603db47ff5dadc0e338af diff --git a/interface_lib/hidapi b/interface_lib/hidapi new file mode 160000 index 0000000..09ab35f --- /dev/null +++ b/interface_lib/hidapi @@ -0,0 +1 @@ +Subproject commit 09ab35ffa9dce60a993f4e55cdeaf0952645f9b7 diff --git a/interface_lib/include/device3.h b/interface_lib/include/device3.h index d4a3b28..3d19182 100644 --- a/interface_lib/include/device3.h +++ b/interface_lib/include/device3.h @@ -122,6 +122,8 @@ struct device3_t { device3_event_callback callback; device3_calibration_type* calibration; + + bool ready; }; typedef struct device3_t device3_type; @@ -138,7 +140,7 @@ void device3_clear(device3_type* device); int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet); -int device3_read(device3_type* device, int timeout); +int device3_read(device3_type* device, int timeout, bool silent); device3_vec3_type device3_get_earth_acceleration(const device3_ahrs_type* ahrs); diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index cc31169..4d7af6e 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -26,12 +26,13 @@ #include #include +#include #include #include #include -#include +#include <../hidapi/hidapi/hidapi.h> #include "crc32.h" @@ -65,7 +66,7 @@ static bool send_payload(device3_type* device, uint8_t size, const uint8_t* payl int transferred = hid_write(device->handle, payload, payload_size); if (transferred != payload_size) { - perror("ERROR: sending payload failed\n"); + fprintf(stderr, "ERROR: sending payload failed\n"); return false; } @@ -89,7 +90,7 @@ static bool recv_payload(device3_type* device, uint8_t size, uint8_t* payload) { } if (transferred != payload_size) { - perror("ERROR: receiving payload failed\n"); + fprintf(stderr, "ERROR: receiving payload failed\n"); return false; } @@ -179,7 +180,6 @@ device3_type* device3_open(device3_event_callback callback) { device3_type* device = (device3_type*) malloc(sizeof(device3_type)); if (!device) { - perror("Not allocated!\n"); return NULL; } @@ -189,7 +189,6 @@ device3_type* device3_open(device3_event_callback callback) { device->callback = callback; if (0 != hid_init()) { - perror("Not initialized!\n"); return device; } @@ -211,14 +210,12 @@ device3_type* device3_open(device3_event_callback callback) { hid_free_enumeration(info); if (!device->handle) { - perror("No handle!\n"); return device; } device3_clear(device); if (!send_payload_msg(device, DEVICE3_MSG_GET_STATIC_ID, 0, NULL)) { - perror("Failed sending payload to get static id!\n"); return device; } @@ -233,7 +230,6 @@ device3_type* device3_open(device3_event_callback callback) { device3_reset_calibration(device); if (!send_payload_msg(device, DEVICE3_MSG_GET_CAL_DATA_LENGTH, 0, NULL)) { - perror("Failed sending payload to get calibration data length!\n"); return device; } @@ -287,7 +283,6 @@ device3_type* device3_open(device3_event_callback callback) { } if (!send_payload_msg_signal(device, DEVICE3_MSG_START_IMU_DATA, 0x1)) { - perror("Failed sending payload to start imu data stream!\n"); return device; } @@ -304,21 +299,24 @@ device3_type* device3_open(device3_event_callback callback) { .gain = 0.5f, .accelerationRejection = 10.0f, .magneticRejection = 20.0f, - .rejectionTimeout = 5 * SAMPLE_RATE, /* 5 seconds */ + .recoveryTriggerPeriod = 5 * SAMPLE_RATE, /* 5 seconds */ }; FusionAhrsSetSettings((FusionAhrs*) device->ahrs, &settings); + + device->ready = true; + return device; } void device3_reset_calibration(device3_type* device) { if (!device) { - perror("No device!\n"); + fprintf(stderr, "No device!\n"); return; } if (!device->calibration) { - perror("Not allocated!\n"); + fprintf(stderr, "Not allocated!\n"); return; } @@ -343,18 +341,18 @@ void device3_reset_calibration(device3_type* device) { int device3_load_calibration(device3_type* device, const char* path) { if (!device) { - perror("No device!\n"); + fprintf(stderr, "No device!\n"); return -1; } if (!device->calibration) { - perror("Not allocated!\n"); + fprintf(stderr, "Not allocated!\n"); return -2; } FILE* file = fopen(path, "rb"); if (!file) { - perror("No file opened!\n"); + fprintf(stderr, "No file opened!\n"); return -3; } @@ -362,11 +360,11 @@ int device3_load_calibration(device3_type* device, const char* path) { count = fread(device->calibration, 1, sizeof(device3_calibration_type), file); if (sizeof(device3_calibration_type) != count) { - perror("Not fully loaded!\n"); + fprintf(stderr, "Not fully loaded!\n"); } if (0 != fclose(file)) { - perror("No file closed!\n"); + fprintf(stderr, "No file closed!\n"); return -4; } @@ -375,18 +373,18 @@ int device3_load_calibration(device3_type* device, const char* path) { int device3_save_calibration(device3_type* device, const char* path) { if (!device) { - perror("No device!\n"); + fprintf(stderr, "No device!\n"); return -1; } if (!device->calibration) { - perror("Not allocated!\n"); + fprintf(stderr, "Not allocated!\n"); return -2; } FILE* file = fopen(path, "wb"); if (!file) { - perror("No file opened!\n"); + fprintf(stderr, "No file opened!\n"); return -3; } @@ -394,11 +392,11 @@ int device3_save_calibration(device3_type* device, const char* path) { count = fwrite(device->calibration, 1, sizeof(device3_calibration_type), file); if (sizeof(device3_calibration_type) != count) { - perror("Not fully saved!\n"); + fprintf(stderr, "Not fully saved!\n"); } if (0 != fclose(file)) { - perror("No file closed!\n"); + fprintf(stderr, "No file closed!\n"); return -4; } @@ -600,17 +598,17 @@ static void apply_calibration(const device3_type* device, } void device3_clear(device3_type* device) { - device3_read(device, 10); + device3_read(device, 10, true); } int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet) { if (!device) { - perror("No device!\n"); + fprintf(stderr, "No device!\n"); return -1; } if (MAX_PACKET_SIZE != sizeof(device3_packet_type)) { - perror("Not proper size!\n"); + fprintf(stderr, "Not proper size!\n"); return -2; } @@ -633,13 +631,18 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool (uint8_t*) &packet, MAX_PACKET_SIZE ); - + + if (transferred == -1) { + fprintf(stderr, "HID read error: device may be unplugged\n"); + return -5; + } + if (transferred == 0) { continue; } - + if (MAX_PACKET_SIZE != transferred) { - perror("Not expected issue!\n"); + fprintf(stderr, "HID read error: unexpected packet size\n"); return -3; } @@ -714,14 +717,18 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool return 0; } -int device3_read(device3_type* device, int timeout) { - if (!device) { - perror("No device!\n"); +int device3_read(device3_type* device, int timeout, bool silent) { + if (!device || !device->ready) { + if (!silent) { + fprintf(stderr, "No device!\n"); + } return -1; } if (MAX_PACKET_SIZE != sizeof(device3_packet_type)) { - perror("Not proper size!\n"); + if (!silent) { + fprintf(stderr, "Not proper size!\n"); + } return -2; } @@ -734,13 +741,22 @@ int device3_read(device3_type* device, int timeout) { MAX_PACKET_SIZE, timeout ); + + if (transferred == -1) { + if (!silent) { + fprintf(stderr, "HID read error: device may be unplugged\n"); + } + return -5; + } if (transferred == 0) { return 1; } if (MAX_PACKET_SIZE != transferred) { - perror("Not expected issue!\n"); + if (!silent) { + fprintf(stderr, "HID read error: unexpected packet size\n"); + } return -3; } @@ -752,7 +768,9 @@ int device3_read(device3_type* device, int timeout) { } if ((packet.signature[0] != 0x01) || (packet.signature[1] != 0x02)) { - perror("Not matching signature!\n"); + if (!silent) { + fprintf(stderr, "Mismatched signature! Try unplugging then replugging your device.\n"); + } return -4; } @@ -782,8 +800,16 @@ int device3_read(device3_type* device, int timeout) { //printf("M: %.2f %.2f %.2f\n", magnetometer.axis.x, magnetometer.axis.y, magnetometer.axis.z); if (device->ahrs) { - FusionAhrsUpdate((FusionAhrs*) device->ahrs, gyroscope, accelerometer, magnetometer, deltaTime); - //FusionAhrsUpdateNoMagnetometer((FusionAhrs*) device->ahrs, gyroscope, accelerometer, deltaTime); + FusionAhrsUpdateNoMagnetometer((FusionAhrs*) device->ahrs, gyroscope, accelerometer, deltaTime); + + // TODO: fix detection of this case; quat.x as a nan value is only a side-effect of some issue with ahrs or + // the gyro/accel/magnet readings + if (isnan(device3_get_orientation(device->ahrs).x)) { + if (!silent) { + fprintf(stderr, "Invalid device reading\n"); + } + return -5; + } } device3_callback(device, timestamp, DEVICE3_EVENT_UPDATE); @@ -834,7 +860,6 @@ device3_vec3_type device3_get_euler(device3_quat_type quat) { void device3_close(device3_type* device) { if (!device) { - perror("No device!\n"); return; } diff --git a/interface_lib/src/device4.c b/interface_lib/src/device4.c index e0eb90d..286c308 100644 --- a/interface_lib/src/device4.c +++ b/interface_lib/src/device4.c @@ -31,7 +31,7 @@ #include #include -#include +#include <../hidapi/hidapi/hidapi.h> #include "crc32.h" @@ -47,7 +47,7 @@ static bool send_payload(device4_type* device, uint8_t size, const uint8_t* payl int transferred = hid_write(device->handle, payload, payload_size); if (transferred != payload_size) { - perror("ERROR: sending payload failed\n"); + fprintf(stderr, "ERROR: sending payload failed\n"); return false; } @@ -71,7 +71,7 @@ static bool recv_payload(device4_type* device, uint8_t size, uint8_t* payload) { } if (transferred != payload_size) { - perror("ERROR: receiving payload failed\n"); + fprintf(stderr, "ERROR: receiving payload failed\n"); return false; } @@ -160,7 +160,7 @@ device4_type* device4_open(device4_event_callback callback) { device4_type* device = (device4_type*) malloc(sizeof(device4_type)); if (!device) { - perror("Not allocated!\n"); + fprintf(stderr, "Not allocated!\n"); return NULL; } @@ -170,7 +170,7 @@ device4_type* device4_open(device4_event_callback callback) { device->callback = callback; if (0 != hid_init()) { - perror("Not initialized!\n"); + fprintf(stderr, "Not initialized!\n"); return device; } @@ -192,57 +192,57 @@ device4_type* device4_open(device4_event_callback callback) { hid_free_enumeration(info); if (!device->handle) { - perror("No handle!\n"); + fprintf(stderr, "No handle!\n"); return device; } device4_clear(device); if (!send_payload_action(device, DEVICE4_MSG_R_ACTIVATION_TIME, 0, NULL)) { - perror("Requesting activation time failed!\n"); + fprintf(stderr, "Requesting activation time failed!\n"); return device; } uint8_t activated; if (!recv_payload_msg(device, DEVICE4_MSG_R_ACTIVATION_TIME, 1, &activated)) { - perror("Receiving activation time failed!\n"); + fprintf(stderr, "Receiving activation time failed!\n"); return device; } device->activated = (activated != 0); if (!device->activated) { - perror("Device is not activated!\n"); + fprintf(stderr, "Device is not activated!\n"); return device; } if (!send_payload_action(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 0, NULL)) { - perror("Requesting current MCU app firmware version!\n"); + fprintf(stderr, "Requesting current MCU app firmware version!\n"); return device; } if (!recv_payload_msg(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 41, (uint8_t*) device->mcu_app_fw_version)) { - perror("Receiving current MCU app firmware version failed!\n"); + fprintf(stderr, "Receiving current MCU app firmware version failed!\n"); return device; } if (!send_payload_action(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 0, NULL)) { - perror("Requesting current DP firmware version!\n"); + fprintf(stderr, "Requesting current DP firmware version!\n"); return device; } if (!recv_payload_msg(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 41, (uint8_t*) device->dp_fw_version)) { - perror("Receiving current DP firmware version failed!\n"); + fprintf(stderr, "Receiving current DP firmware version failed!\n"); return device; } if (!send_payload_action(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 0, NULL)) { - perror("Requesting current DSP app firmware version!\n"); + fprintf(stderr, "Requesting current DSP app firmware version!\n"); return device; } if (!recv_payload_msg(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 41, (uint8_t*) device->dsp_fw_version)) { - perror("Receiving current DSP app firmware version failed!\n"); + fprintf(stderr, "Receiving current DSP app firmware version failed!\n"); return device; } @@ -253,22 +253,22 @@ device4_type* device4_open(device4_event_callback callback) { #endif if (!send_payload_action(device, DEVICE4_MSG_R_BRIGHTNESS, 0, NULL)) { - perror("Requesting initial brightness failed!\n"); + fprintf(stderr, "Requesting initial brightness failed!\n"); return device; } if (!recv_payload_msg(device, DEVICE4_MSG_R_BRIGHTNESS, 1, &device->brightness)) { - perror("Receiving initial brightness failed!\n"); + fprintf(stderr, "Receiving initial brightness failed!\n"); return device; } if (!send_payload_action(device, DEVICE4_MSG_R_DISP_MODE, 0, NULL)) { - perror("Requesting display mode failed!\n"); + fprintf(stderr, "Requesting display mode failed!\n"); return device; } if (!recv_payload_msg(device, DEVICE4_MSG_R_DISP_MODE, 1, &device->disp_mode)) { - perror("Receiving display mode failed!\n"); + fprintf(stderr, "Receiving display mode failed!\n"); return device; } @@ -302,7 +302,7 @@ int device4_read(device4_type* device, int timeout) { } if (MAX_PACKET_SIZE != sizeof(device4_packet_type)) { - perror("Not proper size!\n"); + fprintf(stderr, "Not proper size!\n"); return -2; } @@ -321,12 +321,12 @@ int device4_read(device4_type* device, int timeout) { } if (MAX_PACKET_SIZE != transferred) { - perror("Reading failed!\n"); + fprintf(stderr, "Reading failed!\n"); return -3; } if (packet.head != PACKET_HEAD) { - perror("Wrong packet!\n"); + fprintf(stderr, "Wrong packet!\n"); return -4; } @@ -411,7 +411,7 @@ int device4_read(device4_type* device, int timeout) { device->active = true; if (data_len + text_len != packet.length) { - perror("Not matching length!\n"); + fprintf(stderr, "Not matching length!\n"); return -5; } @@ -447,7 +447,7 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) { } if (!device->activated) { - perror("Device is not activated!\n"); + fprintf(stderr, "Device is not activated!\n"); return false; } @@ -474,12 +474,12 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) { printf("Prepare upload: %lu\n", firmware_len); if (!do_payload_action(device, DEVICE4_MSG_W_UPDATE_MCU_APP_FW_PREPARE, 0, NULL)) { - perror("Failed preparing the device for MCU firmware update!\n"); + fprintf(stderr, "Failed preparing the device for MCU firmware update!\n"); goto cleanup; } if (!do_payload_action(device, DEVICE4_MSG_W_MCU_APP_JUMP_TO_BOOT, 0, NULL)) { - perror("Failed mcu app jumping to boot!\n"); + fprintf(stderr, "Failed mcu app jumping to boot!\n"); goto cleanup; } @@ -501,7 +501,7 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) { } if (!do_payload_action(device, msgid, len, firmware)) { - perror("Failed sending firmware upload!\n"); + fprintf(stderr, "Failed sending firmware upload!\n"); goto jump_to_app; } } @@ -509,7 +509,7 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) { printf("Finish upload!\n"); if (!do_payload_action(device, DEVICE4_MSG_W_UPDATE_MCU_APP_FW_FINISH, 0, NULL)) { - perror("Failed finishing firmware upload!\n"); + fprintf(stderr, "Failed finishing firmware upload!\n"); goto jump_to_app; } @@ -517,7 +517,7 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) { jump_to_app: if (!do_payload_action(device, DEVICE4_MSG_W_BOOT_JUMP_TO_APP, 0, NULL)) { - perror("Failed boot jumping back to app!\n"); + fprintf(stderr, "Failed boot jumping back to app!\n"); goto cleanup; } diff --git a/modules/Fusion b/modules/Fusion deleted file mode 160000 index f69fd25..0000000 --- a/modules/Fusion +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f69fd25df008ca11e709579fea4b784537f8911e diff --git a/src/driver.c b/src/driver.c index 28823c1..f6d3e85 100644 --- a/src/driver.c +++ b/src/driver.c @@ -106,7 +106,7 @@ int main(int argc, const char** argv) { device3_clear(dev3); while (dev3) { - if (device3_read(dev3, 0) < 0) { + if (device3_read(dev3, 0, false) < 0) { break; } } From b6b8f9e38766ab32d819b9db13e63f202d6c79a6 Mon Sep 17 00:00:00 2001 From: wheaney <42350981+wheaney@users.noreply.github.com> Date: Thu, 7 Sep 2023 13:08:14 -0700 Subject: [PATCH 2/7] Remove Fusion library inclusion from the top-level CMakeLists --- CMakeLists.txt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a1cdd2..edbffc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,14 +10,10 @@ add_executable(nrealAirLinuxDriver src/driver.c ) -target_include_directories(nrealAirLinuxDriver - SYSTEM BEFORE PRIVATE ${FUSION_INCLUDE_DIR} -) - target_include_directories(nrealAirLinuxDriver BEFORE PUBLIC ${NREAL_AIR_INCLUDE_DIR} ) target_link_libraries(nrealAirLinuxDriver - ${NREAL_AIR_LIBRARY} ${FUSION_LIBRARY} + ${NREAL_AIR_LIBRARY} ) \ No newline at end of file From 54c0e3ece40d31d7b6d51c6b76b787e939da23ab Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Fri, 8 Sep 2023 00:36:20 +0200 Subject: [PATCH 3/7] Remove silent flag Signed-off-by: TheJackiMonster --- examples/debug_d3/src/debug.c | 2 +- interface_lib/src/device3.c | 4 +--- src/driver.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/debug_d3/src/debug.c b/examples/debug_d3/src/debug.c index 6dd22f4..0d524f8 100644 --- a/examples/debug_d3/src/debug.c +++ b/examples/debug_d3/src/debug.c @@ -56,7 +56,7 @@ int main(int argc, const char** argv) { device3_clear(dev3); while (dev3) { - if (device3_read(dev3, -1, false) < 0) { + if (device3_read(dev3, -1) < 0) { break; } } diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index 16feaf5..0a227f7 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -317,7 +316,6 @@ device3_type* device3_open(device3_event_callback callback) { }; FusionAhrsSetSettings((FusionAhrs*) device->ahrs, &settings); - return device; } @@ -610,7 +608,7 @@ static void apply_calibration(const device3_type* device, } void device3_clear(device3_type* device) { - device3_read(device, 10, true); + device3_read(device, 10); } int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet) { diff --git a/src/driver.c b/src/driver.c index f6d3e85..28823c1 100644 --- a/src/driver.c +++ b/src/driver.c @@ -106,7 +106,7 @@ int main(int argc, const char** argv) { device3_clear(dev3); while (dev3) { - if (device3_read(dev3, 0, false) < 0) { + if (device3_read(dev3, 0) < 0) { break; } } From 98e123d10314253e386cf5098e13beaab94bb8dc Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Fri, 8 Sep 2023 00:38:05 +0200 Subject: [PATCH 4/7] Adjust error values Signed-off-by: TheJackiMonster --- interface_lib/src/device3.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index 0a227f7..6a62202 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -644,7 +644,7 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool if (transferred == -1) { fprintf(stderr, "HID read error: device may be unplugged\n"); - return -5; + return -3; } if (transferred == 0) { @@ -653,7 +653,7 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool if (MAX_PACKET_SIZE != transferred) { device3_error("Not expected issue"); - return -3; + return -4; } if ((packet.signature[0] != 0x01) || (packet.signature[1] != 0x02)) { From 358923e19b35cf983e2e59fdb267603cdfde5b0e Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Fri, 8 Sep 2023 00:40:17 +0200 Subject: [PATCH 5/7] Adjust errors to be consistent Signed-off-by: TheJackiMonster --- interface_lib/src/device3.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index 6a62202..a430b0c 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -616,10 +616,15 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool device3_error("No device"); return -1; } + + if (!device->handle) { + device3_error("No handle"); + return -2; + } if (MAX_PACKET_SIZE != sizeof(device3_packet_type)) { device3_error("Not proper size"); - return -2; + return -3; } device3_packet_type packet; @@ -643,8 +648,8 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool ); if (transferred == -1) { - fprintf(stderr, "HID read error: device may be unplugged\n"); - return -3; + fprintf(stderr, "Device may be unplugged"); + return -4; } if (transferred == 0) { @@ -653,7 +658,7 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool if (MAX_PACKET_SIZE != transferred) { device3_error("Not expected issue"); - return -4; + return -5; } if ((packet.signature[0] != 0x01) || (packet.signature[1] != 0x02)) { From 0717ad9cd57300f9f30d74c9f5f6f4377759216c Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Fri, 8 Sep 2023 00:40:51 +0200 Subject: [PATCH 6/7] Use macro Signed-off-by: TheJackiMonster --- interface_lib/src/device3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index a430b0c..8c1dd02 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -648,7 +648,7 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool ); if (transferred == -1) { - fprintf(stderr, "Device may be unplugged"); + device3_error("Device may be unplugged"); return -4; } From 866cace9a6f7d2c49cd4f7a3a807c6369ef03c72 Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Fri, 8 Sep 2023 00:43:15 +0200 Subject: [PATCH 7/7] Add comment for magnetometer usage Signed-off-by: TheJackiMonster --- interface_lib/src/device3.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interface_lib/src/device3.c b/interface_lib/src/device3.c index 8c1dd02..9b80344 100644 --- a/interface_lib/src/device3.c +++ b/interface_lib/src/device3.c @@ -810,6 +810,8 @@ int device3_read(device3_type* device, int timeout) { //printf("M: %.2f %.2f %.2f\n", magnetometer.axis.x, magnetometer.axis.y, magnetometer.axis.z); if (device->ahrs) { + /* The magnetometer seems to make results of sensor fusion generally worse. So it is not used currently. */ + //FusionAhrsUpdate((FusionAhrs*) device->ahrs, gyroscope, accelerometer, magnetometer, deltaTime); FusionAhrsUpdateNoMagnetometer((FusionAhrs*) device->ahrs, gyroscope, accelerometer, deltaTime); const device3_quat_type orientation = device3_get_orientation(device->ahrs);