From fd0ae6cfdee4f5efbee0015d51f08bb4cc5d9635 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 14 Feb 2019 10:46:43 +0100 Subject: [PATCH 1/7] WIP: switch to LSM9D1 --- .../PhysicsLabFirmware/PhysicsLabFirmware.ino | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index 04694b4..de90528 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -17,8 +17,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include +#include // click here to install the library: http://librarymanager#ArduinoBLE +#include // click here to install the library: http://librarymanager#Adafruit_LSM9DS1 +#include // click here to install the library: http://librarymanager#Adafruit_Sensor #include "INA226.h" @@ -56,6 +57,8 @@ unsigned long lastNotify = 0; //#define DEBUG //uncomment to debug the code :) +Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1(); + void setup() { Serial.begin(9600); #ifdef DEBUG @@ -79,12 +82,16 @@ void setup() { while (1); } - if (!IMU.begin()) { + if (!imu.begin()) { Serial.println("Failled to initialized IMU!"); while (1); } + imu.setupAccel(imu.LSM9DS1_ACCELRANGE_2G); + imu.setupMag(imu.LSM9DS1_MAGGAIN_4GAUSS); + imu.setupGyro(imu.LSM9DS1_GYROSCALE_245DPS); + if (!BLE.begin()) { Serial.println("Failled to initialized BLE!"); @@ -213,7 +220,7 @@ void updateSubscribedCharacteristics() { } else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) { resistanceAvg = resistanceAuxLow; } - + #ifdef DEBUG Serial.print("Resistance (AVG): "); Serial.print(resistanceAvg); @@ -248,27 +255,35 @@ int analogReadAverage(int pin, int numberOfSamples) { } void updateSubscribedIMUCharacteristics() { + + imu.read(); + sensors_event_t a, m, g, temp; + imu.getEvent(&a, &m, &g, &temp); + if (accelerationCharacteristic.subscribed()) { float acceleration[3]; - if (IMU.accelerationAvailable() && IMU.readAcceleration(acceleration[0], acceleration[1], acceleration[2])) { - accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); - } + acceleration[0] = a.acceleration.x; + acceleration[1] = a.acceleration.y; + acceleration[2] = a.acceleration.z; + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); } if (gyroscopeCharacteristic.subscribed()) { float gyroscope[3]; - if (IMU.gyroscopeAvailable() && IMU.readGyroscope(gyroscope[0], gyroscope[1], gyroscope[2])) { - gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); - } + gyroscope[0] = g.gyro.x; + gyroscope[1] = g.gyro.y; + gyroscope[2] = g.gyro.z; + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); } if (magneticFieldCharacteristic.subscribed()) { float magneticField[3]; - if (IMU.magneticFieldAvailable() && IMU.readMagneticField(magneticField[0], magneticField[1], magneticField[2])) { - magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); - } + magneticField[0] = m.magnetic.x; + magneticField[1] = m.magnetic.y; + magneticField[2] = m.magnetic.z; + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); } } From d4118c62e68718fe49b20d3ffb40df6fb18b1f34 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 15 Feb 2019 10:11:03 +0100 Subject: [PATCH 2/7] Fix lib manager links --- examples/PhysicsLabFirmware/PhysicsLabFirmware.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index de90528..dfb2d71 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -18,8 +18,8 @@ */ #include // click here to install the library: http://librarymanager#ArduinoBLE -#include // click here to install the library: http://librarymanager#Adafruit_LSM9DS1 -#include // click here to install the library: http://librarymanager#Adafruit_Sensor +#include // click here to install the library: http://librarymanager#Adafruit&LSM9DS1 +#include // click here to install the library: http://librarymanager#Adafruit&unified&Sensor&abstraction #include "INA226.h" From e8c5e72a0ce4869af31ad7ebc1e7d8ed116ee0d8 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 15 Feb 2019 10:11:40 +0100 Subject: [PATCH 3/7] Change RESISTOR_AUX pin for new HW revision --- examples/PhysicsLabFirmware/PhysicsLabFirmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index dfb2d71..5c75d02 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -47,7 +47,7 @@ const int INPUT3_PIN = A0; const int OUTPUT1_PIN = 5; const int OUTPUT2_PIN = 1; const int RESISTANCE_PIN = A2; -const int RESISTANCE_AUX_PIN = 13; +const int RESISTANCE_AUX_PIN = 8; String name; unsigned long lastNotify = 0; From e1843c171de721bf2d6ae39f7c260fea53eb1a8d Mon Sep 17 00:00:00 2001 From: agdl Date: Fri, 8 Mar 2019 12:34:55 +0100 Subject: [PATCH 4/7] Improved resistance reading --- examples/PhysicsLabFirmware/PhysicsLabFirmware.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index 5c75d02..37148af 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -55,7 +55,7 @@ unsigned long lastNotify = 0; #define RESISTOR_AUX_LOW 47000.0 #define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm -//#define DEBUG //uncomment to debug the code :) +#define DEBUG //uncomment to debug the code :) Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1(); @@ -193,7 +193,7 @@ void updateSubscribedCharacteristics() { digitalWrite(RESISTANCE_AUX_PIN, LOW); Vout = getVoutAverage(); - if (Vout >= 0.1) { + if ((Vout >= 0.1) && (Vout <= 3.0)) { resistanceAuxLow = RESISTOR_AUX_LOW * ((3.3 / Vout) - 1); } @@ -220,6 +220,7 @@ void updateSubscribedCharacteristics() { } else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) { resistanceAvg = resistanceAuxLow; } + resistanceAvg += 0.025 * resistanceAvg; #ifdef DEBUG Serial.print("Resistance (AVG): "); From e26c9b03486d8aec9b84c4298074f73eb9c9d820 Mon Sep 17 00:00:00 2001 From: agdl Date: Mon, 11 Mar 2019 17:05:33 +0100 Subject: [PATCH 5/7] Commented debug --- examples/PhysicsLabFirmware/PhysicsLabFirmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index 37148af..a33406c 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -55,7 +55,7 @@ unsigned long lastNotify = 0; #define RESISTOR_AUX_LOW 47000.0 #define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm -#define DEBUG //uncomment to debug the code :) +//#define DEBUG //uncomment to debug the code :) Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1(); From cdf00643ebc84aa1f33b678e29e18b10d50567d5 Mon Sep 17 00:00:00 2001 From: agdl Date: Mon, 11 Mar 2019 17:13:57 +0100 Subject: [PATCH 6/7] Reduced imu update time --- .../PhysicsLabFirmware/PhysicsLabFirmware.ino | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index a33406c..7241467 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -52,9 +52,13 @@ const int RESISTANCE_AUX_PIN = 8; String name; unsigned long lastNotify = 0; +unsigned long imuTime; + #define RESISTOR_AUX_LOW 47000.0 #define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm +#define IMU_UPDATE_TIME 100 + //#define DEBUG //uncomment to debug the code :) Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1(); @@ -129,6 +133,7 @@ void setup() { BLE.addService(service); BLE.advertise(); + imuTime = millis(); } void loop() { @@ -256,35 +261,37 @@ int analogReadAverage(int pin, int numberOfSamples) { } void updateSubscribedIMUCharacteristics() { + if (millis() - imuTime > IMU_UPDATE_TIME) { + imuTime = millis(); + imu.read(); + sensors_event_t a, m, g, temp; + imu.getEvent(&a, &m, &g, &temp); + + if (accelerationCharacteristic.subscribed()) { + float acceleration[3]; + + acceleration[0] = a.acceleration.x; + acceleration[1] = a.acceleration.y; + acceleration[2] = a.acceleration.z; + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); + } - imu.read(); - sensors_event_t a, m, g, temp; - imu.getEvent(&a, &m, &g, &temp); - - if (accelerationCharacteristic.subscribed()) { - float acceleration[3]; - - acceleration[0] = a.acceleration.x; - acceleration[1] = a.acceleration.y; - acceleration[2] = a.acceleration.z; - accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); - } - - if (gyroscopeCharacteristic.subscribed()) { - float gyroscope[3]; + if (gyroscopeCharacteristic.subscribed()) { + float gyroscope[3]; - gyroscope[0] = g.gyro.x; - gyroscope[1] = g.gyro.y; - gyroscope[2] = g.gyro.z; - gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); - } + gyroscope[0] = g.gyro.x; + gyroscope[1] = g.gyro.y; + gyroscope[2] = g.gyro.z; + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); + } - if (magneticFieldCharacteristic.subscribed()) { - float magneticField[3]; + if (magneticFieldCharacteristic.subscribed()) { + float magneticField[3]; - magneticField[0] = m.magnetic.x; - magneticField[1] = m.magnetic.y; - magneticField[2] = m.magnetic.z; - magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); + magneticField[0] = m.magnetic.x; + magneticField[1] = m.magnetic.y; + magneticField[2] = m.magnetic.z; + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); + } } } From cfea0d3eba45d4e96a835fdbd7d65be0d273f96a Mon Sep 17 00:00:00 2001 From: agdl Date: Mon, 11 Mar 2019 17:39:15 +0100 Subject: [PATCH 7/7] Reviewed update time with Valentina --- examples/PhysicsLabFirmware/PhysicsLabFirmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino index 7241467..b915f05 100644 --- a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino +++ b/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -57,7 +57,7 @@ unsigned long imuTime; #define RESISTOR_AUX_LOW 47000.0 #define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm -#define IMU_UPDATE_TIME 100 +#define IMU_UPDATE_TIME 50 //#define DEBUG //uncomment to debug the code :)