From a13e8df6b9a7dbd9054ae9fc08ee9b1484a09880 Mon Sep 17 00:00:00 2001 From: Cameron Goddard Date: Tue, 7 Jan 2025 15:28:38 -0500 Subject: [PATCH] add I2C timeouts --- platformio.ini | 5 +++-- src/Monitors/IMUMonitor.cpp | 16 ++++++++-------- src/Monitors/IMUMonitor.hpp | 2 ++ src/Monitors/TempMonitor.cpp | 2 -- src/main.cpp | 4 ++++ 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8861e8d..32883f3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,7 +14,8 @@ framework = arduino board = atmega328p build_flags = -D VERBOSE - -D CHIPSAT_ID=2 + -D WIRE_TIMEOUT + -D CHIPSAT_ID=3 upload_protocol = custom upload_speed = 19200 -upload_command = avrdude -C /Users/camerongoddard/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf -P /dev/cu.usbmodem11301 -b 19200 -v -V -p atmega328p -c stk500v1 $UPLOAD_FLAGS -U flash:w:$SOURCE:i \ No newline at end of file +upload_command = avrdude -C /Users/camerongoddard/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf -P /dev/cu.usbmodem211301 -b 19200 -v -V -p atmega328p -c stk500v1 $UPLOAD_FLAGS -U flash:w:$SOURCE:i \ No newline at end of file diff --git a/src/Monitors/IMUMonitor.cpp b/src/Monitors/IMUMonitor.cpp index f6dabe4..14ff234 100644 --- a/src/Monitors/IMUMonitor.cpp +++ b/src/Monitors/IMUMonitor.cpp @@ -1,6 +1,6 @@ #include "IMUMonitor.hpp" -IMUMonitor::IMUMonitor() +IMUMonitor::IMUMonitor(): imu(Wire) { } @@ -10,7 +10,7 @@ void IMUMonitor::execute() #ifdef VERBOSE Serial.println(F("Turning on IMU")); #endif - if (!IMU.begin()) { + if (!imu.begin()) { #ifdef VERBOSE Serial.println(F("IMU failed")); #endif @@ -26,22 +26,22 @@ void IMUMonitor::execute() void IMUMonitor::capture_imu_values() { // Check if the gyroscope, accelerometer, or magnetometer has new data available - if (IMU.gyroscopeAvailable()) { - IMU.readGyroscope( + if (imu.gyroscopeAvailable()) { + imu.readGyroscope( sfr::imu::gyro_x, sfr::imu::gyro_y, sfr::imu::gyro_z); // Data is in degrees/s } - if (IMU.accelerationAvailable()) { - IMU.readAcceleration( + if (imu.accelerationAvailable()) { + imu.readAcceleration( sfr::imu::acc_x, sfr::imu::acc_y, sfr::imu::acc_z); // Data is in m/s^2 } - if (IMU.magneticFieldAvailable()) { - IMU.readMagneticField( + if (imu.magneticFieldAvailable()) { + imu.readMagneticField( sfr::imu::mag_x, sfr::imu::mag_y, sfr::imu::mag_z); // Data in uT diff --git a/src/Monitors/IMUMonitor.hpp b/src/Monitors/IMUMonitor.hpp index 01a2171..1cc3e30 100644 --- a/src/Monitors/IMUMonitor.hpp +++ b/src/Monitors/IMUMonitor.hpp @@ -19,6 +19,8 @@ class IMUMonitor * @brief Reads IMU data */ void capture_imu_values(); + + LSM9DS1Class imu; }; #endif diff --git a/src/Monitors/TempMonitor.cpp b/src/Monitors/TempMonitor.cpp index 2df3e1d..c83ef1b 100644 --- a/src/Monitors/TempMonitor.cpp +++ b/src/Monitors/TempMonitor.cpp @@ -2,8 +2,6 @@ TempMonitor::TempMonitor() { - // Init I2C communication as master - Wire.begin(); } void TempMonitor::execute() diff --git a/src/main.cpp b/src/main.cpp index da9097a..2eb2b7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,10 @@ void setup() Serial.begin(9600); #endif + // Set I2C transaction timeout + Wire.begin(); + Wire.setWireTimeout(3000); + sfr::gps::boot_time = millis(); // Set ChipSat LED high for debugging